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); } }
/** Import the rules/tokens of a delegate grammar. All delegate grammars are * read during the ctor of first Grammar created. * * Do not create NFA here because NFA construction needs to hook up with * overridden rules in delegation root grammar. */ public virtual void ImportGrammar( IToken grammarNameToken, string label ) { string grammarName = grammarNameToken.Text; //[email protected]("import "+gfile.getName()); string gname = grammarName + GrammarFileExtension; TextReader br = null; try { string fullName = Tool.GetLibraryFile( gname ); if (!File.Exists(fullName)) { gname = grammarName + AltGrammarFileExtension; fullName = Tool.GetLibraryFile(gname); } //FileReader fr = new FileReader( fullName ); //br = new BufferedReader( fr ); br = new StringReader( System.IO.File.ReadAllText( fullName ) ); Grammar delegateGrammar = null; delegateGrammar = new Grammar( Tool, gname, composite ); delegateGrammar.label = label; AddDelegateGrammar( delegateGrammar ); delegateGrammar.ParseAndBuildAST( br ); delegateGrammar.AddRulesForSyntacticPredicates(); if ( !ValidImport( delegateGrammar ) ) { ErrorManager.GrammarError( ErrorManager.MSG_INVALID_IMPORT, this, grammarNameToken, this, delegateGrammar ); return; } if ( this.type == GrammarType.Combined && ( delegateGrammar.name.Equals( this.name + grammarTypeToFileNameSuffix[(int)GrammarType.Lexer] ) || delegateGrammar.name.Equals( this.name + grammarTypeToFileNameSuffix[(int)GrammarType.Parser] ) ) ) { ErrorManager.GrammarError( ErrorManager.MSG_IMPORT_NAME_CLASH, this, grammarNameToken, this, delegateGrammar ); return; } if ( delegateGrammar.grammarTree != null ) { // we have a valid grammar // deal with combined grammars if ( delegateGrammar.type == GrammarType.Lexer && this.type == GrammarType.Combined ) { // ooops, we wasted some effort; tell lexer to read it in // later LexerGrammarTemplate.SetAttribute( "imports", grammarName ); // but, this parser grammar will need the vocab // so add to composite anyway so we suck in the tokens later } } //[email protected]("Got grammar:\n"+delegateGrammar); } catch ( IOException ioe ) { ErrorManager.Error( ErrorManager.MSG_CANNOT_OPEN_FILE, gname, ioe ); } finally { if ( br != null ) { try { br.Close(); } catch ( IOException ioe ) { ErrorManager.Error( ErrorManager.MSG_CANNOT_CLOSE_FILE, gname, ioe ); } } } }