## regular-expression-matcher.api
# Compiled by:
#
src/lib/std/standard.lib# Main api for regular expressions.
api Regular_Expression_Matcher {
=~ : (String, String) -> Bool;
# The following are adapted from Allen Leung's "lazy man's interface":
# Any of them may raise the exception
# abstract_regular_expression::CANNOT_PARSE;
# if the supplied regular expression is invalid.
# Match 'regex' once against 'text'.
# Return THE substring corresponding to
# the ith group (parenthesized sub-regex),
# where first group is number 1.
# (0 means return entire string matched.)
#
# Returns NULL if 'regex' fails to match
# 'text'.
#
find_first_match_to_ith_group
: Int # i
-> String # Regex.
-> String # Text.
-> Null_Or( String );
# Match 'regex' once against 'text'
# and return THE matched substring;
#
# If 'regex' fails to match 'text'
# return NULL.
#
# 'find_first_match_to_regex regex' is the same as
# 'find_first_match_to_ith_group 0 regex',
# since 0-th group of a match
# is the complete match.
#
find_first_match_to_regex
: String # Regex.
-> String # Text.
-> Null_Or( String );
# Match 'regex' once against 'text'.
# Return THE list of substrings
# constituting the match.
#
# Returns NULL if 'regex' fails to match 'text'.
#
find_first_match_to_regex_and_return_all_groups
: String # Regex.
-> String # Text.
-> Null_Or( List(String) );
# Find all occurrences of the ith group
# matches of 'regex' against 'text':
#
find_all_matches_to_regex_and_return_values_of_ith_group
: Int # i
-> String # Regex.
-> String # Text.
-> List(String)
;
# Find all matches of 'regex' against 'text'.
# Return a list containing, for each match,
# the list of strings constituting the match:
#
find_all_matches_to_regex_and_return_all_values_of_all_groups
: String # Regex.
-> String # Text.
-> List( List( String ) );
# Return all substrings of 'text' which match 'regex'.
# 'find_all_matches_to_regex regex' is the same as 'find_all_matches_to_regex_and_return_values_of_ith_group 0 regex',
# since 0-th group of a match is the complete match:
#
find_all_matches_to_regex
: String # Regex.
-> String # Text.
-> List( String );
# Return TRUE iff 'regex' matches 'text' somewhere:
#
matches
: String # Regex.
-> String # Text.
-> Bool;
# Match 'regex' once against 'text'.
# Return 'text' if no match is found,
# otherwise pass the list of substrings
# matched to 'f', and splice the resulting
# string into 'text' in place of the match:
#
replace_first_via_fn
: String # Regex.
-> (List( String ) -> String) # Compute replacement substring from matched substrings..
-> String # Text.
-> String; # Result text.
# Same as above, except substitution is
# done at all offsets at which regex
# matches, instead of just first:
#
replace_all_via_fn
: String # regex
-> (List( String ) -> String) # f -- computes replacement substring from matched substrings..
-> String # text.
-> String; # result text.
# Same as replace_first_via_fn except replacement
# is a constant string instead of computed:
#
replace_first
: String # Regex.
-> String # Replacement.
-> String # Text.
-> String; # Result text.
# Same as replace_all_via_fn except replacement
# is a constant string instead of computed:
#
replace_all
: String # Regex.
-> String # Replacement.
-> String # Text.
-> String; # Result text.
# The 'regex_case' function provides a 'case'
# type statement driven by regular expression
# pattern-matching.
#
# The arguments consist of a text to be matched
# followed by a list of (regex, action-fn) pairs
# (and a default action function).
#
# Execution consists of matching each regex
# in order against 'text' until one matches,
# at which point the corresponding action
# is invoked (with the substrings obtained
# from the match) and the result returned.
#
# If no regex matches, the default action
# is executed and the result returned.
#
# In any event, exactly one action function
# is invoked exactly once.
#
regex_case
: String # Text.
->
{ cases: List ( # List of (regex, action) pairs.
( String, # Regex.
List(String) -> X # Action function.
)
),
default: Void -> X # Default action.
}
-> X;
##########################################################################
# The following constitute the original
# lower-level interface predating the
# "lazy man's" interface.
##########################################################################
Compiled_Regular_Expression; # The type of a compiled regular expression
# number_string is from
src/lib/std/src/number-string.pkg # regex_match_result is from
src/lib/regex/glue/regex-match-result.pkg compile # Read an external representation of a regular expression from a stream.
: number_string::Reader( Char, X )
-> number_string::Reader( Compiled_Regular_Expression, X );
compile_string # Read an external representation of a regular expression from a string.
:
String -> Compiled_Regular_Expression;
find # Scan the stream for the first occurrence of the regular expression.
: Compiled_Regular_Expression
-> number_string::Reader( Char, X )
-> number_string::Reader( regex_match_result::Regex_Match_Result( Null_Or { match_position: X, match_length: Int } ), X);
prefix # Try to match the stream at the current position with the regular expression.
: Compiled_Regular_Expression
-> number_string::Reader( Char,X )
-> number_string::Reader( regex_match_result::Regex_Match_Result( Null_Or { match_position: X, match_length: Int } ), X);
stream_match # Try to match the stream at the current position with one
: # of the external representations of regular expressions and trigger
# the corresponding action.
List( (String, ( regex_match_result::Regex_Match_Result( Null_Or { match_position: X, match_length: Int } ) -> Y)))
->
number_string::Reader( Char, X )
->
number_string::Reader( Y, X );
};
### Well, I didn't get much done this year. I tried, but I wasn't too successful.
### Something always kept me from making the significant, creative advances I wanted
### to make. Maybe next year will be better, but this year was a washout.
###
### I don't mean to offer these as excuses, but here are some of the things that
### interrupted my thinking and kept all those neat ideas from springing out of me
### (I know they're in me, somewhere).
###
### I was in charge of the United Way fund for our department.
###
### Three cases of sexual harassment (going both ways) were handled by
### me in my position as graduate field rep.
###
### One student hurt his head and was in the hospital for a week at Christmas time.
### Somebody had to look after him, so I visited him and read comics to him every day.
###
### Because I don't really have any opinions of my own, and can therefore be called neutral,
### I was called on to mediate in the usual political fights between faculty members,
### which we all know are ruining our department.
###
### The heat didn't work and the bathroom stunk and the traffic bureau
### wanted to revoke our VP sticker and a student's dog bit another student and an
### M.Eng. student failed the colloquium course and one student ate five doughnuts
### before one colloquium and we went 1 student over our quota and a student's lunch
### was stolen from the refrigerator. Each of these called for a careful 2-page letter.
### These are only a few of the incidents that I took care of.
###
### I did write and prove correct a 2O line program in January, but I made the mistake
### of testing it on our VAX and it had an error, which two weeks of searching didn't
### uncover, so there went one publication out the window. I guess I could have slipped
### it into `FL anyway, since I'm an editor for it, but, since I have tenure already, I
### didn't feel right in doing that.
###
### I did work on the four-color problem. The work at Illinois had convinced me that
### you didn't have to prove your programs correct to publish in math journals -- the
### messier the program, the more likelihood of acceptance. But, after a week, I had to
### draw maps with four colors and the secretaries were out of the non-permanent
### colored transparency pens and I lost interest.
###
### Every once in a while I would try to get something done at home, but that, too, was
### a bomb. My Terak would act up, so I couldn't write, and my pen was broken. I
### was assistant to the assistant coach in both baseball and soccer for my son's team
### (that's the only way we could get him to play regularly), so I had to spend a lot of
### time at Cass Fark. Then there was boy scouts and those awful camping trips in the
### rain, which would lay me up in bed for weeks at a time with fever and runny nose.
### Something always arose to stop me from working at home. To top it off, one
### evening two weeks ago, I was working hard at home trying to be creative so I would
### have something to say in this damn report, when a neighbor's dog ate the last of our
### guinea pigs, and that began a crisis that lasted for 3 days and ruined the few
### thoughts I had.
###
### Well, so much for the research summary.
###
### Maybe next year will be better.
###
### -- David Gries research report, 1981
## COPYRIGHT (c) 1998 Bell Labs, Lucent Technologies.
## Subsequent changes by Jeff Prothero Copyright (c) 2010-2015,
## released per terms of SMLNJ-COPYRIGHT.