List of usage examples for org.antlr.v4.runtime Token getCharPositionInLine
int getCharPositionInLine();
From source file:com.github.jknack.handlebars.internal.TemplateBuilder.java
License:Apache License
@Override public BaseTemplate visitNewline(final NewlineContext ctx) { Token newline = ctx.NL().getSymbol(); if (newline.getChannel() == Token.HIDDEN_CHANNEL) { return null; }//from ww w . j a va2 s .c o m line.setLength(0); return new Text(newline.getText()).filename(source.filename()).position(newline.getLine(), newline.getCharPositionInLine()); }
From source file:com.ibm.bi.dml.parser.python.PydmlSyntacticValidator.java
License:Open Source License
private ConstIdentifier getConstIdFromString(String varValue, Token start) { // Both varName and varValue are correct int linePosition = start.getLine(); int charPosition = start.getCharPositionInLine(); try {/* w w w .ja v a2 s . co m*/ long val = Long.parseLong(varValue); return new IntIdentifier(val, helper.getCurrentFileName(), linePosition, charPosition, linePosition, charPosition); } catch (Exception e) { try { double val = Double.parseDouble(varValue); return new DoubleIdentifier(val, helper.getCurrentFileName(), linePosition, charPosition, linePosition, charPosition); } catch (Exception e1) { try { if (varValue.compareTo("True") == 0 || varValue.compareTo("False") == 0) { boolean val = false; if (varValue.compareTo("True") == 0) { val = true; } return new BooleanIdentifier(val, helper.getCurrentFileName(), linePosition, charPosition, linePosition, charPosition); } else { String val = ""; String text = varValue; if ((text.startsWith("\"") && text.endsWith("\"")) || (text.startsWith("\'") && text.endsWith("\'"))) { if (text.length() > 2) { val = text.substring(1, text.length() - 1); } } else { val = text; // the commandline parameters can be passed without any quotes // helper.notifyErrorListeners("something wrong while parsing string ... strange", start); // return null; } return new StringIdentifier(val, helper.getCurrentFileName(), linePosition, charPosition, linePosition, charPosition); } } catch (Exception e3) { helper.notifyErrorListeners( "unable to cast the commandline parameter into int/double/boolean/string", start); return null; } } } }
From source file:com.ibm.bi.dml.parser.python.PydmlSyntacticValidatorHelper.java
License:Open Source License
public void notifyErrorListeners(String message, Token op) { this._errorListener.validationError(op.getLine(), op.getCharPositionInLine(), message); }
From source file:com.microsoft.thrifty.schema.parser.ThriftListener.java
License:Open Source License
private Location locationOf(Token token) { int line = token.getLine(); int col = token.getCharPositionInLine() + 1; // Location.col is 1-based, Token.col is 0-based return location.at(line, col); }
From source file:com.oracle.truffle.sl.parser.SimpleLanguageParser.java
License:Open Source License
public void SemErr(Token token, String message) { assert token != null; throwParseError(source, token.getLine(), token.getCharPositionInLine(), token, message); }
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.//from w ww .java 2s. 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: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 ww w . j a v a 2 s . com*/ }
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();/* w w w . ja va 2 s . co 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()); }//from www. j a v a 2 s . c o m return new ParseException("unexpected token: " + token.getText(), null, token.getLine(), token.getCharPositionInLine()); }