1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- G N A T . R E G P A T --
10 -- Copyright (C) 1986 by University of Toronto. --
11 -- Copyright (C) 1996-2001 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 -- This package implements roughly the same set of regular expressions as
36 -- are available in the Perl or Python programming languages.
38 -- This is an extension of the original V7 style regular expression library
39 -- written in C by Henry Spencer. Apart from the translation to Ada, the
40 -- interface has been considerably changed to use the Ada String type
41 -- instead of C-style nul-terminated strings.
43 ------------------------------------------------------------
44 -- Summary of Pattern Matching Packages in GNAT Hierarchy --
45 ------------------------------------------------------------
47 -- There are three related packages that perform pattern maching functions.
48 -- the following is an outline of these packages, to help you determine
49 -- which is best for your needs.
51 -- GNAT.Regexp (files g-regexp.ads/g-regexp.adb)
52 -- This is a simple package providing Unix-style regular expression
53 -- matching with the restriction that it matches entire strings. It
54 -- is particularly useful for file name matching, and in particular
55 -- it provides "globbing patterns" that are useful in implementing
56 -- unix or DOS style wild card matching for file names.
58 -- GNAT.Regpat (files g-regpat.ads/g-regpat.adb)
59 -- This is a more complete implementation of Unix-style regular
60 -- expressions, copied from the Perl regular expression engine,
61 -- written originally in C by Henry Spencer. It is functionally the
62 -- same as that library.
64 -- GNAT.Spitbol.Patterns (files g-spipat.ads/g-spipat.adb)
65 -- This is a completely general pattern matching package based on the
66 -- pattern language of SNOBOL4, as implemented in SPITBOL. The pattern
67 -- language is modeled on context free grammars, with context sensitive
68 -- extensions that provide full (type 0) computational capabilities.
70 package GNAT
.Regpat
is
71 pragma Preelaborate
(Regpat
);
73 -- The grammar is the following:
76 -- ::= ^ expr -- anchor at the beginning of string
77 -- ::= expr $ -- anchor at the end of string
79 -- ::= term | term -- alternation (term or term ...)
81 -- ::= item item ... -- concatenation (item then item)
82 -- item ::= elmt -- match elmt
83 -- ::= elmt * -- zero or more elmt's
84 -- ::= elmt + -- one or more elmt's
85 -- ::= elmt ? -- matches elmt or nothing
86 -- ::= elmt *? -- zero or more times, minimum number
87 -- ::= elmt +? -- one or more times, minimum number
88 -- ::= elmt ?? -- zero or one time, minimum number
89 -- ::= elmt { num } -- matches elmt exactly num times
90 -- ::= elmt { num , } -- matches elmt at least num times
91 -- ::= elmt { num , num2 } -- matches between num and num2 times
92 -- ::= elmt { num }? -- matches elmt exactly num times
93 -- ::= elmt { num , }? -- matches elmt at least num times
95 -- ::= elmt { num , num2 }? -- matches between num and num2 times
97 -- elmt ::= nchr -- matches given character
98 -- ::= [range range ...] -- matches any character listed
99 -- ::= [^ range range ...] -- matches any character not listed
100 -- ::= . -- matches any single character
101 -- -- except newlines
102 -- ::= ( expr ) -- parens used for grouping
103 -- ::= \ num -- reference to num-th parenthesis
104 -- range ::= char - char -- matches chars in given range
106 -- ::= [: posix :] -- any character in the POSIX range
107 -- ::= [:^ posix :] -- not in the POSIX range
108 -- posix ::= alnum -- alphanumeric characters
109 -- ::= alpha -- alphabetic characters
110 -- ::= ascii -- ascii characters (0 .. 127)
111 -- ::= cntrl -- control chars (0..31, 127..159)
112 -- ::= digit -- digits ('0' .. '9')
113 -- ::= graph -- graphic chars (32..126, 160..255)
114 -- ::= lower -- lower case characters
115 -- ::= print -- printable characters (32..127)
116 -- ::= punct -- printable, except alphanumeric
117 -- ::= space -- space characters
118 -- ::= upper -- upper case characters
119 -- ::= word -- alphanumeric characters
120 -- ::= xdigit -- hexadecimal chars (0..9, a..f)
122 -- char ::= any character, including special characters
123 -- ASCII.NUL is not supported.
124 -- nchr ::= any character except \()[].*+?^ or \char to match char
125 -- \n means a newline (ASCII.LF)
126 -- \t means a tab (ASCII.HT)
127 -- \r means a return (ASCII.CR)
128 -- \b matches the empty string at the beginning or end of a
129 -- word. A word is defined as a set of alphanumerical
130 -- characters (see \w below).
131 -- \B matches the empty string only when *not* at the
132 -- beginning or end of a word.
133 -- \d matches any digit character ([0-9])
134 -- \D matches any non digit character ([^0-9])
135 -- \s matches any white space character. This is equivalent
136 -- to [ \t\n\r\f\v] (tab, form-feed, vertical-tab,...
137 -- \S matches any non-white space character.
138 -- \w matches any alphanumeric character or underscore.
139 -- This include accented letters, as defined in the
140 -- package Ada.Characters.Handling.
141 -- \W matches any non-alphanumeric character.
142 -- \A match the empty string only at the beginning of the
143 -- string, whatever flags are used for Compile (the
144 -- behavior of ^ can change, see Regexp_Flags below).
145 -- \G match the empty string only at the end of the
146 -- string, whatever flags are used for Compile (the
147 -- behavior of $ can change, see Regexp_Flags below).
148 -- ... ::= is used to indication repetition (one or more terms)
150 -- Embedded newlines are not matched by the ^ operator.
151 -- It is possible to retrieve the substring matched a parenthesis
152 -- expression. Although the depth of parenthesis is not limited in the
153 -- regexp, only the first 9 substrings can be retrieved.
155 -- The highest value possible for the arguments to the curly operator ({})
156 -- are given by the constant Max_Curly_Repeat below.
158 -- The operators '*', '+', '?' and '{}' always match the longest possible
159 -- substring. They all have a non-greedy version (with an extra ? after the
160 -- operator), which matches the shortest possible substring.
163 -- regexp="<.*>" string="<h1>title</h1>" matches="<h1>title</h1>"
164 -- regexp="<.*?>" string="<h1>title</h1>" matches="<h1>"
166 -- '{' and '}' are only considered as special characters if they appear
167 -- in a substring that looks exactly like '{n}', '{n,m}' or '{n,}', where
168 -- n and m are digits. No space is allowed. In other contexts, the curly
169 -- braces will simply be treated as normal characters.
171 -- Compiling Regular Expressions
172 -- =============================
174 -- To use this package, you first need to compile the regular expression
175 -- (a string) into a byte-code program, in a Pattern_Matcher structure.
176 -- This first step checks that the regexp is valid, and optimizes the
177 -- matching algorithms of the second step.
179 -- Two versions of the Compile subprogram are given: one in which this
180 -- package will compute itself the best possible size to allocate for the
181 -- byte code; the other where you must allocate enough memory yourself. An
182 -- exception is raised if there is not enough memory.
185 -- Regexp : String := "a|b";
187 -- Matcher : Pattern_Matcher := Compile (Regexp);
188 -- -- The size for matcher is automatically allocated
190 -- Matcher2 : Pattern_Matcher (1000);
191 -- -- Some space is allocated directly.
194 -- Compile (Matcher2, Regexp);
198 -- Note that the second version is significantly faster, since with the
199 -- first version the regular expression has in fact to be compiled twice
200 -- (first to compute the size, then to generate the byte code).
202 -- Note also that you can not use the function version of Compile if you
203 -- specify the size of the Pattern_Matcher, since the discriminants will
204 -- most probably be different and you will get a Constraint_Error
209 -- Once the regular expression has been compiled, you can use it as often
210 -- as needed to match strings.
212 -- Several versions of the Match subprogram are provided, with different
213 -- parameters and return results.
215 -- See the description under each of these subprograms.
217 -- Here is a short example showing how to get the substring matched by
218 -- the first parenthesis pair.
221 -- Matches : Match_Array;
222 -- Regexp : String := "a(b|c)d";
223 -- Str : String := "gacdg";
226 -- Match (Compile (Regexp), Str, Matches);
227 -- return Str (Matches (1).First .. Matches (1).Last);
231 -- String Substitution
232 -- ===================
234 -- No subprogram is currently provided for string substitution.
235 -- However, this is easy to simulate with the parenthesis groups, as
238 -- This example swaps the first two words of the string:
241 -- Regexp : String := "([a-z]+) +([a-z]+)";
242 -- Str : String := " first second third ";
243 -- Matches : Match_Array;
246 -- Match (Compile (Regexp), Str, Matches);
247 -- return Str (Str'First .. Matches (1).First - 1)
248 -- & Str (Matches (2).First .. Matches (2).Last)
250 -- & Str (Matches (1).First .. Matches (1).Last)
251 -- & Str (Matches (2).Last + 1 .. Str'Last);
252 -- -- returns " second first third "
259 Expression_Error
: exception;
260 -- This exception is raised when trying to compile an invalid
261 -- regular expression. All subprograms taking an expression
262 -- as parameter may raise Expression_Error.
264 Max_Paren_Count
: constant := 255;
265 -- Maximum number of parenthesis in a regular expression.
266 -- This is limited by the size of a Character, as found in the
267 -- byte-compiled version of regular expressions.
269 Max_Program_Size
: constant := 2**15 - 1;
270 -- Maximum size that can be allocated for a program.
272 Max_Curly_Repeat
: constant := 32767;
273 -- Maximum number of repetition for the curly operator.
274 -- The digits in the {n}, {n,} and {n,m } operators can not be higher
275 -- than this constant, since they have to fit on two characters in the
276 -- byte-compiled version of regular expressions.
278 type Program_Size
is range 0 .. Max_Program_Size
;
279 for Program_Size
'Size use 16;
280 -- Number of bytes allocated for the byte-compiled version of a regular
283 type Regexp_Flags
is mod 256;
284 for Regexp_Flags
'Size use 8;
285 -- Flags that can be given at compile time to specify default
286 -- properties for the regular expression.
288 No_Flags
: constant Regexp_Flags
;
289 Case_Insensitive
: constant Regexp_Flags
;
290 -- The automaton is optimized so that the matching is done in a case
291 -- insensitive manner (upper case characters and lower case characters
292 -- are all treated the same way).
294 Single_Line
: constant Regexp_Flags
;
295 -- Treat the Data we are matching as a single line. This means that
296 -- ^ and $ will ignore \n (unless Multiple_Lines is also specified),
297 -- and that '.' will match \n.
299 Multiple_Lines
: constant Regexp_Flags
;
300 -- Treat the Data as multiple lines. This means that ^ and $ will also
301 -- match on internal newlines (ASCII.LF), in addition to the beginning
302 -- and end of the string.
304 -- This can be combined with Single_Line.
310 subtype Match_Count
is Natural range 0 .. Max_Paren_Count
;
312 type Match_Location
is record
313 First
: Natural := 0;
317 type Match_Array
is array (Match_Count
range <>) of Match_Location
;
318 -- The substring matching a given pair of parenthesis.
319 -- Index 0 is the whole substring that matched the full regular
322 -- For instance, if your regular expression is something like:
323 -- "a(b*)(c+)", then Match_Array(1) will be the indexes of the
324 -- substring that matched "b*" and Match_Array(2) will be the substring
325 -- that matched "c+".
327 -- The number of parenthesis groups that can be retrieved is unlimited,
328 -- and all the Match subprograms below can use a Match_Array of any size.
329 -- Indexes that do not have any matching parenthesis are set to
332 No_Match
: constant Match_Location
:= (First
=> 0, Last
=> 0);
333 -- The No_Match constant is (0, 0) to differentiate between
334 -- matching a null string at position 1, which uses (1, 0)
335 -- and no match at all.
337 ------------------------------
338 -- Pattern_Matcher Creation --
339 ------------------------------
341 type Pattern_Matcher
(Size
: Program_Size
) is private;
342 -- Type used to represent a regular expression compiled into byte code
344 Never_Match
: constant Pattern_Matcher
;
345 -- A regular expression that never matches anything
348 (Expression
: String;
349 Flags
: Regexp_Flags
:= No_Flags
)
350 return Pattern_Matcher
;
351 -- Compile a regular expression into internal code.
352 -- Raises Expression_Error if Expression is not a legal regular expression.
353 -- The appropriate size is calculated automatically, but this means that
354 -- the regular expression has to be compiled twice (the first time to
355 -- calculate the size, the second time to actually generate the byte code).
357 -- Flags is the default value to use to set properties for Expression (case
361 (Matcher
: out Pattern_Matcher
;
363 Final_Code_Size
: out Program_Size
;
364 Flags
: Regexp_Flags
:= No_Flags
);
365 -- Compile a regular expression into into internal code
366 -- This procedure is significantly faster than the function
367 -- Compile, as there is a known maximum size for the matcher.
368 -- This function raises Storage_Error if Matcher is too small
369 -- to hold the resulting code, or Expression_Error is Expression
370 -- is not a legal regular expression.
372 -- Flags is the default value to use to set properties for Expression (case
376 (Matcher
: out Pattern_Matcher
;
378 Flags
: Regexp_Flags
:= No_Flags
);
379 -- Same procedure as above, expect it does not return the final
382 function Paren_Count
(Regexp
: Pattern_Matcher
) return Match_Count
;
383 pragma Inline
(Paren_Count
);
385 -- Return the number of parenthesis pairs in Regexp.
387 -- This is the maximum index that will be filled if a Match_Array is
388 -- used as an argument to Match.
390 -- Thus, if you want to be sure to get all the parenthesis, you should
391 -- do something like:
394 -- Regexp : Pattern_Matcher := Compile ("a(b*)(c+)");
395 -- Matched : Match_Array (0 .. Paren_Count (Regexp));
397 -- Match (Regexp, "a string", Matched);
404 function Quote
(Str
: String) return String;
405 -- Return a version of Str so that every special character is quoted.
406 -- The resulting string can be used in a regular expression to match
407 -- exactly Str, whatever character was present in Str.
414 (Expression
: String;
416 Matches
: out Match_Array
;
417 Size
: Program_Size
:= 0);
418 -- Match Expression against Data and store result in Matches.
419 -- Function raises Storage_Error if Size is too small for Expression,
420 -- or Expression_Error if Expression is not a legal regular expression.
421 -- If Size is 0, then the appropriate size is automatically calculated
422 -- by this package, but this is slightly slower.
424 -- At most Matches'Length parenthesis are returned.
427 (Expression
: String;
429 Size
: Program_Size
:= 0)
431 -- Return the position where Data matches, or (Data'First - 1) if there is
433 -- Function raises Storage_Error if Size is too small for Expression
434 -- or Expression_Error if Expression is not a legal regular expression
435 -- If Size is 0, then the appropriate size is automatically calculated
436 -- by this package, but this is slightly slower.
439 (Expression
: String;
441 Size
: Program_Size
:= 0)
443 -- Return True if Data matches Expression. Match raises Storage_Error
444 -- if Size is too small for Expression, or Expression_Error if Expression
445 -- is not a legal regular expression.
447 -- If Size is 0, then the appropriate size is automatically calculated
448 -- by this package, but this is slightly slower.
450 ------------------------------------------------
451 -- Matching a pre-compiled regular expression --
452 ------------------------------------------------
454 -- The following functions are significantly faster if you need to reuse
455 -- the same regular expression multiple times, since you only have to
459 (Self
: Pattern_Matcher
;
462 -- Return the position where Data matches, or (Data'First - 1) if there is
463 -- no match. Raises Expression_Error if Expression is not a legal regular
466 pragma Inline
(Match
);
467 -- All except the last one below.
470 (Self
: Pattern_Matcher
;
472 Matches
: out Match_Array
);
473 -- Match Data using the given pattern matcher and store result in Matches.
474 -- Raises Expression_Error if Expression is not a legal regular expression.
475 -- The expression matches if Matches (0) /= No_Match.
477 -- At most Matches'Length parenthesis are returned.
483 procedure Dump
(Self
: Pattern_Matcher
);
484 -- Dump the compiled version of the regular expression matched by Self.
486 --------------------------
487 -- Private Declarations --
488 --------------------------
492 subtype Pointer
is Program_Size
;
493 -- The Pointer type is used to point into Program_Data
495 -- Note that the pointer type is not necessarily 2 bytes
496 -- although it is stored in the program using 2 bytes
498 type Program_Data
is array (Pointer
range <>) of Character;
500 Program_First
: constant := 1;
502 -- The "internal use only" fields in regexp are present to pass
503 -- info from compile to execute that permits the execute phase
504 -- to run lots faster on simple cases. They are:
506 -- First character that must begin a match or ASCII.Nul
507 -- Anchored true iff match must start at beginning of line
508 -- Must_Have pointer to string that match must include or null
509 -- Must_Have_Length length of Must_Have string
511 -- First and Anchored permit very fast decisions on suitable
512 -- starting points for a match, cutting down the work a lot.
513 -- Must_Have permits fast rejection of lines that cannot possibly
516 -- The Must_Have tests are costly enough that Optimize
517 -- supplies a Must_Have only if the r.e. contains something potentially
518 -- expensive (at present, the only such thing detected is * or +
519 -- at the start of the r.e., which can involve a lot of backup).
520 -- The length is supplied because the test in Execute needs it
521 -- and Optimize is computing it anyway.
523 -- The initialization is meant to fail-safe in case the user of this
524 -- package tries to use an uninitialized matcher. This takes advantage
525 -- of the knowledge that ASCII.Nul translates to the end-of-program (EOP)
526 -- instruction code of the state machine.
528 No_Flags
: constant Regexp_Flags
:= 0;
529 Case_Insensitive
: constant Regexp_Flags
:= 1;
530 Single_Line
: constant Regexp_Flags
:= 2;
531 Multiple_Lines
: constant Regexp_Flags
:= 4;
533 type Pattern_Matcher
(Size
: Pointer
) is record
534 First
: Character := ASCII
.NUL
; -- internal use only
535 Anchored
: Boolean := False; -- internal use only
536 Must_Have
: Pointer
:= 0; -- internal use only
537 Must_Have_Length
: Natural := 0; -- internal use only
538 Paren_Count
: Natural := 0; -- # paren groups
539 Flags
: Regexp_Flags
:= No_Flags
;
540 Program
: Program_Data
(Program_First
.. Size
) :=
541 (others => ASCII
.NUL
);
544 Never_Match
: constant Pattern_Matcher
:=
545 (0, ASCII
.NUL
, False, 0, 0, 0, No_Flags
, (others => ASCII
.NUL
));