public virtual Grammar LoadImportedGrammar(Grammar g, GrammarAST nameNode)
{
string name = nameNode.Text;
Grammar imported;
if (!importedGrammars.TryGetValue(name, out imported) || imported == null)
{
g.tool.Log("grammar", "load " + name + " from " + g.fileName);
string importedFile = null;
foreach (string extension in ALL_GRAMMAR_EXTENSIONS)
{
importedFile = GetImportedGrammarFile(g, name + extension);
if (importedFile != null)
{
break;
}
}
if (importedFile == null)
{
errMgr.GrammarError(ErrorType.CANNOT_FIND_IMPORTED_GRAMMAR, g.fileName, nameNode.Token, name);
return null;
}
string absolutePath = Path.GetFullPath(importedFile);
string fileContent = File.ReadAllText(absolutePath, Encoding.GetEncoding(grammarEncoding));
char[] fileChars = fileContent.ToCharArray();
ANTLRStringStream @in = new ANTLRStringStream(fileChars, fileChars.Length, importedFile);
GrammarRootAST root = Parse(g.fileName, @in);
if (root == null)
{
return null;
}
imported = CreateGrammar(root);
imported.fileName = absolutePath;
importedGrammars[root.GetGrammarName()] = imported;
}
return imported;
}