use of org.antlr.runtime.BaseRecognizer in project n4js by eclipse.
the class CustomN4JSParser method getFollowElements.
@SuppressWarnings("hiding")
@Override
public Collection<FollowElement> getFollowElements(FollowElement element) {
if (element.getLookAhead() <= 1)
throw new IllegalArgumentException("lookahead may not be less than or equal to 1");
Collection<FollowElement> result = new ArrayList<>();
Collection<AbstractElement> elementsToParse = this.reflective("getElementsToParse", element);
for (AbstractElement elementToParse : elementsToParse) {
// fix is here
elementToParse = unwrapSingleElementGroups(elementToParse);
// done
String ruleName = getRuleName(elementToParse);
String[][] allRuleNames = reflective("getRequiredRuleNames", ruleName, element.getParamStack(), elementToParse);
for (String[] ruleNames : allRuleNames) {
for (int i = 0; i < ruleNames.length; i++) {
AbstractInternalContentAssistParser parser = createParser();
parser.setUnorderedGroupHelper(getUnorderedGroupHelper().get());
parser.getUnorderedGroupHelper().initializeWith(parser);
final Iterator<LookAheadTerminal> iter = element.getLookAheadTerminals().iterator();
ObservableXtextTokenStream tokens = new ObservableXtextTokenStream(new TokenSource() {
@Override
public Token nextToken() {
if (iter.hasNext()) {
LookAheadTerminal lookAhead = iter.next();
return lookAhead.getToken();
}
return Token.EOF_TOKEN;
}
@Override
public String getSourceName() {
return "LookAheadTerminalTokenSource";
}
}, parser);
parser.setTokenStream(tokens);
tokens.setListener(parser);
parser.getGrammarElements().addAll(element.getTrace());
parser.getGrammarElements().add(elementToParse);
parser.getLocalTrace().addAll(element.getLocalTrace());
parser.getLocalTrace().add(elementToParse);
parser.getParamStack().addAll(element.getParamStack());
if (elementToParse instanceof UnorderedGroup && element.getGrammarElement() == elementToParse) {
UnorderedGroup group = (UnorderedGroup) elementToParse;
final IUnorderedGroupHelper helper = parser.getUnorderedGroupHelper();
helper.enter(group);
for (AbstractElement consumed : element.getHandledUnorderedGroupElements()) {
parser.before(consumed);
helper.select(group, group.getElements().indexOf(consumed));
helper.returnFromSelection(group);
parser.after(consumed);
}
parser.setUnorderedGroupHelper(new IUnorderedGroupHelper() {
boolean first = true;
@Override
public void initializeWith(BaseRecognizer recognizer) {
helper.initializeWith(recognizer);
}
@Override
public void enter(UnorderedGroup group) {
if (!first)
helper.enter(group);
first = false;
}
@Override
public void leave(UnorderedGroup group) {
helper.leave(group);
}
@Override
public boolean canSelect(UnorderedGroup group, int index) {
return helper.canSelect(group, index);
}
@Override
public void select(UnorderedGroup group, int index) {
helper.select(group, index);
}
@Override
public void returnFromSelection(UnorderedGroup group) {
helper.returnFromSelection(group);
}
@Override
public boolean canLeave(UnorderedGroup group) {
return helper.canLeave(group);
}
@Override
public UnorderedGroupState snapShot(UnorderedGroup... groups) {
return helper.snapShot(groups);
}
});
}
Collection<FollowElement> elements = reflective("getFollowElements", parser, elementToParse, ruleNames, i);
result.addAll(elements);
}
}
}
return result;
}
Aggregations