Repository navigation
Closed
Description
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
krlmlr commentedon Jul 28, 2017
We could work around this by searching for
EQ_ASSIGNin the parse data, but this is relatively low prio.krlmlr commentedon Jul 28, 2017
This may well be a bug in
getParseData(). IIUC, the problem is that inthe expressions
aand42and theEQ_ASSIGNterminal 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?
jimhester commentedon Jul 28, 2017
Yes, it is IMHO a bug, has been worked around in lintr for ~ 3 years
https://github.com/jimhester/lintr/blob/3312afafafd673aa7ec74231b39d83038508023c/R/get_source_expressions.R#L276-L351
lorenzwalthert commentedon Aug 9, 2017
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?
krlmlr commentedon Aug 11, 2017
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.
lorenzwalthert commentedon Sep 21, 2017
Reference: #213.
lorenzwalthert commentedon Sep 21, 2017
Reference: #216.