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_Always
(Set_In_Class
);
249 pragma Inline_Always
(Get_From_Class
);
250 pragma Inline_Always
(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_Always
(Is_Mult
);
516 pragma Inline_Always
(Emit_Natural
);
517 pragma Inline_Always
(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
;
1568 Parse_Pos
:= Parse_Pos
- 1; -- Look at current character
1570 if (Flags
and Case_Insensitive
) /= 0 then
1571 IP
:= Emit_Node
(EXACTF
);
1573 IP
:= Emit_Node
(EXACT
);
1576 Length_Ptr
:= Emit_Ptr
;
1577 Emit_Ptr
:= String_Operand
(IP
);
1582 C
:= Expression
(Parse_Pos
); -- Get current character
1585 when '.' |
'[' |
'(' |
')' |
'|' | ASCII
.LF |
'$' |
'^' =>
1587 if Start_Pos
= 0 then
1588 Emit
(C
); -- First character is always emitted
1590 exit Parse_Loop
; -- Else we are done
1593 when '?' |
'+' |
'*' |
'{' =>
1595 if Start_Pos
= 0 then
1596 Emit
(C
); -- First character is always emitted
1598 -- Are we looking at an operator, or is this
1599 -- simply a normal character ?
1600 elsif not Is_Mult
(Parse_Pos
) then
1603 -- We've got something like "abc?d". Mark this as a
1604 -- special case. What we want to emit is a first
1605 -- constant string for "ab", then one for "c" that will
1606 -- ultimately be transformed with a CURLY operator, A
1607 -- special case has to be handled for "a?", since there
1608 -- is no initial string to emit.
1609 Start_Pos
:= Natural'Last;
1614 if Parse_Pos
= Parse_End
then
1615 Fail
("Trailing \");
1617 case Expression (Parse_Pos + 1) is
1618 when 'b' | 'B' | 's' | 'S' | 'd' | 'D'
1619 | 'w' | 'W' | '0' .. '9' | 'G' | 'A'
1621 when 'n' => Emit (ASCII.LF);
1622 when 't' => Emit (ASCII.HT);
1623 when 'r' => Emit (ASCII.CR);
1624 when 'f' => Emit (ASCII.FF);
1625 when 'e' => Emit (ASCII.ESC);
1626 when 'a' => Emit (ASCII.BEL);
1627 when others => Emit (Expression (Parse_Pos + 1));
1629 Parse_Pos := Parse_Pos + 1;
1632 when others => Case_Emit (C);
1635 exit Parse_Loop when Emit_Ptr - Length_Ptr = 254;
1637 Start_Pos := Parse_Pos;
1638 Parse_Pos := Parse_Pos + 1;
1640 exit Parse_Loop when Parse_Pos > Parse_End;
1641 end loop Parse_Loop;
1643 -- Is the string followed by a '*+?{' operator ? If yes, and if there
1644 -- is an initial string to emit, do it now.
1646 if Start_Pos = Natural'Last
1647 and then Emit_Ptr >= Length_Ptr + 3
1649 Emit_Ptr := Emit_Ptr - 1;
1650 Parse_Pos := Parse_Pos - 1;
1654 Program (Length_Ptr) := Character'Val (Emit_Ptr - Length_Ptr - 2);
1657 Expr_Flags.Has_Width := True;
1659 -- Slight optimization when there is a single character
1661 if Emit_Ptr = Length_Ptr + 2 then
1662 Expr_Flags.Simple := True;
1670 -- Note that the branching code sequences used for '?' and the
1671 -- general cases of '*' and + are somewhat optimized: they use
1672 -- the same NOTHING node as both the endmarker for their branch
1673 -- list and the body of the last branch. It might seem that
1674 -- this node could be dispensed with entirely, but the endmarker
1675 -- role is not redundant.
1677 procedure Parse_Piece
1678 (Expr_Flags : in out Expression_Flags;
1682 New_Flags : Expression_Flags;
1683 Greedy : Boolean := True;
1686 Parse_Atom (New_Flags, IP);
1692 if Parse_Pos > Parse_End
1693 or else not Is_Mult (Parse_Pos)
1695 Expr_Flags := New_Flags;
1699 Op := Expression (Parse_Pos);
1702 Expr_Flags := (SP_Start => True, others => False);
1704 Expr_Flags := (Has_Width => True, others => False);
1707 -- Detect non greedy operators in the easy cases
1710 and then Parse_Pos + 1 <= Parse_End
1711 and then Expression (Parse_Pos + 1) = '?'
1714 Parse_Pos := Parse_Pos + 1;
1717 -- Generate the byte code
1722 if New_Flags.Simple then
1723 Insert_Operator (STAR, IP, Greedy);
1725 Link_Tail (IP, Emit_Node (WHILEM));
1726 Insert_Curly_Operator
1727 (CURLYX, 0, Max_Curly_Repeat, IP, Greedy);
1728 Link_Tail (IP, Emit_Node (NOTHING));
1733 if New_Flags.Simple then
1734 Insert_Operator (PLUS, IP, Greedy);
1736 Link_Tail (IP, Emit_Node (WHILEM));
1737 Insert_Curly_Operator
1738 (CURLYX, 1, Max_Curly_Repeat, IP, Greedy);
1739 Link_Tail (IP, Emit_Node (NOTHING));
1743 if New_Flags.Simple then
1744 Insert_Curly_Operator (CURLY, 0, 1, IP, Greedy);
1746 Link_Tail (IP, Emit_Node (WHILEM));
1747 Insert_Curly_Operator (CURLYX, 0, 1, IP, Greedy);
1748 Link_Tail (IP, Emit_Node (NOTHING));
1756 Get_Curly_Arguments (Parse_Pos, Min, Max, Greedy);
1758 if New_Flags.Simple then
1759 Insert_Curly_Operator (CURLY, Min, Max, IP, Greedy);
1761 Link_Tail (IP, Emit_Node (WHILEM));
1762 Insert_Curly_Operator (CURLYX, Min, Max, IP, Greedy);
1763 Link_Tail (IP, Emit_Node (NOTHING));
1771 Parse_Pos := Parse_Pos + 1;
1773 if Parse_Pos <= Parse_End
1774 and then Is_Mult (Parse_Pos)
1776 Fail ("nested
*+{");
1780 ---------------------------------
1781 -- Parse_Posix_Character_Class --
1782 ---------------------------------
1784 function Parse_Posix_Character_Class return Std_Class is
1785 Invert : Boolean := False;
1786 Class : Std_Class := ANYOF_NONE;
1787 E : String renames Expression;
1790 if Parse_Pos <= Parse_End
1791 and then Expression (Parse_Pos) = ':'
1793 Parse_Pos := Parse_Pos + 1;
1795 -- Do we have something like: [[:^alpha:]]
1797 if Parse_Pos <= Parse_End
1798 and then Expression (Parse_Pos) = '^'
1801 Parse_Pos := Parse_Pos + 1;
1804 -- All classes have 6 characters at least
1805 -- ??? magid constant 6 should have a name!
1807 if Parse_Pos + 6 <= Parse_End then
1809 case Expression (Parse_Pos) is
1811 if E (Parse_Pos .. Parse_Pos + 4) = "alnum
:]" then
1813 Class := ANYOF_NALNUMC;
1815 Class := ANYOF_ALNUMC;
1818 elsif E (Parse_Pos .. Parse_Pos + 6) = "alpha
:]" then
1820 Class := ANYOF_NALPHA;
1822 Class := ANYOF_ALPHA;
1825 elsif E (Parse_Pos .. Parse_Pos + 6) = "ascii
:]" then
1827 Class := ANYOF_NASCII;
1829 Class := ANYOF_ASCII;
1835 if E (Parse_Pos .. Parse_Pos + 6) = "cntrl
:]" then
1837 Class := ANYOF_NCNTRL;
1839 Class := ANYOF_CNTRL;
1845 if E (Parse_Pos .. Parse_Pos + 6) = "digit
:]" then
1847 Class := ANYOF_NDIGIT;
1849 Class := ANYOF_DIGIT;
1855 if E (Parse_Pos .. Parse_Pos + 6) = "graph
:]" then
1857 Class := ANYOF_NGRAPH;
1859 Class := ANYOF_GRAPH;
1865 if E (Parse_Pos .. Parse_Pos + 6) = "lower
:]" then
1867 Class := ANYOF_NLOWER;
1869 Class := ANYOF_LOWER;
1875 if E (Parse_Pos .. Parse_Pos + 6) = "print
:]" then
1877 Class := ANYOF_NPRINT;
1879 Class := ANYOF_PRINT;
1882 elsif E (Parse_Pos .. Parse_Pos + 6) = "punct
:]" then
1884 Class := ANYOF_NPUNCT;
1886 Class := ANYOF_PUNCT;
1892 if E (Parse_Pos .. Parse_Pos + 6) = "space
:]" then
1894 Class := ANYOF_NSPACE;
1896 Class := ANYOF_SPACE;
1902 if E (Parse_Pos .. Parse_Pos + 6) = "upper
:]" then
1904 Class := ANYOF_NUPPER;
1906 Class := ANYOF_UPPER;
1912 if E (Parse_Pos .. Parse_Pos + 5) = "word
:]" then
1914 Class := ANYOF_NALNUM;
1916 Class := ANYOF_ALNUM;
1919 Parse_Pos := Parse_Pos - 1;
1924 if Parse_Pos + 7 <= Parse_End
1925 and then E (Parse_Pos .. Parse_Pos + 7) = "xdigit
:]"
1928 Class := ANYOF_NXDIGIT;
1930 Class := ANYOF_XDIGIT;
1933 Parse_Pos := Parse_Pos + 1;
1937 Class := ANYOF_NONE;
1941 if Class /= ANYOF_NONE then
1942 Parse_Pos := Parse_Pos + 7;
1946 Fail ("Invalid
character class
");
1954 end Parse_Posix_Character_Class;
1956 Expr_Flags : Expression_Flags;
1959 -- Start of processing for Compile
1963 Parse (False, Expr_Flags, Result);
1966 Fail ("Couldn
't compile expression
");
1969 Final_Code_Size := Emit_Ptr - 1;
1971 -- Do we want to actually compile the expression, or simply get the
1982 (Expression : String;
1983 Flags : Regexp_Flags := No_Flags)
1984 return Pattern_Matcher
1986 Size : Program_Size;
1987 Dummy : Pattern_Matcher (0);
1990 Compile (Dummy, Expression, Size, Flags);
1993 Result : Pattern_Matcher (Size);
1995 Compile (Result, Expression, Size, Flags);
2001 (Matcher : out Pattern_Matcher;
2002 Expression : String;
2003 Flags : Regexp_Flags := No_Flags)
2005 Size : Program_Size;
2008 Compile (Matcher, Expression, Size, Flags);
2015 procedure Dump (Self : Pattern_Matcher) is
2017 -- Index : Pointer := Program_First + 1;
2018 -- What is the above line for ???
2021 Program : Program_Data renames Self.Program;
2023 procedure Dump_Until
2026 Indent : Natural := 0);
2027 -- Dump the program until the node Till (not included) is met.
2028 -- Every line is indented with Index spaces at the beginning
2029 -- Dumps till the end if Till is 0.
2035 procedure Dump_Until
2038 Indent : Natural := 0)
2041 Index : Pointer := Start;
2042 Local_Indent : Natural := Indent;
2046 while Index < Till loop
2048 Op := Opcode'Val (Character'Pos ((Self.Program (Index))));
2051 Local_Indent := Local_Indent - 3;
2055 Point : String := Pointer'Image (Index);
2058 for J in 1 .. 6 - Point'Length loop
2064 & (1 .. Local_Indent => ' ')
2065 & Opcode'Image (Op));
2068 -- Print the parenthesis number
2070 if Op = OPEN or else Op = CLOSE or else Op = REFF then
2071 Put (Natural'Image (Character'Pos (Program (Index + 3))));
2074 Next := Index + Get_Next_Offset (Program, Index);
2076 if Next = Index then
2077 Put (" (next
at 0)");
2079 Put (" (next
at " & Pointer'Image (Next) & ")");
2084 -- Character class operand
2088 Bitmap : Character_Class;
2089 Last : Character := ASCII.Nul;
2090 Current : Natural := 0;
2092 Current_Char : Character;
2095 Bitmap_Operand (Program, Index, Bitmap);
2098 while Current <= 255 loop
2099 Current_Char := Character'Val (Current);
2101 -- First item in a range
2103 if Get_From_Class (Bitmap, Current_Char) then
2104 Last := Current_Char;
2106 -- Search for the last item in the range
2109 Current := Current + 1;
2110 exit when Current > 255;
2111 Current_Char := Character'Val (Current);
2113 not Get_From_Class (Bitmap, Current_Char);
2123 if Character'Succ (Last) /= Current_Char then
2124 Put ("-" & Character'Pred (Current_Char));
2128 Current := Current + 1;
2133 Index := Index + 3 + Bitmap'Length;
2138 when EXACT | EXACTF =>
2139 Length := String_Length (Program, Index);
2140 Put (" operand
(length
:" & Program_Size'Image (Length + 1)
2142 & String (Program (String_Operand (Index)
2143 .. String_Operand (Index)
2145 Index := String_Operand (Index) + Length + 1;
2152 Dump_Until (Index + 3, Next, Local_Indent + 3);
2158 -- Only one instruction
2160 Dump_Until (Index + 3, Index + 4, Local_Indent + 3);
2163 when CURLY | CURLYX =>
2165 & Natural'Image (Read_Natural (Program, Index + 3))
2167 & Natural'Image (Read_Natural (Program, Index + 5))
2170 Dump_Until (Index + 7, Next, Local_Indent + 3);
2176 Local_Indent := Local_Indent + 3;
2178 when CLOSE | REFF =>
2196 -- Start of processing for Dump
2199 pragma Assert (Self.Program (Program_First) = MAGIC,
2200 "Corrupted Pattern_Matcher
");
2202 Put_Line ("Must start
with (Self
.First
) = "
2203 & Character'Image (Self.First));
2205 if (Self.Flags and Case_Insensitive) /= 0 then
2206 Put_Line (" Case_Insensitive mode
");
2209 if (Self.Flags and Single_Line) /= 0 then
2210 Put_Line (" Single_Line mode
");
2213 if (Self.Flags and Multiple_Lines) /= 0 then
2214 Put_Line (" Multiple_Lines mode
");
2217 Put_Line (" 1 : MAGIC
");
2218 Dump_Until (Program_First + 1, Self.Program'Last + 1);
2221 --------------------
2222 -- Get_From_Class --
2223 --------------------
2225 function Get_From_Class
2226 (Bitmap : Character_Class;
2230 Value : constant Class_Byte := Character'Pos (C);
2233 return (Bitmap (Value / 8)
2234 and Bit_Conversion (Value mod 8)) /= 0;
2241 function Get_Next (Program : Program_Data; IP : Pointer) return Pointer is
2242 Offset : constant Pointer := Get_Next_Offset (Program, IP);
2252 ---------------------
2253 -- Get_Next_Offset --
2254 ---------------------
2256 function Get_Next_Offset
2257 (Program : Program_Data;
2262 return Pointer (Read_Natural (Program, IP + 1));
2263 end Get_Next_Offset;
2269 function Is_Alnum (C : Character) return Boolean is
2271 return Is_Alphanumeric (C) or else C = '_';
2278 function Is_Printable (C : Character) return Boolean is
2279 Value : constant Natural := Character'Pos (C);
2282 return (Value > 32 and then Value < 127)
2283 or else Is_Space (C);
2290 function Is_Space (C : Character) return Boolean is
2293 or else C = ASCII.HT
2294 or else C = ASCII.CR
2295 or else C = ASCII.LF
2296 or else C = ASCII.VT
2297 or else C = ASCII.FF;
2305 (Self : Pattern_Matcher;
2307 Matches : out Match_Array)
2309 Program : Program_Data renames Self.Program; -- Shorter notation
2311 -- Global work variables
2313 Input_Pos : Natural; -- String-input pointer
2314 BOL_Pos : Natural; -- Beginning of input, for ^ check
2315 Matched : Boolean := False; -- Until proven True
2317 Matches_Full : Match_Array (0 .. Natural'Max (Self.Paren_Count,
2319 -- Stores the value of all the parenthesis pairs.
2320 -- We do not use directly Matches, so that we can also use back
2321 -- references (REFF) even if Matches is too small.
2323 type Natural_Array is array (Match_Count range <>) of Natural;
2324 Matches_Tmp : Natural_Array (Matches_Full'Range);
2325 -- Save the opening position of parenthesis.
2327 Last_Paren : Natural := 0;
2328 -- Last parenthesis seen
2330 Greedy : Boolean := True;
2331 -- True if the next operator should be greedy
2333 type Current_Curly_Record;
2334 type Current_Curly_Access is access all Current_Curly_Record;
2335 type Current_Curly_Record is record
2336 Paren_Floor : Natural; -- How far back to strip parenthesis data
2337 Cur : Integer; -- How many instances of scan we've matched
2338 Min : Natural; -- Minimal number of scans to match
2339 Max : Natural; -- Maximal number of scans to match
2340 Greedy : Boolean; -- Whether to work our way up or down
2341 Scan : Pointer; -- The thing to match
2342 Next : Pointer; -- What has to match after it
2343 Lastloc : Natural; -- Where we started matching this scan
2344 Old_Cc : Current_Curly_Access; -- Before we started this one
2346 -- Data used to handle the curly operator and the plus and star
2347 -- operators for complex expressions.
2349 Current_Curly : Current_Curly_Access := null;
2350 -- The curly currently being processed.
2352 -----------------------
2353 -- Local Subprograms --
2354 -----------------------
2356 function Index (Start : Positive; C : Character) return Natural;
2357 -- Find character C in Data starting at Start and return position
2361 Max : Natural := Natural'Last)
2363 -- Repeatedly match something simple, report how many
2364 -- It only matches on things of length 1.
2365 -- Starting from Input_Pos, it matches at most Max CURLY.
2367 function Try (Pos : in Positive) return Boolean;
2368 -- Try to match at specific point
2370 function Match (IP : Pointer) return Boolean;
2371 -- This is the main matching routine. Conceptually the strategy
2372 -- is simple: check to see whether the current node matches,
2373 -- call self recursively to see whether the rest matches,
2374 -- and then act accordingly.
2376 -- In practice Match makes some effort to avoid recursion, in
2377 -- particular by going through "ordinary
" nodes (that don't
2378 -- need to know whether the rest of the match failed) by
2379 -- using a loop instead of recursion.
2381 function Match_Whilem (IP : Pointer) return Boolean;
2382 -- Return True if a WHILEM matches
2384 function Recurse_Match (IP : Pointer; From : Natural) return Boolean;
2385 pragma Inline (Recurse_Match);
2386 -- Calls Match recursively. It saves and restores the parenthesis
2387 -- status and location in the input stream correctly, so that
2388 -- backtracking is possible
2390 function Match_Simple_Operator
2396 -- Return True it the simple operator (possibly non-greedy) matches
2398 pragma Inline_Always (Index);
2399 pragma Inline_Always (Repeat);
2401 -- These are two complex functions, but used only once.
2402 pragma Inline_Always (Match_Whilem);
2403 pragma Inline_Always (Match_Simple_Operator);
2415 for J in Start .. Data'Last loop
2416 if Data (J) = C then
2428 function Recurse_Match (IP : Pointer; From : Natural) return Boolean is
2429 L : constant Natural := Last_Paren;
2430 Tmp_F : constant Match_Array :=
2431 Matches_Full (From + 1 .. Matches_Full'Last);
2432 Start : constant Natural_Array :=
2433 Matches_Tmp (From + 1 .. Matches_Tmp'Last);
2434 Input : constant Natural := Input_Pos;
2440 Matches_Full (Tmp_F'Range) := Tmp_F;
2441 Matches_Tmp (Start'Range) := Start;
2450 function Match (IP : Pointer) return Boolean is
2451 Scan : Pointer := IP;
2458 pragma Assert (Scan /= 0);
2460 -- Determine current opcode and count its usage in debug mode
2462 Op := Opcode'Val (Character'Pos (Program (Scan)));
2464 -- Calculate offset of next instruction.
2465 -- Second character is most significant in Program_Data.
2467 Next := Get_Next (Program, Scan);
2471 return True; -- Success !
2474 if Program (Next) /= BRANCH then
2475 Next := Operand (Scan); -- No choice, avoid recursion
2479 if Recurse_Match (Operand (Scan), 0) then
2483 Scan := Get_Next (Program, Scan);
2484 exit when Scan = 0 or Program (Scan) /= BRANCH;
2494 exit State_Machine when
2495 Input_Pos /= BOL_Pos
2496 and then ((Self.Flags and Multiple_Lines) = 0
2497 or else Data (Input_Pos - 1) /= ASCII.LF);
2500 exit State_Machine when
2501 Input_Pos /= BOL_Pos
2502 and then Data (Input_Pos - 1) /= ASCII.LF;
2505 exit State_Machine when Input_Pos /= BOL_Pos;
2508 exit State_Machine when
2509 Input_Pos <= Data'Last
2510 and then ((Self.Flags and Multiple_Lines) = 0
2511 or else Data (Input_Pos) /= ASCII.LF);
2514 exit State_Machine when
2515 Input_Pos <= Data'Last
2516 and then Data (Input_Pos) /= ASCII.LF;
2519 exit State_Machine when Input_Pos <= Data'Last;
2521 when BOUND | NBOUND =>
2523 -- Was last char in word ?
2526 N : Boolean := False;
2527 Ln : Boolean := False;
2530 if Input_Pos /= Data'First then
2531 N := Is_Alnum (Data (Input_Pos - 1));
2534 if Input_Pos > Data'Last then
2537 Ln := Is_Alnum (Data (Input_Pos));
2552 exit State_Machine when
2553 Input_Pos > Data'Last
2554 or else not Is_Space (Data (Input_Pos));
2555 Input_Pos := Input_Pos + 1;
2558 exit State_Machine when
2559 Input_Pos > Data'Last
2560 or else Is_Space (Data (Input_Pos));
2561 Input_Pos := Input_Pos + 1;
2564 exit State_Machine when
2565 Input_Pos > Data'Last
2566 or else not Is_Digit (Data (Input_Pos));
2567 Input_Pos := Input_Pos + 1;
2570 exit State_Machine when
2571 Input_Pos > Data'Last
2572 or else Is_Digit (Data (Input_Pos));
2573 Input_Pos := Input_Pos + 1;
2576 exit State_Machine when
2577 Input_Pos > Data'Last
2578 or else not Is_Alnum (Data (Input_Pos));
2579 Input_Pos := Input_Pos + 1;
2582 exit State_Machine when
2583 Input_Pos > Data'Last
2584 or else Is_Alnum (Data (Input_Pos));
2585 Input_Pos := Input_Pos + 1;
2588 exit State_Machine when Input_Pos > Data'Last
2589 or else Data (Input_Pos) = ASCII.LF;
2590 Input_Pos := Input_Pos + 1;
2593 exit State_Machine when Input_Pos > Data'Last;
2594 Input_Pos := Input_Pos + 1;
2598 Opnd : Pointer := String_Operand (Scan);
2599 Current : Positive := Input_Pos;
2600 Last : constant Pointer :=
2601 Opnd + String_Length (Program, Scan);
2604 while Opnd <= Last loop
2605 exit State_Machine when Current > Data'Last
2606 or else Program (Opnd) /= Data (Current);
2607 Current := Current + 1;
2611 Input_Pos := Current;
2616 Opnd : Pointer := String_Operand (Scan);
2617 Current : Positive := Input_Pos;
2618 Last : constant Pointer :=
2619 Opnd + String_Length (Program, Scan);
2622 while Opnd <= Last loop
2623 exit State_Machine when Current > Data'Last
2624 or else Program (Opnd) /= To_Lower (Data (Current));
2625 Current := Current + 1;
2629 Input_Pos := Current;
2634 Bitmap : Character_Class;
2637 Bitmap_Operand (Program, Scan, Bitmap);
2638 exit State_Machine when
2639 Input_Pos > Data'Last
2640 or else not Get_From_Class (Bitmap, Data (Input_Pos));
2641 Input_Pos := Input_Pos + 1;
2646 No : constant Natural :=
2647 Character'Pos (Program (Operand (Scan)));
2649 Matches_Tmp (No) := Input_Pos;
2654 No : constant Natural :=
2655 Character'Pos (Program (Operand (Scan)));
2657 Matches_Full (No) := (Matches_Tmp (No), Input_Pos - 1);
2658 if Last_Paren < No then
2665 No : constant Natural :=
2666 Character'Pos (Program (Operand (Scan)));
2670 -- If we haven't seen that parenthesis yet
2672 if Last_Paren < No then
2676 Data_Pos := Matches_Full (No).First;
2677 while Data_Pos <= Matches_Full (No).Last loop
2678 if Input_Pos > Data'Last
2679 or else Data (Input_Pos) /= Data (Data_Pos)
2684 Input_Pos := Input_Pos + 1;
2685 Data_Pos := Data_Pos + 1;
2692 when STAR | PLUS | CURLY =>
2694 Greed : constant Boolean := Greedy;
2697 return Match_Simple_Operator (Op, Scan, Next, Greed);
2702 -- Looking at something like:
2703 -- 1: CURLYX {n,m} (->4)
2704 -- 2: code for complex thing (->3)
2709 Cc : aliased Current_Curly_Record;
2710 Min : Natural := Read_Natural (Program, Scan + 3);
2711 Max : Natural := Read_Natural (Program, Scan + 5);
2713 Has_Match : Boolean;
2716 Cc := (Paren_Floor => Last_Paren,
2724 Old_Cc => Current_Curly);
2725 Current_Curly := Cc'Unchecked_Access;
2727 Has_Match := Match (Next - 3);
2729 -- Start on the WHILEM
2731 Current_Curly := Cc.Old_Cc;
2736 return Match_Whilem (IP);
2739 raise Expression_Error; -- Invalid instruction
2743 end loop State_Machine;
2745 -- If we get here, there is no match.
2746 -- For successful matches when EOP is the terminating point.
2751 ---------------------------
2752 -- Match_Simple_Operator --
2753 ---------------------------
2755 function Match_Simple_Operator
2762 Next_Char : Character := ASCII.Nul;
2763 Next_Char_Known : Boolean := False;
2764 No : Integer; -- Can be negative
2766 Max : Natural := Natural'Last;
2767 Operand_Code : Pointer;
2770 Save : Natural := Input_Pos;
2773 -- Lookahead to avoid useless match attempts
2774 -- when we know what character comes next.
2776 if Program (Next) = EXACT then
2777 Next_Char := Program (String_Operand (Next));
2778 Next_Char_Known := True;
2781 -- Find the minimal and maximal values for the operator
2786 Operand_Code := Operand (Scan);
2790 Operand_Code := Operand (Scan);
2793 Min := Read_Natural (Program, Scan + 3);
2794 Max := Read_Natural (Program, Scan + 5);
2795 Operand_Code := Scan + 7;
2798 -- Non greedy operators
2801 -- Test the minimal repetitions
2804 and then Repeat (Operand_Code, Min) < Min
2811 -- Find the place where 'next' could work
2813 if Next_Char_Known then
2814 -- Last position to check
2816 Last_Pos := Input_Pos + Max;
2818 if Last_Pos > Data'Last
2819 or else Max = Natural'Last
2821 Last_Pos := Data'Last;
2824 -- Look for the first possible opportunity
2827 -- Find the next possible position
2829 while Input_Pos <= Last_Pos
2830 and then Data (Input_Pos) /= Next_Char
2832 Input_Pos := Input_Pos + 1;
2835 if Input_Pos > Last_Pos then
2839 -- Check that we still match if we stop
2840 -- at the position we just found.
2843 Num : constant Natural := Input_Pos - Old;
2848 if Repeat (Operand_Code, Num) < Num then
2853 -- Input_Pos now points to the new position
2855 if Match (Get_Next (Program, Scan)) then
2860 Input_Pos := Input_Pos + 1;
2863 -- We know what the next character is
2866 while Max >= Min loop
2868 -- If the next character matches
2870 if Match (Next) then
2874 Input_Pos := Save + Min;
2876 -- Could not or did not match -- move forward
2878 if Repeat (Operand_Code, 1) /= 0 then
2891 No := Repeat (Operand_Code, Max);
2893 -- ??? Perl has some special code here in case the
2894 -- next instruction is of type EOL, since $ and \Z
2895 -- can match before *and* after newline at the end.
2897 -- ??? Perl has some special code here in case (paren)
2900 -- Else, if we don't have any parenthesis
2902 while No >= Min loop
2903 if not Next_Char_Known
2904 or else (Input_Pos <= Data'Last
2905 and then Data (Input_Pos) = Next_Char)
2907 if Match (Next) then
2912 -- Could not or did not work, we back up
2915 Input_Pos := Save + No;
2919 end Match_Simple_Operator;
2925 -- This is really hard to understand, because after we match what we're
2926 -- trying to match, we must make sure the rest of the REx is going to
2927 -- match for sure, and to do that we have to go back UP the parse tree
2928 -- by recursing ever deeper. And if it fails, we have to reset our
2929 -- parent's current state that we can try again after backing off.
2931 function Match_Whilem (IP : Pointer) return Boolean is
2932 Cc : Current_Curly_Access := Current_Curly;
2933 N : Natural := Cc.Cur + 1;
2935 Lastloc : Natural := Cc.Lastloc;
2936 -- Detection of 0-len.
2939 -- If degenerate scan matches "", assume scan done.
2941 if Input_Pos = Cc.Lastloc
2942 and then N >= Cc.Min
2944 -- Temporarily restore the old context, and check that we
2945 -- match was comes after CURLYX.
2947 Current_Curly := Cc.Old_Cc;
2949 if Current_Curly /= null then
2950 Ln := Current_Curly.Cur;
2953 if Match (Cc.Next) then
2957 if Current_Curly /= null then
2958 Current_Curly.Cur := Ln;
2961 Current_Curly := Cc;
2965 -- First, just match a string of min scans.
2969 Cc.Lastloc := Input_Pos;
2971 if Match (Cc.Scan) then
2976 Cc.Lastloc := Lastloc;
2980 -- Prefer next over scan for minimal matching.
2982 if not Cc.Greedy then
2983 Current_Curly := Cc.Old_Cc;
2985 if Current_Curly /= null then
2986 Ln := Current_Curly.Cur;
2989 if Recurse_Match (Cc.Next, Cc.Paren_Floor) then
2993 if Current_Curly /= null then
2994 Current_Curly.Cur := Ln;
2997 Current_Curly := Cc;
2999 -- Maximum greed exceeded ?
3005 -- Try scanning more and see if it helps
3007 Cc.Lastloc := Input_Pos;
3009 if Recurse_Match (Cc.Scan, Cc.Paren_Floor) then
3014 Cc.Lastloc := Lastloc;
3018 -- Prefer scan over next for maximal matching
3020 if N < Cc.Max then -- more greed allowed ?
3022 Cc.Lastloc := Input_Pos;
3024 if Recurse_Match (Cc.Scan, Cc.Paren_Floor) then
3029 -- Failed deeper matches of scan, so see if this one works
3031 Current_Curly := Cc.Old_Cc;
3033 if Current_Curly /= null then
3034 Ln := Current_Curly.Cur;
3037 if Match (Cc.Next) then
3041 if Current_Curly /= null then
3042 Current_Curly.Cur := Ln;
3045 Current_Curly := Cc;
3047 Cc.Lastloc := Lastloc;
3057 Max : Natural := Natural'Last)
3060 Scan : Natural := Input_Pos;
3062 Op : constant Opcode := Opcode'Val (Character'Pos (Program (IP)));
3065 Is_First : Boolean := True;
3066 Bitmap : Character_Class;
3069 if Max = Natural'Last or else Scan + Max - 1 > Data'Last then
3072 Last := Scan + Max - 1;
3078 and then Data (Scan) /= ASCII.LF
3088 -- The string has only one character if Repeat was called
3090 C := Program (String_Operand (IP));
3092 and then C = Data (Scan)
3099 -- The string has only one character if Repeat was called
3101 C := Program (String_Operand (IP));
3103 and then To_Lower (C) = Data (Scan)
3110 Bitmap_Operand (Program, IP, Bitmap);
3115 and then Get_From_Class (Bitmap, Data (Scan))
3122 and then Is_Alnum (Data (Scan))
3129 and then not Is_Alnum (Data (Scan))
3136 and then Is_Space (Data (Scan))
3143 and then not Is_Space (Data (Scan))
3150 and then Is_Digit (Data (Scan))
3157 and then not Is_Digit (Data (Scan))
3163 raise Program_Error;
3166 Count := Scan - Input_Pos;
3175 function Try (Pos : in Positive) return Boolean is
3179 Matches_Full := (others => No_Match);
3181 if Match (Program_First + 1) then
3182 Matches_Full (0) := (Pos, Input_Pos - 1);
3189 -- Start of processing for Match
3192 -- Do we have the regexp Never_Match?
3194 if Self.Size = 0 then
3195 Matches (0) := No_Match;
3199 -- Check validity of program
3202 (Program (Program_First) = MAGIC,
3203 "Corrupted Pattern_Matcher
");
3205 -- If there is a "must appear
" string, look for it
3207 if Self.Must_Have_Length > 0 then
3209 First : constant Character := Program (Self.Must_Have);
3210 Must_First : constant Pointer := Self.Must_Have;
3211 Must_Last : constant Pointer :=
3212 Must_First + Pointer (Self.Must_Have_Length - 1);
3213 Next_Try : Natural := Index (Data'First, First);
3217 and then Data (Next_Try .. Next_Try + Self.Must_Have_Length - 1)
3218 = String (Program (Must_First .. Must_Last))
3220 Next_Try := Index (Next_Try + 1, First);
3223 if Next_Try = 0 then
3224 Matches_Full := (others => No_Match);
3225 return; -- Not present
3230 -- Mark beginning of line for ^
3232 BOL_Pos := Data'First;
3234 -- Simplest case first: an anchored match need be tried only once
3236 if Self.Anchored and then (Self.Flags and Multiple_Lines) = 0 then
3237 Matched := Try (Data'First);
3239 elsif Self.Anchored then
3241 Next_Try : Natural := Data'First;
3243 -- Test the first position in the buffer
3244 Matched := Try (Next_Try);
3246 -- Else only test after newlines
3249 while Next_Try <= Data'Last loop
3250 while Next_Try <= Data'Last
3251 and then Data (Next_Try) /= ASCII.LF
3253 Next_Try := Next_Try + 1;
3256 Next_Try := Next_Try + 1;
3258 if Next_Try <= Data'Last then
3259 Matched := Try (Next_Try);
3266 elsif Self.First /= ASCII.NUL then
3268 -- We know what char it must start with
3271 Next_Try : Natural := Index (Data'First, Self.First);
3274 while Next_Try /= 0 loop
3275 Matched := Try (Next_Try);
3277 Next_Try := Index (Next_Try + 1, Self.First);
3282 -- Messy cases: try all locations (including for the empty string)
3284 Matched := Try (Data'First);
3287 for S in Data'First + 1 .. Data'Last loop
3294 -- Matched has its value
3296 for J in Last_Paren + 1 .. Matches'Last loop
3297 Matches_Full (J) := No_Match;
3300 Matches := Matches_Full (Matches'Range);
3305 (Self : Pattern_Matcher;
3309 Matches : Match_Array (0 .. 0);
3312 Match (Self, Data, Matches);
3313 if Matches (0) = No_Match then
3314 return Data'First - 1;
3316 return Matches (0).First;
3321 (Expression : String;
3323 Matches : out Match_Array;
3324 Size : Program_Size := 0)
3326 PM : Pattern_Matcher (Size);
3327 Finalize_Size : Program_Size;
3331 Match (Compile (Expression), Data, Matches);
3333 Compile (PM, Expression, Finalize_Size);
3334 Match (PM, Data, Matches);
3339 (Expression : String;
3341 Size : Program_Size := 0)
3344 PM : Pattern_Matcher (Size);
3345 Final_Size : Program_Size; -- unused
3349 return Match (Compile (Expression), Data);
3351 Compile (PM, Expression, Final_Size);
3352 return Match (PM, Data);
3357 (Expression : String;
3359 Size : Program_Size := 0)
3362 Matches : Match_Array (0 .. 0);
3363 PM : Pattern_Matcher (Size);
3364 Final_Size : Program_Size; -- unused
3368 Match (Compile (Expression), Data, Matches);
3370 Compile (PM, Expression, Final_Size);
3371 Match (PM, Data, Matches);
3374 return Matches (0).First >= Data'First;
3381 function Operand (P : Pointer) return Pointer is
3390 procedure Optimize (Self : in out Pattern_Matcher) is
3391 Max_Length : Program_Size;
3392 This_Length : Program_Size;
3395 Program : Program_Data renames Self.Program;
3398 -- Start with safe defaults (no optimization):
3399 -- * No known first character of match
3400 -- * Does not necessarily start at beginning of line
3401 -- * No string known that has to appear in data
3403 Self.First := ASCII.NUL;
3404 Self.Anchored := False;
3405 Self.Must_Have := Program'Last + 1;
3406 Self.Must_Have_Length := 0;
3408 Scan := Program_First + 1; -- First instruction (can be anything)
3410 if Program (Scan) = EXACT then
3411 Self.First := Program (String_Operand (Scan));
3413 elsif Program (Scan) = BOL
3414 or else Program (Scan) = SBOL
3415 or else Program (Scan) = MBOL
3417 Self.Anchored := True;
3420 -- If there's something expensive in the regexp, find the
3421 -- longest literal string that must appear and make it the
3422 -- regmust. Resolve ties in favor of later strings, since
3423 -- the regstart check works with the beginning of the regexp.
3424 -- and avoiding duplication strengthens checking. Not a
3425 -- strong reason, but sufficient in the absence of others.
3427 if False then -- if Flags.SP_Start then ???
3430 while Scan /= 0 loop
3431 if Program (Scan) = EXACT or else Program (Scan) = EXACTF then
3432 This_Length := String_Length (Program, Scan);
3434 if This_Length >= Max_Length then
3435 Longest := String_Operand (Scan);
3436 Max_Length := This_Length;
3440 Scan := Get_Next (Program, Scan);
3443 Self.Must_Have := Longest;
3444 Self.Must_Have_Length := Natural (Max_Length) + 1;
3452 function Paren_Count (Regexp : Pattern_Matcher) return Match_Count is
3454 return Regexp.Paren_Count;
3461 function Quote (Str : String) return String is
3462 S : String (1 .. Str'Length * 2);
3463 Last : Natural := 0;
3466 for J in Str'Range loop
3468 when '^' | '$' | '|' | '*' | '+' | '?' | '{'
3469 | '}' | '[' | ']' | '(' | ')' | '\' =>
3471 S (Last + 1) := '\';
3472 S (Last + 2) := Str (J);
3476 S (Last + 1) := Str (J);
3481 return S (1 .. Last);
3488 function Read_Natural
3489 (Program : Program_Data;
3494 return Character'Pos (Program (IP)) +
3495 256 * Character'Pos (Program (IP + 1));
3502 procedure Reset_Class (Bitmap : in out Character_Class) is
3504 Bitmap := (others => 0);
3511 procedure Set_In_Class
3512 (Bitmap : in out Character_Class;
3515 Value : constant Class_Byte := Character'Pos (C);
3518 Bitmap (Value / 8) := Bitmap (Value / 8)
3519 or Bit_Conversion (Value mod 8);
3526 function String_Length
3527 (Program : Program_Data;
3532 pragma Assert (Program (P) = EXACT or else Program (P) = EXACTF);
3533 return Character'Pos (Program (P + 3));
3536 --------------------
3537 -- String_Operand --
3538 --------------------
3540 function String_Operand (P : Pointer) return Pointer is