Skip to content

Commit 26075af

Browse files
Merge pull request #151 from lorenzwalthert/rm_flat_relicts
Remove flat relicts completely (#151).
2 parents 5868c21 + a9e7146 commit 26075af

17 files changed

+511
-618
lines changed

R/parsed.R

Lines changed: 0 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,3 @@
1-
#' Parse and pre-process character vector
2-
#'
3-
#' @description The function obtains detailed parse information for `text` via
4-
#' [utils::getParseData()] and does some minimal pre-processing by calling
5-
#' [enhance_parse_data()].
6-
#' @inheritSection enhance_parse_data Details
7-
#' @param text A character vector.
8-
#' @return A pre-processed parse table.
9-
#' @details Roughly speaking, this is the inverse operation of
10-
#' [serialize_parse_data_flat()], which turns a parse table into a character
11-
#' vector, since `compute_parse_data_flat_enhanced()` turns a character vector
12-
#' into a parse table.
13-
compute_parse_data_flat_enhanced <- function(text) {
14-
parse_data <- tokenize(text)
15-
pd_flat <- enhance_parse_data(parse_data)
16-
pd_flat
17-
}
18-
19-
#' Pre-processing parse data
20-
#'
21-
#' Modifies the parse table minimally by applying some pre-processing steps.
22-
#' @section Details:
23-
#' Preprocessing includes
24-
#' * removing non-terminal entries.
25-
#' * removing columns id, parent and terminal.
26-
#' * adding a start token.
27-
#' * adding line-break and space information.
28-
#' * removing spaces in comments at the end of the line.
29-
#' @param parse_data a parse table.
30-
#' @return a pre-processed parse table.
31-
enhance_parse_data <- function(parse_data) {
32-
parse_data_filtered <-
33-
parse_data %>%
34-
filter_(~terminal) %>%
35-
select_(~-id, ~-parent) %>%
36-
arrange_(~line1, ~col1, ~line2, ~col2) %>%
37-
bind_rows(
38-
tibble(line1 = 1L, col1 = 0L, line2 = 1L, col2 = 0L,
39-
token = "START", text = ""),
40-
.
41-
)
42-
43-
parse_data_filled <-
44-
parse_data_filtered %>%
45-
create_filler()
46-
47-
parse_data_comment_eol <- parse_data_filled
48-
49-
parse_data_comment_eol$text <-
50-
if_else(parse_data_comment_eol$token == "COMMENT",
51-
gsub(" +$", "", parse_data_comment_eol$text),
52-
parse_data_comment_eol$text)
53-
54-
parse_data_comment_eol
55-
}
56-
57-
#' Verify parse data modifications
58-
#'
59-
#' @description Check whether serializing the parse data results in the same
60-
#' number of lines as the initial data that should be styled.
61-
#' @param pd_flat A parse table.
62-
#' @param text A character vector with the initial text to compare against.
63-
#' @return If the verification is successful, `pd` is returned, with empty
64-
#' lines at the end of `text` stripped. \cr
65-
#' Otherwise, an error is thrown.
66-
#' @seealso [serialize_parse_data_flat()]
67-
verify_roundtrip <- function(pd_flat, text) {
68-
roundtrip <- serialize_parse_data_flat(pd_flat)
69-
70-
if (length(roundtrip) < length(text)) {
71-
stopifnot(text[-seq_along(roundtrip)] == "")
72-
text <- text[seq_along(roundtrip)]
73-
}
74-
75-
stopifnot(identical(text, roundtrip))
76-
text
77-
}
78-
791
#' Enrich parse table with space and linebreak information
802
#'
813
#' This function computes difference (as column and line difference) between two

R/serialize.R

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,3 @@
1-
#' Serialize a nested parse table
2-
#'
3-
#' Helper function that recursively extracts terminals from a nested tibble.
4-
#' @param pd_nested A nested parse table.
5-
#' @param pass_indent Level of indention of a token.
6-
#' @return A character vector with all terminal tokens in `pd_nested` plus
7-
#' the appropriate amount of white spaces and line breaks are inserted between
8-
#' them.
9-
#' @importFrom purrr pmap
10-
serialize_parse_data_nested_helper <- function(pd_nested, pass_indent) {
11-
out <- pmap(list(pd_nested$terminal, pd_nested$text, pd_nested$child,
12-
pd_nested$spaces, pd_nested$lag_newlines, pd_nested$indent),
13-
function(terminal, text, child, spaces, lag_newlines, indent) {
14-
total_indent <- pass_indent + indent
15-
preceding_linebreak <- if_else(lag_newlines > 0, 1, 0)
16-
if (terminal) {
17-
c(add_newlines(lag_newlines),
18-
add_spaces(total_indent * preceding_linebreak),
19-
text,
20-
add_spaces(spaces))
21-
} else {
22-
c(add_newlines(lag_newlines),
23-
add_spaces(total_indent * preceding_linebreak),
24-
serialize_parse_data_nested_helper(child, total_indent),
25-
add_spaces(spaces))
26-
}
27-
}
28-
)
29-
out
30-
}
31-
32-
#' Serialize a nested parse table
33-
#'
34-
#' Collapses a nested parse table into its character vector representation.
35-
#' @param pd_nested A nested parse table with line break, spaces and indention
36-
#' information.
37-
#' @return A character string.
38-
serialize_parse_data_nested <- function(pd_nested) {
39-
out <- c(add_newlines(start_on_line(pd_nested) - 1),
40-
serialize_parse_data_nested_helper(pd_nested, pass_indent = 0)) %>%
41-
unlist() %>%
42-
paste0(collapse = "") %>%
43-
strsplit("\n", fixed = TRUE) %>%
44-
.[[1L]] %>%
45-
trimws(which = "right")
46-
out
47-
}
48-
49-
#' Serialize Flat Parse Data
50-
#'
51-
#' Collapses a parse table into character vector representation.
52-
#' @param pd_flat A parse table.
53-
#' @details
54-
#' The function essentially collapses the column text of `pd_flat`
55-
#' while taking into account space and linebreak information from the columns
56-
#' newlines and spaces. \cr
57-
#' Roughly speaking, this is the inverse operation of
58-
#' [compute_parse_data_flat_enhanced()], which turns a character vector into a
59-
#' parse table, since `serialize_parse_data_flat()` turns a parse table back
60-
#' into a character vector.
61-
serialize_parse_data_flat <- function(pd_flat) {
62-
pd_flat %>%
63-
summarize_(
64-
text_ws = ~paste0(
65-
text, newlines_and_spaces(newlines, spaces),
66-
collapse = "")) %>%
67-
.[["text_ws"]] %>%
68-
strsplit("\n", fixed = TRUE) %>%
69-
.[[1L]]
70-
}
71-
721
#' Serialize flattened parse data
732
#'
743
#' Collapses a flattened parse table into character vector representation.

R/utils.R

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,19 @@ rep_char <- function(char, times) {
99
paste(rep.int(char, times), collapse = "")
1010
}
1111

12-
#' concentrate newlines an spaces in a string
12+
#' Concentrate newlines or spaces in a string
1313
#'
14-
#' @param newlines Scalar indicating how many newlines ("\ n") should returned.
15-
#' @param spaces Scalar indicating how many spaces should be appended to the
16-
#' newlines.
14+
#' @param n Scalar indicating how many characters should be concentrated
1715
#' @return A string.
18-
#' @importFrom purrr map2
19-
newlines_and_spaces <- function(newlines, spaces) {
20-
map2(newlines, spaces, ~paste0(rep_char("\n", .x), rep_char(" ", .y)))
21-
}
16+
#' @name add_spaces_or_newlines
17+
NULL
2218

19+
#' @rdname add_spaces_or_newlines
2320
add_newlines <- function(n) {
2421
rep_char("\n", n)
2522
}
2623

24+
#' @rdname add_spaces_or_newlines
2725
add_spaces <- function(n) {
2826
rep_char(" ", n)
2927
}

R/visit.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ visit_one <- function(pd_flat, funs) {
5353
#' relative in `pd_nested`) will be converted into absolute.
5454
#' @inherit context_towards_terminals
5555
#' @seealso context_towards_terminals visitors
56+
#' @importFrom purrr pmap
5657
context_to_terminals <- function(pd_nested,
5758
outer_lag_newlines,
5859
outer_indent,

man/add_spaces_or_newlines.Rd

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

man/compute_parse_data_flat_enhanced.Rd

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

man/enhance_parse_data.Rd

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

man/newlines_and_spaces.Rd

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

man/serialize_parse_data_flat.Rd

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

man/serialize_parse_data_nested.Rd

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

0 commit comments

Comments
 (0)