ImportTokenVocabulary() public method

public ImportTokenVocabulary ( Grammar importFromGr ) : int
importFromGr Grammar
return int
Beispiel #1
0
        public virtual void Process()
        {
            bool exceptionWhenWritingLexerFile = false;
            string lexerGrammarFileName = null;		// necessary at this scope to have access in the catch below

            Stopwatch timer = Stopwatch.StartNew();

            // Have to be tricky here when Maven or build tools call in and must new Tool()
            // before setting options. The banner won't display that way!
            if ( Verbose && showBanner )
            {
                ErrorManager.Info( "ANTLR Parser Generator  Version " + AssemblyInformationalVersion );
                showBanner = false;
            }

            try
            {
                SortGrammarFiles(); // update grammarFileNames
            }
            catch ( Exception e )
            {
                ErrorManager.Error( ErrorManager.MSG_INTERNAL_ERROR, e );
            }

            foreach ( string grammarFileName in GrammarFileNames )
            {
                // If we are in make mode (to support build tools like Maven) and the
                // file is already up to date, then we do not build it (and in verbose mode
                // we will say so).
                if ( Make )
                {
                    try
                    {
                        if ( !BuildRequired( grammarFileName ) )
                            continue;
                    }
                    catch ( Exception e )
                    {
                        ErrorManager.Error( ErrorManager.MSG_INTERNAL_ERROR, e );
                    }
                }

                if ( Verbose && !Depend )
                {
                    Console.Out.WriteLine( grammarFileName );
                }
                try
                {
                    if ( Depend )
                    {
                        BuildDependencyGenerator dep = new BuildDependencyGenerator( this, grammarFileName );
            #if false
                        IList<string> outputFiles = dep.getGeneratedFileList();
                        IList<string> dependents = dep.getDependenciesFileList();
                        Console.Out.WriteLine( "output: " + outputFiles );
                        Console.Out.WriteLine( "dependents: " + dependents );
            #endif
                        Console.Out.WriteLine( dep.GetDependencies().Render() );
                        continue;
                    }

                    Grammar rootGrammar = GetRootGrammar( grammarFileName );
                    // we now have all grammars read in as ASTs
                    // (i.e., root and all delegates)
                    rootGrammar.composite.AssignTokenTypes();
                    //rootGrammar.composite.TranslateLeftRecursiveRules();
                    rootGrammar.AddRulesForSyntacticPredicates();
                    rootGrammar.composite.DefineGrammarSymbols();
                    rootGrammar.composite.CreateNFAs();

                    GenerateRecognizer( rootGrammar );

                    if ( PrintGrammar )
                    {
                        rootGrammar.PrintGrammar( Console.Out );
                    }

                    if (Report)
                    {
                        GrammarReport2 greport = new GrammarReport2(rootGrammar);
                        Console.WriteLine(greport.ToString());
                    }

                    if ( Profile )
                    {
                        GrammarReport report = new GrammarReport(rootGrammar);
                        Stats.WriteReport( GrammarReport.GRAMMAR_STATS_FILENAME,
                                          report.ToNotifyString() );
                    }

                    // now handle the lexer if one was created for a merged spec
                    string lexerGrammarStr = rootGrammar.GetLexerGrammar();
                    //[email protected]("lexer grammar:\n"+lexerGrammarStr);
                    if ( rootGrammar.type == GrammarType.Combined && lexerGrammarStr != null )
                    {
                        lexerGrammarFileName = rootGrammar.ImplicitlyGeneratedLexerFileName;
                        try
                        {
                            TextWriter w = GetOutputFile( rootGrammar, lexerGrammarFileName );
                            w.Write( lexerGrammarStr );
                            w.Close();
                        }
                        catch (IOException)
                        {
                            // emit different error message when creating the implicit lexer fails
                            // due to write permission error
                            exceptionWhenWritingLexerFile = true;
                            throw;
                        }
                        try
                        {
                            StringReader sr = new StringReader( lexerGrammarStr );
                            Grammar lexerGrammar = new Grammar(this);
                            lexerGrammar.composite.WatchNFAConversion = internalOption_watchNFAConversion;
                            lexerGrammar.implicitLexer = true;
                            if ( TestMode )
                                lexerGrammar.DefaultRuleModifier = "public";
                            FileInfo lexerGrammarFullFile = new FileInfo( System.IO.Path.Combine( GetFileDirectory( lexerGrammarFileName ), lexerGrammarFileName ) );
                            lexerGrammar.FileName = lexerGrammarFullFile.ToString();

                            lexerGrammar.ImportTokenVocabulary( rootGrammar );
                            lexerGrammar.ParseAndBuildAST( sr );

                            sr.Close();

                            lexerGrammar.composite.AssignTokenTypes();
                            lexerGrammar.AddRulesForSyntacticPredicates();
                            lexerGrammar.composite.DefineGrammarSymbols();
                            lexerGrammar.composite.CreateNFAs();

                            GenerateRecognizer( lexerGrammar );
                        }
                        finally
                        {
                            // make sure we clean up
                            if ( deleteTempLexer )
                            {
                                System.IO.DirectoryInfo outputDir = GetOutputDirectory( lexerGrammarFileName );
                                FileInfo outputFile = new FileInfo( System.IO.Path.Combine( outputDir.FullName, lexerGrammarFileName ) );
                                outputFile.Delete();
                            }
                        }
                    }
                }
                catch ( IOException e )
                {
                    if ( exceptionWhenWritingLexerFile )
                    {
                        ErrorManager.Error( ErrorManager.MSG_CANNOT_WRITE_FILE, e );
                    }
                    else
                    {
                        ErrorManager.Error( ErrorManager.MSG_CANNOT_OPEN_FILE,
                                           grammarFileName, e );
                    }
                }
                catch ( Exception e )
                {
                    ErrorManager.Error( ErrorManager.MSG_INTERNAL_ERROR, grammarFileName, e );
                }
            #if false
                finally
                {
                    Console.Out.WriteLine( "creates=" + Interval.creates );
                    Console.Out.WriteLine( "hits=" + Interval.hits );
                    Console.Out.WriteLine( "misses=" + Interval.misses );
                    Console.Out.WriteLine( "outOfRange=" + Interval.outOfRange );
                }
            #endif
            }

            if (_showTimer)
            {
                Console.WriteLine("Total parse time: {0}ms", timer.ElapsedMilliseconds);
            }
        }
