Although syntactically identical, utils::getParseData() does not produce the same hierarchy of the parse table (parent and id relationship) for <- and = (See 'Examples'). This is considered to be a bug and causes problems because the nested parse table constructed with compute_parse_data_nested() is not consistent if EQ_ASSIGN occurs in the expression to style. In particular, EQ_ASSIGN and the tokens to its left and right are located too high up in the hierarchy of the nested parse data. Hence, this function wraps the sub-expression into an expression, similar to wrap_expr_in_curly(). Since wrap_expr_in_curly() is called from within a visitor (and relocate_eq_assign() not), we need to wrap the the implementation relocate_eq_assign_nest() that operates on nests into a visitor call.

relocate_eq_assign(pd)

Arguments

pd

A parse table.

Examples

styler:::get_parse_data("a <- b <- 3")
#>    line1 col1 line2 col2 id parent       token terminal        text pos_id
#> 1      1    1     1   11 11      0        expr    FALSE a <- b <- 3      1
#> 2      1    1     1    1  1      3      SYMBOL     TRUE           a      2
#> 3      1    1     1    1  3     11        expr    FALSE           a      3
#> 4      1    3     1    4  2     11 LEFT_ASSIGN     TRUE          <-      4
#> 5      1    6     1   11 10     11        expr    FALSE      b <- 3      5
#> 6      1    6     1    6  4      6      SYMBOL     TRUE           b      6
#> 7      1    6     1    6  6     10        expr    FALSE           b      7
#> 8      1    8     1    9  5     10 LEFT_ASSIGN     TRUE          <-      8
#> 9      1   11     1   11  7      8   NUM_CONST     TRUE           3      9
#> 10     1   11     1   11  8     10        expr    FALSE           3     10
#>    short
#> 1  a <- 
#> 2      a
#> 3      a
#> 4     <-
#> 5  b <- 
#> 6      b
#> 7      b
#> 8     <-
#> 9      3
#> 10     3
styler:::get_parse_data("a  = b = 3")
#>    line1 col1 line2 col2 id parent                  token terminal       text
#> 1      1    1     1   10 14      0 expr_or_assign_or_help    FALSE a  = b = 3
#> 2      1    1     1    1  1      3                 SYMBOL     TRUE          a
#> 3      1    1     1    1  3     14                   expr    FALSE          a
#> 4      1    4     1    4  2     14              EQ_ASSIGN     TRUE          =
#> 5      1    6     1   10 13     14 expr_or_assign_or_help    FALSE      b = 3
#> 6      1    6     1    6  5      7                 SYMBOL     TRUE          b
#> 7      1    6     1    6  7     13                   expr    FALSE          b
#> 8      1    8     1    8  6     13              EQ_ASSIGN     TRUE          =
#> 9      1   10     1   10  9     10              NUM_CONST     TRUE          3
#> 10     1   10     1   10 10     13                   expr    FALSE          3
#>    pos_id short
#> 1       1 a  = 
#> 2       2     a
#> 3       3     a
#> 4       4     =
#> 5       5 b = 3
#> 6       6     b
#> 7       7     b
#> 8       8     =
#> 9       9     3
#> 10     10     3
styler:::get_parse_data(
  "x = 5
  if(x >= 5)
  y = TRUE else
  y = FALSE",
)
#>    line1 col1 line2 col2 id parent                  token terminal
#> 1      1    1     1    5  9      0 expr_or_assign_or_help    FALSE
#> 2      1    1     1    1  1      3                 SYMBOL     TRUE
#> 3      1    1     1    1  3      9                   expr    FALSE
#> 4      1    3     1    3  2      9              EQ_ASSIGN     TRUE
#> 5      1    5     1    5  5      6              NUM_CONST     TRUE
#> 6      1    5     1    5  6      9                   expr    FALSE
#> 7      2    3     4   11 42      0                   expr    FALSE
#> 8      2    3     2    4 11     42                     IF     TRUE
#> 9      2    5     2    5 12     42                    '('     TRUE
#> 10     2    6     2   11 19     42                   expr    FALSE
#> 11     2    6     2    6 13     15                 SYMBOL     TRUE
#> 12     2    6     2    6 15     19                   expr    FALSE
#> 13     2    8     2    9 14     19                     GE     TRUE
#> 14     2   11     2   11 16     17              NUM_CONST     TRUE
#> 15     2   11     2   11 17     19                   expr    FALSE
#> 16     2   12     2   12 18     42                    ')'     TRUE
#> 17     3    3     3   10 31     42 expr_or_assign_or_help    FALSE
#> 18     3    3     3    3 23     25                 SYMBOL     TRUE
#> 19     3    3     3    3 25     31                   expr    FALSE
#> 20     3    5     3    5 24     31              EQ_ASSIGN     TRUE
#> 21     3    7     3   10 27     28              NUM_CONST     TRUE
#> 22     3    7     3   10 28     31                   expr    FALSE
#> 23     3   12     3   15 29     42                   ELSE     TRUE
#> 24     4    3     4   11 41     42 expr_or_assign_or_help    FALSE
#> 25     4    3     4    3 33     35                 SYMBOL     TRUE
#> 26     4    3     4    3 35     41                   expr    FALSE
#> 27     4    5     4    5 34     41              EQ_ASSIGN     TRUE
#> 28     4    7     4   11 37     38              NUM_CONST     TRUE
#> 29     4    7     4   11 38     41                   expr    FALSE
#>                                        text pos_id short
#> 1                                     x = 5      1 x = 5
#> 2                                         x      2     x
#> 3                                         x      3     x
#> 4                                         =      4     =
#> 5                                         5      5     5
#> 6                                         5      6     5
#> 7  if(x >= 5)\n  y = TRUE else\n  y = FALSE      7 if(x 
#> 8                                        if      8    if
#> 9                                         (      9     (
#> 10                                   x >= 5     10 x >= 
#> 11                                        x     11     x
#> 12                                        x     12     x
#> 13                                       >=     13    >=
#> 14                                        5     14     5
#> 15                                        5     15     5
#> 16                                        )     16     )
#> 17                                 y = TRUE     17 y = T
#> 18                                        y     18     y
#> 19                                        y     19     y
#> 20                                        =     20     =
#> 21                                     TRUE     21  TRUE
#> 22                                     TRUE     22  TRUE
#> 23                                     else     23  else
#> 24                                y = FALSE     24 y = F
#> 25                                        y     25     y
#> 26                                        y     26     y
#> 27                                        =     27     =
#> 28                                    FALSE     28 FALSE
#> 29                                    FALSE     29 FALSE
styler:::get_parse_data(
  "x <- 5
  if(x >= 5)
  y <- TRUE else
  y <- FALSE",
)
#>    line1 col1 line2 col2 id parent       token terminal
#> 1      1    1     1    6  7      0        expr    FALSE
#> 2      1    1     1    1  1      3      SYMBOL     TRUE
#> 3      1    1     1    1  3      7        expr    FALSE
#> 4      1    3     1    4  2      7 LEFT_ASSIGN     TRUE
#> 5      1    6     1    6  4      5   NUM_CONST     TRUE
#> 6      1    6     1    6  5      7        expr    FALSE
#> 7      2    3     4   12 39      0        expr    FALSE
#> 8      2    3     2    4 10     39          IF     TRUE
#> 9      2    5     2    5 11     39         '('     TRUE
#> 10     2    6     2   11 18     39        expr    FALSE
#> 11     2    6     2    6 12     14      SYMBOL     TRUE
#> 12     2    6     2    6 14     18        expr    FALSE
#> 13     2    8     2    9 13     18          GE     TRUE
#> 14     2   11     2   11 15     16   NUM_CONST     TRUE
#> 15     2   11     2   11 16     18        expr    FALSE
#> 16     2   12     2   12 17     39         ')'     TRUE
#> 17     3    3     3   11 28     39        expr    FALSE
#> 18     3    3     3    3 22     24      SYMBOL     TRUE
#> 19     3    3     3    3 24     28        expr    FALSE
#> 20     3    5     3    6 23     28 LEFT_ASSIGN     TRUE
#> 21     3    8     3   11 25     26   NUM_CONST     TRUE
#> 22     3    8     3   11 26     28        expr    FALSE
#> 23     3   13     3   16 27     39        ELSE     TRUE
#> 24     4    3     4   12 37     39        expr    FALSE
#> 25     4    3     4    3 31     33      SYMBOL     TRUE
#> 26     4    3     4    3 33     37        expr    FALSE
#> 27     4    5     4    6 32     37 LEFT_ASSIGN     TRUE
#> 28     4    8     4   12 34     35   NUM_CONST     TRUE
#> 29     4    8     4   12 35     37        expr    FALSE
#>                                          text pos_id short
#> 1                                      x <- 5      1 x <- 
#> 2                                           x      2     x
#> 3                                           x      3     x
#> 4                                          <-      4    <-
#> 5                                           5      5     5
#> 6                                           5      6     5
#> 7  if(x >= 5)\n  y <- TRUE else\n  y <- FALSE      7 if(x 
#> 8                                          if      8    if
#> 9                                           (      9     (
#> 10                                     x >= 5     10 x >= 
#> 11                                          x     11     x
#> 12                                          x     12     x
#> 13                                         >=     13    >=
#> 14                                          5     14     5
#> 15                                          5     15     5
#> 16                                          )     16     )
#> 17                                  y <- TRUE     17 y <- 
#> 18                                          y     18     y
#> 19                                          y     19     y
#> 20                                         <-     20    <-
#> 21                                       TRUE     21  TRUE
#> 22                                       TRUE     22  TRUE
#> 23                                       else     23  else
#> 24                                 y <- FALSE     24 y <- 
#> 25                                          y     25     y
#> 26                                          y     26     y
#> 27                                         <-     27    <-
#> 28                                      FALSE     28 FALSE
#> 29                                      FALSE     29 FALSE