Exemplo n.º 1
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;
                }
            }
        }
Exemplo 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 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);
        }