1 |
281 |
jeremybenn |
------------------------------------------------------------------------------
|
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 |
|
|
-- --
|
9 |
|
|
-- Copyright (C) 1998-2008, AdaCore --
|
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, 51 Franklin Street, Fifth Floor, --
|
20 |
|
|
-- Boston, MA 02110-1301, 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 was originally developed by the GNAT team at New York University. --
|
30 |
|
|
-- Extensive contributions were provided by Ada Core Technologies Inc. --
|
31 |
|
|
-- --
|
32 |
|
|
------------------------------------------------------------------------------
|
33 |
|
|
|
34 |
|
|
-- Simple Regular expression matching
|
35 |
|
|
|
36 |
|
|
-- This package provides a simple implementation of a regular expression
|
37 |
|
|
-- pattern matching algorithm, using a subset of the syntax of regular
|
38 |
|
|
-- expressions copied from familiar Unix style utilities.
|
39 |
|
|
|
40 |
|
|
-- Note: this package is in the System hierarchy so that it can be directly
|
41 |
|
|
-- be used by other predefined packages. User access to this package is via
|
42 |
|
|
-- a renaming of this package in GNAT.Regexp (file g-regexp.ads).
|
43 |
|
|
|
44 |
|
|
with Ada.Finalization;
|
45 |
|
|
|
46 |
|
|
package System.Regexp is
|
47 |
|
|
|
48 |
|
|
-- The regular expression must first be compiled, using the Compile
|
49 |
|
|
-- function, which creates a finite state matching table, allowing
|
50 |
|
|
-- very fast matching once the expression has been compiled.
|
51 |
|
|
|
52 |
|
|
-- The following is the form of a regular expression, expressed in Ada
|
53 |
|
|
-- reference manual style BNF is as follows
|
54 |
|
|
|
55 |
|
|
-- regexp ::= term
|
56 |
|
|
|
57 |
|
|
-- regexp ::= term | term -- alternation (term or term ...)
|
58 |
|
|
|
59 |
|
|
-- term ::= item
|
60 |
|
|
|
61 |
|
|
-- term ::= item item ... -- concatenation (item then item)
|
62 |
|
|
|
63 |
|
|
-- item ::= elmt -- match elmt
|
64 |
|
|
-- item ::= elmt * -- zero or more elmt's
|
65 |
|
|
-- item ::= elmt + -- one or more elmt's
|
66 |
|
|
-- item ::= elmt ? -- matches elmt or nothing
|
67 |
|
|
|
68 |
|
|
-- elmt ::= nchr -- matches given character
|
69 |
|
|
-- elmt ::= [nchr nchr ...] -- matches any character listed
|
70 |
|
|
-- elmt ::= [^ nchr nchr ...] -- matches any character not listed
|
71 |
|
|
-- elmt ::= [char - char] -- matches chars in given range
|
72 |
|
|
-- elmt ::= . -- matches any single character
|
73 |
|
|
-- elmt ::= ( regexp ) -- parens used for grouping
|
74 |
|
|
|
75 |
|
|
-- char ::= any character, including special characters
|
76 |
|
|
-- nchr ::= any character except \()[].*+?^ or \char to match char
|
77 |
|
|
-- ... is used to indication repetition (one or more terms)
|
78 |
|
|
|
79 |
|
|
-- See also regexp(1) man page on Unix systems for further details
|
80 |
|
|
|
81 |
|
|
-- A second kind of regular expressions is provided. This one is more
|
82 |
|
|
-- like the wild card patterns used in file names by the Unix shell (or
|
83 |
|
|
-- DOS prompt) command lines. The grammar is the following:
|
84 |
|
|
|
85 |
|
|
-- regexp ::= term
|
86 |
|
|
|
87 |
|
|
-- term ::= elmt
|
88 |
|
|
|
89 |
|
|
-- term ::= elmt elmt ... -- concatenation (elmt then elmt)
|
90 |
|
|
-- term ::= * -- any string of 0 or more characters
|
91 |
|
|
-- term ::= ? -- matches any character
|
92 |
|
|
-- term ::= [char char ...] -- matches any character listed
|
93 |
|
|
-- term ::= [char - char] -- matches any character in given range
|
94 |
|
|
-- term ::= {elmt, elmt, ...} -- alternation (matches any of elmt)
|
95 |
|
|
|
96 |
|
|
-- Important note : This package was mainly intended to match regular
|
97 |
|
|
-- expressions against file names. The whole string has to match the
|
98 |
|
|
-- regular expression. If only a substring matches, then the function
|
99 |
|
|
-- Match will return False.
|
100 |
|
|
|
101 |
|
|
type Regexp is private;
|
102 |
|
|
-- Private type used to represent a regular expression
|
103 |
|
|
|
104 |
|
|
Error_In_Regexp : exception;
|
105 |
|
|
-- Exception raised when an error is found in the regular expression
|
106 |
|
|
|
107 |
|
|
function Compile
|
108 |
|
|
(Pattern : String;
|
109 |
|
|
Glob : Boolean := False;
|
110 |
|
|
Case_Sensitive : Boolean := True) return Regexp;
|
111 |
|
|
-- Compiles a regular expression S. If the syntax of the given
|
112 |
|
|
-- expression is invalid (does not match above grammar), Error_In_Regexp
|
113 |
|
|
-- is raised. If Glob is True, the pattern is considered as a 'globbing
|
114 |
|
|
-- pattern', that is a pattern as given by the second grammar above.
|
115 |
|
|
-- As a special case, if Pattern is the empty string it will always
|
116 |
|
|
-- match.
|
117 |
|
|
|
118 |
|
|
function Match (S : String; R : Regexp) return Boolean;
|
119 |
|
|
-- True if S matches R, otherwise False. Raises Constraint_Error if
|
120 |
|
|
-- R is an uninitialized regular expression value.
|
121 |
|
|
|
122 |
|
|
private
|
123 |
|
|
type Regexp_Value;
|
124 |
|
|
|
125 |
|
|
type Regexp_Access is access Regexp_Value;
|
126 |
|
|
|
127 |
|
|
type Regexp is new Ada.Finalization.Controlled with record
|
128 |
|
|
R : Regexp_Access := null;
|
129 |
|
|
end record;
|
130 |
|
|
|
131 |
|
|
pragma Finalize_Storage_Only (Regexp);
|
132 |
|
|
|
133 |
|
|
procedure Finalize (R : in out Regexp);
|
134 |
|
|
-- Free the memory occupied by R
|
135 |
|
|
|
136 |
|
|
procedure Adjust (R : in out Regexp);
|
137 |
|
|
-- Called after an assignment (do a copy of the Regexp_Access.all)
|
138 |
|
|
|
139 |
|
|
end System.Regexp;
|