use of org.eclipse.xtext.parser.antlr.IUnorderedGroupHelper in project xtext-core by eclipse.
the class BaseContentAssistParser method getInitializedUnorderedGroupHelper.
/**
* @since 2.14
*/
protected IUnorderedGroupHelper getInitializedUnorderedGroupHelper(FE element, InternalParser parser, UnorderedGroup group) {
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);
}
return helper;
}
use of org.eclipse.xtext.parser.antlr.IUnorderedGroupHelper in project xtext-core by eclipse.
the class BaseContentAssistParser method getFollowElements.
public Collection<FE> getFollowElements(String input, boolean strict) {
TokenSource tokenSource = createTokenSource(input);
InternalParser parser = createParser();
parser.setStrict(strict);
ObservableXtextTokenStream tokens = new ObservableXtextTokenStream(tokenSource, parser);
tokens.setInitialHiddenTokens(getInitialHiddenTokens());
parser.setTokenStream(tokens);
IUnorderedGroupHelper helper = createUnorderedGroupHelper();
parser.setUnorderedGroupHelper(helper);
helper.initializeWith(parser);
tokens.setListener(parser);
try {
return Lists.newArrayList(getFollowElements(parser));
} catch (InfiniteRecursion infinite) {
return Lists.newArrayList(parser.getFollowElements());
}
}
use of org.eclipse.xtext.parser.antlr.IUnorderedGroupHelper in project xtext-core by eclipse.
the class BaseContentAssistParser method getFollowElements.
public Collection<FE> getFollowElements(FE element) {
if (element.getLookAhead() <= 1)
throw new IllegalArgumentException("lookahead may not be less than or equal to 1");
Collection<FE> result = new ArrayList<FE>();
for (AbstractElement elementToParse : getElementsToParse(element)) {
elementToParse = unwrapSingleElementGroups(elementToParse);
String ruleName = getRuleName(elementToParse);
String[][] allRuleNames = getRequiredRuleNames(ruleName, element.getParamStack(), elementToParse);
for (String[] ruleNames : allRuleNames) {
for (int i = 0; i < ruleNames.length; i++) {
InternalParser parser = createParser();
parser.setUnorderedGroupHelper(createUnorderedGroupHelper());
parser.getUnorderedGroupHelper().initializeWith(parser);
ObservableXtextTokenStream tokens = setTokensFromFollowElement(parser, element);
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;
IUnorderedGroupHelper helper = getInitializedUnorderedGroupHelper(element, parser, group);
parser.setUnorderedGroupHelper(ignoreFirstEntrance(helper));
}
Collection<FE> elements = getFollowElements(parser, elementToParse, ruleNames, i);
result.addAll(elements);
}
}
}
return result;
}
use of org.eclipse.xtext.parser.antlr.IUnorderedGroupHelper in project xtext-core by eclipse.
the class BaseContentAssistParser method getFollowElements.
public Collection<FE> getFollowElements(IParseResult parseResult, EntryPointFinder entryPointFinder, int offset, boolean strict) {
ICompositeNode entryPoint = entryPointFinder.findEntryPoint(parseResult, offset);
if (entryPoint != null) {
String parseMe = getTextToParse(parseResult, entryPoint, offset);
TokenSource tokenSource = createTokenSource(parseMe);
InternalParser parser = createParser();
parser.setStrict(strict);
ObservableXtextTokenStream tokens = new ObservableXtextTokenStream(tokenSource, parser);
tokens.setInitialHiddenTokens(getInitialHiddenTokens());
parser.setTokenStream(tokens);
IUnorderedGroupHelper helper = createUnorderedGroupHelper();
parser.setUnorderedGroupHelper(helper);
helper.initializeWith(parser);
tokens.setListener(parser);
try {
Collection<FE> followElements = getFollowElements(parser, getEntryGrammarElement(entryPoint));
return Lists.newArrayList(followElements);
} catch (InfiniteRecursion infinite) {
return Lists.newArrayList(parser.getFollowElements());
}
} else {
String text = parseResult.getRootNode().getText();
String parseMe = text.substring(0, offset);
initializeFor(NodeModelUtils.getEntryParserRule(parseResult.getRootNode()));
Collection<FE> followElements = getFollowElements(parseMe, strict);
return followElements;
}
}
use of org.eclipse.xtext.parser.antlr.IUnorderedGroupHelper in project n4js by eclipse.
the class HighlightingParser method doParse.
private List<Token> doParse(CharStream in) {
TokenSource tokenSource = createLexer(in);
LazyTokenStream tokenStream = createTokenStream(tokenSource);
setInitialHiddenTokens(tokenStream);
InternalN4JSParser parser = createParser(tokenStream);
IUnorderedGroupHelper helper = unorderedGroupHelper.get();
if (!(helper instanceof IUnorderedGroupHelper.Null)) {
throw new IllegalStateException("Unexpected usage of unordered groups.");
}
Stopwatch stopwatch = null;
boolean debug = LOGGER.isDebugEnabled();
// boolean debug = true;
if (debug) {
stopwatch = Stopwatch.createStarted();
}
try {
parser.entryRuleScript();
while (tokenStream.LT(1) != Token.EOF_TOKEN) {
tokenStream.consume();
}
@SuppressWarnings("unchecked") List<Token> result = tokenStream.getTokens();
return result;
} catch (Exception re) {
throw new ParseException(re.getMessage(), re);
} finally {
if (debug) {
assert stopwatch != null;
long elapsed = stopwatch.stop().elapsed(TimeUnit.MILLISECONDS);
if (elapsed > 5) {
LOGGER.warn("Coloring parser took: " + elapsed);
}
}
}
}
Aggregations