Class Parser.OrEmpty
Besides between()
and followedBy()
, the sequence()
and Parser.followedBy(Parser.OrEmpty)
methods can be used to specify that a Parser.OrEmpty
grammar rule follows a regular consuming Parser
.
The following is a simplified example of parsing a CSV line: a comma-separated list of
fields with an optional trailing newline. The field values can be empty; empty line results in
empty list []
, not [""]
:
Parser<String> field = consecutive(noneOf(",\n"));
Parser<?> newline = string("\n");
Parser<List<String>> csvRow =
anyOf(
newline.thenReturn(List.of()), // empty line -> []
field
.orElse("") // empty field is ok
.delimitedBy(",") // comma-separated
.followedBy(newline.optional()) // optional trailing newline
.notEmpty()); // fail if the line is completely empty
In addition, the parse(java.lang.String)
convenience method is provided to parse potentially-empty
input in this one stop shop without having to remember to check for emptiness, because this
class already knows the default value to use when the input is empty.
-
Method Summary
Modifier and TypeMethodDescriptionThe current optional (or zero-or-more) parser must be enclosed between non-emptyprefix
andsuffix
.The current optional (or zero-or-more) parser must be enclosed between non-emptyprefix
andsuffix
.delimitedBy
(String delimiter) The current optional parser repeated and delimited bydelimiter
.delimitedBy
(String delimiter, Collector<? super T, ?, R> collector) The current optional parser repeated and delimited bydelimiter
.followedBy
(Parser<S>.OrEmpty suffix) The current optional (or zero-or-more) parser may optionally be followed bysuffix
.followedBy
(String suffix) The current optional (or zero-or-more) parser must be followed by non-emptysuffix
.notEmpty()
Returns the otherwise equivalentParser
that will fail instead of returning the default value if empty.Parses the entire input string and returns the result; if input is empty, returns the default empty value.After matching the current optional (or zero-or-more) parser, proceed to matchsuffix
.
-
Method Details
-
between
-
between
-
then
-
followedBy
The current optional (or zero-or-more) parser must be followed by non-emptysuffix
.Note that there is no
after()
, but you can usesequence()
andParser.followedBy(Parser.OrEmpty)
to specify that aParser.OrEmpty
grammar rule follows a regular consumingParser
. -
followedBy
-
delimitedBy
The current optional parser repeated and delimited bydelimiter
. Since this is an optional parser, at least one value is guaranteed to be collected by the providedcollector
, even if match failed. That is, on match failure, the default value (e.g. fromorElse()
) will be used.Note that it's different from
Parser.zeroOrMoreDelimitedBy(java.lang.String)
, which may produce empty list, but each element is guaranteed to be non-empty. -
delimitedBy
The current optional parser repeated and delimited bydelimiter
. Since this is an optional parser, at least one element is guaranteed to be returned, even if match failed. For example,consecutive(WORD).orElse("").delimitedBy(",")
willparse
input",a,"
asList.of("", "a", "")
; and parse empty input""
asList.of("")
.Note that it's different from
Parser.zeroOrMoreDelimitedBy(java.lang.String)
, which may produce empty list, but each element is guaranteed to be non-empty. -
notEmpty
-
parse
-