Java 类org.antlr.v4.runtime.ANTLRFileStream 实例源码

项目:athena    文件:ParseTreeErrorListenerTest.java   
/**
 * 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();
}
项目:athena    文件:ParseTreeErrorListenerTest.java   
/**
 * 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();
}
项目:exterminator    文件:Test.java   
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);
        }
    }
}
项目:TPKB    文件:TPKB.java   
/**
 * @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);     
}
项目:cs652    文件:Load.java   
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);
    }
}
项目:MiniJCompiler    文件:MiniJParserTest.java   
@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);
}
项目:MiniJCompiler    文件:MiniJParserTest.java   
@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);
}
项目:MiniJCompiler    文件:TypeCheckVisitorTest.java   
@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);
}
项目:Q-Programming-Language    文件:Main.java   
@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 "";
    }
}
项目:vzome-core    文件:ZomicASTCompiler.java   
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;
  }
项目:poly-ql    文件:Calculator.java   
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();
 }
项目:poly-ql    文件:Form.java   
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();
    }
项目:poly-ql    文件:WordSeparation.java   
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();
    }
项目:poly-ql    文件:Form2.java   
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);
    }
项目:cs652    文件:Load.java   
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);
    }
}
项目:protobuf-el    文件:ProtoFiles.java   
@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;
}
项目:queryj    文件:AbstractTemplatesTest.java   
/**
 * 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;
}
项目:queryj    文件:AbstractTemplatesTest.java   
/**
 * 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;
}
项目:antlr4-method-extractor    文件:MethodFinder.java   
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();
    }   
}
项目:io.alef.llvm    文件:LLVMParserTest.java   
@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));
}
项目:codebuff    文件:Dbg.java   
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));
}
项目:codebuff    文件:ProfileJava.java   
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);
    }
项目:Reo    文件:Interpreter.java   
/**
 * 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;
}
项目:saral    文件:SaralCompilationUnitParser.java   
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);
}
项目:arroba-lang    文件:AstGen.java   
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;
    }
}
项目:oql2json    文件:Main.java   
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);
    }
项目:cs652    文件:Test.java   
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);
    }
项目:cs652    文件:TestFaLaLa.java   
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);
}
项目:cs652    文件:TestLaLa.java   
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);
}
项目:WAM    文件:Compiler.java   
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;
}
项目:SkinnyAssembler    文件:Assembler.java   
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);
    }

}
项目:Trinity    文件:Trinity.java   
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());
}
项目:MiniJCompiler    文件:MiniJParserTest.java   
@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);
}
项目:MiniJCompiler    文件:TypeCheckVisitorTest.java   
@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);
}
项目:Q-Programming-Language    文件:Main.java   
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));
}
项目:cs652    文件:Test.java   
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);
    }
项目:cs652    文件:TestFaLaLa.java   
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);
}
项目:cs652    文件:TestLaLa.java   
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);
}
项目:ReadablePlusPlus    文件:ArithTranslate.java   
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));
    }
项目:ReadablePlusPlus    文件:rppTranslate.java   
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));
    }