Skip to content

Commit fd32945

Browse files
validate parse table for R < 3.2
validate parse table for R < 3.2 as parsing problems were found on travis with R 3.1 https://travis-ci.org/r-lib/styler/builds/361687013
1 parent 0195f3f commit fd32945

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

R/parse.R

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ add_id_and_short <- function(pd) {
6060
#' for potential reparsing.
6161
#' @keywords internal
6262
ensure_correct_str_txt <- function(pd, text) {
63+
ensure_valid_pd(pd)
6364
is_problematic_string <- identify_insufficiently_parsed_stings(pd, text)
6465
problematic_strings <- pd[is_problematic_string, ]
6566
is_parent_of_problematic_string <-
@@ -101,6 +102,30 @@ ensure_correct_str_txt <- function(pd, text) {
101102
arrange(pos_id)
102103
}
103104

105+
#' Ensure that the parse data is valid
106+
#'
107+
#' Test whether all non-termnals have at least one child and throw an error
108+
#' otherwise. As this is check is rather expensive, it is only
109+
#' carried out for configurations we have good reasons to expect problems.
110+
#' @param pd A parse table.
111+
ensure_valid_pd <- function(pd) {
112+
if (getRversion() < "3.2") {
113+
non_terminals <- pd %>%
114+
filter(terminal == FALSE)
115+
valid_pd <- non_terminals$id %>%
116+
map_lgl(~ .x %in% pd$parent) %>%
117+
all()
118+
if (!valid_pd) {
119+
stop(paste(
120+
"The parse data is not valid and the problem is most likely related",
121+
"to the parser in base R. Please install R >= 3.2 and try again.",
122+
call. = FALSE
123+
))
124+
}
125+
}
126+
TRUE
127+
}
128+
104129
#' Indentify strings that were not fully parsed
105130
#'
106131
#' Indentifies strings that were not fully parsed due to their vast length.

man/ensure_valid_pd.Rd

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

0 commit comments

Comments
 (0)