Пример #1
0
        public static ExpressionNode Parse(Reader r)
        {
            var nodes = new List<ExpressionNode>();
            var state = State.Start;

            while (!r.End && state != State.End)
            {
                switch (state)
                {
                    case State.Start:
                        state = ParseStart(r, nodes);
                        break;

                    case State.AfterMember:
                        state = ParseAfterMember(r, nodes);
                        break;

                    case State.BeforeMember:
                        state = ParseBeforeMember(r, nodes);
                        break;
                }
            }

            if (state == State.BeforeMember)
            {
                throw new ExpressionParseException(r.Position, "Unexpected end of expression.");
            }

            for (int n = 0; n < nodes.Count - 1; ++n)
            {
                nodes[n].Next = nodes[n + 1];
            }

            return nodes.FirstOrDefault();
        }
Пример #2
0
        public static IList<object> Parse(Reader r, char open, char close)
        {
            if (r.Peek == open)
            {
                var result = new List<object>();

                r.Take();

                while (!r.End)
                {
                    var literal = LiteralParser.Parse(r);

                    if (literal != null)
                    {
                        result.Add(literal);
                    }
                    else
                    {
                        throw new ExpressionParseException(r.Position, "Expected integer.");
                    }

                    r.SkipWhitespace();

                    if (r.End)
                    {
                        throw new ExpressionParseException(r.Position, "Expected ','.");
                    }
                    else if (r.TakeIf(close))
                    {
                        return result;
                    }
                    else
                    {
                        if (r.Take() != ',')
                        {
                            throw new ExpressionParseException(r.Position, "Expected ','.");
                        }

                        r.SkipWhitespace();
                    }
                }

                if (!r.End)
                {
                    r.Take();
                    return result;
                }
                else
                {
                    throw new ExpressionParseException(r.Position, "Expected ']'.");
                }
            }

            return null;
        }
Пример #3
0
        public static IList<string> Parse(Reader r, char open, char close)
        {
            if (r.Peek == open)
            {
                var result = new List<string>();

                r.Take();

                while (!r.End)
                {
                    var builder = new StringBuilder();
                    while (!r.End && r.Peek != ',' && r.Peek != close && !char.IsWhiteSpace(r.Peek))
                    {
                        builder.Append(r.Take());
                    }
                    if (builder.Length == 0)
                    {
                        throw new ExpressionParseException(r.Position, "Expected indexer argument.");
                    }
                    result.Add(builder.ToString());

                    r.SkipWhitespace();

                    if (r.End)
                    {
                        throw new ExpressionParseException(r.Position, "Expected ','.");
                    }
                    else if (r.TakeIf(close))
                    {
                        return result;
                    }
                    else
                    {
                        if (r.Take() != ',')
                        {
                            throw new ExpressionParseException(r.Position, "Expected ','.");
                        }

                        r.SkipWhitespace();
                    }
                }

                if (!r.End)
                {
                    r.Take();
                    return result;
                }
                else
                {
                    throw new ExpressionParseException(r.Position, "Expected ']'.");
                }
            }

            return null;
        }
Пример #4
0
        public static ExpressionNode Build(string expression)
        {
            if (string.IsNullOrWhiteSpace(expression))
            {
                throw new ArgumentException("'expression' may not be empty.");
            }

            var reader = new Reader(expression);
            var node = ExpressionParser.Parse(reader);

            if (!reader.End)
            {
                throw new ExpressionParseException(reader.Position, "Expected end of expression.");
            }

            return node;
        }
Пример #5
0
        public static string Parse(Reader r)
        {
            if (IsValidIdentifierStart(r.Peek))
            {
                var result = new StringBuilder();

                while (!r.End && IsValidIdentifierChar(r.Peek))
                {
                    result.Append(r.Take());
                }

                return result.ToString();
            }
            else
            {
                return null;
            }
        }
Пример #6
0
        private static State ParseStart(Reader r, IList<ExpressionNode> nodes)
        {
            if (ParseNot(r))
            {
                nodes.Add(new LogicalNotNode());
                return State.Start;
            }
            else
            {
                var identifier = IdentifierParser.Parse(r);

                if (identifier != null)
                {
                    nodes.Add(new PropertyAccessorNode(identifier));
                    return State.AfterMember;
                }
            }

            return State.End;
        }
Пример #7
0
        public static object Parse(Reader r)
        {
            if (char.IsDigit(r.Peek))
            {
                StringBuilder result = new StringBuilder();

                while (!r.End)
                {
                    if (char.IsDigit(r.Peek))
                    {
                        result.Append(r.Take());
                    }
                    else
                    {
                        break;
                    }
                }

                return int.Parse(result.ToString(), CultureInfo.InvariantCulture);
            }

            return null;
        }
Пример #8
0
        private static State ParseAfterMember(Reader r, IList<ExpressionNode> nodes)
        {
            if (ParseMemberAccessor(r))
            {
                return State.BeforeMember;
            }
            else
            {
                var args = ArgumentListParser.Parse(r, '[', ']');

                if (args != null)
                {
                    if (args.Count == 0)
                    {
                        throw new ExpressionParseException(r.Position, "Indexer may not be empty.");
                    }

                    nodes.Add(new IndexerNode(args));
                    return State.AfterMember;
                }
            }

            return State.End;
        }
Пример #9
0
        private static State ParseBeforeMember(Reader r, IList<ExpressionNode> nodes)
        {
            var identifier = IdentifierParser.Parse(r);

            if (identifier != null)
            {
                nodes.Add(new PropertyAccessorNode(identifier));
                return State.AfterMember;
            }

            return State.End;
        }
Пример #10
0
 private static bool ParseMemberAccessor(Reader r)
 {
     return !r.End && r.TakeIf('.');
 }
Пример #11
0
 private static bool ParseNot(Reader r)
 {
     return !r.End && r.TakeIf('!');
 }
 internal ExpressionParseException(Reader r, string message)
     : this(r.Position, message)
 {
 }