T
- public class Rule<T> extends Parser<T>
Rule
parser has a very simple reason for existence: in
order to construct recursive grammars, a parser needs to reference
itself. But the normal parser construction routines don't allow that to be
done. An alternative to this design would be to allow the modification of
the sub-parsers of Alternative
, Sequence
,
etc. after the objects have been constructed.
The following matches a sequence of letters and digits that end in a
digit. This could be more easily accomplished, but it demonstrates how a
recursive parser can be constructed:
Rule r = new Rule();
r.set(Parser.alternative(Chset.DIGIT, Parser.sequence(Chset.ALPHA, r)));
r.parse("a0") -> matches "a0"
r.parse("a0b1") -> matches "a0b1"
r.parse("a") -> no matchParser
Constructor and Description |
---|
Rule()
Class constructor for a
null subject . |
Rule(Parser<T> subject)
Class constructor.
|
Modifier and Type | Method and Description |
---|---|
int |
parse(char[] buf,
int start,
int end,
T data)
Delegates parsing responsibilties to
subject if it is
non-null, and returns NO_MATCH otherwise. |
void |
set(Parser<T> subject)
Sets the
subject member. |
public int parse(char[] buf, int start, int end, T data)
subject
if it is
non-null, and returns NO_MATCH
otherwise.parse
in class Parser<T>
buf
- The character array to match against.start
- The start offset of data within the character array to match
against.end
- The end offset of data within the character array to match
against.data
- User defined object that is passed to
Callback.handle
when an Action
fires.Parser.parse(char[], int, int, T)