Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions R/match_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ alt_extractor <- function(x) {
#'
#' @param label The label of the key to parse.
#' @param regex A regex pattern that the key's value should match.
#' @return A parser that matches zero or more occurrences of `_<label>-<id>`
#' @return A parser that matches zero or one occurrence of `_<label>-<id>`
#' @keywords internal
optional_key <- function(label, regex = "[A-Za-z0-9]+") {
if (!is.character(label) || length(label) != 1) {
Expand All @@ -32,7 +32,8 @@ optional_key <- function(label, regex = "[A-Za-z0-9]+") {
pSeq(
function(value) { value[[4]]$value },
pLiteral("_"), pLiteral(label), pLiteral("-"), pRegex("id", regex)
)
),
max = 1
)
}

Expand All @@ -56,7 +57,8 @@ optional_literal <- function(lit, label) {
pSeq(
function(value) { value[[2]][[1]][[1]] },
pLiteral("_"), pLiteral(lit)
)
),
max = 1
)
}

Expand Down Expand Up @@ -138,7 +140,8 @@ zero_or_one_of <- function(labels, label) {
function(value) { value[[2]][[1]] },
pLiteral("_"),
do.call(pAlt, c(literals, tag = function(x) x))
)
),
max = 1
)
}

Expand Down
2 changes: 1 addition & 1 deletion man/optional_key.Rd

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

4 changes: 4 additions & 0 deletions tests/testthat/test_parse.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ test_that("can parse various file types", {
expect_type(encode("sub-sid000009_task-movie_run-03_space-MNI152Lin_res-native_desc-preproc_bold.nii.gz"), "list")
})

test_that("repeated tokens are not allowed", {
expect_null(encode("sub-01_task-rest_bold_bold.nii.gz"))
})


# sub-2001_T1w_brainmask.nii.gz sub-2001_T1w_smoothwm.R.surf.gii
# sub-2001_T1w_class-CSF_probtissue.nii.gz sub-2001_T1w_space-MNI152NLin2009cAsym_brainmask.nii.gz
Expand Down