Class Parser.Rule<T>
java.lang.Object
com.google.common.labs.parse.Parser<T>
com.google.common.labs.parse.Parser.Rule<T>
- All Implemented Interfaces:
Production<T>
A forward-declared production 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, anyOf, atLeastOnce, atLeastOnce, atLeastOnce, atLeastOnceDelimitedBy, atLeastOnceDelimitedBy, atLeastOnceDelimitedBy, atLeastOnceDelimitedBy, between, bmpCodeUnit, caseInsensitive, caseInsensitiveWord, chars, consecutive, consecutive, define, digits, first, flatMap, followedBy, followedBy, followedByOrEof, isPrefixOf, literally, literally, map, matches, notFollowedBy, notFollowedBy, notImmediatelyFollowedBy, one, one, one, optional, 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, skipping, skipping, source, string, suchThat, then, then, thenReturn, withPostfixes, withPostfixes, withPostfixes, withPrefixes, word, word, 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()
-
-
Method Details
-
definedAs
-