예제 #1
0
        protected virtual void ParseValue(TokenWriter tokenWriter, BasicReader reader)
        {
            int startPos = reader.Position;
            if (reader.StartsWith(ListValueSeperator))
            {
                tokenWriter.Add(new Token(TokenType.ListValueSeperator, ListValueSeperator, startPos, ListValueSeperator.Length));
                reader.Skip(1);
            }
            else if (reader.StartsWith("'") || reader.StartsWith("\""))
            {
                this.ParseQuotedString(tokenWriter, reader);
            }
            else if (reader.StartsWith("$"))
            {
                TokenType type;
                char[] endChar = new char[] { ' ', '\t', '\r', '\n', ',', '}', ')', '=', ';' };

                string val = reader.ReadUntil(endChar.Contains);
                switch (val)
                {
                    case "$null":
                        type = TokenType.NullValue;
                        break;
                    case "$true":
                    case "$false":
                        type = TokenType.BoolValue;
                        break;
                    default:
                        throw new InvalidDataException("Not a literal token: " + val);
                }

                tokenWriter.Add(new Token(type, val, startPos, reader.Position - startPos));
            }
            else if (reader.StartsWith(DictionaryStart))
            {
                this.ParseDictionary(tokenWriter, reader);
            }
            else if (reader.StartsWith(ListStart))
            {
                this.ParseList(tokenWriter, reader);
            }
            else
            {
                char[] endChar = new char[] { ' ', '\t', '\r', '\n', ',', '}', ')', '=', ';' };
                string val = reader.PeekUntil(endChar.Contains);

                bool decimalSep = val.Any(c => c == '.');

                int skipSign = val[0] == '+' || val[0] == '-' ? 1 : 0;

                if (val.Skip(skipSign).Count(char.IsDigit) + (decimalSep ? 1 : 0) == val.Length - skipSign)
                {
                    reader.Skip(val.Length);

                    tokenWriter.Add(new Token(TokenType.NumericValue, val, startPos, reader.Position - startPos));
                }
                else
                    this.ParseUnquotedString(tokenWriter, reader);
            }
        }
예제 #2
0
        private void ParseOne(TokenWriter tokenWriter, BasicReader reader)
        {
            reader.AdvanceWhitespace();

            if (reader.StartsWith(ParameterIndicator))
                this.ParseParameter(tokenWriter, reader);
            else
                this.ParseValue(tokenWriter, reader);
        }
예제 #3
0
        protected virtual void ParseParameter(TokenWriter tokenWriter, BasicReader reader)
        {
            if (!reader.StartsWith(ParameterIndicator))
                throw new InvalidOperationException(String.Format("Cannot parse paramer, expected {0}.", ParameterIndicator));

            reader.Skip(1);

            int startPos = reader.Position;
            StringBuilder name = new StringBuilder();
            char next;
            while (reader.TryPeek(out next) && next != ':' && !char.IsWhiteSpace(next))
            {
                name.Append(reader.Read());
            }

            if (!reader.Eof && next == ':')
            {
                tokenWriter.Add(new Token(TokenType.SwitchParameter, name.ToString(), startPos, reader.Position - startPos));
                reader.Skip(1); // skip the :
                this.ParseOne(tokenWriter, reader);
            }
            else
                tokenWriter.Add(new Token(TokenType.ParameterName, name.ToString(), startPos, reader.Position - startPos));
        }
