Search in sources :

Example 6 with FollowElement

use of org.eclipse.xtext.ide.editor.contentassist.antlr.FollowElement in project xtext-eclipse by eclipse.

the class Bug303200Test method testBug303200_03.

@Test
public void testBug303200_03() throws Exception {
    Bug303200TestLanguageParser parser = get(Bug303200TestLanguageParser.class);
    Collection<FollowElement> followElements = Lists.newArrayList(parser.getFollowElements("function f() {} function f() {", false));
    assertEquals(4, followElements.size());
}
Also used : Bug303200TestLanguageParser(org.eclipse.xtext.ui.tests.editor.contentassist.ide.contentassist.antlr.Bug303200TestLanguageParser) FollowElement(org.eclipse.xtext.ide.editor.contentassist.antlr.FollowElement) Test(org.junit.Test)

Example 7 with FollowElement

use of org.eclipse.xtext.ide.editor.contentassist.antlr.FollowElement in project n4js by eclipse.

the class CustomN4JSParser method doGetFollowElements.

/**
 * Initialize the parser properly with the given tokens and process it.
 */
private Collection<FollowElement> doGetFollowElements(AbstractInternalContentAssistParser parser, ObservableXtextTokenStream tokens) {
    tokens.setInitialHiddenTokens(getInitialHiddenTokens());
    parser.setTokenStream(tokens);
    IUnorderedGroupHelper helper = getUnorderedGroupHelper().get();
    parser.setUnorderedGroupHelper(helper);
    helper.initializeWith(parser);
    tokens.setListener(parser);
    Collection<FollowElement> followElements = getFollowElements(parser);
    return followElements;
}
Also used : IUnorderedGroupHelper(org.eclipse.xtext.parser.antlr.IUnorderedGroupHelper) FollowElement(org.eclipse.xtext.ide.editor.contentassist.antlr.FollowElement)

Example 8 with FollowElement

use of org.eclipse.xtext.ide.editor.contentassist.antlr.FollowElement 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;
}
Also used : TokenSource(org.antlr.runtime.TokenSource) AbstractElement(org.eclipse.xtext.AbstractElement) ObservableXtextTokenStream(org.eclipse.xtext.ide.editor.contentassist.antlr.ObservableXtextTokenStream) ArrayList(java.util.ArrayList) Token(org.antlr.runtime.Token) CommonToken(org.antlr.runtime.CommonToken) AbstractInternalContentAssistParser(org.eclipse.xtext.ide.editor.contentassist.antlr.internal.AbstractInternalContentAssistParser) FollowElement(org.eclipse.xtext.ide.editor.contentassist.antlr.FollowElement) IUnorderedGroupHelper(org.eclipse.xtext.parser.antlr.IUnorderedGroupHelper) LookAheadTerminal(org.eclipse.xtext.ide.editor.contentassist.antlr.LookAheadTerminal) UnorderedGroup(org.eclipse.xtext.UnorderedGroup) BaseRecognizer(org.antlr.runtime.BaseRecognizer)

Example 9 with FollowElement

use of org.eclipse.xtext.ide.editor.contentassist.antlr.FollowElement in project n4js by eclipse.

the class CustomN4JSParser method getFollowElements.

/**
 * @param node
 *            the root node of the model to parse
 * @param startOffset
 *            the start offset to consider
 * @param endOffset
 *            the exclusive end offset
 * @param strict
 *            if true the parser will not use error recovery on the very last token of the input.
 * @return a collection of follow elements.
 */
public Collection<FollowElement> getFollowElements(INode node, int startOffset, int endOffset, boolean strict) {
    Set<FollowElement> result = Sets.newLinkedHashSet();
    TokenSource tokenSource = tokenSourceFactory.toTokenSource(node, startOffset, endOffset);
    CustomInternalN4JSParser parser = collectFollowElements(tokenSource, strict, result);
    adjustASIAndCollectFollowElements(parser, strict, result);
    /*
		 * Lists are easier to debug
		 */
    return Lists.newArrayList(result);
}
Also used : TokenSource(org.antlr.runtime.TokenSource) FollowElement(org.eclipse.xtext.ide.editor.contentassist.antlr.FollowElement)

Example 10 with FollowElement

use of org.eclipse.xtext.ide.editor.contentassist.antlr.FollowElement in project n4js by eclipse.

the class PatchedFollowElementComputer method computeFollowElements.

@Override
public void computeFollowElements(Collection<FollowElement> followElements, final IFollowElementAcceptor followElementAcceptor) {
    FollowElementCalculator calculator = feCalculatorProvider.get();
    calculator.setAcceptor(new IFollowElementAcceptor() {

        @Override
        public void accept(AbstractElement element) {
            ParserRule rule = GrammarUtil.containingParserRule(element);
            if (rule == null || !GrammarUtil.isDatatypeRule(rule))
                followElementAcceptor.accept(element);
        }
    });
    for (FollowElement element : followElements) {
        List<Integer> paramStack = element.getParamStack();
        if (!paramStack.isEmpty()) {
            calculator.setParameterConfig(paramStack.get(paramStack.size() - 1));
        } else {
            calculator.setParameterConfig(0);
        }
        computeFollowElements(calculator, element);
    }
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) FollowElementCalculator(org.eclipse.xtext.ide.editor.contentassist.antlr.FollowElementCalculator) AbstractElement(org.eclipse.xtext.AbstractElement) IFollowElementAcceptor(org.eclipse.xtext.ide.editor.contentassist.IFollowElementAcceptor) FollowElement(org.eclipse.xtext.ide.editor.contentassist.antlr.FollowElement)

Aggregations

FollowElement (org.eclipse.xtext.ide.editor.contentassist.antlr.FollowElement)13 AbstractElement (org.eclipse.xtext.AbstractElement)3 Bug303200TestLanguageParser (org.eclipse.xtext.ui.tests.editor.contentassist.ide.contentassist.antlr.Bug303200TestLanguageParser)3 Test (org.junit.Test)3 Token (org.antlr.runtime.Token)2 TokenSource (org.antlr.runtime.TokenSource)2 EObject (org.eclipse.emf.ecore.EObject)2 ParserRule (org.eclipse.xtext.ParserRule)2 UnorderedGroup (org.eclipse.xtext.UnorderedGroup)2 INode (org.eclipse.xtext.nodemodel.INode)2 IUnorderedGroupHelper (org.eclipse.xtext.parser.antlr.IUnorderedGroupHelper)2 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 ArrayList (java.util.ArrayList)1 ANTLRStringStream (org.antlr.runtime.ANTLRStringStream)1 BaseRecognizer (org.antlr.runtime.BaseRecognizer)1 CommonToken (org.antlr.runtime.CommonToken)1 InternalFlexer (org.eclipse.xtend.core.parser.InternalFlexer)1 Assignment (org.eclipse.xtext.Assignment)1 Group (org.eclipse.xtext.Group)1