Example #1
0
        public List <Node> Build(ICharStream charStream)
        {
            var lexer  = new csLexer(charStream);
            var parser = new csParser(new CommonTokenStream(lexer));

            var statement_list_return = parser.statement_list();
            var root = (ITree)statement_list_return.Tree;

            var nodes = new List <Node>();

            if (root.IsNil)
            {
                for (int i = 0; i < root.ChildCount; i++)
                {
                    var node = root.GetChild(i);
                    nodes.Add(Build(node));
                }
            }
            else
            {
                nodes.Add(Build(root));
            }

            return(nodes);
        }
Example #2
0
        public static CommonTreeNodeStream parseFile(string fullName)
        {
            if (cfg.Verbosity > 2)
            {
                Console.WriteLine("Parsing " + Path.GetFileName(fullName));
            }

            ICharStream input = new ANTLRFileStream(fullName);

            PreProcessor lex = new PreProcessor();

            lex.AddDefine(cfg.MacroDefines);
            lex.CharStream       = input;
            lex.TraceDestination = Console.Error;

            CommonTokenStream tokens = new CommonTokenStream(lex);
//            if (tokens.LT(1).Type == TokenTypes.EndOfFile)
//            {
//                Console.WriteLine("File is empty");
//                return null;
//            }
            csParser p = new csParser(tokens);

            p.TraceDestination = Console.Error;
            p.IsJavaish        = cfg.InternalIsJavaish;

            csParser.compilation_unit_return parser_rt = p.compilation_unit();

            if (parser_rt == null || parser_rt.Tree == null)
            {
                if (lex.FoundMeat)
                {
                    Console.WriteLine("No Tree returned from parsing! (Your rule did not parse correctly)");
                }
                else
                {
                    // the file was empty, this is not an error.
                }
                return(null);
            }

            CommonTreeNodeStream nodes = new CommonTreeNodeStream(parser_rt.Tree);

            nodes.TokenStream = tokens;

            return(nodes);
        }
Example #3
0
        /// <summary> ParseFile
        /// </summary>
        /// <param Name="file_name">I expect some_rule_01.ext for a file Name.  If the rule Name matches a real
        /// rule I will call that method.  Otherwise, we call parse.compilation_unit(), the start of the grammar.
        /// </param>
        /// <param Name="wait"> Wait on errors or keep going.</param>
        /// <param Name="count"> Count of files successfully parsed.</param>
        /// <returns>parser.[rule]_return</returns>
        public void ParseFile(string file_name, bool wait, ref int count)
        {
            string rule_name = RuleNameFromFilePath(file_name);

            // if the -a and the Archive attribute is set on the file, then skip it
            if (ArchiveFlagIsClear(file_name, ref count))
            {
                return;
            }

            Console.WriteLine("---------------");
            Console.Error.WriteLine(file_name);

            CommonTokenStream tokens = CreateLexer <PreProcessor>(file_name);
            csParser          p      = new csParser(tokens);

            using (ConsolePause con = new ConsolePause(wait))
            {
                // Try and call a rule like CSParser.namespace_body()
                // Use reflection to find the rule to use.
                MethodInfo mi = p.GetType().GetMethod(rule_name);

                // did we find a method the same as file Name?
                if (mi != null)
                {
                    con.warn(string.Format("parser using rule -- {0}:", rule_name));
                    mi.Invoke(p, new object[0]);
                }
                else
                {
                    // by default use the start rule for csharp, I called this compilation_unit in the grammar.
                    con.warn("parser using rule -- compilation_unit:");
                    p.compilation_unit();
                }


                // If we parsed the file (no error messages), clear the Archive flag
                if (!con.HasMessages)
                {
                    // Clear archive attribute
                    File.SetAttributes(file_name, File.GetAttributes(file_name) & ~FileAttributes.Archive);
                    ++count;
                }
            }
        }
Example #4
0
        public csParser.compilation_unit_return ParseContent(string content)
        {
            var tokens = CreateLexer <PreProcessor>(content);
            var p      = new csParser(tokens);

            p.TraceDestination = Console.Error;

            return(p.compilation_unit());

            /**
             *          using (ConsolePause con = new ConsolePause(wait))
             *          {
             *                  // Try and call a rule like CSParser.namespace_body()
             *                  // Use reflection to find the rule to use.
             *                  MethodInfo mi = p.GetType().GetMethod(rule_name);
             *
             *                  // did we find a method the same as file name?
             *                  if (mi != null)
             *                  {
             *                          con.warn(string.Format("parser using rule -- {0}:", rule_name));
             *                          mi.Invoke(p, new object[0]);
             *                  }
             *                  else
             *                  {
             *                          // by default use the start rule for csharp, I called this compilation_unit in the grammar.
             *                          con.warn("parser using rule -- compilation_unit:");
             *                          p.compilation_unit();
             *                  }
             *
             *
             *                  // If we parsed the file (no error messages), clear the Archive flag
             *                  if (!con.HasMessages)
             *                  {
             *                          // Clear archive attribute
             *                          File.SetAttributes(file_name, File.GetAttributes(file_name) & ~FileAttributes.Archive);
             ++count;
             *                  }
             *          }
             */
        }
