1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- G N A T . R E G E X P --
11 -- Copyright (C) 1998-1999 Ada Core Technologies, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- As a special exception, if other files instantiate generics from this --
25 -- unit, or you link this unit with other files to produce an executable, --
26 -- this unit does not by itself cause the resulting executable to be --
27 -- covered by the GNU General Public License. This exception does not --
28 -- however invalidate any other reasons why the executable file might be --
29 -- covered by the GNU Public License. --
31 -- GNAT is maintained by Ada Core Technologies Inc (http://www.gnat.com). --
33 ------------------------------------------------------------------------------
35 -- Simple Regular expression matching
37 -- This package provides a simple implementation of a regular expression
38 -- pattern matching algorithm, using a subset of the syntax of regular
39 -- expressions copied from familiar Unix style utilities.
41 ------------------------------------------------------------
42 -- Summary of Pattern Matching Packages in GNAT Hierarchy --
43 ------------------------------------------------------------
45 -- There are three related packages that perform pattern maching functions.
46 -- the following is an outline of these packages, to help you determine
47 -- which is best for your needs.
49 -- GNAT.Regexp (files g-regexp.ads/g-regexp.adb)
50 -- This is a simple package providing Unix-style regular expression
51 -- matching with the restriction that it matches entire strings. It
52 -- is particularly useful for file name matching, and in particular
53 -- it provides "globbing patterns" that are useful in implementing
54 -- unix or DOS style wild card matching for file names.
56 -- GNAT.Regpat (files g-regpat.ads/g-regpat.adb)
57 -- This is a more complete implementation of Unix-style regular
58 -- expressions, copied from the original V7 style regular expression
59 -- library written in C by Henry Spencer. It is functionally the
60 -- same as this library, and uses the same internal data structures
61 -- stored in a binary compatible manner.
63 -- GNAT.Spitbol.Patterns (files g-spipat.ads/g-spipat.adb)
64 -- This is a completely general patterm matching package based on the
65 -- pattern language of SNOBOL4, as implemented in SPITBOL. The pattern
66 -- language is modeled on context free grammars, with context sensitive
67 -- extensions that provide full (type 0) computational capabilities.
69 with Ada
.Finalization
;
71 package GNAT
.Regexp
is
73 -- The regular expression must first be compiled, using the Compile
74 -- function, which creates a finite state matching table, allowing
75 -- very fast matching once the expression has been compiled.
77 -- The following is the form of a regular expression, expressed in Ada
78 -- reference manual style BNF is as follows
82 -- regexp ::= term | term -- alternation (term or term ...)
86 -- term ::= item item ... -- concatenation (item then item)
88 -- item ::= elmt -- match elmt
89 -- item ::= elmt * -- zero or more elmt's
90 -- item ::= elmt + -- one or more elmt's
91 -- item ::= elmt ? -- matches elmt or nothing
93 -- elmt ::= nchr -- matches given character
94 -- elmt ::= [nchr nchr ...] -- matches any character listed
95 -- elmt ::= [^ nchr nchr ...] -- matches any character not listed
96 -- elmt ::= [char - char] -- matches chars in given range
97 -- elmt ::= . -- matches any single character
98 -- elmt ::= ( regexp ) -- parens used for grouping
100 -- char ::= any character, including special characters
101 -- nchr ::= any character except \()[].*+?^ or \char to match char
102 -- ... is used to indication repetition (one or more terms)
104 -- See also regexp(1) man page on Unix systems for further details
106 -- A second kind of regular expressions is provided. This one is more
107 -- like the wild card patterns used in file names by the Unix shell (or
108 -- DOS prompt) command lines. The grammar is the following:
114 -- term ::= elmt elmt ... -- concatenation (elmt then elmt)
115 -- term ::= * -- any string of 0 or more characters
116 -- term ::= ? -- matches any character
117 -- term ::= [char char ...] -- matches any character listed
118 -- term ::= [char - char] -- matches any character in given range
119 -- term ::= {elmt, elmt, ...} -- alternation (matches any of elmt)
121 -- Important note : This package was mainly intended to match regular
122 -- expressions against file names. The whole string has to match the
123 -- regular expression. If only a substring matches, then the function
124 -- Match will return False.
126 type Regexp
is private;
127 -- Private type used to represent a regular expression
129 Error_In_Regexp
: exception;
130 -- Exception raised when an error is found in the regular expression
134 Glob
: Boolean := False;
135 Case_Sensitive
: Boolean := True)
137 -- Compiles a regular expression S. If the syntax of the given
138 -- expression is invalid (does not match above grammar, Error_In_Regexp
139 -- is raised. If Glob is True, the pattern is considered as a 'globbing
140 -- pattern', that is a pattern as given by the second grammar above
142 function Match
(S
: String; R
: Regexp
) return Boolean;
143 -- True if S matches R, otherwise False. Raises Constraint_Error if
144 -- R is an uninitialized regular expression value.
149 type Regexp_Access
is access Regexp_Value
;
151 type Regexp
is new Ada
.Finalization
.Controlled
with record
152 R
: Regexp_Access
:= null;
155 pragma Finalize_Storage_Only
(Regexp
);
157 procedure Finalize
(R
: in out Regexp
);
158 -- Free the memory occupied by R
160 procedure Adjust
(R
: in out Regexp
);
161 -- Called after an assignment (do a copy of the Regexp_Access.all)