示例#1
0
    static void Main(string[] args)
    {
        try
        {
            string fileName = "../../../../Text";

            if (args.Length > 0)
            {
                fileName = args[0];
            }

            var code       = File.ReadAllText(fileName);
            var codeStream = new AntlrInputStream(code);
            var lexer      = new __TemplateGrammarName__Lexer(codeStream);
            lexer.RemoveErrorListeners();
            lexer.AddErrorListener(new LexerErrorListener());

            var stopwatch = Stopwatch.StartNew();
            var tokens    = lexer.GetAllTokens();
            stopwatch.Stop();
            Console.WriteLine("LexerTime {0}", stopwatch.Elapsed);
            Console.WriteLine("Tokens {0}", tokens.TokensToString());

/*$ParserPart*/
            string rootRule = null;
            bool   notParse = false;

            if (args.Length > 1)
            {
                rootRule = args[1];
                if (args.Length > 2)
                {
                    bool.TryParse(args[2], out notParse);
                }
            }

            if (!notParse)
            {
                var tokensSource = new ListTokenSource(tokens);
                var tokensStream = new CommonTokenStream(tokensSource);
                var parser       = new __TemplateGrammarName__Parser(tokensStream);
                parser.RemoveErrorListeners();
                parser.AddErrorListener(new ParserErrorListener());

                stopwatch.Restart();
                string ruleName   = rootRule == null ? __TemplateGrammarName__Parser.ruleNames[0] : rootRule;
                var    rootMethod = typeof(__TemplateGrammarName__Parser).GetMethod(ruleName);
                var    ast        = (ParserRuleContext)rootMethod.Invoke(parser, new object[0]);
                stopwatch.Stop();

                Console.WriteLine("ParserTime {0}", stopwatch.Elapsed);
                Console.WriteLine("Tree {0}", ast.ToStringTree(parser));
            }
/*ParserPart$*/
        }
        catch (Exception ex)
        {
            Console.Error.WriteLine(ex.ToString().Replace("\r", "").Replace("\n", ""));
        }
    }
示例#2
0
    public void Init()
    {
        var data   = File.ReadAllText(InputFileName != null ? InputFileName : "default_input_file");
        var stream = new AntlrInputStream(data);
        var lexer  = new __TemplateGrammarName__Lexer(stream);

        lexer.AddErrorListener(new LexerErrorListener());
        tokenStream = new CommonTokenStream(lexer);
        __TemplateGrammarName_____RuntimeName__();
    }