Example #5
0
        public CommonTree parseString(string startRule, string inStr, bool includeNet)
        {
            if (Cfg.Verbosity > 5)
            {
                Console.WriteLine("Parsing fragment ");
            }

            ICharStream input = new ANTLRStringStream(inStr);

            PreProcessor lex = new PreProcessor();

            lex.AddDefine(Cfg.MacroDefines);
            lex.CharStream       = input;
            lex.TraceDestination = Console.Error;

            CommonTokenStream tokens = new CommonTokenStream(lex);

            csParser p = new csParser(tokens);

            p.TraceDestination = Console.Error;
            p.IsJavaish        = true;

            // Try and call a rule like CSParser.namespace_body()
            // Use reflection to find the rule to use.
            MethodInfo mi = p.GetType().GetMethod(startRule);

            if (mi == null)
            {
                throw new Exception("Could not find start rule " + startRule + " in csParser");
            }

            ParserRuleReturnScope csRet = (ParserRuleReturnScope)mi.Invoke(p, new object[0]);

            CommonTreeNodeStream csTreeStream = new CommonTreeNodeStream(csRet.Tree);

            csTreeStream.TokenStream = tokens;

            JavaMaker javaMaker = new JavaMaker(csTreeStream);

            javaMaker.TraceDestination = Console.Error;
            javaMaker.Cfg       = Cfg;
            javaMaker.IsJavaish = true;
            javaMaker.InitParser();

            // Try and call a rule like CSParser.namespace_body()
            // Use reflection to find the rule to use.
            mi = javaMaker.GetType().GetMethod(startRule);

            if (mi == null)
            {
                throw new Exception("Could not find start rule " + startRule + " in javaMaker");
            }

            TreeRuleReturnScope javaSyntaxRet = (TreeRuleReturnScope)mi.Invoke(javaMaker, new object[0]);

            CommonTree retAST = (CommonTree)javaSyntaxRet.Tree;

            if (includeNet)
            {
                CommonTreeNodeStream javaSyntaxNodes = new CommonTreeNodeStream(retAST);

                javaSyntaxNodes.TokenStream = csTreeStream.TokenStream;

                NetMaker netMaker = new NetMaker(javaSyntaxNodes);
                netMaker.TraceDestination = Console.Error;

                netMaker.Cfg       = Cfg;
                netMaker.AppEnv    = AppEnv;
                netMaker.IsJavaish = true;
                netMaker.InitParser();

                retAST = (CommonTree)netMaker.class_member_declarations().Tree;

                // snaffle additional imports
                this.AddToImports(netMaker.Imports);
            }

            return(retAST);
        }
Example #6
0
        public static CommonTreeNodeStream parseFile(string fullName)
        {
            
            if (cfg.Verbosity > 2) Console.WriteLine("Parsing " + Path.GetFileName(fullName));
            
            ICharStream input = new ANTLRFileStream(fullName);

            PreProcessor lex = new PreProcessor();
            lex.AddDefine(cfg.MacroDefines);
            lex.CharStream = input;
            lex.TraceDestination = Console.Error;

            CommonTokenStream tokens = new CommonTokenStream(lex);
//            if (tokens.LT(1).Type == TokenTypes.EndOfFile)
//            {
//                Console.WriteLine("File is empty");
//                return null;
//            }
            csParser p = new csParser(tokens);
            p.TraceDestination = Console.Error;
            p.IsJavaish = cfg.InternalIsJavaish;
			
            csParser.compilation_unit_return parser_rt = p.compilation_unit();

            if (parser_rt == null || parser_rt.Tree == null)
            {
                if (lex.FoundMeat)
                {
                    Console.WriteLine("No Tree returned from parsing! (Your rule did not parse correctly)");
                }
                else
                {
                    // the file was empty, this is not an error.
                }
                return null;
            }

            CommonTreeNodeStream nodes = new CommonTreeNodeStream(parser_rt.Tree);            
            nodes.TokenStream = tokens;
			
            return nodes;

        }
