コード例 #1
0
        protected Exception ParseShouldProduceError(string input, Func <VelocityParser, RuleContext> parseFunc, int?lexerMode = null, bool shouldLexCompletely = true)
        {
            RuleContext parsed;

            try
            {
                parsed = Parse(input, parseFunc, lexerMode);
            }
            catch (ParseCanceledException ex)
            {
                return(ex);
            }

            //For debugging purposes, print the tokens & parse tree generated

            var charStream = new AntlrInputStream(input);
            var lexer      = new VelocityLexer(charStream);

            if (lexerMode.HasValue)
            {
                lexer.Mode(lexerMode.Value);
            }

            foreach (var token in lexer.GetAllTokens())
            {
                Console.WriteLine(token);
                if (Debugger.IsAttached)
                {
                    Debug.WriteLine(token);
                }
            }

            Console.WriteLine(parsed.ToStringTree(new VelocityParser(null)));
            Assert.Fail("No Parse Errors Occurred;");
            throw new InvalidProgramException();
        }
コード例 #2
0
        internal T ParseTemplate <T>(ICharStream input, string name, Func <VelocityParser, T> parseFunc, int?lexerMode = null)
            where T : RuleContext
        {
            var lexerErrorListener  = new ErrorListener <int>();
            var parserErrorListener = new ErrorListener <IToken>();

            var lexer       = new VelocityLexer(input);
            var tokenStream = new CommonTokenStream(lexer);
            var parser      = new VelocityParser(tokenStream)
            {
                BlockDirectives      = _blockDirectives,
                SingleLineDirectives = _singleLineDirectives
            };

            lexer.RemoveErrorListeners();
            lexer.AddErrorListener(lexerErrorListener);

            parser.RemoveErrorListeners();
            //parser.AddErrorListener(new DiagnosticErrorListener(false));
            parser.AddErrorListener(parserErrorListener);

            var originalErrorStrategy = parser.ErrorHandler;

            parser.ErrorHandler = new BailErrorStrategy();

            if (lexerMode.HasValue)
            {
                lexer.Mode(lexerMode.Value);
            }

            parser.Interpreter.PredictionMode = PredictionMode.Sll;

            T template;

            try {
                TemplateGenerationEventSource.Log.ParseStart(name);
                template = parseFunc(parser);
                TemplateGenerationEventSource.Log.ParseStop(name);
            }
            catch (Exception)
            {
                tokenStream.Reset();
                parser.Reset();
                parser.Interpreter.PredictionMode = PredictionMode.Ll;
                parser.ErrorHandler = originalErrorStrategy;

                template = parseFunc(parser);
                Console.WriteLine("Fell back to full LL parsing");
            }

            HandleFailures("Lexer error", lexerErrorListener);
            HandleFailures("Parser error", parserErrorListener);

            if (lexer.Token.Type != -1)
            {
#if DEBUG
                foreach (var token in lexer.GetAllTokens())
                {
                    Console.WriteLine(token);
                }
#endif
                throw new ParseCanceledException("Lexer failed to lex to end of file.");
            }

            if (parser.NumberOfSyntaxErrors > 0)
            {
                throw new ParseCanceledException("Parser syntax errors occurred, but weren't reported properly");
            }

            return(template);
        }