Example usage for org.antlr.v4.runtime ParserRuleContext getStart

List of usage examples for org.antlr.v4.runtime ParserRuleContext getStart

Introduction

In this page you can find the example usage for org.antlr.v4.runtime ParserRuleContext getStart.

Prototype

public Token getStart() 

Source Link

Document

Get the initial token in this context.

Usage

From source file:tnsnamesInterfaceListener.java

License:Open Source License

@Override
public void enterEveryRule(ParserRuleContext ctx) {
    Token startToken = ctx.getStart();
    if (startToken != null) {
        // Lines number from 1.
        this.lineNumber = startToken.getLine();

        // Characters from zero. Adjust.
        this.charPosition = 1 + startToken.getCharPositionInLine();
    } else {/*  w w  w  .j av  a 2  s  . c  o m*/
        // Just in case Token can ever be null.
        this.lineNumber = 0;
        this.charPosition = 0;
    }

    // Build a location string for error messages etc.
    this.whereAmI = "\tLine " + lineNumber + ":" + charPosition + " ";
}

From source file:android.databinding.tool.store.Location.java

License:Apache License

public Location(ParserRuleContext context) {
    this(context == null ? null : context.getStart(), context == null ? null : context.getStop());
}

From source file:annis.ql.parser.JoinListener.java

License:Apache License

private Join addParsedLocation(ParserRuleContext ctx, Join j) {
    j.setParseLocation(AnnisParserAntlr.getLocation(ctx.getStart(), ctx.getStop()));
    return j;
}

From source file:annis.ql.parser.QueryNodeListener.java

License:Apache License

private QueryNode newNode(ParserRuleContext ctx) {
    Long existingID = nodeIntervalToID.get(ctx.getSourceInterval());

    if (existingID == null) {
        throw new IllegalStateException(
                "Could not find a node ID for interval " + ctx.getSourceInterval().toString());
    }//from www. ja va  2s. c  om

    QueryNode n = new QueryNode(existingID);
    if (lastVariableDefinition == null) {
        n.setVariable("" + n.getId());
    } else {
        n.setVariable(lastVariableDefinition);
    }
    lastVariableDefinition = null;

    n.setParseLocation(AnnisParserAntlr.getLocation(ctx.getStart(), ctx.getStop()));

    currentAlternative.put(existingID, n);
    localNodes.put(n.getVariable(), n);
    currentTokenPosition.put(ctx.getSourceInterval(), n);

    return n;
}

From source file:br.beholder.memelang.model.visitor.TreePrinterListener.java

@Override
public void exitEveryRule(ParserRuleContext ctx) {
    if (ctx.getChildCount() > 0) {
        Token positionToken = ctx.getStart();
        if (positionToken != null) {
            builder.append(" [line ");
            builder.append(positionToken.getLine());
            builder.append(", offset ");
            builder.append(positionToken.getStartIndex());
            builder.append(':');
            builder.append(positionToken.getStopIndex());
            builder.append("])");
        } else {/*from   ww w.  j av  a  2s.  c  om*/
            builder.append(')');
        }
    }
}

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 2s. 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:codesniffer.java8.adapter.AdapterUtil.java

License:Open Source License

/**
 * If there are no statements within a block, we need a special method to grab any comments that
 * might exist between braces.//  w  w  w  .j  a  v  a  2  s .com
 *
 * @param node
 * @param parserRuleContext
 * @param adapterParameters
 */
