use of org.antlr.runtime.RecognitionException in project drools by kiegroup.
the class DRL5Parser method recoverFromMismatchedToken.
/**
* Attempt to recover from a single missing or extra token.
*
* EXTRA TOKEN
*
* LA(1) is not what we are looking for. If LA(2) has the right token,
* however, then assume LA(1) is some extra spurious token. Delete it
* and LA(2) as if we were doing a normal match(), which advances the
* input.
*
* MISSING TOKEN
*
* If current token is consistent with what could come after
* ttype then it is ok to "insert" the missing token, else throw
* exception For example, Input "i=(3;" is clearly missing the
* ')'. When the parser returns from the nested call to expr, it
* will have call chain:
*
* stat -> expr -> atom
*
* and it will be trying to match the ')' at this point in the
* derivation:
*
* => ID '=' '(' INT ')' ('+' atom)* ';'
* ^
* match() will see that ';' doesn't match ')' and report a
* mismatched token error. To recover, it sees that LA(1)==';'
* is in the set of tokens that can follow the ')' token
* reference in rule atom. It can assume that you forgot the ')'.
*/
protected Token recoverFromMismatchedToken(TokenStream input, int ttype, String text, int[] follow) throws RecognitionException {
RecognitionException e = null;
// if next token is what we are looking for then "delete" this token
if (mismatchIsUnwantedToken(input, ttype, text)) {
e = new UnwantedTokenException(ttype, input);
// simply delete extra token
input.consume();
// report after consuming so AW sees the token in the exception
reportError(e);
// we want to return the token we're actually matching
Token matchedSymbol = input.LT(1);
// move past ttype token as if all were ok
input.consume();
return matchedSymbol;
}
// can't recover with single token deletion, try insertion
if (mismatchIsMissingToken(input, follow)) {
e = new MissingTokenException(ttype, input, null);
// report after inserting so AW sees the token in the exception
reportError(e);
return null;
}
// even that didn't work; must throw the exception
if (text != null) {
e = new DroolsMismatchedTokenException(ttype, text, input);
} else {
e = new MismatchedTokenException(ttype, input);
}
throw e;
}
use of org.antlr.runtime.RecognitionException in project drools by kiegroup.
the class DRL5Parser method typeDeclaration.
/**
* typeDeclaration := [TYPE] qualifiedIdentifier (EXTENDS qualifiedIdentifier)?
* annotation*
* field*
* END
*
* @return
* @throws RecognitionException
*/
public TypeDeclarationDescr typeDeclaration(DeclareDescrBuilder ddb, boolean isTrait) throws RecognitionException {
TypeDeclarationDescrBuilder declare = null;
try {
declare = helper.start(ddb, TypeDeclarationDescrBuilder.class, null);
declare.setTrait(isTrait);
if (helper.validateIdentifierKey(DroolsSoftKeywords.TYPE)) {
// 'type'
match(input, DRL5Lexer.ID, DroolsSoftKeywords.TYPE, null, DroolsEditorType.KEYWORD);
if (state.failed)
return null;
}
// type may be qualified when adding metadata
String type = qualifiedIdentifier();
if (state.failed)
return null;
if (state.backtracking == 0)
declare.name(type);
if (helper.validateIdentifierKey(DroolsSoftKeywords.EXTENDS)) {
match(input, DRL5Lexer.ID, DroolsSoftKeywords.EXTENDS, null, DroolsEditorType.KEYWORD);
if (!state.failed) {
// Going for type includes generics, which is a no-no (JIRA-3040)
String superType = qualifiedIdentifier();
declare.superType(superType);
while (input.LA(1) == DRL5Lexer.COMMA) {
match(input, DRL5Lexer.COMMA, null, null, DroolsEditorType.SYMBOL);
superType = qualifiedIdentifier();
declare.superType(superType);
}
}
}
while (input.LA(1) == DRL5Lexer.AT) {
// annotation*
annotation(declare);
if (state.failed)
return null;
}
// boolean qualified = type.indexOf( '.' ) >= 0;
while (// ! qualified &&
input.LA(1) == DRL5Lexer.ID && !helper.validateIdentifierKey(DroolsSoftKeywords.END)) {
// field*
field(declare);
if (state.failed)
return null;
}
match(input, DRL5Lexer.ID, DroolsSoftKeywords.END, null, DroolsEditorType.KEYWORD);
if (state.failed)
return null;
} catch (RecognitionException re) {
reportError(re);
} finally {
helper.end(TypeDeclarationDescrBuilder.class, declare);
}
return (declare != null) ? declare.getDescr() : null;
}
use of org.antlr.runtime.RecognitionException in project drools by kiegroup.
the class DRL5Parser method qualifiedIdentifier.
/**
* Matches a qualified identifier
*
* qualifiedIdentifier := ID ( DOT ID )*
*
* @return
* @throws RecognitionException
*/
public String qualifiedIdentifier() throws RecognitionException {
String qi = "";
try {
Token first = match(input, DRL5Lexer.ID, null, new int[] { DRL5Lexer.DOT }, DroolsEditorType.IDENTIFIER);
if (state.failed)
return qi;
Token last = first;
while (input.LA(1) == DRL5Lexer.DOT && input.LA(2) == DRL5Lexer.ID) {
last = match(input, DRL5Lexer.DOT, null, new int[] { DRL5Lexer.ID }, DroolsEditorType.IDENTIFIER);
if (state.failed)
return qi;
last = match(input, DRL5Lexer.ID, null, new int[] { DRL5Lexer.DOT }, DroolsEditorType.IDENTIFIER);
if (state.failed)
return qi;
}
qi = input.toString(first, last);
qi = qi.replace(" ", "");
} catch (RecognitionException re) {
reportError(re);
}
return qi;
}
use of org.antlr.runtime.RecognitionException in project drools by kiegroup.
the class DRL5Parser method declare.
/* ------------------------------------------------------------------------------------------------
* DECLARE STATEMENT
* ------------------------------------------------------------------------------------------------ */
/**
* declare := DECLARE
* | (ENTRY-POINT) => entryPointDeclaration
* | (WINDOW) => windowDeclaration
* | (TRAIT) => typeDeclaration (trait)
* | (ENUM) => enumDeclaration
* | typeDeclaration (class)
* END
*
* @return
* @throws RecognitionException
*/
public BaseDescr declare(PackageDescrBuilder pkg) throws RecognitionException {
BaseDescr declaration = null;
try {
DeclareDescrBuilder declare = helper.start(pkg, DeclareDescrBuilder.class, null);
// 'declare'
match(input, DRL5Lexer.ID, DroolsSoftKeywords.DECLARE, null, DroolsEditorType.KEYWORD);
if (state.failed)
return null;
if (helper.validateIdentifierKey(DroolsSoftKeywords.ENTRY)) {
// entry point declaration
declaration = entryPointDeclaration(declare);
} else if (helper.validateIdentifierKey(DroolsSoftKeywords.WINDOW)) {
// window declaration
declaration = windowDeclaration(declare);
} else if (helper.validateIdentifierKey(DroolsSoftKeywords.TRAIT)) {
// trait type declaration
// 'trait'
match(input, DRL5Lexer.ID, DroolsSoftKeywords.TRAIT, null, DroolsEditorType.KEYWORD);
if (state.failed)
return null;
declaration = typeDeclaration(declare, true);
} else if (helper.validateIdentifierKey(DroolsSoftKeywords.ENUM)) {
match(input, DRL5Lexer.ID, DroolsSoftKeywords.ENUM, null, DroolsEditorType.KEYWORD);
if (state.failed)
return null;
declaration = enumDeclaration(declare);
} else {
// class type declaration
declaration = typeDeclaration(declare, false);
}
} catch (RecognitionException re) {
reportError(re);
}
return declaration;
}
use of org.antlr.runtime.RecognitionException in project drools by kiegroup.
the class DRL5Parser method entryPointDeclaration.
/**
* entryPointDeclaration := ENTRY-POINT stringId annotation* END
*
* @return
* @throws RecognitionException
*/
public EntryPointDeclarationDescr entryPointDeclaration(DeclareDescrBuilder ddb) throws RecognitionException {
EntryPointDeclarationDescrBuilder declare = null;
try {
declare = helper.start(ddb, EntryPointDeclarationDescrBuilder.class, null);
match(input, DRL5Lexer.ID, DroolsSoftKeywords.ENTRY, null, DroolsEditorType.KEYWORD);
if (state.failed)
return null;
match(input, DRL5Lexer.MINUS, null, null, DroolsEditorType.KEYWORD);
if (state.failed)
return null;
match(input, DRL5Lexer.ID, DroolsSoftKeywords.POINT, null, DroolsEditorType.KEYWORD);
if (state.failed)
return null;
String ep = stringId();
if (state.failed)
return null;
if (state.backtracking == 0) {
declare.entryPointId(ep);
}
while (input.LA(1) == DRL5Lexer.AT) {
// annotation*
annotation(declare);
if (state.failed)
return null;
}
match(input, DRL5Lexer.ID, DroolsSoftKeywords.END, null, DroolsEditorType.KEYWORD);
if (state.failed)
return null;
} catch (RecognitionException re) {
reportError(re);
} finally {
helper.end(EntryPointDeclarationDescrBuilder.class, declare);
}
return (declare != null) ? declare.getDescr() : null;
}
Aggregations