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
.notEmpty() // non-empty line
.followedByOrEof(newline)); // trailing newline optional on last line
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-emptyprefixandsuffix.The current optional (or zero-or-more) parser must be enclosed between non-emptyprefixandsuffix.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.immediatelyBetween(String prefix, String suffix) The current optional (or zero-or-more) parser must be immediately enclosed between non-emptyprefixandsuffix(no skippable characters as specified byparseSkipping()in between).notEmpty()Returns the otherwise equivalentParserthat 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.parseSkipping(Parser<?> skip, String input) Parses the entire input string, ignoring patterns matched byskip, and returns the result; if there's nothing to parse except skippable content, returns the default empty value.parseSkipping(CharPredicate charsToSkip, String input) Parses the entire input string, ignoringcharsToSkip, and returns the result; if there's nothing to parse except skippable content, returns the default empty value.After matching the current optional (or zero-or-more) parser, proceed to matchsuffix.
-
Method Details
-
between
-
between
-
immediatelyBetween
The current optional (or zero-or-more) parser must be immediately enclosed between non-emptyprefixandsuffix(no skippable characters as specified byparseSkipping()in between). Useful for matching a literal string, such aszeroOrMore(isNot('"')).immediatelyBetween("\"", "\""). -
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(",")willparseinput",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. -
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.OrEmptygrammar rule follows a regular consumingParser. -
followedBy
-
notEmpty
-
parse
-
parseSkipping
-
parseSkipping
Parses the entire input string, ignoringcharsToSkip, and returns the result; if there's nothing to parse except skippable content, returns the default empty value.
-