org.antlr.runtime.ANTLRFileStream Java Examples
The following examples show how to use
org.antlr.runtime.ANTLRFileStream.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Test.java From quetzal with Eclipse Public License 2.0 | 6 votes |
public static void runParser(String sparqlFile) throws IOException { //System.out.println("Parsing: "+sparqlFile+"\n"); IbmSparqlLexer lex = new IbmSparqlLexer(new ANTLRFileStream(sparqlFile, "UTF8")); CommonTokenStream tokens = new CommonTokenStream(lex); IbmSparqlParser parser = new IbmSparqlParser(tokens); try { IbmSparqlParser.queryUnit_return ret = parser.queryUnit(); CommonTree ast = (CommonTree) ret.getTree(); //System.out.println(ast.toStringTree()); //SparqlParserUtilities.dump_tree(ast, tokens, 0); BufferedTreeNodeStream nodes = new BufferedTreeNodeStream(ast); nodes.setTokenStream(tokens); IbmSparqlAstWalker walker = new IbmSparqlAstWalker(nodes); Query query = walker.queryUnit(); //System.out.println(query.toString()); } catch (Exception e) { e.printStackTrace(); } }
Example #2
Source File: ScriptBuilder.java From scheduler with GNU Lesser General Public License v3.0 | 6 votes |
/** * Build a script from a file. * * @param f the file to parse * @return the resulting script * @throws ScriptBuilderException if an error occurred */ public Script build(File f) throws ScriptBuilderException { int k = f.getAbsolutePath().hashCode(); if (dates.containsKey(k) && dates.get(k) == f.lastModified() && cache.containsKey(f.getPath())) { LOGGER.debug("get '" + f.getName() + "' from the cache"); return cache.get(f.getPath()); } LOGGER.debug(f.getName() + " is built from the file"); dates.put(k, f.lastModified()); String name = f.getName(); try { Script v = build(new ANTLRFileStream(f.getAbsolutePath())); if (!name.equals(v.getlocalName() + Script.EXTENSION)) { throw new ScriptBuilderException("Script '" + v.getlocalName() + "' must be declared in a file named '" + v.getlocalName() + Script.EXTENSION); } cache.put(f.getPath(), v); return v; } catch (IOException e) { throw new ScriptBuilderException(e.getMessage(), e); } }
Example #3
Source File: Tool.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
/** * 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; }
Example #4
Source File: TestExt.java From quetzal with Eclipse Public License 2.0 | 5 votes |
public static void runParser(String sparqlFile) throws IOException { //System.out.println("Parsing: "+sparqlFile+"\n"); IbmSparqlLexer lex = new IbmSparqlLexer(new ANTLRFileStream(sparqlFile, "UTF8")); CommonTokenStream tokens = new CommonTokenStream(lex); IbmSparqlParser parser = new IbmSparqlParser(tokens); parser.setTreeAdaptor(new CommonTreeAdaptor(){ @Override public Object create(Token t) { return new XTree(t); } }); try { IbmSparqlParser.queryUnit_return ret = parser.queryUnit(); XTree ast = (XTree) ret.getTree(); // System.out.println(ast.toStringTree()); //SparqlParserUtilities.dump_tree(ast, tokens, 0); BufferedTreeNodeStream nodes = new BufferedTreeNodeStream(ast); nodes.setTokenStream(tokens); IbmSparqlAstWalker walker = new IbmSparqlAstWalker(nodes); Query query = walker.queryUnit(); // System.out.println(query.toString()); } catch (Exception e) { e.printStackTrace(); } }
Example #5
Source File: TestExt.java From quetzal with Eclipse Public License 2.0 | 5 votes |
public static void runParserExt(String sparqlFile) throws IOException { //System.out.println("Parsing: "+sparqlFile+"\n"); IbmSparqlExtLexer lex = new IbmSparqlExtLexer(new ANTLRFileStream(sparqlFile, "UTF8")); CommonTokenStream tokens = new CommonTokenStream(lex); IbmSparqlExtParser parser = new IbmSparqlExtParser(tokens); parser.setTreeAdaptor(new CommonTreeAdaptor(){ @Override public Object create(Token t) { return new XTree(t); } }); try { IbmSparqlExtParser.queryUnit_return ret = parser.queryUnit(); CommonTree ast = (CommonTree) ret.getTree(); // System.out.println(ast.toStringTree()); //SparqlParserUtilities.dump_tree(ast, tokens, 0); BufferedTreeNodeStream nodes = new BufferedTreeNodeStream(ast); nodes.setTokenStream(tokens); IbmSparqlExtAstWalker walker = new IbmSparqlExtAstWalker(nodes); QueryExt query = walker.queryUnit(); //query. // System.out.println(query.toString()); } catch (Exception e) { e.printStackTrace(); } }
Example #6
Source File: ANTLRv4ExternalAnnotator.java From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License | 5 votes |
private boolean tokenBelongsToFile(Token t, @NotNull PsiFile file) { CharStream inputStream = t.getInputStream(); if ( inputStream instanceof ANTLRFileStream ) { // Not equal if the token belongs to an imported grammar return inputStream.getSourceName().equals(file.getVirtualFile().getCanonicalPath()); } return true; }
Example #7
Source File: Interp.java From jFuzzyLogic with GNU Lesser General Public License v3.0 | 4 votes |
public static void main(String[] args) throws Exception { if ( args.length!=4 ) { System.err.println("java Interp file.g tokens-to-ignore start-rule input-file"); return; } String grammarFileName = args[0]; String ignoreTokens = args[1]; String startRule = args[2]; String inputFileName = args[3]; // TODO: using wrong constructor now Tool tool = new Tool(); CompositeGrammar composite = new CompositeGrammar(); Grammar parser = new Grammar(tool, grammarFileName, composite); composite.setDelegationRoot(parser); FileReader fr = new FileReader(grammarFileName); BufferedReader br = new BufferedReader(fr); parser.parseAndBuildAST(br); br.close(); parser.composite.assignTokenTypes(); parser.composite.defineGrammarSymbols(); parser.composite.createNFAs(); List leftRecursiveRules = parser.checkAllRulesForLeftRecursion(); if ( leftRecursiveRules.size()>0 ) { return; } if ( parser.getRule(startRule)==null ) { System.out.println("undefined start rule "+startRule); return; } String lexerGrammarText = parser.getLexerGrammar(); Grammar lexer = new Grammar(); lexer.importTokenVocabulary(parser); lexer.fileName = grammarFileName; lexer.setTool(tool); if ( lexerGrammarText!=null ) { lexer.setGrammarContent(lexerGrammarText); } else { System.err.println("no lexer grammar found in "+grammarFileName); } lexer.composite.createNFAs(); CharStream input = new ANTLRFileStream(inputFileName); Interpreter lexEngine = new Interpreter(lexer, input); CommonTokenStream tokens = new CommonTokenStream(lexEngine); StringTokenizer tk = new StringTokenizer(ignoreTokens, " "); while ( tk.hasMoreTokens() ) { String tokenName = tk.nextToken(); tokens.setTokenTypeChannel(lexer.getTokenType(tokenName), 99); } if ( parser.getRule(startRule)==null ) { System.err.println("Rule "+startRule+" does not exist in "+grammarFileName); return; } Interpreter parseEngine = new Interpreter(parser, tokens); ParseTree t = parseEngine.parse(startRule); System.out.println(t.toStringTree()); }