下面列出了怎么用org.antlr.runtime.RecognizerSharedState的API类实例代码及写法,或者点击链接到github查看源代码。
/** 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 );
parser.setLeftMostExpr( null ); // setting initial value just in case
BaseDescr expr = parser.conditionalOrExpression();
if ( expr != null && !parser.hasErrors() ) {
constraint = ConstraintConnectiveDescr.newAnd();
constraint.addOrMerge( expr );
}
} catch ( RecognitionException e ) {
helper.reportError( e );
}
return constraint;
}
/**
* Returns {@code true} if the set of expected follow-states includes an implicit or explicit semicolon.
*/
private static boolean followedBySemicolon(RecognizerSharedState state, Callback.RecoverySets recoverySets,
int currentIndex) {
int top = state._fsp;
if (currentIndex != state.lastErrorIndex) {
long[] array = state.following[top].toPackedArray();
if (array.length == 1 && array[0] == (1L << Token.EOR_TOKEN_TYPE)) {
return false;
}
}
for (int i = top; i >= 0; i--) {
BitSet localFollowSet = state.following[i];
if (recoverySets.matches(localFollowSet)) {
return true;
}
}
return false;
}
public ParserHelper(TokenStream input,
RecognizerSharedState state,
LanguageLevelOption languageLevel) {
this.errorMessageFactory = new DroolsParserExceptionFactory( paraphrases, languageLevel );
this.input = input;
this.state = state;
this.languageLevel = languageLevel;
}
public static DRLExpressions getDRLExpressions(TokenStream input, RecognizerSharedState state, ParserHelper helper, LanguageLevelOption languageLevel ) {
switch (languageLevel) {
case DRL5:
return new DRL5Expressions(input, state, helper);
case DRL6:
case DRL6_STRICT:
return new DRL6Expressions(input, state, helper);
}
throw new RuntimeException("Unknown language level");
}
/**
* <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();
if (la == InternalN4JSParser.Semicolon) {
return;
}
// Promoting an EOL means switching it from off channel to on channel.
// 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;
}
}
}
public BaseInternalContentAssistParser(TokenStream input, RecognizerSharedState state) {
super(input, state);
this.grammarElements = new ArrayList<EObject>();
this.localTrace = new ArrayList<EObject>();
this.paramStack = new ArrayList<Integer>();
this.grammarElementsWithParams = new ArrayList<Integer>();
this.followElements = new LinkedHashSetWithoutNull<FollowElement>();
}
/**
* Construct from a token stream and a shared state.
* @param input the token stream
* @param state the shared state
* @param errorHandler handles error messages
*/
public CobolStructureParserImpl(
final TokenStream input,
final RecognizerSharedState state,
final RecognizerErrorHandler errorHandler) {
super(input, state);
_errorHandler = errorHandler;
}
/**
* Construct from a character stream and a shared state.
* @param input the character stream
* @param state the shared state
* @param errorHandler handles error messages
*/
public CobolStructureLexerImpl(
final CharStream input,
final RecognizerSharedState state,
final RecognizerErrorHandler errorHandler) {
super(input, state);
_errorHandler = errorHandler;
}
/**
* Construct from a tree nodes stream and a shared state.
* @param input the tree nodes stream
* @param state the shared state
* @param errorHandler handles error messages
*/
public CobolStructureEmitterImpl(
final TreeNodeStream input,
final RecognizerSharedState state,
final RecognizerErrorHandler errorHandler) {
super(input, state);
_errorHandler = errorHandler;
}
public FastSimpleGenericEdifactDirectXMLLexer( CharStream input ) {
this( input, new RecognizerSharedState() );
}
public FastSimpleGenericEdifactDirectXMLLexer( CharStream input, RecognizerSharedState state ) {
super( input, state );
}
public FastSimpleGenericEdifactDirectXMLParser( TokenStream input ) {
this( input, new RecognizerSharedState() );
}
public FastSimpleGenericEdifactDirectXMLParser( TokenStream input, RecognizerSharedState state ) {
super( input, state );
}
public JavaLexer(CharStream input) {
this(input, new RecognizerSharedState());
}
public JavaLexer(CharStream input, RecognizerSharedState state) {
super(input,state);
}
public DSLMapParser(TokenStream input) {
this(input, new RecognizerSharedState());
}
public DSLMapParser(TokenStream input, RecognizerSharedState state) {
super(input, state);
}
public DSLMapLexer(CharStream input) {
this(input, new RecognizerSharedState());
}
public DSLMapLexer(CharStream input, RecognizerSharedState state) {
super(input,state);
}
public DSLMapWalker(TreeNodeStream input) {
this(input, new RecognizerSharedState());
}
public DSLMapWalker(TreeNodeStream input, RecognizerSharedState state) {
super(input, state);
}
public AbstractDRLParser(TokenStream input) {
this.input = input;
this.state = new RecognizerSharedState();
this.helper = new ParserHelper( input, state, getLanguageLevel() );
}
public AbstractDRLLexer(CharStream input, RecognizerSharedState state) {
super(input, state);
}
public DRLExpressions(TokenStream input, RecognizerSharedState state) {
super(input, state);
}
public FastSimpleGenericEdifactDirectXMLParser( TokenStream input ) {
this( input, new RecognizerSharedState() );
}
/**
* Delegates to super constructor.
*/
protected AbstractInternalHighlightingAntlrParser(TokenStream input, RecognizerSharedState state) {
super(input, state);
}
@Override
public RecognizerSharedState getState() {
return state;
}
/**
* Allows to access the protected state of the super type.
*/
public RecognizerSharedState getState() {
return state;
}
@Override
public RecognizerSharedState getState() {
return state;
}
/**
* 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) {
callback.discardError(); // delete ASI message, a previously added ASI may fix too much! cf.
// 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)) {
// Start on the position before the current token and scan backwards off channel tokens until the
// 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) {
// We found our EOL: everything's good, no need to do additional recovering
// 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);
}
}
}