Class Parser.Rule<T>
- All Implemented Interfaces:
Production<T>
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.
To prevent StackOverflowError, rules enforce a maximum recursion depth limit. The recursion depth is tracked globally per parse operation across all recursive rules on the call stack. Each rule enforces its own limit against this global depth.
-
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, anyOf, anyOf, atLeastOnce, atLeastOnce, atLeastOnce, atLeastOnceDelimitedBy, atLeastOnceDelimitedBy, atLeastOnceDelimitedBy, atLeastOnceDelimitedBy, between, bmpCodeUnit, caseInsensitive, caseInsensitiveWord, chars, consecutive, consecutive, consecutive, define, digits, digits, first, flatMap, followedBy, followedBy, followedByOrEof, hexDigits, isPrefixOf, literally, literally, map, mapWithIndex, matches, nestedBy, nestedByWithEscapes, notFollowedBy, notFollowedBy, notFollowedByEof, notImmediatelyFollowedBy, one, one, one, one, optional, optionallyFollowedBy, optionallyFollowedBy, optionallyFollowedBy, optionallyFollowedBy, or, or, or, orElse, parse, parse, parseSkipping, parseSkipping, parseToStream, parseToStream, parseToStream, probe, probe, probe, quotedBy, quotedBy, quotedByWithEscapes, quotedByWithEscapes, sequence, sequence, sequence, sequence, sequence, sequence, sequence, skipping, skipping, source, string, suchThat, then, then, thenReturn, withPostfixes, withPostfixes, withPostfixes, withPrefixes, word, word, zeroOrMore, zeroOrMore, zeroOrMore, zeroOrMore, zeroOrMore, zeroOrMoreDelimited, zeroOrMoreDelimitedBy, zeroOrMoreDelimitedBy, zeroOrMoreDelimitedByMethods 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
-
Constructor Details
-
Rule
public Rule()Creates a rule with a default maximum recursion depth of 100. -
Rule
public Rule(int maxRecursionDepth) Creates a rule with the given maximum recursion depth.The recursion depth is tracked globally per parse operation across all recursive rules on the call stack.
- Throws:
IllegalArgumentException- ifmaxRecursionDepthis not positive.- Since:
- 10.6
-
-
Method Details
-
definedAs
-