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