Example usage for org.antlr.v4.runtime Token getStartIndex

List of usage examples for org.antlr.v4.runtime Token getStartIndex

Introduction

In this page you can find the example usage for org.antlr.v4.runtime Token getStartIndex.

Prototype

int getStartIndex();

Source Link

Document

The starting character index of the token This method is optional; return -1 if not implemented.

Usage

From source file:org.tinygroup.template.parser.TinyTemplateErrorListener.java

License:GNU General Public License

public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine,
        String msg, RecognitionException e) {

    CommonTokenStream tokens = (CommonTokenStream) recognizer.getInputStream();
    String input = tokens.getTokenSource().getInputStream().toString();
    String[] sourceLines = input.split("\r?\n", -1);
    Token offendingToken = (Token) offendingSymbol;

    StringBuilder sb = new StringBuilder(128);
    sb.append("Template parse failed.\n");
    sb.append(recognizer.getInputStream().getSourceName());
    sb.append(':');
    sb.append(line);//from  w w  w  .j a va 2s. co  m
    sb.append(':');
    sb.append(charPositionInLine);
    sb.append("\nmessage: ");
    sb.append(msg);
    sb.append('\n');
    sb.append(MemorySourceCompiler.getPrettyError(sourceLines, line, charPositionInLine + 1,
            offendingToken.getStartIndex(), offendingToken.getStopIndex(), 5));

    if (e != null) {
        throw new SyntaxErrorException(sb.toString(), line, charPositionInLine, e);
    } else {
        throw new SyntaxErrorException(sb.toString(), line, charPositionInLine);
    }
}

From source file:org.tvl.goworks.editor.go.completion.GoCompletionProvider.java

License:Open Source License

@Override
public int getAutoQueryTypes(JTextComponent component, String typedText) {
    if (typedText == null || typedText.length() != 1) {
        return 0;
    }// w ww.  j  a v a2s . c  om

    boolean triggered = getCompletionAutoPopupTriggers().indexOf(typedText.charAt(0)) >= 0;
    if (triggered || (autoPopupOnIdentifierPart() && GoCompletionQuery.isIdentifierPart(typedText))) {
        int offset = component.getSelectionStart() - 1;
        Token contextToken = getContext(component, offset);
        if (contextToken == null) {
            return 0;
        }

        if (!triggered) {
            // the caret must be at the end of the identifier. note that the
            // offset is already 1 position before the caret, so no need to
            // add 1 to contextToken.getStopIndex().
            if (offset != contextToken.getStopIndex()) {
                return 0;
            }

            // only trigger for the first character of the identifier
            if (contextToken.getStopIndex() > contextToken.getStartIndex()) {
                return 0;
            }
        }

        boolean allowInStrings = false;
        if (isGoContext(contextToken, offset, allowInStrings)) {
            return COMPLETION_QUERY_TYPE | AUTO_QUERY_TYPE;
        }
    }

    return 0;
}

From source file:org.tvl.goworks.editor.go.completion.GoCompletionProvider.java

License:Open Source License