Example #7
0
       public CommonTree parseString(string startRule, string inStr, bool includeNet)
       {
            
          if (Cfg.Verbosity > 5) Console.WriteLine("Parsing fragment ");
            
          ICharStream input = new ANTLRStringStream(inStr);

          PreProcessor lex = new PreProcessor();
          lex.AddDefine(Cfg.MacroDefines);
          lex.CharStream = input;
          lex.TraceDestination = Console.Error;

          CommonTokenStream tokens = new CommonTokenStream(lex);

          csParser p = new csParser(tokens);
          p.TraceDestination = Console.Error;
          p.IsJavaish = true;
			
          // Try and call a rule like CSParser.namespace_body() 
          // Use reflection to find the rule to use.
          MethodInfo mi = p.GetType().GetMethod(startRule);

          if (mi == null)
          {
             throw new Exception("Could not find start rule " + startRule + " in csParser");
          }

          ParserRuleReturnScope csRet = (ParserRuleReturnScope) mi.Invoke(p, new object[0]);

          CommonTreeNodeStream csTreeStream = new CommonTreeNodeStream(csRet.Tree);
          csTreeStream.TokenStream = tokens;

          JavaMaker javaMaker = new JavaMaker(csTreeStream);
          javaMaker.TraceDestination = Console.Error;
          javaMaker.Cfg = Cfg;
          javaMaker.IsJavaish = true;
          javaMaker.InitParser();

          // Try and call a rule like CSParser.namespace_body() 
          // Use reflection to find the rule to use.
          mi = javaMaker.GetType().GetMethod(startRule);

          if (mi == null)
          {
             throw new Exception("Could not find start rule " + startRule + " in javaMaker");
          }

          TreeRuleReturnScope javaSyntaxRet = (TreeRuleReturnScope) mi.Invoke(javaMaker, new object[0]);

          CommonTree retAST = (CommonTree)javaSyntaxRet.Tree;

          if (includeNet) {
             CommonTreeNodeStream javaSyntaxNodes = new CommonTreeNodeStream(retAST);
 
             javaSyntaxNodes.TokenStream = csTreeStream.TokenStream;
                     
             NetMaker netMaker = new NetMaker(javaSyntaxNodes);
             netMaker.TraceDestination = Console.Error;
 
             netMaker.Cfg = Cfg;
             netMaker.AppEnv = AppEnv;
             netMaker.IsJavaish = true;
             netMaker.InitParser();
           
             retAST = (CommonTree)netMaker.class_member_declarations().Tree;

             // snaffle additional imports
             this.AddToImports(netMaker.Imports);
          }

          return retAST;
       }
Example #8
0
        /// <summary> ParseFile
        /// </summary>
        /// <param name="file_name">I expect some_rule_01.ext for a file name.  If the rule name matches a real
        /// rule I will call that method.  Otherwise, we call parse.compilation_unit(), the start of the grammar.
        /// </param>
        /// <param name="wait"> Wait on errors or keep going.</param>
        /// <param name="count"> Count of files successfully parsed.</param>
        /// <returns>parser.[rule]_return</returns>
        public CommonTreeNodeStream ParseFileAST(string file_name, bool wait, ref int count)
        {
            string rule_name = RuleNameFromFilePath(file_name);

            // if the -a and the Archive attribute is set on the file, then skip it
            if (ArchiveFlagIsClear(file_name, ref count))
            {
                return(null);
            }

            Console.WriteLine("---------------");
            Console.Error.WriteLine(file_name);

            CommonTokenStream tokens = CreateLexer <PreProcessor>(file_name);
            csParser          p      = new csParser(tokens);
            object            parser_rt;
            CommonTree        tree = null;

            using (ConsolePause con = new ConsolePause(wait))
            {
                // Try and call a rule like CSParser.namespace_body()
                // Use reflection to find the rule to use.
                MethodInfo mi = p.GetType().GetMethod(rule_name);

                // did we find a method the same as file name?
                if (mi != null)
                {
                    con.warn(string.Format("parser using rule -- {0}:", rule_name));
                    parser_rt = mi.Invoke(p, new object[0]);
                }
                else
                {
                    // by default use the start rule for csharp, I called this compilation_unit in the grammar.
                    con.warn("parser using rule -- compilation_unit:");
                    parser_rt = p.compilation_unit();
                }

                #region Error Checking
                // Sometimes ANTLR returns a CommonErrorNode if we can't parse the file
                if (parser_rt is CommonErrorNode)
                {
                    Console.WriteLine(((CommonErrorNode)parser_rt).trappedException.Message);
                    return(null);
                }

                tree = (CommonTree)((RuleReturnScope)parser_rt).Tree;
                // Check if we didn't get an AST
                // This often happens if your grammar and tree grammar don't match
                if (tree == null)
                {
                    if (tokens.Count > 0)
                    {
                        con.err("No Tree returned from parsing! (Your rule did not parse correctly)");
                    }
                    else
                    {
                        // the file was empty, this is not an error.
                        // Clear archive attribute
                        File.SetAttributes(file_name, File.GetAttributes(file_name) & ~FileAttributes.Archive);
                        ++count;
                    }
                    return(null);
                }

                // If we parsed the file (no error messages), clear the Archive flag
                if (!con.HasMessages)
                {
                    // Clear archive attribute
                    File.SetAttributes(file_name, File.GetAttributes(file_name) & ~FileAttributes.Archive);
                    ++count;
                }
                #endregion
            }

            // Get the AST stream
            CommonTreeNodeStream nodes = new CommonTreeNodeStream(tree);
            // Add the tokens for DumpNodes, otherwise there are no token names to print out.
            nodes.TokenStream = tokens;

            // Dump the tree nodes if -n is passed on the command line.
            DumpNodes(nodes);

            return(nodes);
        }