1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- G N A T . R E G P A T --
9 -- Copyright (C) 1986 by University of Toronto. --
10 -- Copyright (C) 1999-2016, AdaCore --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 3, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. --
19 -- As a special exception under Section 7 of GPL version 3, you are granted --
20 -- additional permissions described in the GCC Runtime Library Exception, --
21 -- version 3.1, as published by the Free Software Foundation. --
23 -- You should have received a copy of the GNU General Public License and --
24 -- a copy of the GCC Runtime Library Exception along with this program; --
25 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
26 -- <http://www.gnu.org/licenses/>. --
28 -- GNAT was originally developed by the GNAT team at New York University. --
29 -- Extensive contributions were provided by Ada Core Technologies Inc. --
31 ------------------------------------------------------------------------------
33 -- This is an altered Ada 95 version of the original V8 style regular
34 -- expression library written in C by Henry Spencer. Apart from the
35 -- translation to Ada, the interface has been considerably changed to
36 -- use the Ada String type instead of C-style nul-terminated strings.
38 -- Beware that some of this code is subtly aware of the way operator
39 -- precedence is structured in regular expressions. Serious changes in
40 -- regular-expression syntax might require a total rethink.
42 with System
.IO
; use System
.IO
;
43 with Ada
.Characters
.Handling
; use Ada
.Characters
.Handling
;
44 with Ada
.Unchecked_Conversion
;
46 package body System
.Regpat
is
48 Debug
: constant Boolean := False;
49 -- Set to True to activate debug traces. This is normally set to constant
50 -- False to simply delete all the trace code. It is to be edited to True
51 -- for internal debugging of the package.
53 ----------------------------
54 -- Implementation details --
55 ----------------------------
57 -- This is essentially a linear encoding of a nondeterministic
58 -- finite-state machine, also known as syntax charts or
59 -- "railroad normal form" in parsing technology.
61 -- Each node is an opcode plus a "next" pointer, possibly plus an
62 -- operand. "Next" pointers of all nodes except BRANCH implement
63 -- concatenation; a "next" pointer with a BRANCH on both ends of it
64 -- is connecting two alternatives.
66 -- The operand of some types of node is a literal string; for others,
67 -- it is a node leading into a sub-FSM. In particular, the operand of
68 -- a BRANCH node is the first node of the branch.
69 -- (NB this is *not* a tree structure: the tail of the branch connects
70 -- to the thing following the set of BRANCHes).
72 -- You can see the exact byte-compiled version by using the Dump
73 -- subprogram. However, here are a few examples:
75 -- (a|b): 1 : BRANCH (next at 9)
76 -- 4 : EXACT (next at 17) operand=a
77 -- 9 : BRANCH (next at 17)
78 -- 12 : EXACT (next at 17) operand=b
79 -- 17 : EOP (next at 0)
81 -- (ab)*: 1 : CURLYX (next at 25) { 0, 32767}
82 -- 8 : OPEN 1 (next at 12)
83 -- 12 : EXACT (next at 18) operand=ab
84 -- 18 : CLOSE 1 (next at 22)
85 -- 22 : WHILEM (next at 0)
86 -- 25 : NOTHING (next at 28)
87 -- 28 : EOP (next at 0)
93 -- Name Operand? Meaning
95 (EOP
, -- no End of program
96 MINMOD
, -- no Next operator is not greedy
98 -- Classes of characters
100 ANY
, -- no Match any one character except newline
101 SANY
, -- no Match any character, including new line
102 ANYOF
, -- class Match any character in this class
103 EXACT
, -- str Match this string exactly
104 EXACTF
, -- str Match this string (case-folding is one)
105 NOTHING
, -- no Match empty string
106 SPACE
, -- no Match any whitespace character
107 NSPACE
, -- no Match any non-whitespace character
108 DIGIT
, -- no Match any numeric character
109 NDIGIT
, -- no Match any non-numeric character
110 ALNUM
, -- no Match any alphanumeric character
111 NALNUM
, -- no Match any non-alphanumeric character
115 BRANCH
, -- node Match this alternative, or the next
117 -- Simple loops (when the following node is one character in length)
119 STAR
, -- node Match this simple thing 0 or more times
120 PLUS
, -- node Match this simple thing 1 or more times
121 CURLY
, -- 2num node Match this simple thing between n and m times.
125 CURLYX
, -- 2num node Match this complex thing {n,m} times
126 -- The nums are coded on two characters each
128 WHILEM
, -- no Do curly processing and see if rest matches
130 -- Matches after or before a word
132 BOL
, -- no Match "" at beginning of line
133 MBOL
, -- no Same, assuming multiline (match after \n)
134 SBOL
, -- no Same, assuming single line (don't match at \n)
135 EOL
, -- no Match "" at end of line
136 MEOL
, -- no Same, assuming multiline (match before \n)
137 SEOL
, -- no Same, assuming single line (don't match at \n)
139 BOUND
, -- no Match "" at any word boundary
140 NBOUND
, -- no Match "" at any word non-boundary
142 -- Parenthesis groups handling
144 REFF
, -- num Match some already matched string, folded
145 OPEN
, -- num Mark this point in input as start of #n
146 CLOSE
); -- num Analogous to OPEN
148 for Opcode
'Size use 8;
153 -- The set of branches constituting a single choice are hooked
154 -- together with their "next" pointers, since precedence prevents
155 -- anything being concatenated to any individual branch. The
156 -- "next" pointer of the last BRANCH in a choice points to the
157 -- thing following the whole choice. This is also where the
158 -- final "next" pointer of each individual branch points; each
159 -- branch starts with the operand node of a BRANCH node.
162 -- '?', and complex '*' and '+', are implemented with CURLYX.
163 -- branches. Simple cases (one character per match) are implemented with
164 -- STAR and PLUS for speed and to minimize recursive plunges.
167 -- ...are numbered at compile time.
170 -- There are in fact two arguments, the first one is the length (minus
171 -- one of the string argument), coded on one character, the second
172 -- argument is the string itself, coded on length + 1 characters.
174 -- A node is one char of opcode followed by two chars of "next" pointer.
175 -- "Next" pointers are stored as two 8-bit pieces, high order first. The
176 -- value is a positive offset from the opcode of the node containing it.
177 -- An operand, if any, simply follows the node. (Note that much of the
178 -- code generation knows about this implicit relationship.)
180 -- Using two bytes for the "next" pointer is vast overkill for most
181 -- things, but allows patterns to get big without disasters.
183 Next_Pointer_Bytes
: constant := 3;
184 -- Points after the "next pointer" data. An instruction is therefore:
185 -- 1 byte: instruction opcode
186 -- 2 bytes: pointer to next instruction
187 -- * bytes: optional data for the instruction
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
;
240 C
: Character) return Boolean;
241 -- Return True if the entry is set for C in the class Bitmap
243 procedure Reset_Class
(Bitmap
: out Character_Class
);
244 -- Clear all the entries in the class Bitmap
246 pragma Inline
(Set_In_Class
);
247 pragma Inline
(Get_From_Class
);
248 pragma Inline
(Reset_Class
);
250 -----------------------
251 -- Local Subprograms --
252 -----------------------
254 function "=" (Left
: Character; Right
: Opcode
) return Boolean;
256 function Is_Alnum
(C
: Character) return Boolean;
257 -- Return True if C is an alphanum character or an underscore ('_')
259 function Is_White_Space
(C
: Character) return Boolean;
260 -- Return True if C is a whitespace character
262 function Is_Printable
(C
: Character) return Boolean;
263 -- Return True if C is a printable character
265 function Operand
(P
: Pointer
) return Pointer
;
266 -- Return a pointer to the first operand of the node at P
268 function String_Length
269 (Program
: Program_Data
;
270 P
: Pointer
) return Program_Size
;
271 -- Return the length of the string argument of the node at P
273 function String_Operand
(P
: Pointer
) return Pointer
;
274 -- Return a pointer to the string argument of the node at P
276 procedure Bitmap_Operand
277 (Program
: Program_Data
;
279 Op
: out Character_Class
);
280 -- Return a pointer to the string argument of the node at P
283 (Program
: Program_Data
;
284 IP
: Pointer
) return Pointer
;
285 -- Dig the next instruction pointer out of a node
287 procedure Optimize
(Self
: in out Pattern_Matcher
);
288 -- Optimize a Pattern_Matcher by noting certain special cases
290 function Read_Natural
291 (Program
: Program_Data
;
292 IP
: Pointer
) return Natural;
293 -- Return the 2-byte natural coded at position IP
295 -- All of the subprograms above are tiny and should be inlined
298 pragma Inline
(Is_Alnum
);
299 pragma Inline
(Is_White_Space
);
300 pragma Inline
(Get_Next
);
301 pragma Inline
(Operand
);
302 pragma Inline
(Read_Natural
);
303 pragma Inline
(String_Length
);
304 pragma Inline
(String_Operand
);
306 type Expression_Flags
is record
307 Has_Width
, -- Known never to match null string
308 Simple
, -- Simple enough to be STAR/PLUS operand
309 SP_Start
: Boolean; -- Starts with * or +
312 Worst_Expression
: constant Expression_Flags
:= (others => False);
316 (Program
: Program_Data
;
317 Index
: in out Pointer
;
320 Do_Print
: Boolean := True);
321 -- Dump the program until the node Till (not included) is met. Every line
322 -- is indented with Index spaces at the beginning Dumps till the end if
325 procedure Dump_Operation
326 (Program
: Program_Data
;
329 -- Same as above, but only dumps a single operation, and compute its
330 -- indentation from the program.
336 function "=" (Left
: Character; Right
: Opcode
) return Boolean is
338 return Character'Pos (Left
) = Opcode
'Pos (Right
);
345 procedure Bitmap_Operand
346 (Program
: Program_Data
;
348 Op
: out Character_Class
)
350 function Convert
is new Ada
.Unchecked_Conversion
351 (Program_Data
, Character_Class
);
354 Op
(0 .. 31) := Convert
(Program
(P
+ Next_Pointer_Bytes
.. P
+ 34));
362 (Matcher
: out Pattern_Matcher
;
364 Final_Code_Size
: out Program_Size
;
365 Flags
: Regexp_Flags
:= No_Flags
)
367 -- We can't allocate space until we know how big the compiled form
368 -- will be, but we can't compile it (and thus know how big it is)
369 -- until we've got a place to put the code. So we cheat: we compile
370 -- it twice, once with code generation turned off and size counting
371 -- turned on, and once "for real".
373 -- This also means that we don't allocate space until we are sure
374 -- that the thing really will compile successfully, and we never
375 -- have to move the code and thus invalidate pointers into it.
377 -- Beware that the optimization-preparation code in here knows
378 -- about some of the structure of the compiled regexp.
380 PM
: Pattern_Matcher
renames Matcher
;
381 Program
: Program_Data
renames PM
.Program
;
383 Emit_Ptr
: Pointer
:= Program_First
;
385 Parse_Pos
: Natural := Expression
'First; -- Input-scan pointer
386 Parse_End
: constant Natural := Expression
'Last;
388 ----------------------------
389 -- Subprograms for Create --
390 ----------------------------
392 procedure Emit
(B
: Character);
393 -- Output the Character B to the Program. If code-generation is
394 -- disabled, simply increments the program counter.
396 function Emit_Node
(Op
: Opcode
) return Pointer
;
397 -- If code-generation is enabled, Emit_Node outputs the
398 -- opcode Op and reserves space for a pointer to the next node.
399 -- Return value is the location of new opcode, i.e. old Emit_Ptr.
401 procedure Emit_Natural
(IP
: Pointer
; N
: Natural);
402 -- Split N on two characters at position IP
404 procedure Emit_Class
(Bitmap
: Character_Class
);
405 -- Emits a character class
407 procedure Case_Emit
(C
: Character);
408 -- Emit C, after converting is to lower-case if the regular
409 -- expression is case insensitive.
412 (Parenthesized
: Boolean;
414 Flags
: out Expression_Flags
;
416 -- Parse regular expression, i.e. main body or parenthesized thing.
417 -- Caller must absorb opening parenthesis. Capturing should be set to
418 -- True when we have an open parenthesis from which we want the user
421 procedure Parse_Branch
422 (Flags
: out Expression_Flags
;
425 -- Implements the concatenation operator and handles '|'.
426 -- First should be true if this is the first item of the alternative.
428 procedure Parse_Piece
429 (Expr_Flags
: out Expression_Flags
;
431 -- Parse something followed by possible [*+?]
434 (Expr_Flags
: out Expression_Flags
;
436 -- Parse_Atom is the lowest level parse procedure.
438 -- Optimization: Gobbles an entire sequence of ordinary characters so
439 -- that it can turn them into a single node, which is smaller to store
440 -- and faster to run. Backslashed characters are exceptions, each
441 -- becoming a separate node; the code is simpler that way and it's
444 procedure Insert_Operator
447 Greedy
: Boolean := True);
448 -- Insert_Operator inserts an operator in front of an already-emitted
449 -- operand and relocates the operand. This applies to PLUS and STAR.
450 -- If Minmod is True, then the operator is non-greedy.
452 function Insert_Operator_Before
456 Opsize
: Pointer
) return Pointer
;
457 -- Insert an operator before Operand (and move the latter forward in the
458 -- program). Opsize is the size needed to represent the operator. This
459 -- returns the position at which the operator was inserted, and moves
460 -- Emit_Ptr after the new position of the operand.
462 procedure Insert_Curly_Operator
467 Greedy
: Boolean := True);
468 -- Insert an operator for CURLY ({Min}, {Min,} or {Min,Max}).
469 -- If Minmod is True, then the operator is non-greedy.
471 procedure Link_Tail
(P
, Val
: Pointer
);
472 -- Link_Tail sets the next-pointer at the end of a node chain
474 procedure Link_Operand_Tail
(P
, Val
: Pointer
);
475 -- Link_Tail on operand of first argument; noop if operand-less
477 procedure Fail
(M
: String);
478 pragma No_Return
(Fail
);
479 -- Fail with a diagnostic message, if possible
481 function Is_Curly_Operator
(IP
: Natural) return Boolean;
482 -- Return True if IP is looking at a '{' that is the beginning
483 -- of a curly operator, i.e. it matches {\d+,?\d*}
485 function Is_Mult
(IP
: Natural) return Boolean;
486 -- Return True if C is a regexp multiplier: '+', '*' or '?'
488 procedure Get_Curly_Arguments
492 Greedy
: out Boolean);
493 -- Parse the argument list for a curly operator.
494 -- It is assumed that IP is indeed pointing at a valid operator.
495 -- So what is IP and how come IP is not referenced in the body ???
497 procedure Parse_Character_Class
(IP
: out Pointer
);
498 -- Parse a character class.
499 -- The calling subprogram should consume the opening '[' before.
501 procedure Parse_Literal
502 (Expr_Flags
: out Expression_Flags
;
504 -- Parse_Literal encodes a string of characters to be matched exactly
506 function Parse_Posix_Character_Class
return Std_Class
;
507 -- Parse a posix character class, like [:alpha:] or [:^alpha:].
508 -- The caller is supposed to absorb the opening [.
510 pragma Inline
(Is_Mult
);
511 pragma Inline
(Emit_Natural
);
512 pragma Inline
(Parse_Character_Class
); -- since used only once
518 procedure Case_Emit
(C
: Character) is
520 if (Flags
and Case_Insensitive
) /= 0 then
524 -- Dump current character
534 procedure Emit
(B
: Character) is
536 if Emit_Ptr
<= PM
.Size
then
537 Program
(Emit_Ptr
) := B
;
540 Emit_Ptr
:= Emit_Ptr
+ 1;
547 procedure Emit_Class
(Bitmap
: Character_Class
) is
548 subtype Program31
is Program_Data
(0 .. 31);
550 function Convert
is new Ada
.Unchecked_Conversion
551 (Character_Class
, Program31
);
554 -- What is the mysterious constant 31 here??? Can't it be expressed
555 -- symbolically (size of integer - 1 or some such???). In any case
556 -- it should be declared as a constant (and referenced presumably
557 -- as this constant + 1 below.
559 if Emit_Ptr
+ 31 <= PM
.Size
then
560 Program
(Emit_Ptr
.. Emit_Ptr
+ 31) := Convert
(Bitmap
);
563 Emit_Ptr
:= Emit_Ptr
+ 32;
570 procedure Emit_Natural
(IP
: Pointer
; N
: Natural) is
572 if IP
+ 1 <= PM
.Size
then
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
;
586 if Emit_Ptr
+ 2 <= PM
.Size
then
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
+ Next_Pointer_Bytes
;
600 procedure Fail
(M
: String) is
602 raise Expression_Error
with M
;
605 -------------------------
606 -- Get_Curly_Arguments --
607 -------------------------
609 procedure Get_Curly_Arguments
613 Greedy
: out Boolean)
615 pragma Unreferenced
(IP
);
617 Save_Pos
: Natural := Parse_Pos
+ 1;
621 Max
:= Max_Curly_Repeat
;
623 while Expression
(Parse_Pos
) /= '}'
624 and then Expression
(Parse_Pos
) /= ','
626 Parse_Pos
:= Parse_Pos
+ 1;
629 Min
:= Natural'Value (Expression
(Save_Pos
.. Parse_Pos
- 1));
631 if Expression
(Parse_Pos
) = ',' then
632 Save_Pos
:= Parse_Pos
+ 1;
633 while Expression
(Parse_Pos
) /= '}' loop
634 Parse_Pos
:= Parse_Pos
+ 1;
637 if Save_Pos
/= Parse_Pos
then
638 Max
:= Natural'Value (Expression
(Save_Pos
.. Parse_Pos
- 1));
645 if Parse_Pos
< Expression
'Last
646 and then Expression
(Parse_Pos
+ 1) = '?'
649 Parse_Pos
:= Parse_Pos
+ 1;
654 end Get_Curly_Arguments
;
656 ---------------------------
657 -- Insert_Curly_Operator --
658 ---------------------------
660 procedure Insert_Curly_Operator
665 Greedy
: Boolean := True)
669 Old
:= Insert_Operator_Before
(Op
, Operand
, Greedy
, Opsize
=> 7);
670 Emit_Natural
(Old
+ Next_Pointer_Bytes
, Min
);
671 Emit_Natural
(Old
+ Next_Pointer_Bytes
+ 2, Max
);
672 end Insert_Curly_Operator
;
674 ----------------------------
675 -- Insert_Operator_Before --
676 ----------------------------
678 function Insert_Operator_Before
682 Opsize
: Pointer
) return Pointer
684 Dest
: constant Pointer
:= Emit_Ptr
;
686 Size
: Pointer
:= Opsize
;
689 -- If not greedy, we have to emit another opcode first
692 Size
:= Size
+ Next_Pointer_Bytes
;
695 -- Move the operand in the byte-compilation, so that we can insert
696 -- the operator before it.
698 if Emit_Ptr
+ Size
<= PM
.Size
then
699 Program
(Operand
+ Size
.. Emit_Ptr
+ Size
) :=
700 Program
(Operand
.. Emit_Ptr
);
703 -- Insert the operator at the position previously occupied by the
709 Old
:= Emit_Node
(MINMOD
);
710 Link_Tail
(Old
, Old
+ Next_Pointer_Bytes
);
713 Old
:= Emit_Node
(Op
);
714 Emit_Ptr
:= Dest
+ Size
;
716 end Insert_Operator_Before
;
718 ---------------------
719 -- Insert_Operator --
720 ---------------------
722 procedure Insert_Operator
725 Greedy
: Boolean := True)
728 pragma Warnings
(Off
, Discard
);
730 Discard
:= Insert_Operator_Before
731 (Op
, Operand
, Greedy
, Opsize
=> Next_Pointer_Bytes
);
734 -----------------------
735 -- Is_Curly_Operator --
736 -----------------------
738 function Is_Curly_Operator
(IP
: Natural) return Boolean is
739 Scan
: Natural := IP
;
742 if Expression
(Scan
) /= '{'
743 or else Scan
+ 2 > Expression
'Last
744 or else not Is_Digit
(Expression
(Scan
+ 1))
756 if Scan
> Expression
'Last then
760 exit when not Is_Digit
(Expression
(Scan
));
763 if Expression
(Scan
) = ',' then
767 if Scan
> Expression
'Last then
771 exit when not Is_Digit
(Expression
(Scan
));
775 return Expression
(Scan
) = '}';
776 end Is_Curly_Operator
;
782 function Is_Mult
(IP
: Natural) return Boolean is
783 C
: constant Character := Expression
(IP
);
789 or else (C
= '{' and then Is_Curly_Operator
(IP
));
792 -----------------------
793 -- Link_Operand_Tail --
794 -----------------------
796 procedure Link_Operand_Tail
(P
, Val
: Pointer
) is
798 if P
<= PM
.Size
and then Program
(P
) = BRANCH
then
799 Link_Tail
(Operand
(P
), Val
);
801 end Link_Operand_Tail
;
807 procedure Link_Tail
(P
, Val
: Pointer
) is
813 -- Find last node (the size of the pattern matcher might be too
814 -- small, so don't try to read past its end).
817 while Scan
+ Next_Pointer_Bytes
<= PM
.Size
loop
818 Temp
:= Get_Next
(Program
, Scan
);
819 exit when Temp
= Scan
;
823 Offset
:= Val
- Scan
;
825 Emit_Natural
(Scan
+ 1, Natural (Offset
));
832 -- Combining parenthesis handling with the base level of regular
833 -- expression is a trifle forced, but the need to tie the tails of the
834 -- the branches to what follows makes it hard to avoid.
837 (Parenthesized
: Boolean;
839 Flags
: out Expression_Flags
;
842 E
: String renames Expression
;
846 New_Flags
: Expression_Flags
;
847 Have_Branch
: Boolean := False;
850 Flags
:= (Has_Width
=> True, others => False); -- Tentatively
852 -- Make an OPEN node, if parenthesized
854 if Parenthesized
and then Capturing
then
855 if Matcher
.Paren_Count
> Max_Paren_Count
then
856 Fail
("too many ()");
859 Par_No
:= Matcher
.Paren_Count
+ 1;
860 Matcher
.Paren_Count
:= Matcher
.Paren_Count
+ 1;
861 IP
:= Emit_Node
(OPEN
);
862 Emit
(Character'Val (Par_No
));
868 -- Pick up the branches, linking them together
870 Parse_Branch
(New_Flags
, True, Br
);
877 if Parse_Pos
<= Parse_End
878 and then E
(Parse_Pos
) = '|'
880 Insert_Operator
(BRANCH
, Br
);
885 Link_Tail
(IP
, Br
); -- OPEN -> first
890 if not New_Flags
.Has_Width
then
891 Flags
.Has_Width
:= False;
894 Flags
.SP_Start
:= Flags
.SP_Start
or else New_Flags
.SP_Start
;
896 while Parse_Pos
<= Parse_End
897 and then (E
(Parse_Pos
) = '|')
899 Parse_Pos
:= Parse_Pos
+ 1;
900 Parse_Branch
(New_Flags
, False, Br
);
907 Link_Tail
(IP
, Br
); -- BRANCH -> BRANCH
909 if not New_Flags
.Has_Width
then
910 Flags
.Has_Width
:= False;
913 Flags
.SP_Start
:= Flags
.SP_Start
or else New_Flags
.SP_Start
;
916 -- Make a closing node, and hook it on the end
918 if Parenthesized
then
920 Ender
:= Emit_Node
(CLOSE
);
921 Emit
(Character'Val (Par_No
));
922 Link_Tail
(IP
, Ender
);
925 -- Need to keep looking after the closing parenthesis
930 Ender
:= Emit_Node
(EOP
);
931 Link_Tail
(IP
, Ender
);
934 if Have_Branch
and then Emit_Ptr
<= PM
.Size
+ 1 then
936 -- Hook the tails of the branches to the closing node
940 Link_Operand_Tail
(Br
, Ender
);
941 Br2
:= Get_Next
(Program
, Br
);
947 -- Check for proper termination
949 if Parenthesized
then
950 if Parse_Pos
> Parse_End
or else E
(Parse_Pos
) /= ')' then
951 Fail
("unmatched ()");
954 Parse_Pos
:= Parse_Pos
+ 1;
956 elsif Parse_Pos
<= Parse_End
then
957 if E
(Parse_Pos
) = ')' then
958 Fail
("unmatched ')'");
960 Fail
("junk on end"); -- "Can't happen"
970 (Expr_Flags
: out Expression_Flags
;
976 -- Tentatively set worst expression case
978 Expr_Flags
:= Worst_Expression
;
980 C
:= Expression
(Parse_Pos
);
981 Parse_Pos
:= Parse_Pos
+ 1;
987 (if (Flags
and Multiple_Lines
) /= 0 then MBOL
988 elsif (Flags
and Single_Line
) /= 0 then SBOL
994 (if (Flags
and Multiple_Lines
) /= 0 then MEOL
995 elsif (Flags
and Single_Line
) /= 0 then SEOL
1001 (if (Flags
and Single_Line
) /= 0 then SANY
else ANY
);
1003 Expr_Flags
.Has_Width
:= True;
1004 Expr_Flags
.Simple
:= True;
1007 Parse_Character_Class
(IP
);
1008 Expr_Flags
.Has_Width
:= True;
1009 Expr_Flags
.Simple
:= True;
1013 New_Flags
: Expression_Flags
;
1016 if Parse_Pos
<= Parse_End
- 1
1017 and then Expression
(Parse_Pos
) = '?'
1018 and then Expression
(Parse_Pos
+ 1) = ':'
1020 Parse_Pos
:= Parse_Pos
+ 2;
1022 -- Non-capturing parenthesis
1024 Parse
(True, False, New_Flags
, IP
);
1027 -- Capturing parenthesis
1029 Parse
(True, True, New_Flags
, IP
);
1030 Expr_Flags
.Has_Width
:=
1031 Expr_Flags
.Has_Width
or else New_Flags
.Has_Width
;
1032 Expr_Flags
.SP_Start
:=
1033 Expr_Flags
.SP_Start
or else New_Flags
.SP_Start
;
1040 when '|' | ASCII
.LF |
')' =>
1041 Fail
("internal urp"); -- Supposed to be caught earlier
1043 when '?' |
'+' |
'*' =>
1044 Fail
(C
& " follows nothing");
1047 if Is_Curly_Operator
(Parse_Pos
- 1) then
1048 Fail
(C
& " follows nothing");
1050 Parse_Literal
(Expr_Flags
, IP
);
1054 if Parse_Pos
> Parse_End
then
1055 Fail
("trailing \");
1058 Parse_Pos := Parse_Pos + 1;
1060 case Expression (Parse_Pos - 1) is
1062 IP := Emit_Node (BOUND);
1065 IP := Emit_Node (NBOUND);
1068 IP := Emit_Node (SPACE);
1069 Expr_Flags.Simple := True;
1070 Expr_Flags.Has_Width := True;
1073 IP := Emit_Node (NSPACE);
1074 Expr_Flags.Simple := True;
1075 Expr_Flags.Has_Width := True;
1078 IP := Emit_Node (DIGIT);
1079 Expr_Flags.Simple := True;
1080 Expr_Flags.Has_Width := True;
1083 IP := Emit_Node (NDIGIT);
1084 Expr_Flags.Simple := True;
1085 Expr_Flags.Has_Width := True;
1088 IP := Emit_Node (ALNUM);
1089 Expr_Flags.Simple := True;
1090 Expr_Flags.Has_Width := True;
1093 IP := Emit_Node (NALNUM);
1094 Expr_Flags.Simple := True;
1095 Expr_Flags.Has_Width := True;
1098 IP := Emit_Node (SBOL);
1101 IP := Emit_Node (SEOL);
1104 IP := Emit_Node (REFF);
1107 Save : constant Natural := Parse_Pos - 1;
1110 while Parse_Pos <= Expression'Last
1111 and then Is_Digit (Expression (Parse_Pos))
1113 Parse_Pos := Parse_Pos + 1;
1116 Emit (Character'Val (Natural'Value
1117 (Expression (Save .. Parse_Pos - 1))));
1121 Parse_Pos := Parse_Pos - 1;
1122 Parse_Literal (Expr_Flags, IP);
1126 Parse_Literal (Expr_Flags, IP);
1134 procedure Parse_Branch
1135 (Flags : out Expression_Flags;
1139 E : String renames Expression;
1142 New_Flags : Expression_Flags;
1145 pragma Warnings (Off, Discard);
1148 Flags := Worst_Expression; -- Tentatively
1149 IP := (if First then Emit_Ptr else Emit_Node (BRANCH));
1152 while Parse_Pos <= Parse_End
1153 and then E (Parse_Pos) /= ')'
1154 and then E (Parse_Pos) /= ASCII.LF
1155 and then E (Parse_Pos) /= '|'
1157 Parse_Piece (New_Flags, Last);
1164 Flags.Has_Width := Flags.Has_Width or else New_Flags.Has_Width;
1166 if Chain = 0 then -- First piece
1167 Flags.SP_Start := Flags.SP_Start or else New_Flags.SP_Start;
1169 Link_Tail (Chain, Last);
1175 -- Case where loop ran zero CURLY
1178 Discard := Emit_Node (NOTHING);
1182 ---------------------------
1183 -- Parse_Character_Class --
1184 ---------------------------
1186 procedure Parse_Character_Class (IP : out Pointer) is
1187 Bitmap : Character_Class;
1188 Invert : Boolean := False;
1189 In_Range : Boolean := False;
1190 Named_Class : Std_Class := ANYOF_NONE;
1192 Last_Value : Character := ASCII.NUL;
1195 Reset_Class (Bitmap);
1197 -- Do we have an invert character class ?
1199 if Parse_Pos <= Parse_End
1200 and then Expression (Parse_Pos) = '^'
1203 Parse_Pos := Parse_Pos + 1;
1206 -- First character can be ] or - without closing the class
1208 if Parse_Pos <= Parse_End
1209 and then (Expression (Parse_Pos) = ']'
1210 or else Expression (Parse_Pos) = '-')
1212 Set_In_Class (Bitmap, Expression (Parse_Pos));
1213 Parse_Pos := Parse_Pos + 1;
1216 -- While we don't have the end of the class
1218 while Parse_Pos <= Parse_End
1219 and then Expression (Parse_Pos) /= ']'
1221 Named_Class := ANYOF_NONE;
1222 Value := Expression (Parse_Pos);
1223 Parse_Pos := Parse_Pos + 1;
1225 -- Do we have a Posix character class
1227 Named_Class := Parse_Posix_Character_Class;
1229 elsif Value = '\' then
1230 if Parse_Pos = Parse_End then
1231 Fail ("Trailing
\");
1233 Value
:= Expression
(Parse_Pos
);
1234 Parse_Pos
:= Parse_Pos
+ 1;
1237 when 'w' => Named_Class
:= ANYOF_ALNUM
;
1238 when 'W' => Named_Class
:= ANYOF_NALNUM
;
1239 when 's' => Named_Class
:= ANYOF_SPACE
;
1240 when 'S' => Named_Class
:= ANYOF_NSPACE
;
1241 when 'd' => Named_Class
:= ANYOF_DIGIT
;
1242 when 'D' => Named_Class
:= ANYOF_NDIGIT
;
1243 when 'n' => Value
:= ASCII
.LF
;
1244 when 'r' => Value
:= ASCII
.CR
;
1245 when 't' => Value
:= ASCII
.HT
;
1246 when 'f' => Value
:= ASCII
.FF
;
1247 when 'e' => Value
:= ASCII
.ESC
;
1248 when 'a' => Value
:= ASCII
.BEL
;
1250 -- when 'x' => ??? hexadecimal value
1251 -- when 'c' => ??? control character
1252 -- when '0'..'9' => ??? octal character
1254 when others => null;
1258 -- Do we have a character class?
1260 if Named_Class
/= ANYOF_NONE
then
1262 -- A range like 'a-\d' or 'a-[:digit:] is not a range
1265 Set_In_Class
(Bitmap
, Last_Value
);
1266 Set_In_Class
(Bitmap
, '-');
1273 when ANYOF_NONE
=> null;
1275 when ANYOF_ALNUM | ANYOF_ALNUMC
=>
1276 for Value
in Class_Byte
'Range loop
1277 if Is_Alnum
(Character'Val (Value
)) then
1278 Set_In_Class
(Bitmap
, Character'Val (Value
));
1282 when ANYOF_NALNUM | ANYOF_NALNUMC
=>
1283 for Value
in Class_Byte
'Range loop
1284 if not Is_Alnum
(Character'Val (Value
)) then
1285 Set_In_Class
(Bitmap
, Character'Val (Value
));
1290 for Value
in Class_Byte
'Range loop
1291 if Is_White_Space
(Character'Val (Value
)) then
1292 Set_In_Class
(Bitmap
, Character'Val (Value
));
1296 when ANYOF_NSPACE
=>
1297 for Value
in Class_Byte
'Range loop
1298 if not Is_White_Space
(Character'Val (Value
)) then
1299 Set_In_Class
(Bitmap
, Character'Val (Value
));
1304 for Value
in Class_Byte
'Range loop
1305 if Is_Digit
(Character'Val (Value
)) then
1306 Set_In_Class
(Bitmap
, Character'Val (Value
));
1310 when ANYOF_NDIGIT
=>
1311 for Value
in Class_Byte
'Range loop
1312 if not Is_Digit
(Character'Val (Value
)) then
1313 Set_In_Class
(Bitmap
, Character'Val (Value
));
1318 for Value
in Class_Byte
'Range loop
1319 if Is_Letter
(Character'Val (Value
)) then
1320 Set_In_Class
(Bitmap
, Character'Val (Value
));
1324 when ANYOF_NALPHA
=>
1325 for Value
in Class_Byte
'Range loop
1326 if not Is_Letter
(Character'Val (Value
)) then
1327 Set_In_Class
(Bitmap
, Character'Val (Value
));
1332 for Value
in 0 .. 127 loop
1333 Set_In_Class
(Bitmap
, Character'Val (Value
));
1336 when ANYOF_NASCII
=>
1337 for Value
in 128 .. 255 loop
1338 Set_In_Class
(Bitmap
, Character'Val (Value
));
1342 for Value
in Class_Byte
'Range loop
1343 if Is_Control
(Character'Val (Value
)) then
1344 Set_In_Class
(Bitmap
, Character'Val (Value
));
1348 when ANYOF_NCNTRL
=>
1349 for Value
in Class_Byte
'Range loop
1350 if not Is_Control
(Character'Val (Value
)) then
1351 Set_In_Class
(Bitmap
, Character'Val (Value
));
1356 for Value
in Class_Byte
'Range loop
1357 if Is_Graphic
(Character'Val (Value
)) then
1358 Set_In_Class
(Bitmap
, Character'Val (Value
));
1362 when ANYOF_NGRAPH
=>
1363 for Value
in Class_Byte
'Range loop
1364 if not Is_Graphic
(Character'Val (Value
)) then
1365 Set_In_Class
(Bitmap
, Character'Val (Value
));
1370 for Value
in Class_Byte
'Range loop
1371 if Is_Lower
(Character'Val (Value
)) then
1372 Set_In_Class
(Bitmap
, Character'Val (Value
));
1376 when ANYOF_NLOWER
=>
1377 for Value
in Class_Byte
'Range loop
1378 if not Is_Lower
(Character'Val (Value
)) then
1379 Set_In_Class
(Bitmap
, Character'Val (Value
));
1384 for Value
in Class_Byte
'Range loop
1385 if Is_Printable
(Character'Val (Value
)) then
1386 Set_In_Class
(Bitmap
, Character'Val (Value
));
1390 when ANYOF_NPRINT
=>
1391 for Value
in Class_Byte
'Range loop
1392 if not Is_Printable
(Character'Val (Value
)) then
1393 Set_In_Class
(Bitmap
, Character'Val (Value
));
1398 for Value
in Class_Byte
'Range loop
1399 if Is_Printable
(Character'Val (Value
))
1400 and then not Is_White_Space
(Character'Val (Value
))
1401 and then not Is_Alnum
(Character'Val (Value
))
1403 Set_In_Class
(Bitmap
, Character'Val (Value
));
1407 when ANYOF_NPUNCT
=>
1408 for Value
in Class_Byte
'Range loop
1409 if not Is_Printable
(Character'Val (Value
))
1410 or else Is_White_Space
(Character'Val (Value
))
1411 or else Is_Alnum
(Character'Val (Value
))
1413 Set_In_Class
(Bitmap
, Character'Val (Value
));
1418 for Value
in Class_Byte
'Range loop
1419 if Is_Upper
(Character'Val (Value
)) then
1420 Set_In_Class
(Bitmap
, Character'Val (Value
));
1424 when ANYOF_NUPPER
=>
1425 for Value
in Class_Byte
'Range loop
1426 if not Is_Upper
(Character'Val (Value
)) then
1427 Set_In_Class
(Bitmap
, Character'Val (Value
));
1431 when ANYOF_XDIGIT
=>
1432 for Value
in Class_Byte
'Range loop
1433 if Is_Hexadecimal_Digit
(Character'Val (Value
)) then
1434 Set_In_Class
(Bitmap
, Character'Val (Value
));
1438 when ANYOF_NXDIGIT
=>
1439 for Value
in Class_Byte
'Range loop
1440 if not Is_Hexadecimal_Digit
1441 (Character'Val (Value
))
1443 Set_In_Class
(Bitmap
, Character'Val (Value
));
1449 -- Not a character range
1451 elsif not In_Range
then
1452 Last_Value
:= Value
;
1454 if Parse_Pos
> Expression
'Last then
1455 Fail
("Empty character class []");
1458 if Expression
(Parse_Pos
) = '-'
1459 and then Parse_Pos
< Parse_End
1460 and then Expression
(Parse_Pos
+ 1) /= ']'
1462 Parse_Pos
:= Parse_Pos
+ 1;
1464 -- Do we have a range like '\d-a' and '[:space:]-a'
1465 -- which is not a real range
1467 if Named_Class
/= ANYOF_NONE
then
1468 Set_In_Class
(Bitmap
, '-');
1474 Set_In_Class
(Bitmap
, Value
);
1478 -- Else in a character range
1481 if Last_Value
> Value
then
1482 Fail
("Invalid Range [" & Last_Value
'Img
1483 & "-" & Value
'Img & "]");
1486 while Last_Value
<= Value
loop
1487 Set_In_Class
(Bitmap
, Last_Value
);
1488 Last_Value
:= Character'Succ (Last_Value
);
1497 -- Optimize case-insensitive ranges (put the upper case or lower
1498 -- case character into the bitmap)
1500 if (Flags
and Case_Insensitive
) /= 0 then
1501 for C
in Character'Range loop
1502 if Get_From_Class
(Bitmap
, C
) then
1503 Set_In_Class
(Bitmap
, To_Lower
(C
));
1504 Set_In_Class
(Bitmap
, To_Upper
(C
));
1509 -- Optimize inverted classes
1512 for J
in Bitmap
'Range loop
1513 Bitmap
(J
) := not Bitmap
(J
);
1517 Parse_Pos
:= Parse_Pos
+ 1;
1521 IP
:= Emit_Node
(ANYOF
);
1522 Emit_Class
(Bitmap
);
1523 end Parse_Character_Class
;
1529 -- This is a bit tricky due to quoted chars and due to
1530 -- the multiplier characters '*', '+', and '?' that
1531 -- take the SINGLE char previous as their operand.
1533 -- On entry, the character at Parse_Pos - 1 is going to go
1534 -- into the string, no matter what it is. It could be
1535 -- following a \ if Parse_Atom was entered from the '\' case.
1537 -- Basic idea is to pick up a good char in C and examine
1538 -- the next char. If Is_Mult (C) then twiddle, if it's a \
1539 -- then frozzle and if it's another magic char then push C and
1540 -- terminate the string. If none of the above, push C on the
1541 -- string and go around again.
1543 -- Start_Pos is used to remember where "the current character"
1544 -- starts in the string, if due to an Is_Mult we need to back
1545 -- up and put the current char in a separate 1-character string.
1546 -- When Start_Pos is 0, C is the only char in the string;
1547 -- this is used in Is_Mult handling, and in setting the SIMPLE
1550 procedure Parse_Literal
1551 (Expr_Flags
: out Expression_Flags
;
1554 Start_Pos
: Natural := 0;
1556 Length_Ptr
: Pointer
;
1558 Has_Special_Operator
: Boolean := False;
1561 Parse_Pos
:= Parse_Pos
- 1; -- Look at current character
1565 (if (Flags
and Case_Insensitive
) /= 0 then EXACTF
else EXACT
);
1567 Length_Ptr
:= Emit_Ptr
;
1568 Emit_Ptr
:= String_Operand
(IP
);
1572 C
:= Expression
(Parse_Pos
); -- Get current character
1575 when '.' |
'[' |
'(' |
')' |
'|' | ASCII
.LF |
'$' |
'^' =>
1577 if Start_Pos
= 0 then
1578 Start_Pos
:= Parse_Pos
;
1579 Emit
(C
); -- First character is always emitted
1581 exit Parse_Loop
; -- Else we are done
1584 when '?' |
'+' |
'*' |
'{' =>
1586 if Start_Pos
= 0 then
1587 Start_Pos
:= Parse_Pos
;
1588 Emit
(C
); -- First character is always emitted
1590 -- Are we looking at an operator, or is this
1591 -- simply a normal character ?
1593 elsif not Is_Mult
(Parse_Pos
) then
1594 Start_Pos
:= Parse_Pos
;
1598 -- We've got something like "abc?d". Mark this as a
1599 -- special case. What we want to emit is a first
1600 -- constant string for "ab", then one for "c" that will
1601 -- ultimately be transformed with a CURLY operator, A
1602 -- special case has to be handled for "a?", since there
1603 -- is no initial string to emit.
1605 Has_Special_Operator
:= True;
1610 Start_Pos
:= Parse_Pos
;
1612 if Parse_Pos
= Parse_End
then
1613 Fail
("Trailing \");
1616 case Expression (Parse_Pos + 1) is
1617 when 'b' | 'B' | 's' | 'S' | 'd' | 'D'
1618 | 'w' | 'W' | '0' .. '9' | 'G' | 'A'
1620 when 'n' => Emit (ASCII.LF);
1621 when 't' => Emit (ASCII.HT);
1622 when 'r' => Emit (ASCII.CR);
1623 when 'f' => Emit (ASCII.FF);
1624 when 'e' => Emit (ASCII.ESC);
1625 when 'a' => Emit (ASCII.BEL);
1626 when others => Emit (Expression (Parse_Pos + 1));
1629 Parse_Pos := Parse_Pos + 1;
1633 Start_Pos := Parse_Pos;
1637 exit Parse_Loop when Emit_Ptr - Length_Ptr = 254;
1639 Parse_Pos := Parse_Pos + 1;
1641 exit Parse_Loop when Parse_Pos > Parse_End;
1642 end loop Parse_Loop;
1644 -- Is the string followed by a '*+?{' operator ? If yes, and if there
1645 -- is an initial string to emit, do it now.
1647 if Has_Special_Operator
1648 and then Emit_Ptr >= Length_Ptr + Next_Pointer_Bytes
1650 Emit_Ptr := Emit_Ptr - 1;
1651 Parse_Pos := Start_Pos;
1654 if Length_Ptr <= PM.Size then
1655 Program (Length_Ptr) := Character'Val (Emit_Ptr - Length_Ptr - 2);
1658 Expr_Flags.Has_Width := True;
1660 -- Slight optimization when there is a single character
1662 if Emit_Ptr = Length_Ptr + 2 then
1663 Expr_Flags.Simple := True;
1671 -- Note that the branching code sequences used for '?' and the
1672 -- general cases of '*' and + are somewhat optimized: they use
1673 -- the same NOTHING node as both the endmarker for their branch
1674 -- list and the body of the last branch. It might seem that
1675 -- this node could be dispensed with entirely, but the endmarker
1676 -- role is not redundant.
1678 procedure Parse_Piece
1679 (Expr_Flags : out Expression_Flags;
1683 New_Flags : Expression_Flags;
1684 Greedy : Boolean := True;
1687 Parse_Atom (New_Flags, IP);
1693 if Parse_Pos > Parse_End
1694 or else not Is_Mult (Parse_Pos)
1696 Expr_Flags := New_Flags;
1700 Op := Expression (Parse_Pos);
1704 then (SP_Start => True, others => False)
1705 else (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;
1789 -- Class names. Note that code assumes that the length of all
1790 -- classes starting with the same letter have the same length.
1792 Alnum : constant String := "alnum
:]";
1793 Alpha : constant String := "alpha
:]";
1794 Ascii_C : constant String := "ascii
:]";
1795 Cntrl : constant String := "cntrl
:]";
1796 Digit : constant String := "digit
:]";
1797 Graph : constant String := "graph
:]";
1798 Lower : constant String := "lower
:]";
1799 Print : constant String := "print
:]";
1800 Punct : constant String := "punct
:]";
1801 Space : constant String := "space
:]";
1802 Upper : constant String := "upper
:]";
1803 Word : constant String := "word
:]";
1804 Xdigit : constant String := "xdigit
:]";
1807 -- Case of character class specified
1809 if Parse_Pos <= Parse_End
1810 and then Expression (Parse_Pos) = ':'
1812 Parse_Pos := Parse_Pos + 1;
1814 -- Do we have something like: [[:^alpha:]]
1816 if Parse_Pos <= Parse_End
1817 and then Expression (Parse_Pos) = '^'
1820 Parse_Pos := Parse_Pos + 1;
1823 -- Check for class names based on first letter
1825 case Expression (Parse_Pos) is
1828 -- All 'a' classes have the same length (Alnum'Length)
1830 if Parse_Pos + Alnum'Length - 1 <= Parse_End then
1832 E (Parse_Pos .. Parse_Pos + Alnum'Length - 1) = Alnum
1835 (if Invert then ANYOF_NALNUMC else ANYOF_ALNUMC);
1836 Parse_Pos := Parse_Pos + Alnum'Length;
1839 E (Parse_Pos .. Parse_Pos + Alpha'Length - 1) = Alpha
1842 (if Invert then ANYOF_NALPHA else ANYOF_ALPHA);
1843 Parse_Pos := Parse_Pos + Alpha'Length;
1845 elsif E (Parse_Pos .. Parse_Pos + Ascii_C'Length - 1) =
1849 (if Invert then ANYOF_NASCII else ANYOF_ASCII);
1850 Parse_Pos := Parse_Pos + Ascii_C'Length;
1852 Fail ("Invalid
character class
: " & E);
1856 Fail ("Invalid
character class
: " & E);
1860 if Parse_Pos + Cntrl'Length - 1 <= Parse_End
1862 E (Parse_Pos .. Parse_Pos + Cntrl'Length - 1) = Cntrl
1864 Class := (if Invert then ANYOF_NCNTRL else ANYOF_CNTRL);
1865 Parse_Pos := Parse_Pos + Cntrl'Length;
1867 Fail ("Invalid
character class
: " & E);
1871 if Parse_Pos + Digit'Length - 1 <= Parse_End
1873 E (Parse_Pos .. Parse_Pos + Digit'Length - 1) = Digit
1875 Class := (if Invert then ANYOF_NDIGIT else ANYOF_DIGIT);
1876 Parse_Pos := Parse_Pos + Digit'Length;
1880 if Parse_Pos + Graph'Length - 1 <= Parse_End
1882 E (Parse_Pos .. Parse_Pos + Graph'Length - 1) = Graph
1884 Class := (if Invert then ANYOF_NGRAPH else ANYOF_GRAPH);
1885 Parse_Pos := Parse_Pos + Graph'Length;
1887 Fail ("Invalid
character class
: " & E);
1891 if Parse_Pos + Lower'Length - 1 <= Parse_End
1893 E (Parse_Pos .. Parse_Pos + Lower'Length - 1) = Lower
1895 Class := (if Invert then ANYOF_NLOWER else ANYOF_LOWER);
1896 Parse_Pos := Parse_Pos + Lower'Length;
1898 Fail ("Invalid
character class
: " & E);
1903 -- All 'p' classes have the same length
1905 if Parse_Pos + Print'Length - 1 <= Parse_End then
1907 E (Parse_Pos .. Parse_Pos + Print'Length - 1) = Print
1910 (if Invert then ANYOF_NPRINT else ANYOF_PRINT);
1911 Parse_Pos := Parse_Pos + Print'Length;
1914 E (Parse_Pos .. Parse_Pos + Punct'Length - 1) = Punct
1917 (if Invert then ANYOF_NPUNCT else ANYOF_PUNCT);
1918 Parse_Pos := Parse_Pos + Punct'Length;
1921 Fail ("Invalid
character class
: " & E);
1925 Fail ("Invalid
character class
: " & E);
1929 if Parse_Pos + Space'Length - 1 <= Parse_End
1931 E (Parse_Pos .. Parse_Pos + Space'Length - 1) = Space
1933 Class := (if Invert then ANYOF_NSPACE else ANYOF_SPACE);
1934 Parse_Pos := Parse_Pos + Space'Length;
1936 Fail ("Invalid
character class
: " & E);
1940 if Parse_Pos + Upper'Length - 1 <= Parse_End
1942 E (Parse_Pos .. Parse_Pos + Upper'Length - 1) = Upper
1944 Class := (if Invert then ANYOF_NUPPER else ANYOF_UPPER);
1945 Parse_Pos := Parse_Pos + Upper'Length;
1947 Fail ("Invalid
character class
: " & E);
1951 if Parse_Pos + Word'Length - 1 <= Parse_End
1953 E (Parse_Pos .. Parse_Pos + Word'Length - 1) = Word
1955 Class := (if Invert then ANYOF_NALNUM else ANYOF_ALNUM);
1956 Parse_Pos := Parse_Pos + Word'Length;
1958 Fail ("Invalid
character class
: " & E);
1962 if Parse_Pos + Xdigit'Length - 1 <= Parse_End
1964 E (Parse_Pos .. Parse_Pos + Xdigit'Length - 1) = Xdigit
1966 Class := (if Invert then ANYOF_NXDIGIT else ANYOF_XDIGIT);
1967 Parse_Pos := Parse_Pos + Xdigit'Length;
1970 Fail ("Invalid
character class
: " & E);
1974 Fail ("Invalid
character class
: " & E);
1977 -- Character class not specified
1984 end Parse_Posix_Character_Class;
1986 -- Local Declarations
1990 Expr_Flags : Expression_Flags;
1991 pragma Unreferenced (Expr_Flags);
1993 -- Start of processing for Compile
1996 Parse (False, False, Expr_Flags, Result);
1999 Fail ("Couldn
't compile expression
");
2002 Final_Code_Size := Emit_Ptr - 1;
2004 -- Do we want to actually compile the expression, or simply get the
2007 if Emit_Ptr <= PM.Size then
2015 (Expression : String;
2016 Flags : Regexp_Flags := No_Flags) return Pattern_Matcher
2018 -- Assume the compiled regexp will fit in 1000 chars. If it does not we
2019 -- will have to compile a second time once the correct size is known. If
2020 -- it fits, we save a significant amount of time by avoiding the second
2023 Dummy : Pattern_Matcher (1000);
2024 Size : Program_Size;
2027 Compile (Dummy, Expression, Size, Flags);
2029 if Size <= Dummy.Size then
2030 return Pattern_Matcher'
2032 First => Dummy.First,
2033 Anchored => Dummy.Anchored,
2034 Must_Have => Dummy.Must_Have,
2035 Must_Have_Length => Dummy.Must_Have_Length,
2036 Paren_Count => Dummy.Paren_Count,
2037 Flags => Dummy.Flags,
2040 (Dummy.Program'First .. Dummy.Program'First + Size - 1));
2042 -- We have to recompile now that we know the size
2043 -- ??? Can we use Ada 2005's return construct ?
2046 Result : Pattern_Matcher (Size);
2048 Compile (Result, Expression, Size, Flags);
2055 (Matcher : out Pattern_Matcher;
2056 Expression : String;
2057 Flags : Regexp_Flags := No_Flags)
2059 Size : Program_Size;
2062 Compile (Matcher, Expression, Size, Flags);
2064 if Size > Matcher.Size then
2065 raise Expression_Error with "Pattern_Matcher
is too small
";
2069 --------------------
2070 -- Dump_Operation --
2071 --------------------
2073 procedure Dump_Operation
2074 (Program : Program_Data;
2078 Current : Pointer := Index;
2080 Dump_Until (Program, Current, Current + 1, Indent);
2087 procedure Dump_Until
2088 (Program : Program_Data;
2089 Index : in out Pointer;
2092 Do_Print : Boolean := True)
2094 function Image (S : String) return String;
2095 -- Remove leading space
2101 function Image (S : String) return String is
2103 if S (S'First) = ' ' then
2104 return S (S'First + 1 .. S'Last);
2115 Local_Indent : Natural := Indent;
2117 -- Start of processing for Dump_Until
2120 while Index < Till loop
2121 Op := Opcode'Val (Character'Pos ((Program (Index))));
2122 Next := Get_Next (Program, Index);
2126 Point : constant String := Pointer'Image (Index);
2128 Put ((1 .. 4 - Point'Length => ' ')
2130 & (1 .. Local_Indent * 2 => ' ') & Opcode'Image (Op));
2133 -- Print the parenthesis number
2135 if Op = OPEN or else Op = CLOSE or else Op = REFF then
2136 Put (Image (Natural'Image
2138 (Program (Index + Next_Pointer_Bytes)))));
2141 if Next = Index then
2144 Put (" (" & Image (Pointer'Image (Next)) & ")");
2151 Bitmap : Character_Class;
2152 Last : Character := ASCII.NUL;
2153 Current : Natural := 0;
2154 Current_Char : Character;
2157 Bitmap_Operand (Program, Index, Bitmap);
2162 while Current <= 255 loop
2163 Current_Char := Character'Val (Current);
2165 -- First item in a range
2167 if Get_From_Class (Bitmap, Current_Char) then
2168 Last := Current_Char;
2170 -- Search for the last item in the range
2173 Current := Current + 1;
2174 exit when Current > 255;
2175 Current_Char := Character'Val (Current);
2177 not Get_From_Class (Bitmap, Current_Char);
2180 if not Is_Graphic (Last) then
2186 if Character'Succ (Last) /= Current_Char then
2187 Put ("\-" & Character'Pred (Current_Char));
2191 Current := Current + 1;
2198 Index := Index + Next_Pointer_Bytes + Bitmap'Length;
2201 when EXACT | EXACTF =>
2202 Length := String_Length (Program, Index);
2204 Put (" (" & Image (Program_Size'Image (Length + 1))
2206 & String (Program (String_Operand (Index)
2207 .. String_Operand (Index)
2212 Index := String_Operand (Index) + Length + 1;
2216 when BRANCH | STAR | PLUS =>
2221 Index := Index + Next_Pointer_Bytes;
2222 Dump_Until (Program, Index, Pointer'Min (Next, Till),
2223 Local_Indent + 1, Do_Print);
2225 when CURLY | CURLYX =>
2229 & Image (Natural'Image
2230 (Read_Natural (Program, Index + Next_Pointer_Bytes)))
2232 & Image (Natural'Image (Read_Natural (Program, Index + 5)))
2237 Dump_Until (Program, Index, Pointer'Min (Next, Till),
2238 Local_Indent + 1, Do_Print);
2246 Local_Indent := Local_Indent + 1;
2248 when CLOSE | REFF =>
2256 Local_Indent := Local_Indent - 1;
2260 Index := Index + Next_Pointer_Bytes;
2275 procedure Dump (Self : Pattern_Matcher) is
2276 Program : Program_Data renames Self.Program;
2277 Index : Pointer := Program'First;
2279 -- Start of processing for Dump
2282 Put_Line ("Must start
with (Self
.First
) = "
2283 & Character'Image (Self.First));
2285 if (Self.Flags and Case_Insensitive) /= 0 then
2286 Put_Line (" Case_Insensitive mode
");
2289 if (Self.Flags and Single_Line) /= 0 then
2290 Put_Line (" Single_Line mode
");
2293 if (Self.Flags and Multiple_Lines) /= 0 then
2294 Put_Line (" Multiple_Lines mode
");
2297 Dump_Until (Program, Index, Self.Program'Last + 1, 0);
2300 --------------------
2301 -- Get_From_Class --
2302 --------------------
2304 function Get_From_Class
2305 (Bitmap : Character_Class;
2306 C : Character) return Boolean
2308 Value : constant Class_Byte := Character'Pos (C);
2311 (Bitmap (Value / 8) and Bit_Conversion (Value mod 8)) /= 0;
2318 function Get_Next (Program : Program_Data; IP : Pointer) return Pointer is
2320 return IP + Pointer (Read_Natural (Program, IP + 1));
2327 function Is_Alnum (C : Character) return Boolean is
2329 return Is_Alphanumeric (C) or else C = '_';
2336 function Is_Printable (C : Character) return Boolean is
2338 -- Printable if space or graphic character or other whitespace
2339 -- Other white space includes (HT/LF/VT/FF/CR = codes 9-13)
2341 return C in Character'Val (32) .. Character'Val (126)
2342 or else C in ASCII.HT .. ASCII.CR;
2345 --------------------
2346 -- Is_White_Space --
2347 --------------------
2349 function Is_White_Space (C : Character) return Boolean is
2351 -- Note: HT = 9, LF = 10, VT = 11, FF = 12, CR = 13
2353 return C = ' ' or else C in ASCII.HT .. ASCII.CR;
2361 (Self : Pattern_Matcher;
2363 Matches : out Match_Array;
2364 Data_First : Integer := -1;
2365 Data_Last : Positive := Positive'Last)
2367 Program : Program_Data renames Self.Program; -- Shorter notation
2369 First_In_Data : constant Integer := Integer'Max (Data_First, Data'First);
2370 Last_In_Data : constant Integer := Integer'Min (Data_Last, Data'Last);
2372 -- Global work variables
2374 Input_Pos : Natural; -- String-input pointer
2375 BOL_Pos : Natural; -- Beginning of input, for ^ check
2376 Matched : Boolean := False; -- Until proven True
2378 Matches_Full : Match_Array (0 .. Natural'Max (Self.Paren_Count,
2380 -- Stores the value of all the parenthesis pairs.
2381 -- We do not use directly Matches, so that we can also use back
2382 -- references (REFF) even if Matches is too small.
2384 type Natural_Array is array (Match_Count range <>) of Natural;
2385 Matches_Tmp : Natural_Array (Matches_Full'Range);
2386 -- Save the opening position of parenthesis
2388 Last_Paren : Natural := 0;
2389 -- Last parenthesis seen
2391 Greedy : Boolean := True;
2392 -- True if the next operator should be greedy
2394 type Current_Curly_Record;
2395 type Current_Curly_Access is access all Current_Curly_Record;
2396 type Current_Curly_Record is record
2397 Paren_Floor : Natural; -- How far back to strip parenthesis data
2398 Cur : Integer; -- How many instances of scan we've matched
2399 Min : Natural; -- Minimal number of scans to match
2400 Max : Natural; -- Maximal number of scans to match
2401 Greedy : Boolean; -- Whether to work our way up or down
2402 Scan : Pointer; -- The thing to match
2403 Next : Pointer; -- What has to match after it
2404 Lastloc : Natural; -- Where we started matching this scan
2405 Old_Cc : Current_Curly_Access; -- Before we started this one
2407 -- Data used to handle the curly operator and the plus and star
2408 -- operators for complex expressions.
2410 Current_Curly : Current_Curly_Access := null;
2411 -- The curly currently being processed
2413 -----------------------
2414 -- Local Subprograms --
2415 -----------------------
2417 function Index (Start : Positive; C : Character) return Natural;
2418 -- Find character C in Data starting at Start and return position
2422 Max : Natural := Natural'Last) return Natural;
2423 -- Repeatedly match something simple, report how many
2424 -- It only matches on things of length 1.
2425 -- Starting from Input_Pos, it matches at most Max CURLY.
2427 function Try (Pos : Positive) return Boolean;
2428 -- Try to match at specific point
2430 function Match (IP : Pointer) return Boolean;
2431 -- This is the main matching routine. Conceptually the strategy
2432 -- is simple: check to see whether the current node matches,
2433 -- call self recursively to see whether the rest matches,
2434 -- and then act accordingly.
2436 -- In practice Match makes some effort to avoid recursion, in
2437 -- particular by going through "ordinary
" nodes (that don't
2438 -- need to know whether the rest of the match failed) by
2439 -- using a loop instead of recursion.
2440 -- Why is the above comment part of the spec rather than body ???
2442 function Match_Whilem return Boolean;
2443 -- Return True if a WHILEM matches the Current_Curly
2445 function Recurse_Match (IP : Pointer; From : Natural) return Boolean;
2446 pragma Inline (Recurse_Match);
2447 -- Calls Match recursively. It saves and restores the parenthesis
2448 -- status and location in the input stream correctly, so that
2449 -- backtracking is possible
2451 function Match_Simple_Operator
2455 Greedy : Boolean) return Boolean;
2456 -- Return True it the simple operator (possibly non-greedy) matches
2458 Dump_Indent : Integer := -1;
2459 procedure Dump_Current (Scan : Pointer; Prefix : Boolean := True);
2460 procedure Dump_Error (Msg : String);
2461 -- Debug: print the current context
2463 pragma Inline (Index);
2464 pragma Inline (Repeat);
2466 -- These are two complex functions, but used only once
2468 pragma Inline (Match_Whilem);
2469 pragma Inline (Match_Simple_Operator);
2475 function Index (Start : Positive; C : Character) return Natural is
2477 for J in Start .. Last_In_Data loop
2478 if Data (J) = C then
2490 function Recurse_Match (IP : Pointer; From : Natural) return Boolean is
2491 L : constant Natural := Last_Paren;
2492 Tmp_F : constant Match_Array :=
2493 Matches_Full (From + 1 .. Matches_Full'Last);
2494 Start : constant Natural_Array :=
2495 Matches_Tmp (From + 1 .. Matches_Tmp'Last);
2496 Input : constant Natural := Input_Pos;
2498 Dump_Indent_Save : constant Integer := Dump_Indent;
2506 Matches_Full (Tmp_F'Range) := Tmp_F;
2507 Matches_Tmp (Start'Range) := Start;
2509 Dump_Indent := Dump_Indent_Save;
2517 procedure Dump_Current (Scan : Pointer; Prefix : Boolean := True) is
2518 Length : constant := 10;
2519 Pos : constant String := Integer'Image (Input_Pos);
2523 Put ((1 .. 5 - Pos'Length => ' '));
2526 .. Integer'Min (Last_In_Data, Input_Pos + Length - 1)));
2527 Put ((1 .. Length - 1 - Last_In_Data + Input_Pos => ' '));
2534 Dump_Operation (Program, Scan, Indent => Dump_Indent);
2541 procedure Dump_Error (Msg : String) is
2544 Put ((1 .. Dump_Indent * 2 => ' '));
2552 function Match (IP : Pointer) return Boolean is
2553 Scan : Pointer := IP;
2559 Dump_Indent := Dump_Indent + 1;
2563 pragma Assert (Scan /= 0);
2565 -- Determine current opcode and count its usage in debug mode
2567 Op := Opcode'Val (Character'Pos (Program (Scan)));
2569 -- Calculate offset of next instruction. Second character is most
2570 -- significant in Program_Data.
2572 Next := Get_Next (Program, Scan);
2575 Dump_Current (Scan);
2580 Dump_Indent := Dump_Indent - 1;
2581 return True; -- Success
2584 if Program (Next) /= BRANCH then
2585 Next := Operand (Scan); -- No choice, avoid recursion
2589 if Recurse_Match (Operand (Scan), 0) then
2590 Dump_Indent := Dump_Indent - 1;
2594 Scan := Get_Next (Program, Scan);
2595 exit when Scan = 0 or else Program (Scan) /= BRANCH;
2605 exit State_Machine when Input_Pos /= BOL_Pos
2606 and then ((Self.Flags and Multiple_Lines) = 0
2607 or else Data (Input_Pos - 1) /= ASCII.LF);
2610 exit State_Machine when Input_Pos /= BOL_Pos
2611 and then Data (Input_Pos - 1) /= ASCII.LF;
2614 exit State_Machine when Input_Pos /= BOL_Pos;
2618 -- A combination of MEOL and SEOL
2620 if (Self.Flags and Multiple_Lines) = 0 then
2624 exit State_Machine when Input_Pos <= Data'Last;
2626 elsif Input_Pos <= Last_In_Data then
2627 exit State_Machine when Data (Input_Pos) /= ASCII.LF;
2629 exit State_Machine when Last_In_Data /= Data'Last;
2633 if Input_Pos <= Last_In_Data then
2634 exit State_Machine when Data (Input_Pos) /= ASCII.LF;
2636 exit State_Machine when Last_In_Data /= Data'Last;
2641 -- If there is a character before Data'Last (even if
2642 -- Last_In_Data stops before then), we can't have the
2645 exit State_Machine when Input_Pos <= Data'Last;
2647 when BOUND | NBOUND =>
2649 -- Was last char in word ?
2652 N : Boolean := False;
2653 Ln : Boolean := False;
2656 if Input_Pos /= First_In_Data then
2657 N := Is_Alnum (Data (Input_Pos - 1));
2661 (if Input_Pos > Last_In_Data
2663 else Is_Alnum (Data (Input_Pos)));
2677 exit State_Machine when Input_Pos > Last_In_Data
2678 or else not Is_White_Space (Data (Input_Pos));
2679 Input_Pos := Input_Pos + 1;
2682 exit State_Machine when Input_Pos > Last_In_Data
2683 or else Is_White_Space (Data (Input_Pos));
2684 Input_Pos := Input_Pos + 1;
2687 exit State_Machine when Input_Pos > Last_In_Data
2688 or else not Is_Digit (Data (Input_Pos));
2689 Input_Pos := Input_Pos + 1;
2692 exit State_Machine when Input_Pos > Last_In_Data
2693 or else Is_Digit (Data (Input_Pos));
2694 Input_Pos := Input_Pos + 1;
2697 exit State_Machine when Input_Pos > Last_In_Data
2698 or else not Is_Alnum (Data (Input_Pos));
2699 Input_Pos := Input_Pos + 1;
2702 exit State_Machine when Input_Pos > Last_In_Data
2703 or else Is_Alnum (Data (Input_Pos));
2704 Input_Pos := Input_Pos + 1;
2707 exit State_Machine when Input_Pos > Last_In_Data
2708 or else Data (Input_Pos) = ASCII.LF;
2709 Input_Pos := Input_Pos + 1;
2712 exit State_Machine when Input_Pos > Last_In_Data;
2713 Input_Pos := Input_Pos + 1;
2717 Opnd : Pointer := String_Operand (Scan);
2718 Current : Positive := Input_Pos;
2719 Last : constant Pointer :=
2720 Opnd + String_Length (Program, Scan);
2723 while Opnd <= Last loop
2724 exit State_Machine when Current > Last_In_Data
2725 or else Program (Opnd) /= Data (Current);
2726 Current := Current + 1;
2730 Input_Pos := Current;
2735 Opnd : Pointer := String_Operand (Scan);
2736 Current : Positive := Input_Pos;
2738 Last : constant Pointer :=
2739 Opnd + String_Length (Program, Scan);
2742 while Opnd <= Last loop
2743 exit State_Machine when Current > Last_In_Data
2744 or else Program (Opnd) /= To_Lower (Data (Current));
2745 Current := Current + 1;
2749 Input_Pos := Current;
2754 Bitmap : Character_Class;
2756 Bitmap_Operand (Program, Scan, Bitmap);
2757 exit State_Machine when Input_Pos > Last_In_Data
2758 or else not Get_From_Class (Bitmap, Data (Input_Pos));
2759 Input_Pos := Input_Pos + 1;
2764 No : constant Natural :=
2765 Character'Pos (Program (Operand (Scan)));
2767 Matches_Tmp (No) := Input_Pos;
2772 No : constant Natural :=
2773 Character'Pos (Program (Operand (Scan)));
2776 Matches_Full (No) := (Matches_Tmp (No), Input_Pos - 1);
2778 if Last_Paren < No then
2785 No : constant Natural :=
2786 Character'Pos (Program (Operand (Scan)));
2791 -- If we haven't seen that parenthesis yet
2793 if Last_Paren < No then
2794 Dump_Indent := Dump_Indent - 1;
2797 Dump_Error ("REFF
: No match
, backtracking
");
2803 Data_Pos := Matches_Full (No).First;
2805 while Data_Pos <= Matches_Full (No).Last loop
2806 if Input_Pos > Last_In_Data
2807 or else Data (Input_Pos) /= Data (Data_Pos)
2809 Dump_Indent := Dump_Indent - 1;
2812 Dump_Error ("REFF
: No match
, backtracking
");
2818 Input_Pos := Input_Pos + 1;
2819 Data_Pos := Data_Pos + 1;
2826 when STAR | PLUS | CURLY =>
2828 Greed : constant Boolean := Greedy;
2831 Result := Match_Simple_Operator (Op, Scan, Next, Greed);
2832 Dump_Indent := Dump_Indent - 1;
2838 -- Looking at something like:
2840 -- 1: CURLYX {n,m} (->4)
2841 -- 2: code for complex thing (->3)
2846 Min : constant Natural :=
2847 Read_Natural (Program, Scan + Next_Pointer_Bytes);
2848 Max : constant Natural :=
2850 (Program, Scan + Next_Pointer_Bytes + 2);
2851 Cc : aliased Current_Curly_Record;
2853 Has_Match : Boolean;
2856 Cc := (Paren_Floor => Last_Paren,
2864 Old_Cc => Current_Curly);
2866 Current_Curly := Cc'Unchecked_Access;
2868 Has_Match := Match (Next - Next_Pointer_Bytes);
2870 -- Start on the WHILEM
2872 Current_Curly := Cc.Old_Cc;
2873 Dump_Indent := Dump_Indent - 1;
2875 if not Has_Match then
2877 Dump_Error ("CURLYX failed
...");
2885 Result := Match_Whilem;
2886 Dump_Indent := Dump_Indent - 1;
2888 if Debug and then not Result then
2889 Dump_Error ("WHILEM
: no match
, backtracking
");
2896 end loop State_Machine;
2899 Dump_Error ("failed
...");
2900 Dump_Indent := Dump_Indent - 1;
2903 -- If we get here, there is no match. For successful matches when EOP
2904 -- is the terminating point.
2909 ---------------------------
2910 -- Match_Simple_Operator --
2911 ---------------------------
2913 function Match_Simple_Operator
2917 Greedy : Boolean) return Boolean
2919 Next_Char : Character := ASCII.NUL;
2920 Next_Char_Known : Boolean := False;
2921 No : Integer; -- Can be negative
2923 Max : Natural := Natural'Last;
2924 Operand_Code : Pointer;
2927 Save : constant Natural := Input_Pos;
2930 -- Lookahead to avoid useless match attempts when we know what
2931 -- character comes next.
2933 if Program (Next) = EXACT then
2934 Next_Char := Program (String_Operand (Next));
2935 Next_Char_Known := True;
2938 -- Find the minimal and maximal values for the operator
2943 Operand_Code := Operand (Scan);
2947 Operand_Code := Operand (Scan);
2950 Min := Read_Natural (Program, Scan + Next_Pointer_Bytes);
2951 Max := Read_Natural (Program, Scan + Next_Pointer_Bytes + 2);
2952 Operand_Code := Scan + 7;
2956 Dump_Current (Operand_Code, Prefix => False);
2959 -- Non greedy operators
2963 -- Test we can repeat at least Min times
2966 No := Repeat (Operand_Code, Min);
2970 Dump_Error ("failed
... matched
" & No'Img & " times
");
2979 -- Find the place where 'next' could work
2981 if Next_Char_Known then
2983 -- Last position to check
2985 if Max = Natural'Last then
2986 Last_Pos := Last_In_Data;
2988 Last_Pos := Input_Pos + Max;
2990 if Last_Pos > Last_In_Data then
2991 Last_Pos := Last_In_Data;
2995 -- Look for the first possible opportunity
2998 Dump_Error ("Next_Char must be
" & Next_Char);
3002 -- Find the next possible position
3004 while Input_Pos <= Last_Pos
3005 and then Data (Input_Pos) /= Next_Char
3007 Input_Pos := Input_Pos + 1;
3010 if Input_Pos > Last_Pos then
3014 -- Check that we still match if we stop at the position we
3018 Num : constant Natural := Input_Pos - Old;
3024 Dump_Error ("Would we still match
at that position?
");
3027 if Repeat (Operand_Code, Num) < Num then
3032 -- Input_Pos now points to the new position
3034 if Match (Get_Next (Program, Scan)) then
3039 Input_Pos := Input_Pos + 1;
3042 -- We do not know what the next character is
3045 while Max >= Min loop
3047 Dump_Error ("Non
-greedy repeat
, N
=" & Min'Img);
3048 Dump_Error ("Do we still match Next
if we stop here?
");
3051 -- If the next character matches
3053 if Recurse_Match (Next, 1) then
3057 Input_Pos := Save + Min;
3059 -- Could not or did not match -- move forward
3061 if Repeat (Operand_Code, 1) /= 0 then
3065 Dump_Error ("Non
-greedy repeat failed
...");
3078 No := Repeat (Operand_Code, Max);
3080 if Debug and then No < Min then
3081 Dump_Error ("failed
... matched
" & No'Img & " times
");
3084 -- ??? Perl has some special code here in case the next
3085 -- instruction is of type EOL, since $ and \Z can match before
3086 -- *and* after newline at the end.
3088 -- ??? Perl has some special code here in case (paren) is True
3090 -- Else, if we don't have any parenthesis
3092 while No >= Min loop
3093 if not Next_Char_Known
3094 or else (Input_Pos <= Last_In_Data
3095 and then Data (Input_Pos) = Next_Char)
3097 if Match (Next) then
3102 -- Could not or did not work, we back up
3105 Input_Pos := Save + No;
3110 end Match_Simple_Operator;
3116 -- This is really hard to understand, because after we match what we
3117 -- are trying to match, we must make sure the rest of the REx is going
3118 -- to match for sure, and to do that we have to go back UP the parse
3119 -- tree by recursing ever deeper. And if it fails, we have to reset
3120 -- our parent's current state that we can try again after backing off.
3122 function Match_Whilem return Boolean is
3123 Cc : constant Current_Curly_Access := Current_Curly;
3125 N : constant Natural := Cc.Cur + 1;
3128 Lastloc : constant Natural := Cc.Lastloc;
3129 -- Detection of 0-len
3132 -- If degenerate scan matches "", assume scan done
3134 if Input_Pos = Cc.Lastloc
3135 and then N >= Cc.Min
3137 -- Temporarily restore the old context, and check that we
3138 -- match was comes after CURLYX.
3140 Current_Curly := Cc.Old_Cc;
3142 if Current_Curly /= null then
3143 Ln := Current_Curly.Cur;
3146 if Match (Cc.Next) then
3150 if Current_Curly /= null then
3151 Current_Curly.Cur := Ln;
3154 Current_Curly := Cc;
3158 -- First, just match a string of min scans
3162 Cc.Lastloc := Input_Pos;
3166 ("Tests that we match
at least
" & Cc.Min'Img & " N
=" & N'Img);
3169 if Match (Cc.Scan) then
3174 Cc.Lastloc := Lastloc;
3177 Dump_Error ("failed
...");
3183 -- Prefer next over scan for minimal matching
3185 if not Cc.Greedy then
3186 Current_Curly := Cc.Old_Cc;
3188 if Current_Curly /= null then
3189 Ln := Current_Curly.Cur;
3192 if Recurse_Match (Cc.Next, Cc.Paren_Floor) then
3196 if Current_Curly /= null then
3197 Current_Curly.Cur := Ln;
3200 Current_Curly := Cc;
3202 -- Maximum greed exceeded ?
3206 Dump_Error ("failed
...");
3211 -- Try scanning more and see if it helps
3213 Cc.Lastloc := Input_Pos;
3216 Dump_Error ("Next failed
, what about Current?
");
3219 if Recurse_Match (Cc.Scan, Cc.Paren_Floor) then
3224 Cc.Lastloc := Lastloc;
3228 -- Prefer scan over next for maximal matching
3230 if N < Cc.Max then -- more greed allowed ?
3232 Cc.Lastloc := Input_Pos;
3235 Dump_Error ("Recurse
at current position
");
3238 if Recurse_Match (Cc.Scan, Cc.Paren_Floor) then
3243 -- Failed deeper matches of scan, so see if this one works
3245 Current_Curly := Cc.Old_Cc;
3247 if Current_Curly /= null then
3248 Ln := Current_Curly.Cur;
3252 Dump_Error ("Failed matching
for later positions
");
3255 if Match (Cc.Next) then
3259 if Current_Curly /= null then
3260 Current_Curly.Cur := Ln;
3263 Current_Curly := Cc;
3265 Cc.Lastloc := Lastloc;
3268 Dump_Error ("failed
...");
3280 Max : Natural := Natural'Last) return Natural
3282 Scan : Natural := Input_Pos;
3284 Op : constant Opcode := Opcode'Val (Character'Pos (Program (IP)));
3287 Is_First : Boolean := True;
3288 Bitmap : Character_Class;
3291 if Max = Natural'Last or else Scan + Max - 1 > Last_In_Data then
3292 Last := Last_In_Data;
3294 Last := Scan + Max - 1;
3300 and then Data (Scan) /= ASCII.LF
3310 -- The string has only one character if Repeat was called
3312 C := Program (String_Operand (IP));
3314 and then C = Data (Scan)
3321 -- The string has only one character if Repeat was called
3323 C := Program (String_Operand (IP));
3325 and then To_Lower (C) = Data (Scan)
3332 Bitmap_Operand (Program, IP, Bitmap);
3337 and then Get_From_Class (Bitmap, Data (Scan))
3344 and then Is_Alnum (Data (Scan))
3351 and then not Is_Alnum (Data (Scan))
3358 and then Is_White_Space (Data (Scan))
3365 and then not Is_White_Space (Data (Scan))
3372 and then Is_Digit (Data (Scan))
3379 and then not Is_Digit (Data (Scan))
3385 raise Program_Error;
3388 Count := Scan - Input_Pos;
3397 function Try (Pos : Positive) return Boolean is
3401 Matches_Full := (others => No_Match);
3403 if Match (Program_First) then
3404 Matches_Full (0) := (Pos, Input_Pos - 1);
3411 -- Start of processing for Match
3414 -- Do we have the regexp Never_Match?
3416 if Self.Size = 0 then
3417 Matches := (others => No_Match);
3421 -- If there is a "must appear
" string, look for it
3423 if Self.Must_Have_Length > 0 then
3425 First : constant Character := Program (Self.Must_Have);
3426 Must_First : constant Pointer := Self.Must_Have;
3427 Must_Last : constant Pointer :=
3428 Must_First + Pointer (Self.Must_Have_Length - 1);
3429 Next_Try : Natural := Index (First_In_Data, First);
3433 and then Data (Next_Try .. Next_Try + Self.Must_Have_Length - 1)
3434 = String (Program (Must_First .. Must_Last))
3436 Next_Try := Index (Next_Try + 1, First);
3439 if Next_Try = 0 then
3440 Matches := (others => No_Match);
3441 return; -- Not present
3446 -- Mark beginning of line for ^
3448 BOL_Pos := Data'First;
3450 -- Simplest case first: an anchored match need be tried only once
3452 if Self.Anchored and then (Self.Flags and Multiple_Lines) = 0 then
3453 Matched := Try (First_In_Data);
3455 elsif Self.Anchored then
3457 Next_Try : Natural := First_In_Data;
3459 -- Test the first position in the buffer
3460 Matched := Try (Next_Try);
3462 -- Else only test after newlines
3465 while Next_Try <= Last_In_Data loop
3466 while Next_Try <= Last_In_Data
3467 and then Data (Next_Try) /= ASCII.LF
3469 Next_Try := Next_Try + 1;
3472 Next_Try := Next_Try + 1;
3474 if Next_Try <= Last_In_Data then
3475 Matched := Try (Next_Try);
3482 elsif Self.First /= ASCII.NUL then
3483 -- We know what char it must start with
3486 Next_Try : Natural := Index (First_In_Data, Self.First);
3489 while Next_Try /= 0 loop
3490 Matched := Try (Next_Try);
3492 Next_Try := Index (Next_Try + 1, Self.First);
3497 -- Messy cases: try all locations (including for the empty string)
3499 Matched := Try (First_In_Data);
3502 for S in First_In_Data + 1 .. Last_In_Data loop
3509 -- Matched has its value
3511 for J in Last_Paren + 1 .. Matches'Last loop
3512 Matches_Full (J) := No_Match;
3515 Matches := Matches_Full (Matches'Range);
3523 (Self : Pattern_Matcher;
3525 Data_First : Integer := -1;
3526 Data_Last : Positive := Positive'Last) return Natural
3528 Matches : Match_Array (0 .. 0);
3531 Match (Self, Data, Matches, Data_First, Data_Last);
3532 if Matches (0) = No_Match then
3533 return Data'First - 1;
3535 return Matches (0).First;
3540 (Self : Pattern_Matcher;
3542 Data_First : Integer := -1;
3543 Data_Last : Positive := Positive'Last) return Boolean
3545 Matches : Match_Array (0 .. 0);
3548 Match (Self, Data, Matches, Data_First, Data_Last);
3549 return Matches (0).First >= Data'First;
3553 (Expression : String;
3555 Matches : out Match_Array;
3556 Size : Program_Size := Auto_Size;
3557 Data_First : Integer := -1;
3558 Data_Last : Positive := Positive'Last)
3560 PM : Pattern_Matcher (Size);
3561 Finalize_Size : Program_Size;
3562 pragma Unreferenced (Finalize_Size);
3565 Match (Compile (Expression), Data, Matches, Data_First, Data_Last);
3567 Compile (PM, Expression, Finalize_Size);
3568 Match (PM, Data, Matches, Data_First, Data_Last);
3577 (Expression : String;
3579 Size : Program_Size := Auto_Size;
3580 Data_First : Integer := -1;
3581 Data_Last : Positive := Positive'Last) return Natural
3583 PM : Pattern_Matcher (Size);
3584 Final_Size : Program_Size;
3585 pragma Unreferenced (Final_Size);
3588 return Match (Compile (Expression), Data, Data_First, Data_Last);
3590 Compile (PM, Expression, Final_Size);
3591 return Match (PM, Data, Data_First, Data_Last);
3600 (Expression : String;
3602 Size : Program_Size := Auto_Size;
3603 Data_First : Integer := -1;
3604 Data_Last : Positive := Positive'Last) return Boolean
3606 Matches : Match_Array (0 .. 0);
3607 PM : Pattern_Matcher (Size);
3608 Final_Size : Program_Size;
3609 pragma Unreferenced (Final_Size);
3612 Match (Compile (Expression), Data, Matches, Data_First, Data_Last);
3614 Compile (PM, Expression, Final_Size);
3615 Match (PM, Data, Matches, Data_First, Data_Last);
3618 return Matches (0).First >= Data'First;
3625 function Operand (P : Pointer) return Pointer is
3627 return P + Next_Pointer_Bytes;
3634 procedure Optimize (Self : in out Pattern_Matcher) is
3636 Program : Program_Data renames Self.Program;
3639 -- Start with safe defaults (no optimization):
3640 -- * No known first character of match
3641 -- * Does not necessarily start at beginning of line
3642 -- * No string known that has to appear in data
3644 Self.First := ASCII.NUL;
3645 Self.Anchored := False;
3646 Self.Must_Have := Program'Last + 1;
3647 Self.Must_Have_Length := 0;
3649 Scan := Program_First; -- First instruction (can be anything)
3651 if Program (Scan) = EXACT then
3652 Self.First := Program (String_Operand (Scan));
3654 elsif Program (Scan) = BOL
3655 or else Program (Scan) = SBOL
3656 or else Program (Scan) = MBOL
3658 Self.Anchored := True;
3666 function Paren_Count (Regexp : Pattern_Matcher) return Match_Count is
3668 return Regexp.Paren_Count;
3675 function Quote (Str : String) return String is
3676 S : String (1 .. Str'Length * 2);
3677 Last : Natural := 0;
3680 for J in Str'Range loop
3682 when '^' | '$' | '|' | '*' | '+' | '?' | '{' |
3683 '}' | '[' | ']' | '(' | ')' | '\' | '.' =>
3685 S (Last + 1) := '\';
3686 S (Last + 2) := Str (J);
3690 S (Last + 1) := Str (J);
3695 return S (1 .. Last);
3702 function Read_Natural
3703 (Program : Program_Data;
3704 IP : Pointer) return Natural
3707 return Character'Pos (Program (IP)) +
3708 256 * Character'Pos (Program (IP + 1));
3715 procedure Reset_Class (Bitmap : out Character_Class) is
3717 Bitmap := (others => 0);
3724 procedure Set_In_Class
3725 (Bitmap : in out Character_Class;
3728 Value : constant Class_Byte := Character'Pos (C);
3730 Bitmap (Value / 8) := Bitmap (Value / 8)
3731 or Bit_Conversion (Value mod 8);
3738 function String_Length
3739 (Program : Program_Data;
3740 P : Pointer) return Program_Size
3743 pragma Assert (Program (P) = EXACT or else Program (P) = EXACTF);
3744 return Character'Pos (Program (P + Next_Pointer_Bytes));
3747 --------------------
3748 -- String_Operand --
3749 --------------------
3751 function String_Operand (P : Pointer) return Pointer is