Пример #1
0
        private string GetOctalString(CharReader charReader)
        {
            var bytes       = new List <byte>();
            var textBuilder = new StringBuilder();

            while (true)
            {
                var peekedChar = charReader.PeekChar();
                if (IsBaseX(peekedChar, 8))
                {
                    textBuilder.Append(charReader.ReadChar());
                    if (textBuilder.Length == 3)
                    {
                        bytes.Add(Convert.ToByte(textBuilder.ToString(), 8));
                        textBuilder.Clear();
                    }
                }
                else
                {
                    break;
                }
            }
            if (textBuilder.Length > 0)
            {
                bytes.Add(Convert.ToByte(textBuilder.ToString(), 8));
            }
            return(Encoding.UTF8.GetString(bytes.ToArray()));
        }
Пример #2
0
 private bool ProcessProcedure(CharReader charReader)
 {
     if (charReader.PeekChar() == '{')
     {
         FlushTokenBuffer();
         charReader.ReadChar();
         OnToken?.Invoke(TokenType.BeginProcedure, "");
         return(true);
     }
     if (charReader.PeekChar() == '}')
     {
         FlushTokenBuffer();
         charReader.ReadChar();
         OnToken?.Invoke(TokenType.EndProcedure, "");
         return(true);
     }
     return(false);
 }
Пример #3
0
 private bool ProcessArray(CharReader charReader)
 {
     if (charReader.PeekChar() == '[')
     {
         FlushTokenBuffer();
         charReader.ReadChar();
         OnToken?.Invoke(TokenType.BeginArray, "");
         return(true);
     }
     if (charReader.PeekChar() == ']')
     {
         FlushTokenBuffer();
         charReader.ReadChar();
         OnToken?.Invoke(TokenType.EndArray, "");
         return(true);
     }
     return(false);
 }
Пример #4
0
        private bool ProcessHexString(CharReader charReader)
        {
            if (charReader.PeekChar() != '<')
            {
                return(false);
            }
            FlushTokenBuffer();
            charReader.ReadChar();
            var textBuilder = new StringBuilder();

            while (true)
            {
                var character = charReader.ReadChar();
                if (character == '>')
                {
                    OnToken?.Invoke(TokenType.HexString, textBuilder.ToString());
                    return(true);
                }
                else if (character == '\0')
                {
                    throw new Exception("Unexpected end of data.");
                }
                else if (IsBaseX(character, 16))
                {
                    textBuilder.Append(character);
                }
                else if (IsWhitespace(character))
                {
                    // Skip new line
                }
                else
                {
                    throw new Exception("Invalid hex charcter.");
                }
            }
        }
Пример #5
0
 private void ProcessBuffer(CharReader charReader)
 {
     while (charReader.PeekChar() != '\0')
     {
         if (ProcessComment(charReader))
         {
             continue;
         }
         else if (ProcessXPacket(charReader))
         {
             continue;
         }
         else if (ProcessDictionary(charReader))
         {
             continue;
         }
         else if (ProcessArray(charReader))
         {
             continue;
         }
         else if (ProcessProcedure(charReader))
         {
             continue;
         }
         else if (ProcessString(charReader))
         {
             continue;
         }
         else if (ProcessHexString(charReader))
         {
             continue;
         }
         else
         {
             var character = charReader.ReadChar();
             if (!IsWhitespace(character))
             {
                 _stringBuilder.Append(character);
             }
             else
             {
                 FlushTokenBuffer();
             }
         }
     }
     FlushTokenBuffer();
 }
Пример #6
0
        private bool ProcessString(CharReader charReader)
        {
            if (charReader.PeekChar() != '(')
            {
                return(false);
            }
            int bracketCount = 1;

            FlushTokenBuffer();
            charReader.ReadChar();
            var textBuilder = new StringBuilder();

            while (true)
            {
                var character = charReader.ReadChar();
                if (character == '\\')
                {
                    var peekedChar = charReader.PeekChar();
                    if (IsBaseX(peekedChar, 8))
                    {
                        var octalString = GetOctalString(charReader);
                        textBuilder.Append(octalString);
                        continue;
                    }
                    if (peekedChar == '\\' || peekedChar == '(' || peekedChar == ')')
                    {
                        textBuilder.Append(charReader.ReadChar());
                        continue;
                    }
                    if (peekedChar == 'n')
                    {
                        charReader.ReadChar();
                        textBuilder.Append('\n');
                        continue;
                    }
                    if (peekedChar == 'r')
                    {
                        charReader.ReadChar();
                        textBuilder.Append('\r');
                        continue;
                    }
                    if (peekedChar == 't')
                    {
                        charReader.ReadChar();
                        textBuilder.Append('\t');
                        continue;
                    }
                    if (peekedChar == 'b')
                    {
                        charReader.ReadChar();
                        textBuilder.Append('\b');
                        continue;
                    }
                    if (peekedChar == 'f')
                    {
                        charReader.ReadChar();
                        textBuilder.Append('\f');
                        continue;
                    }
                    throw new Exception("Unexpected escape code.");
                }
                else if (character == '(')
                {
                    bracketCount++;
                }
                else if (character == ')' && bracketCount > 1)
                {
                    bracketCount--;
                }
                else if (character == ')' && bracketCount == 1)
                {
                    OnToken?.Invoke(TokenType.String, textBuilder.ToString());
                    return(true);
                }
                else if (character == '\0')
                {
                    throw new Exception("Unexpected end of data.");
                }
                textBuilder.Append(character);
            }
        }