public static void setInternalComments(Node node, ParserRuleContext parserRuleContext,
        AdapterParameters adapterParameters) {
    BufferedTokenStream tokens = adapterParameters.getTokens();

    if (node == null || parserRuleContext == null || tokens == null) {
        throw new IllegalArgumentException("Parameters must not be null");
    }

    Token startToken = parserRuleContext.getStart();
    Token stopToken = parserRuleContext.getStop();

    List<Token> commentTokens;
    ArrayList<Comment> internalCommentList = new ArrayList<>();

    // Checking to the right of the start token will check inside the statement
    commentTokens = tokens.getHiddenTokensToRight(startToken.getTokenIndex(), Java8Lexer.COMMENTS);
    if (commentTokens != null) {
        internalCommentList.ensureCapacity(commentTokens.size());
        for (Token commentToken : commentTokens) {

            // Skip already claimed comments (prevents comment repeats)
            if (adapterParameters.isCommentTokenClaimed(commentToken.getTokenIndex())) {
                continue;
            } else {
                // Claim it
                adapterParameters.claimCommentToken(commentToken.getTokenIndex());
            }

            if (commentToken.getText().startsWith("/**")) {
                DocumentComment javadocComment = new DocumentComment(commentToken.getText());
                internalCommentList.add(javadocComment);
            } else if (commentToken.getText().startsWith("/*")) {
                BlockComment blockComment = new BlockComment(commentToken.getText());
                internalCommentList.add(blockComment);
            } else if (commentToken.getText().startsWith("//")) {
                LineComment lineComment = new LineComment(commentToken.getText());
                internalCommentList.add(lineComment);
            }
        }
    }
    if (internalCommentList.size() > 0) {
        if (node.getOrphanComments() != null) {
            node.getOrphanComments().addAll(internalCommentList);
        } else {
            node.setOrphanComments(internalCommentList);
        }
    }
    //        if (internalCommentList.size() > 0) {
    //            if (node.getInternalComments() != null) {
    //                node.getInternalComments().addAll(internalCommentList);
    //            } else {
    //                node.setInternalComments(internalCommentList);
    //            }
    //        }
}

From source file:codesniffer.java8.adapter.AdapterUtil.java

License:Open Source License

public static void setPosition(Node node, ParserRuleContext ctx) {
    int beginLine = ctx.getStart().getLine();
    int beginColumn = ctx.getStart().getCharPositionInLine();
    int endLine = ctx.getStop().getLine();
    int endTokenLength = ctx.getStop().getStopIndex() - ctx.getStop().getStartIndex();
    int endColumn = ctx.getStop().getCharPositionInLine() + endTokenLength;

    node.setBeginLine(beginLine);//from   w w  w.  j a  v a  2 s  .co  m
    node.setBeginColumn(beginColumn);
    node.setEndLine(endLine);
    node.setEndColumn(endColumn);
}

From source file:codesniffer.java8.adapter.AdapterUtil.java

License:Open Source License

