org.antlr.runtime.tree.BufferedTreeNodeStream Java Examples

The following examples show how to use org.antlr.runtime.tree.BufferedTreeNodeStream. 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: SleighEchoRun.java    From ghidra with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
		try {
			LineArrayListWriter writer = new LineArrayListWriter();
			ParsingEnvironment env = new ParsingEnvironment(writer);
			SleighPreprocessor sp = new SleighPreprocessor(
				new HashMapPreprocessorDefinitionsAdapter(), new File(args[0]));
			sp.process(writer);
			CharStream input = new ANTLRStringStream(writer.toString());
			SleighLexer lex = new SleighLexer(input);
			lex.setEnv(env);
			UnbufferedTokenStream tokens = new UnbufferedTokenStream(lex);
			SleighParser parser = new SleighParser(tokens);
			parser.setEnv(env);
			parser.setLexer(lex);
			SleighParser.spec_return root = parser.spec();
			BufferedTreeNodeStream nodes = new BufferedTreeNodeStream(root.tree);
			nodes.setTokenStream(tokens);
//			ANTLRUtil.debugNodeStream(nodes, System.out);
			SleighEcho walker = new SleighEcho(nodes);
			walker.root();
		}
		catch (Throwable t) {
			t.printStackTrace();
		}
	}
 
