Esempio n. 1
0
        public ParseResult <TTokenTypeName, TActionResult> Parse(IReadOnlyList <TokenMatch <TTokenTypeName> > inputTokens, int startIndex, bool mustConsumeTokens)
        {
            int nextParsePosition = startIndex;
            int totalMatchLength  = 0;
            var subActionResults  = new List <TActionResult>();

            foreach (var sequenceElement in _sequenceDefinition)
            {
                var currentResult = sequenceElement.Parse(inputTokens, nextParsePosition, mustConsumeTokens);
                // TODO: Adaptive parsing - skip failed sub results
                if (!currentResult.Succeed)
                {
                    return(currentResult);
                }
                subActionResults.Add(currentResult.SemanticActionResult);
                nextParsePosition = currentResult.NextParseStartIndex;
                totalMatchLength += currentResult.MatchedTokens.MatchedIndices.Length;
            }
            TActionResult actionResult = default;
            var           match        = new TokensMatch <TTokenTypeName>(inputTokens, new MatchRange(startIndex, totalMatchLength));

            if (_matchAction != null && mustConsumeTokens)
            {
                actionResult = _matchAction(match, subActionResults, ToParserSpec.Value);
            }
            return(ParseResult <TTokenTypeName, TActionResult> .Succeeded(nextParsePosition, match, actionResult));
        }
        public OrderedChoiceTests()
        {
            var semanticActions = peg.DefaultSemanticActions;

            semanticActions.OrderedChoiceAction = (matchedTokens, subResults, matchedPeg) => {
                this.matchedTokens = matchedTokens;
                return(subResults.First());
            };
        }
        public OptionalTests()
        {
            var semanticActions = peg.DefaultSemanticActions;

            semanticActions.SetTerminalAction(TokenNames.A, (tokens, _, matchedPeg) => tokens.GetMatchedString());
            semanticActions.OptionalAction = (matchedTokenRange, subResults, matchedPeg) =>
            {
                matchedTokenRangeAssert = matchedTokenRange;
                subActionResults        = subResults;
                return(matchedTokenRange.GetMatchedString());
            };
        }
        public ZeroOrMoreTests()
        {
            var semanticActions = peg.DefaultSemanticActions;

            semanticActions.ZeroOrMoreAction = (tokensMatch, subResults, matchedPeg) =>
            {
                matchedTokens    = tokensMatch;
                subActionResults = subResults;
                return(tokensMatch.GetMatchedString());
            };
            semanticActions.SetTerminalAction(TokenNames.A, (match, _, matchedPeg) => match.GetMatchedString());
            semanticActions.SetTerminalAction(TokenNames.B, (match, _, matchedPeg) => match.GetMatchedString());
            semanticActions.OrderedChoiceAction = (_, subResults, matchedPeg) => subResults.Single();
        }
Esempio n. 5
0
        public SequenceTest()
        {
            var semanticActions      = peg.DefaultSemanticActions;
            var extractValueTerminal = semanticActions.SemanticAction((matchedTokenRange, _, matchedPeg) => matchedTokenRange.GetMatchedString());

            semanticActions.SetTerminalAction(TokenNames.A, extractValueTerminal);
            semanticActions.SetTerminalAction(TokenNames.B, extractValueTerminal);
            semanticActions.SequenceAction = (matchedTokenRange, subResults, matchedPeg) =>
            {
                // Extract string result of matching the Terminal symbol
                matchedTokenRangeAssert = matchedTokenRange;
                subActionResults        = subResults.ToList();
                return(matchedTokenRange.GetMatchedString());
            };
        }