| 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 |
-- -- |
| 9 |
-- Copyright (C) 1998-2025, 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 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 |
| 80 |
-- like the wildcard patterns used in file names by the Unix shell (or |
| 81 |
-- DOS prompt) command lines. The grammar is the following: |
| 82 |
|
| 83 |
-- regexp ::= term |
| 84 |
|
| 85 |
-- term ::= elmt |
| 86 |
-- term ::= seq |
| 87 |
-- term ::= {seq, seq, ...} -- alternation (matches any of seq) |
| 88 |
|
| 89 |
-- seq ::= elmt elmt ... -- concatenation (sequence of elmts) |
| 90 |
|
| 91 |
-- elmt ::= * -- any string of 0 or more characters |
| 92 |
-- elmt ::= ? -- matches any character |
| 93 |
-- elmt ::= char |
| 94 |
-- elmt ::= [^ char char ...] -- matches any character not listed |
| 95 |
-- elmt ::= [char char ...] -- matches any character listed |
| 96 |
-- elmt ::= [char - char] -- matches any character in given range |
| 97 |
|
| 98 |
-- \char is also supported by this grammar. |
| 99 |
|
| 100 |
-- Important note : This package was mainly intended to match regular |
| 101 |
-- expressions against file names. The whole string has to match the |
| 102 |
-- regular expression. If only a substring matches, then the function |
| 103 |
-- Match will return False. |
| 104 |
|
| 105 |
type Regexp is private; |
| 106 |
-- Private type used to represent a regular expression |
| 107 |
|
| 108 |
Error_In_Regexp : exception; |
| 109 |
-- Exception raised when an error is found in the regular expression |
| 110 |
|
| 111 |
function Compile |
| 112 |
(Pattern : String; |
| 113 |
Glob : Boolean := False; |
| 114 |
Case_Sensitive : Boolean := True) return Regexp; |
| 115 |
-- Compiles a regular expression S. If the syntax of the given |
| 116 |
-- expression is invalid (does not match above grammar), Error_In_Regexp |
| 117 |
-- is raised. If Glob is True, the pattern is considered as a 'globbing |
| 118 |
-- pattern', that is a pattern as given by the second grammar above. |
| 119 |
-- As a special case, if Pattern is the empty string it will always |
| 120 |
-- match. |
| 121 |
|
| 122 |
function Match (S : String; R : Regexp) return Boolean; |
| 123 |
-- True if S matches R, otherwise False. Raises Constraint_Error if |
| 124 |
-- R is an uninitialized regular expression value. |
| 125 |
|
| 126 |
private |
| 127 |
type Regexp_Value; |
| 128 |
|
| 129 |
type Regexp_Access is access Regexp_Value; |
| 130 |
|
| 131 |
type Regexp is new Ada.Finalization.Controlled with record |
| 132 |
R : Regexp_Access := null; |
| 133 |
end record; |
| 134 |
|
| 135 |
pragma Finalize_Storage_Only (Regexp); |
| 136 |
|
| 137 |
procedure Finalize (R : in out Regexp); |
| 138 |
-- Free the memory occupied by R |
| 139 |
|
| 140 |
procedure Adjust (R : in out Regexp); |
| 141 |
-- Called after an assignment (do a copy of the Regexp_Access.all) |
| 142 |
|
| 143 |
end System.Regexp; |