Search in sources :

Example 1 with InfiniteRecursion

use of org.eclipse.xtext.ide.editor.contentassist.antlr.internal.InfiniteRecursion 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 2 with InfiniteRecursion

use of org.eclipse.xtext.ide.editor.contentassist.antlr.internal.InfiniteRecursion 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 3 with InfiniteRecursion

use of org.eclipse.xtext.ide.editor.contentassist.antlr.internal.InfiniteRecursion in project xtext-core by eclipse.

the class BaseContentAssistParser method getFollowElements.

/**
 * @since 2.14
 */
protected Set<FE> getFollowElements(final InternalParser parser, String ruleName, boolean swallowInfiniteRecursion) throws Exception {
    Method method = parser.getClass().getMethod(ruleName);
    method.setAccessible(true);
    try {
        method.invoke(parser);
    } catch (InvocationTargetException targetException) {
        Throwable cause = targetException.getCause();
        if (cause instanceof InfiniteRecursion) {
            if (swallowInfiniteRecursion) {
                return parser.getFollowElements();
            } else {
                throw (InfiniteRecursion) cause;
            }
        }
        throw targetException;
    }
    return parser.getFollowElements();
}
Also used : InfiniteRecursion(org.eclipse.xtext.ide.editor.contentassist.antlr.internal.InfiniteRecursion) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 4 with InfiniteRecursion

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

the class CustomN4JSParser method collectFollowElements.

/**
 * First pass. Use the tokens that have been computed from the production parser's result and collect the follow
 * elements from those.
 */
private CustomInternalN4JSParser collectFollowElements(TokenSource tokens, boolean strict, Set<FollowElement> result) {
    CustomInternalN4JSParser parser = createParser();
    parser.setStrict(strict);
    try {
        ObservableXtextTokenStream tokenStream = new ObservableXtextTokenStream(tokens, parser);
        result.addAll(doGetFollowElements(parser, tokenStream));
    } catch (InfiniteRecursion infinite) {
        // this guards against erroneous infinite recovery loops in Antlr.
        // Grammar dependent and not expected something that is expected for N4JS
        // is used in the base class thus also used here.
        result.addAll(parser.getFollowElements());
    }
    return parser;
}
Also used : ObservableXtextTokenStream(org.eclipse.xtext.ide.editor.contentassist.antlr.ObservableXtextTokenStream) InfiniteRecursion(org.eclipse.xtext.ide.editor.contentassist.antlr.internal.InfiniteRecursion)

Aggregations

InfiniteRecursion (org.eclipse.xtext.ide.editor.contentassist.antlr.internal.InfiniteRecursion)4 TokenSource (org.antlr.runtime.TokenSource)2 IUnorderedGroupHelper (org.eclipse.xtext.parser.antlr.IUnorderedGroupHelper)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 ObservableXtextTokenStream (org.eclipse.xtext.ide.editor.contentassist.antlr.ObservableXtextTokenStream)1 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)1