-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathwildcards.R
59 lines (52 loc) · 1.7 KB
/
wildcards.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#' @include escape.R
#' @include utils.R
NULL
#' Wildcards
#'
#' @inheritParams capture
#' @param type the type of match to perform.
#'
#' There are three match types
#' \enumerate{
#' \item \code{greedy}: match the longest string. This is the default matching type.
#' \item \code{lazy}: match the shortest string. This matches the shortest string from the same anchor point, not necessarily the shortest global string.
#' \item \code{possessive}: match and don't allow backtracking
#' }
#' @family rex
#' @name wildcards
NULL
#' @describeIn wildcards match \code{...} zero or more times.
zero_or_more <- function(..., type = c("greedy", "lazy", "possessive")) {
add_type(p("(?:", p(escape_dots(...)), ")*"), type)
}
register(zero_or_more)
#' @describeIn wildcards match \code{...} one or more times.
one_or_more <- function(..., type = c("greedy", "lazy", "possessive")) {
add_type(p("(?:", p(escape_dots(...)), ")+"), type)
}
register(one_or_more)
#' @describeIn wildcards match \code{...} zero or one times.
#' @aliases zero_or_one
maybe <- zero_or_one <- function(..., type = c("greedy", "lazy", "possessive")) {
p("(?:", p(escape_dots(...)), ")?")
}
register(maybe, zero_or_one)
add_type <- function(x, type = c("greedy", "lazy", "possessive")) {
type <- match.arg(type)
switch(type,
greedy = x,
lazy = p(x, "?"),
possessive = p(x, "+")
)
}
#' Do not match
#'
#' @inheritParams capture
#' @inheritParams zero_or_more
#' @family rex
# This is slightly different than if_next_isn't because we want to match
# anything that is not the search term as well
not <- function(..., type = c("greedy", "lazy", "possessive")) {
add_type(p("(?:(?!", escape_dots(...), ").)*"), type = type)
}
register(not)