예제 #1
0
        public static RuleCallbackPair Token(Action <Token> onMatched)
        {
            var rule = new SingleTokenParser(t => RuleResult.Complete(t));

            return(new RuleCallbackPair(() => rule, Wrapper));

            void Wrapper(object node)
            {
                onMatched((Token)node);
            }
        }
예제 #2
0
        public static RuleCallbackPair Token(TokenType type, string content)
        {
            var rule = new SingleTokenParser(t =>
            {
                if (t.Type == type && t.Content == content)
                {
                    return(RuleResult.Complete(t));
                }

                string errMsg = $"Expected the {type} \"{content}\", but got the {t.Type} \"{t.Content}\"";
                return(RuleResult.Failed(t.Position, errMsg));
            });

            return(new RuleCallbackPair(() => rule, BlankCallback));

            void BlankCallback(object node)
            {
            }
        }
예제 #3
0
        public RuleResult FeedToken(Token t)
        {
            if (_isFinished)
            {
                throw new Exception("Tried to feed a token to an already-finished ThenChainParser.");
            }

            var currentCallabck = _ruleSequence[_currentRuleIndex].onMatched;

            // HACK: Create the first rule, if it hasn't been already.
            if (_currentRule == null)
            {
                _currentRule = _ruleSequence[_currentRuleIndex].ruleFactory();
            }

            // Feed the token to the current rule
            var result = _currentRule.FeedToken(t);

            // If it failed, cascade that failure upwards
            if (result.status == RuleStatus.Failed)
            {
                return(result);
            }

            // If it succeeded, invoke the callback and move to the next rule
            if (result.status == RuleStatus.Complete)
            {
                currentCallabck(result.node);
                _currentRuleIndex++;

                // If the last rule just succeeded, the whole chain is complete.
                if (_currentRuleIndex >= _ruleSequence.Count)
                {
                    _isFinished = true;
                    return(RuleResult.Complete(_node));
                }
                _currentRule = _ruleSequence[_currentRuleIndex].ruleFactory();
            }

            return(RuleResult.GoodSoFar());
        }
예제 #4
0
        public static RuleCallbackPair Token(TokenType type, Action <string> onMatched)
        {
            var rule = new SingleTokenParser(t =>
            {
                if (t.Type == type)
                {
                    return(RuleResult.Complete(t));
                }

                string errMsg = $"Expected a {type}, but got the {t.Type} \"{t.Content}\"";
                return(RuleResult.Failed(t.Position, errMsg));
            });

            return(new RuleCallbackPair(() => rule, Wrapper));

            void Wrapper(object node)
            {
                var token = (Token)node;

                onMatched(token.Content);
            }
        }