Class Parser.Rule<T>
java.lang.Object
com.google.common.labs.parse.Parser<T>
com.google.common.labs.parse.Parser.Rule<T>
A forward-declared grammar rule, to be used for recursive grammars.
For example, to create a parser for a simple calculator that supports single-digit numbers, addition, and parentheses, you can write:
var rule = new Parser.Rule<Integer>();
Parser<Integer> num = Parser.single(CharPredicate.inRange('0', '9')).map(c -> c - '0');
Parser<Integer> atomic = rule.between("(", ")").or(num);
Parser<Integer> expr =
atomic.atLeastOnceDelimitedBy("+")
.map(nums -> nums.stream().mapToInt(n -> n).sum());
return rule.definedAs(expr);
For simple definitions, you could use the Parser.define(java.util.function.Function<? super com.google.common.labs.parse.Parser<T>, ? extends com.google.common.labs.parse.Parser<? extends T>>) method with a lambda
to elide the need of an explicit forward declaration.
-
Nested Class Summary
Nested classes/interfaces inherited from class com.google.common.labs.parse.Parser
Parser.Lexical, Parser.OrEmpty, Parser.ParseException, Parser.Rule<T> -
Constructor Summary
Constructors -
Method Summary
Methods inherited from class com.google.common.labs.parse.Parser
anyOf, atLeastOnce, atLeastOnce, atLeastOnce, atLeastOnceDelimitedBy, atLeastOnceDelimitedBy, atLeastOnceDelimitedBy, between, between, chars, consecutive, consecutive, define, digits, flatMap, followedBy, followedBy, followedBy, followedByOrEof, immediatelyBetween, literally, literally, map, notFollowedBy, notFollowedBy, notImmediatelyFollowedBy, optional, optionallyFollowedBy, optionallyFollowedBy, or, or, or, orElse, parse, parse, parseSkipping, parseSkipping, parseToStream, parseToStream, parseToStream, postfix, prefix, probe, probe, probe, quotedStringWithEscapes, sequence, sequence, sequence, single, skipping, skipping, source, string, suchThat, then, then, thenReturn, word, zeroOrMore, zeroOrMore, zeroOrMore, zeroOrMoreDelimited, zeroOrMoreDelimited, zeroOrMoreDelimitedBy, zeroOrMoreDelimitedBy
-
Constructor Details
-
Rule
public Rule()
-
-
Method Details
-
definedAs
-