Пример #1
0
        /// <inheritdoc />
        public Option <TExpressionNode> Parse(
            IPrattParser <TTokenType, TExpressionNode> parser,
            DiagnosticList diagnostics)
        {
            if (parser is null)
            {
                throw new ArgumentNullException(nameof(parser));
            }
            if (diagnostics is null)
            {
                throw new ArgumentNullException(nameof(diagnostics));
            }

            return(_factory(parser.TokenReader.Consume(), diagnostics));
        }
        /// <inheritdoc />
        public Option <TExpressionNode> Parse(
            IPrattParser <TTokenType, TExpressionNode> parser,
            DiagnosticList diagnostics)
        {
            if (parser is null)
            {
                throw new ArgumentNullException(nameof(parser));
            }
            if (diagnostics is null)
            {
                throw new ArgumentNullException(nameof(diagnostics));
            }

            var prefix = parser.TokenReader.Consume();

            return(parser.ParseExpression(_precedence)
                   .AndThen(expression => _factory(prefix, expression, diagnostics)));
        }
Пример #3
0
        /// <inheritdoc />
        public Option <TExpressionNode> Parse(
            IPrattParser <TTokenType, TExpressionNode> parser,
            TExpressionNode expression,
            DiagnosticList diagnostics)
        {
            if (parser is null)
            {
                throw new ArgumentNullException(nameof(parser));
            }
            if (expression is null)
            {
                throw new ArgumentNullException(nameof(expression));
            }
            if (diagnostics is null)
            {
                throw new ArgumentNullException(nameof(diagnostics));
            }

            var op = parser.TokenReader.Consume();

            // We decrease the precedence by one on right-associative operators because the minimum
            // precedence passed to TryParseExpression is exclusive (meaning that the precedence of the
            // infix parselets must be higher than the one we pass it.
            // TODO: Check if this cannot create bugs with other operators that have the same precedence.
            int minPrecedence;

            if (_isRightAssociative)
            {
                minPrecedence = Precedence - 1;
            }
            else
            {
                minPrecedence = Precedence;
            }

            return(parser.ParseExpression(minPrecedence)
                   .AndThen(nextExpr => _factory(expression, op, nextExpr)));
        }