use of org.antlr.runtime.RecognizerSharedState in project drools by kiegroup.
the class DrlExprParser method parse.
/**
* Parse an expression from text
*/
public ConstraintConnectiveDescr parse(final String text) {
ConstraintConnectiveDescr constraint = null;
try {
DRLLexer lexer = DRLFactory.getDRLLexer(new ANTLRStringStream(text), languageLevel);
CommonTokenStream input = new CommonTokenStream(lexer);
RecognizerSharedState state = new RecognizerSharedState();
helper = new ParserHelper(input, state, languageLevel);
DRLExpressions parser = DRLFactory.getDRLExpressions(input, state, helper, languageLevel);
parser.setBuildDescr(true);
// setting initial value just in case
parser.setLeftMostExpr(null);
BaseDescr expr = parser.conditionalOrExpression();
if (expr != null && !parser.hasErrors()) {
constraint = ConstraintConnectiveDescr.newAnd();
constraint.addOrMerge(expr);
}
} catch (RecognitionException e) {
helper.reportError(e);
}
return constraint;
}
use of org.antlr.runtime.RecognizerSharedState in project n4js by eclipse.
the class SemicolonInjectionHelper method promoteEOL.
/**
* <p>
* Promotes EOL which may lead to an automatically inserted semicolon. This is probably the most important method
* for automatic semicolon insertion, as it is only possible to insert a semicolon in case of line breaks (even if
* they are hidden in a multi-line comment!).
* </p>
*/
public static void promoteEOL(Callback callback) {
RecognizerSharedState state = callback.getState();
TokenStream input = callback.getInput();
// Don't promote EOL if there was a syntax error at EOF
if (state.lastErrorIndex == input.size()) {
return;
}
// Get current token and its type (the possibly offending token).
Token prev = input.LT(-1);
Token next = input.LT(1);
int la = next.getType();
// A ML_COMMENT gets promoted when it contains an EOL.
for (int idx = prev == null ? 0 : prev.getTokenIndex() + 1, max = la == Token.EOF ? input.size() : next.getTokenIndex(); idx < max; idx++) {
Token lt = input.get(idx);
if (lt.getChannel() == Token.DEFAULT_CHANNEL) {
// On channel token found: stop scanning (previously promoted)
break;
} else if (isSemicolonEquivalent(lt)) {
// We found our EOL: promote the token to on channel, position the input on it and reset the rule
// start.
lt.setChannel(Token.DEFAULT_CHANNEL);
input.seek(idx);
break;
}
}
}
use of org.antlr.runtime.RecognizerSharedState in project n4js by eclipse.
the class SemicolonInjectionHelper method recover.
/**
* Recover from an error found on the input stream. This is for {@link NoViableAltException} and
* {@link MismatchedTokenException}. If you enable single token insertion and deletion, this will usually not handle
* mismatched symbol exceptions but there could be a mismatched token that the
* {@link Parser#match(IntStream, int, BitSet) match} routine could not recover from.
*/
public static void recover(IntStream inputStream, RecognitionException re, Callback callback) {
RecognizerSharedState state = callback.getState();
if (re instanceof MismatchedTokenException) {
// We expected a specific token
// if that is not a semicolon, ASI is pointless, perform normal recovery
int expecting = ((MismatchedTokenException) re).expecting;
if (expecting != InternalN4JSParser.Semicolon) {
// delete ASI message, a previously added ASI may fix too much! cf.
callback.discardError();
// IDEBUG-215
callback.recoverBase(inputStream, re);
return;
}
}
// System.out.println("Line: " + re.line + ":" + re.index);
int unexpectedTokenType = re.token.getType();
if (!followedBySemicolon(state, callback.getRecoverySets(), re.index) || isOffendingToken(unexpectedTokenType)) {
callback.recoverBase(inputStream, re);
} else {
int la = inputStream.LA(1);
TokenStream casted = (TokenStream) inputStream;
if (!isOffendingToken(la)) {
// previous on channel token.
for (int ix = re.token.getTokenIndex() - 1; ix > 0; ix--) {
Token lt = casted.get(ix);
if (lt.getChannel() == Token.DEFAULT_CHANNEL) {
// On channel token found: stop scanning.
callback.recoverBase(inputStream, re);
return;
} else if (lt.getType() == InternalN4JSParser.RULE_EOL) {
// rule start.
if (!callback.allowASI(re)) {
callback.recoverBase(inputStream, re);
return;
}
if (!findCommaBeforeEOL(casted, ix)) {
callback.addASIMessage();
return;
}
} else if (lt.getType() == InternalN4JSParser.RULE_ML_COMMENT) {
String tokenText = lt.getText();
if (!findCommaBeforeEOL(casted, ix) && (tokenText.indexOf('\n', 2) >= 2 || tokenText.indexOf('\r', 2) >= 2)) {
callback.addASIMessage();
return;
}
}
}
callback.recoverBase(inputStream, re);
}
}
}
use of org.antlr.runtime.RecognizerSharedState in project drools by kiegroup.
the class DrlExprParser method parse.
/**
* Parse an expression from text
*/
public ConstraintConnectiveDescr parse(final String text) {
ConstraintConnectiveDescr constraint = null;
try {
DRLLexer lexer = DRLFactory.getDRLLexer(new ANTLRStringStream(text), languageLevel);
CommonTokenStream input = new CommonTokenStream(lexer);
RecognizerSharedState state = new RecognizerSharedState();
helper = new ParserHelper(input, state, languageLevel);
DRLExpressions parser = DRLFactory.getDRLExpressions(input, state, helper, languageLevel);
parser.setBuildDescr(true);
// setting initial value just in case
parser.setLeftMostExpr(null);
BaseDescr expr = parser.conditionalOrExpression();
if (expr != null && !parser.hasErrors()) {
constraint = ConstraintConnectiveDescr.newAnd();
constraint.addOrMerge(expr);
}
} catch (RecognitionException e) {
helper.reportError(e);
}
return constraint;
}
Aggregations