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);
            }
        }
Exemple #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)
            {
            }
        }
        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);
            }
        }