List of usage examples for org.antlr.v4.runtime Token getStopIndex
int getStopIndex();
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; }/*w w w .j a v a2 s. com*/ 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; }// w w w. j a v a2 s .co m 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 ww .ja v a 2s . co 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 . jav a2 s . c o 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); }
From source file:us.ihmc.idl.generator.IDLGenerator.java
License:Apache License
private static void printToken(CommonTokenStream tokens, int index, Token token) { if (token.getType() != IDLParser.WS) { String out = ""; out += " Index: " + token.getTokenIndex(); out += " Start: " + token.getStartIndex(); out += " Stop: " + token.getStopIndex(); out += " Channel: " + token.getChannel(); out += " Type: " + token.getType(); // out += " Hidden: "; // List<Token> hiddenTokensToLeft = tokens.getHiddenTokensToLeft(index); // for (int i = 0; hiddenTokensToLeft != null && i < hiddenTokensToLeft.size(); i++) // { // if (hiddenTokensToLeft.get(i).getType() != IDLParser.WS) // { // out += "\n\t" + i + ":"; // out += "\n\tChannel: " + hiddenTokensToLeft.get(i).getChannel() + " Type: " + hiddenTokensToLeft.get(i).getType(); // out += hiddenTokensToLeft.get(i).getText().replaceAll("\\s", ""); // } // } out += " " + token.getText().replaceAll("\\s", ""); System.out.println(out);// w ww .j av a 2s. c o m } }
From source file:x10dt.ui.contentProposer.X10ContentProposer.java
License:Open Source License
public ICompletionProposal[] getContentProposals(IParseController controller, int offset, ITextViewer viewer) { ArrayList<ICompletionProposal> list = new ArrayList<ICompletionProposal>(); CommonTokenStream tokens = ((ParseController) controller).getTokens(); Token tokenToComplete = null;//w w w . j av a 2 s . c o m Token previousToken = null; Token nextToken = null; int index = 0; for (Token t : tokens.getTokens()) { index++; if (t.getChannel() == Token.DEFAULT_CHANNEL) { if (t.getStartIndex() <= offset && t.getStopIndex() + 1 >= offset) { tokenToComplete = t; break; } if (t.getStartIndex() > offset) { break; } previousToken = t; } } if (tokenToComplete == null) { nextToken = tokens.getTokens().get(index); } String prefix = tokenToComplete == null ? "" : computePrefixOfToken(tokenToComplete, offset, (ParseController) controller); PolyglotNodeLocator locator = new PolyglotNodeLocator( controller.getProject()/*,((ParseController) controller).getLexStream()*/); Node currentAst = (Node) controller.getCurrentAst(); Node node = tokenToComplete != null ? (Node) locator.findNode(currentAst, tokenToComplete.getStartIndex(), tokenToComplete.getStopIndex()) : null; Node previousNode = (previousToken != null) ? (Node) locator.findNode(currentAst, previousToken.getStartIndex(), previousToken.getStopIndex()) : null; Node nextNode = (nextToken != null) ? (Node) locator.findNode(currentAst, nextToken.getStartIndex(), nextToken.getStopIndex()) : null; if (node != null && node instanceof Eval && tokenToComplete.getType() == X10Parser.DOT) { Type type = ((Eval_c) node).expr().type(); if (type != null && type.isReference()) { getCandidates((ObjectType) type, list, prefix, offset, true); } } else if (node != null && node instanceof Id && previousNode instanceof Field) { Type type = ((Field_c) previousNode).target().type(); if (type != null && type.isReference()) { getCandidates((ObjectType) type, list, prefix, offset, true); } } else if (node != null && node instanceof Id && previousNode instanceof Call) { Type type = ((Call_c) previousNode).target().type(); if (type != null && type.isReference()) { getCandidates((ObjectType) type, list, prefix, offset, true); } //The next case completes an Id with names in scope } else if (node != null && node instanceof Id) { Node n = (node instanceof Id) ? node : previousNode; String pref = (node instanceof Id) ? prefix : computePrefixOfToken(previousToken, offset, (ParseController) controller); addNamesInScope(currentAst, n, pref, list, offset, !EMPTY_PREFIX_MATCHES); } else if (node == null && previousNode != null) { //Display templates, names in scope -- index < 0 when we are at a white space or comment Node location = location(previousNode, nextNode, locator, currentAst); if (location instanceof Block && (justAfter(X10Parser.SEMICOLON, previousToken) || justAfter(X10Parser.RBRACE, previousToken) || justAfter(X10Parser.LBRACE, previousToken))) { //Statement context. addTemplateProposals(offset, viewer, list, prefix, fTemplates); //addNamesInScope(currentAst, node, prefix, list, offset, EMPTY_PREFIX_MATCHES); } else if (justAfter(X10Parser.EQUAL, previousToken) && (location instanceof Assign || location instanceof LocalDecl)) { Template[] templates = new Template[] { fAtExpressionTemplate, fCoercionTemplate, fRegion1DTemplate, fRegion2DTemplate }; addTemplateProposals(offset, viewer, list, prefix, templates); } else if (location instanceof ClassBody) { //Class context Template[] templates = new Template[] { fVariableDeclaration, fValueDeclaration, fConstDeclaration, fPropertyDeclaration, fMainMethod, fMethodTemplate, fConstructorTemplate, fClassTemplate, fStructTemplate, fDependentTypeDeclaration, }; addTemplateProposals(offset, viewer, list, prefix, templates); } } return (ICompletionProposal[]) list.toArray(new ICompletionProposal[list.size()]); }
From source file:x10dt.ui.contentProposer.X10ContentProposer.java
License:Open Source License
private String computePrefixOfToken(Token tokenToComplete, int offset, ParseController pc) { String prefix = ""; if (tokenToComplete.getType() == X10Parser.IDENTIFIER || /*tokenToComplete.getType() == X10Parser.ErrorId ||*/ pc.isKeyword(tokenToComplete.getType())) { if (offset >= tokenToComplete.getStartIndex() && offset <= tokenToComplete.getStopIndex() + 1) { prefix = tokenToComplete.getText().toString().substring(0, offset - tokenToComplete.getStartIndex()); }/* www. j ava 2 s. co m*/ } return prefix; }
From source file:x10dt.ui.parser.ParseController.java
License:Open Source License
private IToken getIToken(final Token token) { IToken ret = new IToken() { public int getAdjunctIndex() { // TODO Auto-generated method stub return 0; }//from w w w .ja va2 s .c om public int getColumn() { return token.getCharPositionInLine(); } public int getEndColumn() { return token.getCharPositionInLine() + token.getStopIndex() - token.getStartIndex(); } public int getEndLine() { return token.getLine(); } public int getEndOffset() { return token.getStopIndex(); } public IToken[] getFollowingAdjuncts() { // TODO Auto-generated method stub return null; } public ILexStream getILexStream() { // TODO Auto-generated method stub return null; } public IPrsStream getIPrsStream() { // TODO Auto-generated method stub return null; } public int getKind() { return token.getType(); } public ILexStream getLexStream() { // TODO Auto-generated method stub return null; } public int getLine() { return token.getLine(); } public IToken[] getPrecedingAdjuncts() { // TODO Auto-generated method stub return null; } public IPrsStream getPrsStream() { // TODO Auto-generated method stub return null; } public int getStartOffset() { return token.getStartIndex(); } public int getTokenIndex() { return token.getTokenIndex(); } public String getValue(char[] arg0) { return token.getText(); } public void setAdjunctIndex(int arg0) { // TODO Auto-generated method stub } public void setEndOffset(int arg0) { } public void setKind(int arg0) { // TODO Auto-generated method stub } public void setStartOffset(int arg0) { // TODO Auto-generated method stub } public void setTokenIndex(int arg0) { // TODO Auto-generated method stub } }; return ret; }