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); }
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; }
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)); }
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); } }
private BrunoExpression ParseGrouping() { if (!IsNextPunctuation(OpenParen)) { return(null); } _lexer.Next(); BrunoExpression ret = Parenthesis(ParseExpression()); SkipPunctuation(CloseParen); return(ret); }
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)); }
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; }
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 }; }
private ExtractVisitor([NotNull] BrunoExpression expression, [NotNull] Func <BrunoExpression, bool> extractAction) { _expression = expression ?? throw new ArgumentNullException(nameof(expression)); _predicate = extractAction ?? throw new ArgumentNullException(nameof(extractAction)); }
public void Deconstruct(out string name, out BrunoExpression p0) { name = Name; p0 = Arguments.Any() ? Arguments.ElementAt(0) : null; }
public static BrunoExpression AcceptChildrenClone(this IVisitor <BrunoExpression> visitor, BrunoExpression expression) => expression switch {
public BrunoAccessor(string name) { Name = name; Children = new BrunoExpression[0]; }
public object Visit(BrunoExpression subject) => subject switch {
public BrunoVariable(string name) { Name = name; Children = new BrunoExpression[0]; }
public BrunoDot(BrunoExpression subject, BrunoExpression accessor) { Subject = subject; Accessor = accessor; Children = new[] { subject }; }
public static IEnumerable <BrunoExtractResult> Extract([NotNull] BrunoExpression expression, [NotNull] Func <BrunoExpression, bool> predicate) => new ExtractVisitor(expression, predicate).Extract();
public BrunoNumber(double value) { Value = value; Children = new BrunoExpression[0]; }
public BrunoParenthesis(BrunoExpression body) { Body = body; Children = new[] { body }; }
public BrunoExtractResult(BrunoExpression parent, int depth, [NotNull] BrunoExpression node) { Parent = parent; Depth = depth; Node = node ?? throw new ArgumentNullException(nameof(node)); }
public BrunoString(string value) { Value = value; Children = new BrunoExpression[0]; }