Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
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;
                }
            }
        }
Ejemplo n.º 3
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;
             *                  }
             *          }
             */
        }
Ejemplo n.º 4
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;

        }
Ejemplo n.º 5
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);
        }