List of usage examples for org.antlr.v4.runtime BaseErrorListener BaseErrorListener
BaseErrorListener
From source file:ai.grakn.graql.Autocomplete.java
License:Open Source License
/** * @param query a graql query//from ww w .java 2 s . co m * @return a list of tokens from running the lexer on the query */ private static List<? extends Token> getTokens(String query) { ANTLRInputStream input = new ANTLRInputStream(query); GraqlLexer lexer = new GraqlLexer(input); // Ignore syntax errors lexer.removeErrorListeners(); lexer.addErrorListener(new BaseErrorListener()); return lexer.getAllTokens(); }
From source file:annis.visualizers.htmlvis.VisParser.java
License:Apache License
public VisParser(InputStream inStream) throws IOException, VisParserException { this.definitions = new LinkedList<VisualizationDefinition>(); HTMLVisConfigLexer lexer = new HTMLVisConfigLexer(new ANTLRInputStream(inStream)); HTMLVisConfigParser parser = new HTMLVisConfigParser(new CommonTokenStream(lexer)); final List<String> errors = new LinkedList<String>(); parser.removeErrorListeners();//from ww w. java 2 s .c om parser.addErrorListener(new BaseErrorListener() { @Override public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) { errors.add("line " + line + ":" + charPositionInLine + " " + msg); } }); ParseTree tree = parser.start(); if (errors.isEmpty()) { ParseTreeWalker walker = new ParseTreeWalker(); walker.walk((VisParser) this, tree); } else { throw new VisParserException("Parser error:\n" + StringUtils.join(errors, "\n")); } }
From source file:beast.util.TreeParser.java
License:Open Source License
/** * Parse a newick-ish string and generate the BEAST tree it describes. * * @param newick string to parse//from w w w . j a va2s.c om * @return root node of tree */ public Node parseNewick(String newick) { CharStream charStream = CharStreams.fromString(newick); // Custom parse/lexer error listener BaseErrorListener errorListener = new BaseErrorListener() { @Override public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) { throw new TreeParsingException(msg, charPositionInLine, line); } }; // Use lexer to produce token stream NewickLexer lexer = new NewickLexer(charStream); lexer.removeErrorListeners(); lexer.addErrorListener(errorListener); CommonTokenStream tokens = new CommonTokenStream(lexer); // Parse token stream to produce parse tree NewickParser parser = new NewickParser(tokens); parser.removeErrorListeners(); parser.addErrorListener(errorListener); ParseTree parseTree = parser.tree(); // Traverse parse tree, constructing BEAST tree along the way NewickASTVisitor visitor = new NewickASTVisitor(); return visitor.visit(parseTree); }
From source file:boa.compiler.BoaCompiler.java
License:Apache License
public static void main(final String[] args) throws IOException { CommandLine cl = processCommandLineOptions(args); if (cl == null) return;/* ww w . j a v a 2 s . c o m*/ final ArrayList<File> inputFiles = BoaCompiler.inputFiles; // get the name of the generated class final String className = getGeneratedClass(cl); // get the filename of the jar we will be writing final String jarName; if (cl.hasOption('o')) jarName = cl.getOptionValue('o'); else jarName = className + ".jar"; // make the output directory final File outputRoot = new File(new File(System.getProperty("java.io.tmpdir")), UUID.randomUUID().toString()); final File outputSrcDir = new File(outputRoot, "boa"); if (!outputSrcDir.mkdirs()) throw new IOException("unable to mkdir " + outputSrcDir); // find custom libs to load final List<URL> libs = new ArrayList<URL>(); if (cl.hasOption('l')) for (final String lib : cl.getOptionValues('l')) libs.add(new File(lib).toURI().toURL()); final File outputFile = new File(outputSrcDir, className + ".java"); final BufferedOutputStream o = new BufferedOutputStream(new FileOutputStream(outputFile)); try { final List<String> jobnames = new ArrayList<String>(); final List<String> jobs = new ArrayList<String>(); boolean isSimple = true; final List<Program> visitorPrograms = new ArrayList<Program>(); SymbolTable.initialize(libs); for (int i = 0; i < inputFiles.size(); i++) { final File f = inputFiles.get(i); try { final BoaLexer lexer = new BoaLexer(new ANTLRFileStream(f.getAbsolutePath())); lexer.removeErrorListeners(); lexer.addErrorListener(new LexerErrorListener()); final CommonTokenStream tokens = new CommonTokenStream(lexer); final BoaParser parser = new BoaParser(tokens); parser.removeErrorListeners(); parser.addErrorListener(new BaseErrorListener() { @Override public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) throws ParseCancellationException { throw new ParseCancellationException(e); } }); final BoaErrorListener parserErrorListener = new ParserErrorListener(); final Start p = parse(tokens, parser, parserErrorListener); if (cl.hasOption("ast")) new ASTPrintingVisitor().start(p); final String jobName = "" + i; try { if (!parserErrorListener.hasError) { new TypeCheckingVisitor().start(p, new SymbolTable()); final TaskClassifyingVisitor simpleVisitor = new TaskClassifyingVisitor(); simpleVisitor.start(p); LOG.info(f.getName() + ": task complexity: " + (!simpleVisitor.isComplex() ? "simple" : "complex")); isSimple &= !simpleVisitor.isComplex(); new InheritedAttributeTransformer().start(p); new LocalAggregationTransformer().start(p); // if a job has no visitor, let it have its own method // also let jobs have own methods if visitor merging is disabled if (!simpleVisitor.isComplex() || cl.hasOption("nv") || inputFiles.size() == 1) { new VisitorOptimizingTransformer().start(p); if (cl.hasOption("pp")) new PrettyPrintVisitor().start(p); if (cl.hasOption("ast")) new ASTPrintingVisitor().start(p); final CodeGeneratingVisitor cg = new CodeGeneratingVisitor(jobName); cg.start(p); jobs.add(cg.getCode()); jobnames.add(jobName); } // if a job has visitors, fuse them all together into a single program else { p.getProgram().jobName = jobName; visitorPrograms.add(p.getProgram()); } } } catch (final TypeCheckException e) { parserErrorListener.error("typecheck", lexer, null, e.n.beginLine, e.n.beginColumn, e.n2.endColumn - e.n.beginColumn + 1, e.getMessage(), e); } } catch (final Exception e) { System.err.print(f.getName() + ": compilation failed: "); e.printStackTrace(); } } final int maxVisitors; if (cl.hasOption('v')) maxVisitors = Integer.parseInt(cl.getOptionValue('v')); else maxVisitors = Integer.MAX_VALUE; if (!visitorPrograms.isEmpty()) try { for (final Program p : new VisitorMergingTransformer().mergePrograms(visitorPrograms, maxVisitors)) { new VisitorOptimizingTransformer().start(p); if (cl.hasOption("pp")) new PrettyPrintVisitor().start(p); if (cl.hasOption("ast")) new ASTPrintingVisitor().start(p); final CodeGeneratingVisitor cg = new CodeGeneratingVisitor(p.jobName); cg.start(p); jobs.add(cg.getCode()); jobnames.add(p.jobName); } } catch (final Exception e) { System.err.println("error fusing visitors - falling back: " + e); e.printStackTrace(); for (final Program p : visitorPrograms) { new VisitorOptimizingTransformer().start(p); if (cl.hasOption("pp")) new PrettyPrintVisitor().start(p); if (cl.hasOption("ast")) new ASTPrintingVisitor().start(p); final CodeGeneratingVisitor cg = new CodeGeneratingVisitor(p.jobName); cg.start(p); jobs.add(cg.getCode()); jobnames.add(p.jobName); } } if (jobs.size() == 0) throw new RuntimeException("no files compiled without error"); final ST st = AbstractCodeGeneratingVisitor.stg.getInstanceOf("Program"); st.add("name", className); st.add("numreducers", inputFiles.size()); st.add("jobs", jobs); st.add("jobnames", jobnames); st.add("combineTables", CodeGeneratingVisitor.combineAggregatorStrings); st.add("reduceTables", CodeGeneratingVisitor.reduceAggregatorStrings); st.add("splitsize", isSimple ? 64 * 1024 * 1024 : 10 * 1024 * 1024); o.write(st.render().getBytes()); } finally { o.close(); } compileGeneratedSrc(cl, jarName, outputRoot, outputFile); }
From source file:boa.compiler.BoaCompiler.java
License:Apache License
public static void parseOnly(final String[] args) throws IOException { CommandLine cl = processParseCommandLineOptions(args); if (cl == null) return;//from w w w .ja va 2s . c o m final ArrayList<File> inputFiles = BoaCompiler.inputFiles; // find custom libs to load final List<URL> libs = new ArrayList<URL>(); if (cl.hasOption('l')) for (final String lib : cl.getOptionValues('l')) libs.add(new File(lib).toURI().toURL()); final List<String> jobnames = new ArrayList<String>(); final List<String> jobs = new ArrayList<String>(); boolean isSimple = true; final List<Program> visitorPrograms = new ArrayList<Program>(); SymbolTable.initialize(libs); for (int i = 0; i < inputFiles.size(); i++) { final File f = inputFiles.get(i); try { final BoaLexer lexer = new BoaLexer(new ANTLRFileStream(f.getAbsolutePath())); lexer.removeErrorListeners(); lexer.addErrorListener(new LexerErrorListener()); final CommonTokenStream tokens = new CommonTokenStream(lexer); final BoaParser parser = new BoaParser(tokens); parser.removeErrorListeners(); parser.addErrorListener(new BaseErrorListener() { @Override public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) throws ParseCancellationException { throw new ParseCancellationException(e); } }); final BoaErrorListener parserErrorListener = new ParserErrorListener(); final Start p = parse(tokens, parser, parserErrorListener); if (cl.hasOption("ast")) new ASTPrintingVisitor().start(p); final String jobName = "" + i; try { if (!parserErrorListener.hasError) { new TypeCheckingVisitor().start(p, new SymbolTable()); final TaskClassifyingVisitor simpleVisitor = new TaskClassifyingVisitor(); simpleVisitor.start(p); LOG.info(f.getName() + ": task complexity: " + (!simpleVisitor.isComplex() ? "simple" : "complex")); isSimple &= !simpleVisitor.isComplex(); new InheritedAttributeTransformer().start(p); new LocalAggregationTransformer().start(p); // if a job has no visitor, let it have its own method // also let jobs have own methods if visitor merging is disabled if (!simpleVisitor.isComplex() || cl.hasOption("nv") || inputFiles.size() == 1) { new VisitorOptimizingTransformer().start(p); if (cl.hasOption("pp")) new PrettyPrintVisitor().start(p); if (cl.hasOption("ast")) new ASTPrintingVisitor().start(p); final CodeGeneratingVisitor cg = new CodeGeneratingVisitor(jobName); cg.start(p); jobs.add(cg.getCode()); jobnames.add(jobName); } // if a job has visitors, fuse them all together into a single program else { p.getProgram().jobName = jobName; visitorPrograms.add(p.getProgram()); } } } catch (final TypeCheckException e) { parserErrorListener.error("typecheck", lexer, null, e.n.beginLine, e.n.beginColumn, e.n2.endColumn - e.n.beginColumn + 1, e.getMessage(), e); } } catch (final Exception e) { System.err.print(f.getName() + ": parsing failed: "); e.printStackTrace(); } } }
From source file:ch.raffael.contracts.devtools.ast.AstInspector.java
License:Apache License
public void parse(final String source) { final StringBuilder errors = new StringBuilder(); final StringBuilder trace = new StringBuilder("Log:\n"); ANTLRErrorListener errorListener = new BaseErrorListener() { @Override//from w w w .j a v a2s.c om public void syntaxError(Recognizer<?, ?> recognizer, @Nullable Object offendingSymbol, int line, int charPositionInLine, String msg, @Nullable RecognitionException e) { errors.append("Line ").append(line).append(':').append(charPositionInLine).append(": ").append(msg) .append('\n'); } }; final CelLexer lexer = new CelLexer(new ANTLRInputStream(source)); final CelParser parser = new CelParser(new CommonTokenStream(lexer)); lexer.addErrorListener(errorListener); parser.addErrorListener(errorListener); parser.addParseListener(new ParseTreeListener() { @Override public void enterEveryRule(ParserRuleContext ctx) { trace.append(" enter\t").append(parser.getRuleNames()[ctx.getRuleIndex()]).append(", LT(1)=") .append(parser.getTokenStream().LT(1).getText()).append('\n'); } @Override public void visitTerminal(TerminalNode node) { trace.append(" consume\t").append(node.getSymbol()).append(" rule ") .append(parser.getRuleNames()[parser.getContext().getRuleIndex()]).append('\n'); } @Override public void visitErrorNode(ErrorNode node) { } @Override public void exitEveryRule(ParserRuleContext ctx) { trace.append(" exit\t").append(parser.getRuleNames()[ctx.getRuleIndex()]).append(", LT(1)=") .append(parser.getTokenStream().LT(1).getText()).append('\n'); } }); parser.setBuildParseTree(true); AstBuilder builder = new AstBuilder(); builder.install(parser); final CelParser.ClauseContext rootContext = parser.clause(); if (errors.length() != 0) { errors.append('\n'); } Runnable guiUpdate = new Runnable() { @Override public void run() { log.setText(errors.toString() + trace.toString()); log.setSelectionStart(0); log.setSelectionEnd(0); if (rootContext == null || rootContext.node == null) { syntaxTree.setModel(new DefaultTreeModel(AstTreeNode.empty())); updateParseTree(null); } else { syntaxTree.setModel(new DefaultTreeModel(AstTreeNode.from(rootContext.node))); updateParseTree(new TreeViewer(Arrays.asList(parser.getRuleNames()), rootContext)); } for (int i = 0; i < syntaxTree.getRowCount(); i++) { syntaxTree.expandRow(i); } } }; if (SwingUtilities.isEventDispatchThread()) { guiUpdate.run(); } else { SwingUtilities.invokeLater(guiUpdate); } }
From source file:ch.raffael.contracts.processor.cel.Compiler.java
License:Apache License
public Clause parse() throws ParseException { ANTLRErrorListener errorListener = new BaseErrorListener() { @Override//from w ww. j a v a2 s .co m public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) { addANTLRError(e, msg); } }; CelLexer lexer = new CelLexer(new ANTLRInputStream(expression)); lexer.addErrorListener(errorListener); CelParser parser = new CelParser(new CommonTokenStream(lexer)); parser.addErrorListener(errorListener); try { Clause ast = new AstBuilder().install(parser).clause().node; if (ast == null) { if (errors.isEmpty()) { throw new IllegalStateException("No AST returned, but no errors reported"); } throw new ParseException(errors); } return ast; } catch (RecognitionException e) { // should not happen addANTLRError(e, "Unexpected: " + e.toString()); throw new ParseException(errors); } }
From source file:com.chiralbehaviors.CoRE.universal.Spa.java
License:Open Source License
public static Spa manifest(String resource) throws IOException { SpaLexer l = new SpaLexer(CharStreams.fromStream(Utils.resolveResource(Spa.class, resource))); SpaParser p = new SpaParser(new CommonTokenStream(l)); p.addErrorListener(new BaseErrorListener() { @Override//from w w w .j av a2s. c o m public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) { throw new IllegalStateException("failed to parse at line " + line + " due to " + msg, e); } }); SpaContext spa = p.spa(); SpaImporter importer = new SpaImporter(); ParseTreeWalker walker = new ParseTreeWalker(); walker.walk(importer, spa); return importer.getSpa(); }
From source file:com.cisco.yangide.core.parser.YangParserUtil.java
License:Open Source License
public static YangContext parseYangSource(char[] content, final IYangValidationListener validationListener) { final ANTLRInputStream input = new ANTLRInputStream(content, content.length); final YangLexer lexer = new YangLexer(input); final CommonTokenStream tokens = new CommonTokenStream(lexer); final YangParser parser = new YangParser(tokens); parser.removeErrorListeners();/* w w w . j a v a 2 s .c o m*/ if (validationListener != null) { parser.addErrorListener(new BaseErrorListener() { @Override public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) { int charStart = 0; int charEnd = 0; if (offendingSymbol != null && offendingSymbol instanceof Token) { charStart = ((Token) offendingSymbol).getStartIndex(); charEnd = ((Token) offendingSymbol).getStopIndex() + 1; } validationListener.syntaxError(msg, line, charStart, charEnd); } }); } return parser.yang(); }
From source file:com.cloudbees.plugins.credentials.CredentialsMatchers.java
License:Open Source License
/** * Attempts to parse a Credentials Query Language expression and construct the corresponding matcher. * * @param cql the Credentials Query Language expression to parse. * @return a {@link CredentialsMatcher} for this expression. * @throws CQLSyntaxException if the expression could not be parsed. * @since 2.1.0//from w w w . jav a 2s .c om */ @NonNull public static CredentialsMatcher parse(final String cql) { if (StringUtils.isEmpty(cql)) { return always(); } CQLLexer lexer = new CQLLexer(new ANTLRInputStream(cql)); CommonTokenStream tokens = new CommonTokenStream(lexer); CQLParser parser = new CQLParser(tokens); parser.removeErrorListeners(); parser.addErrorListener(new BaseErrorListener() { @Override public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) { StringBuilder expression = new StringBuilder( cql.length() + msg.length() + charPositionInLine + 256); String[] lines = StringUtils.split(cql, '\n'); for (int i = 0; i < line; i++) { expression.append(" ").append(lines[i]).append('\n'); } expression.append(" ").append(StringUtils.repeat(" ", charPositionInLine)).append("^ ") .append(msg); for (int i = line; i < lines.length; i++) { expression.append("\n ").append(lines[i]); } throw new CQLSyntaxException( String.format("CQL syntax error: line %d:%d%n%s", line, charPositionInLine, expression), charPositionInLine); } }); CQLParser.ExpressionContext expressionContext = parser.expression(); ParseTreeWalker walker = new ParseTreeWalker(); MatcherBuildingListener listener = new MatcherBuildingListener(); try { walker.walk(listener, expressionContext); return listener.getMatcher(); } catch (EmptyStackException e) { throw new IllegalStateException("There should not be an empty stack when starting from an expression", e); } catch (CQLSyntaxError e) { throw new CQLSyntaxException(String.format("CQL syntax error:%n %s%n %s%s unexpected symbol %s", cql, StringUtils.repeat(" ", e.interval.a), StringUtils.repeat("^", e.interval.length()), e.text), e.interval.a); } }