List of usage examples for org.antlr.v4.runtime Token getStopIndex
int getStopIndex();
From source file:groovy.ui.text.SmartDocumentFilter.java
License:Apache License
private void parseDocument() throws BadLocationException { GroovyLangLexer lexer;//from w w w. j ava 2s . c o m try { lexer = createLexer(styledDocument.getText(0, styledDocument.getLength())); } catch (IOException e) { e.printStackTrace(); return; } CommonTokenStream tokenStream = new CommonTokenStream(lexer); try { tokenStream.fill(); } catch (LexerNoViableAltException | GroovySyntaxError e) { // ignore return; } catch (Exception e) { e.printStackTrace(); return; } List<Token> tokenList = tokenStream.getTokens(); List<Token> tokenListToRender = findTokensToRender(tokenList); for (Token token : tokenListToRender) { int tokenType = token.getType(); // if (token instanceof CommonToken) { // System.out.println(((CommonToken) token).toString(lexer)); // } if (EOF == tokenType) { continue; } int tokenStartIndex = token.getStartIndex(); int tokenStopIndex = token.getStopIndex(); int tokenLength = tokenStopIndex - tokenStartIndex + 1; styledDocument.setCharacterAttributes(tokenStartIndex, tokenLength, findStyleByTokenType(tokenType), true); if (GStringBegin == tokenType || GStringPart == tokenType) { styledDocument.setCharacterAttributes(tokenStartIndex + tokenLength - 1, 1, defaultStyle, true); } } this.latestTokenList = tokenList; }
From source file:groovy.ui.text.SmartDocumentFilter.java
License:Apache License
private List<Token> findTokensToRender(List<Token> tokenList) { int tokenListSize = tokenList.size(); int latestTokenListSize = latestTokenList.size(); if (0 == tokenListSize || 0 == latestTokenListSize) { return tokenList; }/*from w w w. jav a 2s.co m*/ int startTokenIndex = 0; int minSize = Math.min(tokenListSize, latestTokenListSize); for (int i = 0; i < minSize; i++) { Token token = tokenList.get(i); Token latestToken = latestTokenList.get(i); if (token.getType() == latestToken.getType() && token.getStartIndex() == latestToken.getStartIndex() && token.getStopIndex() == latestToken.getStopIndex()) { continue; } startTokenIndex = i; break; } List<Token> newTokenList = new ArrayList<>(tokenList); List<Token> newLatestTokenList = new ArrayList<>(latestTokenList); Collections.reverse(newTokenList); Collections.reverse(newLatestTokenList); int stopTokenIndex = tokenListSize; Token lastToken = newTokenList.get(0); Token lastLatestToken = newLatestTokenList.get(0); for (int i = 0; i < minSize; i++) { Token token = newTokenList.get(i); Token latestToken = newLatestTokenList.get(i); if ((token.getType() == latestToken.getType()) && (token.getStartIndex() - lastToken.getStartIndex()) == (latestToken.getStartIndex() - lastLatestToken.getStartIndex()) && ((token.getStopIndex() - lastToken.getStopIndex()) == (latestToken.getStopIndex() - lastLatestToken.getStopIndex()))) { continue; } stopTokenIndex = tokenListSize - i; break; } if (startTokenIndex <= stopTokenIndex) { return tokenList.subList(startTokenIndex, stopTokenIndex); } return Collections.emptyList(); }
From source file:jetbrick.template.parser.JetTemplateErrorListener.java
License:Open Source License
@Override 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. ja va2 s .com sb.append("\nmessage: "); sb.append(msg); sb.append('\n'); sb.append(StringUtils.getPrettyError(sourceLines, line, charPositionInLine + 1, offendingToken.getStartIndex(), offendingToken.getStopIndex(), 5)); if (e != null) { throw new SyntaxErrorException(sb.toString(), e); } else { throw new SyntaxErrorException(sb.toString()); } }
From source file:kalang.ide.completion.KalangCompletionHandler.java
private List<CompletionProposal> getCompleteType(KaParser.KaParserResult result, int caret) { CompilationUnit cunit = result.getCompilationUnit(); CommonTokenStream ts = cunit.getTokenStream(); TokenNavigator tokenNav = new TokenNavigator(ts.getTokens().toArray(new Token[0])); tokenNav.move(caret - 1);// ww w . j a v a2 s .c o m int currentTokenId = tokenNav.getCurrentToken().getTokenIndex(); if (currentTokenId < 1) { return null; } //TODO skip comment channels Token curToken = ts.get(currentTokenId); log("cur token:" + curToken.getText()); Token prevToken = ts.get(currentTokenId - 1); log("prev token:" + prevToken.getText()); int exprStopCaret; int anchorCaret; if (curToken.getText().equals(".")) { exprStopCaret = prevToken.getStopIndex(); anchorCaret = curToken.getStopIndex() + 1; } else if (prevToken.getText().equals(".")) { if (currentTokenId < 2) { return null; } Token prevPrevToken = ts.get(currentTokenId - 2); exprStopCaret = prevPrevToken.getStopIndex(); anchorCaret = prevToken.getStopIndex() + 1; } else { return null; } AstNode astNode = AstNodeHelper.getAstNodeByCaretOffset(result, exprStopCaret); log("expr ast:" + astNode); if (astNode == null) { return null; } Type type; boolean inStatic; if (astNode instanceof ExprNode) { type = ((ExprNode) astNode).getType(); inStatic = false; } else if (astNode instanceof ClassReference) { type = Types.getClassType(((ClassReference) astNode).getReferencedClassNode()); inStatic = true; } else { return null; } CompletionRequest request = new CompletionRequest(); request.anchorOffset = anchorCaret; request.compiler = result.getCompiler(); String source = result.getSnapshot().getText().toString(); request.prefix = source.substring(anchorCaret, caret); log("prefix:" + request.prefix); return TypeCompletion.complete(request, type, inStatic); }
From source file:kr.simula.formula.ide.ast.SyntaxErrorAdapter.java
License:Apache License
protected static IProblem makeProblem(String fileName, String msg, RecognitionException e) { Token offendingToken = e.getOffendingToken(); IProblem problem = new DefaultProblem(fileName, msg, IProblem.Internal, new String[0], ProblemSeverities.Fatal, offendingToken.getLine(), offendingToken.getCharPositionInLine(), offendingToken.getStartIndex(), offendingToken.getStopIndex()); return problem; }
From source file:languageTools.parser.InputStreamPosition.java
License:Open Source License
/** * TODO//from w ww .ja v a 2 s . c o m * * @param start * @param stop * @param sourceFile */ public InputStreamPosition(Token start, Token stop, File sourceFile) { this(start.getLine(), start.getCharPositionInLine(), start.getStartIndex(), stop.getStopIndex(), sourceFile); }
From source file:mbtarranger.editors.GenericEditor.java
License:Open Source License
private static void highlight(IEditorPart editorPart, org.antlr.v4.runtime.Token startCtx, org.antlr.v4.runtime.Token stopCtx) { if (!(editorPart instanceof ITextEditor)) { return;// w w w. j av a 2s .com } ITextEditor editor = (ITextEditor) editorPart; IDocument document = editor.getDocumentProvider().getDocument(editor.getEditorInput()); if (document != null) { int start = startCtx.getStartIndex(); int stop = stopCtx.getStopIndex() - startCtx.getStartIndex(); editor.setHighlightRange(start, stop + 1, true); editor.selectAndReveal(start, stop + 1); } }
From source file:net.openchrom.xxd.processor.supplier.rscripting.ui.editor.RBaseListen.java
License:Open Source License
/** * {@inheritDoc}// w w w.j a v a 2s. co m * <p/> * The default implementation does nothing. */ @Override public void enterE19DefFunction(@NotNull RParser.E19DefFunctionContext ctx) { /* * Insert function as current scope with a parent current scope * (scope.peek)! */ scopes.push(new RScope(scopes.peek())); Interval sourceInterval = ctx.getSourceInterval(); Token firstToken = tokens.get(sourceInterval.a); // System.out.println(ctx.getParent().getChild(0).getText()); int lineStart = firstToken.getStartIndex(); // String ct=ctx.getText(); // System.out.println("function start at line:"+lineStart); Token lastToken = tokens.get(sourceInterval.b); int lineEnd = lastToken.getStopIndex() + 1 - lineStart; // String ct2=ctx.getText(); // Add to the editor folding action if enabled in the preferences! if (store.getBoolean("FUNCTIONS_FOLDING")) { startStop.add(lineStart + "," + lineEnd); } int lineMethod = calculateLine(lineStart); int childs = ctx.getParent().getChildCount(); int posTree = 0; for (int i = 0; i < childs; i++) { if (ctx.getText().equals(ctx.getParent().getChild(i).getText())) { posTree = i; } } if (ctx.getParent().getChild(posTree - 1) != null && ctx.getParent().getChild(posTree - 2) != null) { String op = ctx.getParent().getChild(posTree - 1).getText(); String name = ctx.getParent().getChild(posTree - 2).getText(); if (op.equals("<-") || op.equals("<<-")) { if (methods.size() == 0) { methods.push(new REditorOutlineNode(name, lineMethod, "function", editor.baseNode)); } else { methods.push(new REditorOutlineNode(name, lineMethod, "function", methods.peek())); } } } else if (posTree == 0) { if (methods.size() == 0) { methods.push(new REditorOutlineNode(ctx.getText(), lineMethod, "function", editor.baseNode)); } else { methods.push(new REditorOutlineNode(ctx.getText(), lineMethod, "function", methods.peek())); } } }
From source file:net.openchrom.xxd.processor.supplier.rscripting.ui.editor.RBaseListen.java
License:Open Source License
public void enterE21(@NotNull RParser.E21Context ctx) { Interval sourceInterval = ctx.getSourceInterval(); Token firstToken = tokens.get(sourceInterval.a); int lineStart = firstToken.getStartIndex(); Token lastToken = tokens.get(sourceInterval.b); int lineEnd = lastToken.getStopIndex() + 1 - lineStart; // Add to the editor folding action if enabled in the preferences! if (store.getBoolean("IF_CONDITION_FOLDING")) { startStop.add(lineStart + "," + lineEnd); }/* w ww . j a v a 2 s. c om*/ }
From source file:net.openchrom.xxd.processor.supplier.rscripting.ui.editor.RBaseListen.java
License:Open Source License
public void enterE22(@NotNull RParser.E22Context ctx) { Interval sourceInterval = ctx.getSourceInterval(); Token firstToken = tokens.get(sourceInterval.a); int lineStart = firstToken.getStartIndex(); Token lastToken = tokens.get(sourceInterval.b); int lineEnd = lastToken.getStopIndex() + 1 - lineStart; // Add to the editor folding action if enabled in the preferences! if (store.getBoolean("IF_CONDITION_FOLDING")) { startStop.add(lineStart + "," + lineEnd); }// w ww . j a v a2 s . c o m }