Search in sources :

Example 1 with IUnorderedGroupHelper

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;
}
Also used : AbstractElement(org.eclipse.xtext.AbstractElement) IUnorderedGroupHelper(org.eclipse.xtext.parser.antlr.IUnorderedGroupHelper)

Example 2 with IUnorderedGroupHelper

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());
    }
}
Also used : TokenSource(org.antlr.runtime.TokenSource) InfiniteRecursion(org.eclipse.xtext.ide.editor.contentassist.antlr.internal.InfiniteRecursion) IUnorderedGroupHelper(org.eclipse.xtext.parser.antlr.IUnorderedGroupHelper)

Example 3 with IUnorderedGroupHelper

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;
}
Also used : AbstractElement(org.eclipse.xtext.AbstractElement) ArrayList(java.util.ArrayList) IUnorderedGroupHelper(org.eclipse.xtext.parser.antlr.IUnorderedGroupHelper) UnorderedGroup(org.eclipse.xtext.UnorderedGroup)

Example 4 with IUnorderedGroupHelper

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;
    }
}
Also used : TokenSource(org.antlr.runtime.TokenSource) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) InfiniteRecursion(org.eclipse.xtext.ide.editor.contentassist.antlr.internal.InfiniteRecursion) IUnorderedGroupHelper(org.eclipse.xtext.parser.antlr.IUnorderedGroupHelper)

Example 5 with IUnorderedGroupHelper

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);
            }
        }
    }
}
Also used : TokenSource(org.antlr.runtime.TokenSource) Stopwatch(com.google.common.base.Stopwatch) Token(org.antlr.runtime.Token) IUnorderedGroupHelper(org.eclipse.xtext.parser.antlr.IUnorderedGroupHelper) IOException(java.io.IOException) WrappedException(org.eclipse.emf.common.util.WrappedException) ParseException(org.eclipse.xtext.parser.ParseException) LazyTokenStream(org.eclipse.n4js.parser.LazyTokenStream) ParseException(org.eclipse.xtext.parser.ParseException)

Aggregations

IUnorderedGroupHelper (org.eclipse.xtext.parser.antlr.IUnorderedGroupHelper)7 TokenSource (org.antlr.runtime.TokenSource)4 AbstractElement (org.eclipse.xtext.AbstractElement)3 ArrayList (java.util.ArrayList)2 Token (org.antlr.runtime.Token)2 UnorderedGroup (org.eclipse.xtext.UnorderedGroup)2 FollowElement (org.eclipse.xtext.ide.editor.contentassist.antlr.FollowElement)2 InfiniteRecursion (org.eclipse.xtext.ide.editor.contentassist.antlr.internal.InfiniteRecursion)2 Stopwatch (com.google.common.base.Stopwatch)1 IOException (java.io.IOException)1 BaseRecognizer (org.antlr.runtime.BaseRecognizer)1 CommonToken (org.antlr.runtime.CommonToken)1 WrappedException (org.eclipse.emf.common.util.WrappedException)1 LazyTokenStream (org.eclipse.n4js.parser.LazyTokenStream)1 LookAheadTerminal (org.eclipse.xtext.ide.editor.contentassist.antlr.LookAheadTerminal)1 ObservableXtextTokenStream (org.eclipse.xtext.ide.editor.contentassist.antlr.ObservableXtextTokenStream)1 AbstractInternalContentAssistParser (org.eclipse.xtext.ide.editor.contentassist.antlr.internal.AbstractInternalContentAssistParser)1 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)1 ParseException (org.eclipse.xtext.parser.ParseException)1