Example #2
Source File: Test.java    From quetzal with Eclipse Public License 2.0 6 votes vote down vote up
public static void runParser(String sparqlFile) throws IOException {
   	//System.out.println("Parsing: "+sparqlFile+"\n");
   	IbmSparqlLexer lex = new IbmSparqlLexer(new ANTLRFileStream(sparqlFile,	"UTF8"));
	CommonTokenStream tokens = new CommonTokenStream(lex);
	IbmSparqlParser parser = new IbmSparqlParser(tokens);
	try {
		IbmSparqlParser.queryUnit_return ret = parser.queryUnit();
		CommonTree ast = (CommonTree) ret.getTree();
		//System.out.println(ast.toStringTree());
		//SparqlParserUtilities.dump_tree(ast, tokens, 0);
		BufferedTreeNodeStream nodes = new BufferedTreeNodeStream(ast);
		nodes.setTokenStream(tokens);
		IbmSparqlAstWalker walker = new IbmSparqlAstWalker(nodes);
		Query query = walker.queryUnit();
		//System.out.println(query.toString());
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
Example #3
Source File: SqlParser.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
static Statement createStatement(CommonTree tree) {
    TreeNodeStream stream = new BufferedTreeNodeStream(tree);
    StatementBuilder builder = new StatementBuilder(stream);
    try {
        return builder.statement().value;
    } catch (RecognitionException e) {
        throw new AssertionError(e); // RecognitionException is not thrown
    }
}
 
Example #4
Source File: SqlParser.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private static Expression createExpression(CommonTree tree) {
    TreeNodeStream stream = new BufferedTreeNodeStream(tree);
    StatementBuilder builder = new StatementBuilder(stream);
    try {
        return builder.singleExpression().value;
    } catch (RecognitionException e) {
        throw new AssertionError(e); // RecognitionException is not thrown
    }
}
 
Example #5
Source File: SqlParser.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private static String createIdentifier(CommonTree identTree) {
    TreeNodeStream stream = new BufferedTreeNodeStream(identTree);
    StatementBuilder builder = new StatementBuilder(stream);
    try {
        return builder.ident().value;
    } catch (RecognitionException e) {
        throw new AssertionError(e); // RecognitionException is not thrown
    }
}
 
Example #6
Source File: SparqlParserUtilities.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
private static Query getQueryExt(CharStream sparqlFile)
		throws RecognitionException {
	// System.out.println("Parsing: "+sparqlFile+"\n");
	IbmSparqlExtLexer lex = new IbmSparqlExtLexer(sparqlFile);
	CommonTokenStream tokens = new CommonTokenStream(lex);
	IbmSparqlExtParser parser = new IbmSparqlExtParser(tokens);

	parser.setTreeAdaptor(new CommonTreeAdaptor() {
		@Override
		public Object create(Token t) {
			return new XTree(t);
		}
	});

	IbmSparqlExtParser.queryUnit_return ret = parser.queryUnit();
	CommonTree ast = (CommonTree) ret.getTree();

	//
	System.out.println(ast.toStringTree());
	// SparqlParserUtilities.dump_tree(ast, tokens, 0);

	BufferedTreeNodeStream nodes = new BufferedTreeNodeStream(ast);
	nodes.setTokenStream(tokens);
	IbmSparqlExtAstWalker walker = new IbmSparqlExtAstWalker(nodes);
	QueryExt query = walker.queryUnit();
	return query;
}
 
Example #7
Source File: SparqlParserUtilities.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
static Query getQuery(CharStream sparqlFile)
		throws RecognitionException {
	CommonTree ast = getParseTree(sparqlFile);
	// System.out.println(ast.toStringTree());
	BufferedTreeNodeStream nodes = new BufferedTreeNodeStream(ast);
	// nodes.setTokenStream(tokens);
	// IbmSparqlAstRewriter astRewriter = new IbmSparqlAstRewriter(nodes);
	// CommonTree ast2 = (CommonTree)astRewriter.downup(ast, false);
	// System.out.println(ast2.toStringTree());
	// BufferedTreeNodeStream nodes2 = new BufferedTreeNodeStream(ast2);
	// nodes2.setTokenStream(tokens);
	IbmSparqlAstWalker walker = new IbmSparqlAstWalker(nodes);
	Query q = walker.queryUnit();
	return q;
}
 
Example #8
Source File: TestExt.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
public static void runParser(String sparqlFile) throws IOException {
   	//System.out.println("Parsing: "+sparqlFile+"\n");
   	IbmSparqlLexer lex = new IbmSparqlLexer(new ANTLRFileStream(sparqlFile,	"UTF8"));
	CommonTokenStream tokens = new CommonTokenStream(lex);
	IbmSparqlParser parser = new IbmSparqlParser(tokens);
	
	parser.setTreeAdaptor(new CommonTreeAdaptor(){
		@Override
		public Object create(Token t) {
			return new XTree(t);
		}
	}); 
	
	try {
		IbmSparqlParser.queryUnit_return ret = parser.queryUnit();
		XTree ast = (XTree) ret.getTree();
		
		//
		System.out.println(ast.toStringTree());
		//SparqlParserUtilities.dump_tree(ast, tokens, 0);
		
		BufferedTreeNodeStream nodes = new BufferedTreeNodeStream(ast);
		nodes.setTokenStream(tokens);
		IbmSparqlAstWalker walker = new IbmSparqlAstWalker(nodes);
		Query query = walker.queryUnit();
		
		//
		System.out.println(query.toString());
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
Example #9
Source File: TestExt.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
public static void runParserExt(String sparqlFile) throws IOException {
   	//System.out.println("Parsing: "+sparqlFile+"\n");
   	IbmSparqlExtLexer lex = new IbmSparqlExtLexer(new ANTLRFileStream(sparqlFile,	"UTF8"));
	CommonTokenStream tokens = new CommonTokenStream(lex);
	IbmSparqlExtParser parser = new IbmSparqlExtParser(tokens);
	
	parser.setTreeAdaptor(new CommonTreeAdaptor(){
		@Override
		public Object create(Token t) {
			return new XTree(t);
		}
	}); 
	
	try {
		IbmSparqlExtParser.queryUnit_return ret = parser.queryUnit();
		CommonTree ast = (CommonTree) ret.getTree();
		
		//
		System.out.println(ast.toStringTree());
		//SparqlParserUtilities.dump_tree(ast, tokens, 0);
		
		BufferedTreeNodeStream nodes = new BufferedTreeNodeStream(ast);
		nodes.setTokenStream(tokens);
		IbmSparqlExtAstWalker walker = new IbmSparqlExtAstWalker(nodes);
		QueryExt query = walker.queryUnit();
		
		//query.
		//
		System.out.println(query.toString());
	} catch (Exception e) {
		e.printStackTrace();
	}
}