An alternation is a collection of parsers, any of which may match against an assembly. As with the Sequence class, the primary service of the Alternation class is matching, as Figure 10.5 shows.
Figure 10.5. The Alternation class. Alternation implements match() so that matching succeeds against any of the parsers in the collection of parsers it inherits from CollectionParser .
An Alternation object matches each of its subparsers against the collection of assemblies that match() receives. The Alternation object accumulates the results of each match into an output final state. Here it is in pseudocode:
out = new state; for each subparser p { out = out + p.match(in); } return out;
The actual code for alternation matching follows this design, but it walks the results of intermediate results more manually. Here is the code for Alternation.match() :
public Vector match(Vector in) { Vector out = new Vector(); Enumeration e = subparsers.elements(); while (e.hasMoreElements()) { Parser p = (Parser) e.nextElement(); add(out, p.matchAndAssemble(in)); } return out; }