@@ -99,55 +99,17 @@ context_towards_terminals <- function(pd_nested,
99
99
100
100
# ' Extract terminal tokens
101
101
# '
102
- # ' Turns a nested parse table into a flat parse table. In particular it extracts
103
- # ' terminal tokens and the following attributes:
104
- # '
105
- # ' * lag_newlines
106
- # ' * indent
107
- # ' * token
108
- # ' * text
109
- # ' * spaces
110
- # ' * id
111
- # ' * parent
112
- # ' * line1
113
- # ' @inheritParams extract_terminals_helper
102
+ # ' Turns a nested parse table into a flat parse table and extracts *all*
103
+ # ' attributes
104
+ # ' @param pd_nested A nested parse table.
114
105
# ' @importFrom readr type_convert col_integer cols
115
106
extract_terminals <- function (pd_nested ) {
116
- flat_vec <- extract_terminals_helper(pd_nested ) %> %
117
- unlist()
118
- nms <- list (
119
- NULL ,
120
- c(" lag_newlines" , " indent" , " token" , " text" , " spaces" , " id" , " parent" , " line1" )
121
- )
122
- flat_tbl <- matrix (flat_vec , ncol = length(nms [[2 ]]), byrow = TRUE , dimnames = nms ) %> %
123
- as_tibble() %> %
124
- type_convert(
125
- col_types = cols(
126
- lag_newlines = col_integer(),
127
- indent = col_integer(),
128
- spaces = col_integer()
129
- )
130
- )
131
- }
132
-
133
- # ' Helper to extract terminals
134
- # '
135
- # ' @param pd_nested A nested parse table.
136
- extract_terminals_helper <- function (pd_nested ) {
137
107
if (is.null(pd_nested )) return (pd )
138
- pmap(list (pd_nested $ terminal , pd_nested $ token , pd_nested $ text ,
139
- pd_nested $ lag_newlines , pd_nested $ spaces , pd_nested $ indent ,
140
- pd_nested $ id , pd_nested $ parent , pd_nested $ line1 , pd_nested $ child ),
141
- function (terminal , token , text , lag_newlines , spaces , indent , id ,
142
- parent , line1 , child ) {
143
- if (terminal ) {
144
- c(lag_newlines , indent , token , text , spaces , id , parent , line1 )
145
- } else {
146
- extract_terminals_helper(child )
147
- }
148
- })
108
+ pd_splitted <- split(pd_nested , seq_len(nrow(pd_nested )))
109
+ bind_rows(ifelse(pd_nested $ terminal , pd_splitted , pd_nested $ child ))
149
110
}
150
111
112
+
151
113
# ' Enrich flattened parse table
152
114
# '
153
115
# ' Enriches a flattened parse table with terminals only. In particular, it is
0 commit comments