Search in sources :

Example 1 with TokenSource

use of org.antlr.runtime.TokenSource in project xtext-eclipse by eclipse.

the class BacktrackingLexerDocumentTokenSource method getRepairEntryData.

/**
 * @since 2.4
 */
@Override
protected RepairEntryData getRepairEntryData(DocumentEvent e) throws Exception {
    int tokenStartsAt = 0;
    int tokenInfoIdx = 0;
    TokenSource source = createTokenSource(e.fDocument.get());
    CommonToken token = (CommonToken) source.nextToken();
    // find start idx
    while (true) {
        if (token == Token.EOF_TOKEN) {
            break;
        }
        if (tokenInfoIdx >= getInternalModifyableTokenInfos().size())
            break;
        TokenInfo tokenInfo = getInternalModifyableTokenInfos().get(tokenInfoIdx);
        if (tokenInfo.getAntlrTokenType() != token.getType() || token.getStopIndex() - token.getStartIndex() + 1 != tokenInfo.getLength())
            break;
        if (tokenStartsAt + tokenInfo.getLength() > e.fOffset)
            break;
        tokenStartsAt += tokenInfo.getLength();
        tokenInfoIdx++;
        token = (CommonToken) source.nextToken();
    }
    return new RepairEntryData(tokenStartsAt, tokenInfoIdx, token, source);
}
Also used : TokenSource(org.antlr.runtime.TokenSource) CommonToken(org.antlr.runtime.CommonToken)

Example 2 with TokenSource

use of org.antlr.runtime.TokenSource 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 TokenSource

use of org.antlr.runtime.TokenSource 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 4 with TokenSource

use of org.antlr.runtime.TokenSource in project xtext-core by eclipse.

the class AbstractAntlrParser method doParse.

protected IParseResult doParse(String ruleName, CharStream in, NodeModelBuilder nodeModelBuilder, int initialLookAhead) {
    TokenSource tokenSource = createLexer(in);
    XtextTokenStream tokenStream = createTokenStream(tokenSource);
    tokenStream.initCurrentLookAhead(initialLookAhead);
    setInitialHiddenTokens(tokenStream);
    AbstractInternalAntlrParser parser = createParser(tokenStream);
    parser.setTokenTypeMap(getTokenDefProvider().getTokenDefMap());
    parser.setSyntaxErrorProvider(getSyntaxErrorProvider());
    parser.setNodeModelBuilder(nodeModelBuilder);
    parser.setSemanticModelBuilder(getElementFactory());
    IUnorderedGroupHelper helper = getUnorderedGroupHelper().get();
    parser.setUnorderedGroupHelper(helper);
    helper.initializeWith(parser);
    try {
        if (ruleName != null)
            return parser.parse(ruleName);
        return parser.parse();
    } catch (Exception re) {
        throw new ParseException(re.getMessage(), re);
    }
}
Also used : TokenSource(org.antlr.runtime.TokenSource) ParseException(org.eclipse.xtext.parser.ParseException) IOException(java.io.IOException) WrappedException(org.eclipse.emf.common.util.WrappedException) ParseException(org.eclipse.xtext.parser.ParseException)

Example 5 with TokenSource

use of org.antlr.runtime.TokenSource in project xtext-core by eclipse.

the class AbstractLexerBasedConverter method assertTokens.

/**
 * @since 2.7
 */
protected void assertTokens(T value, String result) {
    TokenSource tokenSource = getTokenSource(result);
    assertTokens(value, tokenSource, result);
}
Also used : TokenSource(org.antlr.runtime.TokenSource)

Aggregations

TokenSource (org.antlr.runtime.TokenSource)15 CommonToken (org.antlr.runtime.CommonToken)4 CommonTokenStream (org.antlr.runtime.CommonTokenStream)4 IUnorderedGroupHelper (org.eclipse.xtext.parser.antlr.IUnorderedGroupHelper)4 Token (org.antlr.runtime.Token)3 ParseException (org.eclipse.xtext.parser.ParseException)3 IOException (java.io.IOException)2 Method (java.lang.reflect.Method)2 ANTLRStringStream (org.antlr.runtime.ANTLRStringStream)2 Parser (org.antlr.runtime.Parser)2 CommonTree (org.antlr.runtime.tree.CommonTree)2 CommonTreeNodeStream (org.antlr.runtime.tree.CommonTreeNodeStream)2 WrappedException (org.eclipse.emf.common.util.WrappedException)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 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1