111
|
1 ------------------------------------------------------------------------------
|
|
2 -- --
|
|
3 -- GNAT COMPILER COMPONENTS --
|
|
4 -- --
|
|
5 -- S Y S T E M . R E G E X P --
|
|
6 -- --
|
|
7 -- S p e c --
|
|
8 -- --
|
145
|
9 -- Copyright (C) 1998-2019, AdaCore --
|
111
|
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 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. --
|
|
17 -- --
|
|
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. --
|
|
21 -- --
|
|
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/>. --
|
|
26 -- --
|
|
27 -- GNAT was originally developed by the GNAT team at New York University. --
|
|
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
|
|
29 -- --
|
|
30 ------------------------------------------------------------------------------
|
|
31
|
|
32 -- Simple Regular expression matching
|
|
33
|
|
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.
|
|
37
|
|
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).
|
|
41
|
|
42 with Ada.Finalization;
|
|
43
|
|
44 package System.Regexp is
|
|
45
|
|
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.
|
|
49
|
|
50 -- The following is the form of a regular expression, expressed in Ada
|
|
51 -- reference manual style BNF is as follows
|
|
52
|
|
53 -- regexp ::= term
|
|
54
|
|
55 -- regexp ::= term | term -- alternation (term or term ...)
|
|
56
|
|
57 -- term ::= item
|
|
58
|
|
59 -- term ::= item item ... -- concatenation (item then item)
|
|
60
|
|
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
|
|
65
|
|
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
|
|
72
|
|
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)
|
|
76
|
|
77 -- See also regexp(1) man page on Unix systems for further details
|
|
78
|
|
79 -- A second kind of regular expressions is provided. This one is more
|
145
|
80 -- like the wildcard patterns used in file names by the Unix shell (or
|
111
|
81 -- DOS prompt) command lines. The grammar is the following:
|
|
82
|
|
83 -- regexp ::= term
|
|
84
|
|
85 -- term ::= elmt
|
|
86 -- term ::= elmt elmt ... -- concatenation (elmt then elmt)
|
|
87 -- term ::= {elmt, elmt, ...} -- alternation (matches any of elmt)
|
|
88
|
|
89 -- elmt ::= * -- any string of 0 or more characters
|
|
90 -- elmt ::= ? -- matches any character
|
|
91 -- elmt ::= char
|
|
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
|
|
95
|
|
96 -- \char is also supported by this grammar.
|
|
97
|
|
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.
|
|
102
|
|
103 type Regexp is private;
|
|
104 -- Private type used to represent a regular expression
|
|
105
|
|
106 Error_In_Regexp : exception;
|
|
107 -- Exception raised when an error is found in the regular expression
|
|
108
|
|
109 function Compile
|
|
110 (Pattern : String;
|
|
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
|
|
118 -- match.
|
|
119
|
|
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.
|
|
123
|
|
124 private
|
|
125 type Regexp_Value;
|
|
126
|
|
127 type Regexp_Access is access Regexp_Value;
|
|
128
|
|
129 type Regexp is new Ada.Finalization.Controlled with record
|
|
130 R : Regexp_Access := null;
|
|
131 end record;
|
|
132
|
|
133 pragma Finalize_Storage_Only (Regexp);
|
|
134
|
|
135 procedure Finalize (R : in out Regexp);
|
|
136 -- Free the memory occupied by R
|
|
137
|
|
138 procedure Adjust (R : in out Regexp);
|
|
139 -- Called after an assignment (do a copy of the Regexp_Access.all)
|
|
140
|
|
141 end System.Regexp;
|