org.antlr.v4.runtime.ANTLRInputStream Java Examples
The following examples show how to use
org.antlr.v4.runtime.ANTLRInputStream.
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: EagleQueryParser.java From Eagle with Apache License 2.0 | 6 votes |
public ORExpression parse() throws EagleQueryParseException{ try{ EagleFilterLexer lexer = new EagleFilterLexer(new ANTLRInputStream(_query)); CommonTokenStream tokens = new CommonTokenStream(lexer); tokens.fill(); EagleFilterParser p = new EagleFilterParser(tokens); p.setErrorHandler(new EagleANTLRErrorStrategy()); p.setBuildParseTree(true); EagleQueryFilterListenerImpl listener = new EagleQueryFilterListenerImpl(); p.addParseListener(listener); EagleFilterParser.FilterContext fc = p.filter(); if(fc.exception != null){ LOG.error("Can not successfully parse the query:" + _query, fc.exception); throw fc.exception; } return listener.result(); }catch(Exception ex){ LOG.error("Can not successfully parse the query:", ex); throw new EagleQueryParseException("can not successfully parse the query:" + _query); } }
Example #2
Source File: ParseHelper.java From graphicsfuzz with Apache License 2.0 | 6 votes |
private static GLSLParser getParser( InputStream inputStream, ParseTreeListener listener) throws IOException { ANTLRInputStream input = new ANTLRInputStream(inputStream); GLSLLexer lexer = new GLSLLexer(input); PredictionContextCache cache = new PredictionContextCache(); lexer.setInterpreter( new LexerATNSimulator(lexer, lexer.getATN(), lexer.getInterpreter().decisionToDFA, cache)); CommonTokenStream tokens = new CommonTokenStream(lexer); GLSLParser parser = new GLSLParser(tokens); // Remove error listeners, otherwise errors get output to the console. parser.removeErrorListeners(); if (listener != null) { parser.addParseListener(listener); } parser.setInterpreter( new ParserATNSimulator(parser, parser.getATN(), parser.getInterpreter().decisionToDFA, cache)); return parser; }
Example #3
Source File: EagleQueryParser.java From eagle with Apache License 2.0 | 6 votes |
public ORExpression parse() throws EagleQueryParseException { try { EagleFilterLexer lexer = new EagleFilterLexer(new ANTLRInputStream(query)); CommonTokenStream tokens = new CommonTokenStream(lexer); tokens.fill(); EagleFilterParser p = new EagleFilterParser(tokens); p.setErrorHandler(new EagleANTLRErrorStrategy()); p.setBuildParseTree(true); EagleQueryFilterListenerImpl listener = new EagleQueryFilterListenerImpl(); p.addParseListener(listener); EagleFilterParser.FilterContext fc = p.filter(); if (fc.exception != null) { LOG.error("Can not successfully parse the query:" + query, fc.exception); throw fc.exception; } return listener.result(); } catch (Exception ex) { LOG.error("Can not successfully parse the query:", ex); throw new EagleQueryParseException("can not successfully parse the query:" + query); } }
Example #4
Source File: DSLRuleSetBuilder.java From urule with Apache License 2.0 | 6 votes |
public RuleSet build(String script) throws IOException{ ANTLRInputStream antlrInputStream=new ANTLRInputStream(script); RuleParserLexer lexer=new RuleParserLexer(antlrInputStream); CommonTokenStream tokenStream=new CommonTokenStream(lexer); RuleParserParser parser=new RuleParserParser(tokenStream); ScriptDecisionTableErrorListener errorListener=new ScriptDecisionTableErrorListener(); parser.addErrorListener(errorListener); BuildRulesVisitor visitor=new BuildRulesVisitor(contextBuilders,tokenStream); RuleSet ruleSet=visitor.visitRuleSet(parser.ruleSet()); rebuildRuleSet(ruleSet); String error=errorListener.getErrorMessage(); if(error!=null){ throw new RuleException("Script parse error:"+error); } return ruleSet; }
Example #5
Source File: TestSQLListener.java From datacollector with Apache License 2.0 | 6 votes |
@Test public void testSQL() { plsqlLexer l = new plsqlLexer(new ANTLRInputStream(sql)); CommonTokenStream str = new CommonTokenStream(l); plsqlParser parser = new plsqlParser(str); ParserRuleContext c; if (sql.startsWith("insert")) { c = parser.insert_statement(); } else if (sql.startsWith("delete")) { c = parser.delete_statement(); } else { c = parser.update_statement(); } SQLListener sqlListener = new SQLListener(); sqlListener.allowNulls(); sqlListener.setColumns(new HashSet<>(Lists.newArrayList("ID", "NAME", "HIREDATE", "SALARY", "LASTLOGIN"))); ParseTreeWalker parseTreeWalker = new ParseTreeWalker(); // Walk it and attach our sqlListener parseTreeWalker.walk(sqlListener, c); Assert.assertEquals(expected, sqlListener.getColumns()); }
Example #6
Source File: ExpectedParseTreeGenerator.java From contribution with GNU Lesser General Public License v2.1 | 6 votes |
private static ParseTree parseJavadocFromFile(File file) throws IOException { final String content = Files.toString(file, Charsets.UTF_8); final InputStream in = new ByteArrayInputStream(content.getBytes(Charsets.UTF_8)); final ANTLRInputStream input = new ANTLRInputStream(in); final JavadocLexer lexer = new JavadocLexer(input); lexer.removeErrorListeners(); final BaseErrorListener errorListener = new FailOnErrorListener(); lexer.addErrorListener(errorListener); final CommonTokenStream tokens = new CommonTokenStream(lexer); final JavadocParser parser = new JavadocParser(tokens); parser.removeErrorListeners(); parser.addErrorListener(errorListener); return parser.javadoc(); }
Example #7
Source File: Trans.java From cs652 with BSD 3-Clause "New" or "Revised" License | 6 votes |
public static void main(String[] args) { String code = "int x;\n" + "A b;\n"; ANTLRInputStream input = new ANTLRInputStream(code); LangLexer lexer = new LangLexer(input); CommonTokenStream tokens = new CommonTokenStream(lexer); LangParser parser = new LangParser(tokens); ParseTree tree = parser.file(); // start up System.out.println(tree.toStringTree(parser)); ParseTreeWalker walker = new ParseTreeWalker(); Gen listener = new Gen(); walker.walk(listener, tree); System.out.println(listener.file.toString()); }
Example #8
Source File: GDLHandler.java From gdl with Apache License 2.0 | 6 votes |
/** * Checks valid input and creates GDL Handler. * * @param antlrInputStream ANTLR input stream * @return GDL handler */ private GDLHandler build(ANTLRInputStream antlrInputStream) { if (graphLabel == null) { throw new IllegalArgumentException("Graph label must not be null."); } if (vertexLabel == null) { throw new IllegalArgumentException("Vertex label must not be null."); } if (edgeLabel == null) { throw new IllegalArgumentException("Edge label must not be null."); } if (errorStrategy == null) { throw new IllegalArgumentException("Error handler must not be null."); } GDLLexer lexer = new GDLLexer(antlrInputStream); GDLParser parser = new GDLParser(new CommonTokenStream(lexer)); parser.setErrorHandler(errorStrategy); GDLLoader loader = new GDLLoader(graphLabel, vertexLabel, edgeLabel, useDefaultGraphLabel, useDefaultVertexLabel, useDefaultEdgeLabel); new ParseTreeWalker().walk(loader, parser.database()); return new GDLHandler(loader); }
Example #9
Source File: BaseStellarProcessor.java From metron with Apache License 2.0 | 6 votes |
/** * Parses and evaluates the given Stellar expression, {@code rule}. * @param rule The Stellar expression to parse and evaluate. * @return The Expression, which can be reevaluated without reparsing in different Contexts and Resolvers. */ public static StellarCompiler.Expression compile(final String rule) { if (rule == null || isEmpty(rule.trim())) { return null; } ANTLRInputStream input = new ANTLRInputStream(rule); StellarLexer lexer = new StellarLexer(input); lexer.removeErrorListeners(); lexer.addErrorListener(new ErrorListener()); TokenStream tokens = new CommonTokenStream(lexer); StellarParser parser = new StellarParser(tokens); StellarCompiler treeBuilder = new StellarCompiler( ArithmeticEvaluator.INSTANCE, NumberLiteralEvaluator.INSTANCE, ComparisonExpressionWithOperatorEvaluator.INSTANCE ); parser.addParseListener(treeBuilder); parser.removeErrorListeners(); parser.addErrorListener(new ErrorListener()); parser.transformation(); return treeBuilder.getExpression(); }
Example #10
Source File: Antlr4LearningTests.java From jplag with GNU General Public License v3.0 | 6 votes |
@Test public void testVisitor() throws IOException { BufferedInputStream fis = new BufferedInputStream(new FileInputStream(new File(srcTestResources, "ExceptionOne.java"))); ANTLRInputStream input = new ANTLRInputStream(fis); // create a lexer that feeds off of input CharStream Java7Lexer lexer = new Java7Lexer(input); // create a buffer of tokens pulled from the lexer CommonTokenStream tokens = new CommonTokenStream(lexer); jplag.java17.grammar.Java7Parser j7p = new Java7Parser(tokens); j7p.addParseListener(new JplagJava7Listener(new jplag.java17.Parser())); j7p.compilationUnit(); }
Example #11
Source File: MaximaRoutines.java From Gaalop with GNU Lesser General Public License v3.0 | 6 votes |
/** * Returns an expression from a maxima output string. * @param maximaOut The maxima output string * @return The according expression * @throws RecognitionException */ public static Expression getExpressionFromMaximaOutput(String maximaOut) { try { ANTLRInputStream inputStream = new ANTLRInputStream(new StringReader(maximaOut)); MaximaLexer lexer = new MaximaLexer(inputStream); CommonTokenStream tokenStream = new CommonTokenStream(lexer); MaximaParser parser = new MaximaParser(tokenStream); MaximaVisitor visitor = new MaximaVisitor(); Expression expression = visitor.visit(parser.expression()); return unfoldSimplePows(expression); } catch (IOException ex) { Logger.getLogger(MaximaRoutines.class.getName()).log(Level.SEVERE, null, ex); } return null; }
Example #12
Source File: Test.java From cs652 with BSD 3-Clause "New" or "Revised" License | 6 votes |
public static void main(String[] args) { String prop = "id=\"parrt\"\n"; ANTLRInputStream input = new ANTLRInputStream(prop); PropertyFileLexer lexer = new PropertyFileLexer(input); CommonTokenStream tokens = new CommonTokenStream(lexer); PropertyFileParser parser = new PropertyFileParser(tokens); ParseTree tree = parser.file(); System.out.println(tree.toStringTree(parser)); // Trees.inspect(tree, parser); ParseTreeWalker walker = new ParseTreeWalker(); PropertyFileLoader loader = new PropertyFileLoader(); walker.walk(loader, tree); System.out.println(loader.props); }
Example #13
Source File: Load.java From cs652 with BSD 3-Clause "New" or "Revised" License | 6 votes |
public static void main(String[] args) throws IOException { ANTLRInputStream input = new ANTLRFileStream("colleges.csv"); CSVLexer lexer = new CSVLexer(input); CommonTokenStream tokens = new CommonTokenStream(lexer); CSVParser parser = new CSVParser(tokens); ParseTree tree = parser.file(); System.out.println(tree.toStringTree(parser)); ParseTreeWalker walker = new ParseTreeWalker(); CSVLoader loader = new CSVLoader(); walker.walk(loader, tree); for (List<String> row : loader.data) { System.out.println(row); } }
Example #14
Source File: Trans.java From cs652 with BSD 3-Clause "New" or "Revised" License | 6 votes |
public static void main(String[] args) { String code = "int x;\n" + "A b;\n"; ANTLRInputStream input = new ANTLRInputStream(code); LangLexer lexer = new LangLexer(input); CommonTokenStream tokens = new CommonTokenStream(lexer); LangParser parser = new LangParser(tokens); ParseTree tree = parser.file(); // start up System.out.println(tree.toStringTree(parser)); ParseTreeWalker walker = new ParseTreeWalker(); Gen listener = new Gen(); walker.walk(listener, tree); ST output = listener.file.getTemplate(); System.out.println(output.render()); }
Example #15
Source File: Trans.java From cs652 with BSD 3-Clause "New" or "Revised" License | 6 votes |
public static void main(String[] args) { String code = "int x;\n" + "A b;\n"; ANTLRInputStream input = new ANTLRInputStream(code); LangLexer lexer = new LangLexer(input); CommonTokenStream tokens = new CommonTokenStream(lexer); LangParser parser = new LangParser(tokens); ParseTree tree = parser.file(); // start up System.out.println(tree.toStringTree(parser)); ParseTreeWalker walker = new ParseTreeWalker(); Gen listener = new Gen(); walker.walk(listener, tree); for (String decl : listener.decls) { System.out.println(decl); } }
Example #16
Source File: GenCallGraph.java From cs652 with BSD 3-Clause "New" or "Revised" License | 5 votes |
public static void main(String[] args) throws IOException { LangLexer lexer = new LangLexer(new ANTLRInputStream(input)); CommonTokenStream tokens = new CommonTokenStream(lexer); LangParser parser = new LangParser(tokens); ParserRuleContext tree = parser.file(); LinkViz graph = new LinkViz(); ParseTreeWalker walker = new ParseTreeWalker(); CollectCalls calls = new CollectCalls(graph); walker.walk(calls, tree); System.out.println(GenDOT.gen(graph)); }
Example #17
Source File: MainTest.java From gramtest with Apache License 2.0 | 5 votes |
/** * Test with arithmetic expressions grammar * @throws java.io.IOException */ @Test public void testArithExpGram() throws IOException { Lexer lexer = new bnfLexer(new ANTLRInputStream(getClass().getResourceAsStream("/arithexp.bnf"))); CommonTokenStream tokens = new CommonTokenStream(lexer); bnfParser grammarparser = new bnfParser(tokens); ParserRuleContext tree = grammarparser.rulelist(); GeneratorVisitor extractor = new GeneratorVisitor(); extractor.visit(tree); List<String> generatedTests = extractor.getTests(); Assert.assertEquals(100, generatedTests.size()); }
Example #18
Source File: JavaFilePage.java From jd-gui with GNU General Public License v3.0 | 5 votes |
public JavaFilePage(API api, Container.Entry entry) { super(api, entry); // Load content file String text = TextReader.getText(entry.getInputStream()).replace("\r\n", "\n").replace('\r', '\n'); // Parse DeclarationListener declarationListener = new DeclarationListener(entry); ReferenceListener referenceListener = new ReferenceListener(entry); ANTLRJavaParser.parse(new ANTLRInputStream(text), declarationListener); referenceListener.init(declarationListener); ANTLRJavaParser.parse(new ANTLRInputStream(text), referenceListener); // Display setText(text); initLineNumbers(); }
Example #19
Source File: Python3FileAnalyzer.java From steady with Apache License 2.0 | 5 votes |
/** * <p>Getter for the field <code>constructs</code>.</p> * * @param m a {@link java.io.InputStream} object. * @return a {@link java.util.Map} object. * @throws com.sap.psr.vulas.FileAnalysisException if any. * @throws java.io.IOException if any. * @throws org.antlr.v4.runtime.RecognitionException if any. */ public Map<ConstructId, Construct> getConstructs(InputStream m) throws FileAnalysisException, IOException, RecognitionException { final ANTLRInputStream input = new ANTLRInputStream(m); final Python3Lexer lexer = new Python3Lexer(input); final CommonTokenStream tokens = new CommonTokenStream(lexer); final Python3Parser parser = new Python3Parser(tokens); final ParseTree root = parser.file_input(); final ParseTreeWalker walker = new ParseTreeWalker(); try { walker.walk(this, root); } catch(IllegalStateException ise) { throw new FileAnalysisException(ise.getMessage(), ise); } // Update module body after the parsing of the entire file if(this.stmts!=null && this.stmts.size()>0) { final StringBuffer b = new StringBuffer(); for(String stmt: this.stmts) if(!stmt.trim().equals("")) b.append(stmt); this.constructs.get(this.module).setContent(b.toString()); } return this.constructs; }
Example #20
Source File: TestLaLa.java From cs652 with BSD 3-Clause "New" or "Revised" License | 5 votes |
public static void main(String[] args) throws Exception { ANTLRInputStream input = new ANTLRFileStream("bad.lala"); LaLaLexer lexer = new LaLaLexer(input); CommonTokenStream tokens = new CommonTokenStream(lexer); LaLaParser parser = new LaLaParser(tokens); ParseTree tree = parser.prog(); System.out.println(tree.toStringTree(parser)); ParseTreeWalker walker = new ParseTreeWalker(); DefSymbols def = new DefSymbols(); walker.walk(def, tree); System.out.println(def.globals.symbols); }
Example #21
Source File: APathToXPathConverter.java From archie with Apache License 2.0 | 5 votes |
public static String convertWithAntlr(String query) { XPathLexer lexer = new XPathLexer(new ANTLRInputStream(query)); XPathParser parser = new XPathParser(new CommonTokenStream(lexer)); MainContext mainCtx = parser.main(); StringBuilder output = new StringBuilder(); writeTree(output, mainCtx, false); return output.toString(); }
Example #22
Source File: Antlr4ParserTest.java From openCypher with Apache License 2.0 | 5 votes |
public void investigateTokenStream() throws IOException { // Keep: Not really testing things but quite useful for debugging antlr lexing String query = "CREATE (a)"; org.antlr.v4.Tool tool = new org.antlr.v4.Tool(); GrammarRootAST ast = tool.parseGrammarFromString( new String( Files.readAllBytes(Paths.get("../../grammar/generated/Cypher.g4"))) ); org.antlr.v4.tool.Grammar g = tool.createGrammar( ast ); tool.process( g, false ); LexerInterpreter lexer = g.createLexerInterpreter( new ANTLRInputStream( query ) ); CommonTokenStream tokenStream = new CommonTokenStream( lexer ); }
Example #23
Source File: Antlr4TestUtils.java From openCypher with Apache License 2.0 | 5 votes |
private static void parseWithListeners( Grammar grammar, String query, ANTLRErrorListener lexerListener, ANTLRErrorListener parserListener ) { LexerInterpreter lexer = grammar.createLexerInterpreter( new ANTLRInputStream( query ) ); ParserInterpreter parser = grammar.createParserInterpreter( new CommonTokenStream( lexer ) ); lexer.removeErrorListeners(); parser.removeErrorListeners(); lexer.addErrorListener( lexerListener ); parser.addErrorListener( parserListener ); parser.parse( grammar.getRule( Antlr4.PREFIX + "Cypher" ).index ); }
Example #24
Source File: G4Processor.java From openCypher with Apache License 2.0 | 5 votes |
public Grammar processStream(InputStream inStream) { try { return processAntrlStream(new ANTLRInputStream(inStream)); } catch (IOException e) { throw new RuntimeException("Failed to read or convert java.io.InputStream", e); } }
Example #25
Source File: AntlrParser.java From rapidminer-studio with GNU Affero General Public License v3.0 | 5 votes |
/** * Parses the expression using antlr, aborts the parsing on the first error. * * @param expression * an expression, not {@code null} * @return a {@link ParseTree} for further processing * @throws ExpressionException */ ParseTree parseExpression(String expression) throws ExpressionException { if (expression == null) { throw new IllegalArgumentException("expression must not be null"); } ANTLRInputStream in = new ANTLRInputStream(expression); FunctionExpressionLexer lexer = new CapitulatingFunctionExpressionLexer(in); CommonTokenStream tokens = new CommonTokenStream(lexer); FunctionExpressionParser parser = new FunctionExpressionParser(tokens); parser.removeErrorListeners(); lexer.removeErrorListeners(); ExpressionErrorListener listener = new ExpressionErrorListener(); parser.addErrorListener(listener); lexer.addErrorListener(listener); parser.setErrorHandler(new CapitulatingErrorStrategy()); try { ParseTree tree = parser.operationExp(); if (listener.containsError()) { throw new ExpressionException(listener.getErrorMessage(), listener.getErrorLine()); } else { return tree; } } catch (CapitulatingRuntimeException e) { if (listener.containsError()) { throw new ExpressionException(listener.getErrorMessage(), listener.getErrorLine()); } else { // cannot happen since the parser and lexer always register the error before trying // to recover throw new ExpressionException("Unknown error"); } } }
Example #26
Source File: JsonLexerTest.java From netbeans with Apache License 2.0 | 5 votes |
public void testCommentLex() throws Exception { final ANTLRInputStream in = new ANTLRInputStream("{\"a\":1 /*comment*/\n}"); //NOI18N final List<String> expected = Arrays.asList("{","\"a\"",":","1"," ","\n","}"); //NOI18N final JsonLexer lexer = new JsonLexer(in, false); final List<String> result = lexer.getAllTokens().stream().map((t)->t.getText()).collect(Collectors.toList()); assertEquals(expected, result); }
Example #27
Source File: ParseTest.java From RankPL with MIT License | 5 votes |
private AbstractExpression parseExpr(String code) { CharStream charStream = new ANTLRInputStream(code); RankPLLexer lexer = new RankPLLexer(charStream); TokenStream tokens = new CommonTokenStream(lexer); RankPLParser parser = new RankPLParser(tokens); ConcreteParser classVisitor = new ConcreteParser(); ExpContext ctx = parser.exp(); AbstractExpression res = (AbstractExpression)classVisitor.visit(ctx); return res; }
Example #28
Source File: ParseTest.java From RankPL with MIT License | 5 votes |
private LanguageElement parseOpenStatement(String code) { CharStream charStream = new ANTLRInputStream(code); RankPLLexer lexer = new RankPLLexer(charStream); TokenStream tokens = new CommonTokenStream(lexer); RankPLParser parser = new RankPLParser(tokens); ConcreteParser classVisitor = new ConcreteParser(); return ((Program)classVisitor.visit(parser.program())).getBody(); }
Example #29
Source File: RPLBaseTest.java From RankPL with MIT License | 5 votes |
protected static Object evalProgram(String program) { CharStream charStream = new ANTLRInputStream(program); RankPLLexer lexer = new RankPLLexer(charStream); TokenStream tokens = new CommonTokenStream(lexer); RankPLParser parser = new RankPLParser(tokens); ConcreteParser classVisitor = new ConcreteParser(); ExpContext ctx = parser.exp(); AbstractExpression res = (AbstractExpression)classVisitor.visit(ctx); return res; }
Example #30
Source File: MainTest.java From gramtest with Apache License 2.0 | 5 votes |
/** * Test with main grammar * @throws java.io.IOException */ @Test public void testMainStrGram() throws IOException { Lexer lexer = new bnfLexer(new ANTLRInputStream(getClass().getResourceAsStream("/main.bnf"))); CommonTokenStream tokens = new CommonTokenStream(lexer); bnfParser grammarparser = new bnfParser(tokens); ParserRuleContext tree = grammarparser.rulelist(); GeneratorVisitor extractor = new GeneratorVisitor(100,2,1,10,true); extractor.visit(tree); List<String> generatedTests = extractor.getTests(); Assert.assertEquals(100, generatedTests.size()); }