use of org.antlr.v4.runtime.NoViableAltException in project claw-compiler by C2SM-RCM.
the class ClawLanguage method analyze.
/**
* Analyze a raw string input and match it with the CLAW language definition.
*
* @param rawPragma A raw pragma statement to be analyzed against the CLAW
* language.
* @param lineno Line number of the pragma statement.
* @param generator Accelerator directive generator.
* @param target Target that influences the code transformation.
* @return A ClawLanguage object with the corresponding extracted information.
* @throws IllegalDirectiveException If directive does not follow the CLAW
* language specification.
*/
private static ClawLanguage analyze(String rawPragma, int lineno, AcceleratorGenerator generator, Target target) throws IllegalDirectiveException {
// Remove additional claw keyword
rawPragma = nakenize(rawPragma);
// Discard the ignored code after the claw ignore directive
if (rawPragma.toLowerCase().contains(IGNORE)) {
rawPragma = rawPragma.substring(0, rawPragma.toLowerCase().indexOf(IGNORE) + IGNORE.length());
}
// Instantiate the lexer with the raw string input
ClawLexer lexer = new ClawLexer(CharStreams.fromString(rawPragma));
// Get a list of matched tokens
CommonTokenStream tokens = new CommonTokenStream(lexer);
// Pass the tokens to the parser
ClawParser parser = new ClawParser(tokens);
parser.setErrorHandler(new BailErrorStrategy());
parser.removeErrorListeners();
try {
// Start the parser analysis from the "analyze" entry point
ClawParser.AnalyzeContext ctx = parser.analyze();
// Get the ClawLanguage object return by the parser after analysis.
ctx.l.setAcceleratorGenerator(generator);
ctx.l.setTarget(target);
return ctx.l;
} catch (ParseCancellationException pcex) {
if (pcex.getCause() instanceof InputMismatchException) {
InputMismatchException imex = (InputMismatchException) pcex.getCause();
throw new IllegalDirectiveException(getTokens(imex.getExpectedTokens(), parser), lineno, imex.getOffendingToken().getCharPositionInLine());
} else if (pcex.getCause() instanceof NoViableAltException) {
NoViableAltException nvex = (NoViableAltException) pcex.getCause();
throw new IllegalDirectiveException(nvex.getOffendingToken(), getTokens(nvex.getExpectedTokens(), parser), lineno, nvex.getOffendingToken().getCharPositionInLine());
}
throw new IllegalDirectiveException(rawPragma, "Unsupported construct", lineno, 0);
}
}
use of org.antlr.v4.runtime.NoViableAltException 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.runtime.NoViableAltException in project elasticsearch by elastic.
the class ParserErrorStrategy method recover.
@Override
public void recover(final Parser recognizer, final RecognitionException re) {
final Token token = re.getOffendingToken();
String message;
if (token == null) {
message = "no parse token found.";
} else if (re instanceof InputMismatchException) {
message = "unexpected token [" + getTokenErrorDisplay(token) + "]" + " was expecting one of [" + re.getExpectedTokens().toString(recognizer.getVocabulary()) + "].";
} else if (re instanceof NoViableAltException) {
if (token.getType() == PainlessParser.EOF) {
message = "unexpected end of script.";
} else {
message = "invalid sequence of tokens near [" + getTokenErrorDisplay(token) + "].";
}
} else {
message = "unexpected token near [" + getTokenErrorDisplay(token) + "].";
}
Location location = new Location(sourceName, token == null ? -1 : token.getStartIndex());
throw location.createError(new IllegalArgumentException(message, re));
}
use of org.antlr.v4.runtime.NoViableAltException in project antlr4 by antlr.
the class TestATNParserPrediction method checkDFAConstruction.
public void checkDFAConstruction(LexerGrammar lg, Grammar g, int decision, String[] inputString, String[] dfaString) {
// Tool.internalOption_ShowATNConfigsInDFA = true;
ATN lexatn = createATN(lg, true);
LexerATNSimulator lexInterp = new LexerATNSimulator(lexatn, new DFA[] { new DFA(lexatn.getDecisionState(Lexer.DEFAULT_MODE)) }, new PredictionContextCache());
semanticProcess(lg);
g.importVocab(lg);
semanticProcess(g);
ParserInterpreterForTesting interp = new ParserInterpreterForTesting(g, null);
for (int i = 0; i < inputString.length; i++) {
// Check DFA
IntegerList types = getTokenTypesViaATN(inputString[i], lexInterp);
// System.out.println(types);
TokenStream input = new IntTokenStream(types);
try {
interp.adaptivePredict(input, decision, ParserRuleContext.EMPTY);
} catch (NoViableAltException nvae) {
nvae.printStackTrace(System.err);
}
DFA dfa = interp.parser.decisionToDFA[decision];
assertEquals(dfaString[i], dfa.toString(g.getVocabulary()));
}
}
use of org.antlr.v4.runtime.NoViableAltException in project antlr4 by antlr.
the class TestATNInterpreter method testMustTrackPreviousGoodAltWithEOF.
@Test(expected = NoViableAltException.class)
public void testMustTrackPreviousGoodAltWithEOF() throws Exception {
LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "A : 'a' ;\n" + "B : 'b' ;\n" + "C : 'c' ;\n");
Grammar g = new Grammar("parser grammar T;\n" + "a : (A | A B) EOF;");
checkMatchedAlt(lg, g, "a", 1);
checkMatchedAlt(lg, g, "ab", 2);
try {
checkMatchedAlt(lg, g, "ac", 1);
} catch (NoViableAltException re) {
assertEquals(1, re.getOffendingToken().getTokenIndex());
assertEquals(3, re.getOffendingToken().getType());
throw re;
}
}
Aggregations