1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- G N A T . R E G P A T --
11 -- Copyright (C) 1986 by University of Toronto. --
12 -- Copyright (C) 1996-2001 Ada Core Technologies, Inc. --
14 -- GNAT is free software; you can redistribute it and/or modify it under --
15 -- terms of the GNU General Public License as published by the Free Soft- --
16 -- ware Foundation; either version 2, or (at your option) any later ver- --
17 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
18 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
19 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
20 -- for more details. You should have received a copy of the GNU General --
21 -- Public License distributed with GNAT; see file COPYING. If not, write --
22 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
23 -- MA 02111-1307, USA. --
25 -- As a special exception, if other files instantiate generics from this --
26 -- unit, or you link this unit with other files to produce an executable, --
27 -- this unit does not by itself cause the resulting executable to be --
28 -- covered by the GNU General Public License. This exception does not --
29 -- however invalidate any other reasons why the executable file might be --
30 -- covered by the GNU Public License. --
32 -- GNAT is maintained by Ada Core Technologies Inc (http://www.gnat.com). --
34 ------------------------------------------------------------------------------
36 -- This is an altered Ada 95 version of the original V8 style regular
37 -- expression library written in C by Henry Spencer. Apart from the
38 -- translation to Ada, the interface has been considerably changed to
39 -- use the Ada String type instead of C-style nul-terminated strings.
41 -- Beware that some of this code is subtly aware of the way operator
42 -- precedence is structured in regular expressions. Serious changes in
43 -- regular-expression syntax might require a total rethink.
45 with System
.IO
; use System
.IO
;
46 with Ada
.Characters
.Handling
; use Ada
.Characters
.Handling
;
47 with Unchecked_Conversion
;
49 package body GNAT
.Regpat
is
51 MAGIC
: constant Character := Character'Val (10#
0234#
);
52 -- The first byte of the regexp internal "program" is actually
53 -- this magic number; the start node begins in the second byte.
55 -- This is used to make sure that a regular expression was correctly
58 ----------------------------
59 -- Implementation details --
60 ----------------------------
62 -- This is essentially a linear encoding of a nondeterministic
63 -- finite-state machine, also known as syntax charts or
64 -- "railroad normal form" in parsing technology.
66 -- Each node is an opcode plus a "next" pointer, possibly plus an
67 -- operand. "Next" pointers of all nodes except BRANCH implement
68 -- concatenation; a "next" pointer with a BRANCH on both ends of it
69 -- is connecting two alternatives.
71 -- The operand of some types of node is a literal string; for others,
72 -- it is a node leading into a sub-FSM. In particular, the operand of
73 -- a BRANCH node is the first node of the branch.
74 -- (NB this is *not* a tree structure: the tail of the branch connects
75 -- to the thing following the set of BRANCHes).
77 -- You can see the exact byte-compiled version by using the Dump
78 -- subprogram. However, here are a few examples:
81 -- 2 : BRANCH (next at 10)
82 -- 5 : EXACT (next at 18) operand=a
83 -- 10 : BRANCH (next at 18)
84 -- 13 : EXACT (next at 18) operand=b
85 -- 18 : EOP (next at 0)
88 -- 2 : CURLYX (next at 26) { 0, 32767}
89 -- 9 : OPEN 1 (next at 13)
90 -- 13 : EXACT (next at 19) operand=ab
91 -- 19 : CLOSE 1 (next at 23)
92 -- 23 : WHILEM (next at 0)
93 -- 26 : NOTHING (next at 29)
94 -- 29 : EOP (next at 0)
100 -- Name Operand? Meaning
102 (EOP
, -- no End of program
103 MINMOD
, -- no Next operator is not greedy
105 -- Classes of characters
107 ANY
, -- no Match any one character except newline
108 SANY
, -- no Match any character, including new line
109 ANYOF
, -- class Match any character in this class
110 EXACT
, -- str Match this string exactly
111 EXACTF
, -- str Match this string (case-folding is one)
112 NOTHING
, -- no Match empty string
113 SPACE
, -- no Match any whitespace character
114 NSPACE
, -- no Match any non-whitespace character
115 DIGIT
, -- no Match any numeric character
116 NDIGIT
, -- no Match any non-numeric character
117 ALNUM
, -- no Match any alphanumeric character
118 NALNUM
, -- no Match any non-alphanumeric character
122 BRANCH
, -- node Match this alternative, or the next
124 -- Simple loops (when the following node is one character in length)
126 STAR
, -- node Match this simple thing 0 or more times
127 PLUS
, -- node Match this simple thing 1 or more times
128 CURLY
, -- 2num node Match this simple thing between n and m times.
132 CURLYX
, -- 2num node Match this complex thing {n,m} times
133 -- The nums are coded on two characters each.
135 WHILEM
, -- no Do curly processing and see if rest matches
137 -- Matches after or before a word
139 BOL
, -- no Match "" at beginning of line
140 MBOL
, -- no Same, assuming mutiline (match after \n)
141 SBOL
, -- no Same, assuming single line (don't match at \n)
142 EOL
, -- no Match "" at end of line
143 MEOL
, -- no Same, assuming mutiline (match before \n)
144 SEOL
, -- no Same, assuming single line (don't match at \n)
146 BOUND
, -- no Match "" at any word boundary
147 NBOUND
, -- no Match "" at any word non-boundary
149 -- Parenthesis groups handling
151 REFF
, -- num Match some already matched string, folded
152 OPEN
, -- num Mark this point in input as start of #n
153 CLOSE
); -- num Analogous to OPEN
155 for Opcode
'Size use 8;
160 -- The set of branches constituting a single choice are hooked
161 -- together with their "next" pointers, since precedence prevents
162 -- anything being concatenated to any individual branch. The
163 -- "next" pointer of the last BRANCH in a choice points to the
164 -- thing following the whole choice. This is also where the
165 -- final "next" pointer of each individual branch points; each
166 -- branch starts with the operand node of a BRANCH node.
169 -- '?', and complex '*' and '+', are implemented with CURLYX.
170 -- branches. Simple cases (one character per match) are implemented with
171 -- STAR and PLUS for speed and to minimize recursive plunges.
174 -- ...are numbered at compile time.
177 -- There are in fact two arguments, the first one is the length (minus
178 -- one of the string argument), coded on one character, the second
179 -- argument is the string itself, coded on length + 1 characters.
181 -- A node is one char of opcode followed by two chars of "next" pointer.
182 -- "Next" pointers are stored as two 8-bit pieces, high order first. The
183 -- value is a positive offset from the opcode of the node containing it.
184 -- An operand, if any, simply follows the node. (Note that much of the
185 -- code generation knows about this implicit relationship.)
187 -- Using two bytes for the "next" pointer is vast overkill for most
188 -- things, but allows patterns to get big without disasters.
190 -----------------------
191 -- Character classes --
192 -----------------------
193 -- This is the implementation for character classes ([...]) in the
194 -- syntax for regular expressions. Each character (0..256) has an
195 -- entry into the table. This makes for a very fast matching
198 type Class_Byte
is mod 256;
199 type Character_Class
is array (Class_Byte
range 0 .. 31) of Class_Byte
;
201 type Bit_Conversion_Array
is array (Class_Byte
range 0 .. 7) of Class_Byte
;
202 Bit_Conversion
: constant Bit_Conversion_Array
:=
203 (1, 2, 4, 8, 16, 32, 64, 128);
205 type Std_Class
is (ANYOF_NONE
,
206 ANYOF_ALNUM
, -- Alphanumeric class [a-zA-Z0-9]
208 ANYOF_SPACE
, -- Space class [ \t\n\r\f]
210 ANYOF_DIGIT
, -- Digit class [0-9]
212 ANYOF_ALNUMC
, -- Alphanumeric class [a-zA-Z0-9]
214 ANYOF_ALPHA
, -- Alpha class [a-zA-Z]
216 ANYOF_ASCII
, -- Ascii class (7 bits) 0..127
218 ANYOF_CNTRL
, -- Control class
220 ANYOF_GRAPH
, -- Graphic class
222 ANYOF_LOWER
, -- Lower case class [a-z]
224 ANYOF_PRINT
, -- printable class
228 ANYOF_UPPER
, -- Upper case class [A-Z]
230 ANYOF_XDIGIT
, -- Hexadecimal digit
234 procedure Set_In_Class
235 (Bitmap
: in out Character_Class
;
237 -- Set the entry to True for C in the class Bitmap.
239 function Get_From_Class
240 (Bitmap
: Character_Class
;
243 -- Return True if the entry is set for C in the class Bitmap.
245 procedure Reset_Class
(Bitmap
: in out Character_Class
);
246 -- Clear all the entries in the class Bitmap.
248 pragma Inline
(Set_In_Class
);
249 pragma Inline
(Get_From_Class
);
250 pragma Inline
(Reset_Class
);
252 -----------------------
253 -- Local Subprograms --
254 -----------------------
256 function "+" (Left
: Opcode
; Right
: Integer) return Opcode
;
257 function "-" (Left
: Opcode
; Right
: Opcode
) return Integer;
258 function "=" (Left
: Character; Right
: Opcode
) return Boolean;
260 function Is_Alnum
(C
: Character) return Boolean;
261 -- Return True if C is an alphanum character or an underscore ('_')
263 function Is_Space
(C
: Character) return Boolean;
264 -- Return True if C is a whitespace character
266 function Is_Printable
(C
: Character) return Boolean;
267 -- Return True if C is a printable character
269 function Operand
(P
: Pointer
) return Pointer
;
270 -- Return a pointer to the first operand of the node at P
272 function String_Length
273 (Program
: Program_Data
;
276 -- Return the length of the string argument of the node at P
278 function String_Operand
(P
: Pointer
) return Pointer
;
279 -- Return a pointer to the string argument of the node at P
281 procedure Bitmap_Operand
282 (Program
: Program_Data
;
284 Op
: out Character_Class
);
285 -- Return a pointer to the string argument of the node at P
287 function Get_Next_Offset
288 (Program
: Program_Data
;
291 -- Get the offset field of a node. Used by Get_Next.
294 (Program
: Program_Data
;
297 -- Dig the next instruction pointer out of a node
299 procedure Optimize
(Self
: in out Pattern_Matcher
);
300 -- Optimize a Pattern_Matcher by noting certain special cases
302 function Read_Natural
303 (Program
: Program_Data
;
306 -- Return the 2-byte natural coded at position IP.
308 -- All of the subprograms above are tiny and should be inlined
313 pragma Inline
(Is_Alnum
);
314 pragma Inline
(Is_Space
);
315 pragma Inline
(Get_Next
);
316 pragma Inline
(Get_Next_Offset
);
317 pragma Inline
(Operand
);
318 pragma Inline
(Read_Natural
);
319 pragma Inline
(String_Length
);
320 pragma Inline
(String_Operand
);
322 type Expression_Flags
is record
323 Has_Width
, -- Known never to match null string
324 Simple
, -- Simple enough to be STAR/PLUS operand
325 SP_Start
: Boolean; -- Starts with * or +
328 Worst_Expression
: constant Expression_Flags
:= (others => False);
335 function "+" (Left
: Opcode
; Right
: Integer) return Opcode
is
337 return Opcode
'Val (Opcode
'Pos (Left
) + Right
);
344 function "-" (Left
: Opcode
; Right
: Opcode
) return Integer is
346 return Opcode
'Pos (Left
) - Opcode
'Pos (Right
);
353 function "=" (Left
: Character; Right
: Opcode
) return Boolean is
355 return Character'Pos (Left
) = Opcode
'Pos (Right
);
362 procedure Bitmap_Operand
363 (Program
: Program_Data
;
365 Op
: out Character_Class
)
367 function Convert
is new Unchecked_Conversion
368 (Program_Data
, Character_Class
);
371 Op
(0 .. 31) := Convert
(Program
(P
+ 3 .. P
+ 34));
379 (Matcher
: out Pattern_Matcher
;
381 Final_Code_Size
: out Program_Size
;
382 Flags
: Regexp_Flags
:= No_Flags
)
384 -- We can't allocate space until we know how big the compiled form
385 -- will be, but we can't compile it (and thus know how big it is)
386 -- until we've got a place to put the code. So we cheat: we compile
387 -- it twice, once with code generation turned off and size counting
388 -- turned on, and once "for real".
390 -- This also means that we don't allocate space until we are sure
391 -- that the thing really will compile successfully, and we never
392 -- have to move the code and thus invalidate pointers into it.
394 -- Beware that the optimization-preparation code in here knows
395 -- about some of the structure of the compiled regexp.
397 PM
: Pattern_Matcher
renames Matcher
;
398 Program
: Program_Data
renames PM
.Program
;
400 Emit_Code
: constant Boolean := PM
.Size
> 0;
401 Emit_Ptr
: Pointer
:= Program_First
;
403 Parse_Pos
: Natural := Expression
'First; -- Input-scan pointer
404 Parse_End
: Natural := Expression
'Last;
406 ----------------------------
407 -- Subprograms for Create --
408 ----------------------------
410 procedure Emit
(B
: Character);
411 -- Output the Character to the Program.
412 -- If code-generation is disables, simply increments the program
415 function Emit_Node
(Op
: Opcode
) return Pointer
;
416 -- If code-generation is enabled, Emit_Node outputs the
417 -- opcode and reserves space for a pointer to the next node.
418 -- Return value is the location of new opcode, ie old Emit_Ptr.
420 procedure Emit_Natural
(IP
: Pointer
; N
: Natural);
421 -- Split N on two characters at position IP.
423 procedure Emit_Class
(Bitmap
: Character_Class
);
424 -- Emits a character class.
426 procedure Case_Emit
(C
: Character);
427 -- Emit C, after converting is to lower-case if the regular
428 -- expression is case insensitive.
431 (Parenthesized
: Boolean;
432 Flags
: in out Expression_Flags
;
434 -- Parse regular expression, i.e. main body or parenthesized thing
435 -- Caller must absorb opening parenthesis.
437 procedure Parse_Branch
438 (Flags
: in out Expression_Flags
;
441 -- Implements the concatenation operator and handles '|'
442 -- First should be true if this is the first item of the alternative.
444 procedure Parse_Piece
445 (Expr_Flags
: in out Expression_Flags
; IP
: out Pointer
);
446 -- Parse something followed by possible [*+?]
449 (Expr_Flags
: in out Expression_Flags
; IP
: out Pointer
);
450 -- Parse_Atom is the lowest level parse procedure.
451 -- Optimization: gobbles an entire sequence of ordinary characters
452 -- so that it can turn them into a single node, which is smaller to
453 -- store and faster to run. Backslashed characters are exceptions,
454 -- each becoming a separate node; the code is simpler that way and
455 -- it's not worth fixing.
457 procedure Insert_Operator
460 Greedy
: Boolean := True);
461 -- Insert_Operator inserts an operator in front of an
462 -- already-emitted operand and relocates the operand.
463 -- This applies to PLUS and STAR.
464 -- If Minmod is True, then the operator is non-greedy.
466 procedure Insert_Curly_Operator
471 Greedy
: Boolean := True);
472 -- Insert an operator for CURLY ({Min}, {Min,} or {Min,Max}).
473 -- If Minmod is True, then the operator is non-greedy.
475 procedure Link_Tail
(P
, Val
: Pointer
);
476 -- Link_Tail sets the next-pointer at the end of a node chain
478 procedure Link_Operand_Tail
(P
, Val
: Pointer
);
479 -- Link_Tail on operand of first argument; nop if operandless
481 function Next_Instruction
(P
: Pointer
) return Pointer
;
482 -- Dig the "next" pointer out of a node
484 procedure Fail
(M
: in String);
485 -- Fail with a diagnostic message, if possible
487 function Is_Curly_Operator
(IP
: Natural) return Boolean;
488 -- Return True if IP is looking at a '{' that is the beginning
489 -- of a curly operator, ie it matches {\d+,?\d*}
491 function Is_Mult
(IP
: Natural) return Boolean;
492 -- Return True if C is a regexp multiplier: '+', '*' or '?'
494 procedure Get_Curly_Arguments
498 Greedy
: out Boolean);
499 -- Parse the argument list for a curly operator.
500 -- It is assumed that IP is indeed pointing at a valid operator.
502 procedure Parse_Character_Class
(IP
: out Pointer
);
503 -- Parse a character class.
504 -- The calling subprogram should consume the opening '[' before.
506 procedure Parse_Literal
(Expr_Flags
: in out Expression_Flags
;
508 -- Parse_Literal encodes a string of characters
509 -- to be matched exactly.
511 function Parse_Posix_Character_Class
return Std_Class
;
512 -- Parse a posic character class, like [:alpha:] or [:^alpha:].
513 -- The called is suppoed to absorbe the opening [.
515 pragma Inline
(Is_Mult
);
516 pragma Inline
(Emit_Natural
);
517 pragma Inline
(Parse_Character_Class
); -- since used only once
523 procedure Case_Emit
(C
: Character) is
525 if (Flags
and Case_Insensitive
) /= 0 then
529 -- Dump current character
539 procedure Emit
(B
: Character) is
542 Program
(Emit_Ptr
) := B
;
545 Emit_Ptr
:= Emit_Ptr
+ 1;
552 procedure Emit_Class
(Bitmap
: Character_Class
) is
553 subtype Program31
is Program_Data
(0 .. 31);
555 function Convert
is new Unchecked_Conversion
556 (Character_Class
, Program31
);
560 Program
(Emit_Ptr
.. Emit_Ptr
+ 31) := Convert
(Bitmap
);
563 Emit_Ptr
:= Emit_Ptr
+ 32;
570 procedure Emit_Natural
(IP
: Pointer
; N
: Natural) is
573 Program
(IP
+ 1) := Character'Val (N
/ 256);
574 Program
(IP
) := Character'Val (N
mod 256);
582 function Emit_Node
(Op
: Opcode
) return Pointer
is
583 Result
: constant Pointer
:= Emit_Ptr
;
587 Program
(Emit_Ptr
) := Character'Val (Opcode
'Pos (Op
));
588 Program
(Emit_Ptr
+ 1) := ASCII
.NUL
;
589 Program
(Emit_Ptr
+ 2) := ASCII
.NUL
;
592 Emit_Ptr
:= Emit_Ptr
+ 3;
600 procedure Fail
(M
: in String) is
602 raise Expression_Error
;
605 -------------------------
606 -- Get_Curly_Arguments --
607 -------------------------
609 procedure Get_Curly_Arguments
613 Greedy
: out Boolean)
615 Save_Pos
: Natural := Parse_Pos
+ 1;
619 Max
:= Max_Curly_Repeat
;
621 while Expression
(Parse_Pos
) /= '}'
622 and then Expression
(Parse_Pos
) /= ','
624 Parse_Pos
:= Parse_Pos
+ 1;
627 Min
:= Natural'Value (Expression
(Save_Pos
.. Parse_Pos
- 1));
629 if Expression
(Parse_Pos
) = ',' then
630 Save_Pos
:= Parse_Pos
+ 1;
631 while Expression
(Parse_Pos
) /= '}' loop
632 Parse_Pos
:= Parse_Pos
+ 1;
635 if Save_Pos
/= Parse_Pos
then
636 Max
:= Natural'Value (Expression
(Save_Pos
.. Parse_Pos
- 1));
643 if Parse_Pos
< Expression
'Last
644 and then Expression
(Parse_Pos
+ 1) = '?'
647 Parse_Pos
:= Parse_Pos
+ 1;
652 end Get_Curly_Arguments
;
654 ---------------------------
655 -- Insert_Curly_Operator --
656 ---------------------------
658 procedure Insert_Curly_Operator
663 Greedy
: Boolean := True)
665 Dest
: constant Pointer
:= Emit_Ptr
;
670 -- If the operand is not greedy, insert an extra operand before it
676 -- Move the operand in the byte-compilation, so that we can insert
677 -- the operator before it.
680 Program
(Operand
+ Size
.. Emit_Ptr
+ Size
) :=
681 Program
(Operand
.. Emit_Ptr
);
684 -- Insert the operator at the position previously occupied by the
690 Old
:= Emit_Node
(MINMOD
);
691 Link_Tail
(Old
, Old
+ 3);
694 Old
:= Emit_Node
(Op
);
695 Emit_Natural
(Old
+ 3, Min
);
696 Emit_Natural
(Old
+ 5, Max
);
698 Emit_Ptr
:= Dest
+ Size
;
699 end Insert_Curly_Operator
;
701 ---------------------
702 -- Insert_Operator --
703 ---------------------
705 procedure Insert_Operator
708 Greedy
: Boolean := True)
710 Dest
: constant Pointer
:= Emit_Ptr
;
715 -- If not greedy, we have to emit another opcode first
721 -- Move the operand in the byte-compilation, so that we can insert
722 -- the operator before it.
725 Program
(Operand
+ Size
.. Emit_Ptr
+ Size
)
726 := Program
(Operand
.. Emit_Ptr
);
729 -- Insert the operator at the position previously occupied by the
735 Old
:= Emit_Node
(MINMOD
);
736 Link_Tail
(Old
, Old
+ 3);
739 Old
:= Emit_Node
(Op
);
740 Emit_Ptr
:= Dest
+ Size
;
743 -----------------------
744 -- Is_Curly_Operator --
745 -----------------------
747 function Is_Curly_Operator
(IP
: Natural) return Boolean is
748 Scan
: Natural := IP
;
751 if Expression
(Scan
) /= '{'
752 or else Scan
+ 2 > Expression
'Last
753 or else not Is_Digit
(Expression
(Scan
+ 1))
765 if Scan
> Expression
'Last then
769 exit when not Is_Digit
(Expression
(Scan
));
772 if Expression
(Scan
) = ',' then
776 if Scan
> Expression
'Last then
780 exit when not Is_Digit
(Expression
(Scan
));
784 return Expression
(Scan
) = '}';
785 end Is_Curly_Operator
;
791 function Is_Mult
(IP
: Natural) return Boolean is
792 C
: constant Character := Expression
(IP
);
798 or else (C
= '{' and then Is_Curly_Operator
(IP
));
801 -----------------------
802 -- Link_Operand_Tail --
803 -----------------------
805 procedure Link_Operand_Tail
(P
, Val
: Pointer
) is
807 if Emit_Code
and then Program
(P
) = BRANCH
then
808 Link_Tail
(Operand
(P
), Val
);
810 end Link_Operand_Tail
;
816 procedure Link_Tail
(P
, Val
: Pointer
) is
822 if not Emit_Code
then
830 Temp
:= Next_Instruction
(Scan
);
835 Offset
:= Val
- Scan
;
837 Emit_Natural
(Scan
+ 1, Natural (Offset
));
840 ----------------------
841 -- Next_Instruction --
842 ----------------------
844 function Next_Instruction
(P
: Pointer
) return Pointer
is
848 if not Emit_Code
then
852 Offset
:= Get_Next_Offset
(Program
, P
);
859 end Next_Instruction
;
865 -- Combining parenthesis handling with the base level
866 -- of regular expression is a trifle forced, but the
867 -- need to tie the tails of the branches to what follows
868 -- makes it hard to avoid.
871 (Parenthesized
: in Boolean;
872 Flags
: in out Expression_Flags
;
875 E
: String renames Expression
;
879 New_Flags
: Expression_Flags
;
880 Have_Branch
: Boolean := False;
883 Flags
:= (Has_Width
=> True, others => False); -- Tentatively
885 -- Make an OPEN node, if parenthesized
887 if Parenthesized
then
888 if Matcher
.Paren_Count
> Max_Paren_Count
then
889 Fail
("too many ()");
892 Par_No
:= Matcher
.Paren_Count
+ 1;
893 Matcher
.Paren_Count
:= Matcher
.Paren_Count
+ 1;
894 IP
:= Emit_Node
(OPEN
);
895 Emit
(Character'Val (Par_No
));
901 -- Pick up the branches, linking them together
903 Parse_Branch
(New_Flags
, True, Br
);
910 if Parse_Pos
<= Parse_End
911 and then E
(Parse_Pos
) = '|'
913 Insert_Operator
(BRANCH
, Br
);
918 Link_Tail
(IP
, Br
); -- OPEN -> first
923 if not New_Flags
.Has_Width
then
924 Flags
.Has_Width
:= False;
927 Flags
.SP_Start
:= Flags
.SP_Start
or New_Flags
.SP_Start
;
929 while Parse_Pos
<= Parse_End
930 and then (E
(Parse_Pos
) = '|')
932 Parse_Pos
:= Parse_Pos
+ 1;
933 Parse_Branch
(New_Flags
, False, Br
);
940 Link_Tail
(IP
, Br
); -- BRANCH -> BRANCH
942 if not New_Flags
.Has_Width
then
943 Flags
.Has_Width
:= False;
946 Flags
.SP_Start
:= Flags
.SP_Start
or New_Flags
.SP_Start
;
949 -- Make a closing node, and hook it on the end
951 if Parenthesized
then
952 Ender
:= Emit_Node
(CLOSE
);
953 Emit
(Character'Val (Par_No
));
955 Ender
:= Emit_Node
(EOP
);
958 Link_Tail
(IP
, Ender
);
962 -- Hook the tails of the branches to the closing node
967 Link_Operand_Tail
(Br
, Ender
);
968 Br
:= Next_Instruction
(Br
);
972 -- Check for proper termination
974 if Parenthesized
then
975 if Parse_Pos
> Parse_End
or else E
(Parse_Pos
) /= ')' then
976 Fail
("unmatched ()");
979 Parse_Pos
:= Parse_Pos
+ 1;
981 elsif Parse_Pos
<= Parse_End
then
982 if E
(Parse_Pos
) = ')' then
983 Fail
("unmatched ()");
985 Fail
("junk on end"); -- "Can't happen"
995 (Expr_Flags
: in out Expression_Flags
;
1001 -- Tentatively set worst expression case
1003 Expr_Flags
:= Worst_Expression
;
1005 C
:= Expression
(Parse_Pos
);
1006 Parse_Pos
:= Parse_Pos
+ 1;
1010 if (Flags
and Multiple_Lines
) /= 0 then
1011 IP
:= Emit_Node
(MBOL
);
1012 elsif (Flags
and Single_Line
) /= 0 then
1013 IP
:= Emit_Node
(SBOL
);
1015 IP
:= Emit_Node
(BOL
);
1019 if (Flags
and Multiple_Lines
) /= 0 then
1020 IP
:= Emit_Node
(MEOL
);
1021 elsif (Flags
and Single_Line
) /= 0 then
1022 IP
:= Emit_Node
(SEOL
);
1024 IP
:= Emit_Node
(EOL
);
1028 if (Flags
and Single_Line
) /= 0 then
1029 IP
:= Emit_Node
(SANY
);
1031 IP
:= Emit_Node
(ANY
);
1033 Expr_Flags
.Has_Width
:= True;
1034 Expr_Flags
.Simple
:= True;
1037 Parse_Character_Class
(IP
);
1038 Expr_Flags
.Has_Width
:= True;
1039 Expr_Flags
.Simple
:= True;
1043 New_Flags
: Expression_Flags
;
1046 Parse
(True, New_Flags
, IP
);
1052 Expr_Flags
.Has_Width
:=
1053 Expr_Flags
.Has_Width
or New_Flags
.Has_Width
;
1054 Expr_Flags
.SP_Start
:=
1055 Expr_Flags
.SP_Start
or New_Flags
.SP_Start
;
1058 when '|' | ASCII
.LF |
')' =>
1059 Fail
("internal urp"); -- Supposed to be caught earlier
1061 when '?' |
'+' |
'*' |
'{' =>
1062 Fail
("?+*{ follows nothing");
1065 if Parse_Pos
> Parse_End
then
1066 Fail
("trailing \");
1069 Parse_Pos := Parse_Pos + 1;
1071 case Expression (Parse_Pos - 1) is
1073 IP := Emit_Node (BOUND);
1076 IP := Emit_Node (NBOUND);
1079 IP := Emit_Node (SPACE);
1080 Expr_Flags.Simple := True;
1081 Expr_Flags.Has_Width := True;
1084 IP := Emit_Node (NSPACE);
1085 Expr_Flags.Simple := True;
1086 Expr_Flags.Has_Width := True;
1089 IP := Emit_Node (DIGIT);
1090 Expr_Flags.Simple := True;
1091 Expr_Flags.Has_Width := True;
1094 IP := Emit_Node (NDIGIT);
1095 Expr_Flags.Simple := True;
1096 Expr_Flags.Has_Width := True;
1099 IP := Emit_Node (ALNUM);
1100 Expr_Flags.Simple := True;
1101 Expr_Flags.Has_Width := True;
1104 IP := Emit_Node (NALNUM);
1105 Expr_Flags.Simple := True;
1106 Expr_Flags.Has_Width := True;
1109 IP := Emit_Node (SBOL);
1112 IP := Emit_Node (SEOL);
1115 IP := Emit_Node (REFF);
1118 Save : Natural := Parse_Pos - 1;
1121 while Parse_Pos <= Expression'Last
1122 and then Is_Digit (Expression (Parse_Pos))
1124 Parse_Pos := Parse_Pos + 1;
1127 Emit (Character'Val (Natural'Value
1128 (Expression (Save .. Parse_Pos - 1))));
1132 Parse_Pos := Parse_Pos - 1;
1133 Parse_Literal (Expr_Flags, IP);
1136 when others => Parse_Literal (Expr_Flags, IP);
1144 procedure Parse_Branch
1145 (Flags : in out Expression_Flags;
1149 E : String renames Expression;
1152 New_Flags : Expression_Flags;
1156 Flags := Worst_Expression; -- Tentatively
1161 IP := Emit_Node (BRANCH);
1166 while Parse_Pos <= Parse_End
1167 and then E (Parse_Pos) /= ')'
1168 and then E (Parse_Pos) /= ASCII.LF
1169 and then E (Parse_Pos) /= '|'
1171 Parse_Piece (New_Flags, Last);
1178 Flags.Has_Width := Flags.Has_Width or New_Flags.Has_Width;
1180 if Chain = 0 then -- First piece
1181 Flags.SP_Start := Flags.SP_Start or New_Flags.SP_Start;
1183 Link_Tail (Chain, Last);
1189 if Chain = 0 then -- Loop ran zero CURLY
1190 Dummy := Emit_Node (NOTHING);
1195 ---------------------------
1196 -- Parse_Character_Class --
1197 ---------------------------
1199 procedure Parse_Character_Class (IP : out Pointer) is
1200 Bitmap : Character_Class;
1201 Invert : Boolean := False;
1202 In_Range : Boolean := False;
1203 Named_Class : Std_Class := ANYOF_NONE;
1205 Last_Value : Character := ASCII.Nul;
1208 Reset_Class (Bitmap);
1210 -- Do we have an invert character class ?
1212 if Parse_Pos <= Parse_End
1213 and then Expression (Parse_Pos) = '^'
1216 Parse_Pos := Parse_Pos + 1;
1219 -- First character can be ] or -, without closing the class.
1221 if Parse_Pos <= Parse_End
1222 and then (Expression (Parse_Pos) = ']'
1223 or else Expression (Parse_Pos) = '-')
1225 Set_In_Class (Bitmap, Expression (Parse_Pos));
1226 Parse_Pos := Parse_Pos + 1;
1229 -- While we don't have the end of the class
1231 while Parse_Pos <= Parse_End
1232 and then Expression (Parse_Pos) /= ']'
1234 Named_Class := ANYOF_NONE;
1235 Value := Expression (Parse_Pos);
1236 Parse_Pos := Parse_Pos + 1;
1238 -- Do we have a Posix character class
1240 Named_Class := Parse_Posix_Character_Class;
1242 elsif Value = '\' then
1243 if Parse_Pos = Parse_End then
1244 Fail ("Trailing
\");
1246 Value
:= Expression
(Parse_Pos
);
1247 Parse_Pos
:= Parse_Pos
+ 1;
1250 when 'w' => Named_Class
:= ANYOF_ALNUM
;
1251 when 'W' => Named_Class
:= ANYOF_NALNUM
;
1252 when 's' => Named_Class
:= ANYOF_SPACE
;
1253 when 'S' => Named_Class
:= ANYOF_NSPACE
;
1254 when 'd' => Named_Class
:= ANYOF_DIGIT
;
1255 when 'D' => Named_Class
:= ANYOF_NDIGIT
;
1256 when 'n' => Value
:= ASCII
.LF
;
1257 when 'r' => Value
:= ASCII
.CR
;
1258 when 't' => Value
:= ASCII
.HT
;
1259 when 'f' => Value
:= ASCII
.FF
;
1260 when 'e' => Value
:= ASCII
.ESC
;
1261 when 'a' => Value
:= ASCII
.BEL
;
1263 -- when 'x' => ??? hexadecimal value
1264 -- when 'c' => ??? control character
1265 -- when '0'..'9' => ??? octal character
1267 when others => null;
1271 -- Do we have a character class?
1273 if Named_Class
/= ANYOF_NONE
then
1275 -- A range like 'a-\d' or 'a-[:digit:] is not a range
1278 Set_In_Class
(Bitmap
, Last_Value
);
1279 Set_In_Class
(Bitmap
, '-');
1286 when ANYOF_NONE
=> null;
1288 when ANYOF_ALNUM | ANYOF_ALNUMC
=>
1289 for Value
in Class_Byte
'Range loop
1290 if Is_Alnum
(Character'Val (Value
)) then
1291 Set_In_Class
(Bitmap
, Character'Val (Value
));
1295 when ANYOF_NALNUM | ANYOF_NALNUMC
=>
1296 for Value
in Class_Byte
'Range loop
1297 if not Is_Alnum
(Character'Val (Value
)) then
1298 Set_In_Class
(Bitmap
, Character'Val (Value
));
1303 for Value
in Class_Byte
'Range loop
1304 if Is_Space
(Character'Val (Value
)) then
1305 Set_In_Class
(Bitmap
, Character'Val (Value
));
1309 when ANYOF_NSPACE
=>
1310 for Value
in Class_Byte
'Range loop
1311 if not Is_Space
(Character'Val (Value
)) then
1312 Set_In_Class
(Bitmap
, Character'Val (Value
));
1317 for Value
in Class_Byte
'Range loop
1318 if Is_Digit
(Character'Val (Value
)) then
1319 Set_In_Class
(Bitmap
, Character'Val (Value
));
1323 when ANYOF_NDIGIT
=>
1324 for Value
in Class_Byte
'Range loop
1325 if not Is_Digit
(Character'Val (Value
)) then
1326 Set_In_Class
(Bitmap
, Character'Val (Value
));
1331 for Value
in Class_Byte
'Range loop
1332 if Is_Letter
(Character'Val (Value
)) then
1333 Set_In_Class
(Bitmap
, Character'Val (Value
));
1337 when ANYOF_NALPHA
=>
1338 for Value
in Class_Byte
'Range loop
1339 if not Is_Letter
(Character'Val (Value
)) then
1340 Set_In_Class
(Bitmap
, Character'Val (Value
));
1345 for Value
in 0 .. 127 loop
1346 Set_In_Class
(Bitmap
, Character'Val (Value
));
1349 when ANYOF_NASCII
=>
1350 for Value
in 128 .. 255 loop
1351 Set_In_Class
(Bitmap
, Character'Val (Value
));
1355 for Value
in Class_Byte
'Range loop
1356 if Is_Control
(Character'Val (Value
)) then
1357 Set_In_Class
(Bitmap
, Character'Val (Value
));
1361 when ANYOF_NCNTRL
=>
1362 for Value
in Class_Byte
'Range loop
1363 if not Is_Control
(Character'Val (Value
)) then
1364 Set_In_Class
(Bitmap
, Character'Val (Value
));
1369 for Value
in Class_Byte
'Range loop
1370 if Is_Graphic
(Character'Val (Value
)) then
1371 Set_In_Class
(Bitmap
, Character'Val (Value
));
1375 when ANYOF_NGRAPH
=>
1376 for Value
in Class_Byte
'Range loop
1377 if not Is_Graphic
(Character'Val (Value
)) then
1378 Set_In_Class
(Bitmap
, Character'Val (Value
));
1383 for Value
in Class_Byte
'Range loop
1384 if Is_Lower
(Character'Val (Value
)) then
1385 Set_In_Class
(Bitmap
, Character'Val (Value
));
1389 when ANYOF_NLOWER
=>
1390 for Value
in Class_Byte
'Range loop
1391 if not Is_Lower
(Character'Val (Value
)) then
1392 Set_In_Class
(Bitmap
, Character'Val (Value
));
1397 for Value
in Class_Byte
'Range loop
1398 if Is_Printable
(Character'Val (Value
)) then
1399 Set_In_Class
(Bitmap
, Character'Val (Value
));
1403 when ANYOF_NPRINT
=>
1404 for Value
in Class_Byte
'Range loop
1405 if not Is_Printable
(Character'Val (Value
)) then
1406 Set_In_Class
(Bitmap
, Character'Val (Value
));
1411 for Value
in Class_Byte
'Range loop
1412 if Is_Printable
(Character'Val (Value
))
1413 and then not Is_Space
(Character'Val (Value
))
1414 and then not Is_Alnum
(Character'Val (Value
))
1416 Set_In_Class
(Bitmap
, Character'Val (Value
));
1420 when ANYOF_NPUNCT
=>
1421 for Value
in Class_Byte
'Range loop
1422 if not Is_Printable
(Character'Val (Value
))
1423 or else Is_Space
(Character'Val (Value
))
1424 or else Is_Alnum
(Character'Val (Value
))
1426 Set_In_Class
(Bitmap
, Character'Val (Value
));
1431 for Value
in Class_Byte
'Range loop
1432 if Is_Upper
(Character'Val (Value
)) then
1433 Set_In_Class
(Bitmap
, Character'Val (Value
));
1437 when ANYOF_NUPPER
=>
1438 for Value
in Class_Byte
'Range loop
1439 if not Is_Upper
(Character'Val (Value
)) then
1440 Set_In_Class
(Bitmap
, Character'Val (Value
));
1444 when ANYOF_XDIGIT
=>
1445 for Value
in Class_Byte
'Range loop
1446 if Is_Hexadecimal_Digit
(Character'Val (Value
)) then
1447 Set_In_Class
(Bitmap
, Character'Val (Value
));
1451 when ANYOF_NXDIGIT
=>
1452 for Value
in Class_Byte
'Range loop
1453 if not Is_Hexadecimal_Digit
1454 (Character'Val (Value
))
1456 Set_In_Class
(Bitmap
, Character'Val (Value
));
1462 -- Not a character range
1464 elsif not In_Range
then
1465 Last_Value
:= Value
;
1467 if Expression
(Parse_Pos
) = '-'
1468 and then Parse_Pos
< Parse_End
1469 and then Expression
(Parse_Pos
+ 1) /= ']'
1471 Parse_Pos
:= Parse_Pos
+ 1;
1473 -- Do we have a range like '\d-a' and '[:space:]-a'
1474 -- which is not a real range
1476 if Named_Class
/= ANYOF_NONE
then
1477 Set_In_Class
(Bitmap
, '-');
1483 Set_In_Class
(Bitmap
, Value
);
1487 -- Else in a character range
1490 if Last_Value
> Value
then
1491 Fail
("Invalid Range [" & Last_Value
'Img
1492 & "-" & Value
'Img & "]");
1495 while Last_Value
<= Value
loop
1496 Set_In_Class
(Bitmap
, Last_Value
);
1497 Last_Value
:= Character'Succ (Last_Value
);
1506 -- Optimize case-insensitive ranges (put the upper case or lower
1507 -- case character into the bitmap)
1509 if (Flags
and Case_Insensitive
) /= 0 then
1510 for C
in Character'Range loop
1511 if Get_From_Class
(Bitmap
, C
) then
1512 Set_In_Class
(Bitmap
, To_Lower
(C
));
1513 Set_In_Class
(Bitmap
, To_Upper
(C
));
1518 -- Optimize inverted classes
1521 for J
in Bitmap
'Range loop
1522 Bitmap
(J
) := not Bitmap
(J
);
1526 Parse_Pos
:= Parse_Pos
+ 1;
1530 IP
:= Emit_Node
(ANYOF
);
1531 Emit_Class
(Bitmap
);
1532 end Parse_Character_Class
;
1538 -- This is a bit tricky due to quoted chars and due to
1539 -- the multiplier characters '*', '+', and '?' that
1540 -- take the SINGLE char previous as their operand.
1542 -- On entry, the character at Parse_Pos - 1 is going to go
1543 -- into the string, no matter what it is. It could be
1544 -- following a \ if Parse_Atom was entered from the '\' case.
1546 -- Basic idea is to pick up a good char in C and examine
1547 -- the next char. If Is_Mult (C) then twiddle, if it's a \
1548 -- then frozzle and if it's another magic char then push C and
1549 -- terminate the string. If none of the above, push C on the
1550 -- string and go around again.
1552 -- Start_Pos is used to remember where "the current character"
1553 -- starts in the string, if due to an Is_Mult we need to back
1554 -- up and put the current char in a separate 1-character string.
1555 -- When Start_Pos is 0, C is the only char in the string;
1556 -- this is used in Is_Mult handling, and in setting the SIMPLE
1559 procedure Parse_Literal
1560 (Expr_Flags
: in out Expression_Flags
;
1563 Start_Pos
: Natural := 0;
1565 Length_Ptr
: Pointer
;
1566 Has_Special_Operator
: Boolean := False;
1569 Parse_Pos
:= Parse_Pos
- 1; -- Look at current character
1571 if (Flags
and Case_Insensitive
) /= 0 then
1572 IP
:= Emit_Node
(EXACTF
);
1574 IP
:= Emit_Node
(EXACT
);
1577 Length_Ptr
:= Emit_Ptr
;
1578 Emit_Ptr
:= String_Operand
(IP
);
1583 C
:= Expression
(Parse_Pos
); -- Get current character
1586 when '.' |
'[' |
'(' |
')' |
'|' | ASCII
.LF |
'$' |
'^' =>
1588 if Start_Pos
= 0 then
1589 Start_Pos
:= Parse_Pos
;
1590 Emit
(C
); -- First character is always emitted
1592 exit Parse_Loop
; -- Else we are done
1595 when '?' |
'+' |
'*' |
'{' =>
1597 if Start_Pos
= 0 then
1598 Start_Pos
:= Parse_Pos
;
1599 Emit
(C
); -- First character is always emitted
1601 -- Are we looking at an operator, or is this
1602 -- simply a normal character ?
1603 elsif not Is_Mult
(Parse_Pos
) then
1604 Start_Pos
:= Parse_Pos
;
1607 -- We've got something like "abc?d". Mark this as a
1608 -- special case. What we want to emit is a first
1609 -- constant string for "ab", then one for "c" that will
1610 -- ultimately be transformed with a CURLY operator, A
1611 -- special case has to be handled for "a?", since there
1612 -- is no initial string to emit.
1613 Has_Special_Operator
:= True;
1618 Start_Pos
:= Parse_Pos
;
1619 if Parse_Pos
= Parse_End
then
1620 Fail
("Trailing \");
1622 case Expression (Parse_Pos + 1) is
1623 when 'b' | 'B' | 's' | 'S' | 'd' | 'D'
1624 | 'w' | 'W' | '0' .. '9' | 'G' | 'A'
1626 when 'n' => Emit (ASCII.LF);
1627 when 't' => Emit (ASCII.HT);
1628 when 'r' => Emit (ASCII.CR);
1629 when 'f' => Emit (ASCII.FF);
1630 when 'e' => Emit (ASCII.ESC);
1631 when 'a' => Emit (ASCII.BEL);
1632 when others => Emit (Expression (Parse_Pos + 1));
1634 Parse_Pos := Parse_Pos + 1;
1638 Start_Pos := Parse_Pos;
1642 exit Parse_Loop when Emit_Ptr - Length_Ptr = 254;
1644 Parse_Pos := Parse_Pos + 1;
1646 exit Parse_Loop when Parse_Pos > Parse_End;
1647 end loop Parse_Loop;
1649 -- Is the string followed by a '*+?{' operator ? If yes, and if there
1650 -- is an initial string to emit, do it now.
1652 if Has_Special_Operator
1653 and then Emit_Ptr >= Length_Ptr + 3
1655 Emit_Ptr := Emit_Ptr - 1;
1656 Parse_Pos := Start_Pos;
1660 Program (Length_Ptr) := Character'Val (Emit_Ptr - Length_Ptr - 2);
1663 Expr_Flags.Has_Width := True;
1665 -- Slight optimization when there is a single character
1667 if Emit_Ptr = Length_Ptr + 2 then
1668 Expr_Flags.Simple := True;
1676 -- Note that the branching code sequences used for '?' and the
1677 -- general cases of '*' and + are somewhat optimized: they use
1678 -- the same NOTHING node as both the endmarker for their branch
1679 -- list and the body of the last branch. It might seem that
1680 -- this node could be dispensed with entirely, but the endmarker
1681 -- role is not redundant.
1683 procedure Parse_Piece
1684 (Expr_Flags : in out Expression_Flags;
1688 New_Flags : Expression_Flags;
1689 Greedy : Boolean := True;
1692 Parse_Atom (New_Flags, IP);
1698 if Parse_Pos > Parse_End
1699 or else not Is_Mult (Parse_Pos)
1701 Expr_Flags := New_Flags;
1705 Op := Expression (Parse_Pos);
1708 Expr_Flags := (SP_Start => True, others => False);
1710 Expr_Flags := (Has_Width => True, others => False);
1713 -- Detect non greedy operators in the easy cases
1716 and then Parse_Pos + 1 <= Parse_End
1717 and then Expression (Parse_Pos + 1) = '?'
1720 Parse_Pos := Parse_Pos + 1;
1723 -- Generate the byte code
1728 if New_Flags.Simple then
1729 Insert_Operator (STAR, IP, Greedy);
1731 Link_Tail (IP, Emit_Node (WHILEM));
1732 Insert_Curly_Operator
1733 (CURLYX, 0, Max_Curly_Repeat, IP, Greedy);
1734 Link_Tail (IP, Emit_Node (NOTHING));
1739 if New_Flags.Simple then
1740 Insert_Operator (PLUS, IP, Greedy);
1742 Link_Tail (IP, Emit_Node (WHILEM));
1743 Insert_Curly_Operator
1744 (CURLYX, 1, Max_Curly_Repeat, IP, Greedy);
1745 Link_Tail (IP, Emit_Node (NOTHING));
1749 if New_Flags.Simple then
1750 Insert_Curly_Operator (CURLY, 0, 1, IP, Greedy);
1752 Link_Tail (IP, Emit_Node (WHILEM));
1753 Insert_Curly_Operator (CURLYX, 0, 1, IP, Greedy);
1754 Link_Tail (IP, Emit_Node (NOTHING));
1762 Get_Curly_Arguments (Parse_Pos, Min, Max, Greedy);
1764 if New_Flags.Simple then
1765 Insert_Curly_Operator (CURLY, Min, Max, IP, Greedy);
1767 Link_Tail (IP, Emit_Node (WHILEM));
1768 Insert_Curly_Operator (CURLYX, Min, Max, IP, Greedy);
1769 Link_Tail (IP, Emit_Node (NOTHING));
1777 Parse_Pos := Parse_Pos + 1;
1779 if Parse_Pos <= Parse_End
1780 and then Is_Mult (Parse_Pos)
1782 Fail ("nested
*+{");
1786 ---------------------------------
1787 -- Parse_Posix_Character_Class --
1788 ---------------------------------
1790 function Parse_Posix_Character_Class return Std_Class is
1791 Invert : Boolean := False;
1792 Class : Std_Class := ANYOF_NONE;
1793 E : String renames Expression;
1796 if Parse_Pos <= Parse_End
1797 and then Expression (Parse_Pos) = ':'
1799 Parse_Pos := Parse_Pos + 1;
1801 -- Do we have something like: [[:^alpha:]]
1803 if Parse_Pos <= Parse_End
1804 and then Expression (Parse_Pos) = '^'
1807 Parse_Pos := Parse_Pos + 1;
1810 -- All classes have 6 characters at least
1811 -- ??? magid constant 6 should have a name!
1813 if Parse_Pos + 6 <= Parse_End then
1815 case Expression (Parse_Pos) is
1817 if E (Parse_Pos .. Parse_Pos + 4) = "alnum
:]" then
1819 Class := ANYOF_NALNUMC;
1821 Class := ANYOF_ALNUMC;
1824 elsif E (Parse_Pos .. Parse_Pos + 6) = "alpha
:]" then
1826 Class := ANYOF_NALPHA;
1828 Class := ANYOF_ALPHA;
1831 elsif E (Parse_Pos .. Parse_Pos + 6) = "ascii
:]" then
1833 Class := ANYOF_NASCII;
1835 Class := ANYOF_ASCII;
1841 if E (Parse_Pos .. Parse_Pos + 6) = "cntrl
:]" then
1843 Class := ANYOF_NCNTRL;
1845 Class := ANYOF_CNTRL;
1851 if E (Parse_Pos .. Parse_Pos + 6) = "digit
:]" then
1853 Class := ANYOF_NDIGIT;
1855 Class := ANYOF_DIGIT;
1861 if E (Parse_Pos .. Parse_Pos + 6) = "graph
:]" then
1863 Class := ANYOF_NGRAPH;
1865 Class := ANYOF_GRAPH;
1871 if E (Parse_Pos .. Parse_Pos + 6) = "lower
:]" then
1873 Class := ANYOF_NLOWER;
1875 Class := ANYOF_LOWER;
1881 if E (Parse_Pos .. Parse_Pos + 6) = "print
:]" then
1883 Class := ANYOF_NPRINT;
1885 Class := ANYOF_PRINT;
1888 elsif E (Parse_Pos .. Parse_Pos + 6) = "punct
:]" then
1890 Class := ANYOF_NPUNCT;
1892 Class := ANYOF_PUNCT;
1898 if E (Parse_Pos .. Parse_Pos + 6) = "space
:]" then
1900 Class := ANYOF_NSPACE;
1902 Class := ANYOF_SPACE;
1908 if E (Parse_Pos .. Parse_Pos + 6) = "upper
:]" then
1910 Class := ANYOF_NUPPER;
1912 Class := ANYOF_UPPER;
1918 if E (Parse_Pos .. Parse_Pos + 5) = "word
:]" then
1920 Class := ANYOF_NALNUM;
1922 Class := ANYOF_ALNUM;
1925 Parse_Pos := Parse_Pos - 1;
1930 if Parse_Pos + 7 <= Parse_End
1931 and then E (Parse_Pos .. Parse_Pos + 7) = "xdigit
:]"
1934 Class := ANYOF_NXDIGIT;
1936 Class := ANYOF_XDIGIT;
1939 Parse_Pos := Parse_Pos + 1;
1943 Class := ANYOF_NONE;
1947 if Class /= ANYOF_NONE then
1948 Parse_Pos := Parse_Pos + 7;
1952 Fail ("Invalid
character class
");
1960 end Parse_Posix_Character_Class;
1962 Expr_Flags : Expression_Flags;
1965 -- Start of processing for Compile
1969 Parse (False, Expr_Flags, Result);
1972 Fail ("Couldn
't compile expression
");
1975 Final_Code_Size := Emit_Ptr - 1;
1977 -- Do we want to actually compile the expression, or simply get the
1988 (Expression : String;
1989 Flags : Regexp_Flags := No_Flags)
1990 return Pattern_Matcher
1992 Size : Program_Size;
1993 Dummy : Pattern_Matcher (0);
1996 Compile (Dummy, Expression, Size, Flags);
1999 Result : Pattern_Matcher (Size);
2001 Compile (Result, Expression, Size, Flags);
2007 (Matcher : out Pattern_Matcher;
2008 Expression : String;
2009 Flags : Regexp_Flags := No_Flags)
2011 Size : Program_Size;
2014 Compile (Matcher, Expression, Size, Flags);
2021 procedure Dump (Self : Pattern_Matcher) is
2023 -- Index : Pointer := Program_First + 1;
2024 -- What is the above line for ???
2027 Program : Program_Data renames Self.Program;
2029 procedure Dump_Until
2032 Indent : Natural := 0);
2033 -- Dump the program until the node Till (not included) is met.
2034 -- Every line is indented with Index spaces at the beginning
2035 -- Dumps till the end if Till is 0.
2041 procedure Dump_Until
2044 Indent : Natural := 0)
2047 Index : Pointer := Start;
2048 Local_Indent : Natural := Indent;
2052 while Index < Till loop
2054 Op := Opcode'Val (Character'Pos ((Self.Program (Index))));
2057 Local_Indent := Local_Indent - 3;
2061 Point : String := Pointer'Image (Index);
2064 for J in 1 .. 6 - Point'Length loop
2070 & (1 .. Local_Indent => ' ')
2071 & Opcode'Image (Op));
2074 -- Print the parenthesis number
2076 if Op = OPEN or else Op = CLOSE or else Op = REFF then
2077 Put (Natural'Image (Character'Pos (Program (Index + 3))));
2080 Next := Index + Get_Next_Offset (Program, Index);
2082 if Next = Index then
2083 Put (" (next
at 0)");
2085 Put (" (next
at " & Pointer'Image (Next) & ")");
2090 -- Character class operand
2094 Bitmap : Character_Class;
2095 Last : Character := ASCII.Nul;
2096 Current : Natural := 0;
2098 Current_Char : Character;
2101 Bitmap_Operand (Program, Index, Bitmap);
2104 while Current <= 255 loop
2105 Current_Char := Character'Val (Current);
2107 -- First item in a range
2109 if Get_From_Class (Bitmap, Current_Char) then
2110 Last := Current_Char;
2112 -- Search for the last item in the range
2115 Current := Current + 1;
2116 exit when Current > 255;
2117 Current_Char := Character'Val (Current);
2119 not Get_From_Class (Bitmap, Current_Char);
2129 if Character'Succ (Last) /= Current_Char then
2130 Put ("-" & Character'Pred (Current_Char));
2134 Current := Current + 1;
2139 Index := Index + 3 + Bitmap'Length;
2144 when EXACT | EXACTF =>
2145 Length := String_Length (Program, Index);
2146 Put (" operand
(length
:" & Program_Size'Image (Length + 1)
2148 & String (Program (String_Operand (Index)
2149 .. String_Operand (Index)
2151 Index := String_Operand (Index) + Length + 1;
2158 Dump_Until (Index + 3, Next, Local_Indent + 3);
2164 -- Only one instruction
2166 Dump_Until (Index + 3, Index + 4, Local_Indent + 3);
2169 when CURLY | CURLYX =>
2171 & Natural'Image (Read_Natural (Program, Index + 3))
2173 & Natural'Image (Read_Natural (Program, Index + 5))
2176 Dump_Until (Index + 7, Next, Local_Indent + 3);
2182 Local_Indent := Local_Indent + 3;
2184 when CLOSE | REFF =>
2202 -- Start of processing for Dump
2205 pragma Assert (Self.Program (Program_First) = MAGIC,
2206 "Corrupted Pattern_Matcher
");
2208 Put_Line ("Must start
with (Self
.First
) = "
2209 & Character'Image (Self.First));
2211 if (Self.Flags and Case_Insensitive) /= 0 then
2212 Put_Line (" Case_Insensitive mode
");
2215 if (Self.Flags and Single_Line) /= 0 then
2216 Put_Line (" Single_Line mode
");
2219 if (Self.Flags and Multiple_Lines) /= 0 then
2220 Put_Line (" Multiple_Lines mode
");
2223 Put_Line (" 1 : MAGIC
");
2224 Dump_Until (Program_First + 1, Self.Program'Last + 1);
2227 --------------------
2228 -- Get_From_Class --
2229 --------------------
2231 function Get_From_Class
2232 (Bitmap : Character_Class;
2236 Value : constant Class_Byte := Character'Pos (C);
2239 return (Bitmap (Value / 8)
2240 and Bit_Conversion (Value mod 8)) /= 0;
2247 function Get_Next (Program : Program_Data; IP : Pointer) return Pointer is
2248 Offset : constant Pointer := Get_Next_Offset (Program, IP);
2258 ---------------------
2259 -- Get_Next_Offset --
2260 ---------------------
2262 function Get_Next_Offset
2263 (Program : Program_Data;
2268 return Pointer (Read_Natural (Program, IP + 1));
2269 end Get_Next_Offset;
2275 function Is_Alnum (C : Character) return Boolean is
2277 return Is_Alphanumeric (C) or else C = '_';
2284 function Is_Printable (C : Character) return Boolean is
2285 Value : constant Natural := Character'Pos (C);
2288 return (Value > 32 and then Value < 127)
2289 or else Is_Space (C);
2296 function Is_Space (C : Character) return Boolean is
2299 or else C = ASCII.HT
2300 or else C = ASCII.CR
2301 or else C = ASCII.LF
2302 or else C = ASCII.VT
2303 or else C = ASCII.FF;
2311 (Self : Pattern_Matcher;
2313 Matches : out Match_Array)
2315 Program : Program_Data renames Self.Program; -- Shorter notation
2317 -- Global work variables
2319 Input_Pos : Natural; -- String-input pointer
2320 BOL_Pos : Natural; -- Beginning of input, for ^ check
2321 Matched : Boolean := False; -- Until proven True
2323 Matches_Full : Match_Array (0 .. Natural'Max (Self.Paren_Count,
2325 -- Stores the value of all the parenthesis pairs.
2326 -- We do not use directly Matches, so that we can also use back
2327 -- references (REFF) even if Matches is too small.
2329 type Natural_Array is array (Match_Count range <>) of Natural;
2330 Matches_Tmp : Natural_Array (Matches_Full'Range);
2331 -- Save the opening position of parenthesis.
2333 Last_Paren : Natural := 0;
2334 -- Last parenthesis seen
2336 Greedy : Boolean := True;
2337 -- True if the next operator should be greedy
2339 type Current_Curly_Record;
2340 type Current_Curly_Access is access all Current_Curly_Record;
2341 type Current_Curly_Record is record
2342 Paren_Floor : Natural; -- How far back to strip parenthesis data
2343 Cur : Integer; -- How many instances of scan we've matched
2344 Min : Natural; -- Minimal number of scans to match
2345 Max : Natural; -- Maximal number of scans to match
2346 Greedy : Boolean; -- Whether to work our way up or down
2347 Scan : Pointer; -- The thing to match
2348 Next : Pointer; -- What has to match after it
2349 Lastloc : Natural; -- Where we started matching this scan
2350 Old_Cc : Current_Curly_Access; -- Before we started this one
2352 -- Data used to handle the curly operator and the plus and star
2353 -- operators for complex expressions.
2355 Current_Curly : Current_Curly_Access := null;
2356 -- The curly currently being processed.
2358 -----------------------
2359 -- Local Subprograms --
2360 -----------------------
2362 function Index (Start : Positive; C : Character) return Natural;
2363 -- Find character C in Data starting at Start and return position
2367 Max : Natural := Natural'Last)
2369 -- Repeatedly match something simple, report how many
2370 -- It only matches on things of length 1.
2371 -- Starting from Input_Pos, it matches at most Max CURLY.
2373 function Try (Pos : in Positive) return Boolean;
2374 -- Try to match at specific point
2376 function Match (IP : Pointer) return Boolean;
2377 -- This is the main matching routine. Conceptually the strategy
2378 -- is simple: check to see whether the current node matches,
2379 -- call self recursively to see whether the rest matches,
2380 -- and then act accordingly.
2382 -- In practice Match makes some effort to avoid recursion, in
2383 -- particular by going through "ordinary
" nodes (that don't
2384 -- need to know whether the rest of the match failed) by
2385 -- using a loop instead of recursion.
2387 function Match_Whilem (IP : Pointer) return Boolean;
2388 -- Return True if a WHILEM matches
2390 function Recurse_Match (IP : Pointer; From : Natural) return Boolean;
2391 pragma Inline (Recurse_Match);
2392 -- Calls Match recursively. It saves and restores the parenthesis
2393 -- status and location in the input stream correctly, so that
2394 -- backtracking is possible
2396 function Match_Simple_Operator
2402 -- Return True it the simple operator (possibly non-greedy) matches
2404 pragma Inline (Index);
2405 pragma Inline (Repeat);
2407 -- These are two complex functions, but used only once.
2409 pragma Inline (Match_Whilem);
2410 pragma Inline (Match_Simple_Operator);
2422 for J in Start .. Data'Last loop
2423 if Data (J) = C then
2435 function Recurse_Match (IP : Pointer; From : Natural) return Boolean is
2436 L : constant Natural := Last_Paren;
2437 Tmp_F : constant Match_Array :=
2438 Matches_Full (From + 1 .. Matches_Full'Last);
2439 Start : constant Natural_Array :=
2440 Matches_Tmp (From + 1 .. Matches_Tmp'Last);
2441 Input : constant Natural := Input_Pos;
2447 Matches_Full (Tmp_F'Range) := Tmp_F;
2448 Matches_Tmp (Start'Range) := Start;
2457 function Match (IP : Pointer) return Boolean is
2458 Scan : Pointer := IP;
2465 pragma Assert (Scan /= 0);
2467 -- Determine current opcode and count its usage in debug mode
2469 Op := Opcode'Val (Character'Pos (Program (Scan)));
2471 -- Calculate offset of next instruction.
2472 -- Second character is most significant in Program_Data.
2474 Next := Get_Next (Program, Scan);
2478 return True; -- Success !
2481 if Program (Next) /= BRANCH then
2482 Next := Operand (Scan); -- No choice, avoid recursion
2486 if Recurse_Match (Operand (Scan), 0) then
2490 Scan := Get_Next (Program, Scan);
2491 exit when Scan = 0 or Program (Scan) /= BRANCH;
2501 exit State_Machine when
2502 Input_Pos /= BOL_Pos
2503 and then ((Self.Flags and Multiple_Lines) = 0
2504 or else Data (Input_Pos - 1) /= ASCII.LF);
2507 exit State_Machine when
2508 Input_Pos /= BOL_Pos
2509 and then Data (Input_Pos - 1) /= ASCII.LF;
2512 exit State_Machine when Input_Pos /= BOL_Pos;
2515 exit State_Machine when
2516 Input_Pos <= Data'Last
2517 and then ((Self.Flags and Multiple_Lines) = 0
2518 or else Data (Input_Pos) /= ASCII.LF);
2521 exit State_Machine when
2522 Input_Pos <= Data'Last
2523 and then Data (Input_Pos) /= ASCII.LF;
2526 exit State_Machine when Input_Pos <= Data'Last;
2528 when BOUND | NBOUND =>
2530 -- Was last char in word ?
2533 N : Boolean := False;
2534 Ln : Boolean := False;
2537 if Input_Pos /= Data'First then
2538 N := Is_Alnum (Data (Input_Pos - 1));
2541 if Input_Pos > Data'Last then
2544 Ln := Is_Alnum (Data (Input_Pos));
2559 exit State_Machine when
2560 Input_Pos > Data'Last
2561 or else not Is_Space (Data (Input_Pos));
2562 Input_Pos := Input_Pos + 1;
2565 exit State_Machine when
2566 Input_Pos > Data'Last
2567 or else Is_Space (Data (Input_Pos));
2568 Input_Pos := Input_Pos + 1;
2571 exit State_Machine when
2572 Input_Pos > Data'Last
2573 or else not Is_Digit (Data (Input_Pos));
2574 Input_Pos := Input_Pos + 1;
2577 exit State_Machine when
2578 Input_Pos > Data'Last
2579 or else Is_Digit (Data (Input_Pos));
2580 Input_Pos := Input_Pos + 1;
2583 exit State_Machine when
2584 Input_Pos > Data'Last
2585 or else not Is_Alnum (Data (Input_Pos));
2586 Input_Pos := Input_Pos + 1;
2589 exit State_Machine when
2590 Input_Pos > Data'Last
2591 or else Is_Alnum (Data (Input_Pos));
2592 Input_Pos := Input_Pos + 1;
2595 exit State_Machine when Input_Pos > Data'Last
2596 or else Data (Input_Pos) = ASCII.LF;
2597 Input_Pos := Input_Pos + 1;
2600 exit State_Machine when Input_Pos > Data'Last;
2601 Input_Pos := Input_Pos + 1;
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) /= Data (Current);
2614 Current := Current + 1;
2618 Input_Pos := Current;
2623 Opnd : Pointer := String_Operand (Scan);
2624 Current : Positive := Input_Pos;
2625 Last : constant Pointer :=
2626 Opnd + String_Length (Program, Scan);
2629 while Opnd <= Last loop
2630 exit State_Machine when Current > Data'Last
2631 or else Program (Opnd) /= To_Lower (Data (Current));
2632 Current := Current + 1;
2636 Input_Pos := Current;
2641 Bitmap : Character_Class;
2644 Bitmap_Operand (Program, Scan, Bitmap);
2645 exit State_Machine when
2646 Input_Pos > Data'Last
2647 or else not Get_From_Class (Bitmap, Data (Input_Pos));
2648 Input_Pos := Input_Pos + 1;
2653 No : constant Natural :=
2654 Character'Pos (Program (Operand (Scan)));
2656 Matches_Tmp (No) := Input_Pos;
2661 No : constant Natural :=
2662 Character'Pos (Program (Operand (Scan)));
2664 Matches_Full (No) := (Matches_Tmp (No), Input_Pos - 1);
2665 if Last_Paren < No then
2672 No : constant Natural :=
2673 Character'Pos (Program (Operand (Scan)));
2677 -- If we haven't seen that parenthesis yet
2679 if Last_Paren < No then
2683 Data_Pos := Matches_Full (No).First;
2684 while Data_Pos <= Matches_Full (No).Last loop
2685 if Input_Pos > Data'Last
2686 or else Data (Input_Pos) /= Data (Data_Pos)
2691 Input_Pos := Input_Pos + 1;
2692 Data_Pos := Data_Pos + 1;
2699 when STAR | PLUS | CURLY =>
2701 Greed : constant Boolean := Greedy;
2704 return Match_Simple_Operator (Op, Scan, Next, Greed);
2709 -- Looking at something like:
2710 -- 1: CURLYX {n,m} (->4)
2711 -- 2: code for complex thing (->3)
2716 Cc : aliased Current_Curly_Record;
2717 Min : Natural := Read_Natural (Program, Scan + 3);
2718 Max : Natural := Read_Natural (Program, Scan + 5);
2720 Has_Match : Boolean;
2723 Cc := (Paren_Floor => Last_Paren,
2731 Old_Cc => Current_Curly);
2732 Current_Curly := Cc'Unchecked_Access;
2734 Has_Match := Match (Next - 3);
2736 -- Start on the WHILEM
2738 Current_Curly := Cc.Old_Cc;
2743 return Match_Whilem (IP);
2746 raise Expression_Error; -- Invalid instruction
2750 end loop State_Machine;
2752 -- If we get here, there is no match.
2753 -- For successful matches when EOP is the terminating point.
2758 ---------------------------
2759 -- Match_Simple_Operator --
2760 ---------------------------
2762 function Match_Simple_Operator
2769 Next_Char : Character := ASCII.Nul;
2770 Next_Char_Known : Boolean := False;
2771 No : Integer; -- Can be negative
2773 Max : Natural := Natural'Last;
2774 Operand_Code : Pointer;
2777 Save : Natural := Input_Pos;
2780 -- Lookahead to avoid useless match attempts
2781 -- when we know what character comes next.
2783 if Program (Next) = EXACT then
2784 Next_Char := Program (String_Operand (Next));
2785 Next_Char_Known := True;
2788 -- Find the minimal and maximal values for the operator
2793 Operand_Code := Operand (Scan);
2797 Operand_Code := Operand (Scan);
2800 Min := Read_Natural (Program, Scan + 3);
2801 Max := Read_Natural (Program, Scan + 5);
2802 Operand_Code := Scan + 7;
2805 -- Non greedy operators
2808 -- Test the minimal repetitions
2811 and then Repeat (Operand_Code, Min) < Min
2818 -- Find the place where 'next' could work
2820 if Next_Char_Known then
2821 -- Last position to check
2823 Last_Pos := Input_Pos + Max;
2825 if Last_Pos > Data'Last
2826 or else Max = Natural'Last
2828 Last_Pos := Data'Last;
2831 -- Look for the first possible opportunity
2834 -- Find the next possible position
2836 while Input_Pos <= Last_Pos
2837 and then Data (Input_Pos) /= Next_Char
2839 Input_Pos := Input_Pos + 1;
2842 if Input_Pos > Last_Pos then
2846 -- Check that we still match if we stop
2847 -- at the position we just found.
2850 Num : constant Natural := Input_Pos - Old;
2855 if Repeat (Operand_Code, Num) < Num then
2860 -- Input_Pos now points to the new position
2862 if Match (Get_Next (Program, Scan)) then
2867 Input_Pos := Input_Pos + 1;
2870 -- We know what the next character is
2873 while Max >= Min loop
2875 -- If the next character matches
2877 if Match (Next) then
2881 Input_Pos := Save + Min;
2883 -- Could not or did not match -- move forward
2885 if Repeat (Operand_Code, 1) /= 0 then
2898 No := Repeat (Operand_Code, Max);
2900 -- ??? Perl has some special code here in case the
2901 -- next instruction is of type EOL, since $ and \Z
2902 -- can match before *and* after newline at the end.
2904 -- ??? Perl has some special code here in case (paren)
2907 -- Else, if we don't have any parenthesis
2909 while No >= Min loop
2910 if not Next_Char_Known
2911 or else (Input_Pos <= Data'Last
2912 and then Data (Input_Pos) = Next_Char)
2914 if Match (Next) then
2919 -- Could not or did not work, we back up
2922 Input_Pos := Save + No;
2926 end Match_Simple_Operator;
2932 -- This is really hard to understand, because after we match what we're
2933 -- trying to match, we must make sure the rest of the REx is going to
2934 -- match for sure, and to do that we have to go back UP the parse tree
2935 -- by recursing ever deeper. And if it fails, we have to reset our
2936 -- parent's current state that we can try again after backing off.
2938 function Match_Whilem (IP : Pointer) return Boolean is
2939 Cc : Current_Curly_Access := Current_Curly;
2940 N : Natural := Cc.Cur + 1;
2942 Lastloc : Natural := Cc.Lastloc;
2943 -- Detection of 0-len.
2946 -- If degenerate scan matches "", assume scan done.
2948 if Input_Pos = Cc.Lastloc
2949 and then N >= Cc.Min
2951 -- Temporarily restore the old context, and check that we
2952 -- match was comes after CURLYX.
2954 Current_Curly := Cc.Old_Cc;
2956 if Current_Curly /= null then
2957 Ln := Current_Curly.Cur;
2960 if Match (Cc.Next) then
2964 if Current_Curly /= null then
2965 Current_Curly.Cur := Ln;
2968 Current_Curly := Cc;
2972 -- First, just match a string of min scans.
2976 Cc.Lastloc := Input_Pos;
2978 if Match (Cc.Scan) then
2983 Cc.Lastloc := Lastloc;
2987 -- Prefer next over scan for minimal matching.
2989 if not Cc.Greedy then
2990 Current_Curly := Cc.Old_Cc;
2992 if Current_Curly /= null then
2993 Ln := Current_Curly.Cur;
2996 if Recurse_Match (Cc.Next, Cc.Paren_Floor) then
3000 if Current_Curly /= null then
3001 Current_Curly.Cur := Ln;
3004 Current_Curly := Cc;
3006 -- Maximum greed exceeded ?
3012 -- Try scanning more and see if it helps
3014 Cc.Lastloc := Input_Pos;
3016 if Recurse_Match (Cc.Scan, Cc.Paren_Floor) then
3021 Cc.Lastloc := Lastloc;
3025 -- Prefer scan over next for maximal matching
3027 if N < Cc.Max then -- more greed allowed ?
3029 Cc.Lastloc := Input_Pos;
3031 if Recurse_Match (Cc.Scan, Cc.Paren_Floor) then
3036 -- Failed deeper matches of scan, so see if this one works
3038 Current_Curly := Cc.Old_Cc;
3040 if Current_Curly /= null then
3041 Ln := Current_Curly.Cur;
3044 if Match (Cc.Next) then
3048 if Current_Curly /= null then
3049 Current_Curly.Cur := Ln;
3052 Current_Curly := Cc;
3054 Cc.Lastloc := Lastloc;
3064 Max : Natural := Natural'Last)
3067 Scan : Natural := Input_Pos;
3069 Op : constant Opcode := Opcode'Val (Character'Pos (Program (IP)));
3072 Is_First : Boolean := True;
3073 Bitmap : Character_Class;
3076 if Max = Natural'Last or else Scan + Max - 1 > Data'Last then
3079 Last := Scan + Max - 1;
3085 and then Data (Scan) /= ASCII.LF
3095 -- The string has only one character if Repeat was called
3097 C := Program (String_Operand (IP));
3099 and then C = Data (Scan)
3106 -- The string has only one character if Repeat was called
3108 C := Program (String_Operand (IP));
3110 and then To_Lower (C) = Data (Scan)
3117 Bitmap_Operand (Program, IP, Bitmap);
3122 and then Get_From_Class (Bitmap, Data (Scan))
3129 and then Is_Alnum (Data (Scan))
3136 and then not Is_Alnum (Data (Scan))
3143 and then Is_Space (Data (Scan))
3150 and then not Is_Space (Data (Scan))
3157 and then Is_Digit (Data (Scan))
3164 and then not Is_Digit (Data (Scan))
3170 raise Program_Error;
3173 Count := Scan - Input_Pos;
3182 function Try (Pos : in Positive) return Boolean is
3186 Matches_Full := (others => No_Match);
3188 if Match (Program_First + 1) then
3189 Matches_Full (0) := (Pos, Input_Pos - 1);
3196 -- Start of processing for Match
3199 -- Do we have the regexp Never_Match?
3201 if Self.Size = 0 then
3202 Matches (0) := No_Match;
3206 -- Check validity of program
3209 (Program (Program_First) = MAGIC,
3210 "Corrupted Pattern_Matcher
");
3212 -- If there is a "must appear
" string, look for it
3214 if Self.Must_Have_Length > 0 then
3216 First : constant Character := Program (Self.Must_Have);
3217 Must_First : constant Pointer := Self.Must_Have;
3218 Must_Last : constant Pointer :=
3219 Must_First + Pointer (Self.Must_Have_Length - 1);
3220 Next_Try : Natural := Index (Data'First, First);
3224 and then Data (Next_Try .. Next_Try + Self.Must_Have_Length - 1)
3225 = String (Program (Must_First .. Must_Last))
3227 Next_Try := Index (Next_Try + 1, First);
3230 if Next_Try = 0 then
3231 Matches_Full := (others => No_Match);
3232 return; -- Not present
3237 -- Mark beginning of line for ^
3239 BOL_Pos := Data'First;
3241 -- Simplest case first: an anchored match need be tried only once
3243 if Self.Anchored and then (Self.Flags and Multiple_Lines) = 0 then
3244 Matched := Try (Data'First);
3246 elsif Self.Anchored then
3248 Next_Try : Natural := Data'First;
3250 -- Test the first position in the buffer
3251 Matched := Try (Next_Try);
3253 -- Else only test after newlines
3256 while Next_Try <= Data'Last loop
3257 while Next_Try <= Data'Last
3258 and then Data (Next_Try) /= ASCII.LF
3260 Next_Try := Next_Try + 1;
3263 Next_Try := Next_Try + 1;
3265 if Next_Try <= Data'Last then
3266 Matched := Try (Next_Try);
3273 elsif Self.First /= ASCII.NUL then
3275 -- We know what char it must start with
3278 Next_Try : Natural := Index (Data'First, Self.First);
3281 while Next_Try /= 0 loop
3282 Matched := Try (Next_Try);
3284 Next_Try := Index (Next_Try + 1, Self.First);
3289 -- Messy cases: try all locations (including for the empty string)
3291 Matched := Try (Data'First);
3294 for S in Data'First + 1 .. Data'Last loop
3301 -- Matched has its value
3303 for J in Last_Paren + 1 .. Matches'Last loop
3304 Matches_Full (J) := No_Match;
3307 Matches := Matches_Full (Matches'Range);
3312 (Self : Pattern_Matcher;
3316 Matches : Match_Array (0 .. 0);
3319 Match (Self, Data, Matches);
3320 if Matches (0) = No_Match then
3321 return Data'First - 1;
3323 return Matches (0).First;
3328 (Expression : String;
3330 Matches : out Match_Array;
3331 Size : Program_Size := 0)
3333 PM : Pattern_Matcher (Size);
3334 Finalize_Size : Program_Size;
3338 Match (Compile (Expression), Data, Matches);
3340 Compile (PM, Expression, Finalize_Size);
3341 Match (PM, Data, Matches);
3346 (Expression : String;
3348 Size : Program_Size := 0)
3351 PM : Pattern_Matcher (Size);
3352 Final_Size : Program_Size; -- unused
3356 return Match (Compile (Expression), Data);
3358 Compile (PM, Expression, Final_Size);
3359 return Match (PM, Data);
3364 (Expression : String;
3366 Size : Program_Size := 0)
3369 Matches : Match_Array (0 .. 0);
3370 PM : Pattern_Matcher (Size);
3371 Final_Size : Program_Size; -- unused
3375 Match (Compile (Expression), Data, Matches);
3377 Compile (PM, Expression, Final_Size);
3378 Match (PM, Data, Matches);
3381 return Matches (0).First >= Data'First;
3388 function Operand (P : Pointer) return Pointer is
3397 procedure Optimize (Self : in out Pattern_Matcher) is
3398 Max_Length : Program_Size;
3399 This_Length : Program_Size;
3402 Program : Program_Data renames Self.Program;
3405 -- Start with safe defaults (no optimization):
3406 -- * No known first character of match
3407 -- * Does not necessarily start at beginning of line
3408 -- * No string known that has to appear in data
3410 Self.First := ASCII.NUL;
3411 Self.Anchored := False;
3412 Self.Must_Have := Program'Last + 1;
3413 Self.Must_Have_Length := 0;
3415 Scan := Program_First + 1; -- First instruction (can be anything)
3417 if Program (Scan) = EXACT then
3418 Self.First := Program (String_Operand (Scan));
3420 elsif Program (Scan) = BOL
3421 or else Program (Scan) = SBOL
3422 or else Program (Scan) = MBOL
3424 Self.Anchored := True;
3427 -- If there's something expensive in the regexp, find the
3428 -- longest literal string that must appear and make it the
3429 -- regmust. Resolve ties in favor of later strings, since
3430 -- the regstart check works with the beginning of the regexp.
3431 -- and avoiding duplication strengthens checking. Not a
3432 -- strong reason, but sufficient in the absence of others.
3434 if False then -- if Flags.SP_Start then ???
3437 while Scan /= 0 loop
3438 if Program (Scan) = EXACT or else Program (Scan) = EXACTF then
3439 This_Length := String_Length (Program, Scan);
3441 if This_Length >= Max_Length then
3442 Longest := String_Operand (Scan);
3443 Max_Length := This_Length;
3447 Scan := Get_Next (Program, Scan);
3450 Self.Must_Have := Longest;
3451 Self.Must_Have_Length := Natural (Max_Length) + 1;
3459 function Paren_Count (Regexp : Pattern_Matcher) return Match_Count is
3461 return Regexp.Paren_Count;
3468 function Quote (Str : String) return String is
3469 S : String (1 .. Str'Length * 2);
3470 Last : Natural := 0;
3473 for J in Str'Range loop
3475 when '^' | '$' | '|' | '*' | '+' | '?' | '{'
3476 | '}' | '[' | ']' | '(' | ')' | '\' =>
3478 S (Last + 1) := '\';
3479 S (Last + 2) := Str (J);
3483 S (Last + 1) := Str (J);
3488 return S (1 .. Last);
3495 function Read_Natural
3496 (Program : Program_Data;
3501 return Character'Pos (Program (IP)) +
3502 256 * Character'Pos (Program (IP + 1));
3509 procedure Reset_Class (Bitmap : in out Character_Class) is
3511 Bitmap := (others => 0);
3518 procedure Set_In_Class
3519 (Bitmap : in out Character_Class;
3522 Value : constant Class_Byte := Character'Pos (C);
3525 Bitmap (Value / 8) := Bitmap (Value / 8)
3526 or Bit_Conversion (Value mod 8);
3533 function String_Length
3534 (Program : Program_Data;
3539 pragma Assert (Program (P) = EXACT or else Program (P) = EXACTF);
3540 return Character'Pos (Program (P + 3));
3543 --------------------
3544 -- String_Operand --
3545 --------------------
3547 function String_Operand (P : Pointer) return Pointer is