| java.lang.Object org.ofbiz.rules.parse.Parser
All known Subclasses: org.ofbiz.rules.parse.CollectionParser, org.ofbiz.rules.parse.Terminal, org.ofbiz.rules.parse.Empty, org.ofbiz.rules.parse.Repetition,
Parser | abstract public class Parser (Code) | | A Parser is an object that recognizes the elements of a language.
Each Parser object is either a
Terminal or a composition of other parsers.
The Terminal class is a subclass of
Parser , and is itself a hierarchy of
parsers that recognize specific patterns of text. For
example, a Word recognizes any word, and a
Literal matches a specific string.
In addition to Terminal , other subclasses of
Parser provide composite parsers,
describing sequences, alternations, and repetitions of
other parsers. For example, the following
Parser objects culminate in a good
parser that recognizes a description of good
coffee.
Alternation adjective = new Alternation();
adjective.add(new Literal("steaming"));
adjective.add(new Literal("hot"));
Sequence good = new Sequence();
good.add(new Repetition(adjective));
good.add(new Literal("coffee"));
String s = "hot hot steaming hot coffee";
Assembly a = new TokenAssembly(s);
System.out.println(good.bestMatch(a));
This prints out:
[hot, hot, steaming, hot, coffee]
hot/hot/steaming/hot/coffee^
The parser does not match directly against a string,
it matches against an Assembly . The
resulting assembly shows its stack, with four words on it,
along with its sequence of tokens, and the index at the
end of these. In practice, parsers will do some work
on an assembly, based on the text they recognize.
author: Steven J. Metsker version: 1.0 |
Constructor Summary | |
public | Parser() Constructs a nameless parser. | public | Parser(String name) Constructs a parser with the given name.
Parameters: name - A name to be known by. |
Method Summary | |
public void | accept(ParserVisitor pv) Accepts a "visitor" which will perform some operation on
a parser structure. | abstract public void | accept(ParserVisitor pv, List visited) Accepts a "visitor" along with a collection of previously
visited parsers. | public static void | add(List v1, List v2) Adds the elements of one vector to another. | public Assembly | best(List v) Returns the most-matched assembly in a collection. | public Assembly | bestMatch(Assembly a) Returns an assembly with the greatest possible number of
elements consumed by matches of this parser. | public Assembly | completeMatch(Assembly a) Returns either null, or a completely matched version of
the supplied assembly. | public static List | elementClone(List v) Create a copy of a vector, cloning each element of
the vector. | public String | getName() Returns the name of this parser. | abstract public List | match(List in) Given a set (well, a List , really) of
assemblies, this method matches this parser against
all of them, and returns a new set (also really a
List ) of the assemblies that result from
the matches.
For example, consider matching the regular expression
a* against the string "aaab" .
The initial set of states is {^aaab} , where
the ^ indicates how far along the assembly is. | public List | matchAndAssemble(List in) Match this parser against an input state, and then
apply this parser's assembler against the resulting
state. | abstract protected List | randomExpansion(int maxDepth, int depth) Create a random expansion for this parser, where a
concatenation of the returned collection will be a
language element. | public String | randomInput(int maxDepth, String separator) Return a random element of this parser's language. | public Parser | setAssembler(Assembler assembler) Sets the object that will work on an assembly whenever
this parser successfully matches against the
assembly. | public String | toString() Returns a textual description of this parser. | protected String | toString(List visited) Returns a textual description of this parser.
Parsers can be recursive, so when building a
descriptive string, it is important to avoid infinite
recursion by keeping track of the objects already
described. | abstract protected String | unvisitedString(List visited) Returns a textual description of this string. |
assembler | protected Assembler assembler(Code) | | an object that will work on an assembly whenever this
parser successfully matches against the assembly
|
name | protected String name(Code) | | a name to identify this parser
|
Parser | public Parser()(Code) | | Constructs a nameless parser.
|
Parser | public Parser(String name)(Code) | | Constructs a parser with the given name.
Parameters: name - A name to be known by. For parsersthat are deep composites, a simple nameidentifying its purpose is useful. |
accept | public void accept(ParserVisitor pv)(Code) | | Accepts a "visitor" which will perform some operation on
a parser structure. The book, "Design Patterns", explains
the visitor pattern.
Parameters: pv - the visitor to accept |
accept | abstract public void accept(ParserVisitor pv, List visited)(Code) | | Accepts a "visitor" along with a collection of previously
visited parsers.
Parameters: pv - the visitor to accept Parameters: visited - a collection of previously visitedparsers. |
add | public static void add(List v1, List v2)(Code) | | Adds the elements of one vector to another.
Parameters: v1 - the vector to add to Parameters: v2 - the vector with elements to add |
best | public Assembly best(List v)(Code) | | Returns the most-matched assembly in a collection.
the most-matched assembly in a collection. Parameters: v - the collection to look through |
bestMatch | public Assembly bestMatch(Assembly a)(Code) | | Returns an assembly with the greatest possible number of
elements consumed by matches of this parser.
an assembly with the greatest possible number ofelements consumed by this parser Parameters: a - an assembly to match against |
completeMatch | public Assembly completeMatch(Assembly a)(Code) | | Returns either null, or a completely matched version of
the supplied assembly.
either null, or a completely matched version of thesupplied assembly Parameters: a - an assembly to match against |
elementClone | public static List elementClone(List v)(Code) | | Create a copy of a vector, cloning each element of
the vector.
Parameters: in - the vector to copy a copy of the input vector, cloning eachelement of the vector |
getName | public String getName()(Code) | | Returns the name of this parser.
the name of this parser |
match | abstract public List match(List in)(Code) | | Given a set (well, a List , really) of
assemblies, this method matches this parser against
all of them, and returns a new set (also really a
List ) of the assemblies that result from
the matches.
For example, consider matching the regular expression
a* against the string "aaab" .
The initial set of states is {^aaab} , where
the ^ indicates how far along the assembly is. When
a* matches against this initial state, it
creates a new set {^aaab, a^aab, aa^ab,
aaa^b} .
a List of assemblies that result frommatching against a beginning set of assemblies Parameters: in - a vector of assemblies to match against |
matchAndAssemble | public List matchAndAssemble(List in)(Code) | | Match this parser against an input state, and then
apply this parser's assembler against the resulting
state.
a List of assemblies that result from matchingagainst a beginning set of assemblies Parameters: in - a vector of assemblies to match against |
randomExpansion | abstract protected List randomExpansion(int maxDepth, int depth)(Code) | | Create a random expansion for this parser, where a
concatenation of the returned collection will be a
language element.
|
randomInput | public String randomInput(int maxDepth, String separator)(Code) | | Return a random element of this parser's language.
a random element of this parser's language |
setAssembler | public Parser setAssembler(Assembler assembler)(Code) | | Sets the object that will work on an assembly whenever
this parser successfully matches against the
assembly.
Parameters: Assembler - the assembler to apply Parser this |
toString | public String toString()(Code) | | Returns a textual description of this parser.
String a textual description of thisparser, taking care to avoidinfinite recursion |
toString | protected String toString(List visited)(Code) | | Returns a textual description of this parser.
Parsers can be recursive, so when building a
descriptive string, it is important to avoid infinite
recursion by keeping track of the objects already
described. This method keeps an object from printing
twice, and uses unvisitedString which
subclasses must implement.
Parameters: visited - a list of objects already printed a textual version of this parser,avoiding recursion |
unvisitedString | abstract protected String unvisitedString(List visited)(Code) | | Returns a textual description of this string.
|
|
|