Class Parser.OrEmpty
- All Implemented Interfaces:
Production<T>
Besides between() and followedBy(), the
invalid reference
sequence()Parser.followedBy(Parser.OrEmpty) methods can be used to specify that a Parser.OrEmpty
production 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 parser enclosed betweenprefixandsuffix, both allowed to be empty.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<?> suffix) The current production must be followed by non-emptysuffix.followedBy(Parser<S>.OrEmpty suffix) The current optional (or zero-or-more) parser may optionally be followed bysuffix.booleanReturns true if this parser matches the entirety of theinput, or if the input is empty.notEmpty()Returns the otherwise equivalentParserthat will fail instead of returning the default value if empty.optionallyFollowedBy(Parser<S> suffix, BiFunction<? super T, ? super S, ? extends T> op) If this parser matches, optionally matchessuffixwith theopBiFunction to transform the current parser's result.optionallyFollowedBy(String suffix) The current optional (or zero-or-more) parser may optionally be followed bysuffix.optionallyFollowedBy(String suffix, Function<? super T, ? extends T> op) If this parser matches, optionally applies theopfunction if the pattern is followed bysuffix.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.<S> Parser<S> After matching the current production, proceed to matchsuffix.After matching the current optional (or zero-or-more) parser, proceed to matchsuffix.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.google.common.labs.parse.Production
between, between, between, between, followedBy, immediatelyBetween
-
Method Details
-
between
-
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
Description copied from interface:ProductionAfter matching the current production, proceed to matchsuffix.- Specified by:
thenin interfaceProduction<T>
-
then
-
followedBy
Description copied from interface:ProductionThe current production must be followed by non-emptysuffix.- Specified by:
followedByin interfaceProduction<T>
-
followedBy
The current optional (or zero-or-more) parser may optionally be followed bysuffix.- Specified by:
followedByin interfaceProduction<T>
-
optionallyFollowedBy
The current optional (or zero-or-more) parser may optionally be followed bysuffix.- Specified by:
optionallyFollowedByin interfaceProduction<T>- Since:
- 9.5
-
optionallyFollowedBy
public final Parser<T>.OrEmpty optionallyFollowedBy(String suffix, Function<? super T, ? extends T> op) If this parser matches, optionally applies theopfunction if the pattern is followed bysuffix.- Specified by:
optionallyFollowedByin interfaceProduction<T>- Since:
- 10.0
-
optionallyFollowedBy
public final <S> Parser<T>.OrEmpty optionallyFollowedBy(Parser<S> suffix, BiFunction<? super T, ? super S, ? extends T> op) If this parser matches, optionally matchessuffixwith theopBiFunction to transform the current parser's result.- Specified by:
optionallyFollowedByin interfaceProduction<T>- Since:
- 10.0
-
notEmpty
-
parse
Parses the entire input string and returns the result; if input is empty, returns the default empty value.- Specified by:
parsein interfaceProduction<T>
-
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.- Specified by:
parseSkippingin interfaceProduction<T>
-
parseSkipping
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.- Specified by:
parseSkippingin interfaceProduction<T>
-
matches
Returns true if this parser matches the entirety of theinput, or if the input is empty. It's similar to the regexMatcher.matches(String)method.- Specified by:
matchesin interfaceProduction<T>- Since:
- 9.9.1
-