use of org.antlr.v4.runtime.misc.Nullable in project titan.EclipsePlug-ins by eclipse.
the class TitanListener method syntaxError.
@Override
public void syntaxError(@NotNull final Recognizer<?, ?> recognizer, @Nullable final Object offendingSymbol, final int line, final int charPositionInLine, @NotNull final String msg, @Nullable final RecognitionException e) {
SyntacticErrorStorage errorStorage;
if (offendingSymbol instanceof CommonToken) {
final CommonToken token = (CommonToken) offendingSymbol;
errorStorage = new SyntacticErrorStorage(line, token.getStartIndex(), token.getStopIndex() + 1, msg, e);
} else {
errorStorage = new SyntacticErrorStorage(line, charPositionInLine, charPositionInLine + 1, msg, e);
}
errorsStored.add(errorStorage);
}
use of org.antlr.v4.runtime.misc.Nullable in project antlr4 by tunnelvisionlabs.
the class ParserATNFactory method newState.
@NotNull
public ATNState newState(@Nullable GrammarAST node) {
ATNState n = new BasicState();
n.setRuleIndex(currentRule.index);
atn.addState(n);
return n;
}
use of org.antlr.v4.runtime.misc.Nullable in project antlr4 by tunnelvisionlabs.
the class Trees method getNodeText.
public static String getNodeText(@NotNull Tree t, @Nullable List<String> ruleNames) {
if (ruleNames != null) {
if (t instanceof RuleNode) {
RuleContext ruleContext = ((RuleNode) t).getRuleContext();
int ruleIndex = ruleContext.getRuleIndex();
String ruleName = ruleNames.get(ruleIndex);
int altNumber = ruleContext.getAltNumber();
if (altNumber != ATN.INVALID_ALT_NUMBER) {
return ruleName + ":" + altNumber;
}
return ruleName;
} else if (t instanceof ErrorNode) {
return t.toString();
} else if (t instanceof TerminalNode) {
Token symbol = ((TerminalNode) t).getSymbol();
if (symbol != null) {
String s = symbol.getText();
return s;
}
}
}
// no recog for rule names
Object payload = t.getPayload();
if (payload instanceof Token) {
return ((Token) payload).getText();
}
return t.getPayload().toString();
}
use of org.antlr.v4.runtime.misc.Nullable in project antlr4 by tunnelvisionlabs.
the class ScopeParser method parseAttributeDef.
/**
* For decls like "String foo" or "char *foo32[]" compute the ID
* and type declarations. Also handle "int x=3" and 'T t = new T("foo")'
* but if the separator is ',' you cannot use ',' in the initvalue
* unless you escape use "\," escape.
*/
public static Attribute parseAttributeDef(@Nullable ActionAST action, @NotNull Tuple2<String, Integer> decl, Grammar g) {
if (decl.getItem1() == null)
return null;
Attribute attr = new Attribute();
int rightEdgeOfDeclarator = decl.getItem1().length() - 1;
int equalsIndex = decl.getItem1().indexOf('=');
if (equalsIndex > 0) {
// everything after the '=' is the init value
attr.initValue = decl.getItem1().substring(equalsIndex + 1, decl.getItem1().length()).trim();
rightEdgeOfDeclarator = equalsIndex - 1;
}
String declarator = decl.getItem1().substring(0, rightEdgeOfDeclarator + 1);
Tuple2<Integer, Integer> p;
String text = decl.getItem1();
text = text.replaceAll("::", "");
if (text.contains(":")) {
// declarator has type appearing after the name like "x:T"
p = _parsePostfixDecl(attr, declarator, action, g);
} else {
// declarator has type appearing before the name like "T x"
p = _parsePrefixDecl(attr, declarator, action, g);
}
int idStart = p.getItem1();
int idStop = p.getItem2();
attr.decl = decl.getItem1();
if (action != null) {
String actionText = action.getText();
int[] lines = new int[actionText.length()];
int[] charPositionInLines = new int[actionText.length()];
for (int i = 0, line = 0, col = 0; i < actionText.length(); i++, col++) {
lines[i] = line;
charPositionInLines[i] = col;
if (actionText.charAt(i) == '\n') {
line++;
col = -1;
}
}
int[] charIndexes = new int[actionText.length()];
for (int i = 0, j = 0; i < actionText.length(); i++, j++) {
charIndexes[j] = i;
// skip comments
if (i < actionText.length() - 1 && actionText.charAt(i) == '/' && actionText.charAt(i + 1) == '/') {
while (i < actionText.length() && actionText.charAt(i) != '\n') {
i++;
}
}
}
int declOffset = charIndexes[decl.getItem2()];
int declLine = lines[declOffset + idStart];
int line = action.getToken().getLine() + declLine;
int charPositionInLine = charPositionInLines[declOffset + idStart];
if (declLine == 0) {
/* offset for the start position of the ARG_ACTION token, plus 1
* since the ARG_ACTION text had the leading '[' stripped before
* reaching the scope parser.
*/
charPositionInLine += action.getToken().getCharPositionInLine() + 1;
}
int offset = ((CommonToken) action.getToken()).getStartIndex();
attr.token = new CommonToken(action.getToken().getInputStream(), ANTLRParser.ID, BaseRecognizer.DEFAULT_TOKEN_CHANNEL, offset + declOffset + idStart + 1, offset + declOffset + idStop);
attr.token.setLine(line);
attr.token.setCharPositionInLine(charPositionInLine);
assert attr.name.equals(attr.token.getText()) : "Attribute text should match the pseudo-token text at this point.";
}
return attr;
}
use of org.antlr.v4.runtime.misc.Nullable in project antlr4 by tunnelvisionlabs.
the class ParserATNSimulator method adaptivePredict.
public int adaptivePredict(@NotNull TokenStream input, int decision, @Nullable ParserRuleContext outerContext, boolean useContext) {
DFA dfa = atn.decisionToDFA[decision];
assert dfa != null;
if (optimize_ll1 && !dfa.isPrecedenceDfa() && !dfa.isEmpty()) {
int ll_1 = input.LA(1);
if (ll_1 >= 0 && ll_1 <= Short.MAX_VALUE) {
int key = (decision << 16) + ll_1;
Integer alt = atn.LL1Table.get(key);
if (alt != null) {
return alt;
}
}
}
this.dfa = dfa;
if (force_global_context) {
useContext = true;
} else if (!always_try_local_context) {
useContext |= dfa.isContextSensitive();
}
userWantsCtxSensitive = useContext || (predictionMode != PredictionMode.SLL && outerContext != null && !atn.decisionToState.get(decision).sll);
if (outerContext == null) {
outerContext = ParserRuleContext.emptyContext();
}
SimulatorState state = null;
if (!dfa.isEmpty()) {
state = getStartState(dfa, input, outerContext, useContext);
}
if (state == null) {
if (outerContext == null)
outerContext = ParserRuleContext.emptyContext();
if (debug)
System.out.println("ATN decision " + dfa.decision + " exec LA(1)==" + getLookaheadName(input) + ", outerContext=" + outerContext.toString(parser));
state = computeStartState(dfa, outerContext, useContext);
}
int m = input.mark();
int index = input.index();
try {
int alt = execDFA(dfa, input, index, state);
if (debug)
System.out.println("DFA after predictATN: " + dfa.toString(parser.getVocabulary(), parser.getRuleNames()));
return alt;
} finally {
this.dfa = null;
input.seek(index);
input.release(m);
}
}
Aggregations