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

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

Introduction

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

Prototype

int getLine();

Source Link

Document

The line number on which the 1st character of this token was matched, line=1..n

Usage

From source file:ca.nines.ise.dom.DOMBuilder.java

License:Open Source License

/**
 * Set up a newly created node with information from the context.
 *
 * @param n/*from   w  w  w .  j av  a  2  s  .  co  m*/
 * @param ctx
 * @return Node
 */
// @TODO turn this into Node.Builder and provide a Node.builder()
// etc.
private Node setupNode(Node n, ParserRuleContext ctx) {
    Token t = ctx.getStart();
    n.setOwner(dom);
    n.setLine(t.getLine());
    n.setColumn(t.getCharPositionInLine());
    n.setText(tokens.getText(ctx.getSourceInterval()));
    return n;
}

From source file:cfml.parsing.cfscript.CFParsedStatement.java

License:Open Source License

public void setToken(Token t) {
    if (t != null) {
        line = t.getLine();
        col = t.getCharPositionInLine() + 1;
    }
    token = t;
}

From source file:cfml.parsing.cfscript.script.CFParsedStatement.java

License:Open Source License

protected CFParsedStatement(Token t) {
    this(t.getLine(), t.getCharPositionInLine());
    token = t;
}

From source file:ch.raffael.contracts.processor.cel.ast.Nodes.java

License:Apache License

@NotNull
private static Position pos(@NotNull Token tok) {
    return new Position(tok.getLine(), tok.getCharPositionInLine());
}

From source file:ch.raffael.contracts.processor.cel.Position.java

License:Apache License

public Position(@NotNull Token token) {
    this(token.getLine(), token.getCharPositionInLine());
}

From source file:com.cisco.yangide.core.parser.YangParserModelListener.java

License:Open Source License

private void updateNamedNode(ASTNamedNode astNode, ParseTree treeNode) {
    updateNodePosition(astNode, treeNode);
    for (int i = 0; i < treeNode.getChildCount(); ++i) {
        if (treeNode.getChild(i) instanceof StringContext) {
            final StringContext context = (StringContext) treeNode.getChild(i);
            if (context != null) {
                Token token = context.getStart();
                astNode.setNameStartPosition(token.getStartIndex());
                astNode.setNameLength(token.getStopIndex() - token.getStartIndex() + 1);
                astNode.setLineNumber(token.getLine());
                astNode.setName(stringFromStringContext(context));
            }/* w w w.  j  a  va2  s  . c  o m*/
        }
    }
}

From source file:com.cisco.yangide.core.parser.YangParserUtil.java

License:Open Source License

public static void validateYangContext(YangContext context, IYangValidationListener validationListener) {
    final ParseTreeWalker walker = new ParseTreeWalker();
    final YangModelBasicValidationListener yangModelParser = new YangModelBasicValidationListener();
    try {/*from  ww  w  .  j a  v a 2s .c  om*/
        walker.walk(yangModelParser, context);
    } catch (YangValidationException e) {
        if (validationListener != null) {
            int lineNumber = -1;
            int charStart = 0;
            int charEnd = 0;
            if (e.getContext() instanceof ParserRuleContext) {
                Token token = ((ParserRuleContext) e.getContext()).getStart();
                lineNumber = token.getLine();
                charStart = token.getStartIndex();
                charEnd = token.getStopIndex() + 1;
            }
            validationListener.validationError(e.getMessage(), lineNumber, charStart, charEnd);
        }
    }
}

From source file:com.espertech.esper.core.deploy.EPLModuleUtil.java

License:Open Source License

