1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- G N A T . R E G E X P --
9 -- Copyright (C) 1999-2007, 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 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. --
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. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
34 with Ada
.Unchecked_Deallocation
;
37 with System
.Case_Util
;
39 package body System
.Regexp
is
41 Open_Paren
: constant Character := '(';
42 Close_Paren
: constant Character := ')';
43 Open_Bracket
: constant Character := '[';
44 Close_Bracket
: constant Character := ']';
46 type State_Index
is new Natural;
47 type Column_Index
is new Natural;
49 type Regexp_Array
is array
50 (State_Index
range <>, Column_Index
range <>) of State_Index
;
51 -- First index is for the state number
52 -- Second index is for the character type
53 -- Contents is the new State
55 type Regexp_Array_Access
is access Regexp_Array
;
56 -- Use this type through the functions Set below, so that it
57 -- can grow dynamically depending on the needs.
59 type Mapping
is array (Character'Range) of Column_Index
;
60 -- Mapping between characters and column in the Regexp_Array
62 type Boolean_Array
is array (State_Index
range <>) of Boolean;
65 (Alphabet_Size
: Column_Index
;
66 Num_States
: State_Index
) is
69 States
: Regexp_Array
(1 .. Num_States
, 0 .. Alphabet_Size
);
70 Is_Final
: Boolean_Array
(1 .. Num_States
);
71 Case_Sensitive
: Boolean;
73 -- Deterministic finite-state machine
75 -----------------------
76 -- Local Subprograms --
77 -----------------------
80 (Table
: in out Regexp_Array_Access
;
82 Column
: Column_Index
;
84 -- Sets a value in the table. If the table is too small, reallocate it
85 -- dynamically so that (State, Column) is a valid index in it.
88 (Table
: Regexp_Array_Access
;
90 Column
: Column_Index
)
92 -- Returns the value in the table at (State, Column).
93 -- If this index does not exist in the table, returns 0
95 procedure Free
is new Ada
.Unchecked_Deallocation
96 (Regexp_Array
, Regexp_Array_Access
);
102 procedure Adjust
(R
: in out Regexp
) is
106 Tmp
:= new Regexp_Value
(Alphabet_Size
=> R
.R
.Alphabet_Size
,
107 Num_States
=> R
.R
.Num_States
);
118 Glob
: Boolean := False;
119 Case_Sensitive
: Boolean := True)
122 S
: String := Pattern
;
123 -- The pattern which is really compiled (when the pattern is case
124 -- insensitive, we convert this string to lower-cases
126 Map
: Mapping
:= (others => 0);
127 -- Mapping between characters and columns in the tables
129 Alphabet_Size
: Column_Index
:= 0;
130 -- Number of significant characters in the regular expression.
131 -- This total does not include special operators, such as *, (, ...
133 procedure Create_Mapping
;
134 -- Creates a mapping between characters in the regexp and columns
135 -- in the tables representing the regexp. Test that the regexp is
136 -- well-formed Modifies Alphabet_Size and Map
138 procedure Create_Primary_Table
139 (Table
: out Regexp_Array_Access
;
140 Num_States
: out State_Index
;
141 Start_State
: out State_Index
;
142 End_State
: out State_Index
);
143 -- Creates the first version of the regexp (this is a non determinist
144 -- finite state machine, which is unadapted for a fast pattern
145 -- matching algorithm). We use a recursive algorithm to process the
146 -- parenthesis sub-expressions.
148 -- Table : at the end of the procedure : Column 0 is for any character
149 -- ('.') and the last columns are for no character (closure)
150 -- Num_States is set to the number of states in the table
151 -- Start_State is the number of the starting state in the regexp
152 -- End_State is the number of the final state when the regexp matches
154 procedure Create_Primary_Table_Glob
155 (Table
: out Regexp_Array_Access
;
156 Num_States
: out State_Index
;
157 Start_State
: out State_Index
;
158 End_State
: out State_Index
);
159 -- Same function as above, but it deals with the second possible
160 -- grammar for 'globbing pattern', which is a kind of subset of the
161 -- whole regular expression grammar.
163 function Create_Secondary_Table
164 (First_Table
: Regexp_Array_Access
;
165 Num_States
: State_Index
;
166 Start_State
: State_Index
;
167 End_State
: State_Index
)
169 -- Creates the definitive table representing the regular expression
170 -- This is actually a transformation of the primary table First_Table,
171 -- where every state is grouped with the states in its 'no-character'
172 -- columns. The transitions between the new states are then recalculated
173 -- and if necessary some new states are created.
175 -- Note that the resulting finite-state machine is not optimized in
176 -- terms of the number of states : it would be more time-consuming to
177 -- add a third pass to reduce the number of states in the machine, with
178 -- no speed improvement...
180 procedure Raise_Exception
183 pragma No_Return
(Raise_Exception
);
184 -- Raise an exception, indicating an error at character Index in S
190 procedure Create_Mapping
is
192 procedure Add_In_Map
(C
: Character);
193 -- Add a character in the mapping, if it is not already defined
199 procedure Add_In_Map
(C
: Character) is
202 Alphabet_Size
:= Alphabet_Size
+ 1;
203 Map
(C
) := Alphabet_Size
;
207 J
: Integer := S
'First;
208 Parenthesis_Level
: Integer := 0;
209 Curly_Level
: Integer := 0;
211 -- Start of processing for Create_Mapping
214 while J
<= S
'Last loop
223 if S
(J
) = ']' or S
(J
) = '-' then
227 -- The first character never has a special meaning
232 ("Ran out of characters while parsing ", J
);
235 exit when S
(J
) = Close_Bracket
;
238 and then S
(J
+ 1) /= Close_Bracket
241 Start
: constant Integer := J
- 1;
250 for Char
in S
(Start
) .. S
(J
) loop
265 -- A close bracket must follow a open_bracket,
266 -- and cannot be found alone on the line
268 when Close_Bracket
=>
270 ("Incorrect character ']' in regular expression", J
);
278 -- \ not allowed at the end of the regexp
281 ("Incorrect character '\' in regular expression", J
);
286 Parenthesis_Level
:= Parenthesis_Level
+ 1;
288 Add_In_Map
(Open_Paren
);
293 Parenthesis_Level
:= Parenthesis_Level
- 1;
295 if Parenthesis_Level
< 0 then
297 ("')' is not associated with '(' in regular "
301 if S
(J
- 1) = Open_Paren
then
303 ("Empty parenthesis not allowed in regular "
308 Add_In_Map
(Close_Paren
);
320 Curly_Level
:= Curly_Level
+ 1;
327 Curly_Level
:= Curly_Level
- 1;
334 ("'*', '+', '?' and '|' operators cannot be in "
335 & "first position in regular expression", J
);
343 -- These operators must apply to a sub-expression,
344 -- and cannot be found at the beginning of the line
347 ("'*', '+', '?' and '|' operators cannot be in "
348 & "first position in regular expression", J
);
362 -- A closing parenthesis must follow an open parenthesis
364 if Parenthesis_Level
/= 0 then
366 ("'(' must always be associated with a ')'", J
);
369 if Curly_Level
/= 0 then
371 ("'{' must always be associated with a '}'", J
);
375 --------------------------
376 -- Create_Primary_Table --
377 --------------------------
379 procedure Create_Primary_Table
380 (Table
: out Regexp_Array_Access
;
381 Num_States
: out State_Index
;
382 Start_State
: out State_Index
;
383 End_State
: out State_Index
)
385 Empty_Char
: constant Column_Index
:= Alphabet_Size
+ 1;
387 Current_State
: State_Index
:= 0;
388 -- Index of the last created state
390 procedure Add_Empty_Char
391 (State
: State_Index
;
392 To_State
: State_Index
);
393 -- Add a empty-character transition from State to To_State
395 procedure Create_Repetition
396 (Repetition
: Character;
397 Start_Prev
: State_Index
;
398 End_Prev
: State_Index
;
399 New_Start
: out State_Index
;
400 New_End
: in out State_Index
);
401 -- Create the table in case we have a '*', '+' or '?'.
402 -- Start_Prev .. End_Prev should indicate respectively the start and
403 -- end index of the previous expression, to which '*', '+' or '?' is
406 procedure Create_Simple
407 (Start_Index
: Integer;
409 Start_State
: out State_Index
;
410 End_State
: out State_Index
);
411 -- Fill the table for the regexp Simple.
412 -- This is the recursive procedure called to handle () expressions
413 -- If End_State = 0, then the call to Create_Simple creates an
414 -- independent regexp, not a concatenation
415 -- Start_Index .. End_Index is the starting index in the string S.
417 -- Warning: it may look like we are creating too many empty-string
418 -- transitions, but they are needed to get the correct regexp.
419 -- The table is filled as follow ( s means start-state, e means
422 -- regexp state_num | a b * empty_string
423 -- ------- ------------------------------
427 -- ab 1 (s) | 2 - - -
444 -- (a) 1 (s) | 2 - - -
460 function Next_Sub_Expression
461 (Start_Index
: Integer;
464 -- Returns the index of the last character of the next sub-expression
465 -- in Simple. Index cannot be greater than End_Index.
471 procedure Add_Empty_Char
472 (State
: State_Index
;
473 To_State
: State_Index
)
475 J
: Column_Index
:= Empty_Char
;
478 while Get
(Table
, State
, J
) /= 0 loop
482 Set
(Table
, State
, J
, To_State
);
485 -----------------------
486 -- Create_Repetition --
487 -----------------------
489 procedure Create_Repetition
490 (Repetition
: Character;
491 Start_Prev
: State_Index
;
492 End_Prev
: State_Index
;
493 New_Start
: out State_Index
;
494 New_End
: in out State_Index
)
497 New_Start
:= Current_State
+ 1;
500 Add_Empty_Char
(New_End
, New_Start
);
503 Current_State
:= Current_State
+ 2;
504 New_End
:= Current_State
;
506 Add_Empty_Char
(End_Prev
, New_End
);
507 Add_Empty_Char
(New_Start
, Start_Prev
);
509 if Repetition
/= '+' then
510 Add_Empty_Char
(New_Start
, New_End
);
513 if Repetition
/= '?' then
514 Add_Empty_Char
(New_End
, New_Start
);
516 end Create_Repetition
;
522 procedure Create_Simple
523 (Start_Index
: Integer;
525 Start_State
: out State_Index
;
526 End_State
: out State_Index
)
528 J
: Integer := Start_Index
;
529 Last_Start
: State_Index
:= 0;
534 while J
<= End_Index
loop
538 J_Start
: constant Integer := J
+ 1;
539 Next_Start
: State_Index
;
540 Next_End
: State_Index
;
543 J
:= Next_Sub_Expression
(J
, End_Index
);
544 Create_Simple
(J_Start
, J
- 1, Next_Start
, Next_End
);
547 and then (S
(J
+ 1) = '*' or else
548 S
(J
+ 1) = '+' or else
560 Last_Start
:= Next_Start
;
562 if End_State
/= 0 then
563 Add_Empty_Char
(End_State
, Last_Start
);
566 End_State
:= Next_End
;
572 Start_Prev
: constant State_Index
:= Start_State
;
573 End_Prev
: constant State_Index
:= End_State
;
574 Start_J
: constant Integer := J
+ 1;
575 Start_Next
: State_Index
:= 0;
576 End_Next
: State_Index
:= 0;
579 J
:= Next_Sub_Expression
(J
, End_Index
);
581 -- Create a new state for the start of the alternative
583 Current_State
:= Current_State
+ 1;
584 Last_Start
:= Current_State
;
585 Start_State
:= Last_Start
;
587 -- Create the tree for the second part of alternative
589 Create_Simple
(Start_J
, J
, Start_Next
, End_Next
);
591 -- Create the end state
593 Add_Empty_Char
(Last_Start
, Start_Next
);
594 Add_Empty_Char
(Last_Start
, Start_Prev
);
595 Current_State
:= Current_State
+ 1;
596 End_State
:= Current_State
;
597 Add_Empty_Char
(End_Prev
, End_State
);
598 Add_Empty_Char
(End_Next
, End_State
);
602 Current_State
:= Current_State
+ 1;
605 Next_State
: State_Index
:= Current_State
+ 1;
615 for Column
in 0 .. Alphabet_Size
loop
616 Set
(Table
, Current_State
, Column
,
617 Value
=> Current_State
+ 1);
621 -- Automatically add the first character
623 if S
(J
) = '-' or S
(J
) = ']' then
624 Set
(Table
, Current_State
, Map
(S
(J
)),
625 Value
=> Next_State
);
629 -- Loop till closing bracket found
632 exit when S
(J
) = Close_Bracket
;
635 and then S
(J
+ 1) /= ']'
638 Start
: constant Integer := J
- 1;
647 for Char
in S
(Start
) .. S
(J
) loop
648 Set
(Table
, Current_State
, Map
(Char
),
649 Value
=> Next_State
);
658 Set
(Table
, Current_State
, Map
(S
(J
)),
659 Value
=> Next_State
);
665 Current_State
:= Current_State
+ 1;
667 -- If the next symbol is a special symbol
670 and then (S
(J
+ 1) = '*' or else
671 S
(J
+ 1) = '+' or else
683 Last_Start
:= Current_State
- 1;
685 if End_State
/= 0 then
686 Add_Empty_Char
(End_State
, Last_Start
);
689 End_State
:= Current_State
;
692 when '*' |
'+' |
'?' | Close_Paren | Close_Bracket
=>
694 ("Incorrect character in regular expression :", J
);
697 Current_State
:= Current_State
+ 1;
699 -- Create the state for the symbol S (J)
702 for K
in 0 .. Alphabet_Size
loop
703 Set
(Table
, Current_State
, K
,
704 Value
=> Current_State
+ 1);
712 Set
(Table
, Current_State
, Map
(S
(J
)),
713 Value
=> Current_State
+ 1);
716 Current_State
:= Current_State
+ 1;
718 -- If the next symbol is a special symbol
721 and then (S
(J
+ 1) = '*' or else
722 S
(J
+ 1) = '+' or else
734 Last_Start
:= Current_State
- 1;
736 if End_State
/= 0 then
737 Add_Empty_Char
(End_State
, Last_Start
);
740 End_State
:= Current_State
;
745 if Start_State
= 0 then
746 Start_State
:= Last_Start
;
753 -------------------------
754 -- Next_Sub_Expression --
755 -------------------------
757 function Next_Sub_Expression
758 (Start_Index
: Integer;
762 J
: Integer := Start_Index
;
763 Start_On_Alter
: Boolean := False;
767 Start_On_Alter
:= True;
771 exit when J
= End_Index
;
781 exit when S
(J
) = Close_Bracket
;
789 J
:= Next_Sub_Expression
(J
, End_Index
);
795 if Start_On_Alter
then
805 end Next_Sub_Expression
;
807 -- Start of Create_Primary_Table
810 Table
.all := (others => (others => 0));
811 Create_Simple
(S
'First, S
'Last, Start_State
, End_State
);
812 Num_States
:= Current_State
;
813 end Create_Primary_Table
;
815 -------------------------------
816 -- Create_Primary_Table_Glob --
817 -------------------------------
819 procedure Create_Primary_Table_Glob
820 (Table
: out Regexp_Array_Access
;
821 Num_States
: out State_Index
;
822 Start_State
: out State_Index
;
823 End_State
: out State_Index
)
825 Empty_Char
: constant Column_Index
:= Alphabet_Size
+ 1;
827 Current_State
: State_Index
:= 0;
828 -- Index of the last created state
830 procedure Add_Empty_Char
831 (State
: State_Index
;
832 To_State
: State_Index
);
833 -- Add a empty-character transition from State to To_State
835 procedure Create_Simple
836 (Start_Index
: Integer;
838 Start_State
: out State_Index
;
839 End_State
: out State_Index
);
840 -- Fill the table for the S (Start_Index .. End_Index).
841 -- This is the recursive procedure called to handle () expressions
847 procedure Add_Empty_Char
848 (State
: State_Index
;
849 To_State
: State_Index
)
851 J
: Column_Index
:= Empty_Char
;
854 while Get
(Table
, State
, J
) /= 0 loop
858 Set
(Table
, State
, J
,
866 procedure Create_Simple
867 (Start_Index
: Integer;
869 Start_State
: out State_Index
;
870 End_State
: out State_Index
)
872 J
: Integer := Start_Index
;
873 Last_Start
: State_Index
:= 0;
879 while J
<= End_Index
loop
883 Current_State
:= Current_State
+ 1;
886 Next_State
: State_Index
:= Current_State
+ 1;
895 for Column
in 0 .. Alphabet_Size
loop
896 Set
(Table
, Current_State
, Column
,
897 Value
=> Current_State
+ 1);
901 -- Automatically add the first character
903 if S
(J
) = '-' or S
(J
) = ']' then
904 Set
(Table
, Current_State
, Map
(S
(J
)),
905 Value
=> Current_State
);
909 -- Loop till closing bracket found
912 exit when S
(J
) = Close_Bracket
;
915 and then S
(J
+ 1) /= ']'
918 Start
: constant Integer := J
- 1;
926 for Char
in S
(Start
) .. S
(J
) loop
927 Set
(Table
, Current_State
, Map
(Char
),
928 Value
=> Next_State
);
937 Set
(Table
, Current_State
, Map
(S
(J
)),
938 Value
=> Next_State
);
944 Last_Start
:= Current_State
;
945 Current_State
:= Current_State
+ 1;
947 if End_State
/= 0 then
948 Add_Empty_Char
(End_State
, Last_Start
);
951 End_State
:= Current_State
;
956 Start_Regexp_Sub
: State_Index
;
957 End_Regexp_Sub
: State_Index
;
958 Create_Start
: State_Index
:= 0;
960 Create_End
: State_Index
:= 0;
961 -- Initialized to avoid junk warning
964 while S
(J
) /= '}' loop
966 -- First step : find sub pattern
969 while S
(End_Sub
) /= ','
970 and then S
(End_Sub
) /= '}'
972 End_Sub
:= End_Sub
+ 1;
975 -- Second step : create a sub pattern
985 -- Third step : create an alternative
987 if Create_Start
= 0 then
988 Current_State
:= Current_State
+ 1;
989 Create_Start
:= Current_State
;
990 Add_Empty_Char
(Create_Start
, Start_Regexp_Sub
);
991 Current_State
:= Current_State
+ 1;
992 Create_End
:= Current_State
;
993 Add_Empty_Char
(End_Regexp_Sub
, Create_End
);
996 Current_State
:= Current_State
+ 1;
997 Add_Empty_Char
(Current_State
, Create_Start
);
998 Create_Start
:= Current_State
;
999 Add_Empty_Char
(Create_Start
, Start_Regexp_Sub
);
1000 Add_Empty_Char
(End_Regexp_Sub
, Create_End
);
1004 if End_State
/= 0 then
1005 Add_Empty_Char
(End_State
, Create_Start
);
1008 End_State
:= Create_End
;
1009 Last_Start
:= Create_Start
;
1013 Current_State
:= Current_State
+ 1;
1015 if End_State
/= 0 then
1016 Add_Empty_Char
(End_State
, Current_State
);
1019 Add_Empty_Char
(Current_State
, Current_State
+ 1);
1020 Add_Empty_Char
(Current_State
, Current_State
+ 3);
1021 Last_Start
:= Current_State
;
1023 Current_State
:= Current_State
+ 1;
1025 for K
in 0 .. Alphabet_Size
loop
1026 Set
(Table
, Current_State
, K
,
1027 Value
=> Current_State
+ 1);
1030 Current_State
:= Current_State
+ 1;
1031 Add_Empty_Char
(Current_State
, Current_State
+ 1);
1033 Current_State
:= Current_State
+ 1;
1034 Add_Empty_Char
(Current_State
, Last_Start
);
1035 End_State
:= Current_State
;
1038 Current_State
:= Current_State
+ 1;
1041 for K
in 0 .. Alphabet_Size
loop
1042 Set
(Table
, Current_State
, K
,
1043 Value
=> Current_State
+ 1);
1051 -- Create the state for the symbol S (J)
1053 Set
(Table
, Current_State
, Map
(S
(J
)),
1054 Value
=> Current_State
+ 1);
1057 Last_Start
:= Current_State
;
1058 Current_State
:= Current_State
+ 1;
1060 if End_State
/= 0 then
1061 Add_Empty_Char
(End_State
, Last_Start
);
1064 End_State
:= Current_State
;
1068 if Start_State
= 0 then
1069 Start_State
:= Last_Start
;
1076 -- Start of processing for Create_Primary_Table_Glob
1079 Table
.all := (others => (others => 0));
1080 Create_Simple
(S
'First, S
'Last, Start_State
, End_State
);
1081 Num_States
:= Current_State
;
1082 end Create_Primary_Table_Glob
;
1084 ----------------------------
1085 -- Create_Secondary_Table --
1086 ----------------------------
1088 function Create_Secondary_Table
1089 (First_Table
: Regexp_Array_Access
;
1090 Num_States
: State_Index
;
1091 Start_State
: State_Index
;
1092 End_State
: State_Index
) return Regexp
1094 pragma Warnings
(Off
, Num_States
);
1096 Last_Index
: constant State_Index
:= First_Table
'Last (1);
1097 type Meta_State
is array (1 .. Last_Index
) of Boolean;
1099 Table
: Regexp_Array
(1 .. Last_Index
, 0 .. Alphabet_Size
) :=
1100 (others => (others => 0));
1102 Meta_States
: array (1 .. Last_Index
+ 1) of Meta_State
:=
1103 (others => (others => False));
1105 Temp_State_Not_Null
: Boolean;
1107 Is_Final
: Boolean_Array
(1 .. Last_Index
) := (others => False);
1109 Current_State
: State_Index
:= 1;
1110 Nb_State
: State_Index
:= 1;
1113 (State
: in out Meta_State
;
1114 Item
: State_Index
);
1115 -- Compute the closure of the state (that is every other state which
1116 -- has a empty-character transition) and add it to the state
1123 (State
: in out Meta_State
;
1127 if State
(Item
) then
1131 State
(Item
) := True;
1133 for Column
in Alphabet_Size
+ 1 .. First_Table
'Last (2) loop
1134 if First_Table
(Item
, Column
) = 0 then
1138 Closure
(State
, First_Table
(Item
, Column
));
1142 -- Start of procesing for Create_Secondary_Table
1145 -- Create a new state
1147 Closure
(Meta_States
(Current_State
), Start_State
);
1149 while Current_State
<= Nb_State
loop
1151 -- If this new meta-state includes the primary table end state,
1152 -- then this meta-state will be a final state in the regexp
1154 if Meta_States
(Current_State
)(End_State
) then
1155 Is_Final
(Current_State
) := True;
1158 -- For every character in the regexp, calculate the possible
1159 -- transitions from Current_State
1161 for Column
in 0 .. Alphabet_Size
loop
1162 Meta_States
(Nb_State
+ 1) := (others => False);
1163 Temp_State_Not_Null
:= False;
1165 for K
in Meta_States
(Current_State
)'Range loop
1166 if Meta_States
(Current_State
)(K
)
1167 and then First_Table
(K
, Column
) /= 0
1170 (Meta_States
(Nb_State
+ 1), First_Table
(K
, Column
));
1171 Temp_State_Not_Null
:= True;
1175 -- If at least one transition existed
1177 if Temp_State_Not_Null
then
1179 -- Check if this new state corresponds to an old one
1181 for K
in 1 .. Nb_State
loop
1182 if Meta_States
(K
) = Meta_States
(Nb_State
+ 1) then
1183 Table
(Current_State
, Column
) := K
;
1188 -- If not, create a new state
1190 if Table
(Current_State
, Column
) = 0 then
1191 Nb_State
:= Nb_State
+ 1;
1192 Table
(Current_State
, Column
) := Nb_State
;
1197 Current_State
:= Current_State
+ 1;
1200 -- Returns the regexp
1206 R
:= new Regexp_Value
(Alphabet_Size
=> Alphabet_Size
,
1207 Num_States
=> Nb_State
);
1209 R
.Is_Final
:= Is_Final
(1 .. Nb_State
);
1210 R
.Case_Sensitive
:= Case_Sensitive
;
1212 for State
in 1 .. Nb_State
loop
1213 for K
in 0 .. Alphabet_Size
loop
1214 R
.States
(State
, K
) := Table
(State
, K
);
1218 return (Ada
.Finalization
.Controlled
with R
=> R
);
1220 end Create_Secondary_Table
;
1222 ---------------------
1223 -- Raise_Exception --
1224 ---------------------
1226 procedure Raise_Exception
1231 Ada
.Exceptions
.Raise_Exception
1232 (Error_In_Regexp
'Identity, M
& " at offset " & Index
'Img);
1233 end Raise_Exception
;
1235 -- Start of processing for Compile
1238 -- Special case for the empty string: it always matches, and the
1239 -- following processing would fail on it.
1241 return (Ada
.Finalization
.Controlled
with
1242 R
=> new Regexp_Value
'
1243 (Alphabet_Size => 0,
1245 Map => (others => 0),
1246 States => (others => (others => 1)),
1247 Is_Final => (others => True),
1248 Case_Sensitive => True));
1251 if not Case_Sensitive then
1252 System.Case_Util.To_Lower (S);
1257 -- Creates the primary table
1260 Table : Regexp_Array_Access;
1261 Num_States : State_Index;
1262 Start_State : State_Index;
1263 End_State : State_Index;
1267 Table := new Regexp_Array (1 .. 100,
1268 0 .. Alphabet_Size + 10);
1270 Create_Primary_Table (Table, Num_States, Start_State, End_State);
1272 Create_Primary_Table_Glob
1273 (Table, Num_States, Start_State, End_State);
1276 -- Creates the secondary table
1278 R := Create_Secondary_Table
1279 (Table, Num_States, Start_State, End_State);
1289 procedure Finalize (R : in out Regexp) is
1290 procedure Free is new
1291 Ada.Unchecked_Deallocation (Regexp_Value, Regexp_Access);
1302 (Table : Regexp_Array_Access;
1303 State : State_Index;
1304 Column : Column_Index) return State_Index
1307 if State <= Table'Last (1)
1308 and then Column <= Table'Last (2)
1310 return Table (State, Column);
1320 function Match (S : String; R : Regexp) return Boolean is
1321 Current_State : State_Index := 1;
1325 raise Constraint_Error;
1328 for Char in S'Range loop
1330 if R.R.Case_Sensitive then
1331 Current_State := R.R.States (Current_State, R.R.Map (S (Char)));
1334 R.R.States (Current_State,
1335 R.R.Map (System.Case_Util.To_Lower (S (Char))));
1338 if Current_State = 0 then
1344 return R.R.Is_Final (Current_State);
1352 (Table : in out Regexp_Array_Access;
1353 State : State_Index;
1354 Column : Column_Index;
1355 Value : State_Index)
1357 New_Lines : State_Index;
1358 New_Columns : Column_Index;
1359 New_Table : Regexp_Array_Access;
1362 if State <= Table'Last (1)
1363 and then Column <= Table'Last (2)
1365 Table (State, Column) := Value;
1367 -- Doubles the size of the table until it is big enough that
1368 -- (State, Column) is a valid index
1370 New_Lines := Table'Last (1) * (State / Table'Last (1) + 1);
1371 New_Columns := Table'Last (2) * (Column / Table'Last (2) + 1);
1372 New_Table := new Regexp_Array (Table'First (1) .. New_Lines,
1373 Table'First (2) .. New_Columns);
1374 New_Table.all := (others => (others => 0));
1376 for J in Table'Range (1) loop
1377 for K in Table'Range (2) loop
1378 New_Table (J, K) := Table (J, K);
1384 Table (State, Column) := Value;