Antlr4.Tool.Grammar.LoadImportedGrammars C# (CSharp) Method

LoadImportedGrammars() public method

public LoadImportedGrammars ( ) : void
return void
        public virtual void LoadImportedGrammars()
        {
            if (ast == null)
                return;
            GrammarAST i = (GrammarAST)ast.GetFirstChildWithType(ANTLRParser.IMPORT);
            if (i == null)
                return;
            ISet<string> visited = new HashSet<string>();
            visited.Add(this.name);
            importedGrammars = new List<Grammar>();
            foreach (object c in i.Children)
            {
                GrammarAST t = (GrammarAST)c;
                string importedGrammarName = null;
                if (t.Type == ANTLRParser.ASSIGN)
                {
                    t = (GrammarAST)t.GetChild(1);
                    importedGrammarName = t.Text;
                }
                else if (t.Type == ANTLRParser.ID)
                {
                    importedGrammarName = t.Text;
                }
                if (visited.Contains(importedGrammarName))
                {
                    // ignore circular refs
                    continue;
                }
                Grammar g;
                try
                {
                    g = tool.LoadImportedGrammar(this, t);
                }
                catch (IOException)
                {
                    tool.errMgr.GrammarError(ErrorType.ERROR_READING_IMPORTED_GRAMMAR,
                                             importedGrammarName,
                                             t.Token,
                                             importedGrammarName,
                                             name);
                    continue;
                }
                // did it come back as error node or missing?
                if (g == null)
                    continue;
                g.parent = this;
                importedGrammars.Add(g);
                g.LoadImportedGrammars(); // recursively pursue any imports in this import
            }
        }

Usage Example

Example #1
0
        /** To process a grammar, we load all of its imported grammars into
            subordinate grammar objects. Then we merge the imported rules
            into the root grammar. If a root grammar is a combined grammar,
            we have to extract the implicit lexer. Once all this is done, we
            process the lexer first, if present, and then the parser grammar
         */
        public virtual void Process(Grammar g, bool gencode)
        {
            g.LoadImportedGrammars();

            GrammarTransformPipeline transform = new GrammarTransformPipeline(g, this);
            transform.Process();

            LexerGrammar lexerg;
            GrammarRootAST lexerAST;
            if (g.ast != null && g.ast.grammarType == ANTLRParser.COMBINED &&
                 !g.ast.hasErrors)
            {
                lexerAST = transform.ExtractImplicitLexer(g); // alters g.ast
                if (lexerAST != null)
                {
                    if (grammarOptions != null)
                    {
                        lexerAST.cmdLineOptions = grammarOptions;
                    }

                    lexerg = new LexerGrammar(this, lexerAST);
                    lexerg.fileName = g.fileName;
                    lexerg.originalGrammar = g;
                    g.implicitLexer = lexerg;
                    lexerg.implicitLexerOwner = g;

                    int prevErrors = errMgr.GetNumErrors();
                    ProcessNonCombinedGrammar(lexerg, gencode);
                    if (errMgr.GetNumErrors() > prevErrors)
                    {
                        return;
                    }

                    //				System.out.println("lexer tokens="+lexerg.tokenNameToTypeMap);
                    //				System.out.println("lexer strings="+lexerg.stringLiteralToTypeMap);
                }
            }
            if (g.implicitLexer != null)
                g.ImportVocab(g.implicitLexer);
            //		System.out.println("tokens="+g.tokenNameToTypeMap);
            //		System.out.println("strings="+g.stringLiteralToTypeMap);
            ProcessNonCombinedGrammar(g, gencode);
        }