Sather Home Page

Section 8.17.3.11:
REG_EXP

class REG_EXP < $BINARY

Inheritance map $BINARY

Formal Types

types

SAME = REG_EXP ;

REG_EXP = token

This class provides POSIX-style string pattern matching. It provides a matches operation to find out if a test string matches the extended regular expression.

An extended regular expression consists of one or more of the following kinds of components :-

  1. Collating Elements - any single character that is not a meta-character.
  2. Duplication count - a numeric constant.
  3. Meta-characters - the characters "^", "-" and "]".
  4. Left Anchor - the character "^" appearing at the beginning of the expression.
  5. Ordinary characters
  6. Quoted characters - the characters "^", ".", "*", "[", "$" and "\" escaped using a reverse solidus.
  7. Right Anchor - the character "$" appearing at the end of the expression.
  8. Special Characters - the characters ".", "\", "[", "^", "$", "*", "(", ")", "|", "?", "{" and "+".
NOTE The syntax of extended regular expressions is defined in the POSIX standard ISO/IEC 9945-2:1995 to which reference should be made in understanding the regular expression mechanism.

External specifications

The following features are required to be implemented for this class in accordance with the specifications given in $BINARY :-


create

This creation routine checks that the argument is a valid extended regular expression and returns it if it is, otherwise the argument returned is an indicative error message.

create (
inout  str : STR
) : SAME
Formal Signature

This formal signature uses a tuple for the result, the implementation being required to set the string component of that as the inout argument.

create(arg : STR) res : [SAME] * STR
Pre-condition

Since this is a creation routine, the pre-condition is vacuously true.

Post-condition
post let mk_(rexp,arg_out) = res in
(rexp <> nil
and arg_out = arg)
or (res = nil
and arg_out <> arg)

This routine creates a new regular expression pattern object unless an error has been detected, when void is returned and the string inout parameter is an error message indicating the problem.


matches

This predicate returns true of the given string matches self. Note that if self and the string have different encodings then a match is identically false.

matches (
test_str : STR
) : BOOL
Formal Signature
matches2(self : SAME, test_str : STR) res : BOOL
Pre-condition
pre len test_str > 0
Post-condition

Since this routine is a predicate, the post-condition is vacuously true.

This predicate returns true if and only if the given string matches self.


matches

This second variant of the matching predicate takes a string cursor, moving the cursor position to the end of the matched string if successful.

matches (
test_cursor : STR_CURSOR
) : BOOL
Formal Signature
matches(self : SAME, test_cursor : STR_CURSOR) res : BOOL
Pre-condition
pre not STR_CURSOR.is_done(test_cursor)
Post-condition
post (res and let head : seq of CHAR,
match : seq of CHAR,
tail : seq of CHAR be st

head ^ matching ^ tail = test_cursor
and len head = test_cursor(1, ..., STR_CURSOR.index(test_cursor~))
and len (head ^ matching) = test_cursor(1, ..., STR_CURSOR.index(test_cursor)) in

match2(matching))
or (not res
and STR_CURSOR.index(test_cursor~) = STR_CURSOR.index(test_cursor))

This predicate returns true if and only if self matches the test cursor, starting at the current buffer position. If a match is found, the cursor is positioned at the end of the longest matching string found.


Language Index Library Index Text Index
Comments or enquiries should be made to Keith Hopper.
Page last modified: Thursday, 23 November 2000.
Produced with Amaya