use of org.antlr.v4.runtime.CommonTokenFactory in project pinot by linkedin.
the class Pql2Compiler method compileToBrokerRequest.
@Override
public BrokerRequest compileToBrokerRequest(String expression) throws Pql2CompilationException {
try {
//
CharStream charStream = new ANTLRInputStream(expression);
PQL2Lexer lexer = new PQL2Lexer(charStream);
lexer.setTokenFactory(new CommonTokenFactory(true));
TokenStream tokenStream = new UnbufferedTokenStream<CommonToken>(lexer);
PQL2Parser parser = new PQL2Parser(tokenStream);
parser.setErrorHandler(new BailErrorStrategy());
// Parse
ParseTree parseTree = parser.root();
ParseTreeWalker walker = new ParseTreeWalker();
Pql2AstListener listener = new Pql2AstListener(expression);
walker.walk(listener, parseTree);
AstNode rootNode = listener.getRootNode();
BrokerRequest brokerRequest = new BrokerRequest();
rootNode.updateBrokerRequest(brokerRequest);
return brokerRequest;
} catch (Pql2CompilationException e) {
throw e;
} catch (Exception e) {
throw new Pql2CompilationException(e.getMessage());
}
}
use of org.antlr.v4.runtime.CommonTokenFactory in project antlr4 by antlr.
the class TestUnbufferedCharStream method testAFewTokens.
@Test
public void testAFewTokens() throws Exception {
LexerGrammar g = new LexerGrammar("lexer grammar t;\n" + "ID : 'a'..'z'+;\n" + "INT : '0'..'9'+;\n" + "SEMI : ';';\n" + "ASSIGN : '=';\n" + "PLUS : '+';\n" + "MULT : '*';\n" + "WS : ' '+;\n");
// Tokens: 012345678901234567
// Input: x = 3 * 0 + 2 * 0;
TestingUnbufferedCharStream input = createStream("x = 302 * 91 + 20234234 * 0;");
LexerInterpreter lexEngine = g.createLexerInterpreter(input);
// copy text into tokens from char stream
lexEngine.setTokenFactory(new CommonTokenFactory(true));
CommonTokenStream tokens = new CommonTokenStream(lexEngine);
String result = tokens.LT(1).getText();
String expecting = "x";
assertEquals(expecting, result);
tokens.fill();
expecting = "[[@0,0:0='x',<1>,1:0], [@1,1:1=' ',<7>,1:1], [@2,2:2='=',<4>,1:2]," + " [@3,3:3=' ',<7>,1:3], [@4,4:6='302',<2>,1:4], [@5,7:7=' ',<7>,1:7]," + " [@6,8:8='*',<6>,1:8], [@7,9:9=' ',<7>,1:9], [@8,10:11='91',<2>,1:10]," + " [@9,12:12=' ',<7>,1:12], [@10,13:13='+',<5>,1:13], [@11,14:14=' ',<7>,1:14]," + " [@12,15:22='20234234',<2>,1:15], [@13,23:23=' ',<7>,1:23]," + " [@14,24:24='*',<6>,1:24], [@15,25:25=' ',<7>,1:25], [@16,26:26='0',<2>,1:26]," + " [@17,27:27=';',<3>,1:27], [@18,28:27='',<-1>,1:28]]";
assertEquals(expecting, tokens.getTokens().toString());
}
use of org.antlr.v4.runtime.CommonTokenFactory in project pinot by linkedin.
the class Pql2Compiler method compileToExpressionTree.
@Override
public TransformExpressionTree compileToExpressionTree(String expression) {
CharStream charStream = new ANTLRInputStream(expression);
PQL2Lexer lexer = new PQL2Lexer(charStream);
lexer.setTokenFactory(new CommonTokenFactory(true));
TokenStream tokenStream = new UnbufferedTokenStream<CommonToken>(lexer);
PQL2Parser parser = new PQL2Parser(tokenStream);
parser.setErrorHandler(new BailErrorStrategy());
// Parse
ParseTree parseTree = parser.expression();
ParseTreeWalker walker = new ParseTreeWalker();
Pql2AstListener listener = new Pql2AstListener(expression);
walker.walk(listener, parseTree);
final AstNode rootNode = listener.getRootNode();
return TransformExpressionTree.buildTree(rootNode);
}
use of org.antlr.v4.runtime.CommonTokenFactory in project Alpha by alpha-asp.
the class Main method parseVisit.
public static ParsedProgram parseVisit(ANTLRInputStream is) throws IOException {
/*
// In order to require less memory: use unbuffered streams and avoid constructing a full parse tree.
ASPCore2Lexer lexer = new ASPCore2Lexer(new UnbufferedCharStream(is));
lexer.setTokenFactory(new CommonTokenFactory(true));
final ASPCore2Parser parser = new ASPCore2Parser(new UnbufferedTokenStream<>(lexer));
parser.setBuildParseTree(false);
*/
CommonTokenStream tokens = new CommonTokenStream(new ASPCore2Lexer(is));
final ASPCore2Parser parser = new ASPCore2Parser(tokens);
// Try SLL parsing mode (faster but may terminate incorrectly).
parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
parser.removeErrorListeners();
parser.setErrorHandler(new BailErrorStrategy());
final CustomErrorListener errorListener = new CustomErrorListener(is.getSourceName());
ASPCore2Parser.ProgramContext programContext;
try {
// Parse program
programContext = parser.program();
} catch (ParseCancellationException e) {
// retry with LL parser and DefaultErrorStrategy printing errors to console.
if (e.getCause() instanceof RecognitionException) {
tokens.reset();
parser.addErrorListener(errorListener);
parser.setErrorHandler(new DefaultErrorStrategy());
parser.getInterpreter().setPredictionMode(PredictionMode.LL);
// Re-run parse.
programContext = parser.program();
} else {
throw e;
}
}
// is attempted) and the user will only see the first error encountered.
if (errorListener.getRecognitionException() != null) {
throw errorListener.getRecognitionException();
}
// Construct internal program representation.
ParsedTreeVisitor visitor = new ParsedTreeVisitor();
return (ParsedProgram) visitor.visitProgram(programContext);
}
Aggregations