Stream: t-compiler/wg-rls-2.0

Topic: macro and syntax tree ranges


Jeremy Kolb (Nov 19 2019 at 21:12, on Zulip):

I'm a little confused. If I have an empty macro in my file:

macro_rules! id {
                () => {}
            }

Any nav target I generate falls outside of the range that shows up from the show sytnax tree command

Jeremy Kolb (Nov 19 2019 at 21:17, on Zulip):

For instance this:

fn foo() {}
        fn bar() {
            fo<|>o();
        }

Generates foo FN_DEF FileId(1) [0; 11) [3; 6)Which makes sense to me..

FN_DEF@[0; 11)
    FN_KW@[0; 2) "fn"
    WHITESPACE@[2; 3) " "
    NAME@[3; 6)
      IDENT@[3; 6) "foo"
    PARAM_LIST@[6; 8)
      L_PAREN@[6; 7) "("
      R_PAREN@[7; 8) ")"
    WHITESPACE@[8; 9) " "
    BLOCK_EXPR@[9; 11)
      BLOCK@[9; 11)
        L_CURLY@[9; 10) "{"
        R_CURLY@[10; 11) "}"
Jeremy Kolb (Nov 19 2019 at 21:19, on Zulip):

While

macro_rules! id {
                () => {}
            }
            fn foo() {}
            fn bar() {
                fo<|>o();
            }

Gives

SOURCE_FILE@[0; 140)
  MACRO_CALL@[0; 56)
    PATH@[0; 11)
      PATH_SEGMENT@[0; 11)
        NAME_REF@[0; 11)
          IDENT@[0; 11) "macro_rules"
    EXCL@[11; 12) "!"
    WHITESPACE@[12; 13) " "
    NAME@[13; 15)
      IDENT@[13; 15) "id"
    WHITESPACE@[15; 16) " "
    TOKEN_TREE@[16; 56)
      L_CURLY@[16; 17) "{"
      WHITESPACE@[17; 34) "\n                "
      TOKEN_TREE@[34; 36)
        L_PAREN@[34; 35) "("
        R_PAREN@[35; 36) ")"
      WHITESPACE@[36; 37) " "
      EQ@[37; 38) "="
      R_ANGLE@[38; 39) ">"
      WHITESPACE@[39; 40) " "
      TOKEN_TREE@[40; 42)
        L_CURLY@[40; 41) "{"
        R_CURLY@[41; 42) "}"
      WHITESPACE@[42; 55) "\n            "
      R_CURLY@[55; 56) "}"
  WHITESPACE@[56; 69) "\n            "
  FN_DEF@[69; 80)
    FN_KW@[69; 71) "fn"
    WHITESPACE@[71; 72) " "
    NAME@[72; 75)
      IDENT@[72; 75) "foo"
    PARAM_LIST@[75; 77)
      L_PAREN@[75; 76) "("
      R_PAREN@[76; 77) ")"
    WHITESPACE@[77; 78) " "
    BLOCK_EXPR@[78; 80)
      BLOCK@[78; 80)
        L_CURLY@[78; 79) "{"
        R_CURLY@[79; 80) "}"
  WHITESPACE@[80; 93) "\n            "
  FN_DEF@[93; 140)
    FN_KW@[93; 95) "fn"
    WHITESPACE@[95; 96) " "
    NAME@[96; 99)
      IDENT@[96; 99) "bar"
    PARAM_LIST@[99; 101)
      L_PAREN@[99; 100) "("
      R_PAREN@[100; 101) ")"
    WHITESPACE@[101; 102) " "
    BLOCK_EXPR@[102; 140)
      BLOCK@[102; 140)
        L_CURLY@[102; 103) "{"
        WHITESPACE@[103; 120) "\n                "
        EXPR_STMT@[120; 126)
          CALL_EXPR@[120; 125)
            PATH_EXPR@[120; 123)
              PATH@[120; 123)
                PATH_SEGMENT@[120; 123)
                  NAME_REF@[120; 123)
                    IDENT@[120; 123) "foo"
            ARG_LIST@[123; 125)
              L_PAREN@[123; 124) "("
              R_PAREN@[124; 125) ")"
          SEMI@[125; 126) ";"
        WHITESPACE@[126; 139) "\n            "
        R_CURLY@[139; 140) "}"

with foo FN_DEF FileId(1) [33; 44) [36; 39) That range is not within the fn def

Jeremy Kolb (Nov 19 2019 at 21:19, on Zulip):

But goto def works in this case

matklad (Nov 20 2019 at 05:42, on Zulip):

@Jeremy Kolb hm, [33; 44) is exactly the range of the fn node. Tests strip leadning indentation I think

Jeremy Kolb (Nov 20 2019 at 12:07, on Zulip):

Why isn't it [69; 80)?

matklad (Nov 20 2019 at 12:09, on Zulip):

pasted image

matklad (Nov 20 2019 at 12:09, on Zulip):

pasted image

matklad (Nov 20 2019 at 12:10, on Zulip):

(33 is the start of the fn

Jeremy Kolb (Nov 20 2019 at 12:15, on Zulip):

So... then what do the ranges in the syntax tree represent?

Jeremy Kolb (Nov 20 2019 at 12:15, on Zulip):

I've always assumed they were the character offsets.

matklad (Nov 20 2019 at 12:20, on Zulip):

Character offsets

matklad (Nov 20 2019 at 12:20, on Zulip):

33-44 is exactly the range of fn foo() {} (exactly 11 chars)

Jeremy Kolb (Nov 20 2019 at 12:23, on Zulip):

Right... so why is it FN_DEF@[69; 80) above?

matklad (Nov 20 2019 at 12:23, on Zulip):

Because it is indented

matklad (Nov 20 2019 at 12:23, on Zulip):

if it is not indended, it's 33-44

Jeremy Kolb (Nov 20 2019 at 12:24, on Zulip):

ah. gotcha

Last update: Dec 12 2019 at 01:50UTC