1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- G N A T . R E G P A T --
9 -- Copyright (C) 1986 by University of Toronto. --
10 -- Copyright (C) 1996-2002 Ada Core Technologies, Inc. --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
23 -- As a special exception, if other files instantiate generics from this --
24 -- unit, or you link this unit with other files to produce an executable, --
25 -- this unit does not by itself cause the resulting executable to be --
26 -- covered by the GNU General Public License. This exception does not --
27 -- however invalidate any other reasons why the executable file might be --
28 -- covered by the GNU Public License. --
30 -- GNAT is maintained by Ada Core Technologies Inc (http://www.gnat.com). --
32 ------------------------------------------------------------------------------
34 -- This is an altered Ada 95 version of the original V8 style regular
35 -- expression library written in C by Henry Spencer. Apart from the
36 -- translation to Ada, the interface has been considerably changed to
37 -- use the Ada String type instead of C-style nul-terminated strings.
39 -- Beware that some of this code is subtly aware of the way operator
40 -- precedence is structured in regular expressions. Serious changes in
41 -- regular-expression syntax might require a total rethink.
43 with System.IO; use System.IO;
44 with Ada.Characters.Handling; use Ada.Characters.Handling;
45 with Unchecked_Conversion;
47 package body GNAT.Regpat is
49 MAGIC : constant Character := Character'Val (10#0234#);
50 -- The first byte of the regexp internal "program" is actually
51 -- this magic number; the start node begins in the second byte.
53 -- This is used to make sure that a regular expression was correctly
56 ----------------------------
57 -- Implementation details --
58 ----------------------------
60 -- This is essentially a linear encoding of a nondeterministic
61 -- finite-state machine, also known as syntax charts or
62 -- "railroad normal form" in parsing technology.
64 -- Each node is an opcode plus a "next" pointer, possibly plus an
65 -- operand. "Next" pointers of all nodes except BRANCH implement
66 -- concatenation; a "next" pointer with a BRANCH on both ends of it
67 -- is connecting two alternatives.
69 -- The operand of some types of node is a literal string; for others,
70 -- it is a node leading into a sub-FSM. In particular, the operand of
71 -- a BRANCH node is the first node of the branch.
72 -- (NB this is *not* a tree structure: the tail of the branch connects
73 -- to the thing following the set of BRANCHes).
75 -- You can see the exact byte-compiled version by using the Dump
76 -- subprogram. However, here are a few examples:
79 -- 2 : BRANCH (next at 10)
80 -- 5 : EXACT (next at 18) operand=a
81 -- 10 : BRANCH (next at 18)
82 -- 13 : EXACT (next at 18) operand=b
83 -- 18 : EOP (next at 0)
86 -- 2 : CURLYX (next at 26) { 0, 32767}
87 -- 9 : OPEN 1 (next at 13)
88 -- 13 : EXACT (next at 19) operand=ab
89 -- 19 : CLOSE 1 (next at 23)
90 -- 23 : WHILEM (next at 0)
91 -- 26 : NOTHING (next at 29)
92 -- 29 : EOP (next at 0)
98 -- Name Operand? Meaning
100 (EOP, -- no End of program
101 MINMOD, -- no Next operator is not greedy
103 -- Classes of characters
105 ANY, -- no Match any one character except newline
106 SANY, -- no Match any character, including new line
107 ANYOF, -- class Match any character in this class
108 EXACT, -- str Match this string exactly
109 EXACTF, -- str Match this string (case-folding is one)
110 NOTHING, -- no Match empty string
111 SPACE, -- no Match any whitespace character
112 NSPACE, -- no Match any non-whitespace character
113 DIGIT, -- no Match any numeric character
114 NDIGIT, -- no Match any non-numeric character
115 ALNUM, -- no Match any alphanumeric character
116 NALNUM, -- no Match any non-alphanumeric character
120 BRANCH, -- node Match this alternative, or the next
122 -- Simple loops (when the following node is one character in length)
124 STAR, -- node Match this simple thing 0 or more times
125 PLUS, -- node Match this simple thing 1 or more times
126 CURLY, -- 2num node Match this simple thing between n and m times.
130 CURLYX, -- 2num node Match this complex thing {n,m} times
131 -- The nums are coded on two characters each.
133 WHILEM, -- no Do curly processing and see if rest matches
135 -- Matches after or before a word
137 BOL, -- no Match "" at beginning of line
138 MBOL, -- no Same, assuming mutiline (match after \n)
139 SBOL, -- no Same, assuming single line (don't match at \n)
140 EOL, -- no Match "" at end of line
141 MEOL, -- no Same, assuming mutiline (match before \n)
142 SEOL, -- no Same, assuming single line (don't match at \n)
144 BOUND, -- no Match "" at any word boundary
145 NBOUND, -- no Match "" at any word non-boundary
147 -- Parenthesis groups handling
149 REFF, -- num Match some already matched string, folded
150 OPEN, -- num Mark this point in input as start of #n
151 CLOSE); -- num Analogous to OPEN
153 for Opcode'Size use 8;
158 -- The set of branches constituting a single choice are hooked
159 -- together with their "next" pointers, since precedence prevents
160 -- anything being concatenated to any individual branch. The
161 -- "next" pointer of the last BRANCH in a choice points to the
162 -- thing following the whole choice. This is also where the
163 -- final "next" pointer of each individual branch points; each
164 -- branch starts with the operand node of a BRANCH node.
167 -- '?', and complex '*' and '+', are implemented with CURLYX.
168 -- branches. Simple cases (one character per match) are implemented with
169 -- STAR and PLUS for speed and to minimize recursive plunges.
172 -- ...are numbered at compile time.
175 -- There are in fact two arguments, the first one is the length (minus
176 -- one of the string argument), coded on one character, the second
177 -- argument is the string itself, coded on length + 1 characters.
179 -- A node is one char of opcode followed by two chars of "next" pointer.
180 -- "Next" pointers are stored as two 8-bit pieces, high order first. The
181 -- value is a positive offset from the opcode of the node containing it.
182 -- An operand, if any, simply follows the node. (Note that much of the
183 -- code generation knows about this implicit relationship.)
185 -- Using two bytes for the "next" pointer is vast overkill for most
186 -- things, but allows patterns to get big without disasters.
188 -----------------------
189 -- Character classes --
190 -----------------------
191 -- This is the implementation for character classes ([...]) in the
192 -- syntax for regular expressions. Each character (0..256) has an
193 -- entry into the table. This makes for a very fast matching
196 type Class_Byte is mod 256;
197 type Character_Class is array (Class_Byte range 0 .. 31) of Class_Byte;
199 type Bit_Conversion_Array is array (Class_Byte range 0 .. 7) of Class_Byte;
200 Bit_Conversion : constant Bit_Conversion_Array :=
201 (1, 2, 4, 8, 16, 32, 64, 128);
203 type Std_Class is (ANYOF_NONE,
204 ANYOF_ALNUM, -- Alphanumeric class [a-zA-Z0-9]
206 ANYOF_SPACE, -- Space class [ \t\n\r\f]
208 ANYOF_DIGIT, -- Digit class [0-9]
210 ANYOF_ALNUMC, -- Alphanumeric class [a-zA-Z0-9]
212 ANYOF_ALPHA, -- Alpha class [a-zA-Z]
214 ANYOF_ASCII, -- Ascii class (7 bits) 0..127
216 ANYOF_CNTRL, -- Control class
218 ANYOF_GRAPH, -- Graphic class
220 ANYOF_LOWER, -- Lower case class [a-z]
222 ANYOF_PRINT, -- printable class
226 ANYOF_UPPER, -- Upper case class [A-Z]
228 ANYOF_XDIGIT, -- Hexadecimal digit
232 procedure Set_In_Class
233 (Bitmap : in out Character_Class;
235 -- Set the entry to True for C in the class Bitmap.
237 function Get_From_Class
238 (Bitmap : Character_Class;
241 -- Return True if the entry is set for C in the class Bitmap.
243 procedure Reset_Class (Bitmap : in out Character_Class);
244 -- Clear all the entries in the class Bitmap.
246 pragma Inline (Set_In_Class);
247 pragma Inline (Get_From_Class);
248 pragma Inline (Reset_Class);
250 -----------------------
251 -- Local Subprograms --
252 -----------------------
254 function "=" (Left : Character; Right : Opcode) return Boolean;
256 function Is_Alnum (C : Character) return Boolean;
257 -- Return True if C is an alphanum character or an underscore ('_')
259 function Is_Space (C : Character) return Boolean;
260 -- Return True if C is a whitespace character
262 function Is_Printable (C : Character) return Boolean;
263 -- Return True if C is a printable character
265 function Operand (P : Pointer) return Pointer;
266 -- Return a pointer to the first operand of the node at P
268 function String_Length
269 (Program : Program_Data;
272 -- Return the length of the string argument of the node at P
274 function String_Operand (P : Pointer) return Pointer;
275 -- Return a pointer to the string argument of the node at P
277 procedure Bitmap_Operand
278 (Program : Program_Data;
280 Op : out Character_Class);
281 -- Return a pointer to the string argument of the node at P
283 function Get_Next_Offset
284 (Program : Program_Data;
287 -- Get the offset field of a node. Used by Get_Next.
290 (Program : Program_Data;
293 -- Dig the next instruction pointer out of a node
295 procedure Optimize (Self : in out Pattern_Matcher);
296 -- Optimize a Pattern_Matcher by noting certain special cases
298 function Read_Natural
299 (Program : Program_Data;
302 -- Return the 2-byte natural coded at position IP.
304 -- All of the subprograms above are tiny and should be inlined
307 pragma Inline (Is_Alnum);
308 pragma Inline (Is_Space);
309 pragma Inline (Get_Next);
310 pragma Inline (Get_Next_Offset);
311 pragma Inline (Operand);
312 pragma Inline (Read_Natural);
313 pragma Inline (String_Length);
314 pragma Inline (String_Operand);
316 type Expression_Flags is record
317 Has_Width, -- Known never to match null string
318 Simple, -- Simple enough to be STAR/PLUS operand
319 SP_Start : Boolean; -- Starts with * or +
322 Worst_Expression : constant Expression_Flags := (others => False);
329 function "=" (Left : Character; Right : Opcode) return Boolean is
331 return Character'Pos (Left) = Opcode'Pos (Right);
338 procedure Bitmap_Operand
339 (Program : Program_Data;
341 Op : out Character_Class)
343 function Convert is new Unchecked_Conversion
344 (Program_Data, Character_Class);
347 Op (0 .. 31) := Convert (Program (P + 3 .. P + 34));
355 (Matcher : out Pattern_Matcher;
357 Final_Code_Size : out Program_Size;
358 Flags : Regexp_Flags := No_Flags)
360 -- We can't allocate space until we know how big the compiled form
361 -- will be, but we can't compile it (and thus know how big it is)
362 -- until we've got a place to put the code. So we cheat: we compile
363 -- it twice, once with code generation turned off and size counting
364 -- turned on, and once "for real".
366 -- This also means that we don't allocate space until we are sure
367 -- that the thing really will compile successfully, and we never
368 -- have to move the code and thus invalidate pointers into it.
370 -- Beware that the optimization-preparation code in here knows
371 -- about some of the structure of the compiled regexp.
373 PM : Pattern_Matcher renames Matcher;
374 Program : Program_Data renames PM.Program;
376 Emit_Code : constant Boolean := PM.Size > 0;
377 Emit_Ptr : Pointer := Program_First;
379 Parse_Pos : Natural := Expression'First; -- Input-scan pointer
380 Parse_End : Natural := Expression'Last;
382 ----------------------------
383 -- Subprograms for Create --
384 ----------------------------
386 procedure Emit (B : Character);
387 -- Output the Character to the Program.
388 -- If code-generation is disables, simply increments the program
391 function Emit_Node (Op : Opcode) return Pointer;
392 -- If code-generation is enabled, Emit_Node outputs the
393 -- opcode and reserves space for a pointer to the next node.
394 -- Return value is the location of new opcode, ie old Emit_Ptr.
396 procedure Emit_Natural (IP : Pointer; N : Natural);
397 -- Split N on two characters at position IP.
399 procedure Emit_Class (Bitmap : Character_Class);
400 -- Emits a character class.
402 procedure Case_Emit (C : Character);
403 -- Emit C, after converting is to lower-case if the regular
404 -- expression is case insensitive.
407 (Parenthesized : Boolean;
408 Flags : in out Expression_Flags;
410 -- Parse regular expression, i.e. main body or parenthesized thing
411 -- Caller must absorb opening parenthesis.
413 procedure Parse_Branch
414 (Flags : in out Expression_Flags;
417 -- Implements the concatenation operator and handles '|'
418 -- First should be true if this is the first item of the alternative.
420 procedure Parse_Piece
421 (Expr_Flags : in out Expression_Flags; IP : out Pointer);
422 -- Parse something followed by possible [*+?]
425 (Expr_Flags : in out Expression_Flags; IP : out Pointer);
426 -- Parse_Atom is the lowest level parse procedure.
427 -- Optimization: gobbles an entire sequence of ordinary characters
428 -- so that it can turn them into a single node, which is smaller to
429 -- store and faster to run. Backslashed characters are exceptions,
430 -- each becoming a separate node; the code is simpler that way and
431 -- it's not worth fixing.
433 procedure Insert_Operator
436 Greedy : Boolean := True);
437 -- Insert_Operator inserts an operator in front of an
438 -- already-emitted operand and relocates the operand.
439 -- This applies to PLUS and STAR.
440 -- If Minmod is True, then the operator is non-greedy.
442 procedure Insert_Curly_Operator
447 Greedy : Boolean := True);
448 -- Insert an operator for CURLY ({Min}, {Min,} or {Min,Max}).
449 -- If Minmod is True, then the operator is non-greedy.
451 procedure Link_Tail (P, Val : Pointer);
452 -- Link_Tail sets the next-pointer at the end of a node chain
454 procedure Link_Operand_Tail (P, Val : Pointer);
455 -- Link_Tail on operand of first argument; nop if operandless
457 function Next_Instruction (P : Pointer) return Pointer;
458 -- Dig the "next" pointer out of a node
460 procedure Fail (M : in String);
461 pragma No_Return (Fail);
462 -- Fail with a diagnostic message, if possible
464 function Is_Curly_Operator (IP : Natural) return Boolean;
465 -- Return True if IP is looking at a '{' that is the beginning
466 -- of a curly operator, ie it matches {\d+,?\d*}
468 function Is_Mult (IP : Natural) return Boolean;
469 -- Return True if C is a regexp multiplier: '+', '*' or '?'
471 procedure Get_Curly_Arguments
475 Greedy : out Boolean);
476 -- Parse the argument list for a curly operator.
477 -- It is assumed that IP is indeed pointing at a valid operator.
479 procedure Parse_Character_Class (IP : out Pointer);
480 -- Parse a character class.
481 -- The calling subprogram should consume the opening '[' before.
483 procedure Parse_Literal (Expr_Flags : in out Expression_Flags;
485 -- Parse_Literal encodes a string of characters
486 -- to be matched exactly.
488 function Parse_Posix_Character_Class return Std_Class;
489 -- Parse a posic character class, like [:alpha:] or [:^alpha:].
490 -- The called is suppoed to absorbe the opening [.
492 pragma Inline (Is_Mult);
493 pragma Inline (Emit_Natural);
494 pragma Inline (Parse_Character_Class); -- since used only once
500 procedure Case_Emit (C : Character) is
502 if (Flags and Case_Insensitive) /= 0 then
506 -- Dump current character
516 procedure Emit (B : Character) is
519 Program (Emit_Ptr) := B;
522 Emit_Ptr := Emit_Ptr + 1;
529 procedure Emit_Class (Bitmap : Character_Class) is
530 subtype Program31 is Program_Data (0 .. 31);
532 function Convert is new Unchecked_Conversion
533 (Character_Class, Program31);
537 Program (Emit_Ptr .. Emit_Ptr + 31) := Convert (Bitmap);
540 Emit_Ptr := Emit_Ptr + 32;
547 procedure Emit_Natural (IP : Pointer; N : Natural) is
550 Program (IP + 1) := Character'Val (N / 256);
551 Program (IP) := Character'Val (N mod 256);
559 function Emit_Node (Op : Opcode) return Pointer is
560 Result : constant Pointer := Emit_Ptr;
564 Program (Emit_Ptr) := Character'Val (Opcode'Pos (Op));
565 Program (Emit_Ptr + 1) := ASCII.NUL;
566 Program (Emit_Ptr + 2) := ASCII.NUL;
569 Emit_Ptr := Emit_Ptr + 3;
577 procedure Fail (M : in String) is
579 raise Expression_Error;
582 -------------------------
583 -- Get_Curly_Arguments --
584 -------------------------
586 procedure Get_Curly_Arguments
590 Greedy : out Boolean)
592 pragma Warnings (Off, IP);
594 Save_Pos : Natural := Parse_Pos + 1;
598 Max := Max_Curly_Repeat;
600 while Expression (Parse_Pos) /= '}'
601 and then Expression (Parse_Pos) /= ','
603 Parse_Pos := Parse_Pos + 1;
606 Min := Natural'Value (Expression (Save_Pos .. Parse_Pos - 1));
608 if Expression (Parse_Pos) = ',' then
609 Save_Pos := Parse_Pos + 1;
610 while Expression (Parse_Pos) /= '}' loop
611 Parse_Pos := Parse_Pos + 1;
614 if Save_Pos /= Parse_Pos then
615 Max := Natural'Value (Expression (Save_Pos .. Parse_Pos - 1));
622 if Parse_Pos < Expression'Last
623 and then Expression (Parse_Pos + 1) = '?'
626 Parse_Pos := Parse_Pos + 1;
631 end Get_Curly_Arguments;
633 ---------------------------
634 -- Insert_Curly_Operator --
635 ---------------------------
637 procedure Insert_Curly_Operator
642 Greedy : Boolean := True)
644 Dest : constant Pointer := Emit_Ptr;
649 -- If the operand is not greedy, insert an extra operand before it
655 -- Move the operand in the byte-compilation, so that we can insert
656 -- the operator before it.
659 Program (Operand + Size .. Emit_Ptr + Size) :=
660 Program (Operand .. Emit_Ptr);
663 -- Insert the operator at the position previously occupied by the
669 Old := Emit_Node (MINMOD);
670 Link_Tail (Old, Old + 3);
673 Old := Emit_Node (Op);
674 Emit_Natural (Old + 3, Min);
675 Emit_Natural (Old + 5, Max);
677 Emit_Ptr := Dest + Size;
678 end Insert_Curly_Operator;
680 ---------------------
681 -- Insert_Operator --
682 ---------------------
684 procedure Insert_Operator
687 Greedy : Boolean := True)
689 Dest : constant Pointer := Emit_Ptr;
694 -- If not greedy, we have to emit another opcode first
700 -- Move the operand in the byte-compilation, so that we can insert
701 -- the operator before it.
704 Program (Operand + Size .. Emit_Ptr + Size)
705 := Program (Operand .. Emit_Ptr);
708 -- Insert the operator at the position previously occupied by the
714 Old := Emit_Node (MINMOD);
715 Link_Tail (Old, Old + 3);
718 Old := Emit_Node (Op);
719 Emit_Ptr := Dest + Size;
722 -----------------------
723 -- Is_Curly_Operator --
724 -----------------------
726 function Is_Curly_Operator (IP : Natural) return Boolean is
727 Scan : Natural := IP;
730 if Expression (Scan) /= '{'
731 or else Scan + 2 > Expression'Last
732 or else not Is_Digit (Expression (Scan + 1))
744 if Scan > Expression'Last then
748 exit when not Is_Digit (Expression (Scan));
751 if Expression (Scan) = ',' then
755 if Scan > Expression'Last then
759 exit when not Is_Digit (Expression (Scan));
763 return Expression (Scan) = '}';
764 end Is_Curly_Operator;
770 function Is_Mult (IP : Natural) return Boolean is
771 C : constant Character := Expression (IP);
777 or else (C = '{' and then Is_Curly_Operator (IP));
780 -----------------------
781 -- Link_Operand_Tail --
782 -----------------------
784 procedure Link_Operand_Tail (P, Val : Pointer) is
786 if Emit_Code and then Program (P) = BRANCH then
787 Link_Tail (Operand (P), Val);
789 end Link_Operand_Tail;
795 procedure Link_Tail (P, Val : Pointer) is
801 if not Emit_Code then
809 Temp := Next_Instruction (Scan);
814 Offset := Val - Scan;
816 Emit_Natural (Scan + 1, Natural (Offset));
819 ----------------------
820 -- Next_Instruction --
821 ----------------------
823 function Next_Instruction (P : Pointer) return Pointer is
827 if not Emit_Code then
831 Offset := Get_Next_Offset (Program, P);
838 end Next_Instruction;
844 -- Combining parenthesis handling with the base level
845 -- of regular expression is a trifle forced, but the
846 -- need to tie the tails of the branches to what follows
847 -- makes it hard to avoid.
850 (Parenthesized : in Boolean;
851 Flags : in out Expression_Flags;
854 E : String renames Expression;
858 New_Flags : Expression_Flags;
859 Have_Branch : Boolean := False;
862 Flags := (Has_Width => True, others => False); -- Tentatively
864 -- Make an OPEN node, if parenthesized
866 if Parenthesized then
867 if Matcher.Paren_Count > Max_Paren_Count then
868 Fail ("too many ()");
871 Par_No := Matcher.Paren_Count + 1;
872 Matcher.Paren_Count := Matcher.Paren_Count + 1;
873 IP := Emit_Node (OPEN);
874 Emit (Character'Val (Par_No));
881 -- Pick up the branches, linking them together
883 Parse_Branch (New_Flags, True, Br);
890 if Parse_Pos <= Parse_End
891 and then E (Parse_Pos) = '|'
893 Insert_Operator (BRANCH, Br);
898 Link_Tail (IP, Br); -- OPEN -> first
903 if not New_Flags.Has_Width then
904 Flags.Has_Width := False;
907 Flags.SP_Start := Flags.SP_Start or New_Flags.SP_Start;
909 while Parse_Pos <= Parse_End
910 and then (E (Parse_Pos) = '|')
912 Parse_Pos := Parse_Pos + 1;
913 Parse_Branch (New_Flags, False, Br);
920 Link_Tail (IP, Br); -- BRANCH -> BRANCH
922 if not New_Flags.Has_Width then
923 Flags.Has_Width := False;
926 Flags.SP_Start := Flags.SP_Start or New_Flags.SP_Start;
929 -- Make a closing node, and hook it on the end
931 if Parenthesized then
932 Ender := Emit_Node (CLOSE);
933 Emit (Character'Val (Par_No));
935 Ender := Emit_Node (EOP);
938 Link_Tail (IP, Ender);
942 -- Hook the tails of the branches to the closing node
947 Link_Operand_Tail (Br, Ender);
948 Br := Next_Instruction (Br);
952 -- Check for proper termination
954 if Parenthesized then
955 if Parse_Pos > Parse_End or else E (Parse_Pos) /= ')' then
956 Fail ("unmatched ()");
959 Parse_Pos := Parse_Pos + 1;
961 elsif Parse_Pos <= Parse_End then
962 if E (Parse_Pos) = ')' then
963 Fail ("unmatched ()");
965 Fail ("junk on end"); -- "Can't happen"
975 (Expr_Flags : in out Expression_Flags;
981 -- Tentatively set worst expression case
983 Expr_Flags := Worst_Expression;
985 C := Expression (Parse_Pos);
986 Parse_Pos := Parse_Pos + 1;
990 if (Flags and Multiple_Lines) /= 0 then
991 IP := Emit_Node (MBOL);
992 elsif (Flags and Single_Line) /= 0 then
993 IP := Emit_Node (SBOL);
995 IP := Emit_Node (BOL);
999 if (Flags and Multiple_Lines) /= 0 then
1000 IP := Emit_Node (MEOL);
1001 elsif (Flags and Single_Line) /= 0 then
1002 IP := Emit_Node (SEOL);
1004 IP := Emit_Node (EOL);
1008 if (Flags and Single_Line) /= 0 then
1009 IP := Emit_Node (SANY);
1011 IP := Emit_Node (ANY);
1014 Expr_Flags.Has_Width := True;
1015 Expr_Flags.Simple := True;
1018 Parse_Character_Class (IP);
1019 Expr_Flags.Has_Width := True;
1020 Expr_Flags.Simple := True;
1024 New_Flags : Expression_Flags;
1027 Parse (True, New_Flags, IP);
1033 Expr_Flags.Has_Width :=
1034 Expr_Flags.Has_Width or New_Flags.Has_Width;
1035 Expr_Flags.SP_Start :=
1036 Expr_Flags.SP_Start or New_Flags.SP_Start;
1039 when '|' | ASCII.LF | ')' =>
1040 Fail ("internal urp"); -- Supposed to be caught earlier
1042 when '?' | '+' | '*' | '{' =>
1043 Fail ("?+*{ follows nothing");
1046 if Parse_Pos > Parse_End then
1047 Fail ("trailing \");
1050 Parse_Pos := Parse_Pos + 1;
1052 case Expression (Parse_Pos - 1) is
1054 IP := Emit_Node (BOUND);
1057 IP := Emit_Node (NBOUND);
1060 IP := Emit_Node (SPACE);
1061 Expr_Flags.Simple := True;
1062 Expr_Flags.Has_Width := True;
1065 IP := Emit_Node (NSPACE);
1066 Expr_Flags.Simple := True;
1067 Expr_Flags.Has_Width := True;
1070 IP := Emit_Node (DIGIT);
1071 Expr_Flags.Simple := True;
1072 Expr_Flags.Has_Width := True;
1075 IP := Emit_Node (NDIGIT);
1076 Expr_Flags.Simple := True;
1077 Expr_Flags.Has_Width := True;
1080 IP := Emit_Node (ALNUM);
1081 Expr_Flags.Simple := True;
1082 Expr_Flags.Has_Width := True;
1085 IP := Emit_Node (NALNUM);
1086 Expr_Flags.Simple := True;
1087 Expr_Flags.Has_Width := True;
1090 IP := Emit_Node (SBOL);
1093 IP := Emit_Node (SEOL);
1096 IP := Emit_Node (REFF);
1099 Save : Natural := Parse_Pos - 1;
1102 while Parse_Pos <= Expression'Last
1103 and then Is_Digit (Expression (Parse_Pos))
1105 Parse_Pos := Parse_Pos + 1;
1108 Emit (Character'Val (Natural'Value
1109 (Expression (Save .. Parse_Pos - 1))));
1113 Parse_Pos := Parse_Pos - 1;
1114 Parse_Literal (Expr_Flags, IP);
1118 Parse_Literal (Expr_Flags, IP);
1126 procedure Parse_Branch
1127 (Flags : in out Expression_Flags;
1131 E : String renames Expression;
1134 New_Flags : Expression_Flags;
1138 Flags := Worst_Expression; -- Tentatively
1143 IP := Emit_Node (BRANCH);
1148 while Parse_Pos <= Parse_End
1149 and then E (Parse_Pos) /= ')'
1150 and then E (Parse_Pos) /= ASCII.LF
1151 and then E (Parse_Pos) /= '|'
1153 Parse_Piece (New_Flags, Last);
1160 Flags.Has_Width := Flags.Has_Width or New_Flags.Has_Width;
1162 if Chain = 0 then -- First piece
1163 Flags.SP_Start := Flags.SP_Start or New_Flags.SP_Start;
1165 Link_Tail (Chain, Last);
1171 if Chain = 0 then -- Loop ran zero CURLY
1172 Dummy := Emit_Node (NOTHING);
1177 ---------------------------
1178 -- Parse_Character_Class --
1179 ---------------------------
1181 procedure Parse_Character_Class (IP : out Pointer) is
1182 Bitmap : Character_Class;
1183 Invert : Boolean := False;
1184 In_Range : Boolean := False;
1185 Named_Class : Std_Class := ANYOF_NONE;
1187 Last_Value : Character := ASCII.Nul;
1190 Reset_Class (Bitmap);
1192 -- Do we have an invert character class ?
1194 if Parse_Pos <= Parse_End
1195 and then Expression (Parse_Pos) = '^'
1198 Parse_Pos := Parse_Pos + 1;
1201 -- First character can be ] or -, without closing the class.
1203 if Parse_Pos <= Parse_End
1204 and then (Expression (Parse_Pos) = ']'
1205 or else Expression (Parse_Pos) = '-')
1207 Set_In_Class (Bitmap, Expression (Parse_Pos));
1208 Parse_Pos := Parse_Pos + 1;
1211 -- While we don't have the end of the class
1213 while Parse_Pos <= Parse_End
1214 and then Expression (Parse_Pos) /= ']'
1216 Named_Class := ANYOF_NONE;
1217 Value := Expression (Parse_Pos);
1218 Parse_Pos := Parse_Pos + 1;
1220 -- Do we have a Posix character class
1222 Named_Class := Parse_Posix_Character_Class;
1224 elsif Value = '\' then
1225 if Parse_Pos = Parse_End then
1226 Fail ("Trailing \");
1228 Value := Expression (Parse_Pos);
1229 Parse_Pos := Parse_Pos + 1;
1232 when 'w' => Named_Class := ANYOF_ALNUM;
1233 when 'W' => Named_Class := ANYOF_NALNUM;
1234 when 's' => Named_Class := ANYOF_SPACE;
1235 when 'S' => Named_Class := ANYOF_NSPACE;
1236 when 'd' => Named_Class := ANYOF_DIGIT;
1237 when 'D' => Named_Class := ANYOF_NDIGIT;
1238 when 'n' => Value := ASCII.LF;
1239 when 'r' => Value := ASCII.CR;
1240 when 't' => Value := ASCII.HT;
1241 when 'f' => Value := ASCII.FF;
1242 when 'e' => Value := ASCII.ESC;
1243 when 'a' => Value := ASCII.BEL;
1245 -- when 'x' => ??? hexadecimal value
1246 -- when 'c' => ??? control character
1247 -- when '0'..'9' => ??? octal character
1249 when others => null;
1253 -- Do we have a character class?
1255 if Named_Class /= ANYOF_NONE then
1257 -- A range like 'a-\d' or 'a-[:digit:] is not a range
1260 Set_In_Class (Bitmap, Last_Value);
1261 Set_In_Class (Bitmap, '-');
1268 when ANYOF_NONE => null;
1270 when ANYOF_ALNUM | ANYOF_ALNUMC =>
1271 for Value in Class_Byte'Range loop
1272 if Is_Alnum (Character'Val (Value)) then
1273 Set_In_Class (Bitmap, Character'Val (Value));
1277 when ANYOF_NALNUM | ANYOF_NALNUMC =>
1278 for Value in Class_Byte'Range loop
1279 if not Is_Alnum (Character'Val (Value)) then
1280 Set_In_Class (Bitmap, Character'Val (Value));
1285 for Value in Class_Byte'Range loop
1286 if Is_Space (Character'Val (Value)) then
1287 Set_In_Class (Bitmap, Character'Val (Value));
1291 when ANYOF_NSPACE =>
1292 for Value in Class_Byte'Range loop
1293 if not Is_Space (Character'Val (Value)) then
1294 Set_In_Class (Bitmap, Character'Val (Value));
1299 for Value in Class_Byte'Range loop
1300 if Is_Digit (Character'Val (Value)) then
1301 Set_In_Class (Bitmap, Character'Val (Value));
1305 when ANYOF_NDIGIT =>
1306 for Value in Class_Byte'Range loop
1307 if not Is_Digit (Character'Val (Value)) then
1308 Set_In_Class (Bitmap, Character'Val (Value));
1313 for Value in Class_Byte'Range loop
1314 if Is_Letter (Character'Val (Value)) then
1315 Set_In_Class (Bitmap, Character'Val (Value));
1319 when ANYOF_NALPHA =>
1320 for Value in Class_Byte'Range loop
1321 if not Is_Letter (Character'Val (Value)) then
1322 Set_In_Class (Bitmap, Character'Val (Value));
1327 for Value in 0 .. 127 loop
1328 Set_In_Class (Bitmap, Character'Val (Value));
1331 when ANYOF_NASCII =>
1332 for Value in 128 .. 255 loop
1333 Set_In_Class (Bitmap, Character'Val (Value));
1337 for Value in Class_Byte'Range loop
1338 if Is_Control (Character'Val (Value)) then
1339 Set_In_Class (Bitmap, Character'Val (Value));
1343 when ANYOF_NCNTRL =>
1344 for Value in Class_Byte'Range loop
1345 if not Is_Control (Character'Val (Value)) then
1346 Set_In_Class (Bitmap, Character'Val (Value));
1351 for Value in Class_Byte'Range loop
1352 if Is_Graphic (Character'Val (Value)) then
1353 Set_In_Class (Bitmap, Character'Val (Value));
1357 when ANYOF_NGRAPH =>
1358 for Value in Class_Byte'Range loop
1359 if not Is_Graphic (Character'Val (Value)) then
1360 Set_In_Class (Bitmap, Character'Val (Value));
1365 for Value in Class_Byte'Range loop
1366 if Is_Lower (Character'Val (Value)) then
1367 Set_In_Class (Bitmap, Character'Val (Value));
1371 when ANYOF_NLOWER =>
1372 for Value in Class_Byte'Range loop
1373 if not Is_Lower (Character'Val (Value)) then
1374 Set_In_Class (Bitmap, Character'Val (Value));
1379 for Value in Class_Byte'Range loop
1380 if Is_Printable (Character'Val (Value)) then
1381 Set_In_Class (Bitmap, Character'Val (Value));
1385 when ANYOF_NPRINT =>
1386 for Value in Class_Byte'Range loop
1387 if not Is_Printable (Character'Val (Value)) then
1388 Set_In_Class (Bitmap, Character'Val (Value));
1393 for Value in Class_Byte'Range loop
1394 if Is_Printable (Character'Val (Value))
1395 and then not Is_Space (Character'Val (Value))
1396 and then not Is_Alnum (Character'Val (Value))
1398 Set_In_Class (Bitmap, Character'Val (Value));
1402 when ANYOF_NPUNCT =>
1403 for Value in Class_Byte'Range loop
1404 if not Is_Printable (Character'Val (Value))
1405 or else Is_Space (Character'Val (Value))
1406 or else Is_Alnum (Character'Val (Value))
1408 Set_In_Class (Bitmap, Character'Val (Value));
1413 for Value in Class_Byte'Range loop
1414 if Is_Upper (Character'Val (Value)) then
1415 Set_In_Class (Bitmap, Character'Val (Value));
1419 when ANYOF_NUPPER =>
1420 for Value in Class_Byte'Range loop
1421 if not Is_Upper (Character'Val (Value)) then
1422 Set_In_Class (Bitmap, Character'Val (Value));
1426 when ANYOF_XDIGIT =>
1427 for Value in Class_Byte'Range loop
1428 if Is_Hexadecimal_Digit (Character'Val (Value)) then
1429 Set_In_Class (Bitmap, Character'Val (Value));
1433 when ANYOF_NXDIGIT =>
1434 for Value in Class_Byte'Range loop
1435 if not Is_Hexadecimal_Digit
1436 (Character'Val (Value))
1438 Set_In_Class (Bitmap, Character'Val (Value));
1444 -- Not a character range
1446 elsif not In_Range then
1447 Last_Value := Value;
1449 if Expression (Parse_Pos) = '-'
1450 and then Parse_Pos < Parse_End
1451 and then Expression (Parse_Pos + 1) /= ']'
1453 Parse_Pos := Parse_Pos + 1;
1455 -- Do we have a range like '\d-a' and '[:space:]-a'
1456 -- which is not a real range
1458 if Named_Class /= ANYOF_NONE then
1459 Set_In_Class (Bitmap, '-');
1465 Set_In_Class (Bitmap, Value);
1469 -- Else in a character range
1472 if Last_Value > Value then
1473 Fail ("Invalid Range [" & Last_Value'Img
1474 & "-" & Value'Img & "]");
1477 while Last_Value <= Value loop
1478 Set_In_Class (Bitmap, Last_Value);
1479 Last_Value := Character'Succ (Last_Value);
1488 -- Optimize case-insensitive ranges (put the upper case or lower
1489 -- case character into the bitmap)
1491 if (Flags and Case_Insensitive) /= 0 then
1492 for C in Character'Range loop
1493 if Get_From_Class (Bitmap, C) then
1494 Set_In_Class (Bitmap, To_Lower (C));
1495 Set_In_Class (Bitmap, To_Upper (C));
1500 -- Optimize inverted classes
1503 for J in Bitmap'Range loop
1504 Bitmap (J) := not Bitmap (J);
1508 Parse_Pos := Parse_Pos + 1;
1512 IP := Emit_Node (ANYOF);
1513 Emit_Class (Bitmap);
1514 end Parse_Character_Class;
1520 -- This is a bit tricky due to quoted chars and due to
1521 -- the multiplier characters '*', '+', and '?' that
1522 -- take the SINGLE char previous as their operand.
1524 -- On entry, the character at Parse_Pos - 1 is going to go
1525 -- into the string, no matter what it is. It could be
1526 -- following a \ if Parse_Atom was entered from the '\' case.
1528 -- Basic idea is to pick up a good char in C and examine
1529 -- the next char. If Is_Mult (C) then twiddle, if it's a \
1530 -- then frozzle and if it's another magic char then push C and
1531 -- terminate the string. If none of the above, push C on the
1532 -- string and go around again.
1534 -- Start_Pos is used to remember where "the current character"
1535 -- starts in the string, if due to an Is_Mult we need to back
1536 -- up and put the current char in a separate 1-character string.
1537 -- When Start_Pos is 0, C is the only char in the string;
1538 -- this is used in Is_Mult handling, and in setting the SIMPLE
1541 procedure Parse_Literal
1542 (Expr_Flags : in out Expression_Flags;
1545 Start_Pos : Natural := 0;
1547 Length_Ptr : Pointer;
1548 Has_Special_Operator : Boolean := False;
1551 Parse_Pos := Parse_Pos - 1; -- Look at current character
1553 if (Flags and Case_Insensitive) /= 0 then
1554 IP := Emit_Node (EXACTF);
1556 IP := Emit_Node (EXACT);
1559 Length_Ptr := Emit_Ptr;
1560 Emit_Ptr := String_Operand (IP);
1565 C := Expression (Parse_Pos); -- Get current character
1568 when '.' | '[' | '(' | ')' | '|' | ASCII.LF | '$' | '^' =>
1570 if Start_Pos = 0 then
1571 Start_Pos := Parse_Pos;
1572 Emit (C); -- First character is always emitted
1574 exit Parse_Loop; -- Else we are done
1577 when '?' | '+' | '*' | '{' =>
1579 if Start_Pos = 0 then
1580 Start_Pos := Parse_Pos;
1581 Emit (C); -- First character is always emitted
1583 -- Are we looking at an operator, or is this
1584 -- simply a normal character ?
1585 elsif not Is_Mult (Parse_Pos) then
1586 Start_Pos := Parse_Pos;
1589 -- We've got something like "abc?d". Mark this as a
1590 -- special case. What we want to emit is a first
1591 -- constant string for "ab", then one for "c" that will
1592 -- ultimately be transformed with a CURLY operator, A
1593 -- special case has to be handled for "a?", since there
1594 -- is no initial string to emit.
1595 Has_Special_Operator := True;
1600 Start_Pos := Parse_Pos;
1601 if Parse_Pos = Parse_End then
1602 Fail ("Trailing \");
1604 case Expression (Parse_Pos + 1) is
1605 when 'b' | 'B' | 's' | 'S' | 'd' | 'D'
1606 | 'w' | 'W' | '0' .. '9' | 'G' | 'A'
1608 when 'n' => Emit (ASCII.LF);
1609 when 't' => Emit (ASCII.HT);
1610 when 'r' => Emit (ASCII.CR);
1611 when 'f' => Emit (ASCII.FF);
1612 when 'e' => Emit (ASCII.ESC);
1613 when 'a' => Emit (ASCII.BEL);
1614 when others => Emit (Expression (Parse_Pos + 1));
1616 Parse_Pos := Parse_Pos + 1;
1620 Start_Pos := Parse_Pos;
1624 exit Parse_Loop when Emit_Ptr - Length_Ptr = 254;
1626 Parse_Pos := Parse_Pos + 1;
1628 exit Parse_Loop when Parse_Pos > Parse_End;
1629 end loop Parse_Loop;
1631 -- Is the string followed by a '*+?{' operator ? If yes, and if there
1632 -- is an initial string to emit, do it now.
1634 if Has_Special_Operator
1635 and then Emit_Ptr >= Length_Ptr + 3
1637 Emit_Ptr := Emit_Ptr - 1;
1638 Parse_Pos := Start_Pos;
1642 Program (Length_Ptr) := Character'Val (Emit_Ptr - Length_Ptr - 2);
1645 Expr_Flags.Has_Width := True;
1647 -- Slight optimization when there is a single character
1649 if Emit_Ptr = Length_Ptr + 2 then
1650 Expr_Flags.Simple := True;
1658 -- Note that the branching code sequences used for '?' and the
1659 -- general cases of '*' and + are somewhat optimized: they use
1660 -- the same NOTHING node as both the endmarker for their branch
1661 -- list and the body of the last branch. It might seem that
1662 -- this node could be dispensed with entirely, but the endmarker
1663 -- role is not redundant.
1665 procedure Parse_Piece
1666 (Expr_Flags : in out Expression_Flags;
1670 New_Flags : Expression_Flags;
1671 Greedy : Boolean := True;
1674 Parse_Atom (New_Flags, IP);
1680 if Parse_Pos > Parse_End
1681 or else not Is_Mult (Parse_Pos)
1683 Expr_Flags := New_Flags;
1687 Op := Expression (Parse_Pos);
1690 Expr_Flags := (SP_Start => True, others => False);
1692 Expr_Flags := (Has_Width => True, others => False);
1695 -- Detect non greedy operators in the easy cases
1698 and then Parse_Pos + 1 <= Parse_End
1699 and then Expression (Parse_Pos + 1) = '?'
1702 Parse_Pos := Parse_Pos + 1;
1705 -- Generate the byte code
1710 if New_Flags.Simple then
1711 Insert_Operator (STAR, IP, Greedy);
1713 Link_Tail (IP, Emit_Node (WHILEM));
1714 Insert_Curly_Operator
1715 (CURLYX, 0, Max_Curly_Repeat, IP, Greedy);
1716 Link_Tail (IP, Emit_Node (NOTHING));
1721 if New_Flags.Simple then
1722 Insert_Operator (PLUS, IP, Greedy);
1724 Link_Tail (IP, Emit_Node (WHILEM));
1725 Insert_Curly_Operator
1726 (CURLYX, 1, Max_Curly_Repeat, IP, Greedy);
1727 Link_Tail (IP, Emit_Node (NOTHING));
1731 if New_Flags.Simple then
1732 Insert_Curly_Operator (CURLY, 0, 1, IP, Greedy);
1734 Link_Tail (IP, Emit_Node (WHILEM));
1735 Insert_Curly_Operator (CURLYX, 0, 1, IP, Greedy);
1736 Link_Tail (IP, Emit_Node (NOTHING));
1744 Get_Curly_Arguments (Parse_Pos, Min, Max, Greedy);
1746 if New_Flags.Simple then
1747 Insert_Curly_Operator (CURLY, Min, Max, IP, Greedy);
1749 Link_Tail (IP, Emit_Node (WHILEM));
1750 Insert_Curly_Operator (CURLYX, Min, Max, IP, Greedy);
1751 Link_Tail (IP, Emit_Node (NOTHING));
1759 Parse_Pos := Parse_Pos + 1;
1761 if Parse_Pos <= Parse_End
1762 and then Is_Mult (Parse_Pos)
1764 Fail ("nested *+{");
1768 ---------------------------------
1769 -- Parse_Posix_Character_Class --
1770 ---------------------------------
1772 function Parse_Posix_Character_Class return Std_Class is
1773 Invert : Boolean := False;
1774 Class : Std_Class := ANYOF_NONE;
1775 E : String renames Expression;
1778 if Parse_Pos <= Parse_End
1779 and then Expression (Parse_Pos) = ':'
1781 Parse_Pos := Parse_Pos + 1;
1783 -- Do we have something like: [[:^alpha:]]
1785 if Parse_Pos <= Parse_End
1786 and then Expression (Parse_Pos) = '^'
1789 Parse_Pos := Parse_Pos + 1;
1792 -- All classes have 6 characters at least
1793 -- ??? magid constant 6 should have a name!
1795 if Parse_Pos + 6 <= Parse_End then
1797 case Expression (Parse_Pos) is
1799 if E (Parse_Pos .. Parse_Pos + 4) = "alnum:]" then
1801 Class := ANYOF_NALNUMC;
1803 Class := ANYOF_ALNUMC;
1806 elsif E (Parse_Pos .. Parse_Pos + 6) = "alpha:]" then
1808 Class := ANYOF_NALPHA;
1810 Class := ANYOF_ALPHA;
1813 elsif E (Parse_Pos .. Parse_Pos + 6) = "ascii:]" then
1815 Class := ANYOF_NASCII;
1817 Class := ANYOF_ASCII;
1823 if E (Parse_Pos .. Parse_Pos + 6) = "cntrl:]" then
1825 Class := ANYOF_NCNTRL;
1827 Class := ANYOF_CNTRL;
1833 if E (Parse_Pos .. Parse_Pos + 6) = "digit:]" then
1835 Class := ANYOF_NDIGIT;
1837 Class := ANYOF_DIGIT;
1843 if E (Parse_Pos .. Parse_Pos + 6) = "graph:]" then
1845 Class := ANYOF_NGRAPH;
1847 Class := ANYOF_GRAPH;
1853 if E (Parse_Pos .. Parse_Pos + 6) = "lower:]" then
1855 Class := ANYOF_NLOWER;
1857 Class := ANYOF_LOWER;
1863 if E (Parse_Pos .. Parse_Pos + 6) = "print:]" then
1865 Class := ANYOF_NPRINT;
1867 Class := ANYOF_PRINT;
1870 elsif E (Parse_Pos .. Parse_Pos + 6) = "punct:]" then
1872 Class := ANYOF_NPUNCT;
1874 Class := ANYOF_PUNCT;
1880 if E (Parse_Pos .. Parse_Pos + 6) = "space:]" then
1882 Class := ANYOF_NSPACE;
1884 Class := ANYOF_SPACE;
1890 if E (Parse_Pos .. Parse_Pos + 6) = "upper:]" then
1892 Class := ANYOF_NUPPER;
1894 Class := ANYOF_UPPER;
1900 if E (Parse_Pos .. Parse_Pos + 5) = "word:]" then
1902 Class := ANYOF_NALNUM;
1904 Class := ANYOF_ALNUM;
1907 Parse_Pos := Parse_Pos - 1;
1912 if Parse_Pos + 7 <= Parse_End
1913 and then E (Parse_Pos .. Parse_Pos + 7) = "xdigit:]"
1916 Class := ANYOF_NXDIGIT;
1918 Class := ANYOF_XDIGIT;
1921 Parse_Pos := Parse_Pos + 1;
1925 Class := ANYOF_NONE;
1929 if Class /= ANYOF_NONE then
1930 Parse_Pos := Parse_Pos + 7;
1934 Fail ("Invalid character class");
1942 end Parse_Posix_Character_Class;
1944 Expr_Flags : Expression_Flags;
1947 -- Start of processing for Compile
1951 Parse (False, Expr_Flags, Result);
1954 Fail ("Couldn't compile expression");
1957 Final_Code_Size := Emit_Ptr - 1;
1959 -- Do we want to actually compile the expression, or simply get the
1970 (Expression : String;
1971 Flags : Regexp_Flags := No_Flags)
1972 return Pattern_Matcher
1974 Size : Program_Size;
1975 Dummy : Pattern_Matcher (0);
1978 Compile (Dummy, Expression, Size, Flags);
1981 Result : Pattern_Matcher (Size);
1983 Compile (Result, Expression, Size, Flags);
1989 (Matcher : out Pattern_Matcher;
1990 Expression : String;
1991 Flags : Regexp_Flags := No_Flags)
1993 Size : Program_Size;
1996 Compile (Matcher, Expression, Size, Flags);
2003 procedure Dump (Self : Pattern_Matcher) is
2005 -- Index : Pointer := Program_First + 1;
2006 -- What is the above line for ???
2009 Program : Program_Data renames Self.Program;
2011 procedure Dump_Until
2014 Indent : Natural := 0);
2015 -- Dump the program until the node Till (not included) is met.
2016 -- Every line is indented with Index spaces at the beginning
2017 -- Dumps till the end if Till is 0.
2023 procedure Dump_Until
2026 Indent : Natural := 0)
2029 Index : Pointer := Start;
2030 Local_Indent : Natural := Indent;
2034 while Index < Till loop
2036 Op := Opcode'Val (Character'Pos ((Self.Program (Index))));
2039 Local_Indent := Local_Indent - 3;
2043 Point : String := Pointer'Image (Index);
2046 for J in 1 .. 6 - Point'Length loop
2052 & (1 .. Local_Indent => ' ')
2053 & Opcode'Image (Op));
2056 -- Print the parenthesis number
2058 if Op = OPEN or else Op = CLOSE or else Op = REFF then
2059 Put (Natural'Image (Character'Pos (Program (Index + 3))));
2062 Next := Index + Get_Next_Offset (Program, Index);
2064 if Next = Index then
2065 Put (" (next at 0)");
2067 Put (" (next at " & Pointer'Image (Next) & ")");
2072 -- Character class operand
2076 Bitmap : Character_Class;
2077 Last : Character := ASCII.Nul;
2078 Current : Natural := 0;
2080 Current_Char : Character;
2083 Bitmap_Operand (Program, Index, Bitmap);
2086 while Current <= 255 loop
2087 Current_Char := Character'Val (Current);
2089 -- First item in a range
2091 if Get_From_Class (Bitmap, Current_Char) then
2092 Last := Current_Char;
2094 -- Search for the last item in the range
2097 Current := Current + 1;
2098 exit when Current > 255;
2099 Current_Char := Character'Val (Current);
2101 not Get_From_Class (Bitmap, Current_Char);
2111 if Character'Succ (Last) /= Current_Char then
2112 Put ("-" & Character'Pred (Current_Char));
2116 Current := Current + 1;
2121 Index := Index + 3 + Bitmap'Length;
2126 when EXACT | EXACTF =>
2127 Length := String_Length (Program, Index);
2128 Put (" operand (length:" & Program_Size'Image (Length + 1)
2130 & String (Program (String_Operand (Index)
2131 .. String_Operand (Index)
2133 Index := String_Operand (Index) + Length + 1;
2140 Dump_Until (Index + 3, Next, Local_Indent + 3);
2146 -- Only one instruction
2148 Dump_Until (Index + 3, Index + 4, Local_Indent + 3);
2151 when CURLY | CURLYX =>
2153 & Natural'Image (Read_Natural (Program, Index + 3))
2155 & Natural'Image (Read_Natural (Program, Index + 5))
2158 Dump_Until (Index + 7, Next, Local_Indent + 3);
2164 Local_Indent := Local_Indent + 3;
2166 when CLOSE | REFF =>
2184 -- Start of processing for Dump
2187 pragma Assert (Self.Program (Program_First) = MAGIC,
2188 "Corrupted Pattern_Matcher");
2190 Put_Line ("Must start with (Self.First) = "
2191 & Character'Image (Self.First));
2193 if (Self.Flags and Case_Insensitive) /= 0 then
2194 Put_Line (" Case_Insensitive mode");
2197 if (Self.Flags and Single_Line) /= 0 then
2198 Put_Line (" Single_Line mode");
2201 if (Self.Flags and Multiple_Lines) /= 0 then
2202 Put_Line (" Multiple_Lines mode");
2205 Put_Line (" 1 : MAGIC");
2206 Dump_Until (Program_First + 1, Self.Program'Last + 1);
2209 --------------------
2210 -- Get_From_Class --
2211 --------------------
2213 function Get_From_Class
2214 (Bitmap : Character_Class;
2218 Value : constant Class_Byte := Character'Pos (C);
2221 return (Bitmap (Value / 8)
2222 and Bit_Conversion (Value mod 8)) /= 0;
2229 function Get_Next (Program : Program_Data; IP : Pointer) return Pointer is
2230 Offset : constant Pointer := Get_Next_Offset (Program, IP);
2240 ---------------------
2241 -- Get_Next_Offset --
2242 ---------------------
2244 function Get_Next_Offset
2245 (Program : Program_Data;
2250 return Pointer (Read_Natural (Program, IP + 1));
2251 end Get_Next_Offset;
2257 function Is_Alnum (C : Character) return Boolean is
2259 return Is_Alphanumeric (C) or else C = '_';
2266 function Is_Printable (C : Character) return Boolean is
2267 Value : constant Natural := Character'Pos (C);
2270 return (Value > 32 and then Value < 127)
2271 or else Is_Space (C);
2278 function Is_Space (C : Character) return Boolean is
2281 or else C = ASCII.HT
2282 or else C = ASCII.CR
2283 or else C = ASCII.LF
2284 or else C = ASCII.VT
2285 or else C = ASCII.FF;
2293 (Self : Pattern_Matcher;
2295 Matches : out Match_Array)
2297 Program : Program_Data renames Self.Program; -- Shorter notation
2299 -- Global work variables
2301 Input_Pos : Natural; -- String-input pointer
2302 BOL_Pos : Natural; -- Beginning of input, for ^ check
2303 Matched : Boolean := False; -- Until proven True
2305 Matches_Full : Match_Array (0 .. Natural'Max (Self.Paren_Count,
2307 -- Stores the value of all the parenthesis pairs.
2308 -- We do not use directly Matches, so that we can also use back
2309 -- references (REFF) even if Matches is too small.
2311 type Natural_Array is array (Match_Count range <>) of Natural;
2312 Matches_Tmp : Natural_Array (Matches_Full'Range);
2313 -- Save the opening position of parenthesis.
2315 Last_Paren : Natural := 0;
2316 -- Last parenthesis seen
2318 Greedy : Boolean := True;
2319 -- True if the next operator should be greedy
2321 type Current_Curly_Record;
2322 type Current_Curly_Access is access all Current_Curly_Record;
2323 type Current_Curly_Record is record
2324 Paren_Floor : Natural; -- How far back to strip parenthesis data
2325 Cur : Integer; -- How many instances of scan we've matched
2326 Min : Natural; -- Minimal number of scans to match
2327 Max : Natural; -- Maximal number of scans to match
2328 Greedy : Boolean; -- Whether to work our way up or down
2329 Scan : Pointer; -- The thing to match
2330 Next : Pointer; -- What has to match after it
2331 Lastloc : Natural; -- Where we started matching this scan
2332 Old_Cc : Current_Curly_Access; -- Before we started this one
2334 -- Data used to handle the curly operator and the plus and star
2335 -- operators for complex expressions.
2337 Current_Curly : Current_Curly_Access := null;
2338 -- The curly currently being processed.
2340 -----------------------
2341 -- Local Subprograms --
2342 -----------------------
2344 function Index (Start : Positive; C : Character) return Natural;
2345 -- Find character C in Data starting at Start and return position
2349 Max : Natural := Natural'Last)
2351 -- Repeatedly match something simple, report how many
2352 -- It only matches on things of length 1.
2353 -- Starting from Input_Pos, it matches at most Max CURLY.
2355 function Try (Pos : in Positive) return Boolean;
2356 -- Try to match at specific point
2358 function Match (IP : Pointer) return Boolean;
2359 -- This is the main matching routine. Conceptually the strategy
2360 -- is simple: check to see whether the current node matches,
2361 -- call self recursively to see whether the rest matches,
2362 -- and then act accordingly.
2364 -- In practice Match makes some effort to avoid recursion, in
2365 -- particular by going through "ordinary" nodes (that don't
2366 -- need to know whether the rest of the match failed) by
2367 -- using a loop instead of recursion.
2369 function Match_Whilem (IP : Pointer) return Boolean;
2370 -- Return True if a WHILEM matches
2372 function Recurse_Match (IP : Pointer; From : Natural) return Boolean;
2373 pragma Inline (Recurse_Match);
2374 -- Calls Match recursively. It saves and restores the parenthesis
2375 -- status and location in the input stream correctly, so that
2376 -- backtracking is possible
2378 function Match_Simple_Operator
2384 -- Return True it the simple operator (possibly non-greedy) matches
2386 pragma Inline (Index);
2387 pragma Inline (Repeat);
2389 -- These are two complex functions, but used only once.
2391 pragma Inline (Match_Whilem);
2392 pragma Inline (Match_Simple_Operator);
2404 for J in Start .. Data'Last loop
2405 if Data (J) = C then
2417 function Recurse_Match (IP : Pointer; From : Natural) return Boolean is
2418 L : constant Natural := Last_Paren;
2419 Tmp_F : constant Match_Array :=
2420 Matches_Full (From + 1 .. Matches_Full'Last);
2421 Start : constant Natural_Array :=
2422 Matches_Tmp (From + 1 .. Matches_Tmp'Last);
2423 Input : constant Natural := Input_Pos;
2429 Matches_Full (Tmp_F'Range) := Tmp_F;
2430 Matches_Tmp (Start'Range) := Start;
2439 function Match (IP : Pointer) return Boolean is
2440 Scan : Pointer := IP;
2447 pragma Assert (Scan /= 0);
2449 -- Determine current opcode and count its usage in debug mode
2451 Op := Opcode'Val (Character'Pos (Program (Scan)));
2453 -- Calculate offset of next instruction.
2454 -- Second character is most significant in Program_Data.
2456 Next := Get_Next (Program, Scan);
2460 return True; -- Success !
2463 if Program (Next) /= BRANCH then
2464 Next := Operand (Scan); -- No choice, avoid recursion
2468 if Recurse_Match (Operand (Scan), 0) then
2472 Scan := Get_Next (Program, Scan);
2473 exit when Scan = 0 or Program (Scan) /= BRANCH;
2483 exit State_Machine when
2484 Input_Pos /= BOL_Pos
2485 and then ((Self.Flags and Multiple_Lines) = 0
2486 or else Data (Input_Pos - 1) /= ASCII.LF);
2489 exit State_Machine when
2490 Input_Pos /= BOL_Pos
2491 and then Data (Input_Pos - 1) /= ASCII.LF;
2494 exit State_Machine when Input_Pos /= BOL_Pos;
2497 exit State_Machine when
2498 Input_Pos <= Data'Last
2499 and then ((Self.Flags and Multiple_Lines) = 0
2500 or else Data (Input_Pos) /= ASCII.LF);
2503 exit State_Machine when
2504 Input_Pos <= Data'Last
2505 and then Data (Input_Pos) /= ASCII.LF;
2508 exit State_Machine when Input_Pos <= Data'Last;
2510 when BOUND | NBOUND =>
2512 -- Was last char in word ?
2515 N : Boolean := False;
2516 Ln : Boolean := False;
2519 if Input_Pos /= Data'First then
2520 N := Is_Alnum (Data (Input_Pos - 1));
2523 if Input_Pos > Data'Last then
2526 Ln := Is_Alnum (Data (Input_Pos));
2541 exit State_Machine when
2542 Input_Pos > Data'Last
2543 or else not Is_Space (Data (Input_Pos));
2544 Input_Pos := Input_Pos + 1;
2547 exit State_Machine when
2548 Input_Pos > Data'Last
2549 or else Is_Space (Data (Input_Pos));
2550 Input_Pos := Input_Pos + 1;
2553 exit State_Machine when
2554 Input_Pos > Data'Last
2555 or else not Is_Digit (Data (Input_Pos));
2556 Input_Pos := Input_Pos + 1;
2559 exit State_Machine when
2560 Input_Pos > Data'Last
2561 or else Is_Digit (Data (Input_Pos));
2562 Input_Pos := Input_Pos + 1;
2565 exit State_Machine when
2566 Input_Pos > Data'Last
2567 or else not Is_Alnum (Data (Input_Pos));
2568 Input_Pos := Input_Pos + 1;
2571 exit State_Machine when
2572 Input_Pos > Data'Last
2573 or else Is_Alnum (Data (Input_Pos));
2574 Input_Pos := Input_Pos + 1;
2577 exit State_Machine when Input_Pos > Data'Last
2578 or else Data (Input_Pos) = ASCII.LF;
2579 Input_Pos := Input_Pos + 1;
2582 exit State_Machine when Input_Pos > Data'Last;
2583 Input_Pos := Input_Pos + 1;
2587 Opnd : Pointer := String_Operand (Scan);
2588 Current : Positive := Input_Pos;
2589 Last : constant Pointer :=
2590 Opnd + String_Length (Program, Scan);
2593 while Opnd <= Last loop
2594 exit State_Machine when Current > Data'Last
2595 or else Program (Opnd) /= Data (Current);
2596 Current := Current + 1;
2600 Input_Pos := Current;
2605 Opnd : Pointer := String_Operand (Scan);
2606 Current : Positive := Input_Pos;
2607 Last : constant Pointer :=
2608 Opnd + String_Length (Program, Scan);
2611 while Opnd <= Last loop
2612 exit State_Machine when Current > Data'Last
2613 or else Program (Opnd) /= To_Lower (Data (Current));
2614 Current := Current + 1;
2618 Input_Pos := Current;
2623 Bitmap : Character_Class;
2626 Bitmap_Operand (Program, Scan, Bitmap);
2627 exit State_Machine when
2628 Input_Pos > Data'Last
2629 or else not Get_From_Class (Bitmap, Data (Input_Pos));
2630 Input_Pos := Input_Pos + 1;
2635 No : constant Natural :=
2636 Character'Pos (Program (Operand (Scan)));
2638 Matches_Tmp (No) := Input_Pos;
2643 No : constant Natural :=
2644 Character'Pos (Program (Operand (Scan)));
2646 Matches_Full (No) := (Matches_Tmp (No), Input_Pos - 1);
2647 if Last_Paren < No then
2654 No : constant Natural :=
2655 Character'Pos (Program (Operand (Scan)));
2659 -- If we haven't seen that parenthesis yet
2661 if Last_Paren < No then
2665 Data_Pos := Matches_Full (No).First;
2666 while Data_Pos <= Matches_Full (No).Last loop
2667 if Input_Pos > Data'Last
2668 or else Data (Input_Pos) /= Data (Data_Pos)
2673 Input_Pos := Input_Pos + 1;
2674 Data_Pos := Data_Pos + 1;
2681 when STAR | PLUS | CURLY =>
2683 Greed : constant Boolean := Greedy;
2686 return Match_Simple_Operator (Op, Scan, Next, Greed);
2691 -- Looking at something like:
2692 -- 1: CURLYX {n,m} (->4)
2693 -- 2: code for complex thing (->3)
2698 Cc : aliased Current_Curly_Record;
2699 Min : Natural := Read_Natural (Program, Scan + 3);
2700 Max : Natural := Read_Natural (Program, Scan + 5);
2702 Has_Match : Boolean;
2705 Cc := (Paren_Floor => Last_Paren,
2713 Old_Cc => Current_Curly);
2714 Current_Curly := Cc'Unchecked_Access;
2716 Has_Match := Match (Next - 3);
2718 -- Start on the WHILEM
2720 Current_Curly := Cc.Old_Cc;
2725 return Match_Whilem (IP);
2728 raise Expression_Error; -- Invalid instruction
2732 end loop State_Machine;
2734 -- If we get here, there is no match.
2735 -- For successful matches when EOP is the terminating point.
2740 ---------------------------
2741 -- Match_Simple_Operator --
2742 ---------------------------
2744 function Match_Simple_Operator
2751 Next_Char : Character := ASCII.Nul;
2752 Next_Char_Known : Boolean := False;
2753 No : Integer; -- Can be negative
2755 Max : Natural := Natural'Last;
2756 Operand_Code : Pointer;
2759 Save : Natural := Input_Pos;
2762 -- Lookahead to avoid useless match attempts
2763 -- when we know what character comes next.
2765 if Program (Next) = EXACT then
2766 Next_Char := Program (String_Operand (Next));
2767 Next_Char_Known := True;
2770 -- Find the minimal and maximal values for the operator
2775 Operand_Code := Operand (Scan);
2779 Operand_Code := Operand (Scan);
2782 Min := Read_Natural (Program, Scan + 3);
2783 Max := Read_Natural (Program, Scan + 5);
2784 Operand_Code := Scan + 7;
2787 -- Non greedy operators
2790 -- Test the minimal repetitions
2793 and then Repeat (Operand_Code, Min) < Min
2800 -- Find the place where 'next' could work
2802 if Next_Char_Known then
2803 -- Last position to check
2805 Last_Pos := Input_Pos + Max;
2807 if Last_Pos > Data'Last
2808 or else Max = Natural'Last
2810 Last_Pos := Data'Last;
2813 -- Look for the first possible opportunity
2816 -- Find the next possible position
2818 while Input_Pos <= Last_Pos
2819 and then Data (Input_Pos) /= Next_Char
2821 Input_Pos := Input_Pos + 1;
2824 if Input_Pos > Last_Pos then
2828 -- Check that we still match if we stop
2829 -- at the position we just found.
2832 Num : constant Natural := Input_Pos - Old;
2837 if Repeat (Operand_Code, Num) < Num then
2842 -- Input_Pos now points to the new position
2844 if Match (Get_Next (Program, Scan)) then
2849 Input_Pos := Input_Pos + 1;
2852 -- We know what the next character is
2855 while Max >= Min loop
2857 -- If the next character matches
2859 if Match (Next) then
2863 Input_Pos := Save + Min;
2865 -- Could not or did not match -- move forward
2867 if Repeat (Operand_Code, 1) /= 0 then
2880 No := Repeat (Operand_Code, Max);
2882 -- ??? Perl has some special code here in case the
2883 -- next instruction is of type EOL, since $ and \Z
2884 -- can match before *and* after newline at the end.
2886 -- ??? Perl has some special code here in case (paren)
2889 -- Else, if we don't have any parenthesis
2891 while No >= Min loop
2892 if not Next_Char_Known
2893 or else (Input_Pos <= Data'Last
2894 and then Data (Input_Pos) = Next_Char)
2896 if Match (Next) then
2901 -- Could not or did not work, we back up
2904 Input_Pos := Save + No;
2908 end Match_Simple_Operator;
2914 -- This is really hard to understand, because after we match what we're
2915 -- trying to match, we must make sure the rest of the REx is going to
2916 -- match for sure, and to do that we have to go back UP the parse tree
2917 -- by recursing ever deeper. And if it fails, we have to reset our
2918 -- parent's current state that we can try again after backing off.
2920 function Match_Whilem (IP : Pointer) return Boolean is
2921 pragma Warnings (Off, IP);
2923 Cc : Current_Curly_Access := Current_Curly;
2924 N : Natural := Cc.Cur + 1;
2927 Lastloc : Natural := Cc.Lastloc;
2928 -- Detection of 0-len.
2931 -- If degenerate scan matches "", assume scan done.
2933 if Input_Pos = Cc.Lastloc
2934 and then N >= Cc.Min
2936 -- Temporarily restore the old context, and check that we
2937 -- match was comes after CURLYX.
2939 Current_Curly := Cc.Old_Cc;
2941 if Current_Curly /= null then
2942 Ln := Current_Curly.Cur;
2945 if Match (Cc.Next) then
2949 if Current_Curly /= null then
2950 Current_Curly.Cur := Ln;
2953 Current_Curly := Cc;
2957 -- First, just match a string of min scans.
2961 Cc.Lastloc := Input_Pos;
2963 if Match (Cc.Scan) then
2968 Cc.Lastloc := Lastloc;
2972 -- Prefer next over scan for minimal matching.
2974 if not Cc.Greedy then
2975 Current_Curly := Cc.Old_Cc;
2977 if Current_Curly /= null then
2978 Ln := Current_Curly.Cur;
2981 if Recurse_Match (Cc.Next, Cc.Paren_Floor) then
2985 if Current_Curly /= null then
2986 Current_Curly.Cur := Ln;
2989 Current_Curly := Cc;
2991 -- Maximum greed exceeded ?
2997 -- Try scanning more and see if it helps
2999 Cc.Lastloc := Input_Pos;
3001 if Recurse_Match (Cc.Scan, Cc.Paren_Floor) then
3006 Cc.Lastloc := Lastloc;
3010 -- Prefer scan over next for maximal matching
3012 if N < Cc.Max then -- more greed allowed ?
3014 Cc.Lastloc := Input_Pos;
3016 if Recurse_Match (Cc.Scan, Cc.Paren_Floor) then
3021 -- Failed deeper matches of scan, so see if this one works
3023 Current_Curly := Cc.Old_Cc;
3025 if Current_Curly /= null then
3026 Ln := Current_Curly.Cur;
3029 if Match (Cc.Next) then
3033 if Current_Curly /= null then
3034 Current_Curly.Cur := Ln;
3037 Current_Curly := Cc;
3039 Cc.Lastloc := Lastloc;
3049 Max : Natural := Natural'Last)
3052 Scan : Natural := Input_Pos;
3054 Op : constant Opcode := Opcode'Val (Character'Pos (Program (IP)));
3057 Is_First : Boolean := True;
3058 Bitmap : Character_Class;
3061 if Max = Natural'Last or else Scan + Max - 1 > Data'Last then
3064 Last := Scan + Max - 1;
3070 and then Data (Scan) /= ASCII.LF
3080 -- The string has only one character if Repeat was called
3082 C := Program (String_Operand (IP));
3084 and then C = Data (Scan)
3091 -- The string has only one character if Repeat was called
3093 C := Program (String_Operand (IP));
3095 and then To_Lower (C) = Data (Scan)
3102 Bitmap_Operand (Program, IP, Bitmap);
3107 and then Get_From_Class (Bitmap, Data (Scan))
3114 and then Is_Alnum (Data (Scan))
3121 and then not Is_Alnum (Data (Scan))
3128 and then Is_Space (Data (Scan))
3135 and then not Is_Space (Data (Scan))
3142 and then Is_Digit (Data (Scan))
3149 and then not Is_Digit (Data (Scan))
3155 raise Program_Error;
3158 Count := Scan - Input_Pos;
3167 function Try (Pos : in Positive) return Boolean is
3171 Matches_Full := (others => No_Match);
3173 if Match (Program_First + 1) then
3174 Matches_Full (0) := (Pos, Input_Pos - 1);
3181 -- Start of processing for Match
3184 -- Do we have the regexp Never_Match?
3186 if Self.Size = 0 then
3187 Matches (0) := No_Match;
3191 -- Check validity of program
3194 (Program (Program_First) = MAGIC,
3195 "Corrupted Pattern_Matcher");
3197 -- If there is a "must appear" string, look for it
3199 if Self.Must_Have_Length > 0 then
3201 First : constant Character := Program (Self.Must_Have);
3202 Must_First : constant Pointer := Self.Must_Have;
3203 Must_Last : constant Pointer :=
3204 Must_First + Pointer (Self.Must_Have_Length - 1);
3205 Next_Try : Natural := Index (Data'First, First);
3209 and then Data (Next_Try .. Next_Try + Self.Must_Have_Length - 1)
3210 = String (Program (Must_First .. Must_Last))
3212 Next_Try := Index (Next_Try + 1, First);
3215 if Next_Try = 0 then
3216 Matches_Full := (others => No_Match);
3217 return; -- Not present
3222 -- Mark beginning of line for ^
3224 BOL_Pos := Data'First;
3226 -- Simplest case first: an anchored match need be tried only once
3228 if Self.Anchored and then (Self.Flags and Multiple_Lines) = 0 then
3229 Matched := Try (Data'First);
3231 elsif Self.Anchored then
3233 Next_Try : Natural := Data'First;
3235 -- Test the first position in the buffer
3236 Matched := Try (Next_Try);
3238 -- Else only test after newlines
3241 while Next_Try <= Data'Last loop
3242 while Next_Try <= Data'Last
3243 and then Data (Next_Try) /= ASCII.LF
3245 Next_Try := Next_Try + 1;
3248 Next_Try := Next_Try + 1;
3250 if Next_Try <= Data'Last then
3251 Matched := Try (Next_Try);
3258 elsif Self.First /= ASCII.NUL then
3260 -- We know what char it must start with
3263 Next_Try : Natural := Index (Data'First, Self.First);
3266 while Next_Try /= 0 loop
3267 Matched := Try (Next_Try);
3269 Next_Try := Index (Next_Try + 1, Self.First);
3274 -- Messy cases: try all locations (including for the empty string)
3276 Matched := Try (Data'First);
3279 for S in Data'First + 1 .. Data'Last loop
3286 -- Matched has its value
3288 for J in Last_Paren + 1 .. Matches'Last loop
3289 Matches_Full (J) := No_Match;
3292 Matches := Matches_Full (Matches'Range);
3297 (Self : Pattern_Matcher;
3301 Matches : Match_Array (0 .. 0);
3304 Match (Self, Data, Matches);
3305 if Matches (0) = No_Match then
3306 return Data'First - 1;
3308 return Matches (0).First;
3313 (Expression : String;
3315 Matches : out Match_Array;
3316 Size : Program_Size := 0)
3318 PM : Pattern_Matcher (Size);
3319 Finalize_Size : Program_Size;
3323 Match (Compile (Expression), Data, Matches);
3325 Compile (PM, Expression, Finalize_Size);
3326 Match (PM, Data, Matches);
3331 (Expression : String;
3333 Size : Program_Size := 0)
3336 PM : Pattern_Matcher (Size);
3337 Final_Size : Program_Size; -- unused
3341 return Match (Compile (Expression), Data);
3343 Compile (PM, Expression, Final_Size);
3344 return Match (PM, Data);
3349 (Expression : String;
3351 Size : Program_Size := 0)
3354 Matches : Match_Array (0 .. 0);
3355 PM : Pattern_Matcher (Size);
3356 Final_Size : Program_Size; -- unused
3360 Match (Compile (Expression), Data, Matches);
3362 Compile (PM, Expression, Final_Size);
3363 Match (PM, Data, Matches);
3366 return Matches (0).First >= Data'First;
3373 function Operand (P : Pointer) return Pointer is
3382 procedure Optimize (Self : in out Pattern_Matcher) is
3383 Max_Length : Program_Size;
3384 This_Length : Program_Size;
3387 Program : Program_Data renames Self.Program;
3390 -- Start with safe defaults (no optimization):
3391 -- * No known first character of match
3392 -- * Does not necessarily start at beginning of line
3393 -- * No string known that has to appear in data
3395 Self.First := ASCII.NUL;
3396 Self.Anchored := False;
3397 Self.Must_Have := Program'Last + 1;
3398 Self.Must_Have_Length := 0;
3400 Scan := Program_First + 1; -- First instruction (can be anything)
3402 if Program (Scan) = EXACT then
3403 Self.First := Program (String_Operand (Scan));
3405 elsif Program (Scan) = BOL
3406 or else Program (Scan) = SBOL
3407 or else Program (Scan) = MBOL
3409 Self.Anchored := True;
3412 -- If there's something expensive in the regexp, find the
3413 -- longest literal string that must appear and make it the
3414 -- regmust. Resolve ties in favor of later strings, since
3415 -- the regstart check works with the beginning of the regexp.
3416 -- and avoiding duplication strengthens checking. Not a
3417 -- strong reason, but sufficient in the absence of others.
3419 if False then -- if Flags.SP_Start then ???
3422 while Scan /= 0 loop
3423 if Program (Scan) = EXACT or else Program (Scan) = EXACTF then
3424 This_Length := String_Length (Program, Scan);
3426 if This_Length >= Max_Length then
3427 Longest := String_Operand (Scan);
3428 Max_Length := This_Length;
3432 Scan := Get_Next (Program, Scan);
3435 Self.Must_Have := Longest;
3436 Self.Must_Have_Length := Natural (Max_Length) + 1;
3444 function Paren_Count (Regexp : Pattern_Matcher) return Match_Count is
3446 return Regexp.Paren_Count;
3453 function Quote (Str : String) return String is
3454 S : String (1 .. Str'Length * 2);
3455 Last : Natural := 0;
3458 for J in Str'Range loop
3460 when '^' | '$' | '|' | '*' | '+' | '?' | '{'
3461 | '}' | '[' | ']' | '(' | ')' | '\' =>
3463 S (Last + 1) := '\';
3464 S (Last + 2) := Str (J);
3468 S (Last + 1) := Str (J);
3473 return S (1 .. Last);
3480 function Read_Natural
3481 (Program : Program_Data;
3486 return Character'Pos (Program (IP)) +
3487 256 * Character'Pos (Program (IP + 1));
3494 procedure Reset_Class (Bitmap : in out Character_Class) is
3496 Bitmap := (others => 0);
3503 procedure Set_In_Class
3504 (Bitmap : in out Character_Class;
3507 Value : constant Class_Byte := Character'Pos (C);
3510 Bitmap (Value / 8) := Bitmap (Value / 8)
3511 or Bit_Conversion (Value mod 8);
3518 function String_Length
3519 (Program : Program_Data;
3524 pragma Assert (Program (P) = EXACT or else Program (P) = EXACTF);
3525 return Character'Pos (Program (P + 3));
3528 --------------------
3529 -- String_Operand --
3530 --------------------
3532 function String_Operand (P : Pointer) return Pointer is