List of usage examples for org.antlr.v4.runtime Token getTokenSource
TokenSource getTokenSource();
From source file:annis.ql.parser.ListTokenSource.java
License:Apache License
public ListTokenSource(List<Token> token) { this.token = token; Preconditions.checkNotNull(token);//from w w w. j a va 2 s. c o m Preconditions.checkArgument(!token.isEmpty(), "Internal token list must not be empty"); for (Token t : token) { if (t.getTokenSource() != null) { this.factory = t.getTokenSource().getTokenFactory(); break; } } Preconditions.checkNotNull(this.factory, "Internal token list needs a valid TokenSource"); Token lastToken = token.get(token.size() - 1); eofToken.setLine(lastToken.getLine()); eofToken.setCharPositionInLine(lastToken.getCharPositionInLine()); }
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 v a 2 s. com*/ */ @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:edu.clemson.cs.r2jt.absynnew.UnderliningErrorListener.java
License:Open Source License
/** * <p>This is called mainly when an {@link SrcErrorException} is raised * or caught.</p>/*from w w w . jav a 2s . com*/ * * @param offendingSymbol The token indicating a problem site. * @param msg The error message. */ public void semanticError(Token offendingSymbol, String msg) { if (offendingSymbol == null) { System.err.println("-1:-1:-1: " + msg); } else if (offendingSymbol.getTokenSource() == null) { System.err.println("-1:-1:-1: " + msg); } else { String fileName = offendingSymbol.getTokenSource().getSourceName(); System.err.println(groomFileName(fileName) + ":" + offendingSymbol.getLine() + ":" + offendingSymbol.getCharPositionInLine() + ": " + msg); underlineError(null, offendingSymbol, offendingSymbol.getLine(), offendingSymbol.getCharPositionInLine()); } }
From source file:edu.clemson.cs.r2jt.absynnew.UnderliningErrorListener.java
License:Open Source License
protected void underlineError(Recognizer recognizer, Token offendingToken, int line, int charPositionInLine) { String input;/*from ww w .j a v a 2 s . com*/ if (recognizer == null) { input = offendingToken.getTokenSource().getInputStream().toString(); } else { CommonTokenStream src = (CommonTokenStream) recognizer.getInputStream(); input = src.getTokenSource().getInputStream().toString(); } String[] lines = input.split("\n"); String errorLine = lines[line - 1].replaceAll("\t", " "); System.err.println(errorLine); for (int i = 0; i < charPositionInLine; i++) { System.err.print(" "); } System.err.print("^"); System.exit(1); }
From source file:edu.clemson.resolve.compiler.LanguageSemanticsMessage.java
License:BSD License
public LanguageSemanticsMessage(ErrorKind etype, Token offendingToken, Object... args) { super(etype, offendingToken, args); if (offendingToken != null) { this.fileName = Utils.groomFileName(offendingToken.getTokenSource().getSourceName()); this.line = offendingToken.getLine(); this.charPosition = offendingToken.getCharPositionInLine(); }// ww w . j a v a 2s .c o m }
From source file:edu.clemson.resolve.compiler.LanguageSyntaxMessage.java
License:BSD License
public LanguageSyntaxMessage(ErrorKind etype, Token offendingToken, RecognitionException antlrException, Object... args) {//from www.j a va2 s . c om super(etype, antlrException, offendingToken, args); this.offendingToken = offendingToken; if (offendingToken != null) { this.fileName = offendingToken.getTokenSource().getSourceName(); this.line = offendingToken.getLine(); this.charPosition = offendingToken.getCharPositionInLine(); } }
From source file:javasharp.XmlEmittingVisitor.java
License:Open Source License
@Override public Object visitTerminal(TerminalNode node) { Token symbol = node.getSymbol(); int tokenIndex = symbol.getTokenIndex(); emitComments(tokenIndex);/*from w w w. ja v a 2 s.c om*/ TokenSource tokenSource = symbol.getTokenSource(); emitToken("Symbol", symbol); return null; }
From source file:org.ledyba.sora.parser.FortranTokenStream.java
License:Open Source License
/** * Create a subset list of the non-whitespace tokens in the current line. *//* w w w . j a v a 2s . c o m*/ private ArrayList<Token> createPackedList() { int i = 0; Token tk = null; ArrayList<Token> pList = new ArrayList<>(this.lineLength + 1); for (i = 0; i < currLine.size(); i++) { tk = getTokenFromCurrLine(i); try { if (tk.getChannel() != Token.HIDDEN_CHANNEL) { pList.add(tk); } } catch (Exception e) { e.printStackTrace(); System.exit(1); } } // need to make sure the line was terminated with a T_EOS. this may // not happen if we're working on a file that ended w/o a newline Token last = pList.get(pList.size() - 1); if (last.getType() != FortranLexer.T_EOS) { Pair<TokenSource, CharStream> src = new Pair<>(last.getTokenSource(), last.getInputStream()); FortranToken eos = new FortranToken(src, FortranLexer.T_EOS, Token.DEFAULT_CHANNEL, last.getTokenIndex(), last.getTokenIndex() + 1); eos.setText("\n"); packedList.add(eos); } return pList; }
From source file:org.ledyba.sora.parser.FortranTokenStream.java
License:Open Source License
public void addTokenTo(int index, int line, int col, int tokenType, String tokenText) { try {// w w w. j a v a2 s .com // for example: // index = 1 // packedList == label T_CONTINUE T_EOS (size is 3) // newTokenList.size() == 22 // 22-3+1=20 // so, inserted between the label and T_CONTINUE Token last = this.tokens.get(index <= 0 ? 0 : index - 1); Pair<TokenSource, CharStream> src = new Pair<>(last.getTokenSource(), last.getInputStream()); FortranToken token = new FortranToken(src, tokenType, Token.DEFAULT_CHANNEL, last.getStopIndex(), last.getStopIndex()); token.setText(tokenText == null ? "" : tokenText); token.setLine(line); token.setCharPositionInLine(col); this.packedList.add(index, token); } catch (Exception e) { e.printStackTrace(); System.exit(1); } return; }
From source file:org.ledyba.sora.parser.FortranTokenStream.java
License:Open Source License
public void addToken(int type, String text, int line, int col) { Token last = this.tokens.get(this.tokens.size() - 1); Pair<TokenSource, CharStream> src = new Pair<>(last.getTokenSource(), last.getInputStream()); FortranToken token = new FortranToken(src, type, Token.DEFAULT_CHANNEL, 0, 0); token.setLine(line);/*from www . jav a 2s . co m*/ token.setCharPositionInLine(col); this.addToken(token); }