@Override
public Token getContext(Document document, int offset) {
    Parameters.notNull("document", document);

    if (document instanceof AbstractDocument) {
        ((AbstractDocument) document).readLock();
    }/*from   w w w. ja v a 2  s .c  om*/

    try {
        //            try {
        ParserTaskManager taskManager = Lookup.getDefault().lookup(ParserTaskManager.class);
        DocumentSnapshot snapshot = VersionedDocumentUtilities.getVersionedDocument(document)
                .getCurrentSnapshot();
        Future<ParserData<Tagger<TokenTag<Token>>>> futureTokensData = taskManager.getData(snapshot,
                GoParserDataDefinitions.LEXER_TOKENS, EnumSet.of(ParserDataOptions.SYNCHRONOUS));
        if (futureTokensData == null) {
            return null;
        }

        Tagger<TokenTag<Token>> tagger;
        try {
            tagger = futureTokensData.get().getData();
            if (tagger == null) {
                return null;
            }
        } catch (InterruptedException | ExecutionException ex) {
            LOGGER.log(Level.WARNING, "An exception occurred while getting tokens.", ex);
            return null;
        }

        // get the token(s) at the cursor position, with affinity both directions
        OffsetRegion region = OffsetRegion.fromBounds(Math.max(0, offset - 1),
                Math.min(snapshot.length(), offset + 1));
        Iterable<TaggedPositionRegion<TokenTag<Token>>> tags = tagger.getTags(
                new NormalizedSnapshotPositionRegionCollection(new SnapshotPositionRegion(snapshot, region)));

        // TODO: cache tokens
        //                ANTLRInputStream input = new ANTLRInputStream(document.getText(0, document.getLength()));
        //                GoLexer lexer = new GoLexer(input);
        //                CommonTokenStream tokenStream = new CommonTokenStream(lexer);
        Token token = null;
        //                for (token = tokenStream.LT(1); token != null && token.getType() != Token.EOF; token = tokenStream.LT(1)) {
        //                    tokenStream.consume();
        //                    if (token.getStartIndex() <= offset && token.getStopIndex() >= offset) {
        //                        break;
        //                    }
        //                }
        for (TaggedPositionRegion<TokenTag<Token>> taggedRegion : tags) {
            if (taggedRegion.getTag().getToken().getChannel() != Lexer.DEFAULT_TOKEN_CHANNEL) {
                continue;
            }

            token = taggedRegion.getTag().getToken();
            if (token.getStartIndex() <= offset && token.getStopIndex() >= offset) {
                break;
            }
        }

        if (token == null) {
            // try again without skipping off-channel tokens
            for (TaggedPositionRegion<TokenTag<Token>> taggedRegion : tags) {
                token = taggedRegion.getTag().getToken();
                if (token.getStartIndex() <= offset && token.getStopIndex() >= offset) {
                    break;
                }
            }
        }

        return token;
        //List<Token> tokens;
        //            } catch (BadLocationException ex) {
        //                Exceptions.printStackTrace(ex);
        //                return null;
        //            }
    } finally {
        if (document instanceof AbstractDocument) {
            ((AbstractDocument) document).readUnlock();
        }
    }
}

From source file:org.tvl.goworks.editor.go.highlighter.MarkOccurrencesHighlighter.java

License:Open Source License

@Override
protected void addHighlights(List<Tuple2<OffsetRegion, AttributeSet>> intermediateContainer,
        DocumentSnapshot sourceSnapshot, DocumentSnapshot currentSnapshot, Collection<Token> tokens,
        AttributeSet attributes) {
    for (Token token : tokens) {
        TrackingPositionRegion trackingRegion = sourceSnapshot.createTrackingRegion(
                OffsetRegion.fromBounds(token.getStartIndex(), token.getStopIndex() + 1),
                TrackingPositionRegion.Bias.Forward);
        SnapshotPositionRegion region = trackingRegion.getRegion(currentSnapshot);
        intermediateContainer.add(Tuple.create(region.getRegion(), attributes));
        markPositions.add(region.getStart());
    }/*from   w  ww.java2 s .com*/
}

From source file:org.tvl.goworks.editor.go.highlighter.MarkOccurrencesHighlighter.java

License:Open Source License

@CheckForNull
public static Token getContext(SnapshotPosition position) {
    ParserTaskManager taskManager = Lookup.getDefault().lookup(ParserTaskManager.class);
    DocumentSnapshot snapshot = position.getSnapshot();
    int offset = position.getOffset();
    Future<ParserData<Tagger<TokenTag<Token>>>> futureTokensData = taskManager.getData(snapshot,
            GoParserDataDefinitions.LEXER_TOKENS,
            EnumSet.of(ParserDataOptions.NO_UPDATE, ParserDataOptions.SYNCHRONOUS));
    if (futureTokensData == null) {
        return null;
    }//from   w ww . j  a v  a  2  s .  c  o m

    ParserData<Tagger<TokenTag<Token>>> tokensData;
    try {
        tokensData = futureTokensData.get();
        if (tokensData == null) {
            return null;
        }
    } catch (InterruptedException | ExecutionException ex) {
        LOGGER.log(Level.WARNING, "An exception occurred while getting token data.", ex);
        return null;
    }

    Tagger<TokenTag<Token>> tagger = tokensData.getData();
    if (tagger == null) {
        return null;
    }

    // get the token(s) at the cursor position, with affinity both directions
    OffsetRegion region = OffsetRegion.fromBounds(Math.max(0, offset - 1),
            Math.min(snapshot.length(), offset + 1));
    Iterable<TaggedPositionRegion<TokenTag<Token>>> tags = tagger.getTags(
            new NormalizedSnapshotPositionRegionCollection(new SnapshotPositionRegion(snapshot, region)));

    Token token = null;
    for (TaggedPositionRegion<TokenTag<Token>> taggedRegion : tags) {
        if (taggedRegion.getTag().getToken().getChannel() != Lexer.DEFAULT_TOKEN_CHANNEL) {
            continue;
        }

        Token previousToken = token;
        Token nextToken = taggedRegion.getTag().getToken();
        if (nextToken.getStartIndex() <= offset && nextToken.getStopIndex() + 1 >= offset) {
            if (previousToken != null && previousToken.getStopIndex() + 1 == offset) {
                // prefer the end of a word token to the beginning of a non-word token
                if (CompletionParserATNSimulator.WORDLIKE_TOKEN_TYPES.contains(previousToken.getType())) {
                    break;
                }
            }

            token = nextToken;
        }
    }

    if (token == null) {
        // try again without skipping off-channel tokens
        for (TaggedPositionRegion<TokenTag<Token>> taggedRegion : tags) {
            token = taggedRegion.getTag().getToken();
            if (token.getStartIndex() <= offset && token.getStopIndex() >= offset) {
                break;
            }
        }
    }

    return token;
}

