Search in sources :

Example 1 with ANTLRFileStream

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;
}
Also used : ANTLRFileStream(org.antlr.runtime.ANTLRFileStream) GrammarRootAST(org.antlr.v4.tool.ast.GrammarRootAST) IOException(java.io.IOException) File(java.io.File)

Example 2 with ANTLRFileStream

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;
}
Also used : ANTLRFileStream(org.antlr.runtime.ANTLRFileStream) TokenRewriteStream(org.antlr.runtime.TokenRewriteStream) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) CeylonLexer(com.redhat.ceylon.compiler.typechecker.parser.CeylonLexer) ImportModule(com.redhat.ceylon.compiler.typechecker.tree.Tree.ImportModule) CeylonParser(com.redhat.ceylon.compiler.typechecker.parser.CeylonParser) ImportModule(com.redhat.ceylon.compiler.typechecker.tree.Tree.ImportModule)

Example 3 with ANTLRFileStream

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;
}
Also used : ANTLRFileStream(org.antlr.runtime.ANTLRFileStream) GrammarRootAST(org.antlr.v4.tool.ast.GrammarRootAST) Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) File(java.io.File)

Example 4 with ANTLRFileStream

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;
}
Also used : ANTLRFileStream(org.antlr.runtime.ANTLRFileStream) TokenRewriteStream(org.antlr.runtime.TokenRewriteStream) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) CeylonLexer(com.redhat.ceylon.compiler.typechecker.parser.CeylonLexer) CeylonParser(com.redhat.ceylon.compiler.typechecker.parser.CeylonParser)

Aggregations

ANTLRFileStream (org.antlr.runtime.ANTLRFileStream)4 CeylonLexer (com.redhat.ceylon.compiler.typechecker.parser.CeylonLexer)2 CeylonParser (com.redhat.ceylon.compiler.typechecker.parser.CeylonParser)2 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)2 File (java.io.File)2 TokenRewriteStream (org.antlr.runtime.TokenRewriteStream)2 GrammarRootAST (org.antlr.v4.tool.ast.GrammarRootAST)2 ImportModule (com.redhat.ceylon.compiler.typechecker.tree.Tree.ImportModule)1 IOException (java.io.IOException)1 Grammar (org.antlr.v4.tool.Grammar)1 LexerGrammar (org.antlr.v4.tool.LexerGrammar)1