PatternMatch.mesa
Copyright Ó 1987, 1992 by Xerox Corporation. All rights reserved.
Doug Terry, July 20, 1987 5:38:35 pm PDT
Brian Oki, March 12, 1990 1:53 pm PST
A collection of pattern matching routines of general utility, and a facility for registering them and retrieving them by name.
DIRECTORY
Rope USING [ROPE];
PatternMatch: CEDAR DEFINITIONS
~ BEGIN
ROPE: TYPE ~ Rope.ROPE;
MatchProc: TYPE = PROC [value: ROPE, pattern: ROPE, pparsed: REF ¬ NIL] RETURNS [match: BOOLEAN, nothingGreater: BOOLEAN ¬ FALSE, pparsedNew: REF ¬ NIL];
Compares the presented value to the pattern; match indicates the result of the comparison. If nothingGreater=TRUE then no value lexicographically greater than or equal to the presented value could possibly match the pattern; nothingGreater=FALSE implies nothing about such comparisons.
To possibly improve performance when comparing a set of values against a single pattern, pparsedNew is returned by the initial call; this should be treated as opaque and passed as the pparsed argument to subsequent calls. This allows the cost of parsing the pattern to be amortized over several calls. Warning: a returned pparsed ref should only be passed to the same MatchProc that returned it and only if the pattern is identical.
Equal: MatchProc;
Compares the value and pattern for equality.
Prefix: MatchProc;
Checks if the pattern is a prefix of the value. Prefix[v, "p"] is the same as Wildcard[v, "p*"], though faster.
Wildcard: MatchProc;
The pattern may contain zero or more wildcards (the character "*") that match anything.
RE: MatchProc;
The pattern is taken to be a regular expression as defined in RegularExpressionDoc.tioga.
SoundexMatch: MatchProc;
Compares the value and pattern based on their Soundex codes. The Soundex encoding tends to group together variants of the same name; for instance, Johnson, Jansen, and Johansen have identical Soundex codes.
SoundexPrefix: MatchProc;
Like Soundex except the code of the pattern need only be a prefix of the value's code. For example, "John" is a soundex prefix of "Jansen".
Subrange: MatchProc;
Checks if the value is in the range specified by the pattern. The pattern consists of two prefixes separated by a "-".
NumSubrange: MatchProc;
Checks if the value is in the numerical range specified by the pattern. The pattern should consist of two positive integers separated by a "-". If the value can not be parsed as an integer then match=FALSE is returned; if the pattern is bad then match=FALSE and nothingGreater=TRUE is returned.
DateSubrange: MatchProc;
Checks if the value is in the chronological range specified by the pattern. The pattern should consist of two date and times (as can be parsed by Tempus) separated by a "-". If the value can not be parsed as a date and time then match=FALSE is returned; if the pattern is bad then match=FALSE and nothingGreater=TRUE is returned.
DateAndTime: MatchProc;
Checks if the value matches the pattern within the time precision of the pattern. The pattern, as well as the value, should be a date and time (as can be parsed by Tempus). If the value can not be parsed as a date and time then match=FALSE is returned; if the pattern is bad then match=FALSE and nothingGreater=TRUE is returned. A match can not occur unless the pattern is no more precise than the value. For example, a pattern of "Wednesday" will match a value of "Wednesday at 2 pm", but not vice versa.
DWIM: PROC [pattern: ROPE] RETURNS [ptype: ROPE];
Trys to deduce an appropriate pattern matcher from the given pattern. The returned ptype can be passed to Lookup to get the associated MatchProc.
CheckPattern: PROC [pattern: ROPE, proc: MatchProc] RETURNS [ok: BOOLEAN ¬ TRUE, info: ROPE ¬ NIL];
Checks if the given pattern is suitable for input to the given pattern matcher. For instance, proc=DateSubrange returns ok=TRUE only if the pattern is a valid date range. The returned info is a human readable analysis of the pattern.
Lookup: PROC [name: ROPE] RETURNS [proc: MatchProc];
Get a pattern matcher by name.
Register: PROC [name: ROPE, proc: MatchProc];
Register a name for a pattern matcher.
EachPairAction: TYPE = PROC [name: ROPE, proc: MatchProc] RETURNS [quit: BOOL ¬ FALSE];
Pairs: PROC [action: EachPairAction] RETURNS [BOOL];
... enumerates pairs currently in the pattern matcher registry in unspecified order; applies action to each pair until action returns TRUE or no more pairs; returns TRUE if some action returns TRUE.
END.
Doug Terry, April 14, 1987 12:50:27 pm PDT
Moved from LoganQuery.
changes to: DIRECTORY, PatternMatch, ~