use of org.antlr.runtime.ANTLRFileStream in project antlr4 by antlr.
the class Tool method parseGrammar.
public GrammarRootAST parseGrammar(String fileName) {
try {
File file = new File(fileName);
if (!file.isAbsolute()) {
file = new File(inputDirectory, fileName);
}
ANTLRFileStream in = new ANTLRFileStream(file.getAbsolutePath(), grammarEncoding);
GrammarRootAST t = parse(fileName, in);
return t;
} catch (IOException ioe) {
errMgr.toolError(ErrorType.CANNOT_OPEN_FILE, ioe, fileName);
}
return null;
}
use of org.antlr.runtime.ANTLRFileStream in project ceylon-compiler by ceylon.
the class CeylonVersionTool method updateModuleImports.
private boolean updateModuleImports(Module module, Map<String, String> updatedModuleVersions) throws IOException, RecognitionException {
String moduleDescriptorPath = module.getUnit().getFullPath();
CeylonLexer lexer = new CeylonLexer(new ANTLRFileStream(moduleDescriptorPath, encoding));
TokenRewriteStream tokenStream = new TokenRewriteStream(lexer);
CeylonParser parser = new CeylonParser(tokenStream);
Tree.CompilationUnit cu = parser.compilationUnit();
List<Tree.ImportModule> moduleImports = findUpdatedImport(cu, updatedModuleVersions);
for (Tree.ImportModule moduleImport : moduleImports) {
String importedModuleName = getModuleName(moduleImport);
String newVersion = updatedModuleVersions.get(importedModuleName);
if (newVersion == null)
newVersion = this.newVersion;
String v = confirm("update.dependency.version", importedModuleName, module.getNameAsString(), module.getVersion(), newVersion);
if (v == null) {
return false;
} else if (!v.isEmpty()) {
updateImportVersion(moduleDescriptorPath, tokenStream, moduleImport, v);
}
}
return true;
}
use of org.antlr.runtime.ANTLRFileStream in project antlr4 by antlr.
the class Tool method loadImportedGrammar.
/**
* Try current dir then dir of g then lib dir
* @param g
* @param nameNode The node associated with the imported grammar name.
*/
public Grammar loadImportedGrammar(Grammar g, GrammarAST nameNode) throws IOException {
String name = nameNode.getText();
Grammar imported = importedGrammars.get(name);
if (imported == null) {
g.tool.log("grammar", "load " + name + " from " + g.fileName);
File importedFile = null;
for (String extension : 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.getToken(), name);
return null;
}
String absolutePath = importedFile.getAbsolutePath();
ANTLRFileStream in = new ANTLRFileStream(absolutePath, grammarEncoding);
GrammarRootAST root = parse(g.fileName, in);
if (root == null) {
return null;
}
imported = createGrammar(root);
imported.fileName = absolutePath;
importedGrammars.put(root.getGrammarName(), imported);
}
return imported;
}
use of org.antlr.runtime.ANTLRFileStream in project ceylon-compiler by ceylon.
the class CeylonVersionTool method updateModuleVersion.
private boolean updateModuleVersion(Module module, Map<String, String> updatedModuleVersions) throws IOException, RecognitionException {
String moduleDescriptorPath = module.getUnit().getFullPath();
CeylonLexer lexer = new CeylonLexer(new ANTLRFileStream(moduleDescriptorPath, encoding));
TokenRewriteStream tokenStream = new TokenRewriteStream(lexer);
CeylonParser parser = new CeylonParser(tokenStream);
Tree.CompilationUnit cu = parser.compilationUnit();
String v = this.confirm == Confirm.dependencies ? this.newVersion : confirm("update.module.version", module.getNameAsString(), module.getVersion(), this.newVersion);
if (v == null) {
return false;
} else if (!v.isEmpty()) {
// record the new version for this module
updatedModuleVersions.put(module.getNameAsString(), v);
updateModuleVersion(moduleDescriptorPath, tokenStream, cu, v);
}
return true;
}
Aggregations