本文整理汇总了Java中org.antlr.v4.runtime.tree.ParseTreeWalker类的典型用法代码示例。如果您正苦于以下问题:Java ParseTreeWalker类的具体用法?Java ParseTreeWalker怎么用?Java ParseTreeWalker使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ParseTreeWalker类属于org.antlr.v4.runtime.tree包,在下文中一共展示了ParseTreeWalker类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: parse
import org.antlr.v4.runtime.tree.ParseTreeWalker; //导入依赖的package包/类
private void parse(
SQLParserListener sqlParserListener,
ErrorListener errorListener,
ParserRuleContext entryPoint) {
ParseTreeWalker walker = new ParseTreeWalker();
walker.walk(sqlParserListener, entryPoint);
// if the parser call raised any exceptions then lets throw
errorListener
.getException()
.ifPresent(
ex -> {
throw ex;
});
}
示例2: callSql
import org.antlr.v4.runtime.tree.ParseTreeWalker; //导入依赖的package包/类
public PlanNode callSql(String sqlstring, ArrayList list) throws FileNotFoundException, IOException {
for (int i=0; i< list.size() ; i++) {
sqlstring = sqlstring.replaceFirst("\\?", (String) list.get(i));
}
ANTLRInputStream input = new ANTLRInputStream( new ByteArrayInputStream(sqlstring.getBytes()));
PLSQLLexer lexer = new PLSQLLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
PLSQLParser parser = new PLSQLParser(tokens);
parser.setBuildParseTree(true);
ParseTree tree = parser.sql_statement();
ParseTreeWalker walker = new ParseTreeWalker();
SQLModel model = new SQLModel();
walker.walk(new MyListener(model), tree);
return model.current;
}
示例3: GraphParser
import org.antlr.v4.runtime.tree.ParseTreeWalker; //导入依赖的package包/类
public GraphParser(InputStream is) throws GraphParserException {
DOTLexer lexer = null;
DOTParser parser = null;
try {
lexer = new DOTLexer(new ANTLRInputStream(is));
lexer.addErrorListener(new ErrorListener());
parser = new DOTParser(new CommonTokenStream(lexer));
parser.setErrorHandler(new ExceptionErrorStrategy());
parser.addErrorListener(new ErrorListener());
ParseTree tree = parser.graph();
ParseTreeWalker.DEFAULT.walk(new NodeListener(), tree);
ParseTreeWalker.DEFAULT.walk(new EdgeListener(), tree);
}
catch (Throwable t) {
if (mErrMsg != null) throw new GraphParserException(mErrMsg, t);
if (lexer != null) {
mErrMsg = "at line " + lexer.getLine() + ":" + lexer.getCharPositionInLine();
throw new GraphParserException(mErrMsg, t);
}
throw new GraphParserException(t);
}
if (mErrMsg != null) throw new GraphParserException(mErrMsg);
}
示例4: parseSqlStream
import org.antlr.v4.runtime.tree.ParseTreeWalker; //导入依赖的package包/类
public static void parseSqlStream(InputStream inputStream, String charsetName,
String parsedObjectName, List<AntlrError> errors,IProgressMonitor mon, int monitoringLevel,
Collection<SQLParserBaseListener> listeners) throws IOException, InterruptedException {
SQLParser parser = makeBasicParser(SQLParser.class, inputStream, charsetName, parsedObjectName, errors);
parser.addParseListener(new CustomParseTreeListener(
monitoringLevel, mon == null ? new NullProgressMonitor() : mon));
try {
SqlContext ctx = parser.sql();
for (SQLParserBaseListener listener : listeners) {
ParseTreeWalker.DEFAULT.walk(listener, ctx);
}
} catch (MonitorCancelledRuntimeException mcre){
throw new InterruptedException();
} catch (UnresolvedReferenceException ex) {
errors.add(CustomSQLParserListener.handleUnresolvedReference(ex));
}
}
示例5: getScript
import org.antlr.v4.runtime.tree.ParseTreeWalker; //导入依赖的package包/类
public Script getScript() {
Script script = new Script(struct.getGMLName() + "_" + name, "");
List<String> args = new ArrayList<>();
if (context.idList() != null) {
for (IdContext id : context.idList().id()) {
args.add(id.getText());
script.addParameter(id.getText(), "");
}
}
ParseTreeWalker walker = new ParseTreeWalker();
walker.walk(new StaticAnalyzer(), context.block());
//walker.walk(new ScriptWriter(script), context.block());
return script;
}
示例6: walkTree
import org.antlr.v4.runtime.tree.ParseTreeWalker; //导入依赖的package包/类
public void walkTree() {
try {
init();
QueryLexer lexer = new QueryLexer(new ANTLRInputStream(new StringReader(queryStr)));
CommonTokenStream token = new CommonTokenStream(lexer);
QueryParser parser = new QueryParser(token);
parser.setBuildParseTree(true);
QueryParser.ScriptContext tree = parser.script(); // parse
ParseTreeWalker parseTreeWalker = new ParseTreeWalker();
parseTreeWalker.walk( this, tree);
//check for key# if size = 1
checkPredicateStack();
} catch (Exception ex) {
logger.error(ex.getMessage());
throw new QueryException( ex.getMessage(), ex );
}
}
示例7: main
import org.antlr.v4.runtime.tree.ParseTreeWalker; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
String[] opts = new String[] {"-query"};
String[] defaults = new String[] {"replace Order set name = \"test\", campaign.id = 10 , booking.no =20.2 where not record# = 1 ; insert Campaign set cid=20, cname=\"go\""};
String[] paras = Utils.getOpts(args, opts, defaults);
//String query = paras[0] ;
String query = "select id, name, bookings from order where id = 10";
//CharStream input = new UnbufferedTokenStream(new ANTLRInputStream( new StringReader(query)));
ObjectQueryLexer lexer = new ObjectQueryLexer( new ANTLRInputStream( new StringReader(query)));
CommonTokenStream token = new CommonTokenStream( lexer);
ObjectQueryParser objectQueryParser = new ObjectQueryParser( token);
objectQueryParser.setBuildParseTree( true);
ParserRuleContext tree = objectQueryParser.script();
ParseTreeWalker walker = new ParseTreeWalker();
System.out.println(walker.getClass().getName()+ " simple "+ walker.getClass().getSimpleName());
System.out.println( tree.toString()) ;
}
示例8: interpret
import org.antlr.v4.runtime.tree.ParseTreeWalker; //导入依赖的package包/类
public void interpret() {
ProgramContext parseTree = matrixParser.program();
ParseTreeWalker.DEFAULT.walk(semanticListener, parseTree);
List<SemanticError> errorList = semanticListener.getSemanticErrors();
if (matrixParser.getNumberOfSyntaxErrors() > 0) {
System.err.println("Aborting...");
} else if (!errorList.isEmpty()) {
System.err.println("------------------- ERRORS ---------------------");
errorList.forEach(System.err::println);
} else {
CodeExecutor codeExecutor = new CodeExecutor(semanticListener.getSymbolTable(), parseTree);
try {
codeExecutor.execute();
} catch (RuntimeError e) {
System.err.println("[" + e.getClass().getSimpleName() + "]: " + e.getMessage());
System.err.println("Aborting...");
}
}
}
示例9: storeOperator
import org.antlr.v4.runtime.tree.ParseTreeWalker; //导入依赖的package包/类
/**
* Special case: STORE operator.
*
* @param queryArray
* @param i
* @param file
* @return
* @throws Exception
*/
public static ResultSet storeOperator(String[] queryArray, int i, boolean file) throws Exception {
ParseTree tree = TriALQLParseQuery.parse(queryArray[i] + ";", file);
TriALQLClassListener extractor = new TriALQLClassListener();
ParseTreeWalker.DEFAULT.walk(extractor, tree);
theLastQuery = true;
if (QueryStruct.oldTableName.get(i).equals(QueryStruct.newTableName.get(i))) {
// results = ImpalaDaemon.main(QueryStruct.baseQuery.get(i - 1));
notToDrop = QueryStruct.newTableName.get(i);
} else {
parseTree = false;
queryExecutor(queryArray[i] + ";", file, theLastQuery, parseTree);
results = ImpalaDaemon.main(QueryStruct.baseQuery.get(i));
}
return results;
}
示例10: checkForProblems
import org.antlr.v4.runtime.tree.ParseTreeWalker; //导入依赖的package包/类
/**
* Performs the first stage of assembling a program. But stops once it
* determines whether the program is valid or not. This is useful when only
* the validity of the program needs to be known.
* @param input the program string to assemble
* @return any problems with the program (empty list if program valid)
*/
public static List<Problem> checkForProblems(String input) {
StoreProblemLogger log = new StoreProblemLogger();
input += '\n'; // to parse correctly, must end with a newline
SimpLexer lexer = new SimpLexer(new ANTLRInputStream(input));
SimpParser parser = new SimpParser(new CommonTokenStream(lexer));
// prevent outputting to the console
lexer.removeErrorListeners();
parser.removeErrorListeners();
// try to parse a program from the input
SimpParser.ProgramContext tree = parser.program();
ProgramExtractor extractor = new ProgramExtractor(log);
ParseTreeWalker.DEFAULT.walk(extractor, tree);
return log.getProblems();
}
示例11: parse
import org.antlr.v4.runtime.tree.ParseTreeWalker; //导入依赖的package包/类
/**
* Parses a source file using ANTLR4, and walks over the parse tree to
* interpret this source file as a Java object.
*
* @param c
* input character stream
* @param path
* location of the file
* @return an interpreted source file, or null in case of an error.
*/
@Nullable
private ReoFile<T> parse(CharStream c, String path) {
ReoLexer lexer = new ReoLexer(c);
CommonTokenStream tokens = new CommonTokenStream(lexer);
ReoParser parser = new ReoParser(tokens);
ErrorListener errListener = new ErrorListener(m);
parser.removeErrorListeners();
parser.addErrorListener(errListener);
ParseTree tree = parser.file();
if (errListener.hasError)
return null;
ParseTreeWalker walker = new ParseTreeWalker();
listener.setFileName(path);
walker.walk(listener, tree);
return listener.getMain();
}
示例12: compileFiles
import org.antlr.v4.runtime.tree.ParseTreeWalker; //导入依赖的package包/类
private void compileFiles(List<RawFile> files, OOPSourceCodeModel srcModel, List<String> projectFileTypes) {
for (RawFile file : files) {
try {
CharStream charStream = new ANTLRInputStream(file.content());
GolangLexer lexer = new GolangLexer(charStream);
TokenStream tokens = new CommonTokenStream(lexer);
GolangParser parser = new GolangParser(tokens);
SourceFileContext sourceFileContext = parser.sourceFile();
parser.setErrorHandler(new BailErrorStrategy());
parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
ParseTreeWalker walker = new ParseTreeWalker();
GolangBaseListener listener = new GoLangTreeListener(srcModel, projectFileTypes, file);
walker.walk(listener, sourceFileContext);
} catch (Exception e) {
e.printStackTrace();
}
}
}
示例13: compileQueryNoOptimize
import org.antlr.v4.runtime.tree.ParseTreeWalker; //导入依赖的package包/类
/**
* <p>Create an Elasticsearch query for the given search string but does
* not apply the {@link ElasticsearchQueryOptimizer} to it.</p>
* @param search the search string
* @return the Elasticsearch query (may be null)
*/
protected JsonObject compileQueryNoOptimize(String search) {
if (search == null || search.isEmpty()) {
// match everything by default
return matchAllQuery();
}
// parse query
QueryLexer lexer = new QueryLexer(new ANTLRInputStream(search.trim()));
CommonTokenStream tokens = new CommonTokenStream(lexer);
QueryParser parser = new QueryParser(tokens);
QueryContext ctx = parser.query();
// compile query to QueryBuilder
QueryCompilerListener listener = new QueryCompilerListener();
ParseTreeWalker.DEFAULT.walk(listener, ctx);
if (listener.result.isEmpty()) {
return matchAllQuery();
}
return listener.result.pop();
}
示例14: parse
import org.antlr.v4.runtime.tree.ParseTreeWalker; //导入依赖的package包/类
/**
* Parse the given Thrift {@code text}, using the given {@code location}
* to anchor parsed elements withing the file.
* @param location the {@link Location} of the data being parsed.
* @param text the text to be parsed.
* @param reporter an {@link ErrorReporter} to collect warnings.
* @return a representation of the parsed Thrift data.
*/
public static ThriftFileElement parse(Location location, String text, ErrorReporter reporter) {
ANTLRInputStream charStream = new ANTLRInputStream(text);
AntlrThriftLexer lexer = new AntlrThriftLexer(charStream);
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
AntlrThriftParser antlrParser = new AntlrThriftParser(tokenStream);
ThriftListener thriftListener = new ThriftListener(tokenStream, reporter, location);
ParseTreeWalker walker = new ParseTreeWalker();
walker.walk(thriftListener, antlrParser.document());
if (reporter.hasError()) {
String errorReports = Joiner.on('\n').join(reporter.formattedReports());
String message = String.format(Locale.US, "Syntax errors in %s:\n%s", location, errorReports);
throw new IllegalStateException(message);
}
return thriftListener.buildFileElement();
}
示例15: processFile
import org.antlr.v4.runtime.tree.ParseTreeWalker; //导入依赖的package包/类
public static Document processFile(String filePath) {
Pair<Parser, Lexer> pl = parsePHP(filePath);
PHPParser parser = (PHPParser) pl.a;
parser.setBuildParseTree(true);
/*
* htmlDocument is the start rule (the top-level rule)
* for the PHP grammar
*/
ParserRuleContext tree = parser.htmlDocument();
List<String> ruleNames = Arrays.asList(parser.getRuleNames());
Map<Integer, String> invTokenMap = getInvTokenMap(parser);
TokenStream tokenStream = parser.getTokenStream();
ParseTreeDOMSerializer ptSerializer = new ParseTreeDOMSerializer(ruleNames, invTokenMap, tokenStream);
ParseTreeWalker.DEFAULT.walk(ptSerializer, tree);
Document result= ptSerializer.getDOMDocument();
return result;
}