/** * Checks that no exception is generated for YANG file with valid syntax. */ @Test public void checkValidYangFileForNoSyntaxError() throws IOException { ANTLRInputStream input = new ANTLRFileStream("src/test/resources/YangFileWithoutSyntaxError.yang"); // Create a lexer that feeds off of input char stream. GeneratedYangLexer lexer = new GeneratedYangLexer(input); // Create a buffer of tokens pulled from the lexer. CommonTokenStream tokens = new CommonTokenStream(lexer); // Create a parser that feeds off the tokens buffer. GeneratedYangParser parser = new GeneratedYangParser(tokens); // Remove console error listener. parser.removeErrorListeners(); // Create instance of customized error listener. ParseTreeErrorListener parseTreeErrorListener = new ParseTreeErrorListener(); // Add customized error listener to catch errors during parsing. parser.addErrorListener(parseTreeErrorListener); // Begin parsing YANG file and generate parse tree. ParseTree tree = parser.yangfile(); }
/** * Checks that exception is generated for YANG file with invalid syntax. */ @Test public void checkInvalidYangFileForSyntaxError() throws IOException { // Get the exception occurred during parsing. thrown.expect(ParserException.class); thrown.expect(CustomExceptionMatcher.errorLocation(3, 0)); thrown.expectMessage("no viable alternative at input 'yang-version 1\\nnamespace'"); ANTLRInputStream input = new ANTLRFileStream("src/test/resources/YangFileWithSyntaxError.yang"); // Create a lexer that feeds off of input char stream. GeneratedYangLexer lexer = new GeneratedYangLexer(input); // Create a buffer of tokens pulled from the lexer. CommonTokenStream tokens = new CommonTokenStream(lexer); // Create a parser that feeds off the tokens buffer. GeneratedYangParser parser = new GeneratedYangParser(tokens); // Remove console error listener. parser.removeErrorListeners(); // Create instance of customized error listener. ParseTreeErrorListener parseTreeErrorListener = new ParseTreeErrorListener(); // Add customized error listener to catch errors during parsing. parser.addErrorListener(parseTreeErrorListener); // Begin parsing YANG file and generate parse tree. ParseTree tree = parser.yangfile(); }
public static void main(String[] args) throws IOException { CoqLexer lexer = new CoqLexer(new ANTLRFileStream("../TestProgramC.v")); CoqFTParser parser = new CoqFTParser(new CommonTokenStream(lexer)); //Listener listener = new Listener(parser); //parser.addParseListener(listener); //CoqParser.ProgContext ctx = parser.prog(); //System.out.println(ctx.toStringTree(Arrays.asList(CoqParser.ruleNames))); Prog prog = parser.parseProg(); for(Command command : prog) { if(command instanceof AssertionAndProof) { AssertionAndProof ap = (AssertionAndProof) command; System.out.println(ap.getAssertion()); System.out.println("Proof:"); for(Tactic tactic : ap.getProof().getTactics()) { System.out.println(tactic); } } else { System.out.println(command); } } }
/** * @param args * @throws IOException * @throws SQLException */ public static void main(String[] args) throws IOException, SQLException { TPKBLexer lexer = new TPKBLexer(new ANTLRFileStream("test.kb")); TPKBParser parser = new TPKBParser(new CommonTokenStream(lexer)); TPKBClassListener listener = new TPKBClassListener(); ParseTree tree = parser.tpkb(); ParseTreeWalker walker = new ParseTreeWalker(); walker.walk(listener, tree); MySQLEngine engine = new MySQLEngine(); engine.processTSubRaw(); engine.processTIsRaw(); double Z = engine.computeZ(); System.out.println("Z = "+Z); Z = engine.computeZWithEvidence(); System.out.println("Z(E) = "+Z); }
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); } }
@Test public void testParseWorkingExamples() throws IOException { FileVisitor<Path> workingFilesVisitior = new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { System.out.println("Testing parser input from file \""+file.toString()+"\""); ANTLRFileStream antlrStream = new ANTLRFileStream(file.toString()); MiniJLexer lexer = new MiniJLexer(antlrStream); TokenStream tokens = new CommonTokenStream(lexer); MiniJParser parser = new MiniJParser(tokens); parser.setErrorHandler(new BailErrorStrategy()); parser.prog(); return super.visitFile(file, attrs); } }; Files.walkFileTree(EXAMPLE_PROGRAM_PATH_WORKING, workingFilesVisitior); }
@Test public void testParseFailingExamples() throws IOException { FileVisitor<Path> workingFilesVisitior = new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { System.out.println("Testing parser input from file \""+file.toString()+"\""); ANTLRFileStream antlrStream = new ANTLRFileStream(file.toString()); MiniJLexer lexer = new MiniJLexer(antlrStream); TokenStream tokens = new CommonTokenStream(lexer); MiniJParser parser = new MiniJParser(tokens); parser.setErrorHandler(new BailErrorStrategy()); /* * Catch all exceptions first, to ensure that every single * compilation unit exits with an Exception. Otherwise, this * method will return after the first piece of code. */ try { parser.prog(); fail("The example "+file.toString()+" should have failed, but was accepted by the parser."); } catch (ParseCancellationException e) { } return super.visitFile(file, attrs); } }; Files.walkFileTree(EXAMPLE_PROGRAM_PATH_FAILING, workingFilesVisitior); }
@Test public void testVisitTypeErrorExamples() throws Exception { FileVisitor<Path> failingFilesVisitior = new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { if (file.toString().endsWith("LinkedListBUG.java")) { return super.visitFile(file, attrs); } System.out.println("Testing type checker with file \""+file.toString()+"\""); ANTLRFileStream reader = new ANTLRFileStream(file.toString()); MiniJLexer lexer = new MiniJLexer((CharStream) reader); TokenStream tokens = new CommonTokenStream(lexer); MiniJParser parser = new MiniJParser(tokens); ParseTree parseTree = parser.prog(); ASTVisitor astVisitor = new ASTVisitor(); Program ast = (Program) astVisitor.visit(parseTree); TypeInferenceVisitor typeInferenceVisitor = new TypeInferenceVisitor(); ast.accept(typeInferenceVisitor); TypeCheckVisitor visitor = new TypeCheckVisitor(); boolean typesCorrect = ast.accept(visitor); assertFalse("\"" + file.toString() + "\" passed type check but it shouldn't", typesCorrect); return super.visitFile(file, attrs); } }; Files.walkFileTree(EXAMPLE_PROGRAM_PATH_FAILING, failingFilesVisitior); }
@Override public String visitIncludeFile(QPLParser.IncludeFileContext ctx) { String filePath = ctx.path.getText().trim(); filePath = filePath.substring(1, filePath.length() - 1); if (filePath.charAt(0) != '/') { filePath = Paths.get(__FILE_PATH__, filePath).toString(); } try { ANTLRInputStream ais = new ANTLRFileStream(filePath); QPLLexer lex = new QPLLexer(ais); TokenStream toks = new CommonTokenStream(lex); QPLParser parse = new QPLParser(toks); ParseTree tree = parse.prog(); return new ImpVisitor(filePath).visit(tree); } catch (IOException ex) { System.err.println(filePath + " cannot be found! Ignoring"); return ""; } }
public static Walk compile( File file, IcosahedralSymmetry symm ) { String fileName = file.getAbsolutePath(); Walk program = null; ErrorHandler.Default errors = new ErrorHandler.Default(); ZomicASTCompiler compiler = new ZomicASTCompiler(symm ); try { ANTLRFileStream fileStream = new ANTLRFileStream(fileName); program = compiler.compile( fileStream, errors ); if( program != null ) { program.setErrors( errors.getErrors() ); } } catch (IOException ex) { errors.parseError( ErrorHandler.UNKNOWN, ErrorHandler.UNKNOWN, ex.getMessage() ); } return program; }
public static void main(String[] args) throws Exception { String testFile = ""; // set test input JFileChooser chooser = new JFileChooser(); chooser.setCurrentDirectory(new java.io.File(".")); chooser.setDialogTitle("Please choose the master file"); chooser.setAcceptAllFileFilterUsed(false); if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { testFile = chooser.getSelectedFile().getAbsolutePath(); } else { System.exit(0); } Calculator4Lexer lexer = new Calculator4Lexer(new ANTLRFileStream(testFile)); CommonTokenStream tokens = new CommonTokenStream(lexer); Calculator4Parser p = new Calculator4Parser(tokens); p.setBuildParseTree(true); p.addParseListener(new CalculatorListener()); ParserRuleContext t = p.program(); }
public static void main(String[] args) throws Exception { String testFile = ""; // set test input JFileChooser chooser = new JFileChooser(); chooser.setCurrentDirectory(new java.io.File(".")); chooser.setDialogTitle("Please choose the master file"); chooser.setAcceptAllFileFilterUsed(false); if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { testFile = chooser.getSelectedFile().getAbsolutePath(); } else { System.exit(0); } FormLexer lexer = new FormLexer( new ANTLRFileStream(testFile)); CommonTokenStream tokens = new CommonTokenStream(lexer); System.out.println(tokens); FormParser p = new FormParser(tokens); p.setBuildParseTree(true); p.addParseListener(new FormListener()); ParserRuleContext t = p.form(); }
public static void main(String[] args) throws Exception { String testFile = ""; // set test input JFileChooser chooser = new JFileChooser(); chooser.setCurrentDirectory(new java.io.File(".")); chooser.setDialogTitle("Please choose the master file"); chooser.setAcceptAllFileFilterUsed(false); if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { testFile = chooser.getSelectedFile().getAbsolutePath(); } else { System.exit(0); } WordSeparationLexer lexer = new WordSeparationLexer( new ANTLRFileStream(testFile)); CommonTokenStream tokens = new CommonTokenStream(lexer); System.out.println(tokens); WordSeparationParser p = new WordSeparationParser(tokens); p.setBuildParseTree(true); p.addParseListener(new WordSeparationListener()); ParserRuleContext t = p.program(); }
public static void main(String[] args) throws Exception { String testFile = ""; // set test input JFileChooser chooser = new JFileChooser(); chooser.setCurrentDirectory(new java.io.File(".")); chooser.setDialogTitle("Please choose the master file"); chooser.setAcceptAllFileFilterUsed(false); if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { testFile = chooser.getSelectedFile().getAbsolutePath(); } else { System.exit(0); } // create lexer and perser Form2Lexer lexer = new Form2Lexer(new ANTLRFileStream(testFile)); Form2Parser parser = new Form2Parser(new CommonTokenStream(lexer)); // generate tree and visitor ParseTree tree = parser.form(); Form2Visitor visitor = new Form2CustomVisitor(); visitor.visit(tree); }
@SuppressWarnings("unused") private ParsedContext parseFile(final File rootDir, final String protoName) { Throwable exception = null; try { final File file = new File(rootDir, protoName); final ANTLRFileStream ais = new ANTLRFileStream(file.getCanonicalPath()); return parseProto(ais, protoName); } catch (final IOException e) { exception = e; } finally { if (exception != null) { throw new RuntimeException(exception); } } return null; }
/** * Sets up the Java parser. * @param javaFile the Java contents to parse. * @return the {@link JavaParser} instance. * @throws RecognitionException if the comment cannot be parsed. * @throws IOException if the file could not be read. */ @SuppressWarnings("unchecked") @NotNull protected JavaParser setUpParser(@NotNull final File javaFile) throws RecognitionException, IOException { @NotNull final JavaParser result; @NotNull final JavaLexer t_Lexer = new JavaLexer(new ANTLRFileStream(javaFile.getAbsolutePath())); @NotNull final CommonTokenStream t_Tokens = new CommonTokenStream(t_Lexer); result = new JavaParser(t_Tokens); return result; }
/** * Sets up the Java parser. * @param javaFile the Java contents to parse. * @return the {@link JavaParser} instance. * @throws RecognitionException if the format is invalid. * @throws IOException if the source cannot be read. */ @SuppressWarnings("unchecked") @NotNull protected JavaParser setUpParser(@NotNull final File javaFile) throws RecognitionException, IOException { @NotNull final JavaParser result; @NotNull final JavaLexer t_Lexer = new JavaLexer(new ANTLRFileStream(javaFile.getAbsolutePath())); @NotNull final ANTLRErrorListener errorListener = new PropagatingErrorListener(javaFile); t_Lexer.addErrorListener(errorListener); @NotNull final CommonTokenStream t_Tokens = new CommonTokenStream(t_Lexer); result = new JavaParser(t_Tokens); result.addErrorListener(errorListener); return result; }
private void parseFile(File child) { try { currentJavaFile = child; intervals = new ArrayList<Interval>(); input = new ANTLRFileStream(child.getPath()); Java7Lexer lexer = new Java7Lexer(input); CommonTokenStream tokens = new CommonTokenStream(lexer); Java7Parser parser = new Java7Parser(tokens); ParserRuleContext tree = parser.compilationUnit(); ParseTreeWalker walker = new ParseTreeWalker(); walker.walk(this, tree); if (intervals.size()>0) { writeOutputFile(); } } catch (IOException e) { System.err.println("Could not parse " + child.getPath()); e.printStackTrace(); } }
@Test public void parse() throws IOException { System.out.println(file.getName() + "-------------------------------"); final LLVMLexer lexer = new LLVMLexer(new ANTLRFileStream(file.getAbsolutePath())); final CommonTokenStream stream = new CommonTokenStream(lexer); final LLVMParser parser = new LLVMParser(stream); parser.setErrorHandler(new BailErrorStrategy()); final ModuleContext module = parser.module(); System.out.println(Trees.toStringTree(module)); }
public static void main(String[] args) throws Exception { ANTLRv4Lexer lexer = new ANTLRv4Lexer(new ANTLRFileStream("grammars/org/antlr/codebuff/ANTLRv4Lexer.g4")); CommonTokenStream tokens = new CodeBuffTokenStream(lexer); ANTLRv4Parser parser = new ANTLRv4Parser(tokens); ANTLRv4Parser.GrammarSpecContext tree = parser.grammarSpec(); System.out.println(tree.toStringTree(parser)); }
public static void main(String[] args) throws Exception { Thread.sleep(10000); ANTLRFileStream input = new ANTLRFileStream(args[0]); JavaLexer lexer = new JavaLexer(input); CommonTokenStream tokens = new CommonTokenStream(lexer); JavaParser parser = new JavaParser(tokens); JavaParser.CompilationUnitContext tree = parser.compilationUnit(); // System.out.println(tree.toStringTree(parser)); Thread.sleep(10000); }
/** * Parses a source file using ANTLR4, and walks over the parse tree to * interpret this source file as a Java object. * * @param path * location of the file * @return an interpreted source file, or null in case of an error. */ @Nullable private ReoFile<T> parse(String path) { try { return parse(new ANTLRFileStream(path), path); } catch (IOException e) { m.add("Cannot open " + path + "."); } return null; }
public Init getCompilationUnit(File preprocessedTempFile, String className) throws IOException{ String fileAbsPath = preprocessedTempFile.getAbsolutePath(); CharStream charStream = new ANTLRFileStream(fileAbsPath); SaralLexer saralLexer = new SaralLexer(charStream); CommonTokenStream commonTokenStream = new CommonTokenStream(saralLexer); SaralParser saralParser = new SaralParser(commonTokenStream); BaseErrorListener errorListener = new SaralTreeWalkErrorListener(); saralParser.addErrorListener(errorListener); InitVisitor initVisitor = new InitVisitor(className); return saralParser.init().accept(initVisitor); }
public static ArrobaParser.CompilationUnitContext makeAst(String fileName) { try { ANTLRFileStream fileStream = new ANTLRFileStream(fileName); ArrobaLexer lexer = new ArrobaLexer(fileStream); CommonTokenStream tokenStream = new CommonTokenStream(lexer); ArrobaParser parser = new ArrobaParser(tokenStream); parser.setBuildParseTree(true); return parser.compilationUnit(); } catch (IOException e) { System.err.println("fatal error: could not open file: " + fileName); //e.printStackTrace(); return null; } }
public static void convert(String fn) throws Exception { ItnmDumpLexer lexer = new ItnmDumpLexer(new ANTLRFileStream(fn)); CommonTokenStream tokens = new CommonTokenStream(lexer); ItnmDumpParser parser = new ItnmDumpParser(tokens); parser.setBuildParseTree(true); ParseContext parseContext = parser.parse(); ParseTreeWalker walker = new ParseTreeWalker(); ItnmDumpListener listener = new ItnmDumpJSONWriter(); walker.walk(listener, parseContext); }
public static void main(String[] args) throws IOException { ANTLRInputStream input = new ANTLRFileStream(args[0]); SimpleLexer l = new SimpleLexer(input); TokenStream tokens = new CommonTokenStream(l); SimpleParser parser = new SimpleParser(tokens); ParserRuleContext tree = parser.file(); System.out.println(tree.toStringTree(parser)); // tree.inspect(parser); DefSymbolsAndScopes def = new DefSymbolsAndScopes(); ParseTreeWalker walker = new ParseTreeWalker(); walker.walk(def, tree); }
public static void main(String[] args) throws Exception { ANTLRInputStream input = new ANTLRFileStream("test.falala"); FaLaLaLexer lexer = new FaLaLaLexer(input); CommonTokenStream tokens = new CommonTokenStream(lexer); FaLaLaParser parser = new FaLaLaParser(tokens); ParseTree tree = parser.prog(); System.out.println(tree.toStringTree(parser)); ParseTreeWalker walker = new ParseTreeWalker(); DefSymbols def = new DefSymbols(); walker.walk(def, tree); }
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); }
public static Map<String, List<List<CompileToken>>> tokenizeFile(String file){ try { ANTLRFileStream stream = new ANTLRFileStream(file); return visitor(stream).getTokens(); } catch (IOException e) { e.printStackTrace(); } return null; }
public static void main(String[] args) { if (args.length != 1) { printUsage(); System.exit(1); } File input = new File(args[0]); if (!input.exists()) { System.err.println("File " + args[0] + " does not exits!"); System.exit(2); } try { ANTLRFileStream fileStream = new ANTLRFileStream(args[0]); SkinnyLexer lexer = new SkinnyLexer(fileStream); CommonTokenStream tokens = new CommonTokenStream(lexer); SkinnyParser parser = new SkinnyParser(tokens); ParseTree tree = parser.source(); Assembler asm = new Assembler(); ParseTreeWalker.DEFAULT.walk(asm, tree); ParseTreeWalker.DEFAULT.walk(asm, tree); if (!asm.hasErrors) System.out.println(asm.getText()); } catch (IOException e) { System.err.println("Could not open file: " + args[0]); System.exit(2); } }
private static void prettyPrint(Path filename, int indentation) throws Exception { ANTLRInputStream input = new ANTLRFileStream(filename.toString()); ParseTree tree = parse(input).a; PrettyPrintVisitor prettyPrinter = new PrettyPrintVisitor(indentation); prettyPrinter.visit(tree); System.out.print(prettyPrinter.getOutput()); }
@Test public void testTypeFailingExamples() throws IOException { FileVisitor<Path> typeFailingFilesVisitior = new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { System.out.println("Testing type for file \""+file.toString()+"\""); ANTLRFileStream antlrStream = new ANTLRFileStream(file.toString()); MiniJLexer lexer = new MiniJLexer(antlrStream); TokenStream tokens = new CommonTokenStream(lexer); MiniJParser parser = new MiniJParser(tokens); ParseTree tree = parser.prog(); ASTVisitor astVisitor = new ASTVisitor(); Program program = (Program) astVisitor.visit(tree); TypeInferenceVisitor typeInferenceVisitor = new TypeInferenceVisitor(); program.accept(typeInferenceVisitor); TypeCheckVisitor typeCheckVisitor = new TypeCheckVisitor(); if (program.accept(typeCheckVisitor)) { fail("The example "+file.toString()+" should have failed, but was accepted by the type checker."); } return super.visitFile(file, attrs); } }; Files.walkFileTree(EXAMPLE_PROGRAM_PATH_TYPE_FAILING, typeFailingFilesVisitior); }
@Test public void testVisitWorkingExamples() throws Exception { FileVisitor<Path> workingFilesVisitior = new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { System.out.println("Testing type checker with file \""+file.toString()+"\""); ANTLRFileStream reader = new ANTLRFileStream(file.toString()); MiniJLexer lexer = new MiniJLexer((CharStream) reader); TokenStream tokens = new CommonTokenStream(lexer); MiniJParser parser = new MiniJParser(tokens); ParseTree parseTree = parser.prog(); ASTVisitor astVisitor = new ASTVisitor(); Program ast = (Program) astVisitor.visit(parseTree); TypeInferenceVisitor typeInferenceVisitor = new TypeInferenceVisitor(); ast.accept(typeInferenceVisitor); TypeCheckVisitor visitor = new TypeCheckVisitor(); boolean typesCorrect = ast.accept(visitor); if (!typesCorrect) { for (String error : visitor.getErrors()) { System.err.println(error); } } assertTrue(typesCorrect); return super.visitFile(file, attrs); } }; Files.walkFileTree(EXAMPLE_PROGRAM_PATH_WORKING, workingFilesVisitior); }
public static void main(String[] args) throws IOException { if (args.length != 1) { throw new RuntimeException("Please supply file as argument"); } ANTLRInputStream ais = new ANTLRFileStream(args[0]); QPLLexer lex = new QPLLexer(ais); TokenStream toks = new CommonTokenStream(lex); QPLParser parse = new QPLParser(toks); ParseTree tree = parse.prog(); System.out.println(new ImpVisitor(Paths.get(args[0]).getParent().normalize().toString()).visit(tree)); }
public static void main(String[] args) throws Exception { if (args.length == 0) args = new String[]{"arithmeticsTest.art"}; System.out.println("parsing: " + args[0]); arithmeticsLexer lexer = new arithmeticsLexer(new ANTLRFileStream(args[0])); arithmeticsParser parser = new arithmeticsParser(new CommonTokenStream(lexer)); ParseTree tree = parser.line(); ArithWalk visitor = new ArithWalk(); System.out.println(visitor.visit(tree)); }
public static void main(String[] args) throws Exception { if (args.length == 0) { args = new String[]{"rppTest.rpp"}; } System.out.println("parsing: " + args[0]); rppLexer lexer = new rppLexer(new ANTLRFileStream(args[0])); rppParser parser = new rppParser(new CommonTokenStream(lexer)); ParseTree tree = parser.compilationUnit(); rppWalk visitor = new rppWalk(); System.out.println(visitor.visit(tree)); }