diff --git a/R/dplyr.R b/R/dplyr.R index b8d9e38f5..488e7f3e8 100644 --- a/R/dplyr.R +++ b/R/dplyr.R @@ -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 diff --git a/R/initialize.R b/R/initialize.R index ee7fd3393..54dbf276d 100644 --- a/R/initialize.R +++ b/R/initialize.R @@ -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() %>% @@ -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 } diff --git a/R/rules-other.R b/R/rules-other.R index b7a06a228..a413f2133 100644 --- a/R/rules-other.R +++ b/R/rules-other.R @@ -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) diff --git a/R/token-create.R b/R/token-create.R index c7b3ccfef..12e45af00 100644 --- a/R/token-create.R +++ b/R/token-create.R @@ -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, @@ -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, @@ -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 @@ -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) diff --git a/man/apply_transformers.Rd b/man/apply_transformers.Rd index 2acc6819c..22e1b53bd 100644 --- a/man/apply_transformers.Rd +++ b/man/apply_transformers.Rd @@ -12,11 +12,21 @@ apply_transformers(pd_nested, transformers) \item{transformers}{A list of \emph{named} transformer functions} } \description{ -Depending on whether \code{transformers} contains functions to modify the -line break information, the column \code{multi_line} is updated (after -the line break information is modified) and -the rest of the transformers is applied afterwards, or (if line break -information is not to be modified), all transformers are applied in one -step. The former requires two pre visits and one post visit, the latter -only one pre visit. +The column \code{multi_line} is updated (after the line break information is +modified) and the rest of the transformers are applied afterwards, +The former requires two pre visits and one post visit. +} +\details{ +The order of the transformations is: +\itemize{ +\item Initialization (must be first). +\item Line breaks (must be before spacing due to indention). +\item Update of newline and multi-line attributes (must not change afterwards, +hence line breaks must be modified first). +\item spacing rules (must be after line-breaks and updating newlines and +multi-line). +\item token manipulation / replacement (is last since adding and removing tokens +will invalidate columns token_after and token_before). +\item Update indention reference (must be after line breaks). +} } diff --git a/man/create_parent_id.Rd b/man/create_parent_id.Rd deleted file mode 100644 index 4e329d95f..000000000 --- a/man/create_parent_id.Rd +++ /dev/null @@ -1,15 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/token-create.R -\name{create_parent_id} -\alias{create_parent_id} -\title{Create the parent id for a new token} -\usage{ -create_parent_id(pd_nested) -} -\arguments{ -\item{pd_nested}{A nested parse table.} -} -\description{ -Just wraps \code{\link[=find_parent]{find_parent()}}, since it can also be used to obtain the parent id -that is needed to create a new token in this nest. -} diff --git a/man/create_tokens.Rd b/man/create_tokens.Rd index aff62f8aa..48bbee6c5 100644 --- a/man/create_tokens.Rd +++ b/man/create_tokens.Rd @@ -4,7 +4,7 @@ \alias{create_tokens} \title{Create a terminal token} \usage{ -create_tokens(tokens, texts, lag_newlines = 0, spaces = 0, pos_ids, parents, +create_tokens(tokens, texts, lag_newlines = 0, spaces = 0, pos_ids, token_before = NA, token_after = NA, indention_ref_ids = NA, indents = 0, terminal = TRUE, child = NULL) } @@ -21,9 +21,6 @@ tokens.} \item{pos_ids}{Character vector with positional id corresponding to the tokens.} -\item{parents}{Vector with \code{id} corresponding to the -parent of the tokens we want to create.} - \item{token_before}{Character vector corresponding to the columns \code{token_before}.} @@ -39,6 +36,9 @@ corresponding to the tokens.} not.} \item{child}{The children of the tokens.} + +\item{parents}{Vector with \code{id} corresponding to the +parent of the tokens we want to create.} } \description{ Creates a terminal token represented as (a row of) a parse table. diff --git a/man/find_parent.Rd b/man/find_parent.Rd deleted file mode 100644 index 9bd832aae..000000000 --- a/man/find_parent.Rd +++ /dev/null @@ -1,15 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/token-create.R -\name{find_parent} -\alias{find_parent} -\title{Find the parent of a nest} -\usage{ -find_parent(pd_nested) -} -\arguments{ -\item{pd_nested}{A nested parse table.} -} -\description{ -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. -} diff --git a/tests/testthat/test-create_token.R b/tests/testthat/test-create_token.R index 2eea633a6..d733e592b 100644 --- a/tests/testthat/test-create_token.R +++ b/tests/testthat/test-create_token.R @@ -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 @@ -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))