public static void setComments(Node node, ParserRuleContext parserRuleContext,
        AdapterParameters adapterParameters) {
    BufferedTokenStream tokens = adapterParameters.getTokens();

    if (node == null || parserRuleContext == null || tokens == null) {
        // Just return
        return;/*  w  w  w  . j  ava  2  s  . c  o m*/
    }

    Token startToken = parserRuleContext.getStart();
    List<Token> commentTokens = null;
    try {
        //            commentTokens = tokens.getHiddenTokensToLeft(startToken.getTokenIndex(), Java8Lexer.COMMENTS);
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    if (commentTokens != null && commentTokens.size() > 0) {
        Iterator<Token> iter = commentTokens.iterator();
        Token ct = iter.next();
        int idx = ct.getTokenIndex();
        if (adapterParameters.isCommentTokenClaimed(idx)) {
            adapterParameters.claimCommentToken(idx);

            node.setComment(extractComment(ct));

            if (iter.hasNext()) {
                List<Comment> orf = new ArrayList<>(8);
                while (iter.hasNext()) {
                    orf.add(extractComment(iter.next()));
                }
                node.setOrphanComments(orf);
            }

        }
    }

    /**
     * obsolete api. bowen 7/19
     */
    //        List<Comment> beginCommentList = new LinkedList<Comment>();
    //        List<Comment> endCommentList = new LinkedList<Comment>();
    //        if (commentTokens != null) {
    //            for (Token commentToken : commentTokens) {
    //
    //                // Skip already claimed comments (prevents comment repeats)
    //                if (adapterParameters.isCommentTokenClaimed(commentToken.getTokenIndex())) {
    //                    continue;
    //                } else {
    //                    // Claim it
    //                    adapterParameters.claimCommentToken(commentToken.getTokenIndex());
    //                }
    //
    //                if (commentToken.getText().trim().startsWith("/**")) {
    //                    DocumentComment javadocComment = new DocumentComment(commentToken.getText());
    //                    beginCommentList.add(javadocComment);
    //                } else if (commentToken.getText().trim().startsWith("/*")) {
    //                    BlockComment blockComment = new BlockComment(commentToken.getText());
    //                    beginCommentList.add(blockComment);
    //                } else if (commentToken.getText().trim().startsWith("//")) {
    //                    LineComment lineComment = new LineComment(commentToken.getText());
    //                    beginCommentList.add(lineComment);
    //                }
    //            }
    //        }

    //        commentTokens = tokens.getHiddenTokensToRight(stopToken.getTokenIndex(), Java8Lexer.COMMENTS);
    //        if (commentTokens != null) {
    //            for (Token commentToken : commentTokens) {
    //
    //                if (commentToken.getLine() == stopToken.getLine()) {
    //
    //                    // Skip already claimed comments (prevents comment repeats)
    //                    if (adapterParameters.isCommentTokenClaimed(commentToken.getTokenIndex())) {
    //                        continue;
    //                    } else {
    //                        // Claim it
    //                        adapterParameters.claimCommentToken(commentToken.getTokenIndex());
    //                    }
    //
    //                    if (commentToken.getText().trim().startsWith("/**")) {
    //                        DocumentComment javadocComment = new DocumentComment(commentToken.getText());
    //                        endCommentList.add(javadocComment);
    //                    } else if (commentToken.getText().trim().startsWith("/*")) {
    //                        BlockComment blockComment = new BlockComment(commentToken.getText());
    //                        endCommentList.add(blockComment);
    //                    } else if (commentToken.getText().trim().startsWith("//")) {
    //                        LineComment lineComment = new LineComment(commentToken.getText());
    //                        endCommentList.add(lineComment);
    //                    }
    //                }
    //            }
    //        }

    //        if (beginCommentList.size() > 0) {
    //            if (node.getBeginComments() != null) {
    //                node.getBeginComments().addAll(beginCommentList);
    //            } else {
    //                node.setBeginComments(beginCommentList);
    //            }
    //        }
    //
    //        if (endCommentList.size() > 0) {
    //            if (node.getEndComments() != null) {
    //                node.getEndComments().addAll(endCommentList);
    //            } else {
    //                node.setEndComments(endCommentList);
    //            }
    //        }
}

From source file:com.bacoder.parser.core.Adapter.java

License:Apache License

protected <T> void setNodeAttributes(T data, ParseTree startNode, ParseTree endNode) {
    if (data instanceof Node) {
        Node node = (Node) data;

        if (startNode instanceof ParserRuleContext) {
            ParserRuleContext context = (ParserRuleContext) startNode;
            node.setStartLine(context.getStart().getLine());
            node.setStartColumn(context.getStart().getCharPositionInLine());
        } else if (startNode instanceof TerminalNode) {
            TerminalNode terminal = (TerminalNode) startNode;
            node.setStartLine(terminal.getSymbol().getLine());
            node.setStartColumn(terminal.getSymbol().getCharPositionInLine());
        }/*from   ww  w.ja  va  2 s.c  o  m*/

        if (endNode instanceof ParserRuleContext) {
            ParserRuleContext context = (ParserRuleContext) endNode;
            node.setEndLine(context.getStop().getLine());
            node.setEndColumn(
                    context.getStop().getCharPositionInLine() + context.getStop().getText().length() - 1);
        } else if (endNode instanceof TerminalNode) {
            TerminalNode terminal = (TerminalNode) endNode;
            node.setEndLine(terminal.getSymbol().getLine());
            node.setEndColumn(
                    terminal.getSymbol().getCharPositionInLine() + terminal.getSymbol().getText().length() - 1);
        }
    }
}