Пример #1
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)
            {
            }
        }
Пример #2
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);
            }
        }