예제 #4
0
        protected virtual void ParseDictionary(TokenWriter tokenWriter, BasicReader reader)
        {
            if (!reader.StartsWith(DictionaryStart))
                throw new InvalidOperationException(String.Format("Cannot parse dictionary, expected {0}.", DictionaryStart));

            tokenWriter.Add(new Token(TokenType.DictionaryStart, DictionaryStart, reader.Position, DictionaryStart.Length));                    

            // skip
            reader.Skip(DictionaryStart.Length);

            while (!reader.StartsWith(DictionaryEnd) && !reader.Eof)
            {
                // trim ws
                reader.AdvanceWhitespace();

                // parse dict-key
                this.ParseValue(tokenWriter, reader);

                // trim ws
                reader.AdvanceWhitespace();

                // read seperator
                if (reader.StartsWith(DictionaryValueSeperator))
                {
                    // we no longer emit these tokens
                    ////tokenWriter.Add(new Token
                    ////                    {
                    ////                        Position = reader.Position,
                    ////                        Length = DictionaryValueSeperator.Length,
                    ////                        Type = TokenType.DictionaryValueSeperator,
                    ////                        Value = DictionaryValueSeperator
                    ////                    });

                    reader.Skip(DictionaryValueSeperator.Length);
                }
                else
                {
                    throw new Exception("Expected key seperator");
                }

                // trim ws
                reader.AdvanceWhitespace();

                // parse dict-value
                this.ParseValue(tokenWriter, reader);

                // trim ws
                reader.AdvanceWhitespace();

                // read seperator
                if (reader.StartsWith(DictionaryKeySeperator))
                {
                    // we no longer emit these tokens
                    ////tokenWriter.Add(new Token
                    ////                    {
                    ////                        Position = reader.Position,
                    ////                        Length = DictionaryKeySeperator.Length,
                    ////                        Type = TokenType.DictionaryKeySeperator,
                    ////                        Value = DictionaryKeySeperator
                    ////                    });

                    reader.Skip(DictionaryKeySeperator.Length);
                }
                else
                    break;
            }

            if (!reader.Eof && reader.StartsWith(DictionaryEnd))
            {
                tokenWriter.Add(new Token(TokenType.DictionaryEnd, DictionaryEnd, reader.Position, DictionaryEnd.Length));
                                    
                reader.Skip(DictionaryEnd.Length);
            }
            else
            {
                throw new Exception("Expected end of dictionary");
            }
        }
예제 #5
0
        protected virtual void ParseList(TokenWriter tokenWriter, BasicReader reader)
        {
            if (!reader.StartsWith(ListStart))
                throw new InvalidOperationException(String.Format("Cannot parse list, expected {0}.", ListStart));


            tokenWriter.Add(new Token(TokenType.ListStart, ListStart, reader.Position, ListStart.Length));
            
            // skip it
            reader.Skip(ListStart.Length);

            // trim ws
            reader.AdvanceWhitespace();

            while (!reader.StartsWith(ListEnd) && !reader.Eof)
            {
                // parse list value
                this.ParseValue(tokenWriter, reader);

                // trim ws
                reader.AdvanceWhitespace();

                // read seperator
                if (reader.StartsWith(ListValueSeperator))
                {
                    // don't output these anymore
                    ////tokenWriter.Add(new Token
                    ////                    {
                    ////                        Position = reader.Position,
                    ////                        Length = ListValueSeperator.Length,
                    ////                        Type = TokenType.ListValueSeperator,
                    ////                        Value = ListValueSeperator
                    ////                    });

                    reader.Skip(ListValueSeperator.Length);
                }
                else if (!reader.StartsWith(ListEnd))
                {
                    throw new Exception("Expected value seperator or end of list.");
                }

                // trim ws
                reader.AdvanceWhitespace();
            }



            if (reader.StartsWith(ListEnd))
            {
                tokenWriter.Add(new Token(TokenType.ListEnd, ListEnd, reader.Position, ListEnd.Length));
                    
                reader.Skip(ListEnd.Length);
            }
            else
            {
                throw new Exception("Expected end of list");
            }
        }
예제 #6
0
        protected virtual void ParseDictionary(TokenWriter tokenWriter, BasicReader reader)
        {
            if (!reader.StartsWith(DictionaryStart))
                throw new InvalidOperationException($"Cannot parse dictionary, expected {DictionaryStart}.");

            tokenWriter.Add(new Token(TokenType.DictionaryStart, DictionaryStart, reader.Position, DictionaryStart.Length));

            // skip
            reader.Skip(DictionaryStart.Length);

            while (!reader.StartsWith(DictionaryEnd) && !reader.Eof)
            {
                // trim ws
                reader.AdvanceWhitespace();

                // parse dict-key
                this.ParseValue(tokenWriter, reader);

                // trim ws
                reader.AdvanceWhitespace();

                // read seperator
                if (reader.StartsWith(DictionaryValueSeperator))
                    reader.Skip(DictionaryValueSeperator.Length);
                else
                    throw new Exception("Expected key seperator");

                // trim ws
                reader.AdvanceWhitespace();

                // parse dict-value
                this.ParseValue(tokenWriter, reader);

                // trim ws
                reader.AdvanceWhitespace();

                // read seperator
                if (reader.StartsWith(DictionaryKeySeperator))
                    reader.Skip(DictionaryKeySeperator.Length);
                else
                    break;
            }

            if (!reader.Eof && reader.StartsWith(DictionaryEnd))
            {
                tokenWriter.Add(new Token(TokenType.DictionaryEnd, DictionaryEnd, reader.Position, DictionaryEnd.Length));

                reader.Skip(DictionaryEnd.Length);
            }
            else
                throw new Exception("Expected end of dictionary");
        }