List of usage examples for org.antlr.v4.runtime Token getLine
int getLine();
From source file:com.sample.JavaErrorStrategy.java
License:BSD License
/** * Conjure up a missing token during error recovery. * * The recognizer attempts to recover from single missing symbols. But, * actions might refer to that missing symbol. For example, x=ID {f($x);}. * The action clearly assumes that there has been an identifier matched * previously and that $x points at that token. If that token is missing, * but the next token in the stream is what we want we assume that this * token is missing and we keep going. Because we have to return some token * to replace the missing token, we have to conjure one up. This method * gives the user control over the tokens returned for missing tokens. * Mostly, you will want to create something special for identifier tokens. * For literals such as '{' and ',', the default action in the parser or * tree parser works. It simply creates a CommonToken of the appropriate * type. The text will be the token. If you change what tokens must be * created by the lexer, override this method to create the appropriate * tokens./* w w w .j a va 2 s .c om*/ */ @NotNull protected Token getMissingSymbol(@NotNull Parser recognizer) { Token currentSymbol = recognizer.getCurrentToken(); IntervalSet expecting = getExpectedTokens(recognizer); int expectedTokenType = expecting.getMinElement(); // get any element String tokenText; if (expectedTokenType == Token.EOF) tokenText = "<missing EOF>"; else tokenText = "<missing " + recognizer.getTokenNames()[expectedTokenType] + ">"; Token current = currentSymbol; Token lookback = recognizer.getInputStream().LT(-1); if (current.getType() == Token.EOF && lookback != null) { current = lookback; } return recognizer.getTokenFactory() .create(new Pair<TokenSource, CharStream>(current.getTokenSource(), current.getTokenSource().getInputStream()), expectedTokenType, tokenText, Token.DEFAULT_CHANNEL, -1, -1, current.getLine(), current.getCharPositionInLine()); }
From source file:com.shelloid.script.Compiler.java
void compileError(Token token, String msg) { String errorMsg = "Syntax Error at: " + token.getLine() + ": " + token.getCharPositionInLine() + " near " + token.getText() + ", cause: " + msg; errorMsgs.add(errorMsg);/*from w ww. j a va 2 s .co m*/ }
From source file:com.shelloid.script.TokenInfo.java
public TokenInfo(Token token) { line = token.getLine(); charPos = token.getCharPositionInLine(); text = token.getText(); }
From source file:com.spotify.heroic.grammar.CoreQueryParser.java
License:Apache License
private QueryListener parse(Function<HeroicQueryParser, ParserRuleContext> op, String input) { final HeroicQueryLexer lexer = new HeroicQueryLexer(new ANTLRInputStream(input)); final CommonTokenStream tokens = new CommonTokenStream(lexer); final HeroicQueryParser parser = new HeroicQueryParser(tokens); parser.removeErrorListeners();//from ww w.j a v a 2 s . c o m parser.setErrorHandler(new BailErrorStrategy()); final ParserRuleContext context; try { context = op.apply(parser); } catch (final ParseCancellationException e) { if (!(e.getCause() instanceof RecognitionException)) { throw e; } throw toParseException((RecognitionException) e.getCause()); } final QueryListener listener = new QueryListener(); ParseTreeWalker.DEFAULT.walk(listener, context); final Token last = lexer.getToken(); if (last.getType() != Token.EOF) { throw new ParseException(String.format("garbage at end of string: '%s'", last.getText()), null, last.getLine(), last.getCharPositionInLine()); } return listener; }
From source file:com.spotify.heroic.grammar.CoreQueryParser.java
License:Apache License
private ParseException toParseException(final RecognitionException e) { final Token token = e.getOffendingToken(); if (token.getType() == HeroicQueryLexer.UnterminatedQutoedString) { return new ParseException(String.format("unterminated string: %s", token.getText()), null, token.getLine(), token.getCharPositionInLine()); }// w w w .j a va 2s .co m return new ParseException("unexpected token: " + token.getText(), null, token.getLine(), token.getCharPositionInLine()); }
From source file:com.yahoo.yqlplus.language.parser.ProgramParser.java
private Location toLocation(Scope scope, ParseTree node) { Token start; if (node instanceof ParserRuleContext) { start = ((ParserRuleContext) node).start; } else if (node instanceof TerminalNode) { start = ((TerminalNode) node).getSymbol(); } else {//from ww w.j av a2s . co m throw new ProgramCompileException("Location is not available for type " + node.getClass()); } Location location = new Location(scope != null ? scope.programName : "<string>", start.getLine(), start.getCharPositionInLine()); return location; }
From source file:compile.compilersource.ErrorReporter.java
public void CreateErrorMessage(String message, Token startToken) { String errMessage = MessageFormat.format("Exception! line {0}, char {2}: {1}", startToken.getLine(), message, startToken.getCharPositionInLine()); errorList.add(errMessage);/*www . j a va2 s.c om*/ if (ui.getEditor() != null) { try { int offset = ui.getEditor().getLineStartOffset(startToken.getLine() - 1); int endOffset = ui.getEditor().getLineEndOffset(startToken.getLine() - 1); Highlighter highlighter = ui.getEditor().getHighlighter(); highlighter.addHighlight(offset, endOffset, new DefaultHighlighter.DefaultHighlightPainter(Color.RED)); } catch (BadLocationException ex) { System.out.println("Semantic highlight error: " + ex.getMessage()); } } else System.out.println("Semantic error: editor cannot be accessed"); }
From source file:de.adrodoc55.minecraft.mpl.interpretation.MplInterpreter.java
License:Open Source License
public MplSource toSource(@Nullable Token token) { String line = token != null ? lines.get(token.getLine() - 1) : null; return new MplSource(programFile, token, line); }
From source file:de.huberlin.cuneiform.language.BaseCuneiformParser.java
License:Apache License
protected void addDefTask(Token idToken) { String name;// www. j a va2 s . com if (idToken == null) throw new NullPointerException("Id token must not be null."); name = idToken.getText(); // check whether we have seen a declare statement yet if (declare == null) { reportError(ERROR_ORDER, idToken.getLine(), "Task definition for '" + name + "' must not appear prior to declare statement."); return; } // check whether this is the first time, this task is introduced if (defTaskMap.containsKey(name)) { reportError(ERROR_UNIQUENESS, idToken.getLine(), "Duplicate task definition. The task '" + name + "' has already been defined."); return; } defTaskMap.put(name, new DefTask(name)); }
From source file:de.huberlin.cuneiform.language.BaseCuneiformParser.java
License:Apache License
protected void addDefTaskParam(Token nameToken, DefTaskParam p) { String name;//w ww .j a v a 2 s . co m DefTask task; if (nameToken == null) throw new NullPointerException("Task name token must not be null."); if (p == null) throw new NullPointerException("Input parameter must not be null."); name = nameToken.getText(); task = defTaskMap.get(name); if (task == null) throw new NullPointerException("Referenced task does not exits."); for (ParamItem invar : p) if (task.containsExplicitParamWithName(invar.getValue())) if (invar.getValue().equals(Constant.TOKEN_TASK) && task.isTaskParamExplicit()) { reportError(BaseParser.ERROR_UNIQUENESS, nameToken.getLine(), "Duplicate definition of input variable name '" + invar + "'."); return; } // if this cluster contains the task parameter, it must not be of type reduce if (p instanceof ReduceParam) if (((ReduceParam) p).getValue().equals(Constant.TOKEN_TASK)) { reportError(ERROR_REFERENCE, nameToken.getLine(), "Special task parameter cannot be marked reduce."); return; } task.addParam(p); }