use of org.antlr.v4.parse.v3TreeGrammarException in project antlr4 by antlr.
the class ParserATNFactory method createATN.
@Override
public ATN createATN() {
_createATN(g.rules.values());
assert atn.maxTokenType == g.getMaxTokenType();
addRuleFollowLinks();
addEOFTransitionToStartRules();
ATNOptimizer.optimize(g, atn);
for (Triple<Rule, ATNState, ATNState> pair : preventEpsilonClosureBlocks) {
LL1Analyzer analyzer = new LL1Analyzer(atn);
ATNState blkStart = pair.b;
ATNState blkStop = pair.c;
IntervalSet lookahead = analyzer.LOOK(blkStart, blkStop, null);
if (lookahead.contains(org.antlr.v4.runtime.Token.EPSILON)) {
ErrorType errorType = pair.a instanceof LeftRecursiveRule ? ErrorType.EPSILON_LR_FOLLOW : ErrorType.EPSILON_CLOSURE;
g.tool.errMgr.grammarError(errorType, g.fileName, ((GrammarAST) pair.a.ast.getChild(0)).getToken(), pair.a.name);
}
}
optionalCheck: for (Triple<Rule, ATNState, ATNState> pair : preventEpsilonOptionalBlocks) {
int bypassCount = 0;
for (int i = 0; i < pair.b.getNumberOfTransitions(); i++) {
ATNState startState = pair.b.transition(i).target;
if (startState == pair.c) {
bypassCount++;
continue;
}
LL1Analyzer analyzer = new LL1Analyzer(atn);
if (analyzer.LOOK(startState, pair.c, null).contains(org.antlr.v4.runtime.Token.EPSILON)) {
g.tool.errMgr.grammarError(ErrorType.EPSILON_OPTIONAL, g.fileName, ((GrammarAST) pair.a.ast.getChild(0)).getToken(), pair.a.name);
continue optionalCheck;
}
}
if (bypassCount != 1) {
throw new UnsupportedOperationException("Expected optional block with exactly 1 bypass alternative.");
}
}
return atn;
}
use of org.antlr.v4.parse.v3TreeGrammarException in project incubator-systemml by apache.
the class PyDMLParserWrapper method doParse.
/**
* This function is supposed to be called directly only from PydmlSyntacticValidator when it encounters 'import'
* @param fileName script file name
* @param dmlScript script file contents
* @param sourceNamespace namespace from source statement
* @param argVals script arguments
* @return dml program, or null if at least one error
* @throws ParseException if ParseException occurs
*/
public DMLProgram doParse(String fileName, String dmlScript, String sourceNamespace, Map<String, String> argVals) throws ParseException {
DMLProgram dmlPgm = null;
ANTLRInputStream in;
try {
if (dmlScript == null) {
dmlScript = readDMLScript(fileName, LOG);
}
InputStream stream = new ByteArrayInputStream(dmlScript.getBytes());
in = new org.antlr.v4.runtime.ANTLRInputStream(stream);
} catch (FileNotFoundException e) {
throw new ParseException("Cannot find file/resource: " + fileName, e);
} catch (IOException e) {
throw new ParseException("Cannot open file: " + fileName, e);
} catch (LanguageException e) {
throw new ParseException(e.getMessage(), e);
}
ProgramrootContext ast = null;
CustomErrorListener errorListener = new CustomErrorListener();
try {
PydmlLexer lexer = new PydmlLexer(in);
CommonTokenStream tokens = new CommonTokenStream(lexer);
PydmlParser antlr4Parser = new PydmlParser(tokens);
// For now no optimization, since it is not able to parse integer value.
boolean tryOptimizedParsing = false;
if (tryOptimizedParsing) {
// Try faster and simpler SLL
antlr4Parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
antlr4Parser.removeErrorListeners();
antlr4Parser.setErrorHandler(new BailErrorStrategy());
try {
ast = antlr4Parser.programroot();
// If successful, no need to try out full LL(*) ... SLL was enough
} catch (ParseCancellationException ex) {
// Error occurred, so now try full LL(*) for better error messages
tokens.reset();
antlr4Parser.reset();
if (fileName != null) {
errorListener.setCurrentFileName(fileName);
} else {
errorListener.setCurrentFileName("MAIN_SCRIPT");
}
// Set our custom error listener
antlr4Parser.addErrorListener(errorListener);
antlr4Parser.setErrorHandler(new DefaultErrorStrategy());
antlr4Parser.getInterpreter().setPredictionMode(PredictionMode.LL);
ast = antlr4Parser.programroot();
}
} else {
// Set our custom error listener
antlr4Parser.removeErrorListeners();
antlr4Parser.addErrorListener(errorListener);
errorListener.setCurrentFileName(fileName);
// Now do the parsing
ast = antlr4Parser.programroot();
}
} catch (Exception e) {
throw new ParseException("ERROR: Cannot parse the program:" + fileName, e);
}
// Now convert the parse tree into DMLProgram
// Do syntactic validation while converting
ParseTree tree = ast;
// And also do syntactic validation
ParseTreeWalker walker = new ParseTreeWalker();
// Get list of function definitions which take precedence over built-in functions if same name
PydmlPreprocessor prep = new PydmlPreprocessor(errorListener);
walker.walk(prep, tree);
// Syntactic validation
PydmlSyntacticValidator validator = new PydmlSyntacticValidator(errorListener, argVals, sourceNamespace, prep.getFunctionDefs());
walker.walk(validator, tree);
errorListener.unsetCurrentFileName();
this.parseIssues = errorListener.getParseIssues();
this.atLeastOneWarning = errorListener.isAtLeastOneWarning();
this.atLeastOneError = errorListener.isAtLeastOneError();
if (atLeastOneError) {
throw new ParseException(parseIssues, dmlScript);
}
if (atLeastOneWarning) {
LOG.warn(CustomErrorListener.generateParseIssuesMessage(dmlScript, parseIssues));
}
dmlPgm = createDMLProgram(ast, sourceNamespace);
return dmlPgm;
}
use of org.antlr.v4.parse.v3TreeGrammarException in project oxTrust by GluuFederation.
the class ScimFilterParser method criteria.
public final CriteriaContext criteria() throws RecognitionException {
CriteriaContext _localctx = new CriteriaContext(_ctx, getState());
enterRule(_localctx, 4, RULE_criteria);
try {
int _alt;
enterOuterAlt(_localctx, 1);
{
setState(139);
match(T__0);
setState(141);
_errHandler.sync(this);
_alt = 1 + 1;
do {
switch(_alt) {
case 1 + 1:
{
{
setState(140);
matchWildcard();
}
}
break;
default:
throw new NoViableAltException(this);
}
setState(143);
_errHandler.sync(this);
_alt = getInterpreter().adaptivePredict(_input, 20, _ctx);
} while (_alt != 1 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER);
setState(145);
match(T__0);
}
} catch (RecognitionException re) {
_localctx.exception = re;
_errHandler.reportError(this, re);
_errHandler.recover(this, re);
} finally {
exitRule();
}
return _localctx;
}
use of org.antlr.v4.parse.v3TreeGrammarException in project L42 by ElvisResearchGroup.
the class ReplState method start.
public static ReplState start(String code) {
try {
Expression.ClassReuse code1 = (ClassReuse) Parser.parse("Repl", code);
auxiliaryGrammar.WellFormedness.checkAll(code1);
Expression.ClassReuse code2 = (ClassReuse) Desugar.of(code1);
assert auxiliaryGrammar.WellFormedness.checkAll(code2);
ExpCore.ClassB code3 = (ExpCore.ClassB) code2.accept(new InjectionOnCore());
assert coreVisitors.CheckNoVarDeclaredTwice.of(code3);
// TODO: will die after new reduction Refresh of position identities, it is used to generate correct Java code.
code3 = (ExpCore.ClassB) code3.accept(new CloneVisitor() {
@Override
public ExpCore visit(ExpCore.ClassB cb) {
Position p = cb.getP();
cb = cb.withP(new Position(p.getFile(), p.getLine1(), p.getPos1(), p.getLine2(), p.getPos2(), p.get_next()));
return super.visit(cb);
}
});
ExpCore.ClassB result = ProgramReduction.allSteps(code3);
return new ReplState(code, code2, result);
} catch (org.antlr.v4.runtime.misc.ParseCancellationException parser) {
System.out.println(parser.getMessage());
return null;
} catch (ErrorMessage msg) {
ErrorFormatter.topFormatErrorMessage(msg);
return null;
}
}
use of org.antlr.v4.parse.v3TreeGrammarException in project oxTrust by GluuFederation.
the class ScimFilterParser method expression.
private ExpressionContext expression(int _p) throws RecognitionException {
ParserRuleContext _parentctx = _ctx;
int _parentState = getState();
ExpressionContext _localctx = new ExpressionContext(_ctx, _parentState);
ExpressionContext _prevctx = _localctx;
int _startState = 2;
enterRecursionRule(_localctx, 2, RULE_expression, _p);
int _la;
try {
int _alt;
enterOuterAlt(_localctx, 1);
{
setState(92);
_errHandler.sync(this);
switch(getInterpreter().adaptivePredict(_input, 11, _ctx)) {
case 1:
{
_localctx = new NOT_EXPRContext(_localctx);
_ctx = _localctx;
_prevctx = _localctx;
setState(17);
match(NOT);
setState(19);
_errHandler.sync(this);
_alt = 1 + 1;
do {
switch(_alt) {
case 1 + 1:
{
{
setState(18);
match(WS);
}
}
break;
default:
throw new NoViableAltException(this);
}
setState(21);
_errHandler.sync(this);
_alt = getInterpreter().adaptivePredict(_input, 1, _ctx);
} while (_alt != 1 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER);
setState(23);
expression(9);
}
break;
case 2:
{
_localctx = new ATTR_OPER_EXPRContext(_localctx);
_ctx = _localctx;
_prevctx = _localctx;
setState(24);
match(ATTRNAME);
setState(26);
_errHandler.sync(this);
_alt = 1 + 1;
do {
switch(_alt) {
case 1 + 1:
{
{
setState(25);
match(WS);
}
}
break;
default:
throw new NoViableAltException(this);
}
setState(28);
_errHandler.sync(this);
_alt = getInterpreter().adaptivePredict(_input, 2, _ctx);
} while (_alt != 1 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER);
setState(30);
operator();
setState(32);
_errHandler.sync(this);
_alt = 1 + 1;
do {
switch(_alt) {
case 1 + 1:
{
{
setState(31);
match(WS);
}
}
break;
default:
throw new NoViableAltException(this);
}
setState(34);
_errHandler.sync(this);
_alt = getInterpreter().adaptivePredict(_input, 3, _ctx);
} while (_alt != 1 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER);
setState(36);
expression(4);
}
break;
case 3:
{
_localctx = new ATTR_PRContext(_localctx);
_ctx = _localctx;
_prevctx = _localctx;
setState(38);
match(ATTRNAME);
setState(40);
_errHandler.sync(this);
_alt = 1 + 1;
do {
switch(_alt) {
case 1 + 1:
{
{
setState(39);
match(WS);
}
}
break;
default:
throw new NoViableAltException(this);
}
setState(42);
_errHandler.sync(this);
_alt = getInterpreter().adaptivePredict(_input, 4, _ctx);
} while (_alt != 1 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER);
setState(44);
match(PR);
}
break;
case 4:
{
_localctx = new ATTR_OPER_CRITERIAContext(_localctx);
_ctx = _localctx;
_prevctx = _localctx;
setState(45);
match(ATTRNAME);
setState(47);
_errHandler.sync(this);
_alt = 1 + 1;
do {
switch(_alt) {
case 1 + 1:
{
{
setState(46);
match(WS);
}
}
break;
default:
throw new NoViableAltException(this);
}
setState(49);
_errHandler.sync(this);
_alt = getInterpreter().adaptivePredict(_input, 5, _ctx);
} while (_alt != 1 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER);
setState(51);
operator();
setState(53);
_errHandler.sync(this);
_alt = 1 + 1;
do {
switch(_alt) {
case 1 + 1:
{
{
setState(52);
match(WS);
}
}
break;
default:
throw new NoViableAltException(this);
}
setState(55);
_errHandler.sync(this);
_alt = getInterpreter().adaptivePredict(_input, 6, _ctx);
} while (_alt != 1 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER);
setState(57);
criteria();
}
break;
case 5:
{
_localctx = new LPAREN_EXPR_RPARENContext(_localctx);
_ctx = _localctx;
_prevctx = _localctx;
setState(59);
match(LPAREN);
setState(63);
_errHandler.sync(this);
_alt = getInterpreter().adaptivePredict(_input, 7, _ctx);
while (_alt != 1 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER) {
if (_alt == 1 + 1) {
{
{
setState(60);
match(WS);
}
}
}
setState(65);
_errHandler.sync(this);
_alt = getInterpreter().adaptivePredict(_input, 7, _ctx);
}
setState(66);
expression(0);
setState(70);
_errHandler.sync(this);
_alt = getInterpreter().adaptivePredict(_input, 8, _ctx);
while (_alt != 1 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER) {
if (_alt == 1 + 1) {
{
{
setState(67);
match(WS);
}
}
}
setState(72);
_errHandler.sync(this);
_alt = getInterpreter().adaptivePredict(_input, 8, _ctx);
}
setState(73);
match(RPAREN);
}
break;
case 6:
{
_localctx = new LBRAC_EXPR_RBRACContext(_localctx);
_ctx = _localctx;
_prevctx = _localctx;
setState(75);
match(ATTRNAME);
setState(76);
match(LBRAC);
setState(80);
_errHandler.sync(this);
_alt = getInterpreter().adaptivePredict(_input, 9, _ctx);
while (_alt != 1 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER) {
if (_alt == 1 + 1) {
{
{
setState(77);
match(WS);
}
}
}
setState(82);
_errHandler.sync(this);
_alt = getInterpreter().adaptivePredict(_input, 9, _ctx);
}
setState(83);
expression(0);
setState(87);
_errHandler.sync(this);
_alt = getInterpreter().adaptivePredict(_input, 10, _ctx);
while (_alt != 1 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER) {
if (_alt == 1 + 1) {
{
{
setState(84);
match(WS);
}
}
}
setState(89);
_errHandler.sync(this);
_alt = getInterpreter().adaptivePredict(_input, 10, _ctx);
}
setState(90);
match(RBRAC);
}
break;
}
_ctx.stop = _input.LT(-1);
setState(136);
_errHandler.sync(this);
_alt = getInterpreter().adaptivePredict(_input, 19, _ctx);
while (_alt != 2 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER) {
if (_alt == 1) {
if (_parseListeners != null)
triggerExitRuleEvent();
_prevctx = _localctx;
{
setState(134);
_errHandler.sync(this);
switch(getInterpreter().adaptivePredict(_input, 18, _ctx)) {
case 1:
{
_localctx = new EXPR_AND_EXPRContext(new ExpressionContext(_parentctx, _parentState));
pushNewRecursionContext(_localctx, _startState, RULE_expression);
setState(94);
if (!(precpred(_ctx, 8)))
throw new FailedPredicateException(this, "precpred(_ctx, 8)");
setState(96);
_errHandler.sync(this);
_alt = 1 + 1;
do {
switch(_alt) {
case 1 + 1:
{
{
setState(95);
match(WS);
}
}
break;
default:
throw new NoViableAltException(this);
}
setState(98);
_errHandler.sync(this);
_alt = getInterpreter().adaptivePredict(_input, 12, _ctx);
} while (_alt != 1 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER);
setState(100);
match(AND);
setState(102);
_errHandler.sync(this);
_alt = 1 + 1;
do {
switch(_alt) {
case 1 + 1:
{
{
setState(101);
match(WS);
}
}
break;
default:
throw new NoViableAltException(this);
}
setState(104);
_errHandler.sync(this);
_alt = getInterpreter().adaptivePredict(_input, 13, _ctx);
} while (_alt != 1 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER);
setState(106);
expression(9);
}
break;
case 2:
{
_localctx = new EXPR_OR_EXPRContext(new ExpressionContext(_parentctx, _parentState));
pushNewRecursionContext(_localctx, _startState, RULE_expression);
setState(107);
if (!(precpred(_ctx, 7)))
throw new FailedPredicateException(this, "precpred(_ctx, 7)");
setState(109);
_errHandler.sync(this);
_alt = 1 + 1;
do {
switch(_alt) {
case 1 + 1:
{
{
setState(108);
match(WS);
}
}
break;
default:
throw new NoViableAltException(this);
}
setState(111);
_errHandler.sync(this);
_alt = getInterpreter().adaptivePredict(_input, 14, _ctx);
} while (_alt != 1 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER);
setState(113);
match(OR);
setState(115);
_errHandler.sync(this);
_la = _input.LA(1);
do {
{
{
setState(114);
match(WS);
}
}
setState(117);
_errHandler.sync(this);
_la = _input.LA(1);
} while (_la == WS);
setState(119);
expression(8);
}
break;
case 3:
{
_localctx = new EXPR_OPER_EXPRContext(new ExpressionContext(_parentctx, _parentState));
pushNewRecursionContext(_localctx, _startState, RULE_expression);
setState(120);
if (!(precpred(_ctx, 6)))
throw new FailedPredicateException(this, "precpred(_ctx, 6)");
setState(122);
_errHandler.sync(this);
_alt = 1 + 1;
do {
switch(_alt) {
case 1 + 1:
{
{
setState(121);
match(WS);
}
}
break;
default:
throw new NoViableAltException(this);
}
setState(124);
_errHandler.sync(this);
_alt = getInterpreter().adaptivePredict(_input, 16, _ctx);
} while (_alt != 1 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER);
setState(126);
operator();
setState(128);
_errHandler.sync(this);
_alt = 1 + 1;
do {
switch(_alt) {
case 1 + 1:
{
{
setState(127);
match(WS);
}
}
break;
default:
throw new NoViableAltException(this);
}
setState(130);
_errHandler.sync(this);
_alt = getInterpreter().adaptivePredict(_input, 17, _ctx);
} while (_alt != 1 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER);
setState(132);
expression(7);
}
break;
}
}
}
setState(138);
_errHandler.sync(this);
_alt = getInterpreter().adaptivePredict(_input, 19, _ctx);
}
}
} catch (RecognitionException re) {
_localctx.exception = re;
_errHandler.reportError(this, re);
_errHandler.recover(this, re);
} finally {
unrollRecursionContexts(_parentctx);
}
return _localctx;
}
Aggregations