Esempio n. 1
0
        private IEnumerable <BrunoExpression> ParseDelimited(char begin, char separator, char end)
        {
            List <BrunoExpression> ret = new();

            SkipPunctuation(begin);

            while (!_lexer.IsEnd())
            {
                if (IsNextPunctuation(end))
                {
                    break;
                }

                BrunoExpression argExp = ParseExpression();
                ret.Add(argExp);

                if (IsNextPunctuation(separator))
                {
                    SkipPunctuation(separator);
                }
            }

            SkipPunctuation(end);

            return(ret);
        }
Esempio n. 2
0
 public void Deconstruct(out string name,
                         out BrunoExpression arg0,
                         out BrunoExpression arg1)
 {
     name = Name;
     arg0 = Arguments.Any() ? Arguments.ElementAt(0) : null;
     arg1 = Arguments.Count() > 1 ? Arguments.ElementAt(1) : null;
 }
Esempio n. 3
0
        private BrunoExpression ParseExpression()
        {
            BrunoExpression ret = ParseLiteral()
                                  ?? ParseGrouping()
                                  ?? ParseIdentifier()
                                  ?? throw new Exception($"Unexpected token: {_lexer.Peek()?.ToString() ?? "<null>"} {_input.Location}");

            ret = ParseDot(ret);
            return(ParseOperator(ret));
        }
Esempio n. 4
0
        public static void AcceptChildren(this IVisitor visitor, BrunoExpression expression)
        {
            if (expression.Children == null || !expression.Children.Any())
            {
                return;
            }

            foreach (BrunoExpression child in expression.Children)
            {
                child.Accept(visitor);
            }
        }
Esempio n. 5
0
        private BrunoExpression ParseGrouping()
        {
            if (!IsNextPunctuation(OpenParen))
            {
                return(null);
            }

            _lexer.Next();
            BrunoExpression ret = Parenthesis(ParseExpression());

            SkipPunctuation(CloseParen);

            return(ret);
        }
Esempio n. 6
0
        private BrunoExpression ParseDot(BrunoExpression left)
        {
            if (!IsNextPunctuation(Period))
            {
                return(left);
            }

            LexToken token = _lexer.Next();

            if (!IsNextIdentifier())
            {
                throw new Exception($"Unexpected token after dot: {token} {_input.Location}");
            }

            BrunoExpression accessor = ParseExpression();

            return(Dot(left, accessor));
        }
Esempio n. 7
0
        public void Visit(BrunoExpression node)
        {
            if (_predicate(node))
            {
                _results.Add(new BrunoExtractResult(_parent, _depth, node));
            }

            if (node.Children == null || !node.Children.Any())
            {
                return;
            }

            _parent = node;
            _depth++;
            this.AcceptChildren(node);
            _depth--;
            _parent = null;
        }
Esempio n. 8
0
        public BrunoDivide(BrunoExpression left, BrunoExpression right)
        {
            if (left is IBrunoOperator ileft && ileft.Precedence < Precedence)
            {
                left = Parenthesis(left);
            }

            if (right is IBrunoOperator iright && iright.Precedence < Precedence)
            {
                right = Parenthesis(right);
            }

            Left     = left;
            Right    = right;
            Children = new[] {
                left,
                right
            };
        }
Esempio n. 9
0
 private ExtractVisitor([NotNull] BrunoExpression expression, [NotNull] Func <BrunoExpression, bool> extractAction)
 {
     _expression = expression ?? throw new ArgumentNullException(nameof(expression));
     _predicate  = extractAction ?? throw new ArgumentNullException(nameof(extractAction));
 }
Esempio n. 10
0
 public void Deconstruct(out string name,
                         out BrunoExpression p0)
 {
     name = Name;
     p0   = Arguments.Any() ? Arguments.ElementAt(0) : null;
 }
Esempio n. 11
0
 public static BrunoExpression AcceptChildrenClone(this IVisitor <BrunoExpression> visitor, BrunoExpression expression)
 => expression switch
 {
Esempio n. 12
0
 public BrunoAccessor(string name)
 {
     Name     = name;
     Children = new BrunoExpression[0];
 }
Esempio n. 13
0
 public object Visit(BrunoExpression subject)
 => subject switch
 {
Esempio n. 14
0
 public BrunoVariable(string name)
 {
     Name     = name;
     Children = new BrunoExpression[0];
 }
Esempio n. 15
0
 public BrunoDot(BrunoExpression subject, BrunoExpression accessor)
 {
     Subject  = subject;
     Accessor = accessor;
     Children = new[] { subject };
 }
Esempio n. 16
0
 public static IEnumerable <BrunoExtractResult> Extract([NotNull] BrunoExpression expression,
                                                        [NotNull] Func <BrunoExpression, bool> predicate)
 => new ExtractVisitor(expression, predicate).Extract();
Esempio n. 17
0
 public BrunoNumber(double value)
 {
     Value    = value;
     Children = new BrunoExpression[0];
 }
Esempio n. 18
0
 public BrunoParenthesis(BrunoExpression body)
 {
     Body     = body;
     Children = new[] { body };
 }
Esempio n. 19
0
 public BrunoExtractResult(BrunoExpression parent, int depth, [NotNull] BrunoExpression node)
 {
     Parent = parent;
     Depth  = depth;
     Node   = node ?? throw new ArgumentNullException(nameof(node));
 }
Esempio n. 20
0
 public BrunoString(string value)
 {
     Value    = value;
     Children = new BrunoExpression[0];
 }