Java Code Examples for org.antlr.v4.runtime.CharStreams#fromFileName()

The following examples show how to use org.antlr.v4.runtime.CharStreams#fromFileName() . 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: PomFileParser.java    From depends with MIT License 6 votes vote down vote up
@Override
public void parse() throws IOException {
	/* If file already exist, skip it */
	Entity fileEntity = entityRepo.getEntity(fileFullPath);
	if (fileEntity!=null && fileEntity instanceof FileEntity) {
		return;
	}
	/*parse file*/
       CharStream input = CharStreams.fromFileName(fileFullPath);
       Lexer lexer = new XMLLexer(input);
       CommonTokenStream tokens = new CommonTokenStream(lexer);
       XMLParser parser = new XMLParser(tokens);
       PomListener bridge = new PomListener(fileFullPath, entityRepo, includePaths,parseCreator,inferer);
    ParseTreeWalker walker = new ParseTreeWalker();
    walker.walk(bridge, parser.document());
	fileEntity = entityRepo.getEntity(fileFullPath);
	bridge.done();
	((FileEntity)fileEntity).cacheAllExpressions();
}
 
Example 2
Source File: JavaFileParser.java    From depends with MIT License 6 votes vote down vote up
@Override
public void parse() throws IOException {
       CharStream input = CharStreams.fromFileName(fileFullPath);
       Lexer lexer = new JavaLexer(input);
       lexer.setInterpreter(new LexerATNSimulator(lexer, lexer.getATN(), lexer.getInterpreter().decisionToDFA, new PredictionContextCache()));
       CommonTokenStream tokens = new CommonTokenStream(lexer);
       JavaParser parser = new JavaParser(tokens);
       ParserATNSimulator interpreter = new ParserATNSimulator(parser, parser.getATN(), parser.getInterpreter().decisionToDFA, new PredictionContextCache());
       parser.setInterpreter(interpreter);
       JavaListener bridge = new JavaListener(fileFullPath, entityRepo,inferer);
    ParseTreeWalker walker = new ParseTreeWalker();
    try {
    	walker.walk(bridge, parser.compilationUnit());
		Entity fileEntity = entityRepo.getEntity(fileFullPath);
		((FileEntity)fileEntity).cacheAllExpressions();
		interpreter.clearDFA();
		bridge.done();
    	
    }catch (Exception e) {
    	System.err.println("error encountered during parse..." );
    	e.printStackTrace();
    }
    
   }
 
Example 3
Source File: JRubyFileParser.java    From depends with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void parse() throws IOException {
	/** If file already exist, skip it */
	Entity fileEntity = entityRepo.getEntity(fileFullPath);
	if (fileEntity!=null && fileEntity instanceof FileEntity) {
		return;
	}
	
       CharStream input = CharStreams.fromFileName(fileFullPath);
	Parser rubyParser = new Parser();
	StringReader in = new StringReader(input.toString());
	CompatVersion version = CompatVersion.RUBY2_3;
	ParserConfiguration config = new ParserConfiguration(0, version);
	try {
		Node node = rubyParser.parse("<code>", in, config);
		JRubyVisitor parser = new JRubyVisitor(fileFullPath, entityRepo, includesFileLocator,executor,inferer,parserCreator);
		node.accept(parser);
		fileEntity = entityRepo.getEntity(fileFullPath);
		((FileEntity)fileEntity).cacheAllExpressions();
		parser.done();
	}catch(Exception e) {
		System.err.println("parsing error in "+fileFullPath);
	}
}
 
Example 4
Source File: PythonFileParser.java    From depends with MIT License 6 votes vote down vote up
@Override
public void parse() throws IOException {
	/** If file already exist, skip it */
	Entity fileEntity = entityRepo.getEntity(fileFullPath);
	if (fileEntity!=null && fileEntity instanceof FileEntity) {
		return;
	}
       CharStream input = CharStreams.fromFileName(fileFullPath);
       Lexer lexer = new PythonLexer(input);
       CommonTokenStream tokens = new CommonTokenStream(lexer);
       
       
       PythonParser parser = new PythonParser(tokens);
       PythonCodeListener bridge = new PythonCodeListener(fileFullPath, entityRepo,inferer, includeFileLocator, processor);
    ParseTreeWalker walker = new ParseTreeWalker();
    walker.walk(bridge, parser.file_input());
	fileEntity = entityRepo.getEntity(fileFullPath);
	((FileEntity)fileEntity).cacheAllExpressions();
	bridge.done();
}
 
