Skip to content

Commit 0fd8723

Browse files
Use visitor for terminal extraction
Use visitor for terminal extraction instead of complicated and pmap / matrix construct.
1 parent 02f691d commit 0fd8723

File tree

4 files changed

+9
-73
lines changed

4 files changed

+9
-73
lines changed

R/transform.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ parse_transform_serialize <- function(text, transformers) {
8585
pd_nested <- compute_parse_data_nested(text)
8686
transformed_pd <- apply_transformers(pd_nested, transformers)
8787
# TODO verify_roundtrip
88-
flattened_pd <- extract_terminals(transformed_pd) %>%
88+
flattened_pd <- post_visit(transformed_pd, list(extract_terminals)) %>%
8989
enrich_terminals()
9090

9191
serialized_transformed_text <- serialize_parse_data_flattened(flattened_pd)

R/visit.R

Lines changed: 6 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -99,55 +99,17 @@ context_towards_terminals <- function(pd_nested,
9999

100100
#' Extract terminal tokens
101101
#'
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.
114105
#' @importFrom readr type_convert col_integer cols
115106
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) {
137107
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))
149110
}
150111

112+
151113
#' Enrich flattened parse table
152114
#'
153115
#' Enriches a flattened parse table with terminals only. In particular, it is

man/extract_terminals.Rd

Lines changed: 2 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/extract_terminals_helper.Rd

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)