1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- G N A T . R E G P A T --
10 -- Copyright (C) 1986 by University of Toronto. --
11 -- Copyright (C) 1996-2002 Ada Core Technologies, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- As a special exception, if other files instantiate generics from this --
25 -- unit, or you link this unit with other files to produce an executable, --
26 -- this unit does not by itself cause the resulting executable to be --
27 -- covered by the GNU General Public License. This exception does not --
28 -- however invalidate any other reasons why the executable file might be --
29 -- covered by the GNU Public License. --
31 -- GNAT is maintained by Ada Core Technologies Inc (http://www.gnat.com). --
33 ------------------------------------------------------------------------------
35 -- This is an altered Ada 95 version of the original V8 style regular
36 -- expression library written in C by Henry Spencer. Apart from the
37 -- translation to Ada, the interface has been considerably changed to
38 -- use the Ada String type instead of C-style nul-terminated strings.
40 -- Beware that some of this code is subtly aware of the way operator
41 -- precedence is structured in regular expressions. Serious changes in
42 -- regular-expression syntax might require a total rethink.
44 with System
.IO
; use System
.IO
;
45 with Ada
.Characters
.Handling
; use Ada
.Characters
.Handling
;
46 with Unchecked_Conversion
;
48 package body GNAT
.Regpat
is
50 MAGIC
: constant Character := Character'Val (10#
0234#
);
51 -- The first byte of the regexp internal "program" is actually
52 -- this magic number; the start node begins in the second byte.
54 -- This is used to make sure that a regular expression was correctly
57 ----------------------------
58 -- Implementation details --
59 ----------------------------
61 -- This is essentially a linear encoding of a nondeterministic
62 -- finite-state machine, also known as syntax charts or
63 -- "railroad normal form" in parsing technology.
65 -- Each node is an opcode plus a "next" pointer, possibly plus an
66 -- operand. "Next" pointers of all nodes except BRANCH implement
67 -- concatenation; a "next" pointer with a BRANCH on both ends of it
68 -- is connecting two alternatives.
70 -- The operand of some types of node is a literal string; for others,
71 -- it is a node leading into a sub-FSM. In particular, the operand of
72 -- a BRANCH node is the first node of the branch.
73 -- (NB this is *not* a tree structure: the tail of the branch connects
74 -- to the thing following the set of BRANCHes).
76 -- You can see the exact byte-compiled version by using the Dump
77 -- subprogram. However, here are a few examples:
80 -- 2 : BRANCH (next at 10)
81 -- 5 : EXACT (next at 18) operand=a
82 -- 10 : BRANCH (next at 18)
83 -- 13 : EXACT (next at 18) operand=b
84 -- 18 : EOP (next at 0)
87 -- 2 : CURLYX (next at 26) { 0, 32767}
88 -- 9 : OPEN 1 (next at 13)
89 -- 13 : EXACT (next at 19) operand=ab
90 -- 19 : CLOSE 1 (next at 23)
91 -- 23 : WHILEM (next at 0)
92 -- 26 : NOTHING (next at 29)
93 -- 29 : EOP (next at 0)
99 -- Name Operand? Meaning
101 (EOP
, -- no End of program
102 MINMOD
, -- no Next operator is not greedy
104 -- Classes of characters
106 ANY
, -- no Match any one character except newline
107 SANY
, -- no Match any character, including new line
108 ANYOF
, -- class Match any character in this class
109 EXACT
, -- str Match this string exactly
110 EXACTF
, -- str Match this string (case-folding is one)
111 NOTHING
, -- no Match empty string
112 SPACE
, -- no Match any whitespace character
113 NSPACE
, -- no Match any non-whitespace character
114 DIGIT
, -- no Match any numeric character
115 NDIGIT
, -- no Match any non-numeric character
116 ALNUM
, -- no Match any alphanumeric character
117 NALNUM
, -- no Match any non-alphanumeric character
121 BRANCH
, -- node Match this alternative, or the next
123 -- Simple loops (when the following node is one character in length)
125 STAR
, -- node Match this simple thing 0 or more times
126 PLUS
, -- node Match this simple thing 1 or more times
127 CURLY
, -- 2num node Match this simple thing between n and m times.
131 CURLYX
, -- 2num node Match this complex thing {n,m} times
132 -- The nums are coded on two characters each.
134 WHILEM
, -- no Do curly processing and see if rest matches
136 -- Matches after or before a word
138 BOL
, -- no Match "" at beginning of line
139 MBOL
, -- no Same, assuming mutiline (match after \n)
140 SBOL
, -- no Same, assuming single line (don't match at \n)
141 EOL
, -- no Match "" at end of line
142 MEOL
, -- no Same, assuming mutiline (match before \n)
143 SEOL
, -- no Same, assuming single line (don't match at \n)
145 BOUND
, -- no Match "" at any word boundary
146 NBOUND
, -- no Match "" at any word non-boundary
148 -- Parenthesis groups handling
150 REFF
, -- num Match some already matched string, folded
151 OPEN
, -- num Mark this point in input as start of #n
152 CLOSE
); -- num Analogous to OPEN
154 for Opcode
'Size use 8;
159 -- The set of branches constituting a single choice are hooked
160 -- together with their "next" pointers, since precedence prevents
161 -- anything being concatenated to any individual branch. The
162 -- "next" pointer of the last BRANCH in a choice points to the
163 -- thing following the whole choice. This is also where the
164 -- final "next" pointer of each individual branch points; each
165 -- branch starts with the operand node of a BRANCH node.
168 -- '?', and complex '*' and '+', are implemented with CURLYX.
169 -- branches. Simple cases (one character per match) are implemented with
170 -- STAR and PLUS for speed and to minimize recursive plunges.
173 -- ...are numbered at compile time.
176 -- There are in fact two arguments, the first one is the length (minus
177 -- one of the string argument), coded on one character, the second
178 -- argument is the string itself, coded on length + 1 characters.
180 -- A node is one char of opcode followed by two chars of "next" pointer.
181 -- "Next" pointers are stored as two 8-bit pieces, high order first. The
182 -- value is a positive offset from the opcode of the node containing it.
183 -- An operand, if any, simply follows the node. (Note that much of the
184 -- code generation knows about this implicit relationship.)
186 -- Using two bytes for the "next" pointer is vast overkill for most
187 -- things, but allows patterns to get big without disasters.
189 -----------------------
190 -- Character classes --
191 -----------------------
192 -- This is the implementation for character classes ([...]) in the
193 -- syntax for regular expressions. Each character (0..256) has an
194 -- entry into the table. This makes for a very fast matching
197 type Class_Byte
is mod 256;
198 type Character_Class
is array (Class_Byte
range 0 .. 31) of Class_Byte
;
200 type Bit_Conversion_Array
is array (Class_Byte
range 0 .. 7) of Class_Byte
;
201 Bit_Conversion
: constant Bit_Conversion_Array
:=
202 (1, 2, 4, 8, 16, 32, 64, 128);
204 type Std_Class
is (ANYOF_NONE
,
205 ANYOF_ALNUM
, -- Alphanumeric class [a-zA-Z0-9]
207 ANYOF_SPACE
, -- Space class [ \t\n\r\f]
209 ANYOF_DIGIT
, -- Digit class [0-9]
211 ANYOF_ALNUMC
, -- Alphanumeric class [a-zA-Z0-9]
213 ANYOF_ALPHA
, -- Alpha class [a-zA-Z]
215 ANYOF_ASCII
, -- Ascii class (7 bits) 0..127
217 ANYOF_CNTRL
, -- Control class
219 ANYOF_GRAPH
, -- Graphic class
221 ANYOF_LOWER
, -- Lower case class [a-z]
223 ANYOF_PRINT
, -- printable class
227 ANYOF_UPPER
, -- Upper case class [A-Z]
229 ANYOF_XDIGIT
, -- Hexadecimal digit
233 procedure Set_In_Class
234 (Bitmap
: in out Character_Class
;
236 -- Set the entry to True for C in the class Bitmap.
238 function Get_From_Class
239 (Bitmap
: Character_Class
;
242 -- Return True if the entry is set for C in the class Bitmap.
244 procedure Reset_Class
(Bitmap
: in out Character_Class
);
245 -- Clear all the entries in the class Bitmap.
247 pragma Inline
(Set_In_Class
);
248 pragma Inline
(Get_From_Class
);
249 pragma Inline
(Reset_Class
);
251 -----------------------
252 -- Local Subprograms --
253 -----------------------
255 function "=" (Left
: Character; Right
: Opcode
) return Boolean;
257 function Is_Alnum
(C
: Character) return Boolean;
258 -- Return True if C is an alphanum character or an underscore ('_')
260 function Is_Space
(C
: Character) return Boolean;
261 -- Return True if C is a whitespace character
263 function Is_Printable
(C
: Character) return Boolean;
264 -- Return True if C is a printable character
266 function Operand
(P
: Pointer
) return Pointer
;
267 -- Return a pointer to the first operand of the node at P
269 function String_Length
270 (Program
: Program_Data
;
273 -- Return the length of the string argument of the node at P
275 function String_Operand
(P
: Pointer
) return Pointer
;
276 -- Return a pointer to the string argument of the node at P
278 procedure Bitmap_Operand
279 (Program
: Program_Data
;
281 Op
: out Character_Class
);
282 -- Return a pointer to the string argument of the node at P
284 function Get_Next_Offset
285 (Program
: Program_Data
;
288 -- Get the offset field of a node. Used by Get_Next.
291 (Program
: Program_Data
;
294 -- Dig the next instruction pointer out of a node
296 procedure Optimize
(Self
: in out Pattern_Matcher
);
297 -- Optimize a Pattern_Matcher by noting certain special cases
299 function Read_Natural
300 (Program
: Program_Data
;
303 -- Return the 2-byte natural coded at position IP.
305 -- All of the subprograms above are tiny and should be inlined
308 pragma Inline
(Is_Alnum
);
309 pragma Inline
(Is_Space
);
310 pragma Inline
(Get_Next
);
311 pragma Inline
(Get_Next_Offset
);
312 pragma Inline
(Operand
);
313 pragma Inline
(Read_Natural
);
314 pragma Inline
(String_Length
);
315 pragma Inline
(String_Operand
);
317 type Expression_Flags
is record
318 Has_Width
, -- Known never to match null string
319 Simple
, -- Simple enough to be STAR/PLUS operand
320 SP_Start
: Boolean; -- Starts with * or +
323 Worst_Expression
: constant Expression_Flags
:= (others => False);
330 function "=" (Left
: Character; Right
: Opcode
) return Boolean is
332 return Character'Pos (Left
) = Opcode
'Pos (Right
);
339 procedure Bitmap_Operand
340 (Program
: Program_Data
;
342 Op
: out Character_Class
)
344 function Convert
is new Unchecked_Conversion
345 (Program_Data
, Character_Class
);
348 Op
(0 .. 31) := Convert
(Program
(P
+ 3 .. P
+ 34));
356 (Matcher
: out Pattern_Matcher
;
358 Final_Code_Size
: out Program_Size
;
359 Flags
: Regexp_Flags
:= No_Flags
)
361 -- We can't allocate space until we know how big the compiled form
362 -- will be, but we can't compile it (and thus know how big it is)
363 -- until we've got a place to put the code. So we cheat: we compile
364 -- it twice, once with code generation turned off and size counting
365 -- turned on, and once "for real".
367 -- This also means that we don't allocate space until we are sure
368 -- that the thing really will compile successfully, and we never
369 -- have to move the code and thus invalidate pointers into it.
371 -- Beware that the optimization-preparation code in here knows
372 -- about some of the structure of the compiled regexp.
374 PM
: Pattern_Matcher
renames Matcher
;
375 Program
: Program_Data
renames PM
.Program
;
377 Emit_Code
: constant Boolean := PM
.Size
> 0;
378 Emit_Ptr
: Pointer
:= Program_First
;
380 Parse_Pos
: Natural := Expression
'First; -- Input-scan pointer
381 Parse_End
: Natural := Expression
'Last;
383 ----------------------------
384 -- Subprograms for Create --
385 ----------------------------
387 procedure Emit
(B
: Character);
388 -- Output the Character to the Program.
389 -- If code-generation is disables, simply increments the program
392 function Emit_Node
(Op
: Opcode
) return Pointer
;
393 -- If code-generation is enabled, Emit_Node outputs the
394 -- opcode and reserves space for a pointer to the next node.
395 -- Return value is the location of new opcode, ie old Emit_Ptr.
397 procedure Emit_Natural
(IP
: Pointer
; N
: Natural);
398 -- Split N on two characters at position IP.
400 procedure Emit_Class
(Bitmap
: Character_Class
);
401 -- Emits a character class.
403 procedure Case_Emit
(C
: Character);
404 -- Emit C, after converting is to lower-case if the regular
405 -- expression is case insensitive.
408 (Parenthesized
: Boolean;
409 Flags
: in out Expression_Flags
;
411 -- Parse regular expression, i.e. main body or parenthesized thing
412 -- Caller must absorb opening parenthesis.
414 procedure Parse_Branch
415 (Flags
: in out Expression_Flags
;
418 -- Implements the concatenation operator and handles '|'
419 -- First should be true if this is the first item of the alternative.
421 procedure Parse_Piece
422 (Expr_Flags
: in out Expression_Flags
; IP
: out Pointer
);
423 -- Parse something followed by possible [*+?]
426 (Expr_Flags
: in out Expression_Flags
; IP
: out Pointer
);
427 -- Parse_Atom is the lowest level parse procedure.
428 -- Optimization: gobbles an entire sequence of ordinary characters
429 -- so that it can turn them into a single node, which is smaller to
430 -- store and faster to run. Backslashed characters are exceptions,
431 -- each becoming a separate node; the code is simpler that way and
432 -- it's not worth fixing.
434 procedure Insert_Operator
437 Greedy
: Boolean := True);
438 -- Insert_Operator inserts an operator in front of an
439 -- already-emitted operand and relocates the operand.
440 -- This applies to PLUS and STAR.
441 -- If Minmod is True, then the operator is non-greedy.
443 procedure Insert_Curly_Operator
448 Greedy
: Boolean := True);
449 -- Insert an operator for CURLY ({Min}, {Min,} or {Min,Max}).
450 -- If Minmod is True, then the operator is non-greedy.
452 procedure Link_Tail
(P
, Val
: Pointer
);
453 -- Link_Tail sets the next-pointer at the end of a node chain
455 procedure Link_Operand_Tail
(P
, Val
: Pointer
);
456 -- Link_Tail on operand of first argument; nop if operandless
458 function Next_Instruction
(P
: Pointer
) return Pointer
;
459 -- Dig the "next" pointer out of a node
461 procedure Fail
(M
: in String);
462 pragma No_Return
(Fail
);
463 -- Fail with a diagnostic message, if possible
465 function Is_Curly_Operator
(IP
: Natural) return Boolean;
466 -- Return True if IP is looking at a '{' that is the beginning
467 -- of a curly operator, ie it matches {\d+,?\d*}
469 function Is_Mult
(IP
: Natural) return Boolean;
470 -- Return True if C is a regexp multiplier: '+', '*' or '?'
472 procedure Get_Curly_Arguments
476 Greedy
: out Boolean);
477 -- Parse the argument list for a curly operator.
478 -- It is assumed that IP is indeed pointing at a valid operator.
480 procedure Parse_Character_Class
(IP
: out Pointer
);
481 -- Parse a character class.
482 -- The calling subprogram should consume the opening '[' before.
484 procedure Parse_Literal
(Expr_Flags
: in out Expression_Flags
;
486 -- Parse_Literal encodes a string of characters
487 -- to be matched exactly.
489 function Parse_Posix_Character_Class
return Std_Class
;
490 -- Parse a posic character class, like [:alpha:] or [:^alpha:].
491 -- The called is suppoed to absorbe the opening [.
493 pragma Inline
(Is_Mult
);
494 pragma Inline
(Emit_Natural
);
495 pragma Inline
(Parse_Character_Class
); -- since used only once
501 procedure Case_Emit
(C
: Character) is
503 if (Flags
and Case_Insensitive
) /= 0 then
507 -- Dump current character
517 procedure Emit
(B
: Character) is
520 Program
(Emit_Ptr
) := B
;
523 Emit_Ptr
:= Emit_Ptr
+ 1;
530 procedure Emit_Class
(Bitmap
: Character_Class
) is
531 subtype Program31
is Program_Data
(0 .. 31);
533 function Convert
is new Unchecked_Conversion
534 (Character_Class
, Program31
);
538 Program
(Emit_Ptr
.. Emit_Ptr
+ 31) := Convert
(Bitmap
);
541 Emit_Ptr
:= Emit_Ptr
+ 32;
548 procedure Emit_Natural
(IP
: Pointer
; N
: Natural) is
551 Program
(IP
+ 1) := Character'Val (N
/ 256);
552 Program
(IP
) := Character'Val (N
mod 256);
560 function Emit_Node
(Op
: Opcode
) return Pointer
is
561 Result
: constant Pointer
:= Emit_Ptr
;
565 Program
(Emit_Ptr
) := Character'Val (Opcode
'Pos (Op
));
566 Program
(Emit_Ptr
+ 1) := ASCII
.NUL
;
567 Program
(Emit_Ptr
+ 2) := ASCII
.NUL
;
570 Emit_Ptr
:= Emit_Ptr
+ 3;
578 procedure Fail
(M
: in String) is
580 raise Expression_Error
;
583 -------------------------
584 -- Get_Curly_Arguments --
585 -------------------------
587 procedure Get_Curly_Arguments
591 Greedy
: out Boolean)
593 pragma Warnings
(Off
, IP
);
595 Save_Pos
: Natural := Parse_Pos
+ 1;
599 Max
:= Max_Curly_Repeat
;
601 while Expression
(Parse_Pos
) /= '}'
602 and then Expression
(Parse_Pos
) /= ','
604 Parse_Pos
:= Parse_Pos
+ 1;
607 Min
:= Natural'Value (Expression
(Save_Pos
.. Parse_Pos
- 1));
609 if Expression
(Parse_Pos
) = ',' then
610 Save_Pos
:= Parse_Pos
+ 1;
611 while Expression
(Parse_Pos
) /= '}' loop
612 Parse_Pos
:= Parse_Pos
+ 1;
615 if Save_Pos
/= Parse_Pos
then
616 Max
:= Natural'Value (Expression
(Save_Pos
.. Parse_Pos
- 1));
623 if Parse_Pos
< Expression
'Last
624 and then Expression
(Parse_Pos
+ 1) = '?'
627 Parse_Pos
:= Parse_Pos
+ 1;
632 end Get_Curly_Arguments
;
634 ---------------------------
635 -- Insert_Curly_Operator --
636 ---------------------------
638 procedure Insert_Curly_Operator
643 Greedy
: Boolean := True)
645 Dest
: constant Pointer
:= Emit_Ptr
;
650 -- If the operand is not greedy, insert an extra operand before it
656 -- Move the operand in the byte-compilation, so that we can insert
657 -- the operator before it.
660 Program
(Operand
+ Size
.. Emit_Ptr
+ Size
) :=
661 Program
(Operand
.. Emit_Ptr
);
664 -- Insert the operator at the position previously occupied by the
670 Old
:= Emit_Node
(MINMOD
);
671 Link_Tail
(Old
, Old
+ 3);
674 Old
:= Emit_Node
(Op
);
675 Emit_Natural
(Old
+ 3, Min
);
676 Emit_Natural
(Old
+ 5, Max
);
678 Emit_Ptr
:= Dest
+ Size
;
679 end Insert_Curly_Operator
;
681 ---------------------
682 -- Insert_Operator --
683 ---------------------
685 procedure Insert_Operator
688 Greedy
: Boolean := True)
690 Dest
: constant Pointer
:= Emit_Ptr
;
695 -- If not greedy, we have to emit another opcode first
701 -- Move the operand in the byte-compilation, so that we can insert
702 -- the operator before it.
705 Program
(Operand
+ Size
.. Emit_Ptr
+ Size
)
706 := Program
(Operand
.. Emit_Ptr
);
709 -- Insert the operator at the position previously occupied by the
715 Old
:= Emit_Node
(MINMOD
);
716 Link_Tail
(Old
, Old
+ 3);
719 Old
:= Emit_Node
(Op
);
720 Emit_Ptr
:= Dest
+ Size
;
723 -----------------------
724 -- Is_Curly_Operator --
725 -----------------------
727 function Is_Curly_Operator
(IP
: Natural) return Boolean is
728 Scan
: Natural := IP
;
731 if Expression
(Scan
) /= '{'
732 or else Scan
+ 2 > Expression
'Last
733 or else not Is_Digit
(Expression
(Scan
+ 1))
745 if Scan
> Expression
'Last then
749 exit when not Is_Digit
(Expression
(Scan
));
752 if Expression
(Scan
) = ',' then
756 if Scan
> Expression
'Last then
760 exit when not Is_Digit
(Expression
(Scan
));
764 return Expression
(Scan
) = '}';
765 end Is_Curly_Operator
;
771 function Is_Mult
(IP
: Natural) return Boolean is
772 C
: constant Character := Expression
(IP
);
778 or else (C
= '{' and then Is_Curly_Operator
(IP
));
781 -----------------------
782 -- Link_Operand_Tail --
783 -----------------------
785 procedure Link_Operand_Tail
(P
, Val
: Pointer
) is
787 if Emit_Code
and then Program
(P
) = BRANCH
then
788 Link_Tail
(Operand
(P
), Val
);
790 end Link_Operand_Tail
;
796 procedure Link_Tail
(P
, Val
: Pointer
) is
802 if not Emit_Code
then
810 Temp
:= Next_Instruction
(Scan
);
815 Offset
:= Val
- Scan
;
817 Emit_Natural
(Scan
+ 1, Natural (Offset
));
820 ----------------------
821 -- Next_Instruction --
822 ----------------------
824 function Next_Instruction
(P
: Pointer
) return Pointer
is
828 if not Emit_Code
then
832 Offset
:= Get_Next_Offset
(Program
, P
);
839 end Next_Instruction
;
845 -- Combining parenthesis handling with the base level
846 -- of regular expression is a trifle forced, but the
847 -- need to tie the tails of the branches to what follows
848 -- makes it hard to avoid.
851 (Parenthesized
: in Boolean;
852 Flags
: in out Expression_Flags
;
855 E
: String renames Expression
;
859 New_Flags
: Expression_Flags
;
860 Have_Branch
: Boolean := False;
863 Flags
:= (Has_Width
=> True, others => False); -- Tentatively
865 -- Make an OPEN node, if parenthesized
867 if Parenthesized
then
868 if Matcher
.Paren_Count
> Max_Paren_Count
then
869 Fail
("too many ()");
872 Par_No
:= Matcher
.Paren_Count
+ 1;
873 Matcher
.Paren_Count
:= Matcher
.Paren_Count
+ 1;
874 IP
:= Emit_Node
(OPEN
);
875 Emit
(Character'Val (Par_No
));
882 -- Pick up the branches, linking them together
884 Parse_Branch
(New_Flags
, True, Br
);
891 if Parse_Pos
<= Parse_End
892 and then E
(Parse_Pos
) = '|'
894 Insert_Operator
(BRANCH
, Br
);
899 Link_Tail
(IP
, Br
); -- OPEN -> first
904 if not New_Flags
.Has_Width
then
905 Flags
.Has_Width
:= False;
908 Flags
.SP_Start
:= Flags
.SP_Start
or New_Flags
.SP_Start
;
910 while Parse_Pos
<= Parse_End
911 and then (E
(Parse_Pos
) = '|')
913 Parse_Pos
:= Parse_Pos
+ 1;
914 Parse_Branch
(New_Flags
, False, Br
);
921 Link_Tail
(IP
, Br
); -- BRANCH -> BRANCH
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
;
930 -- Make a closing node, and hook it on the end
932 if Parenthesized
then
933 Ender
:= Emit_Node
(CLOSE
);
934 Emit
(Character'Val (Par_No
));
936 Ender
:= Emit_Node
(EOP
);
939 Link_Tail
(IP
, Ender
);
943 -- Hook the tails of the branches to the closing node
948 Link_Operand_Tail
(Br
, Ender
);
949 Br
:= Next_Instruction
(Br
);
953 -- Check for proper termination
955 if Parenthesized
then
956 if Parse_Pos
> Parse_End
or else E
(Parse_Pos
) /= ')' then
957 Fail
("unmatched ()");
960 Parse_Pos
:= Parse_Pos
+ 1;
962 elsif Parse_Pos
<= Parse_End
then
963 if E
(Parse_Pos
) = ')' then
964 Fail
("unmatched ()");
966 Fail
("junk on end"); -- "Can't happen"
976 (Expr_Flags
: in out Expression_Flags
;
982 -- Tentatively set worst expression case
984 Expr_Flags
:= Worst_Expression
;
986 C
:= Expression
(Parse_Pos
);
987 Parse_Pos
:= Parse_Pos
+ 1;
991 if (Flags
and Multiple_Lines
) /= 0 then
992 IP
:= Emit_Node
(MBOL
);
993 elsif (Flags
and Single_Line
) /= 0 then
994 IP
:= Emit_Node
(SBOL
);
996 IP
:= Emit_Node
(BOL
);
1000 if (Flags
and Multiple_Lines
) /= 0 then
1001 IP
:= Emit_Node
(MEOL
);
1002 elsif (Flags
and Single_Line
) /= 0 then
1003 IP
:= Emit_Node
(SEOL
);
1005 IP
:= Emit_Node
(EOL
);
1009 if (Flags
and Single_Line
) /= 0 then
1010 IP
:= Emit_Node
(SANY
);
1012 IP
:= Emit_Node
(ANY
);
1015 Expr_Flags
.Has_Width
:= True;
1016 Expr_Flags
.Simple
:= True;
1019 Parse_Character_Class
(IP
);
1020 Expr_Flags
.Has_Width
:= True;
1021 Expr_Flags
.Simple
:= True;
1025 New_Flags
: Expression_Flags
;
1028 Parse
(True, New_Flags
, IP
);
1034 Expr_Flags
.Has_Width
:=
1035 Expr_Flags
.Has_Width
or New_Flags
.Has_Width
;
1036 Expr_Flags
.SP_Start
:=
1037 Expr_Flags
.SP_Start
or New_Flags
.SP_Start
;
1040 when '|' | ASCII
.LF |
')' =>
1041 Fail
("internal urp"); -- Supposed to be caught earlier
1043 when '?' |
'+' |
'*' |
'{' =>
1044 Fail
("?+*{ follows nothing");
1047 if Parse_Pos
> Parse_End
then
1048 Fail
("trailing \");
1051 Parse_Pos := Parse_Pos + 1;
1053 case Expression (Parse_Pos - 1) is
1055 IP := Emit_Node (BOUND);
1058 IP := Emit_Node (NBOUND);
1061 IP := Emit_Node (SPACE);
1062 Expr_Flags.Simple := True;
1063 Expr_Flags.Has_Width := True;
1066 IP := Emit_Node (NSPACE);
1067 Expr_Flags.Simple := True;
1068 Expr_Flags.Has_Width := True;
1071 IP := Emit_Node (DIGIT);
1072 Expr_Flags.Simple := True;
1073 Expr_Flags.Has_Width := True;
1076 IP := Emit_Node (NDIGIT);
1077 Expr_Flags.Simple := True;
1078 Expr_Flags.Has_Width := True;
1081 IP := Emit_Node (ALNUM);
1082 Expr_Flags.Simple := True;
1083 Expr_Flags.Has_Width := True;
1086 IP := Emit_Node (NALNUM);
1087 Expr_Flags.Simple := True;
1088 Expr_Flags.Has_Width := True;
1091 IP := Emit_Node (SBOL);
1094 IP := Emit_Node (SEOL);
1097 IP := Emit_Node (REFF);
1100 Save : Natural := Parse_Pos - 1;
1103 while Parse_Pos <= Expression'Last
1104 and then Is_Digit (Expression (Parse_Pos))
1106 Parse_Pos := Parse_Pos + 1;
1109 Emit (Character'Val (Natural'Value
1110 (Expression (Save .. Parse_Pos - 1))));
1114 Parse_Pos := Parse_Pos - 1;
1115 Parse_Literal (Expr_Flags, IP);
1119 Parse_Literal (Expr_Flags, IP);
1127 procedure Parse_Branch
1128 (Flags : in out Expression_Flags;
1132 E : String renames Expression;
1135 New_Flags : Expression_Flags;
1139 Flags := Worst_Expression; -- Tentatively
1144 IP := Emit_Node (BRANCH);
1149 while Parse_Pos <= Parse_End
1150 and then E (Parse_Pos) /= ')'
1151 and then E (Parse_Pos) /= ASCII.LF
1152 and then E (Parse_Pos) /= '|'
1154 Parse_Piece (New_Flags, Last);
1161 Flags.Has_Width := Flags.Has_Width or New_Flags.Has_Width;
1163 if Chain = 0 then -- First piece
1164 Flags.SP_Start := Flags.SP_Start or New_Flags.SP_Start;
1166 Link_Tail (Chain, Last);
1172 if Chain = 0 then -- Loop ran zero CURLY
1173 Dummy := Emit_Node (NOTHING);
1178 ---------------------------
1179 -- Parse_Character_Class --
1180 ---------------------------
1182 procedure Parse_Character_Class (IP : out Pointer) is
1183 Bitmap : Character_Class;
1184 Invert : Boolean := False;
1185 In_Range : Boolean := False;
1186 Named_Class : Std_Class := ANYOF_NONE;
1188 Last_Value : Character := ASCII.Nul;
1191 Reset_Class (Bitmap);
1193 -- Do we have an invert character class ?
1195 if Parse_Pos <= Parse_End
1196 and then Expression (Parse_Pos) = '^'
1199 Parse_Pos := Parse_Pos + 1;
1202 -- First character can be ] or -, without closing the class.
1204 if Parse_Pos <= Parse_End
1205 and then (Expression (Parse_Pos) = ']'
1206 or else Expression (Parse_Pos) = '-')
1208 Set_In_Class (Bitmap, Expression (Parse_Pos));
1209 Parse_Pos := Parse_Pos + 1;
1212 -- While we don't have the end of the class
1214 while Parse_Pos <= Parse_End
1215 and then Expression (Parse_Pos) /= ']'
1217 Named_Class := ANYOF_NONE;
1218 Value := Expression (Parse_Pos);
1219 Parse_Pos := Parse_Pos + 1;
1221 -- Do we have a Posix character class
1223 Named_Class := Parse_Posix_Character_Class;
1225 elsif Value = '\' then
1226 if Parse_Pos = Parse_End then
1227 Fail ("Trailing
\");
1229 Value
:= Expression
(Parse_Pos
);
1230 Parse_Pos
:= Parse_Pos
+ 1;
1233 when 'w' => Named_Class
:= ANYOF_ALNUM
;
1234 when 'W' => Named_Class
:= ANYOF_NALNUM
;
1235 when 's' => Named_Class
:= ANYOF_SPACE
;
1236 when 'S' => Named_Class
:= ANYOF_NSPACE
;
1237 when 'd' => Named_Class
:= ANYOF_DIGIT
;
1238 when 'D' => Named_Class
:= ANYOF_NDIGIT
;
1239 when 'n' => Value
:= ASCII
.LF
;
1240 when 'r' => Value
:= ASCII
.CR
;
1241 when 't' => Value
:= ASCII
.HT
;
1242 when 'f' => Value
:= ASCII
.FF
;
1243 when 'e' => Value
:= ASCII
.ESC
;
1244 when 'a' => Value
:= ASCII
.BEL
;
1246 -- when 'x' => ??? hexadecimal value
1247 -- when 'c' => ??? control character
1248 -- when '0'..'9' => ??? octal character
1250 when others => null;
1254 -- Do we have a character class?
1256 if Named_Class
/= ANYOF_NONE
then
1258 -- A range like 'a-\d' or 'a-[:digit:] is not a range
1261 Set_In_Class
(Bitmap
, Last_Value
);
1262 Set_In_Class
(Bitmap
, '-');
1269 when ANYOF_NONE
=> null;
1271 when ANYOF_ALNUM | ANYOF_ALNUMC
=>
1272 for Value
in Class_Byte
'Range loop
1273 if Is_Alnum
(Character'Val (Value
)) then
1274 Set_In_Class
(Bitmap
, Character'Val (Value
));
1278 when ANYOF_NALNUM | ANYOF_NALNUMC
=>
1279 for Value
in Class_Byte
'Range loop
1280 if not Is_Alnum
(Character'Val (Value
)) then
1281 Set_In_Class
(Bitmap
, Character'Val (Value
));
1286 for Value
in Class_Byte
'Range loop
1287 if Is_Space
(Character'Val (Value
)) then
1288 Set_In_Class
(Bitmap
, Character'Val (Value
));
1292 when ANYOF_NSPACE
=>
1293 for Value
in Class_Byte
'Range loop
1294 if not Is_Space
(Character'Val (Value
)) then
1295 Set_In_Class
(Bitmap
, Character'Val (Value
));
1300 for Value
in Class_Byte
'Range loop
1301 if Is_Digit
(Character'Val (Value
)) then
1302 Set_In_Class
(Bitmap
, Character'Val (Value
));
1306 when ANYOF_NDIGIT
=>
1307 for Value
in Class_Byte
'Range loop
1308 if not Is_Digit
(Character'Val (Value
)) then
1309 Set_In_Class
(Bitmap
, Character'Val (Value
));
1314 for Value
in Class_Byte
'Range loop
1315 if Is_Letter
(Character'Val (Value
)) then
1316 Set_In_Class
(Bitmap
, Character'Val (Value
));
1320 when ANYOF_NALPHA
=>
1321 for Value
in Class_Byte
'Range loop
1322 if not Is_Letter
(Character'Val (Value
)) then
1323 Set_In_Class
(Bitmap
, Character'Val (Value
));
1328 for Value
in 0 .. 127 loop
1329 Set_In_Class
(Bitmap
, Character'Val (Value
));
1332 when ANYOF_NASCII
=>
1333 for Value
in 128 .. 255 loop
1334 Set_In_Class
(Bitmap
, Character'Val (Value
));
1338 for Value
in Class_Byte
'Range loop
1339 if Is_Control
(Character'Val (Value
)) then
1340 Set_In_Class
(Bitmap
, Character'Val (Value
));
1344 when ANYOF_NCNTRL
=>
1345 for Value
in Class_Byte
'Range loop
1346 if not Is_Control
(Character'Val (Value
)) then
1347 Set_In_Class
(Bitmap
, Character'Val (Value
));
1352 for Value
in Class_Byte
'Range loop
1353 if Is_Graphic
(Character'Val (Value
)) then
1354 Set_In_Class
(Bitmap
, Character'Val (Value
));
1358 when ANYOF_NGRAPH
=>
1359 for Value
in Class_Byte
'Range loop
1360 if not Is_Graphic
(Character'Val (Value
)) then
1361 Set_In_Class
(Bitmap
, Character'Val (Value
));
1366 for Value
in Class_Byte
'Range loop
1367 if Is_Lower
(Character'Val (Value
)) then
1368 Set_In_Class
(Bitmap
, Character'Val (Value
));
1372 when ANYOF_NLOWER
=>
1373 for Value
in Class_Byte
'Range loop
1374 if not Is_Lower
(Character'Val (Value
)) then
1375 Set_In_Class
(Bitmap
, Character'Val (Value
));
1380 for Value
in Class_Byte
'Range loop
1381 if Is_Printable
(Character'Val (Value
)) then
1382 Set_In_Class
(Bitmap
, Character'Val (Value
));
1386 when ANYOF_NPRINT
=>
1387 for Value
in Class_Byte
'Range loop
1388 if not Is_Printable
(Character'Val (Value
)) then
1389 Set_In_Class
(Bitmap
, Character'Val (Value
));
1394 for Value
in Class_Byte
'Range loop
1395 if Is_Printable
(Character'Val (Value
))
1396 and then not Is_Space
(Character'Val (Value
))
1397 and then not Is_Alnum
(Character'Val (Value
))
1399 Set_In_Class
(Bitmap
, Character'Val (Value
));
1403 when ANYOF_NPUNCT
=>
1404 for Value
in Class_Byte
'Range loop
1405 if not Is_Printable
(Character'Val (Value
))
1406 or else Is_Space
(Character'Val (Value
))
1407 or else Is_Alnum
(Character'Val (Value
))
1409 Set_In_Class
(Bitmap
, Character'Val (Value
));
1414 for Value
in Class_Byte
'Range loop
1415 if Is_Upper
(Character'Val (Value
)) then
1416 Set_In_Class
(Bitmap
, Character'Val (Value
));
1420 when ANYOF_NUPPER
=>
1421 for Value
in Class_Byte
'Range loop
1422 if not Is_Upper
(Character'Val (Value
)) then
1423 Set_In_Class
(Bitmap
, Character'Val (Value
));
1427 when ANYOF_XDIGIT
=>
1428 for Value
in Class_Byte
'Range loop
1429 if Is_Hexadecimal_Digit
(Character'Val (Value
)) then
1430 Set_In_Class
(Bitmap
, Character'Val (Value
));
1434 when ANYOF_NXDIGIT
=>
1435 for Value
in Class_Byte
'Range loop
1436 if not Is_Hexadecimal_Digit
1437 (Character'Val (Value
))
1439 Set_In_Class
(Bitmap
, Character'Val (Value
));
1445 -- Not a character range
1447 elsif not In_Range
then
1448 Last_Value
:= Value
;
1450 if Expression
(Parse_Pos
) = '-'
1451 and then Parse_Pos
< Parse_End
1452 and then Expression
(Parse_Pos
+ 1) /= ']'
1454 Parse_Pos
:= Parse_Pos
+ 1;
1456 -- Do we have a range like '\d-a' and '[:space:]-a'
1457 -- which is not a real range
1459 if Named_Class
/= ANYOF_NONE
then
1460 Set_In_Class
(Bitmap
, '-');
1466 Set_In_Class
(Bitmap
, Value
);
1470 -- Else in a character range
1473 if Last_Value
> Value
then
1474 Fail
("Invalid Range [" & Last_Value
'Img
1475 & "-" & Value
'Img & "]");
1478 while Last_Value
<= Value
loop
1479 Set_In_Class
(Bitmap
, Last_Value
);
1480 Last_Value
:= Character'Succ (Last_Value
);
1489 -- Optimize case-insensitive ranges (put the upper case or lower
1490 -- case character into the bitmap)
1492 if (Flags
and Case_Insensitive
) /= 0 then
1493 for C
in Character'Range loop
1494 if Get_From_Class
(Bitmap
, C
) then
1495 Set_In_Class
(Bitmap
, To_Lower
(C
));
1496 Set_In_Class
(Bitmap
, To_Upper
(C
));
1501 -- Optimize inverted classes
1504 for J
in Bitmap
'Range loop
1505 Bitmap
(J
) := not Bitmap
(J
);
1509 Parse_Pos
:= Parse_Pos
+ 1;
1513 IP
:= Emit_Node
(ANYOF
);
1514 Emit_Class
(Bitmap
);
1515 end Parse_Character_Class
;
1521 -- This is a bit tricky due to quoted chars and due to
1522 -- the multiplier characters '*', '+', and '?' that
1523 -- take the SINGLE char previous as their operand.
1525 -- On entry, the character at Parse_Pos - 1 is going to go
1526 -- into the string, no matter what it is. It could be
1527 -- following a \ if Parse_Atom was entered from the '\' case.
1529 -- Basic idea is to pick up a good char in C and examine
1530 -- the next char. If Is_Mult (C) then twiddle, if it's a \
1531 -- then frozzle and if it's another magic char then push C and
1532 -- terminate the string. If none of the above, push C on the
1533 -- string and go around again.
1535 -- Start_Pos is used to remember where "the current character"
1536 -- starts in the string, if due to an Is_Mult we need to back
1537 -- up and put the current char in a separate 1-character string.
1538 -- When Start_Pos is 0, C is the only char in the string;
1539 -- this is used in Is_Mult handling, and in setting the SIMPLE
1542 procedure Parse_Literal
1543 (Expr_Flags
: in out Expression_Flags
;
1546 Start_Pos
: Natural := 0;
1548 Length_Ptr
: Pointer
;
1549 Has_Special_Operator
: Boolean := False;
1552 Parse_Pos
:= Parse_Pos
- 1; -- Look at current character
1554 if (Flags
and Case_Insensitive
) /= 0 then
1555 IP
:= Emit_Node
(EXACTF
);
1557 IP
:= Emit_Node
(EXACT
);
1560 Length_Ptr
:= Emit_Ptr
;
1561 Emit_Ptr
:= String_Operand
(IP
);
1566 C
:= Expression
(Parse_Pos
); -- Get current character
1569 when '.' |
'[' |
'(' |
')' |
'|' | ASCII
.LF |
'$' |
'^' =>
1571 if Start_Pos
= 0 then
1572 Start_Pos
:= Parse_Pos
;
1573 Emit
(C
); -- First character is always emitted
1575 exit Parse_Loop
; -- Else we are done
1578 when '?' |
'+' |
'*' |
'{' =>
1580 if Start_Pos
= 0 then
1581 Start_Pos
:= Parse_Pos
;
1582 Emit
(C
); -- First character is always emitted
1584 -- Are we looking at an operator, or is this
1585 -- simply a normal character ?
1586 elsif not Is_Mult
(Parse_Pos
) then
1587 Start_Pos
:= Parse_Pos
;
1590 -- We've got something like "abc?d". Mark this as a
1591 -- special case. What we want to emit is a first
1592 -- constant string for "ab", then one for "c" that will
1593 -- ultimately be transformed with a CURLY operator, A
1594 -- special case has to be handled for "a?", since there
1595 -- is no initial string to emit.
1596 Has_Special_Operator
:= True;
1601 Start_Pos
:= Parse_Pos
;
1602 if Parse_Pos
= Parse_End
then
1603 Fail
("Trailing \");
1605 case Expression (Parse_Pos + 1) is
1606 when 'b' | 'B' | 's' | 'S' | 'd' | 'D'
1607 | 'w' | 'W' | '0' .. '9' | 'G' | 'A'
1609 when 'n' => Emit (ASCII.LF);
1610 when 't' => Emit (ASCII.HT);
1611 when 'r' => Emit (ASCII.CR);
1612 when 'f' => Emit (ASCII.FF);
1613 when 'e' => Emit (ASCII.ESC);
1614 when 'a' => Emit (ASCII.BEL);
1615 when others => Emit (Expression (Parse_Pos + 1));
1617 Parse_Pos := Parse_Pos + 1;
1621 Start_Pos := Parse_Pos;
1625 exit Parse_Loop when Emit_Ptr - Length_Ptr = 254;
1627 Parse_Pos := Parse_Pos + 1;
1629 exit Parse_Loop when Parse_Pos > Parse_End;
1630 end loop Parse_Loop;
1632 -- Is the string followed by a '*+?{' operator ? If yes, and if there
1633 -- is an initial string to emit, do it now.
1635 if Has_Special_Operator
1636 and then Emit_Ptr >= Length_Ptr + 3
1638 Emit_Ptr := Emit_Ptr - 1;
1639 Parse_Pos := Start_Pos;
1643 Program (Length_Ptr) := Character'Val (Emit_Ptr - Length_Ptr - 2);
1646 Expr_Flags.Has_Width := True;
1648 -- Slight optimization when there is a single character
1650 if Emit_Ptr = Length_Ptr + 2 then
1651 Expr_Flags.Simple := True;
1659 -- Note that the branching code sequences used for '?' and the
1660 -- general cases of '*' and + are somewhat optimized: they use
1661 -- the same NOTHING node as both the endmarker for their branch
1662 -- list and the body of the last branch. It might seem that
1663 -- this node could be dispensed with entirely, but the endmarker
1664 -- role is not redundant.
1666 procedure Parse_Piece
1667 (Expr_Flags : in out Expression_Flags;
1671 New_Flags : Expression_Flags;
1672 Greedy : Boolean := True;
1675 Parse_Atom (New_Flags, IP);
1681 if Parse_Pos > Parse_End
1682 or else not Is_Mult (Parse_Pos)
1684 Expr_Flags := New_Flags;
1688 Op := Expression (Parse_Pos);
1691 Expr_Flags := (SP_Start => True, others => False);
1693 Expr_Flags := (Has_Width => True, others => False);
1696 -- Detect non greedy operators in the easy cases
1699 and then Parse_Pos + 1 <= Parse_End
1700 and then Expression (Parse_Pos + 1) = '?'
1703 Parse_Pos := Parse_Pos + 1;
1706 -- Generate the byte code
1711 if New_Flags.Simple then
1712 Insert_Operator (STAR, IP, Greedy);
1714 Link_Tail (IP, Emit_Node (WHILEM));
1715 Insert_Curly_Operator
1716 (CURLYX, 0, Max_Curly_Repeat, IP, Greedy);
1717 Link_Tail (IP, Emit_Node (NOTHING));
1722 if New_Flags.Simple then
1723 Insert_Operator (PLUS, IP, Greedy);
1725 Link_Tail (IP, Emit_Node (WHILEM));
1726 Insert_Curly_Operator
1727 (CURLYX, 1, Max_Curly_Repeat, IP, Greedy);
1728 Link_Tail (IP, Emit_Node (NOTHING));
1732 if New_Flags.Simple then
1733 Insert_Curly_Operator (CURLY, 0, 1, IP, Greedy);
1735 Link_Tail (IP, Emit_Node (WHILEM));
1736 Insert_Curly_Operator (CURLYX, 0, 1, IP, Greedy);
1737 Link_Tail (IP, Emit_Node (NOTHING));
1745 Get_Curly_Arguments (Parse_Pos, Min, Max, Greedy);
1747 if New_Flags.Simple then
1748 Insert_Curly_Operator (CURLY, Min, Max, IP, Greedy);
1750 Link_Tail (IP, Emit_Node (WHILEM));
1751 Insert_Curly_Operator (CURLYX, Min, Max, IP, Greedy);
1752 Link_Tail (IP, Emit_Node (NOTHING));
1760 Parse_Pos := Parse_Pos + 1;
1762 if Parse_Pos <= Parse_End
1763 and then Is_Mult (Parse_Pos)
1765 Fail ("nested
*+{");
1769 ---------------------------------
1770 -- Parse_Posix_Character_Class --
1771 ---------------------------------
1773 function Parse_Posix_Character_Class return Std_Class is
1774 Invert : Boolean := False;
1775 Class : Std_Class := ANYOF_NONE;
1776 E : String renames Expression;
1779 if Parse_Pos <= Parse_End
1780 and then Expression (Parse_Pos) = ':'
1782 Parse_Pos := Parse_Pos + 1;
1784 -- Do we have something like: [[:^alpha:]]
1786 if Parse_Pos <= Parse_End
1787 and then Expression (Parse_Pos) = '^'
1790 Parse_Pos := Parse_Pos + 1;
1793 -- All classes have 6 characters at least
1794 -- ??? magid constant 6 should have a name!
1796 if Parse_Pos + 6 <= Parse_End then
1798 case Expression (Parse_Pos) is
1800 if E (Parse_Pos .. Parse_Pos + 4) = "alnum
:]" then
1802 Class := ANYOF_NALNUMC;
1804 Class := ANYOF_ALNUMC;
1807 elsif E (Parse_Pos .. Parse_Pos + 6) = "alpha
:]" then
1809 Class := ANYOF_NALPHA;
1811 Class := ANYOF_ALPHA;
1814 elsif E (Parse_Pos .. Parse_Pos + 6) = "ascii
:]" then
1816 Class := ANYOF_NASCII;
1818 Class := ANYOF_ASCII;
1824 if E (Parse_Pos .. Parse_Pos + 6) = "cntrl
:]" then
1826 Class := ANYOF_NCNTRL;
1828 Class := ANYOF_CNTRL;
1834 if E (Parse_Pos .. Parse_Pos + 6) = "digit
:]" then
1836 Class := ANYOF_NDIGIT;
1838 Class := ANYOF_DIGIT;
1844 if E (Parse_Pos .. Parse_Pos + 6) = "graph
:]" then
1846 Class := ANYOF_NGRAPH;
1848 Class := ANYOF_GRAPH;
1854 if E (Parse_Pos .. Parse_Pos + 6) = "lower
:]" then
1856 Class := ANYOF_NLOWER;
1858 Class := ANYOF_LOWER;
1864 if E (Parse_Pos .. Parse_Pos + 6) = "print
:]" then
1866 Class := ANYOF_NPRINT;
1868 Class := ANYOF_PRINT;
1871 elsif E (Parse_Pos .. Parse_Pos + 6) = "punct
:]" then
1873 Class := ANYOF_NPUNCT;
1875 Class := ANYOF_PUNCT;
1881 if E (Parse_Pos .. Parse_Pos + 6) = "space
:]" then
1883 Class := ANYOF_NSPACE;
1885 Class := ANYOF_SPACE;
1891 if E (Parse_Pos .. Parse_Pos + 6) = "upper
:]" then
1893 Class := ANYOF_NUPPER;
1895 Class := ANYOF_UPPER;
1901 if E (Parse_Pos .. Parse_Pos + 5) = "word
:]" then
1903 Class := ANYOF_NALNUM;
1905 Class := ANYOF_ALNUM;
1908 Parse_Pos := Parse_Pos - 1;
1913 if Parse_Pos + 7 <= Parse_End
1914 and then E (Parse_Pos .. Parse_Pos + 7) = "xdigit
:]"
1917 Class := ANYOF_NXDIGIT;
1919 Class := ANYOF_XDIGIT;
1922 Parse_Pos := Parse_Pos + 1;
1926 Class := ANYOF_NONE;
1930 if Class /= ANYOF_NONE then
1931 Parse_Pos := Parse_Pos + 7;
1935 Fail ("Invalid
character class
");
1943 end Parse_Posix_Character_Class;
1945 Expr_Flags : Expression_Flags;
1948 -- Start of processing for Compile
1952 Parse (False, Expr_Flags, Result);
1955 Fail ("Couldn
't compile expression
");
1958 Final_Code_Size := Emit_Ptr - 1;
1960 -- Do we want to actually compile the expression, or simply get the
1971 (Expression : String;
1972 Flags : Regexp_Flags := No_Flags)
1973 return Pattern_Matcher
1975 Size : Program_Size;
1976 Dummy : Pattern_Matcher (0);
1979 Compile (Dummy, Expression, Size, Flags);
1982 Result : Pattern_Matcher (Size);
1984 Compile (Result, Expression, Size, Flags);
1990 (Matcher : out Pattern_Matcher;
1991 Expression : String;
1992 Flags : Regexp_Flags := No_Flags)
1994 Size : Program_Size;
1997 Compile (Matcher, Expression, Size, Flags);
2004 procedure Dump (Self : Pattern_Matcher) is
2006 -- Index : Pointer := Program_First + 1;
2007 -- What is the above line for ???
2010 Program : Program_Data renames Self.Program;
2012 procedure Dump_Until
2015 Indent : Natural := 0);
2016 -- Dump the program until the node Till (not included) is met.
2017 -- Every line is indented with Index spaces at the beginning
2018 -- Dumps till the end if Till is 0.
2024 procedure Dump_Until
2027 Indent : Natural := 0)
2030 Index : Pointer := Start;
2031 Local_Indent : Natural := Indent;
2035 while Index < Till loop
2037 Op := Opcode'Val (Character'Pos ((Self.Program (Index))));
2040 Local_Indent := Local_Indent - 3;
2044 Point : String := Pointer'Image (Index);
2047 for J in 1 .. 6 - Point'Length loop
2053 & (1 .. Local_Indent => ' ')
2054 & Opcode'Image (Op));
2057 -- Print the parenthesis number
2059 if Op = OPEN or else Op = CLOSE or else Op = REFF then
2060 Put (Natural'Image (Character'Pos (Program (Index + 3))));
2063 Next := Index + Get_Next_Offset (Program, Index);
2065 if Next = Index then
2066 Put (" (next
at 0)");
2068 Put (" (next
at " & Pointer'Image (Next) & ")");
2073 -- Character class operand
2077 Bitmap : Character_Class;
2078 Last : Character := ASCII.Nul;
2079 Current : Natural := 0;
2081 Current_Char : Character;
2084 Bitmap_Operand (Program, Index, Bitmap);
2087 while Current <= 255 loop
2088 Current_Char := Character'Val (Current);
2090 -- First item in a range
2092 if Get_From_Class (Bitmap, Current_Char) then
2093 Last := Current_Char;
2095 -- Search for the last item in the range
2098 Current := Current + 1;
2099 exit when Current > 255;
2100 Current_Char := Character'Val (Current);
2102 not Get_From_Class (Bitmap, Current_Char);
2112 if Character'Succ (Last) /= Current_Char then
2113 Put ("-" & Character'Pred (Current_Char));
2117 Current := Current + 1;
2122 Index := Index + 3 + Bitmap'Length;
2127 when EXACT | EXACTF =>
2128 Length := String_Length (Program, Index);
2129 Put (" operand
(length
:" & Program_Size'Image (Length + 1)
2131 & String (Program (String_Operand (Index)
2132 .. String_Operand (Index)
2134 Index := String_Operand (Index) + Length + 1;
2141 Dump_Until (Index + 3, Next, Local_Indent + 3);
2147 -- Only one instruction
2149 Dump_Until (Index + 3, Index + 4, Local_Indent + 3);
2152 when CURLY | CURLYX =>
2154 & Natural'Image (Read_Natural (Program, Index + 3))
2156 & Natural'Image (Read_Natural (Program, Index + 5))
2159 Dump_Until (Index + 7, Next, Local_Indent + 3);
2165 Local_Indent := Local_Indent + 3;
2167 when CLOSE | REFF =>
2185 -- Start of processing for Dump
2188 pragma Assert (Self.Program (Program_First) = MAGIC,
2189 "Corrupted Pattern_Matcher
");
2191 Put_Line ("Must start
with (Self
.First
) = "
2192 & Character'Image (Self.First));
2194 if (Self.Flags and Case_Insensitive) /= 0 then
2195 Put_Line (" Case_Insensitive mode
");
2198 if (Self.Flags and Single_Line) /= 0 then
2199 Put_Line (" Single_Line mode
");
2202 if (Self.Flags and Multiple_Lines) /= 0 then
2203 Put_Line (" Multiple_Lines mode
");
2206 Put_Line (" 1 : MAGIC
");
2207 Dump_Until (Program_First + 1, Self.Program'Last + 1);
2210 --------------------
2211 -- Get_From_Class --
2212 --------------------
2214 function Get_From_Class
2215 (Bitmap : Character_Class;
2219 Value : constant Class_Byte := Character'Pos (C);
2222 return (Bitmap (Value / 8)
2223 and Bit_Conversion (Value mod 8)) /= 0;
2230 function Get_Next (Program : Program_Data; IP : Pointer) return Pointer is
2231 Offset : constant Pointer := Get_Next_Offset (Program, IP);
2241 ---------------------
2242 -- Get_Next_Offset --
2243 ---------------------
2245 function Get_Next_Offset
2246 (Program : Program_Data;
2251 return Pointer (Read_Natural (Program, IP + 1));
2252 end Get_Next_Offset;
2258 function Is_Alnum (C : Character) return Boolean is
2260 return Is_Alphanumeric (C) or else C = '_';
2267 function Is_Printable (C : Character) return Boolean is
2268 Value : constant Natural := Character'Pos (C);
2271 return (Value > 32 and then Value < 127)
2272 or else Is_Space (C);
2279 function Is_Space (C : Character) return Boolean is
2282 or else C = ASCII.HT
2283 or else C = ASCII.CR
2284 or else C = ASCII.LF
2285 or else C = ASCII.VT
2286 or else C = ASCII.FF;
2294 (Self : Pattern_Matcher;
2296 Matches : out Match_Array)
2298 Program : Program_Data renames Self.Program; -- Shorter notation
2300 -- Global work variables
2302 Input_Pos : Natural; -- String-input pointer
2303 BOL_Pos : Natural; -- Beginning of input, for ^ check
2304 Matched : Boolean := False; -- Until proven True
2306 Matches_Full : Match_Array (0 .. Natural'Max (Self.Paren_Count,
2308 -- Stores the value of all the parenthesis pairs.
2309 -- We do not use directly Matches, so that we can also use back
2310 -- references (REFF) even if Matches is too small.
2312 type Natural_Array is array (Match_Count range <>) of Natural;
2313 Matches_Tmp : Natural_Array (Matches_Full'Range);
2314 -- Save the opening position of parenthesis.
2316 Last_Paren : Natural := 0;
2317 -- Last parenthesis seen
2319 Greedy : Boolean := True;
2320 -- True if the next operator should be greedy
2322 type Current_Curly_Record;
2323 type Current_Curly_Access is access all Current_Curly_Record;
2324 type Current_Curly_Record is record
2325 Paren_Floor : Natural; -- How far back to strip parenthesis data
2326 Cur : Integer; -- How many instances of scan we've matched
2327 Min : Natural; -- Minimal number of scans to match
2328 Max : Natural; -- Maximal number of scans to match
2329 Greedy : Boolean; -- Whether to work our way up or down
2330 Scan : Pointer; -- The thing to match
2331 Next : Pointer; -- What has to match after it
2332 Lastloc : Natural; -- Where we started matching this scan
2333 Old_Cc : Current_Curly_Access; -- Before we started this one
2335 -- Data used to handle the curly operator and the plus and star
2336 -- operators for complex expressions.
2338 Current_Curly : Current_Curly_Access := null;
2339 -- The curly currently being processed.
2341 -----------------------
2342 -- Local Subprograms --
2343 -----------------------
2345 function Index (Start : Positive; C : Character) return Natural;
2346 -- Find character C in Data starting at Start and return position
2350 Max : Natural := Natural'Last)
2352 -- Repeatedly match something simple, report how many
2353 -- It only matches on things of length 1.
2354 -- Starting from Input_Pos, it matches at most Max CURLY.
2356 function Try (Pos : in Positive) return Boolean;
2357 -- Try to match at specific point
2359 function Match (IP : Pointer) return Boolean;
2360 -- This is the main matching routine. Conceptually the strategy
2361 -- is simple: check to see whether the current node matches,
2362 -- call self recursively to see whether the rest matches,
2363 -- and then act accordingly.
2365 -- In practice Match makes some effort to avoid recursion, in
2366 -- particular by going through "ordinary
" nodes (that don't
2367 -- need to know whether the rest of the match failed) by
2368 -- using a loop instead of recursion.
2370 function Match_Whilem (IP : Pointer) return Boolean;
2371 -- Return True if a WHILEM matches
2373 function Recurse_Match (IP : Pointer; From : Natural) return Boolean;
2374 pragma Inline (Recurse_Match);
2375 -- Calls Match recursively. It saves and restores the parenthesis
2376 -- status and location in the input stream correctly, so that
2377 -- backtracking is possible
2379 function Match_Simple_Operator
2385 -- Return True it the simple operator (possibly non-greedy) matches
2387 pragma Inline (Index);
2388 pragma Inline (Repeat);
2390 -- These are two complex functions, but used only once.
2392 pragma Inline (Match_Whilem);
2393 pragma Inline (Match_Simple_Operator);
2405 for J in Start .. Data'Last loop
2406 if Data (J) = C then
2418 function Recurse_Match (IP : Pointer; From : Natural) return Boolean is
2419 L : constant Natural := Last_Paren;
2420 Tmp_F : constant Match_Array :=
2421 Matches_Full (From + 1 .. Matches_Full'Last);
2422 Start : constant Natural_Array :=
2423 Matches_Tmp (From + 1 .. Matches_Tmp'Last);
2424 Input : constant Natural := Input_Pos;
2430 Matches_Full (Tmp_F'Range) := Tmp_F;
2431 Matches_Tmp (Start'Range) := Start;
2440 function Match (IP : Pointer) return Boolean is
2441 Scan : Pointer := IP;
2448 pragma Assert (Scan /= 0);
2450 -- Determine current opcode and count its usage in debug mode
2452 Op := Opcode'Val (Character'Pos (Program (Scan)));
2454 -- Calculate offset of next instruction.
2455 -- Second character is most significant in Program_Data.
2457 Next := Get_Next (Program, Scan);
2461 return True; -- Success !
2464 if Program (Next) /= BRANCH then
2465 Next := Operand (Scan); -- No choice, avoid recursion
2469 if Recurse_Match (Operand (Scan), 0) then
2473 Scan := Get_Next (Program, Scan);
2474 exit when Scan = 0 or Program (Scan) /= BRANCH;
2484 exit State_Machine when
2485 Input_Pos /= BOL_Pos
2486 and then ((Self.Flags and Multiple_Lines) = 0
2487 or else Data (Input_Pos - 1) /= ASCII.LF);
2490 exit State_Machine when
2491 Input_Pos /= BOL_Pos
2492 and then Data (Input_Pos - 1) /= ASCII.LF;
2495 exit State_Machine when Input_Pos /= BOL_Pos;
2498 exit State_Machine when
2499 Input_Pos <= Data'Last
2500 and then ((Self.Flags and Multiple_Lines) = 0
2501 or else Data (Input_Pos) /= ASCII.LF);
2504 exit State_Machine when
2505 Input_Pos <= Data'Last
2506 and then Data (Input_Pos) /= ASCII.LF;
2509 exit State_Machine when Input_Pos <= Data'Last;
2511 when BOUND | NBOUND =>
2513 -- Was last char in word ?
2516 N : Boolean := False;
2517 Ln : Boolean := False;
2520 if Input_Pos /= Data'First then
2521 N := Is_Alnum (Data (Input_Pos - 1));
2524 if Input_Pos > Data'Last then
2527 Ln := Is_Alnum (Data (Input_Pos));
2542 exit State_Machine when
2543 Input_Pos > Data'Last
2544 or else not Is_Space (Data (Input_Pos));
2545 Input_Pos := Input_Pos + 1;
2548 exit State_Machine when
2549 Input_Pos > Data'Last
2550 or else Is_Space (Data (Input_Pos));
2551 Input_Pos := Input_Pos + 1;
2554 exit State_Machine when
2555 Input_Pos > Data'Last
2556 or else not Is_Digit (Data (Input_Pos));
2557 Input_Pos := Input_Pos + 1;
2560 exit State_Machine when
2561 Input_Pos > Data'Last
2562 or else Is_Digit (Data (Input_Pos));
2563 Input_Pos := Input_Pos + 1;
2566 exit State_Machine when
2567 Input_Pos > Data'Last
2568 or else not Is_Alnum (Data (Input_Pos));
2569 Input_Pos := Input_Pos + 1;
2572 exit State_Machine when
2573 Input_Pos > Data'Last
2574 or else Is_Alnum (Data (Input_Pos));
2575 Input_Pos := Input_Pos + 1;
2578 exit State_Machine when Input_Pos > Data'Last
2579 or else Data (Input_Pos) = ASCII.LF;
2580 Input_Pos := Input_Pos + 1;
2583 exit State_Machine when Input_Pos > Data'Last;
2584 Input_Pos := Input_Pos + 1;
2588 Opnd : Pointer := String_Operand (Scan);
2589 Current : Positive := Input_Pos;
2590 Last : constant Pointer :=
2591 Opnd + String_Length (Program, Scan);
2594 while Opnd <= Last loop
2595 exit State_Machine when Current > Data'Last
2596 or else Program (Opnd) /= Data (Current);
2597 Current := Current + 1;
2601 Input_Pos := Current;
2606 Opnd : Pointer := String_Operand (Scan);
2607 Current : Positive := Input_Pos;
2608 Last : constant Pointer :=
2609 Opnd + String_Length (Program, Scan);
2612 while Opnd <= Last loop
2613 exit State_Machine when Current > Data'Last
2614 or else Program (Opnd) /= To_Lower (Data (Current));
2615 Current := Current + 1;
2619 Input_Pos := Current;
2624 Bitmap : Character_Class;
2627 Bitmap_Operand (Program, Scan, Bitmap);
2628 exit State_Machine when
2629 Input_Pos > Data'Last
2630 or else not Get_From_Class (Bitmap, Data (Input_Pos));
2631 Input_Pos := Input_Pos + 1;
2636 No : constant Natural :=
2637 Character'Pos (Program (Operand (Scan)));
2639 Matches_Tmp (No) := Input_Pos;
2644 No : constant Natural :=
2645 Character'Pos (Program (Operand (Scan)));
2647 Matches_Full (No) := (Matches_Tmp (No), Input_Pos - 1);
2648 if Last_Paren < No then
2655 No : constant Natural :=
2656 Character'Pos (Program (Operand (Scan)));
2660 -- If we haven't seen that parenthesis yet
2662 if Last_Paren < No then
2666 Data_Pos := Matches_Full (No).First;
2667 while Data_Pos <= Matches_Full (No).Last loop
2668 if Input_Pos > Data'Last
2669 or else Data (Input_Pos) /= Data (Data_Pos)
2674 Input_Pos := Input_Pos + 1;
2675 Data_Pos := Data_Pos + 1;
2682 when STAR | PLUS | CURLY =>
2684 Greed : constant Boolean := Greedy;
2687 return Match_Simple_Operator (Op, Scan, Next, Greed);
2692 -- Looking at something like:
2693 -- 1: CURLYX {n,m} (->4)
2694 -- 2: code for complex thing (->3)
2699 Cc : aliased Current_Curly_Record;
2700 Min : Natural := Read_Natural (Program, Scan + 3);
2701 Max : Natural := Read_Natural (Program, Scan + 5);
2703 Has_Match : Boolean;
2706 Cc := (Paren_Floor => Last_Paren,
2714 Old_Cc => Current_Curly);
2715 Current_Curly := Cc'Unchecked_Access;
2717 Has_Match := Match (Next - 3);
2719 -- Start on the WHILEM
2721 Current_Curly := Cc.Old_Cc;
2726 return Match_Whilem (IP);
2729 raise Expression_Error; -- Invalid instruction
2733 end loop State_Machine;
2735 -- If we get here, there is no match.
2736 -- For successful matches when EOP is the terminating point.
2741 ---------------------------
2742 -- Match_Simple_Operator --
2743 ---------------------------
2745 function Match_Simple_Operator
2752 Next_Char : Character := ASCII.Nul;
2753 Next_Char_Known : Boolean := False;
2754 No : Integer; -- Can be negative
2756 Max : Natural := Natural'Last;
2757 Operand_Code : Pointer;
2760 Save : Natural := Input_Pos;
2763 -- Lookahead to avoid useless match attempts
2764 -- when we know what character comes next.
2766 if Program (Next) = EXACT then
2767 Next_Char := Program (String_Operand (Next));
2768 Next_Char_Known := True;
2771 -- Find the minimal and maximal values for the operator
2776 Operand_Code := Operand (Scan);
2780 Operand_Code := Operand (Scan);
2783 Min := Read_Natural (Program, Scan + 3);
2784 Max := Read_Natural (Program, Scan + 5);
2785 Operand_Code := Scan + 7;
2788 -- Non greedy operators
2791 -- Test the minimal repetitions
2794 and then Repeat (Operand_Code, Min) < Min
2801 -- Find the place where 'next' could work
2803 if Next_Char_Known then
2804 -- Last position to check
2806 Last_Pos := Input_Pos + Max;
2808 if Last_Pos > Data'Last
2809 or else Max = Natural'Last
2811 Last_Pos := Data'Last;
2814 -- Look for the first possible opportunity
2817 -- Find the next possible position
2819 while Input_Pos <= Last_Pos
2820 and then Data (Input_Pos) /= Next_Char
2822 Input_Pos := Input_Pos + 1;
2825 if Input_Pos > Last_Pos then
2829 -- Check that we still match if we stop
2830 -- at the position we just found.
2833 Num : constant Natural := Input_Pos - Old;
2838 if Repeat (Operand_Code, Num) < Num then
2843 -- Input_Pos now points to the new position
2845 if Match (Get_Next (Program, Scan)) then
2850 Input_Pos := Input_Pos + 1;
2853 -- We know what the next character is
2856 while Max >= Min loop
2858 -- If the next character matches
2860 if Match (Next) then
2864 Input_Pos := Save + Min;
2866 -- Could not or did not match -- move forward
2868 if Repeat (Operand_Code, 1) /= 0 then
2881 No := Repeat (Operand_Code, Max);
2883 -- ??? Perl has some special code here in case the
2884 -- next instruction is of type EOL, since $ and \Z
2885 -- can match before *and* after newline at the end.
2887 -- ??? Perl has some special code here in case (paren)
2890 -- Else, if we don't have any parenthesis
2892 while No >= Min loop
2893 if not Next_Char_Known
2894 or else (Input_Pos <= Data'Last
2895 and then Data (Input_Pos) = Next_Char)
2897 if Match (Next) then
2902 -- Could not or did not work, we back up
2905 Input_Pos := Save + No;
2909 end Match_Simple_Operator;
2915 -- This is really hard to understand, because after we match what we're
2916 -- trying to match, we must make sure the rest of the REx is going to
2917 -- match for sure, and to do that we have to go back UP the parse tree
2918 -- by recursing ever deeper. And if it fails, we have to reset our
2919 -- parent's current state that we can try again after backing off.
2921 function Match_Whilem (IP : Pointer) return Boolean is
2922 pragma Warnings (Off, IP);
2924 Cc : Current_Curly_Access := Current_Curly;
2925 N : Natural := Cc.Cur + 1;
2928 Lastloc : Natural := Cc.Lastloc;
2929 -- Detection of 0-len.
2932 -- If degenerate scan matches "", assume scan done.
2934 if Input_Pos = Cc.Lastloc
2935 and then N >= Cc.Min
2937 -- Temporarily restore the old context, and check that we
2938 -- match was comes after CURLYX.
2940 Current_Curly := Cc.Old_Cc;
2942 if Current_Curly /= null then
2943 Ln := Current_Curly.Cur;
2946 if Match (Cc.Next) then
2950 if Current_Curly /= null then
2951 Current_Curly.Cur := Ln;
2954 Current_Curly := Cc;
2958 -- First, just match a string of min scans.
2962 Cc.Lastloc := Input_Pos;
2964 if Match (Cc.Scan) then
2969 Cc.Lastloc := Lastloc;
2973 -- Prefer next over scan for minimal matching.
2975 if not Cc.Greedy then
2976 Current_Curly := Cc.Old_Cc;
2978 if Current_Curly /= null then
2979 Ln := Current_Curly.Cur;
2982 if Recurse_Match (Cc.Next, Cc.Paren_Floor) then
2986 if Current_Curly /= null then
2987 Current_Curly.Cur := Ln;
2990 Current_Curly := Cc;
2992 -- Maximum greed exceeded ?
2998 -- Try scanning more and see if it helps
3000 Cc.Lastloc := Input_Pos;
3002 if Recurse_Match (Cc.Scan, Cc.Paren_Floor) then
3007 Cc.Lastloc := Lastloc;
3011 -- Prefer scan over next for maximal matching
3013 if N < Cc.Max then -- more greed allowed ?
3015 Cc.Lastloc := Input_Pos;
3017 if Recurse_Match (Cc.Scan, Cc.Paren_Floor) then
3022 -- Failed deeper matches of scan, so see if this one works
3024 Current_Curly := Cc.Old_Cc;
3026 if Current_Curly /= null then
3027 Ln := Current_Curly.Cur;
3030 if Match (Cc.Next) then
3034 if Current_Curly /= null then
3035 Current_Curly.Cur := Ln;
3038 Current_Curly := Cc;
3040 Cc.Lastloc := Lastloc;
3050 Max : Natural := Natural'Last)
3053 Scan : Natural := Input_Pos;
3055 Op : constant Opcode := Opcode'Val (Character'Pos (Program (IP)));
3058 Is_First : Boolean := True;
3059 Bitmap : Character_Class;
3062 if Max = Natural'Last or else Scan + Max - 1 > Data'Last then
3065 Last := Scan + Max - 1;
3071 and then Data (Scan) /= ASCII.LF
3081 -- The string has only one character if Repeat was called
3083 C := Program (String_Operand (IP));
3085 and then C = Data (Scan)
3092 -- The string has only one character if Repeat was called
3094 C := Program (String_Operand (IP));
3096 and then To_Lower (C) = Data (Scan)
3103 Bitmap_Operand (Program, IP, Bitmap);
3108 and then Get_From_Class (Bitmap, Data (Scan))
3115 and then Is_Alnum (Data (Scan))
3122 and then not Is_Alnum (Data (Scan))
3129 and then Is_Space (Data (Scan))
3136 and then not Is_Space (Data (Scan))
3143 and then Is_Digit (Data (Scan))
3150 and then not Is_Digit (Data (Scan))
3156 raise Program_Error;
3159 Count := Scan - Input_Pos;
3168 function Try (Pos : in Positive) return Boolean is
3172 Matches_Full := (others => No_Match);
3174 if Match (Program_First + 1) then
3175 Matches_Full (0) := (Pos, Input_Pos - 1);
3182 -- Start of processing for Match
3185 -- Do we have the regexp Never_Match?
3187 if Self.Size = 0 then
3188 Matches (0) := No_Match;
3192 -- Check validity of program
3195 (Program (Program_First) = MAGIC,
3196 "Corrupted Pattern_Matcher
");
3198 -- If there is a "must appear
" string, look for it
3200 if Self.Must_Have_Length > 0 then
3202 First : constant Character := Program (Self.Must_Have);
3203 Must_First : constant Pointer := Self.Must_Have;
3204 Must_Last : constant Pointer :=
3205 Must_First + Pointer (Self.Must_Have_Length - 1);
3206 Next_Try : Natural := Index (Data'First, First);
3210 and then Data (Next_Try .. Next_Try + Self.Must_Have_Length - 1)
3211 = String (Program (Must_First .. Must_Last))
3213 Next_Try := Index (Next_Try + 1, First);
3216 if Next_Try = 0 then
3217 Matches_Full := (others => No_Match);
3218 return; -- Not present
3223 -- Mark beginning of line for ^
3225 BOL_Pos := Data'First;
3227 -- Simplest case first: an anchored match need be tried only once
3229 if Self.Anchored and then (Self.Flags and Multiple_Lines) = 0 then
3230 Matched := Try (Data'First);
3232 elsif Self.Anchored then
3234 Next_Try : Natural := Data'First;
3236 -- Test the first position in the buffer
3237 Matched := Try (Next_Try);
3239 -- Else only test after newlines
3242 while Next_Try <= Data'Last loop
3243 while Next_Try <= Data'Last
3244 and then Data (Next_Try) /= ASCII.LF
3246 Next_Try := Next_Try + 1;
3249 Next_Try := Next_Try + 1;
3251 if Next_Try <= Data'Last then
3252 Matched := Try (Next_Try);
3259 elsif Self.First /= ASCII.NUL then
3261 -- We know what char it must start with
3264 Next_Try : Natural := Index (Data'First, Self.First);
3267 while Next_Try /= 0 loop
3268 Matched := Try (Next_Try);
3270 Next_Try := Index (Next_Try + 1, Self.First);
3275 -- Messy cases: try all locations (including for the empty string)
3277 Matched := Try (Data'First);
3280 for S in Data'First + 1 .. Data'Last loop
3287 -- Matched has its value
3289 for J in Last_Paren + 1 .. Matches'Last loop
3290 Matches_Full (J) := No_Match;
3293 Matches := Matches_Full (Matches'Range);
3298 (Self : Pattern_Matcher;
3302 Matches : Match_Array (0 .. 0);
3305 Match (Self, Data, Matches);
3306 if Matches (0) = No_Match then
3307 return Data'First - 1;
3309 return Matches (0).First;
3314 (Expression : String;
3316 Matches : out Match_Array;
3317 Size : Program_Size := 0)
3319 PM : Pattern_Matcher (Size);
3320 Finalize_Size : Program_Size;
3324 Match (Compile (Expression), Data, Matches);
3326 Compile (PM, Expression, Finalize_Size);
3327 Match (PM, Data, Matches);
3332 (Expression : String;
3334 Size : Program_Size := 0)
3337 PM : Pattern_Matcher (Size);
3338 Final_Size : Program_Size; -- unused
3342 return Match (Compile (Expression), Data);
3344 Compile (PM, Expression, Final_Size);
3345 return Match (PM, Data);
3350 (Expression : String;
3352 Size : Program_Size := 0)
3355 Matches : Match_Array (0 .. 0);
3356 PM : Pattern_Matcher (Size);
3357 Final_Size : Program_Size; -- unused
3361 Match (Compile (Expression), Data, Matches);
3363 Compile (PM, Expression, Final_Size);
3364 Match (PM, Data, Matches);
3367 return Matches (0).First >= Data'First;
3374 function Operand (P : Pointer) return Pointer is
3383 procedure Optimize (Self : in out Pattern_Matcher) is
3384 Max_Length : Program_Size;
3385 This_Length : Program_Size;
3388 Program : Program_Data renames Self.Program;
3391 -- Start with safe defaults (no optimization):
3392 -- * No known first character of match
3393 -- * Does not necessarily start at beginning of line
3394 -- * No string known that has to appear in data
3396 Self.First := ASCII.NUL;
3397 Self.Anchored := False;
3398 Self.Must_Have := Program'Last + 1;
3399 Self.Must_Have_Length := 0;
3401 Scan := Program_First + 1; -- First instruction (can be anything)
3403 if Program (Scan) = EXACT then
3404 Self.First := Program (String_Operand (Scan));
3406 elsif Program (Scan) = BOL
3407 or else Program (Scan) = SBOL
3408 or else Program (Scan) = MBOL
3410 Self.Anchored := True;
3413 -- If there's something expensive in the regexp, find the
3414 -- longest literal string that must appear and make it the
3415 -- regmust. Resolve ties in favor of later strings, since
3416 -- the regstart check works with the beginning of the regexp.
3417 -- and avoiding duplication strengthens checking. Not a
3418 -- strong reason, but sufficient in the absence of others.
3420 if False then -- if Flags.SP_Start then ???
3423 while Scan /= 0 loop
3424 if Program (Scan) = EXACT or else Program (Scan) = EXACTF then
3425 This_Length := String_Length (Program, Scan);
3427 if This_Length >= Max_Length then
3428 Longest := String_Operand (Scan);
3429 Max_Length := This_Length;
3433 Scan := Get_Next (Program, Scan);
3436 Self.Must_Have := Longest;
3437 Self.Must_Have_Length := Natural (Max_Length) + 1;
3445 function Paren_Count (Regexp : Pattern_Matcher) return Match_Count is
3447 return Regexp.Paren_Count;
3454 function Quote (Str : String) return String is
3455 S : String (1 .. Str'Length * 2);
3456 Last : Natural := 0;
3459 for J in Str'Range loop
3461 when '^' | '$' | '|' | '*' | '+' | '?' | '{'
3462 | '}' | '[' | ']' | '(' | ')' | '\' =>
3464 S (Last + 1) := '\';
3465 S (Last + 2) := Str (J);
3469 S (Last + 1) := Str (J);
3474 return S (1 .. Last);
3481 function Read_Natural
3482 (Program : Program_Data;
3487 return Character'Pos (Program (IP)) +
3488 256 * Character'Pos (Program (IP + 1));
3495 procedure Reset_Class (Bitmap : in out Character_Class) is
3497 Bitmap := (others => 0);
3504 procedure Set_In_Class
3505 (Bitmap : in out Character_Class;
3508 Value : constant Class_Byte := Character'Pos (C);
3511 Bitmap (Value / 8) := Bitmap (Value / 8)
3512 or Bit_Conversion (Value mod 8);
3519 function String_Length
3520 (Program : Program_Data;
3525 pragma Assert (Program (P) = EXACT or else Program (P) = EXACTF);
3526 return Character'Pos (Program (P + 3));
3529 --------------------
3530 -- String_Operand --
3531 --------------------
3533 function String_Operand (P : Pointer) return Pointer is