1
1
# ' Visit'em all
2
2
# '
3
3
# ' Apply a list of functions to each level in a nested parse table.
4
- # ' `pre_visit()` applies `funs` before it proceeds to the children,
5
- # ' (that is, starts from the outermost level of nesting progressing
6
- # ' to the innermost level), `post_visit()` proceeds to its children
7
- # ' before applying the functions (meaning it first applies the functions
8
- # ' to the innermost level of nesting first and then going outwards).
4
+ # ' `pre_visit()` applies `funs` before it proceeds to the children,
5
+ # ' (that is, starts from the outermost level of nesting progressing
6
+ # ' to the innermost level), `post_visit()` proceeds to its children
7
+ # ' before applying the functions (meaning it first applies the functions
8
+ # ' to the innermost level of nesting first and then going outwards).
9
9
# ' @param pd_nested A nested parse table.
10
10
# ' @inheritParams visit_one
11
11
# ' @family visitors
@@ -46,13 +46,12 @@ visit_one <- function(pd_flat, funs) {
46
46
)
47
47
}
48
48
49
-
50
49
# ' Propagate context to terminals
51
50
# '
52
51
# ' Implements a very specific pre-visiting scheme, namely to propagate
53
- # ' indention, spaces and lag_newlines to inner token to terminals. This means
54
- # ' that information regarding indention, line breaks and spaces (which is
55
- # ' relative in `pd_nested`) will be converted into absolute.
52
+ # ' indention, spaces and lag_newlines to inner token to terminals. This means
53
+ # ' that information regarding indention, line breaks and spaces (which is
54
+ # ' relative in `pd_nested`) will be converted into absolute.
56
55
# ' @inherit context_towards_terminals
57
56
# ' @seealso context_towards_terminals visitors
58
57
# ' @importFrom purrr pmap
@@ -80,14 +79,13 @@ context_to_terminals <- function(pd_nested,
80
79
pd_transformed
81
80
}
82
81
83
-
84
82
# ' Update the a parse table given outer context
85
83
# '
86
84
# ' `outer_lag_newlines` are added to the first token in `pd`,
87
- # ' `outer_indent` is added to all tokens in `pd`, `outer_spaces` is added to
88
- # ' the last token in `pd`. [context_to_terminals()] calls this function
89
- # ' repeatedly, which means the propagation of the parse information to the
90
- # ' terminal tokens.
85
+ # ' `outer_indent` is added to all tokens in `pd`, `outer_spaces` is added to
86
+ # ' the last token in `pd`. [context_to_terminals()] calls this function
87
+ # ' repeatedly, which means the propagation of the parse information to the
88
+ # ' terminal tokens.
91
89
# ' @param pd_nested A nested parse table.
92
90
# ' @param outer_lag_newlines The lag_newlines to be propagated inwards.
93
91
# ' @param outer_indent The indention depth to be propagated inwards.
@@ -113,30 +111,30 @@ context_towards_terminals <- function(pd_nested,
113
111
# ' Extract terminal tokens
114
112
# '
115
113
# ' Turns a nested parse table into a flat parse table and extracts *all*
116
- # ' attributes
114
+ # ' attributes.
117
115
# ' @param pd_nested A nested parse table.
118
116
extract_terminals <- function (pd_nested ) {
119
117
if (is.null(pd_nested )) return (pd )
120
118
pd_split <- split(pd_nested , seq_len(nrow(pd_nested )))
121
119
bind_rows(if_else(pd_nested $ terminal , pd_split , pd_nested $ child ))
122
120
}
123
121
124
-
125
122
# ' Enrich flattened parse table
126
123
# '
127
124
# ' Enriches a flattened parse table with terminals only. In particular, it is
128
- # ' possible to compute the exact position a token will have (line and column)
129
- # ' when it will be serialized.
130
- # ' @details Since we have only terminal tokens now, the line on which a token
131
- # ' starts we also be the line on which it ends. We call `line1` the line on
132
- # ' which the token starts. `line1` has the same meaning as `line1` that can be
133
- # ' found in a flat parse table (see [tokenize()]), just that the `line1`
134
- # ' created by `enrich_terminals()` is the updated version of the former
135
- # ' `line1`. The same applies for `col1` and `col2`. Note that this function
136
- # ' does remove the columns `indent` and `spaces.` All information of the former
137
- # ' is stored in `lag_spaces` now. The later was removed because it is redundant
138
- # ' after adding the column `lag_spaces`, which is more convenient to work with,
139
- # ' in particular when serializing the parse table.
125
+ # ' possible to compute the exact position a token will have (line and column)
126
+ # ' when it will be serialized.
127
+ # ' @details
128
+ # ' Since we have only terminal tokens now, the line on which a token
129
+ # ' starts we also be the line on which it ends. We call `line1` the line on
130
+ # ' which the token starts. `line1` has the same meaning as `line1` that can be
131
+ # ' found in a flat parse table (see [tokenize()]), just that the `line1`
132
+ # ' created by `enrich_terminals()` is the updated version of the former
133
+ # ' `line1`. The same applies for `col1` and `col2`. Note that this function
134
+ # ' does remove the columns `indent` and `spaces.` All information of the former
135
+ # ' is stored in `lag_spaces` now. The later was removed because it is redundant
136
+ # ' after adding the column `lag_spaces`, which is more convenient to work with,
137
+ # ' in particular when serializing the parse table.
140
138
# ' @inheritParams choose_indention
141
139
enrich_terminals <- function (flattened_pd , use_raw_indention = FALSE ) {
142
140
flattened_pd $ lag_spaces <- lag(flattened_pd $ spaces , default = 0L )
@@ -162,18 +160,17 @@ enrich_terminals <- function(flattened_pd, use_raw_indention = FALSE) {
162
160
# ' Choose the indention method for the tokens
163
161
# '
164
162
# ' Either use the raw indention, which is just the spaces computed between
165
- # ' the first token on a new line and the token before it, or use the indention
166
- # ' computed according to the transformer used, which is stored in the column
167
- # ' `indention`.
168
- # '
169
- # ' All indention information will be combined with the space information for
170
- # ' the first token on a new line.
171
- # ' If `use_raw_indention` is set, information in the column `indention` will
172
- # ' be discarded anyways. If it is not set, the first token on a new line will
173
- # ' "inherit" the indention of the whole line.
174
- # ' The column `indention` will be removed since all information necessary is
175
- # ' contained in the spacing information of the first token on a new line and
176
- # ' the position of the tokens will not be changed anymore at this stage.
163
+ # ' the first token on a new line and the token before it, or use the indention
164
+ # ' computed according to the transformer used, which is stored in the column
165
+ # ' `indention`.
166
+ # ' All indention information will be combined with the space information for
167
+ # ' the first token on a new line.
168
+ # ' If `use_raw_indention` is set, information in the column `indention` will
169
+ # ' be discarded anyways. If it is not set, the first token on a new line will
170
+ # ' "inherit" the indention of the whole line.
171
+ # ' The column `indention` will be removed since all information necessary is
172
+ # ' contained in the spacing information of the first token on a new line and
173
+ # ' the position of the tokens will not be changed anymore at this stage.
177
174
# ' @param flattened_pd A nested parse table that was turned into a flat parse
178
175
# ' table using [extract_terminals()].
179
176
# ' @param use_raw_indention Boolean indicating whether or not the raw indention
0 commit comments