本文整理了Java中org.antlr.v4.runtime.Parser
类的一些代码示例,展示了Parser
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Parser
类的具体详情如下:
包路径:org.antlr.v4.runtime.Parser
类名称:Parser
[英]This is all the parsing support code essentially; most of it is error recovery stuff.
[中]这是所有解析支持代码的本质;大部分都是错误恢复的东西。
代码示例来源:origin: graphql-java/graphql-java
private InvalidSyntaxException mkException(Parser recognizer, RecognitionException cause) {
String sourcePreview = null;
String offendingToken = null;
SourceLocation sourceLocation = null;
Token currentToken = recognizer.getCurrentToken();
if (currentToken != null) {
int line = currentToken.getLine();
int column = currentToken.getCharPositionInLine();
offendingToken = currentToken.getText();
sourcePreview = mkPreview(line);
sourceLocation = new SourceLocation(line, column, sourceName);
}
return new InvalidSyntaxException(sourceLocation, null, sourcePreview, offendingToken, cause);
}
代码示例来源:origin: org.antlr/antlr4-runtime
/** Consume tokens until one matches the given token set. */
protected void consumeUntil(Parser recognizer, IntervalSet set) {
// System.err.println("consumeUntil("+set.toString(recognizer.getTokenNames())+")");
int ttype = recognizer.getInputStream().LA(1);
while (ttype != Token.EOF && !set.contains(ttype) ) {
//System.out.println("consume during recover LA(1)="+getTokenNames()[input.LA(1)]);
// recognizer.getInputStream().consume();
recognizer.consume();
ttype = recognizer.getInputStream().LA(1);
}
}
}
代码示例来源:origin: prestodb/presto
Parser parser = (Parser) recognizer;
ATN atn = parser.getATN();
currentState = atn.states.get(parser.getState());
currentToken = parser.getCurrentToken();
context = parser.getContext();
Analyzer analyzer = new Analyzer(atn, parser.getVocabulary(), specialRules, specialTokens, ignoredRules, parser.getTokenStream());
Multimap<Integer, String> candidates = analyzer.process(currentState, currentToken.getTokenIndex(), context);
.collect(Collectors.joining(", "));
message = String.format("mismatched input '%s'. Expecting: %s", ((Token) offendingSymbol).getText(), expected);
代码示例来源:origin: apache/incubator-shardingsphere
/**
* Get matched token by token type.
*
* @param tokenType token type
* @return matched token
* @throws RecognitionException mismatch throw exception
*/
public Token getMatchedToken(final int tokenType) throws RecognitionException {
Token result = parser.getCurrentToken();
boolean isIdentifierCompatible = false;
if (identifierTokenIndex == tokenType && identifierTokenIndex > result.getType()) {
isIdentifierCompatible = true;
}
if (result.getType() == tokenType || isIdentifierCompatible) {
if (Token.EOF != tokenType && isIdentifierCompatible && result instanceof CommonToken) {
((CommonToken) result).setType(identifierTokenIndex);
}
parser.getErrorHandler().reportMatch(parser);
parser.consume();
} else {
result = parser.getErrorHandler().recoverInline(parser);
if (parser.getBuildParseTree() && -1 == result.getTokenIndex()) {
parser.getContext().addErrorNode(parser.createErrorNode(parser.getContext(), result));
}
}
return result;
}
}
代码示例来源:origin: apache/incubator-shardingsphere
TokenStream tokens = recognizer.getInputStream();
Token token = tokens.LT(1);
ATNState state = recognizer.getInterpreter().atn.states.get(recognizer.getState());
IntervalSet nextTokens = recognizer.getATN().nextTokens(state);
if (nextTokens.contains(token.getType())) {
nextTokensContext = null;
nextTokensState = ATNState.INVALID_STATE_NUMBER;
return;
if (nextTokens.contains(Token.EPSILON)) {
if (null == nextTokensContext) {
nextTokensContext = recognizer.getContext();
nextTokensState = recognizer.getState();
if (nextTokens.contains(identifierTokenIndex)) {
((CommonToken) token).setType(identifierTokenIndex);
代码示例来源:origin: confluentinc/ksql
protected void reportMissingToken(final Parser recognizer) {
if (!this.inErrorRecoveryMode(recognizer)) {
this.beginErrorCondition(recognizer);
final Token t = recognizer.getCurrentToken();
final IntervalSet expecting = this.getExpectedTokens(recognizer);
final String msg =
"missing " + expecting.toString(recognizer.getVocabulary()) + " at " + this
.getTokenErrorDisplay(t);
recognizer.notifyErrorListeners(t, msg, (RecognitionException) null);
}
}
}
代码示例来源:origin: org.antlr/antlr4-runtime
@Override
public void enterEveryRule(ParserRuleContext ctx) {
System.out.println("enter " + getRuleNames()[ctx.getRuleIndex()] +
", LT(1)=" + _input.LT(1).getText());
}
代码示例来源:origin: org.antlr/antlr4-runtime
Token currentSymbol = recognizer.getCurrentToken();
IntervalSet expecting = getExpectedTokens(recognizer);
int expectedTokenType = Token.INVALID_TYPE;
if ( !expecting.isNil() ) {
expectedTokenType = expecting.getMinElement(); // get any element
else tokenText = "<missing "+recognizer.getVocabulary().getDisplayName(expectedTokenType)+">";
Token current = currentSymbol;
Token lookback = recognizer.getInputStream().LT(-1);
if ( current.getType() == Token.EOF && lookback!=null ) {
current = lookback;
recognizer.getTokenFactory().create(new Pair<TokenSource, CharStream>(current.getTokenSource(), current.getTokenSource().getInputStream()), expectedTokenType, tokenText,
Token.DEFAULT_CHANNEL,
-1, -1,
代码示例来源:origin: org.antlr/antlr4-runtime
ATNState s = recognizer.getInterpreter().atn.states.get(recognizer.getState());
TokenStream tokens = recognizer.getInputStream();
int la = tokens.LA(1);
IntervalSet nextTokens = recognizer.getATN().nextTokens(s);
if (nextTokens.contains(la)) {
if (nextTokens.contains(Token.EPSILON)) {
if (nextTokensContext == null) {
nextTokensContext = recognizer.getContext();
nextTokensState = recognizer.getState();
throw new InputMismatchException(recognizer);
IntervalSet expecting = recognizer.getExpectedTokens();
IntervalSet whatFollowsLoopIterationOrRule =
expecting.or(getErrorRecoverySet(recognizer));
consumeUntil(recognizer, whatFollowsLoopIterationOrRule);
break;
代码示例来源:origin: org.antlr/antlr4-runtime
int currentSymbolType = recognizer.getInputStream().LA(1);
ATNState currentState = recognizer.getInterpreter().atn.states.get(recognizer.getState());
ATNState next = currentState.transition(0).target;
ATN atn = recognizer.getInterpreter().atn;
IntervalSet expectingAtLL2 = atn.nextTokens(next, recognizer._ctx);
if ( expectingAtLL2.contains(currentSymbolType) ) {
reportMissingToken(recognizer);
return true;
代码示例来源:origin: javamonkey/beetl2.0
protected void reportInputMismatch(@NotNull Parser recognizer, @NotNull InputMismatchException e)
{
Token t1 = recognizer.getInputStream().LT(-1);
String msg = "缺少输入在 " + getTokenErrorDisplay(t1) + " 后面, 期望 "
+ e.getExpectedTokens().toString(recognizer.getTokenNames());
BeetlException exception = new BeetlParserException(BeetlException.PARSER_MISS_ERROR, msg, e);
// exception.token = this.getGrammarToken(e.getOffendingToken());
exception.pushToken(this.getGrammarToken(t1));
throw exception;
}
代码示例来源:origin: org.antlr/antlr4-runtime
Token t = getCurrentToken();
if (t.getType() > 0) {
_errHandler.reportMatch(this);
consume();
if (_buildParseTrees && t.getTokenIndex() == -1) {
_ctx.addErrorNode(createErrorNode(_ctx,t));
代码示例来源:origin: confluentinc/ksql
protected void reportNoViableAlternative(final Parser recognizer, final NoViableAltException e) {
final TokenStream tokens = recognizer.getInputStream();
final String input;
if (tokens != null) {
if (e.getStartToken().getType() == -1) {
input = "<EOF>";
} else {
input = tokens.getText(e.getStartToken(), e.getOffendingToken());
}
} else {
input = "<unknown input>";
}
final String msg = "no viable alternative at input " + this.escapeWSAndQuote(input);
recognizer.notifyErrorListeners(e.getOffendingToken(), msg, e);
}
代码示例来源:origin: org.antlr/antlr4-runtime
System.out.println("adaptivePredict decision "+decision+
" exec LA(1)=="+ getLookaheadName(input)+
" line "+input.LT(1).getLine()+":"+input.LT(1).getCharPositionInLine());
_startIndex = input.index();
_outerContext = outerContext;
DFA dfa = decisionToDFA[decision];
s0 = dfa.getPrecedenceStartState(parser.getPrecedence());
dfa.setPrecedenceStartState(parser.getPrecedence(), s0);
if ( debug ) System.out.println("DFA after predictATN: "+ dfa.toString(parser.getVocabulary()));
return alt;
代码示例来源:origin: org.antlr/antlr4-runtime
Token o = getCurrentToken();
if (o.getType() != EOF) {
getInputStream().consume();
ErrorNode node = _ctx.addErrorNode(createErrorNode(_ctx,o));
if (_parseListeners != null) {
for (ParseTreeListener listener : _parseListeners) {
TerminalNode node = _ctx.addChild(createTerminalNode(_ctx,o));
if (_parseListeners != null) {
for (ParseTreeListener listener : _parseListeners) {
代码示例来源:origin: org.antlr/antlr4-runtime
@Override
public void reportAttemptingFullContext(Parser recognizer,
DFA dfa,
int startIndex,
int stopIndex,
BitSet conflictingAlts,
ATNConfigSet configs)
{
String format = "reportAttemptingFullContext d=%s, input='%s'";
String decision = getDecisionDescription(recognizer, dfa);
String text = recognizer.getTokenStream().getText(Interval.of(startIndex, stopIndex));
String message = String.format(format, decision, text);
recognizer.notifyErrorListeners(message);
}
代码示例来源:origin: org.antlr/antlr4
parser.addErrorListener(new DiagnosticErrorListener());
parser.getInterpreter().setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION);
parser.setBuildParseTree(true);
parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
parser.setTokenStream(tokens);
parser.setTrace(trace);
System.out.println(tree.toStringTree(parser));
代码示例来源:origin: org.antlr/antlr4-runtime
if ( lastErrorIndex==recognizer.getInputStream().index() &&
lastErrorStates != null &&
lastErrorStates.contains(recognizer.getState()) ) {
recognizer.consume();
lastErrorIndex = recognizer.getInputStream().index();
if ( lastErrorStates==null ) lastErrorStates = new IntervalSet();
lastErrorStates.add(recognizer.getState());
IntervalSet followSet = getErrorRecoverySet(recognizer);
consumeUntil(recognizer, followSet);
代码示例来源:origin: org.antlr/antlr4-runtime
/**
* This is called by {@link #reportError} when the exception is an
* {@link InputMismatchException}.
*
* @see #reportError
*
* @param recognizer the parser instance
* @param e the recognition exception
*/
protected void reportInputMismatch(Parser recognizer,
InputMismatchException e)
{
String msg = "mismatched input "+getTokenErrorDisplay(e.getOffendingToken())+
" expecting "+e.getExpectedTokens().toString(recognizer.getVocabulary());
recognizer.notifyErrorListeners(e.getOffendingToken(), msg, e);
}
代码示例来源:origin: org.antlr/antlr4
@Override
public void recover(Parser recognizer, RecognitionException e) {
int errIndex = recognizer.getInputStream().index();
if ( firstErrorTokenIndex == -1 ) {
firstErrorTokenIndex = errIndex; // latch
}
// System.err.println("recover: error at " + errIndex);
TokenStream input = recognizer.getInputStream();
if ( input.index()<input.size()-1 ) { // don't consume() eof
recognizer.consume(); // just kill this bad token and let it continue.
}
}
内容来源于网络,如有侵权,请联系作者删除!