OSDN Git Service

Delete all lines containing "$Revision:".
[pf3gnuchains/gcc-fork.git] / gcc / ada / g-regpat.ads
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT LIBRARY COMPONENTS                          --
4 --                                                                          --
5 --                          G N A T . R E G P A T                           --
6 --                                                                          --
7 --                                 S p e c                                  --
8 --                                                                          --
9 --                                                                          --
10 --               Copyright (C) 1986 by University of Toronto.               --
11 --           Copyright (C) 1996-2001 Ada Core Technologies, Inc.            --
12 --                                                                          --
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.                                                      --
23 --                                                                          --
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.                                      --
30 --                                                                          --
31 -- GNAT is maintained by Ada Core Technologies Inc (http://www.gnat.com).   --
32 --                                                                          --
33 ------------------------------------------------------------------------------
34
35 --  This package implements roughly the same set of regular expressions as
36 --  are available in the Perl or Python programming languages.
37
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.
42
43 ------------------------------------------------------------
44 -- Summary of Pattern Matching Packages in GNAT Hierarchy --
45 ------------------------------------------------------------
46
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.
50
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.
57
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.
63
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.
69
70 package GNAT.Regpat is
71 pragma Preelaborate (Regpat);
72
73    --  The grammar is the following:
74
75    --     regexp ::= expr
76    --            ::= ^ expr               -- anchor at the beginning of string
77    --            ::= expr $               -- anchor at the end of string
78    --     expr   ::= term
79    --            ::= term | term          -- alternation (term or term ...)
80    --     term   ::= item
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
94    --                                        non-greedy version
95    --            ::= elmt { num , num2 }? -- matches between num and num2 times
96    --                                        non-greedy version
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
105    --            ::= nchr
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)
121
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)
149
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.
154
155    --  The highest value possible for the arguments to the curly operator ({})
156    --  are given by the constant Max_Curly_Repeat below.
157
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.
161
162    --  For instance:
163    --      regexp="<.*>"   string="<h1>title</h1>"   matches="<h1>title</h1>"
164    --      regexp="<.*?>"  string="<h1>title</h1>"   matches="<h1>"
165    --
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.
170
171    --  Compiling Regular Expressions
172    --  =============================
173
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.
178
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.
183
184    --     declare
185    --        Regexp : String := "a|b";
186
187    --        Matcher : Pattern_Matcher := Compile (Regexp);
188    --        --  The size for matcher is automatically allocated
189
190    --        Matcher2 : Pattern_Matcher (1000);
191    --        --  Some space is allocated directly.
192
193    --     begin
194    --        Compile (Matcher2, Regexp);
195    --        ...
196    --     end;
197
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).
201
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
205
206    --  Matching Strings
207    --  ================
208
209    --  Once the regular expression has been compiled, you can use it as often
210    --  as needed to match strings.
211
212    --  Several versions of the Match subprogram are provided, with different
213    --  parameters and return results.
214
215    --  See the description under each of these subprograms.
216
217    --  Here is a short example showing how to get the substring matched by
218    --  the first parenthesis pair.
219
220    --     declare
221    --        Matches : Match_Array;
222    --        Regexp  : String := "a(b|c)d";
223    --        Str     : String := "gacdg";
224
225    --     begin
226    --        Match (Compile (Regexp), Str, Matches);
227    --        return Str (Matches (1).First .. Matches (1).Last);
228    --        --  returns 'c'
229    --     end;
230
231    --  String Substitution
232    --  ===================
233
234    --  No subprogram is currently provided for string substitution.
235    --  However, this is easy to simulate with the parenthesis groups, as
236    --  shown below.
237
238    --  This example swaps the first two words of the string:
239
240    --     declare
241    --        Regexp  : String := "([a-z]+) +([a-z]+)";
242    --        Str     : String := " first   second third ";
243    --        Matches : Match_Array;
244
245    --     begin
246    --        Match (Compile (Regexp), Str, Matches);
247    --        return Str (Str'First .. Matches (1).First - 1)
248    --               & Str (Matches (2).First .. Matches (2).Last)
249    --               & " "
250    --               & Str (Matches (1).First .. Matches (1).Last)
251    --               & Str (Matches (2).Last + 1 .. Str'Last);
252    --        --  returns " second first third "
253    --     end;
254
255    ---------------
256    -- Constants --
257    ---------------
258
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.
263
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.
268
269    Max_Program_Size : constant := 2**15 - 1;
270    --  Maximum size that can be allocated for a program.
271
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.
277
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
281    --  expression.
282
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.
287
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).
293
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.
298
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.
303    --
304    --  This can be combined with Single_Line.
305
306    -----------------
307    -- Match_Array --
308    -----------------
309
310    subtype Match_Count is Natural range 0 .. Max_Paren_Count;
311
312    type Match_Location is record
313       First : Natural := 0;
314       Last  : Natural := 0;
315    end record;
316
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
320    --  expression.
321    --
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+".
326    --
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
330    --  No_Match.
331
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.
336
337    ------------------------------
338    -- Pattern_Matcher Creation --
339    ------------------------------
340
341    type Pattern_Matcher (Size : Program_Size) is private;
342    --  Type used to represent a regular expression compiled into byte code
343
344    Never_Match : constant Pattern_Matcher;
345    --  A regular expression that never matches anything
346
347    function Compile
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).
356    --
357    --  Flags is the default value to use to set properties for Expression (case
358    --  sensitivity,...).
359
360    procedure Compile
361      (Matcher         : out Pattern_Matcher;
362       Expression      : String;
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.
371    --
372    --  Flags is the default value to use to set properties for Expression (case
373    --  sensitivity,...).
374
375    procedure Compile
376      (Matcher    : out Pattern_Matcher;
377       Expression : String;
378       Flags      : Regexp_Flags := No_Flags);
379    --  Same procedure as above, expect it does not return the final
380    --  program size.
381
382    function Paren_Count (Regexp : Pattern_Matcher) return Match_Count;
383    pragma Inline (Paren_Count);
384
385    --  Return the number of parenthesis pairs in Regexp.
386
387    --  This is the maximum index that will be filled if a Match_Array is
388    --  used as an argument to Match.
389    --
390    --  Thus, if you want to be sure to get all the parenthesis, you should
391    --  do something like:
392    --
393    --     declare
394    --        Regexp  : Pattern_Matcher := Compile ("a(b*)(c+)");
395    --        Matched : Match_Array (0 .. Paren_Count (Regexp));
396    --     begin
397    --        Match (Regexp, "a string", Matched);
398    --     end;
399
400    -------------
401    -- Quoting --
402    -------------
403
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.
408
409    --------------
410    -- Matching --
411    --------------
412
413    procedure Match
414      (Expression     : String;
415       Data           : 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.
423    --
424    --  At most Matches'Length parenthesis are returned.
425
426    function  Match
427      (Expression : String;
428       Data       : String;
429       Size       : Program_Size := 0)
430       return       Natural;
431    --  Return the position where Data matches, or (Data'First - 1) if there is
432    --  no match.
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.
437
438    function Match
439      (Expression : String;
440       Data       : String;
441       Size       : Program_Size := 0)
442       return       Boolean;
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.
446    --
447    --  If Size is 0, then the appropriate size is automatically calculated
448    --  by this package, but this is slightly slower.
449
450    ------------------------------------------------
451    -- Matching a pre-compiled regular expression --
452    ------------------------------------------------
453
454    --  The following functions are significantly faster if you need to reuse
455    --  the same regular expression multiple times, since you only have to
456    --  compile it once.
457
458    function  Match
459      (Self : Pattern_Matcher;
460       Data : String)
461       return Natural;
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
464    --  expression.
465
466    pragma Inline (Match);
467    --  All except the last one below.
468
469    procedure Match
470      (Self    : Pattern_Matcher;
471       Data    : String;
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.
476    --
477    --  At most Matches'Length parenthesis are returned.
478
479    -----------
480    -- Debug --
481    -----------
482
483    procedure Dump (Self : Pattern_Matcher);
484    --  Dump the compiled version of the regular expression matched by Self.
485
486 --------------------------
487 -- Private Declarations --
488 --------------------------
489
490 private
491
492    subtype Pointer is Program_Size;
493    --  The Pointer type is used to point into Program_Data
494
495    --  Note that the pointer type is not necessarily 2 bytes
496    --  although it is stored in the program using 2 bytes
497
498    type Program_Data is array (Pointer range <>) of Character;
499
500    Program_First : constant := 1;
501
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:
505
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
510
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
514    --  match.
515
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.
522
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.
527
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;
532
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);
542    end record;
543
544    Never_Match : constant Pattern_Matcher :=
545       (0, ASCII.NUL, False, 0, 0, 0, No_Flags, (others => ASCII.NUL));
546
547 end GNAT.Regpat;