List of usage examples for org.antlr.v4.runtime CommonTokenStream CommonTokenStream
public CommonTokenStream(TokenSource tokenSource)
From source file:mbtarranger.languages.mcrlOuter.Glue.java
License:Open Source License
private static <T_Pass extends org.antlr.v4.runtime.tree.ParseTreeListener & IBaseListenerWithFileContext> T_Pass extractSinglePass( File osPath, Class<T_Pass> cls) throws IOException, RecognitionException, MbtArrangerException { McrlOuterLexer lexer;// w ww . j av a 2s. co m lexer = new McrlOuterLexer(new ANTLRFileStream(osPath.getAbsolutePath())); CommonTokenStream tokens = new CommonTokenStream(lexer); McrlOuterParser p = new McrlOuterParser(tokens); p.setBuildParseTree(true); p.setErrorHandler(new ErrorReporter(osPath)); T_Pass pass1; try { pass1 = cls.newInstance(); } catch (InstantiationException | IllegalAccessException e) { throw new MbtArrangerException("misdesigned generics using reflection to type instantiation", e); } pass1.SetFileContext(osPath); p.setBuildParseTree(true); p.addParseListener(pass1); try { p.root(); } catch (NullPointerException nulex) { throw new MbtArrangerException( /* NullPointerExceptions caught here mean, that the parser needs additional logic to handle grammar robustly */ "ARRANGE parser failed", nulex); } return pass1; }
From source file:mbtarranger.languages.promelaOuter.Glue.java
License:Open Source License
private static <T_Pass extends PromelaOuterBaseListenerWithFileContext> T_Pass extractSinglePass(File osPath, Class<T_Pass> cls) throws IOException, RecognitionException, MbtArrangerException { PromelaOuterLexer lexer;//from w w w. ja v a 2 s .c om lexer = new PromelaOuterLexer(new ANTLRFileStream(osPath.getAbsolutePath())); CommonTokenStream tokens = new CommonTokenStream(lexer); PromelaOuterParser p = new PromelaOuterParser(tokens); p.setBuildParseTree(true); p.setErrorHandler(new ErrorReporter(osPath)); T_Pass pass1; try { pass1 = cls.newInstance(); } catch (InstantiationException | IllegalAccessException e) { throw new MbtArrangerException("misdesigned generics using reflection to type instantiation", e); } pass1.SetFileContext(osPath); p.setBuildParseTree(true); p.addParseListener(pass1); try { p.root(); } catch (NullPointerException nulex) { throw new MbtArrangerException( /* NullPointerExceptions caught here mean, that the parser needs additional logic to handle grammar robustly */ "ARRANGE parser failed", nulex); } return pass1; }
From source file:mbtarranger.languages.smv.Glue.java
License:Open Source License
private static <T_Pass extends org.antlr.v4.runtime.tree.ParseTreeListener & IBaseListenerWithFileContext> T_Pass extractSinglePass( File osPath, Class<T_Pass> cls) throws IOException, RecognitionException, MbtArrangerException { SmvLexer lexer;/*from w ww. ja v a2 s . co m*/ lexer = new SmvLexer(new ANTLRFileStream(osPath.getAbsolutePath())); CommonTokenStream tokens = new CommonTokenStream(lexer); SmvParser p = new SmvParser(tokens); p.setBuildParseTree(true); p.setErrorHandler(new ErrorReporter(osPath)); T_Pass pass1; try { pass1 = cls.newInstance(); } catch (InstantiationException | IllegalAccessException e) { throw new MbtArrangerException("misdesigned generics using reflection to type instantiation", e); } pass1.SetFileContext(osPath); p.setBuildParseTree(true); p.addParseListener(pass1); try { p.nuSmvModel(); } catch (NullPointerException nulex) { throw new MbtArrangerException( /* * NullPointerExceptions caught here mean, that the parser needs * additional logic to handle grammar robustly */ "ARRANGE parser failed", nulex); } return pass1; }
From source file:minij.parse.Parser.java
License:Open Source License
public static Program parse(Configuration config) throws ParserException { try {/*from w ww . j ava 2 s. com*/ ANTLRFileStream reader = new ANTLRFileStream(config.inputFile); MiniJLexer lexer = new MiniJLexer((CharStream) reader); TokenStream tokens = new CommonTokenStream(lexer); MiniJParser parser = new MiniJParser(tokens); ParserErrorListener listener = new ParserErrorListener(); parser.addErrorListener(listener); ParseTree parseTree = parser.prog(); if (listener.didErrorOccurr()) { throw new ParserException("Could not parse file"); } ASTVisitor astVisitor = new ASTVisitor(); Program program = (Program) astVisitor.visit(parseTree); Logger.logVerbosely("Successfully parsed input file"); if (config.printSourceCode) { Logger.log(program.accept(new PrettyPrintVisitor(""))); } return program; } catch (IOException e) { throw new ParserException("Antlr could not open file " + config.inputFile, e); } catch (ParserException e) { throw e; } catch (Exception e) { throw new ParserException("Could not parse file", e); } }
From source file:miniportugol.Run.java
/** * @param args the command line arguments *///from w w w . j ava 2 s.c om public static void main(String[] args) { ANTLRInputStream input = new ANTLRInputStream("2/2+2\n"); MiniPortugolLexer lexer = new MiniPortugolLexer(input); CommonTokenStream tokens = new CommonTokenStream(lexer); MiniPortugolParser parser = new MiniPortugolParser(tokens); ParseTree tree = parser.prog(); PortugolVisitor eval = new PortugolVisitor(); eval.visit(tree); }
From source file:mips.Program.java
/** * * @param filename/*from w w w. j av a 2s. c o m*/ */ public Program(String filename) { try { // Invoke parser ANTLRInputStream input = new ANTLRFileStream(filename); MipsLexer lexer = new MipsLexer(input); CommonTokenStream tokens = new CommonTokenStream(lexer); MipsParser parser = new MipsParser(tokens); RuleContext tree = parser.prog(); parser.checkLabels(); memory = parser.getMemory(); codeSnippet = parser.getInstructions(); labelMap = parser.getLabelMap(); System.out.println("Total Instructions: " + codeSnippet.size()); Iterator it = labelMap.entrySet().iterator(); while (it.hasNext()) { Map.Entry pairs = (Map.Entry) it.next(); System.out.println("Label " + pairs.getKey() + ": instruction index = " + pairs.getValue()); } // TODO: fetch labelMap, and execute instructions accordingly code = new ArrayList<Instruction>(); int addr = 0; for (Instruction instruction : codeSnippet) { Instruction clonedInstruction = instruction.clone(); clonedInstruction.id = 0; clonedInstruction.presentStage = 0; clonedInstruction.stageToExecute = 1; clonedInstruction.address = addr; code.add(clonedInstruction); addr++; } sepInstructions = new ArrayList<ArrayList<Instruction>>(totalStages); for (int i = 0; i < totalStages; i++) { sepInstructions.add(new ArrayList<Instruction>()); } } catch (IOException ex) { Logger.getLogger(Program.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:mongofx.js.support.JsAntlrPathBuilder.java
License:Open Source License
public static Optional<List<String>> buildPath(String jsCode, int position) { if (position <= 0) { return Optional.of(Collections.singletonList("")); }/* w w w.jav a 2 s. c om*/ position--; ECMAScriptLexer lexer = new ECMAScriptLexer(new ANTLRInputStream(jsCode)); ECMAScriptParser parser = new ECMAScriptParser(new CommonTokenStream(lexer)); ScriptVisitor scriptVisitor = new ScriptVisitor(position); scriptVisitor.visit(parser.program()); return scriptVisitor.getPath(); }
From source file:mut.lexparse.LexerParserFactory.java
License:Open Source License
/** * @param inputText//from w ww. j ava 2 s. co m * @return */ static public MutatorParser makeParser(ANTLRInputStream inputText) { final MutatorLexer lexer = makeLexer(inputText); final CommonTokenStream tokenStream = new CommonTokenStream(lexer); final MutatorParser parser = new MutatorParser(tokenStream); parser.addErrorListener(new BaseErrorListener() { @Override public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) { throw new MutatorParserException(msg, e); } }); return parser; }
From source file:nanoverse.compiler.pipeline.interpret.AntlrBinding.java
License:Open Source License
/** * Read a file, lex it and parse it, visit the tree, and return * the root in the form returned by the visitor. * * @param file/*from w w w .j ava2s . com*/ * @return */ public ASTNode interpret(File file) { try { FileInputStream inputStream = new FileInputStream(file); ANTLRInputStream input = new ANTLRInputStream(inputStream); NanosyntaxLexer lexer = new NanosyntaxLexer(input); CommonTokenStream tokenStream = new CommonTokenStream(lexer); NanosyntaxParser parser = new NanosyntaxParser(tokenStream); ParseTree tree = parser.root(); ASTNode root = visitor.visit(tree); return root; } catch (FileNotFoundException ex) { throw new RuntimeException("File not found: " + file.getAbsolutePath()); } catch (IOException ex) { throw new RuntimeException(ex); } }
From source file:net.certiv.json.converter.Converter.java
License:Open Source License
private JsonContext parse(String srcData, PhaseState state) throws IOException { lastError = "Failure in acquiring input stream."; ByteArrayInputStream is = new ByteArrayInputStream(srcData.getBytes()); ANTLRInputStream input = new ANTLRInputStream(is); input.name = processor.getSourceName(); lastError = "Failure in generating lexer token stream."; JsonLexer lexer = new JsonLexer(input); JsonTokenFactory factory = new JsonTokenFactory(input); lexer.setTokenFactory(factory);/* ww w. java 2 s .c om*/ CommonTokenStream tokens = new CommonTokenStream(lexer); state.tokens = tokens; lastError = "Failure in parser production."; JsonParser parser = new JsonParser(tokens); parser.setTokenFactory(factory); parser.removeErrorListeners(); // remove ConsoleErrorListener parser.addErrorListener(new JsonErrorListener()); // parser.setErrorHandler(new JsonParserErrorStrategy()); return parser.json(); }