Skip to content

Commit

Permalink
Don't drag along column parent.
Browse files Browse the repository at this point in the history
Delete column parent in parse table after parse data is nested.
  • Loading branch information
lorenzwalthert committed Oct 26, 2017
1 parent 3523032 commit dbc3cda
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 82 deletions.
2 changes: 1 addition & 1 deletion R/dplyr.R
Expand Up @@ -73,7 +73,7 @@ left_join <- function(x, y, by, ...) {
by_x <- by_y <- by
}
res <- as_tibble(merge(x, y, by.x = by_x, by.y = by_y, all.x = TRUE, ...))
res <- arrange(res, line1, col1, line2, col2, parent)
res <- arrange(res, pos_id)

# dplyr::left_join set unknown list columns to NULL, merge sets them
# to NA
Expand Down
6 changes: 3 additions & 3 deletions R/initialize.R
Expand Up @@ -9,7 +9,7 @@ initialize_attributes <- function(pd_flat) {
init_pd <-
initialize_newlines(pd_flat) %>%
initialize_spaces() %>%
remove_line_col() %>%
remove_unused_attributes() %>%
initialize_multi_line() %>%
initialize_indention_ref_id() %>%
initialize_indent() %>%
Expand Down Expand Up @@ -38,8 +38,8 @@ initialize_spaces <- function(pd_flat) {
pd_flat
}

remove_line_col <- function(pd_flat) {
pd_flat[c("line1", "line2", "col1", "col2")] <- rep(list(NULL), 4)
remove_unused_attributes <- function(pd_flat) {
pd_flat[c("line1", "line2", "col1", "col2", "parent")] <- rep(list(NULL), 4)
pd_flat
}

Expand Down
2 changes: 1 addition & 1 deletion R/rules-other.R
Expand Up @@ -10,7 +10,7 @@ add_brackets_in_pipe_one <- function(pd, pos) {
new_pos_ids <- create_pos_ids(pd$child[[next_non_comment]], 1, after = TRUE, n = 2)
new_pd <- create_tokens(
tokens = c("'('", "')'"), texts = c("(", ")"), pos_ids = new_pos_ids,
lag_newlines = rep(0, 2), parents = create_parent_id(pd))
lag_newlines = rep(0, 2))
pd$child[[next_non_comment]] <-
bind_rows(pd$child[[next_non_comment]], new_pd) %>%
arrange(pos_id)
Expand Down
27 changes: 2 additions & 25 deletions R/token-create.R
Expand Up @@ -26,7 +26,6 @@ create_tokens <- function(tokens,
lag_newlines = 0,
spaces = 0,
pos_ids,
parents,
token_before = NA,
token_after = NA,
indention_ref_ids = NA,
Expand All @@ -41,7 +40,6 @@ create_tokens <- function(tokens,
lag_newlines = lag_newlines,
newlines = lead(lag_newlines),
pos_id = pos_ids,
parent = parents,
token_before = token_before,
token_after = token_after,
id = NA,
Expand Down Expand Up @@ -110,24 +108,6 @@ validate_new_pos_ids <- function(new_ids, after) {
if (any(abs(new_ids - ref) > 0.5)) stop("too many ids assigned")
}

#' Find the parent of a nest
#'
#' It is any id of the nest (nested parse table at one level
#' of nesting) since by definition, all tokens on a nest have the same id.
#' @param pd_nested A nested parse table.
find_parent <- function(pd_nested) {
pd_nested$parent[1]
}

#' Create the parent id for a new token
#'
#' Just wraps [find_parent()], since it can also be used to obtain the parent id
#' that is needed to create a new token in this nest.
#' @inheritParams find_parent
create_parent_id <- function(pd_nested) {
find_parent(pd_nested)
}

#' Wrap an expression in curly braces
#'
#' Adds curly braces to an expression (represented as a parse table) if there
Expand All @@ -146,20 +126,17 @@ wrap_expr_in_curly <- function(pd, stretch_out = FALSE) {
expr <- create_tokens(
"expr", "",
pos_ids = create_pos_ids(pd, 1, after = FALSE),
parents = create_parent_id(pd),
child = pd,
terminal = FALSE
)
opening <- create_tokens(
"'{'", "{",
pos_ids = create_pos_ids(expr, 1, after = FALSE),
parents = NA
pos_ids = create_pos_ids(pd, 1, after = FALSE)
)

closing <- create_tokens(
"'}'", "}", spaces = 1, lag_newlines = as.integer(stretch_out),
pos_ids = create_pos_ids(pd, nrow(pd), after = TRUE),
parents = NA
pos_ids = create_pos_ids(pd, nrow(pd), after = TRUE)
)

bind_rows(opening, expr, closing)
Expand Down
24 changes: 17 additions & 7 deletions man/apply_transformers.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 0 additions & 15 deletions man/create_parent_id.Rd

This file was deleted.

8 changes: 4 additions & 4 deletions man/create_tokens.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 0 additions & 15 deletions man/find_parent.Rd

This file was deleted.

19 changes: 8 additions & 11 deletions tests/testthat/test-create_token.R
Expand Up @@ -3,23 +3,22 @@ context("token insertion")
test_that("can create a token that has relevant columns", {
pd_names <- c(
"token", "text", "short", "lag_newlines", "newlines", "pos_id",
"parent", "token_before", "token_after", "id", "terminal", "internal",
"token_before", "token_after", "id", "terminal", "internal",
"spaces", "multi_line", "indention_ref_id", "indent", "child"
)

expect_equal(
names(create_tokens("'{'", "{", pos_ids = 3, parents = 99)),
names(create_tokens("'{'", "{", pos_ids = 3)),
pd_names
)
})

test_that("pos_id can be created", {
pd <- create_tokens("XZY_TEST", "test", pos_ids = 3, parents = 0)
pd <- create_tokens("XZY_TEST", "test", pos_ids = 3)
new_id <- create_pos_ids(pd, 1L, by = 0.4)
parents <- create_parent_id(pd)
expect_error(
bind_rows(
create_tokens("XZY_TEST", "test", pos_ids = new_id, parents = parents),
create_tokens("XZY_TEST", "test", pos_ids = new_id),
pd
),
NA
Expand All @@ -28,23 +27,21 @@ test_that("pos_id can be created", {


test_that("unambiguous pos_id won't be created (down)", {
pd <- create_tokens("XZY_TEST", "test", pos_ids = 3, parents = 0)
pd <- create_tokens("XZY_TEST", "test", pos_ids = 3)
new_id <- create_pos_ids(pd, 1L, by = 0.4)
parents <- create_parent_id(pd)
pd <- bind_rows(
create_tokens("XZY_TEST", "test", pos_ids = new_id, parents = parents),
create_tokens("XZY_TEST", "test", pos_ids = new_id),
pd
)
expect_error(create_pos_id(pd, 1L, by = 0.4))
})

test_that("unambiguous pos_id won't be created (up)", {
pd <- create_tokens("XZY_TEST", "test", pos_ids = 3, parents = 0)
pd <- create_tokens("XZY_TEST", "test", pos_ids = 3)
new_id <- create_pos_ids(pd, 1L, by = 0.4, after = TRUE)
parents <- create_parent_id(pd)

pd <- bind_rows(
create_tokens("XZY_TEST", "test", pos_ids = new_id, parents = parents),
create_tokens("XZY_TEST", "test", pos_ids = new_id),
pd
)
expect_error(create_pos_id(pd, 1L, by = 0.4, after = TRUE))
Expand Down

0 comments on commit dbc3cda

Please sign in to comment.