Example 5
Source File: Tool.java    From bookish with MIT License 6 votes vote down vote up
/** Parse a file and also traverse parse tree to set XML attributes Map
	 *  for any attrs rule refs.
	 */
	public Object parse(Artifact artifact, String fileName, boolean isRoot) throws IOException {
		CharStream input = CharStreams.fromFileName(fileName);
		BookishLexer lexer = new BookishLexer(input);
		CommonTokenStream tokens = new CommonTokenStream(lexer);
		BookishParser parser = new BookishParser(tokens);

		lexer.removeErrorListeners();
		lexer.addErrorListener(getErrorListener());
		parser.removeErrorListeners();
		parser.addErrorListener(getErrorListener());

		ParserRuleContext doctree;
		if ( isRoot ) doctree = parser.rootdocument();
		else doctree = parser.chapter();

		verifyAndGatherXMLAttributes(parser, doctree);

//		System.out.println();
//		System.out.println(doctree.toStringTree(Arrays.asList(BookishParser.ruleNames)));

		if ( isRoot ) {
			return new RootDocInfo(artifact, parser, doctree);
		}
		return new ChapDocInfo(artifact, parser, (BookishParser.ChapterContext)doctree);
	}
 
Example 6
Source File: ZserioTool.java    From zserio with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private Package parsePackage(ZserioAstBuilder astBuilder,
        String inputFileFullName) throws Exception
{
    ZserioToolPrinter.printMessage("Parsing " + inputFileFullName);

    inputFileManager.registerFile(inputFileFullName);

    final CharStream inputStream = CharStreams.fromFileName(inputFileFullName, Charset.forName("UTF-8"));
    final ParseErrorListener parseErrorListener = new ParseErrorListener();
    final ZserioLexer lexer = new ZserioLexer(inputStream);
    lexer.removeErrorListeners();
    lexer.addErrorListener(parseErrorListener);
    final CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    final ZserioParser parser = new ZserioParser(tokenStream);
    parser.removeErrorListeners();
    parser.addErrorListener(parseErrorListener);

    final ParseTree tree = parser.packageDeclaration();

    final ZserioParseTreeChecker parseTreeChecker = new ZserioParseTreeChecker(inputFileManager);
    parseTreeChecker.visit(tree);

    final Package parsedPackage = (Package)astBuilder.visit(tree, tokenStream);

    return parsedPackage;
}
 
Example 7
Source File: BdsCompiler.java    From BigDataScript with Apache License 2.0 6 votes vote down vote up
/**
 * Create an AST from a program (using ANTLR lexer & parser)
 * Returns null if error
 * Use 'alreadyIncluded' to keep track of from 'include' statements
 */
ParseTree createAst(File file, boolean debug, Set<String> alreadyIncluded) {
	alreadyIncluded.add(Gpr.getCanonicalFileName(file));
	String fileName = file.toString();
	String filePath = fileName;

	try {
		filePath = file.getCanonicalPath();

		// Input stream
		if (!Gpr.canRead(filePath)) {
			CompilerMessages.get().addError("Can't read file '" + filePath + "'");
			return null;
		}

		// Create a CharStream that reads from standard input
		CharStream input = CharStreams.fromFileName(fileName);
		return createAst(input, debug, alreadyIncluded);
	} catch (Exception e) {
		String msg = e.getMessage();
		CompilerMessages.get().addError("Could not compile " + filePath //
				+ (msg != null ? " :" + e.getMessage() : "") //
		);
		return null;
	}
}
 
Example 8
Source File: KotlinFileParser.java    From depends with MIT License 5 votes vote down vote up
@Override
public void parse() throws IOException {
       CharStream input = CharStreams.fromFileName(fileFullPath);
        Lexer lexer = new KotlinLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        KotlinParser parser = new KotlinParser(tokens);
        KotlinListener bridge = new KotlinListener(fileFullPath, entityRepo,inferer);
	    ParseTreeWalker walker = new ParseTreeWalker();
	    walker.walk(bridge, parser.kotlinFile());
}
 
Example 9
Source File: Tool.java    From bookish with MIT License 5 votes vote down vote up
public Pair<BookishParser.DocumentContext,BookishParser> parseChapter(String inputDir,
                                                                      String inputFilename,
                                                                      int chapNumber)
	throws IOException
{
	CharStream input = CharStreams.fromFileName(inputDir+"/"+inputFilename);
	BookishLexer lexer = new BookishLexer(input);
	CommonTokenStream tokens = new CommonTokenStream(lexer);
	BookishParser parser = new BookishParser(tokens, inputFilename, chapNumber);
	BookishParser.DocumentContext doctree = parser.document();
	return new Pair<>(doctree, parser);
}
 
Example 10
Source File: DirectFileLoader.java    From Concurnas with MIT License 4 votes vote down vote up
@Override
public CharStream readFile(String filename) throws IOException {
	return CharStreams.fromFileName(this.getCanonicalPath(filename));
}
 
Example 11
Source File: DSLParserWrapper.java    From SPADE with GNU General Public License v3.0 4 votes vote down vote up
public ParseProgram fromFile(String filename) throws IOException {
  CharStream input = CharStreams.fromFileName(filename);
  return fromCharStream(input);
}