-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#' The `.parcr` environment is used for storing and retrieving variables by the | ||
#' user. | ||
#' @returns \value{None} | ||
#' @noRd | ||
.parcr <- list2env(list(), parent = emptyenv()) | ||
|
||
#' Store and retrieve objects | ||
#' | ||
#' Sometimes you want to use a parsed object to modify a later parser operation, | ||
#' as in the example below. The `store()` and `retrieve()` functions provide the | ||
#' tools to create such a parser. | ||
#' | ||
#' @param name a string used as the name of the stored object. | ||
#' @param value object to be stored. | ||
#' | ||
#' @returns \value{None} for `store()` and the stored object for `retrieve()`. | ||
#' @export | ||
#' | ||
#' @examples | ||
#' parse_nr <- function(line) { | ||
#' m <- stringr::str_extract(line, "number=(\\d+)", group=1) | ||
#' if (is.na(m)) list() | ||
#' else store("nr", as.numeric(m)) | ||
#' } | ||
#' | ||
#' p <- function() { | ||
#' match_s(parse_nr) %then% | ||
#' exactly(retrieve("nr"), literal("A")) | ||
#' } | ||
#' | ||
#' p()(c("number=3", "A", "A", "A")) # success | ||
#' p()(c("number=2", "A", "A", "A")) # failure | ||
store <- function(name, value) { | ||
.parcr[[name]] <- value | ||
} | ||
|
||
#' Retrieve an object | ||
#' | ||
#' @export | ||
#' @rdname store | ||
#' | ||
retrieve <- function(name) { | ||
.parcr[[name]] | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
test_that("storage and retrieval basics work", { | ||
expect_no_error(store("a", 1)) | ||
expect_equal(retrieve("a"), 1) | ||
}) | ||
|
||
test_that("storage and retrieval in a parser works", { | ||
parse_nr <- function(line) { | ||
m <- stringr::str_extract(line, "NUMBER=(\\d+)", group=1) | ||
if (is.na(m)) list() | ||
else { | ||
N <- as.numeric(m) | ||
store("N",N) | ||
} | ||
} | ||
p <- function() {match_s(parse_nr) %then% exactly(retrieve("N"), literal("A"))} | ||
expect_equal((p()(c("NUMBER=3", "A", "A", "A")))[["R"]], character(0)) | ||
}) |