1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- S Y S T E M . R E G E X P --
9 -- Copyright (C) 1998-2018, AdaCore --
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 3, 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. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
32 -- Simple Regular expression matching
34 -- This package provides a simple implementation of a regular expression
35 -- pattern matching algorithm, using a subset of the syntax of regular
36 -- expressions copied from familiar Unix style utilities.
38 -- Note: this package is in the System hierarchy so that it can be directly
39 -- be used by other predefined packages. User access to this package is via
40 -- a renaming of this package in GNAT.Regexp (file g-regexp.ads).
42 with Ada
.Finalization
;
44 package System
.Regexp
is
46 -- The regular expression must first be compiled, using the Compile
47 -- function, which creates a finite state matching table, allowing
48 -- very fast matching once the expression has been compiled.
50 -- The following is the form of a regular expression, expressed in Ada
51 -- reference manual style BNF is as follows
55 -- regexp ::= term | term -- alternation (term or term ...)
59 -- term ::= item item ... -- concatenation (item then item)
61 -- item ::= elmt -- match elmt
62 -- item ::= elmt * -- zero or more elmt's
63 -- item ::= elmt + -- one or more elmt's
64 -- item ::= elmt ? -- matches elmt or nothing
66 -- elmt ::= nchr -- matches given character
67 -- elmt ::= [nchr nchr ...] -- matches any character listed
68 -- elmt ::= [^ nchr nchr ...] -- matches any character not listed
69 -- elmt ::= [char - char] -- matches chars in given range
70 -- elmt ::= . -- matches any single character
71 -- elmt ::= ( regexp ) -- parens used for grouping
73 -- char ::= any character, including special characters
74 -- nchr ::= any character except \()[].*+?^ or \char to match char
75 -- ... is used to indication repetition (one or more terms)
77 -- See also regexp(1) man page on Unix systems for further details
79 -- A second kind of regular expressions is provided. This one is more
80 -- like the wild card patterns used in file names by the Unix shell (or
81 -- DOS prompt) command lines. The grammar is the following:
86 -- term ::= elmt elmt ... -- concatenation (elmt then elmt)
87 -- term ::= {elmt, elmt, ...} -- alternation (matches any of elmt)
89 -- elmt ::= * -- any string of 0 or more characters
90 -- elmt ::= ? -- matches any character
92 -- elmt ::= [^ char char ...] -- matches any character not listed
93 -- elmt ::= [char char ...] -- matches any character listed
94 -- elmt ::= [char - char] -- matches any character in given range
96 -- \char is also supported by this grammar.
98 -- Important note : This package was mainly intended to match regular
99 -- expressions against file names. The whole string has to match the
100 -- regular expression. If only a substring matches, then the function
101 -- Match will return False.
103 type Regexp
is private;
104 -- Private type used to represent a regular expression
106 Error_In_Regexp
: exception;
107 -- Exception raised when an error is found in the regular expression
111 Glob
: Boolean := False;
112 Case_Sensitive
: Boolean := True) return Regexp
;
113 -- Compiles a regular expression S. If the syntax of the given
114 -- expression is invalid (does not match above grammar), Error_In_Regexp
115 -- is raised. If Glob is True, the pattern is considered as a 'globbing
116 -- pattern', that is a pattern as given by the second grammar above.
117 -- As a special case, if Pattern is the empty string it will always
120 function Match
(S
: String; R
: Regexp
) return Boolean;
121 -- True if S matches R, otherwise False. Raises Constraint_Error if
122 -- R is an uninitialized regular expression value.
127 type Regexp_Access
is access Regexp_Value
;
129 type Regexp
is new Ada
.Finalization
.Controlled
with record
130 R
: Regexp_Access
:= null;
133 pragma Finalize_Storage_Only
(Regexp
);
135 procedure Finalize
(R
: in out Regexp
);
136 -- Free the memory occupied by R
138 procedure Adjust
(R
: in out Regexp
);
139 -- Called after an assignment (do a copy of the Regexp_Access.all)