Beispiel #2
0
        public void TestArgsOnTokenInLexerRuleOfCombined()
        {
            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "grammar t;\n" +
                "a : R;\n" +
                "R : 'z' ID[32] ;\n" +
                "ID : 'a';\n" );

            string lexerGrammarStr = g.GetLexerGrammar();
            System.IO.StringReader sr = new System.IO.StringReader( lexerGrammarStr );
            Grammar lexerGrammar = new Grammar();
            lexerGrammar.FileName = "<internally-generated-lexer>";
            lexerGrammar.ImportTokenVocabulary( g );
            lexerGrammar.ParseAndBuildAST( sr );
            lexerGrammar.DefineGrammarSymbols();
            lexerGrammar.CheckNameSpaceAndActions();
            sr.Close();

            AntlrTool antlr = newTool();
            antlr.SetOutputDirectory( null ); // write to /dev/null
            CodeGenerator generator = new CodeGenerator( antlr, lexerGrammar, "Java" );
            lexerGrammar.CodeGenerator = generator;
            generator.GenRecognizer();

            int expectedMsgID = ErrorManager.MSG_RULE_HAS_NO_ARGS;
            object expectedArg = "ID";
            object expectedArg2 = null;
            GrammarSemanticsMessage expectedMessage =
                new GrammarSemanticsMessage( expectedMsgID, lexerGrammar, null, expectedArg, expectedArg2 );
            checkError( equeue, expectedMessage );
        }