Advertisement
Guest User

How to read TML the clean way :P

a guest
Mar 31st, 2020
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.74 KB | None | 0 0
  1.         public bool GetTemplateInfo(KStream stream, ref KTemplateInfo info, ref int linecount)
  2.         {
  3.             List<Template> templates = new List<Template>();
  4.  
  5.             do
  6.             {
  7.                 Template template = new Template();
  8.  
  9.                 moveToNext(CharType.Char); // file usually begins with \n | second+ read will move our position to the next reserved word
  10.                 string resWord = readWord(CharType.Char);
  11.  
  12.                 if (isReservedWord(resWord) && resWord == "template")
  13.                 {
  14.                     template.Name = readWord(CharType.OBracket); // template name
  15.  
  16.                     if (string.IsNullOrEmpty(template.Name))
  17.                         throw new InvalidDataException("block_name cannot be empty!");
  18.  
  19.                     try
  20.                     {
  21.                         resWord = readWord(CharType.Char); // template guid
  22.                         template.Guid = new System.Guid(resWord);
  23.                     }
  24.                     catch (Exception ex) { throw ex; }
  25.  
  26.                     do
  27.                     {
  28.                         TemplateMember tMem = new TemplateMember();
  29.                         tMem.Type = MemberType.Char;
  30.  
  31.                         resWord = readWord(CharType.Char);
  32.  
  33.                         if (string.IsNullOrEmpty(resWord))
  34.                             throw new InvalidDataException("resWord cannot be empty!");
  35.  
  36.                         if (!isReservedWord(resWord))
  37.                             tMem.Type = MemberType.Template;
  38.                         else
  39.                         {
  40.                             int rwIdx = reserved_word.FindIndex(rW => rW == resWord);
  41.  
  42.                             if (rwIdx == -1)
  43.                                 throw new Exception($"Could not find the index of reserved word: {resWord}");
  44.  
  45.                             tMem.Type = (MemberType)rwIdx;
  46.                         }
  47.  
  48.                         tMem.Name = readDelimitedWord(new List<char>() { '[', ';' });
  49.  
  50.                         char delimiter = currentDelimiterType; //We must get our current delimiter char since we did not move!
  51.                         bool breakLoop = false;
  52.  
  53.                         switch (delimiter)
  54.                         {
  55.                             case '[': // member has array
  56.                                 {
  57.                                     //TODO: read the next delimited word by ']' and assign it as the value then goto case ';'
  58.                                     string arrayVal = readDelimitedWord(new List<char>() { ']' });
  59.  
  60.                                     int aVal = default;
  61.                                     if (int.TryParse(arrayVal, out aVal))
  62.                                         tMem.ArrayVal = aVal;
  63.                                     else
  64.                                         tMem.ArrayVal = arrayVal;
  65.  
  66.                                     if (readChar() == ';')
  67.                                         goto case ';';
  68.                                 }
  69.                                 break;
  70.  
  71.                             case ';':
  72.                                 {
  73.                                     if (peekNextNonWS() == CharType.CBracket)
  74.                                     {
  75.                                         breakLoop = true;
  76.                                         break;
  77.                                     }                                  
  78.                                 }
  79.                                 break;
  80.                         }
  81.  
  82.                         template.Members.Add(tMem);
  83.  
  84.                         if (breakLoop) // we have hit the end of this block
  85.                             break;
  86.                     }
  87.                     while (peek() != CharType.NewLine); //Since we are manually parsing it is ok to assume that each new line by the time we peek() seperates current and new block
  88.  
  89.                     templates.Add(template);
  90.                 }
  91.                 else
  92.                     throw new InvalidDataException("Unexpected word in stream!");
  93.             }
  94.             while (moveToNext(CharType.Char) != -1);
  95.  
  96.             return false;
  97.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement