List of usage examples for org.antlr.v4.runtime Token getStartIndex
int getStartIndex();
From source file:net.openchrom.xxd.processor.supplier.rscripting.ui.editor.RBaseListen.java
License:Open Source License
public void enterE25(@NotNull RParser.E25Context 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("REPEAT_LOOP_FOLDING")) { startStop.add(lineStart + "," + lineEnd); }// www .j a v a2s . c o m }
From source file:net.openchrom.xxd.processor.supplier.rscripting.ui.editor.RBaseListen.java
License:Open Source License
@Override public void enterE17VariableDeclaration(@NotNull RParser.E17VariableDeclarationContext ctx) { Interval sourceInterval = ctx.getSourceInterval(); int start = sourceInterval.a; Token assign = tokens.get(start + 2); String subExpr = assign.getText(); if (subExpr.equals("function") == false) { Token firstToken = tokens.get(start); int lineStart = firstToken.getStartIndex(); int line = calculateLine(lineStart); if (ctx.getParent().getChild(1) != null) { String op = tokens.get(start + 1).getText(); if (op.equals("<-") || op.equals("<<-") || op.equals("=")) { String name = tokens.get(start).getText(); if (methods.size() == 0) { if (checkVarName(name)) { RScope scope = scopes.peek(); scope.add(name); new REditorOutlineNode(name, line, "variable", editor.baseNode); }//from ww w .j a v a 2s. co m } else { if (checkVarName(name)) { RScope scope = scopes.peek(); scope.add(name); new REditorOutlineNode(name, line, "variable", methods.peek()); } } } else if (op.equals("->") || op.equals("->>")) { String name = tokens.get(start + 2).getText(); if (methods.size() == 0) { if (checkVarName(name)) { RScope scope = scopes.peek(); scope.add(name); new REditorOutlineNode(name, line, "variable", editor.baseNode); } } else { if (checkVarName(name)) { RScope scope = scopes.peek(); scope.add(name); new REditorOutlineNode(name, line, "variable", methods.peek()); } } } } } }
From source file:net.openchrom.xxd.processor.supplier.rscripting.ui.editor.RBaseListen.java
License:Open Source License
@Override public void enterE20CallFunction(@NotNull RParser.E20CallFunctionContext ctx) { /*/*from w w w. j av a2 s. c o m*/ * Interval sourceInterval = ctx.getSourceInterval(); int start = * sourceInterval.a; Token assign = tokens.get(start); */ Token start = ctx.start; String subExpr = start.getText(); /* Detect libraries and add them to the outline! */ if (subExpr.equals("library") || subExpr.equals("require")) { Token firstToken = start; int lineStart = firstToken.getStartIndex(); int line = calculateLine(lineStart); if (ctx.getParent().getChild(1) != null) { String name = ctx.getChild(2).getText(); // The third token should be a parenthesis! String parenthesis = ctx.getChild(3).getText(); if (parenthesis.equals(")")) { if (methods.size() == 0) { new REditorOutlineNode(name, line, "library", editor.baseNode); } else { new REditorOutlineNode(name, line, "library", methods.peek()); } } } } }
From source file:nl.lxtreme.libtdl.grammar.adv.AdvTdlSemanticAnalyzer.java
License:Apache License
private void validateDeclaredTerm(Token term) { String name = normalizeName(term.getText()); if (!m_declarations.containsKey(name)) { int offset = term.getStartIndex(); int length = term.getStopIndex() - offset; String msg = name + " is not declared"; Marker marker = new MarkerBuilder().setCategory(Category.SEMANTIC).setType(Type.ERROR) // .setLocation(offset, length, term.getLine(), term.getCharPositionInLine()) // .setDescription(msg).build(); m_problemReporter.report(marker); }//from w w w. j a v a 2 s . c o m }
From source file:no.ssb.vtl.script.support.SyntaxErrorListener.java
License:Apache License
@Override public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int startLine, int startColumn, String msg, RecognitionException e) { VTLScriptException vtlScriptException; // Use the context from the RecognitionException if available. if (e != null && e.getCtx() != null) { vtlScriptException = new VTLScriptException(msg, (ParserRuleContext) e.getCtx()); } else {/*from ww w. j av a 2 s .c om*/ int stopColumn = startColumn; if (offendingSymbol instanceof Token) { Token symbol = (Token) offendingSymbol; int start = symbol.getStartIndex(); int stop = symbol.getStopIndex(); if (start >= 0 && stop >= 0) { stopColumn = startColumn + (stop - start) + 1; } vtlScriptException = new VTLScriptException(msg, startLine, startColumn, startLine, stopColumn); } else { vtlScriptException = new VTLScriptException(msg, startLine, startColumn); } } errorConsumer.accept(vtlScriptException); }
From source file:no.ssb.vtl.test.junit.GrammarRule.java
License:Apache License
/** * Parse an expression starting from the given <b>ANTLR rule</b> * <p>// w ww .ja va 2 s . c o m * In order to get the Rule, use the {@link #withRule(String)} method. * * @param expression the expression to parse. * @param rule the rule to start from. * @param diagnostic {@link DiagnosticErrorListener} will be used if true. * @return the resulting parse tree. * @throws Exception if the expression failed to parse. */ public ParserRuleContext parse(String expression, Rule rule, boolean diagnostic) throws Exception { Multimap<Integer, String> messages = LinkedListMultimap.create(); LexerInterpreter lexerInterpreter = grammar.createLexerInterpreter(new ANTLRInputStream(expression)); GrammarParserInterpreter parserInterpreter = grammar .createGrammarParserInterpreter(new CommonTokenStream(lexerInterpreter)); BaseErrorListener errorListener; if (diagnostic) { errorListener = new DiagnosticErrorListener(); } else { errorListener = new ConsoleErrorListener(); } BaseErrorListener ruleErrorReporter = new BaseErrorListener() { @Override public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, org.antlr.v4.runtime.RecognitionException e) { int startLine = line, stopLine = line; int startColumn = charPositionInLine, stopColumn = charPositionInLine; if (offendingSymbol instanceof Token) { Token symbol = (Token) offendingSymbol; int start = symbol.getStartIndex(); int stop = symbol.getStopIndex(); if (start >= 0 && stop >= 0) { stopColumn = startColumn + (stop - start) + 1; } } messages.put(stopLine, String.format("at [%4s:%6s]:\t%s (%s)\n", String.format("%d,%d", startLine, stopLine), String.format("%d,%d", startColumn, stopColumn), msg, Optional.ofNullable(e).map(ex -> ex.getClass().getSimpleName()).orElse("null"))); } }; parserInterpreter.setErrorHandler(new GrammarParserInterpreter.BailButConsumeErrorStrategy()); lexerInterpreter.removeErrorListeners(); parserInterpreter.removeErrorListeners(); lexerInterpreter.addErrorListener(errorListener); parserInterpreter.addErrorListener(errorListener); lexerInterpreter.addErrorListener(ruleErrorReporter); parserInterpreter.addErrorListener(ruleErrorReporter); ParserRuleContext parse = parserInterpreter.parse(rule.index); if (!messages.isEmpty()) { StringBuilder expressionWithErrors = new StringBuilder(); LineNumberReader expressionReader = new LineNumberReader(new StringReader(expression)); String line; while ((line = expressionReader.readLine()) != null) { int lineNumber = expressionReader.getLineNumber(); expressionWithErrors.append(String.format("\t%d:%s%n", lineNumber, line)); if (messages.containsKey(lineNumber)) { expressionWithErrors.append(String.format("%n")); for (String message : messages.get(lineNumber)) { expressionWithErrors.append(message); } } } throw new Exception( String.format("errors parsing expression:%n%n%s%n", expressionWithErrors.toString())); } return parse; }
From source file:org.apache.hive.hplsql.Exec.java
License:Apache License
String getText(ParserRuleContext ctx, Token start, Token stop) { return ctx.start.getInputStream() .getText(new org.antlr.v4.runtime.misc.Interval(start.getStartIndex(), stop.getStopIndex())); }
From source file:org.apache.hive.hplsql.Exec.java
License:Apache License
/** * Append the text preserving the formatting (space symbols) between tokens *///from ww w.j a v a 2 s . com void append(StringBuilder str, String appendStr, Token start, Token stop) { String spaces = start.getInputStream() .getText(new org.antlr.v4.runtime.misc.Interval(start.getStartIndex(), stop.getStopIndex())); spaces = spaces.substring(start.getText().length(), spaces.length() - stop.getText().length()); str.append(spaces); str.append(appendStr); }
From source file:org.apache.lucene.expressions.js.JavascriptParserErrorStrategy.java
License:Apache License
/** * Ensures the ANTLR parser will throw an exception after the first error * * @param recognizer the parser being used * @param re the original exception from the parser *//*from w w w . java 2s . c om*/ @Override public void recover(Parser recognizer, RecognitionException re) { Token token = re.getOffendingToken(); String message; if (token == null) { message = "error " + getTokenErrorDisplay(token); } else if (re instanceof InputMismatchException) { message = "unexpected token " + getTokenErrorDisplay(token) + " on line (" + token.getLine() + ") position (" + token.getCharPositionInLine() + ")" + " was expecting one of " + re.getExpectedTokens().toString(recognizer.getVocabulary()); } else if (re instanceof NoViableAltException) { if (token.getType() == JavascriptParser.EOF) { message = "unexpected end of expression"; } else { message = "invalid sequence of tokens near " + getTokenErrorDisplay(token) + " on line (" + token.getLine() + ") position (" + token.getCharPositionInLine() + ")"; } } else { message = " unexpected token near " + getTokenErrorDisplay(token) + " on line (" + token.getLine() + ") position (" + token.getCharPositionInLine() + ")"; } ParseException parseException = new ParseException(message, token.getStartIndex()); parseException.initCause(re); throw new RuntimeException(parseException); }
From source file:org.apache.lucene.expressions.js.JavascriptParserErrorStrategy.java
License:Apache License
/** * Ensures the ANTLR parser will throw an exception after the first error * * @param recognizer the parser being used * @return no actual return value/*from www. jav a 2 s .c o m*/ * @throws RecognitionException not used as a ParseException wrapped in a RuntimeException is thrown instead */ @Override public Token recoverInline(Parser recognizer) throws RecognitionException { Token token = recognizer.getCurrentToken(); String message = "unexpected token " + getTokenErrorDisplay(token) + " on line (" + token.getLine() + ") position (" + token.getCharPositionInLine() + ")" + " was expecting one of " + recognizer.getExpectedTokens().toString(recognizer.getVocabulary()); ParseException parseException = new ParseException(message, token.getStartIndex()); throw new RuntimeException(parseException); }