Esempio n. 1
0
        internal override bool Parse(ParseContext context, ParseRule rule, out Token result)
        {
            SequenceMemento state;
            if (context.Recovering)
                state = (SequenceMemento)context.Recover();
            else
                state = new SequenceMemento(context);

            context.BeginExpression(state);

            // Parse each subexpression to find nested tokens
            for (; state.Index < ExpressionCount; ++state.Index) {
                var expr = Expression(state.Index);
                Token t;

                // Parsing failed on a subexpression - return an overall failure
                if (!expr.Parse(context, rule, out t)) {
                    context.Offset = state.Offset;
                    context.EndExpression();
                    result = t;
                    return false;
                }

                // Skip blank tokens
                if (t.Blank)
                    continue;

                // Add the parsed token to our result
                state.Result.Add(t);
            }

            if (state.Result.HasChildren)
                state.Result.EndOffset = context.Offset;

            context.EndExpression();

            result = state.Result;
            return true;
        }
Esempio n. 2
0
        internal override bool Parse(ParseContext context, ParseRule rule, out Token result)
        {
            OrderedChoiceMemento state;
            if (context.Recovering)
                state = (OrderedChoiceMemento)context.Recover();
            else
                state = new OrderedChoiceMemento(context);

            context.BeginExpression(state);

            // Parse each subexpression until the first match
            for (; state.index < ExpressionCount; ++state.index) {
                var expr = Expression(state.index);
                Token t;

                // Parsing succeeded on a subexpression - return the result
                if (expr.Parse(context, rule, out t)) {
                    context.EndExpression();
                    result = t;
                    return true;
                }

                context.Offset = state.offset;
            }

            context.EndExpression();

            return context.Reject(rule, this, out result);
        }