From source file:org.tvl.goworks.editor.go.navigation.GoHyperlinkProvider.java

License:Open Source License

@Override
public int[] getHyperlinkSpan(Document doc, int offset, HyperlinkType type) {
    final Lookup lookup = MimeLookup.getLookup(MimePath.get(GoEditorKit.GO_MIME_TYPE));
    Collection<? extends CompletionProvider> providers = lookup.lookupAll(CompletionProvider.class);
    GoCompletionProvider provider = null;
    for (CompletionProvider current : providers) {
        if (!(current instanceof GoCompletionProvider)) {
            continue;
        }/*from  w  w w . j  a  v a  2s. c  om*/

        provider = (GoCompletionProvider) current;
    }

    if (provider == null) {
        return null;
    }

    Token token = provider.getContext(doc, offset);
    if (token == null) {
        return null;
    }

    switch (token.getType()) {
    case GoParser.IDENTIFIER:
        break;

    default:
        return null;
    }

    return new int[] { token.getStartIndex(), token.getStopIndex() + 1 };
}

From source file:swiprolog.visitor.Visitor4Internal.java

License:Open Source License

/**
 * Create {@link SourceInfoObject} for given context.
 *
 * @param ctx/*from  w  w w  .j a  va 2  s  .c o  m*/
 *            the {@link DirectiveContext} from the parsed object
 * @return {@link SourceInfoObject}
 */
private SourceInfo getSourceInfo(ParserRuleContext ctx) {
    Token start = (ctx == null) ? null : ctx.getStart();
    Token stop = (ctx == null) ? null : ctx.getStop();
    if (stop == null) {
        // happens if we are at EOF...
        stop = start;
    }
    return (start == null) ? null
            : new SourceInfoObject(this.source.getSource(), start.getLine(), start.getCharPositionInLine(),
                    this.source.getStartIndex() + start.getStartIndex() + 1,
                    this.source.getStartIndex() + stop.getStopIndex() + 1);
}

From source file:swiprolog.visitor.Visitor4Internal.java

License:Open Source License

private SourceInfo getSourceInfo(TerminalNode leaf) {
    Token symbol = (leaf == null) ? null : leaf.getSymbol();
    return (symbol == null) ? null
            : new SourceInfoObject(this.source.getSource(), symbol.getLine(), symbol.getCharPositionInLine(),
                    this.source.getStartIndex() + symbol.getStartIndex() + 1,
                    this.source.getStartIndex() + symbol.getStopIndex() + 1);
}

From source file:tuprolog.visitor.Visitor4Internal.java

License:Open Source License

/**
 * Create {@link SourceInfoObject} for given context.
 *
 * @param ctx//from  w  ww .j a  v a 2 s . co m
 *            the {@link DirectiveContext} from the parsed object
 * @return {@link SourceInfoObject}
 */
private SourceInfo getSourceInfo(ParserRuleContext ctx) {
    Token start = ctx.getStart();
    Token stop = ctx.getStop();
    if (stop == null) {
        // happens if we are at EOF...
        stop = start;
    }
    return new SourceInfoObject(this.source.getSource(), start.getLine(), start.getCharPositionInLine(),
            this.source.getStartIndex() + start.getStartIndex() + 1,
            this.source.getStartIndex() + stop.getStopIndex() + 1);
}

From source file:tuprolog.visitor.Visitor4Internal.java

License:Open Source License

private SourceInfo getSourceInfo(TerminalNode leaf) {
    Token symbol = leaf.getSymbol();
    return new SourceInfoObject(this.source.getSource(), symbol.getLine(), symbol.getCharPositionInLine(),
            this.source.getStartIndex() + symbol.getStartIndex() + 1,
            this.source.getStartIndex() + symbol.getStopIndex() + 1);
}