public static List<EPLModuleParseItem> parse(String module) throws ParseException {

    CharStream input;//from ww  w.  java2  s .c om
    try {
        input = new NoCaseSensitiveStream(new StringReader(module));
    } catch (IOException ex) {
        log.error("Exception reading module expression: " + ex.getMessage(), ex);
        return null;
    }

    EsperEPL2GrammarLexer lex = ParseHelper.newLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lex);
    try {
        tokens.fill();
    } catch (RuntimeException ex) {
        String message = "Unexpected exception recognizing module text";
        if (ex instanceof LexerNoViableAltException) {
            if (ParseHelper.hasControlCharacters(module)) {
                message = "Unrecognized control characters found in text, failed to parse text";
            } else {
                message += ", recognition failed for " + ex.toString();
            }
        } else if (ex instanceof RecognitionException) {
            RecognitionException recog = (RecognitionException) ex;
            message += ", recognition failed for " + recog.toString();
        } else if (ex.getMessage() != null) {
            message += ": " + ex.getMessage();
        }
        message += " [" + module + "]";
        log.error(message, ex);
        throw new ParseException(message);
    }

    List<EPLModuleParseItem> statements = new ArrayList<EPLModuleParseItem>();
    StringWriter current = new StringWriter();
    Integer lineNum = null;
    int charPosStart = 0;
    int charPos = 0;
    List<Token> tokenList = tokens.getTokens();
    Set<Integer> skippedSemicolonIndexes = getSkippedSemicolons(tokenList);
    int index = -1;
    for (Object token : tokenList) // Call getTokens first before invoking tokens.size! ANTLR problem
    {
        index++;
        Token t = (Token) token;
        boolean semi = t.getType() == EsperEPL2GrammarLexer.SEMI && !skippedSemicolonIndexes.contains(index);
        if (semi) {
            if (current.toString().trim().length() > 0) {
                statements.add(new EPLModuleParseItem(current.toString().trim(), lineNum == null ? 0 : lineNum,
                        charPosStart, charPos));
                lineNum = null;
            }
            current = new StringWriter();
        } else {
            if ((lineNum == null) && (t.getType() != EsperEPL2GrammarParser.WS)) {
                lineNum = t.getLine();
                charPosStart = charPos;
            }
            if (t.getType() != EsperEPL2GrammarLexer.EOF) {
                current.append(t.getText());
                charPos += t.getText().length();
            }
        }
    }

    if (current.toString().trim().length() > 0) {
        statements.add(new EPLModuleParseItem(current.toString().trim(), lineNum == null ? 0 : lineNum, 0, 0));
    }
    return statements;
}

From source file:com.espertech.esper.epl.parse.ExceptionConvertor.java

License:Open Source License

/**
 * Returns the position information string for a parser exception.
 * @param t the token to return the information for
 * @return is a string with line and column information
 *///from   ww w.  jav  a  2s  . c o m
private static String getPositionInfo(Token t) {
    return t.getLine() > 0 && t.getCharPositionInLine() > 0
            ? " at line " + t.getLine() + " column " + t.getCharPositionInLine()
            : "";
}

From source file:com.espertech.esper.epl.parse.ParseHelper.java

License:Open Source License

private static String getNoAnnotation(String expression,
        List<EsperEPL2GrammarParser.AnnotationEnumContext> annos, CommonTokenStream tokens) {
    if (annos == null || annos.isEmpty()) {
        return expression;
    }//from   ww w . j a  v a  2  s . c  o  m
    Token lastAnnotationToken = annos.get(annos.size() - 1).getStop();

    if (lastAnnotationToken == null) {
        return null;
    }

    try {
        int line = lastAnnotationToken.getLine();
        int charpos = lastAnnotationToken.getCharPositionInLine();
        int fromChar = charpos + lastAnnotationToken.getText().length();
        if (line == 1) {
            return expression.substring(fromChar).trim();
        }

        String[] lines = expression.split("\r\n|\r|\n");
        StringBuilder buf = new StringBuilder();
        buf.append(lines[line - 1].substring(fromChar));
        for (int i = line; i < lines.length; i++) {
            buf.append(lines[i]);
            if (i < lines.length - 1) {
                buf.append(newline);
            }
        }
        return buf.toString().trim();
    } catch (RuntimeException ex) {
        log.error("Error determining non-annotated expression sting: " + ex.getMessage(), ex);
    }
    return null;
}