Example usage for org.antlr.v4.runtime Token getCharPositionInLine

List of usage examples for org.antlr.v4.runtime Token getCharPositionInLine

Introduction

In this page you can find the example usage for org.antlr.v4.runtime Token getCharPositionInLine.

Prototype

int getCharPositionInLine();

Source Link

Document

The index of the first character of this token relative to the beginning of the line at which it occurs, 0..n-1

Usage

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  www.j a v  a 2  s  .  c  om*/
        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);//w  w w .j  av a  2s  . 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.huberlin.wbi.cuneiform.core.preprocess.PreListener.java

License:Apache License

@Override
public void exitFromStackExpr(@NotNull CuneiformParser.FromStackExprContext ctx) {

    Token symbol;

    symbol = ctx.FROMSTACK().getSymbol();

    if (exprStack.isEmpty())
        throw new ParseException(symbol.getLine(), symbol.getCharPositionInLine(), symbol.getText(),
                "Pipe destination encountered where there is no source.");

    rewriter.replace(symbol, exprStack.pop());
}

From source file:de.uni.bremen.monty.moco.ast.ASTBuilder.java

License:Open Source License

private Position position(Token idSymbol) {
    return new Position(fileName, idSymbol.getLine(), idSymbol.getCharPositionInLine());
}

From source file:de.up.ling.irtg.codec.ExceptionErrorStrategy.java

private String getTokenPosition(Token t) {
    return "Line " + t.getLine() + ":" + t.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 ww  .j  a  v a  2s  .  c om*/
 *
 * @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.rsrg.parsing.TreeBuildingListener.java

License:Open Source License

/**
 * <p>Create a location for the current parser token
 * we are visiting.</p>//from w ww  .  j  av a 2  s. com
 *
 * @param t The visiting ANTLR4 parser token.
 *
 * @return A {@link Location} for the rule.
 */
private Location createLocation(Token t) {
    return new Location(myFile, t.getLine(), t.getCharPositionInLine());
}

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();
    }//from   w w w .j  av a  2  s .  c om
}

From source file:edu.clemson.resolve.compiler.LanguageSyntaxMessage.java

License:BSD License

public LanguageSyntaxMessage(ErrorKind etype, Token offendingToken, RecognitionException antlrException,
        Object... args) {/*from   w w w .ja v  a 2  s. c  o  m*/
    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:it.polimi.tower4clouds.rules.RulesValidator.java

License:Apache License

private Set<Problem> validateCondition(MonitoringRule rule) {
    Set<Problem> problems = new HashSet<Problem>();
    if (rule.getCondition() == null)
        return problems;
    String condition = rule.getCondition().getValue();
    if (condition != null) {
        ANTLRInputStream input = new ANTLRInputStream(condition);
        ConditionLexer lexer = new ConditionLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        ConditionParser parser = new ConditionParser(tokens);
        parser.setErrorHandler(new ErrorStrategy());
        parser.removeErrorListeners();// w w w  .j av a  2 s. c o m
        lexer.removeErrorListeners();

        try {
            parser.expression();
        } catch (Exception e) {
            Problem problem = new Problem();
            problem.setElementId(rule.getId());
            problem.setTagName("condition");
            problem.setError(EnumErrorType.CONDITION_LEXICAL_ERROR);
            if (e.getCause().toString().contains("NoViableAltException")) {
                Token token = ((NoViableAltException) (e.getCause())).getOffendingToken();
                problem.setDescription(
                        "Lexical error: '" + condition.substring(0, token.getCharPositionInLine()) + " "
                                + condition.substring(token.getCharPositionInLine()) + "'");
            } else if (e instanceof InputMismatchException) {
                problem.setDescription("Input Mismatch Exception");
            } else if (e instanceof FailedPredicateException) {
                problem.setDescription("Failed Predicate Exception");
            } else {
                problem.setDescription("Unknown recognition error! Exception: " + e.getClass().getName());
            }
            problems.add(problem);
        }

    }
    return problems;
}