Esempio n. 1
0
        public void Parse(ParserDetails pd)
        {
            string     ffn  = pd.FullFileName;
            string     code = pd.Code;
            IParseTree pt   = null;

            // Set up Antlr to parse input grammar.
            byte[] byteArray = Encoding.UTF8.GetBytes(code);
            var    ais       = new AntlrInputStream(
                new StreamReader(
                    new MemoryStream(byteArray)).ReadToEnd());

            ais.name = ffn;
            CommonTokenStream cts = new CommonTokenStream(new Java9Lexer(ais));
            var parser            = new Java9Parser(cts);

            try
            {
                pt = parser.compilationUnit();
            }
            catch (Exception)
            {
                // Parsing error.
            }

            //StringBuilder sb = new StringBuilder();
            //TreeSerializer.ParenthesizedAST(pt, sb, "", cts);
            //string fn = System.IO.Path.GetFileName(ffn);
            //fn = "c:\\temp\\" + fn;
            //System.IO.File.WriteAllText(fn, sb.ToString());

            pd.ParseTree = pt;
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            int  i          = 0;
            bool build_tree = true;

            for (; i < args.Length; ++i)
            {
                switch (args[i])
                {
                case "-notree":
                    build_tree = false;
                    break;
                }
            }
            var input  = File.OpenText(args[i - 1]);
            var str    = new AntlrInputStream(input);
            var lexer  = new Java9Lexer(str);
            var tokens = new CommonTokenStream(lexer);
            var parser = new Java9Parser(tokens);

            parser.Lexer = lexer;
            lexer.Parser = parser;
            var listener = new ErrorListener <IToken>(parser, lexer, tokens);

            parser.AddErrorListener(listener);
            parser.BuildParseTree = build_tree;
            var start = DateTime.Now;
            var tree  = parser.compilationUnit();

            if (listener.had_error)
            {
                System.Console.WriteLine("error in parse.");
            }
            else
            {
                System.Console.WriteLine("parse completed.");
            }
            var end = DateTime.Now;

            System.Console.WriteLine(tokens.OutputTokens());
            if (tree != null)
            {
                System.Console.WriteLine(tree.OutputTree(tokens));
            }
            System.Console.WriteLine(end - start);
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            bool build_tree = false;
            bool show_tree  = false;
            bool group      = true;

            for (int i = 0; i < args.Length; ++i)
            {
                switch (args[i])
                {
                case "-notree":
                    build_tree = false;
                    break;

                case "-showtree":
                    show_tree = true;
                    break;

                case "-group":
                    group = true;
                    break;
                }
            }
            if (group)
            {
                var str      = new AntlrInputStream("");
                var lexer    = new Java9Lexer(str);
                var tokens   = new CommonTokenStream(lexer);
                var parser   = new Java9Parser(tokens);
                var listener = new ErrorListener <IToken>(parser, lexer, tokens);
                parser.AddErrorListener(listener);
                parser.BuildParseTree = build_tree;
                for (int i = 0; i < args.Length; ++i)
                {
                    if (args[i].StartsWith('-'))
                    {
                        continue;
                    }
                    var input = File.ReadAllText(args[i]);
                    str = new AntlrInputStream(input);
                    //                  lexer.Reset();
                    parser.Reset();
                    lexer.SetInputStream(str);
                    tokens.SetTokenSource(lexer);
                    parser.TokenStream = tokens;
                    var start = DateTime.Now;
                    var tree  = parser.compilationUnit();
                    if (listener.had_error)
                    {
                        System.Console.WriteLine("error in parse.");
                    }
                    else
                    {
                        System.Console.WriteLine("parse completed.");
                    }
                    var end = DateTime.Now;
                    //                  System.Console.WriteLine(tokens.OutputTokens());
                    if (show_tree && tree != null)
                    {
                        System.Console.WriteLine(tree.OutputTree(tokens));
                    }
                    System.Console.WriteLine(end - start);
                }
            }
            else
            {
                for (int i = 0; i < args.Length; ++i)
                {
                    if (args[i].StartsWith('-'))
                    {
                        continue;
                    }
                    var input    = File.OpenText(args[i]);
                    var str      = new AntlrInputStream(input);
                    var lexer    = new Java9Lexer(str);
                    var tokens   = new CommonTokenStream(lexer);
                    var parser   = new Java9Parser(tokens);
                    var listener = new ErrorListener <IToken>(parser, lexer, tokens);
                    parser.AddErrorListener(listener);
                    parser.BuildParseTree = build_tree;
                    var start = DateTime.Now;
                    var tree  = parser.compilationUnit();
                    if (listener.had_error)
                    {
                        System.Console.WriteLine("error in parse.");
                    }
                    else
                    {
                        System.Console.WriteLine("parse completed.");
                    }
                    var end = DateTime.Now;
                    //                   System.Console.WriteLine(tokens.OutputTokens());
                    if (show_tree && tree != null)
                    {
                        System.Console.WriteLine(tree.OutputTree(tokens));
                    }
                    System.Console.WriteLine(end - start);
                }
            }
        }
Esempio n. 4
0
        public static void Main(String[] args)
        {
            bool print_tree   = false;
            bool perf         = false;
            bool print_tokens = false;
            int  file_index   = 0;

            if (args.Length == 0)
            {
                System.Console.WriteLine("Antlr Java parser.");
                return;
            }
            else
            {
                for (int i = 0; i < args.Length; ++i)
                {
                    if (args[i] == "-t")
                    {
                        print_tree = true;
                    }
                    else if (args[i] == "-p")
                    {
                        perf = true;
                    }
                    else if (args[i] == "-i")
                    {
                        print_tokens = true;
                    }
                    else
                    {
                        file_index = i;
                    }
                }
            }
            int               exceptions    = 0;
            var               errorListener = new ErrorListener <IToken>();
            IParseTree        tree          = null;
            CommonTokenStream tokens        = null;
            var               start         = DateTime.Now;

            try
            {
                var input = File.OpenText(args[file_index]);
                var str   = new AntlrInputStream(input);
                var lexer = new Java9Lexer(str);
                tokens = new CommonTokenStream(lexer);
                var parser = new Java9Parser(tokens);
                parser.RemoveErrorListeners();
                parser.AddErrorListener(errorListener);
                tree = parser.compilationUnit();
            }
            catch (Exception e)
            {
                exceptions++;
            }
            System.Console.WriteLine("errors " + errorListener.num_errors + " exceptions " + exceptions);
            var end = DateTime.Now;

            if (perf)
            {
                System.Console.WriteLine(end - start);
            }
            if (print_tokens && tokens != null)
            {
                foreach (var token in tokens.GetTokens())
                {
                    System.Console.WriteLine("Token " + token.TokenIndex + " " + token.Type + " " + Output.PerformEscapes(token.Text));
                }
            }
            if (print_tree && tree != null)
            {
                System.Console.WriteLine(tree.OutputTree(tokens));
            }
        }