Skip to content

inconsistency in getParseData? #100

Closed

Description

@lorenzwalthert

Why is the following different and what can we do about it?

arrow <- "a <- 3"
eq <- "a = 3"

styler:::create_tree(arrow)
#>                                             levelName
#> 1 ROOT (token: short_text [lag_newlines/spaces] {id})
#> 2  °--expr:  [0/0] {7}                               
#> 3      ¦--expr:  [0/1] {3}                           
#> 4      ¦   °--SYMBOL: a [0/0] {1}                    
#> 5      ¦--LEFT_ASSIGN: <- [0/1] {2}                  
#> 6      °--expr:  [0/0] {5}                           
#> 7          °--NUM_CONST: 3 [0/0] {4}
styler:::create_tree(eq)
#>                                             levelName
#> 1 ROOT (token: short_text [lag_newlines/spaces] {id})
#> 2  ¦--expr:  [0/1] {3}                               
#> 3  ¦   °--SYMBOL: a [0/0] {1}                        
#> 4  ¦--EQ_ASSIGN: = [0/1] {2}                         
#> 5  °--expr:  [0/0] {5}                               
#> 6      °--NUM_CONST: 3 [0/0] {4}

utils::getParseData(parse(text = eq))
#>  line1 col1 line2 col2 id parent     token terminal text
#> 1     1    1     1    1  1      3    SYMBOL     TRUE    a
#> 3     1    1     1    1  3      0      expr    FALSE     
#> 2     1    3     1    3  2      0 EQ_ASSIGN     TRUE    =
#> 4     1    5     1    5  4      5 NUM_CONST     TRUE    3
#> 5     1    5     1    5  5      0      expr    FALSE     

utils::getParseData(parse(text = arrow))
#>  line1 col1 line2 col2 id parent     token terminal text
#> 7     1    1     1    6  7      0        expr    FALSE     
#> 1     1    1     1    1  1      3      SYMBOL     TRUE    a
#> 3     1    1     1    1  3      7        expr    FALSE     
#> 2     1    3     1    4  2      7 LEFT_ASSIGN     TRUE   <-
#> 4     1    6     1    6  4      5   NUM_CONST     TRUE    3
#> 5     1    6     1    6  5      7        expr    FALSE     

Now there was just one expression, but the problem is worse if there are multiple, because with =,
they will all be on the same level, i.e. more than one expression has parent 0.

styler:::create_tree("a = 3 \n 3")
#>                                             levelName
#> 1 ROOT (token: short_text [lag_newlines/spaces] {id})
#> 2  ¦--expr:  [0/1] {3}                               
#> 3  ¦   °--SYMBOL: a [0/0] {1}                        
#> 4  ¦--EQ_ASSIGN: = [0/1] {2}                         
#> 5  ¦--expr:  [0/1] {5}                               
#> 6  ¦   °--NUM_CONST: 3 [0/0] {4}                     
#> 7  °--expr:  [1/0] {12}                              
#> 8      °--NUM_CONST: 3 [0/0] {11}

#> line1 col1 line2 col2 id parent     token terminal text
#> 1      1    1     1    1  1      3    SYMBOL     TRUE    a
#> 3      1    1     1    1  3      0      expr    FALSE     
#> 2      1    3     1    3  2      0 EQ_ASSIGN     TRUE    =
#> 4      1    5     1    5  4      5 NUM_CONST     TRUE    3
#> 5      1    5     1    5  5      0      expr    FALSE     
#> 11     2    2     2    2 11     12 NUM_CONST     TRUE    3
#> 12     2    2     2    2 12      0      expr    FALSE     

For us, it is a problem if we want to indent based on =. We can just indent the token after =, but
if within that expression we have something that also needs indention, we cannot flatten out and will have too much indention:

a =
  b +  # still correct
    c + # not correct anymore
    d

Activity

  1. krlmlr commented on Jul 28, 2017

    @krlmlr
    Member

    We could work around this by searching for EQ_ASSIGN in the parse data, but this is relatively low prio.

  2. krlmlr commented on Jul 28, 2017

    @krlmlr
    Member

    This may well be a bug in getParseData(). IIUC, the problem is that in

    withr::with_options(list(keep.source = TRUE), utils::getParseData(parse(text = "a=42")))
    #>   line1 col1 line2 col2 id parent     token terminal text
    #> 1     1    1     1    1  1      3    SYMBOL     TRUE    a
    #> 3     1    1     1    1  3      0      expr    FALSE     
    #> 2     1    2     1    2  2      0 EQ_ASSIGN     TRUE    =
    #> 4     1    3     1    4  4      5 NUM_CONST     TRUE   42
    #> 5     1    3     1    4  5      0      expr    FALSE

    the expressions a and 42 and the EQ_ASSIGN terminal have 0 as parent, and we lose the information that these three items actually belong to the same expression.

    @jimhester @halpo: Does this ring a bell?

  3. jimhester commented on Jul 28, 2017

    @jimhester
    Member
  4. lorenzwalthert commented on Aug 9, 2017

    @lorenzwalthert
    CollaboratorAuthor

    Thanks @jimhester.
    @krlmlr Do you think there any chance this will get fixed if we file an issue or should we just use the work around from lintr?

  5. krlmlr commented on Aug 11, 2017

    @krlmlr
    Member

    Even if a fix to base R got accepted now, it won't be earlier than R 3.5.0 (or perhaps R 3.4.2) until users see it. We'll need a workaround anyway to support earlier versions. Borrowing from lintr seems like the easiest option, but we do need to respect the license.

    I wonder if we could put this and other functionality into a package dedicated to handling parse data. There is sourcetools, but the data format seems to be different.

  6. lorenzwalthert commented on Sep 21, 2017

    @lorenzwalthert
    CollaboratorAuthor

    Reference: #213.

  7. lorenzwalthert commented on Sep 21, 2017

    @lorenzwalthert
    CollaboratorAuthor

    Reference: #216.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions