Why am I not getting the correct column positions in this parse tree?

Say I have a line like this:

var code = "Private Sub DoSomething(ByVal foo As Integer)\r\n    DoSomethingElse(foo)\r\nEnd Sub";

When I pass my ANTLR parser with this line, I get a parsing tree that looks something like this:

[SubStmtContext]
    [VisibilityContext]
    [ArgListContext]
        [ArgContext]
            [AmbiguousIdentifierContext]
            [AsTypeClauseContext]
    [BlockContext]
        [ImplicitCallStmt_InBlockContext]
            [ICS_B_SubCallContext]
                [CertainIdentifierContext]
                [ArgsCallContext]
                    [ArgCallContext]
                        [ValueStmtContext]
                            [ImplicitCallStmt_InStmtContext]
                                [ICS_S_VariableCallContext]
                                    [VariableCallStmtContext]
                                        [AmbiguousIdentifierContext]

The base class ParserRuleContextcontains the properties IToken Startand IToken Stop, each of which sets the number Linec StartIndexand a StopIndex.

So, back to my input line, I have an identifier DoSomethingin row 1, the beginning of column position 12 and stop 22 - why is it that I have an identifier DoSomethingElsein row 2, beginning 51 of the column position and stop 65?

ANTLR, \r\n , ? - , ... , DoSomethingElse 2, 4 18, ?

, :

    public IParseTree Parse(string code)
    {
        var input = new AntlrInputStream(code);
        var lexer = new VBLexer(input); // generated type
        var tokens = new CommonTokenStream(lexer);
        var parser = new VBParser(tokens); // generated type

        var result = parser.StartRule(); // generated method
        return result;
    }

. - ?

+4
1

Token "-". getStopIndex(), , . getStartIndex() getStopIndex() ( ). , getCharPositionInLine() .

+3

All Articles