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-2009, 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 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
21 -- Boston, MA 02110-1301, USA. --
23 -- As a special exception, if other files instantiate generics from this --
24 -- unit, or you link this unit with other files to produce an executable, --
25 -- this unit does not by itself cause the resulting executable to be --
26 -- covered by the GNU General Public License. This exception does not --
27 -- however invalidate any other reasons why the executable file might be --
28 -- covered by the GNU Public License. --
30 -- GNAT was originally developed by the GNAT team at New York University. --
31 -- Extensive contributions were provided by Ada Core Technologies Inc. --
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 Ada
.Unchecked_Conversion
;
48 package body System
.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 multiline (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 multiline (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
;
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
282 function Get_Next_Offset
283 (Program
: Program_Data
;
284 IP
: Pointer
) return Pointer
;
285 -- Get the offset field of a node. Used by Get_Next
288 (Program
: Program_Data
;
289 IP
: Pointer
) return Pointer
;
290 -- Dig the next instruction pointer out of a node
292 procedure Optimize
(Self
: in out Pattern_Matcher
);
293 -- Optimize a Pattern_Matcher by noting certain special cases
295 function Read_Natural
296 (Program
: Program_Data
;
297 IP
: Pointer
) return Natural;
298 -- Return the 2-byte natural coded at position IP
300 -- All of the subprograms above are tiny and should be inlined
303 pragma Inline
(Is_Alnum
);
304 pragma Inline
(Is_White_Space
);
305 pragma Inline
(Get_Next
);
306 pragma Inline
(Get_Next_Offset
);
307 pragma Inline
(Operand
);
308 pragma Inline
(Read_Natural
);
309 pragma Inline
(String_Length
);
310 pragma Inline
(String_Operand
);
312 type Expression_Flags
is record
313 Has_Width
, -- Known never to match null string
314 Simple
, -- Simple enough to be STAR/PLUS operand
315 SP_Start
: Boolean; -- Starts with * or +
318 Worst_Expression
: constant Expression_Flags
:= (others => False);
325 function "=" (Left
: Character; Right
: Opcode
) return Boolean is
327 return Character'Pos (Left
) = Opcode
'Pos (Right
);
334 procedure Bitmap_Operand
335 (Program
: Program_Data
;
337 Op
: out Character_Class
)
339 function Convert
is new Ada
.Unchecked_Conversion
340 (Program_Data
, Character_Class
);
343 Op
(0 .. 31) := Convert
(Program
(P
+ 3 .. P
+ 34));
351 (Matcher
: out Pattern_Matcher
;
353 Final_Code_Size
: out Program_Size
;
354 Flags
: Regexp_Flags
:= No_Flags
)
356 -- We can't allocate space until we know how big the compiled form
357 -- will be, but we can't compile it (and thus know how big it is)
358 -- until we've got a place to put the code. So we cheat: we compile
359 -- it twice, once with code generation turned off and size counting
360 -- turned on, and once "for real".
362 -- This also means that we don't allocate space until we are sure
363 -- that the thing really will compile successfully, and we never
364 -- have to move the code and thus invalidate pointers into it.
366 -- Beware that the optimization-preparation code in here knows
367 -- about some of the structure of the compiled regexp.
369 PM
: Pattern_Matcher
renames Matcher
;
370 Program
: Program_Data
renames PM
.Program
;
372 Emit_Code
: constant Boolean := PM
.Size
> 0;
373 Emit_Ptr
: Pointer
:= Program_First
;
375 Parse_Pos
: Natural := Expression
'First; -- Input-scan pointer
376 Parse_End
: constant Natural := Expression
'Last;
378 ----------------------------
379 -- Subprograms for Create --
380 ----------------------------
382 procedure Emit
(B
: Character);
383 -- Output the Character B to the Program. If code-generation is
384 -- disabled, simply increments the program counter.
386 function Emit_Node
(Op
: Opcode
) return Pointer
;
387 -- If code-generation is enabled, Emit_Node outputs the
388 -- opcode Op and reserves space for a pointer to the next node.
389 -- Return value is the location of new opcode, i.e. old Emit_Ptr.
391 procedure Emit_Natural
(IP
: Pointer
; N
: Natural);
392 -- Split N on two characters at position IP
394 procedure Emit_Class
(Bitmap
: Character_Class
);
395 -- Emits a character class
397 procedure Case_Emit
(C
: Character);
398 -- Emit C, after converting is to lower-case if the regular
399 -- expression is case insensitive.
402 (Parenthesized
: Boolean;
403 Flags
: out Expression_Flags
;
405 -- Parse regular expression, i.e. main body or parenthesized thing
406 -- Caller must absorb opening parenthesis.
408 procedure Parse_Branch
409 (Flags
: out Expression_Flags
;
412 -- Implements the concatenation operator and handles '|'
413 -- First should be true if this is the first item of the alternative.
415 procedure Parse_Piece
416 (Expr_Flags
: out Expression_Flags
;
418 -- Parse something followed by possible [*+?]
421 (Expr_Flags
: out Expression_Flags
;
423 -- Parse_Atom is the lowest level parse procedure.
424 -- Optimization: gobbles an entire sequence of ordinary characters
425 -- so that it can turn them into a single node, which is smaller to
426 -- store and faster to run. Backslashed characters are exceptions,
427 -- each becoming a separate node; the code is simpler that way and
428 -- it's not worth fixing.
430 procedure Insert_Operator
433 Greedy
: Boolean := True);
434 -- Insert_Operator inserts an operator in front of an
435 -- already-emitted operand and relocates the operand.
436 -- This applies to PLUS and STAR.
437 -- If Minmod is True, then the operator is non-greedy.
439 procedure Insert_Curly_Operator
444 Greedy
: Boolean := True);
445 -- Insert an operator for CURLY ({Min}, {Min,} or {Min,Max}).
446 -- If Minmod is True, then the operator is non-greedy.
448 procedure Link_Tail
(P
, Val
: Pointer
);
449 -- Link_Tail sets the next-pointer at the end of a node chain
451 procedure Link_Operand_Tail
(P
, Val
: Pointer
);
452 -- Link_Tail on operand of first argument; noop if operand-less
454 function Next_Instruction
(P
: Pointer
) return Pointer
;
455 -- Dig the "next" pointer out of a node
457 procedure Fail
(M
: String);
458 pragma No_Return
(Fail
);
459 -- Fail with a diagnostic message, if possible
461 function Is_Curly_Operator
(IP
: Natural) return Boolean;
462 -- Return True if IP is looking at a '{' that is the beginning
463 -- of a curly operator, i.e. it matches {\d+,?\d*}
465 function Is_Mult
(IP
: Natural) return Boolean;
466 -- Return True if C is a regexp multiplier: '+', '*' or '?'
468 procedure Get_Curly_Arguments
472 Greedy
: out Boolean);
473 -- Parse the argument list for a curly operator.
474 -- It is assumed that IP is indeed pointing at a valid operator.
475 -- So what is IP and how come IP is not referenced in the body ???
477 procedure Parse_Character_Class
(IP
: out Pointer
);
478 -- Parse a character class.
479 -- The calling subprogram should consume the opening '[' before.
481 procedure Parse_Literal
482 (Expr_Flags
: out Expression_Flags
;
484 -- Parse_Literal encodes a string of characters to be matched exactly
486 function Parse_Posix_Character_Class
return Std_Class
;
487 -- Parse a posix character class, like [:alpha:] or [:^alpha:].
488 -- The caller is supposed to absorb the opening [.
490 pragma Inline
(Is_Mult
);
491 pragma Inline
(Emit_Natural
);
492 pragma Inline
(Parse_Character_Class
); -- since used only once
498 procedure Case_Emit
(C
: Character) is
500 if (Flags
and Case_Insensitive
) /= 0 then
504 -- Dump current character
514 procedure Emit
(B
: Character) is
517 Program
(Emit_Ptr
) := B
;
520 Emit_Ptr
:= Emit_Ptr
+ 1;
527 procedure Emit_Class
(Bitmap
: Character_Class
) is
528 subtype Program31
is Program_Data
(0 .. 31);
530 function Convert
is new Ada
.Unchecked_Conversion
531 (Character_Class
, Program31
);
535 Program
(Emit_Ptr
.. Emit_Ptr
+ 31) := Convert
(Bitmap
);
538 Emit_Ptr
:= Emit_Ptr
+ 32;
545 procedure Emit_Natural
(IP
: Pointer
; N
: Natural) is
548 Program
(IP
+ 1) := Character'Val (N
/ 256);
549 Program
(IP
) := Character'Val (N
mod 256);
557 function Emit_Node
(Op
: Opcode
) return Pointer
is
558 Result
: constant Pointer
:= Emit_Ptr
;
562 Program
(Emit_Ptr
) := Character'Val (Opcode
'Pos (Op
));
563 Program
(Emit_Ptr
+ 1) := ASCII
.NUL
;
564 Program
(Emit_Ptr
+ 2) := ASCII
.NUL
;
567 Emit_Ptr
:= Emit_Ptr
+ 3;
575 procedure Fail
(M
: String) is
577 raise Expression_Error
with M
;
580 -------------------------
581 -- Get_Curly_Arguments --
582 -------------------------
584 procedure Get_Curly_Arguments
588 Greedy
: out Boolean)
590 pragma Unreferenced
(IP
);
592 Save_Pos
: Natural := Parse_Pos
+ 1;
596 Max
:= Max_Curly_Repeat
;
598 while Expression
(Parse_Pos
) /= '}'
599 and then Expression
(Parse_Pos
) /= ','
601 Parse_Pos
:= Parse_Pos
+ 1;
604 Min
:= Natural'Value (Expression
(Save_Pos
.. Parse_Pos
- 1));
606 if Expression
(Parse_Pos
) = ',' then
607 Save_Pos
:= Parse_Pos
+ 1;
608 while Expression
(Parse_Pos
) /= '}' loop
609 Parse_Pos
:= Parse_Pos
+ 1;
612 if Save_Pos
/= Parse_Pos
then
613 Max
:= Natural'Value (Expression
(Save_Pos
.. Parse_Pos
- 1));
620 if Parse_Pos
< Expression
'Last
621 and then Expression
(Parse_Pos
+ 1) = '?'
624 Parse_Pos
:= Parse_Pos
+ 1;
629 end Get_Curly_Arguments
;
631 ---------------------------
632 -- Insert_Curly_Operator --
633 ---------------------------
635 procedure Insert_Curly_Operator
640 Greedy
: Boolean := True)
642 Dest
: constant Pointer
:= Emit_Ptr
;
647 -- If the operand is not greedy, insert an extra operand before it
653 -- Move the operand in the byte-compilation, so that we can insert
654 -- the operator before it.
657 Program
(Operand
+ Size
.. Emit_Ptr
+ Size
) :=
658 Program
(Operand
.. Emit_Ptr
);
661 -- Insert the operator at the position previously occupied by the
667 Old
:= Emit_Node
(MINMOD
);
668 Link_Tail
(Old
, Old
+ 3);
671 Old
:= Emit_Node
(Op
);
672 Emit_Natural
(Old
+ 3, Min
);
673 Emit_Natural
(Old
+ 5, Max
);
675 Emit_Ptr
:= Dest
+ Size
;
676 end Insert_Curly_Operator
;
678 ---------------------
679 -- Insert_Operator --
680 ---------------------
682 procedure Insert_Operator
685 Greedy
: Boolean := True)
687 Dest
: constant Pointer
:= Emit_Ptr
;
692 pragma Warnings
(Off
, Discard
);
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 Discard
:= 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
: Boolean;
852 Flags
: 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 else 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 else 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
: 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;
993 (if (Flags
and Multiple_Lines
) /= 0 then MBOL
994 elsif (Flags
and Single_Line
) /= 0 then SBOL
1000 (if (Flags
and Multiple_Lines
) /= 0 then MEOL
1001 elsif (Flags
and Single_Line
) /= 0 then SEOL
1007 (if (Flags
and Single_Line
) /= 0 then SANY
else ANY
);
1009 Expr_Flags
.Has_Width
:= True;
1010 Expr_Flags
.Simple
:= True;
1013 Parse_Character_Class
(IP
);
1014 Expr_Flags
.Has_Width
:= True;
1015 Expr_Flags
.Simple
:= True;
1019 New_Flags
: Expression_Flags
;
1022 Parse
(True, New_Flags
, IP
);
1028 Expr_Flags
.Has_Width
:=
1029 Expr_Flags
.Has_Width
or else New_Flags
.Has_Width
;
1030 Expr_Flags
.SP_Start
:=
1031 Expr_Flags
.SP_Start
or else New_Flags
.SP_Start
;
1034 when '|' | ASCII
.LF |
')' =>
1035 Fail
("internal urp"); -- Supposed to be caught earlier
1037 when '?' |
'+' |
'*' =>
1038 Fail
(C
& " follows nothing");
1041 if Is_Curly_Operator
(Parse_Pos
- 1) then
1042 Fail
(C
& " follows nothing");
1044 Parse_Literal
(Expr_Flags
, IP
);
1048 if Parse_Pos
> Parse_End
then
1049 Fail
("trailing \");
1052 Parse_Pos := Parse_Pos + 1;
1054 case Expression (Parse_Pos - 1) is
1056 IP := Emit_Node (BOUND);
1059 IP := Emit_Node (NBOUND);
1062 IP := Emit_Node (SPACE);
1063 Expr_Flags.Simple := True;
1064 Expr_Flags.Has_Width := True;
1067 IP := Emit_Node (NSPACE);
1068 Expr_Flags.Simple := True;
1069 Expr_Flags.Has_Width := True;
1072 IP := Emit_Node (DIGIT);
1073 Expr_Flags.Simple := True;
1074 Expr_Flags.Has_Width := True;
1077 IP := Emit_Node (NDIGIT);
1078 Expr_Flags.Simple := True;
1079 Expr_Flags.Has_Width := True;
1082 IP := Emit_Node (ALNUM);
1083 Expr_Flags.Simple := True;
1084 Expr_Flags.Has_Width := True;
1087 IP := Emit_Node (NALNUM);
1088 Expr_Flags.Simple := True;
1089 Expr_Flags.Has_Width := True;
1092 IP := Emit_Node (SBOL);
1095 IP := Emit_Node (SEOL);
1098 IP := Emit_Node (REFF);
1101 Save : constant Natural := Parse_Pos - 1;
1104 while Parse_Pos <= Expression'Last
1105 and then Is_Digit (Expression (Parse_Pos))
1107 Parse_Pos := Parse_Pos + 1;
1110 Emit (Character'Val (Natural'Value
1111 (Expression (Save .. Parse_Pos - 1))));
1115 Parse_Pos := Parse_Pos - 1;
1116 Parse_Literal (Expr_Flags, IP);
1120 Parse_Literal (Expr_Flags, IP);
1128 procedure Parse_Branch
1129 (Flags : out Expression_Flags;
1133 E : String renames Expression;
1136 New_Flags : Expression_Flags;
1139 pragma Warnings (Off, Discard);
1142 Flags := Worst_Expression; -- Tentatively
1143 IP := (if First then Emit_Ptr else Emit_Node (BRANCH));
1146 while Parse_Pos <= Parse_End
1147 and then E (Parse_Pos) /= ')'
1148 and then E (Parse_Pos) /= ASCII.LF
1149 and then E (Parse_Pos) /= '|'
1151 Parse_Piece (New_Flags, Last);
1158 Flags.Has_Width := Flags.Has_Width or else New_Flags.Has_Width;
1160 if Chain = 0 then -- First piece
1161 Flags.SP_Start := Flags.SP_Start or else New_Flags.SP_Start;
1163 Link_Tail (Chain, Last);
1169 -- Case where loop ran zero CURLY
1172 Discard := Emit_Node (NOTHING);
1176 ---------------------------
1177 -- Parse_Character_Class --
1178 ---------------------------
1180 procedure Parse_Character_Class (IP : out Pointer) is
1181 Bitmap : Character_Class;
1182 Invert : Boolean := False;
1183 In_Range : Boolean := False;
1184 Named_Class : Std_Class := ANYOF_NONE;
1186 Last_Value : Character := ASCII.NUL;
1189 Reset_Class (Bitmap);
1191 -- Do we have an invert character class ?
1193 if Parse_Pos <= Parse_End
1194 and then Expression (Parse_Pos) = '^'
1197 Parse_Pos := Parse_Pos + 1;
1200 -- First character can be ] or - without closing the class
1202 if Parse_Pos <= Parse_End
1203 and then (Expression (Parse_Pos) = ']'
1204 or else Expression (Parse_Pos) = '-')
1206 Set_In_Class (Bitmap, Expression (Parse_Pos));
1207 Parse_Pos := Parse_Pos + 1;
1210 -- While we don't have the end of the class
1212 while Parse_Pos <= Parse_End
1213 and then Expression (Parse_Pos) /= ']'
1215 Named_Class := ANYOF_NONE;
1216 Value := Expression (Parse_Pos);
1217 Parse_Pos := Parse_Pos + 1;
1219 -- Do we have a Posix character class
1221 Named_Class := Parse_Posix_Character_Class;
1223 elsif Value = '\' then
1224 if Parse_Pos = Parse_End then
1225 Fail ("Trailing
\");
1227 Value
:= Expression
(Parse_Pos
);
1228 Parse_Pos
:= Parse_Pos
+ 1;
1231 when 'w' => Named_Class
:= ANYOF_ALNUM
;
1232 when 'W' => Named_Class
:= ANYOF_NALNUM
;
1233 when 's' => Named_Class
:= ANYOF_SPACE
;
1234 when 'S' => Named_Class
:= ANYOF_NSPACE
;
1235 when 'd' => Named_Class
:= ANYOF_DIGIT
;
1236 when 'D' => Named_Class
:= ANYOF_NDIGIT
;
1237 when 'n' => Value
:= ASCII
.LF
;
1238 when 'r' => Value
:= ASCII
.CR
;
1239 when 't' => Value
:= ASCII
.HT
;
1240 when 'f' => Value
:= ASCII
.FF
;
1241 when 'e' => Value
:= ASCII
.ESC
;
1242 when 'a' => Value
:= ASCII
.BEL
;
1244 -- when 'x' => ??? hexadecimal value
1245 -- when 'c' => ??? control character
1246 -- when '0'..'9' => ??? octal character
1248 when others => null;
1252 -- Do we have a character class?
1254 if Named_Class
/= ANYOF_NONE
then
1256 -- A range like 'a-\d' or 'a-[:digit:] is not a range
1259 Set_In_Class
(Bitmap
, Last_Value
);
1260 Set_In_Class
(Bitmap
, '-');
1267 when ANYOF_NONE
=> null;
1269 when ANYOF_ALNUM | ANYOF_ALNUMC
=>
1270 for Value
in Class_Byte
'Range loop
1271 if Is_Alnum
(Character'Val (Value
)) then
1272 Set_In_Class
(Bitmap
, Character'Val (Value
));
1276 when ANYOF_NALNUM | ANYOF_NALNUMC
=>
1277 for Value
in Class_Byte
'Range loop
1278 if not Is_Alnum
(Character'Val (Value
)) then
1279 Set_In_Class
(Bitmap
, Character'Val (Value
));
1284 for Value
in Class_Byte
'Range loop
1285 if Is_White_Space
(Character'Val (Value
)) then
1286 Set_In_Class
(Bitmap
, Character'Val (Value
));
1290 when ANYOF_NSPACE
=>
1291 for Value
in Class_Byte
'Range loop
1292 if not Is_White_Space
(Character'Val (Value
)) then
1293 Set_In_Class
(Bitmap
, Character'Val (Value
));
1298 for Value
in Class_Byte
'Range loop
1299 if Is_Digit
(Character'Val (Value
)) then
1300 Set_In_Class
(Bitmap
, Character'Val (Value
));
1304 when ANYOF_NDIGIT
=>
1305 for Value
in Class_Byte
'Range loop
1306 if not Is_Digit
(Character'Val (Value
)) then
1307 Set_In_Class
(Bitmap
, Character'Val (Value
));
1312 for Value
in Class_Byte
'Range loop
1313 if Is_Letter
(Character'Val (Value
)) then
1314 Set_In_Class
(Bitmap
, Character'Val (Value
));
1318 when ANYOF_NALPHA
=>
1319 for Value
in Class_Byte
'Range loop
1320 if not Is_Letter
(Character'Val (Value
)) then
1321 Set_In_Class
(Bitmap
, Character'Val (Value
));
1326 for Value
in 0 .. 127 loop
1327 Set_In_Class
(Bitmap
, Character'Val (Value
));
1330 when ANYOF_NASCII
=>
1331 for Value
in 128 .. 255 loop
1332 Set_In_Class
(Bitmap
, Character'Val (Value
));
1336 for Value
in Class_Byte
'Range loop
1337 if Is_Control
(Character'Val (Value
)) then
1338 Set_In_Class
(Bitmap
, Character'Val (Value
));
1342 when ANYOF_NCNTRL
=>
1343 for Value
in Class_Byte
'Range loop
1344 if not Is_Control
(Character'Val (Value
)) then
1345 Set_In_Class
(Bitmap
, Character'Val (Value
));
1350 for Value
in Class_Byte
'Range loop
1351 if Is_Graphic
(Character'Val (Value
)) then
1352 Set_In_Class
(Bitmap
, Character'Val (Value
));
1356 when ANYOF_NGRAPH
=>
1357 for Value
in Class_Byte
'Range loop
1358 if not Is_Graphic
(Character'Val (Value
)) then
1359 Set_In_Class
(Bitmap
, Character'Val (Value
));
1364 for Value
in Class_Byte
'Range loop
1365 if Is_Lower
(Character'Val (Value
)) then
1366 Set_In_Class
(Bitmap
, Character'Val (Value
));
1370 when ANYOF_NLOWER
=>
1371 for Value
in Class_Byte
'Range loop
1372 if not Is_Lower
(Character'Val (Value
)) then
1373 Set_In_Class
(Bitmap
, Character'Val (Value
));
1378 for Value
in Class_Byte
'Range loop
1379 if Is_Printable
(Character'Val (Value
)) then
1380 Set_In_Class
(Bitmap
, Character'Val (Value
));
1384 when ANYOF_NPRINT
=>
1385 for Value
in Class_Byte
'Range loop
1386 if not Is_Printable
(Character'Val (Value
)) then
1387 Set_In_Class
(Bitmap
, Character'Val (Value
));
1392 for Value
in Class_Byte
'Range loop
1393 if Is_Printable
(Character'Val (Value
))
1394 and then not Is_White_Space
(Character'Val (Value
))
1395 and then not Is_Alnum
(Character'Val (Value
))
1397 Set_In_Class
(Bitmap
, Character'Val (Value
));
1401 when ANYOF_NPUNCT
=>
1402 for Value
in Class_Byte
'Range loop
1403 if not Is_Printable
(Character'Val (Value
))
1404 or else Is_White_Space
(Character'Val (Value
))
1405 or else Is_Alnum
(Character'Val (Value
))
1407 Set_In_Class
(Bitmap
, Character'Val (Value
));
1412 for Value
in Class_Byte
'Range loop
1413 if Is_Upper
(Character'Val (Value
)) then
1414 Set_In_Class
(Bitmap
, Character'Val (Value
));
1418 when ANYOF_NUPPER
=>
1419 for Value
in Class_Byte
'Range loop
1420 if not Is_Upper
(Character'Val (Value
)) then
1421 Set_In_Class
(Bitmap
, Character'Val (Value
));
1425 when ANYOF_XDIGIT
=>
1426 for Value
in Class_Byte
'Range loop
1427 if Is_Hexadecimal_Digit
(Character'Val (Value
)) then
1428 Set_In_Class
(Bitmap
, Character'Val (Value
));
1432 when ANYOF_NXDIGIT
=>
1433 for Value
in Class_Byte
'Range loop
1434 if not Is_Hexadecimal_Digit
1435 (Character'Val (Value
))
1437 Set_In_Class
(Bitmap
, Character'Val (Value
));
1443 -- Not a character range
1445 elsif not In_Range
then
1446 Last_Value
:= Value
;
1448 if Parse_Pos
> Expression
'Last then
1449 Fail
("Empty character class []");
1452 if Expression
(Parse_Pos
) = '-'
1453 and then Parse_Pos
< Parse_End
1454 and then Expression
(Parse_Pos
+ 1) /= ']'
1456 Parse_Pos
:= Parse_Pos
+ 1;
1458 -- Do we have a range like '\d-a' and '[:space:]-a'
1459 -- which is not a real range
1461 if Named_Class
/= ANYOF_NONE
then
1462 Set_In_Class
(Bitmap
, '-');
1468 Set_In_Class
(Bitmap
, Value
);
1472 -- Else in a character range
1475 if Last_Value
> Value
then
1476 Fail
("Invalid Range [" & Last_Value
'Img
1477 & "-" & Value
'Img & "]");
1480 while Last_Value
<= Value
loop
1481 Set_In_Class
(Bitmap
, Last_Value
);
1482 Last_Value
:= Character'Succ (Last_Value
);
1491 -- Optimize case-insensitive ranges (put the upper case or lower
1492 -- case character into the bitmap)
1494 if (Flags
and Case_Insensitive
) /= 0 then
1495 for C
in Character'Range loop
1496 if Get_From_Class
(Bitmap
, C
) then
1497 Set_In_Class
(Bitmap
, To_Lower
(C
));
1498 Set_In_Class
(Bitmap
, To_Upper
(C
));
1503 -- Optimize inverted classes
1506 for J
in Bitmap
'Range loop
1507 Bitmap
(J
) := not Bitmap
(J
);
1511 Parse_Pos
:= Parse_Pos
+ 1;
1515 IP
:= Emit_Node
(ANYOF
);
1516 Emit_Class
(Bitmap
);
1517 end Parse_Character_Class
;
1523 -- This is a bit tricky due to quoted chars and due to
1524 -- the multiplier characters '*', '+', and '?' that
1525 -- take the SINGLE char previous as their operand.
1527 -- On entry, the character at Parse_Pos - 1 is going to go
1528 -- into the string, no matter what it is. It could be
1529 -- following a \ if Parse_Atom was entered from the '\' case.
1531 -- Basic idea is to pick up a good char in C and examine
1532 -- the next char. If Is_Mult (C) then twiddle, if it's a \
1533 -- then frozzle and if it's another magic char then push C and
1534 -- terminate the string. If none of the above, push C on the
1535 -- string and go around again.
1537 -- Start_Pos is used to remember where "the current character"
1538 -- starts in the string, if due to an Is_Mult we need to back
1539 -- up and put the current char in a separate 1-character string.
1540 -- When Start_Pos is 0, C is the only char in the string;
1541 -- this is used in Is_Mult handling, and in setting the SIMPLE
1544 procedure Parse_Literal
1545 (Expr_Flags
: out Expression_Flags
;
1548 Start_Pos
: Natural := 0;
1550 Length_Ptr
: Pointer
;
1552 Has_Special_Operator
: Boolean := False;
1555 Parse_Pos
:= Parse_Pos
- 1; -- Look at current character
1559 (if (Flags
and Case_Insensitive
) /= 0 then EXACTF
else EXACT
);
1561 Length_Ptr
:= Emit_Ptr
;
1562 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 ?
1587 elsif not Is_Mult
(Parse_Pos
) then
1588 Start_Pos
:= Parse_Pos
;
1592 -- We've got something like "abc?d". Mark this as a
1593 -- special case. What we want to emit is a first
1594 -- constant string for "ab", then one for "c" that will
1595 -- ultimately be transformed with a CURLY operator, A
1596 -- special case has to be handled for "a?", since there
1597 -- is no initial string to emit.
1599 Has_Special_Operator
:= True;
1604 Start_Pos
:= Parse_Pos
;
1606 if Parse_Pos
= Parse_End
then
1607 Fail
("Trailing \");
1610 case Expression (Parse_Pos + 1) is
1611 when 'b' | 'B' | 's' | 'S' | 'd' | 'D'
1612 | 'w' | 'W' | '0' .. '9' | 'G' | 'A'
1614 when 'n' => Emit (ASCII.LF);
1615 when 't' => Emit (ASCII.HT);
1616 when 'r' => Emit (ASCII.CR);
1617 when 'f' => Emit (ASCII.FF);
1618 when 'e' => Emit (ASCII.ESC);
1619 when 'a' => Emit (ASCII.BEL);
1620 when others => Emit (Expression (Parse_Pos + 1));
1623 Parse_Pos := Parse_Pos + 1;
1627 Start_Pos := Parse_Pos;
1631 exit Parse_Loop when Emit_Ptr - Length_Ptr = 254;
1633 Parse_Pos := Parse_Pos + 1;
1635 exit Parse_Loop when Parse_Pos > Parse_End;
1636 end loop Parse_Loop;
1638 -- Is the string followed by a '*+?{' operator ? If yes, and if there
1639 -- is an initial string to emit, do it now.
1641 if Has_Special_Operator
1642 and then Emit_Ptr >= Length_Ptr + 3
1644 Emit_Ptr := Emit_Ptr - 1;
1645 Parse_Pos := Start_Pos;
1649 Program (Length_Ptr) := Character'Val (Emit_Ptr - Length_Ptr - 2);
1652 Expr_Flags.Has_Width := True;
1654 -- Slight optimization when there is a single character
1656 if Emit_Ptr = Length_Ptr + 2 then
1657 Expr_Flags.Simple := True;
1665 -- Note that the branching code sequences used for '?' and the
1666 -- general cases of '*' and + are somewhat optimized: they use
1667 -- the same NOTHING node as both the endmarker for their branch
1668 -- list and the body of the last branch. It might seem that
1669 -- this node could be dispensed with entirely, but the endmarker
1670 -- role is not redundant.
1672 procedure Parse_Piece
1673 (Expr_Flags : out Expression_Flags;
1677 New_Flags : Expression_Flags;
1678 Greedy : Boolean := True;
1681 Parse_Atom (New_Flags, IP);
1687 if Parse_Pos > Parse_End
1688 or else not Is_Mult (Parse_Pos)
1690 Expr_Flags := New_Flags;
1694 Op := Expression (Parse_Pos);
1698 then (SP_Start => True, others => False)
1699 else (Has_Width => True, others => False));
1701 -- Detect non greedy operators in the easy cases
1704 and then Parse_Pos + 1 <= Parse_End
1705 and then Expression (Parse_Pos + 1) = '?'
1708 Parse_Pos := Parse_Pos + 1;
1711 -- Generate the byte code
1716 if New_Flags.Simple then
1717 Insert_Operator (STAR, IP, Greedy);
1719 Link_Tail (IP, Emit_Node (WHILEM));
1720 Insert_Curly_Operator
1721 (CURLYX, 0, Max_Curly_Repeat, IP, Greedy);
1722 Link_Tail (IP, Emit_Node (NOTHING));
1727 if New_Flags.Simple then
1728 Insert_Operator (PLUS, IP, Greedy);
1730 Link_Tail (IP, Emit_Node (WHILEM));
1731 Insert_Curly_Operator
1732 (CURLYX, 1, Max_Curly_Repeat, IP, Greedy);
1733 Link_Tail (IP, Emit_Node (NOTHING));
1737 if New_Flags.Simple then
1738 Insert_Curly_Operator (CURLY, 0, 1, IP, Greedy);
1740 Link_Tail (IP, Emit_Node (WHILEM));
1741 Insert_Curly_Operator (CURLYX, 0, 1, IP, Greedy);
1742 Link_Tail (IP, Emit_Node (NOTHING));
1750 Get_Curly_Arguments (Parse_Pos, Min, Max, Greedy);
1752 if New_Flags.Simple then
1753 Insert_Curly_Operator (CURLY, Min, Max, IP, Greedy);
1755 Link_Tail (IP, Emit_Node (WHILEM));
1756 Insert_Curly_Operator (CURLYX, Min, Max, IP, Greedy);
1757 Link_Tail (IP, Emit_Node (NOTHING));
1765 Parse_Pos := Parse_Pos + 1;
1767 if Parse_Pos <= Parse_End
1768 and then Is_Mult (Parse_Pos)
1770 Fail ("nested
*+{");
1774 ---------------------------------
1775 -- Parse_Posix_Character_Class --
1776 ---------------------------------
1778 function Parse_Posix_Character_Class return Std_Class is
1779 Invert : Boolean := False;
1780 Class : Std_Class := ANYOF_NONE;
1781 E : String renames Expression;
1783 -- Class names. Note that code assumes that the length of all
1784 -- classes starting with the same letter have the same length.
1786 Alnum : constant String := "alnum
:]";
1787 Alpha : constant String := "alpha
:]";
1788 Ascii_C : constant String := "ascii
:]";
1789 Cntrl : constant String := "cntrl
:]";
1790 Digit : constant String := "digit
:]";
1791 Graph : constant String := "graph
:]";
1792 Lower : constant String := "lower
:]";
1793 Print : constant String := "print
:]";
1794 Punct : constant String := "punct
:]";
1795 Space : constant String := "space
:]";
1796 Upper : constant String := "upper
:]";
1797 Word : constant String := "word
:]";
1798 Xdigit : constant String := "xdigit
:]";
1801 -- Case of character class specified
1803 if Parse_Pos <= Parse_End
1804 and then Expression (Parse_Pos) = ':'
1806 Parse_Pos := Parse_Pos + 1;
1808 -- Do we have something like: [[:^alpha:]]
1810 if Parse_Pos <= Parse_End
1811 and then Expression (Parse_Pos) = '^'
1814 Parse_Pos := Parse_Pos + 1;
1817 -- Check for class names based on first letter
1819 case Expression (Parse_Pos) is
1822 -- All 'a' classes have the same length (Alnum'Length)
1824 if Parse_Pos + Alnum'Length - 1 <= Parse_End then
1826 E (Parse_Pos .. Parse_Pos + Alnum'Length - 1) = Alnum
1829 (if Invert then ANYOF_NALNUMC else ANYOF_ALNUMC);
1830 Parse_Pos := Parse_Pos + Alnum'Length;
1833 E (Parse_Pos .. Parse_Pos + Alpha'Length - 1) = Alpha
1836 (if Invert then ANYOF_NALPHA else ANYOF_ALPHA);
1837 Parse_Pos := Parse_Pos + Alpha'Length;
1839 elsif E (Parse_Pos .. Parse_Pos + Ascii_C'Length - 1) =
1843 (if Invert then ANYOF_NASCII else ANYOF_ASCII);
1844 Parse_Pos := Parse_Pos + Ascii_C'Length;
1846 Fail ("Invalid
character class
: " & E);
1850 Fail ("Invalid
character class
: " & E);
1854 if Parse_Pos + Cntrl'Length - 1 <= Parse_End
1856 E (Parse_Pos .. Parse_Pos + Cntrl'Length - 1) = Cntrl
1858 Class := (if Invert then ANYOF_NCNTRL else ANYOF_CNTRL);
1859 Parse_Pos := Parse_Pos + Cntrl'Length;
1861 Fail ("Invalid
character class
: " & E);
1865 if Parse_Pos + Digit'Length - 1 <= Parse_End
1867 E (Parse_Pos .. Parse_Pos + Digit'Length - 1) = Digit
1869 Class := (if Invert then ANYOF_NDIGIT else ANYOF_DIGIT);
1870 Parse_Pos := Parse_Pos + Digit'Length;
1874 if Parse_Pos + Graph'Length - 1 <= Parse_End
1876 E (Parse_Pos .. Parse_Pos + Graph'Length - 1) = Graph
1878 Class := (if Invert then ANYOF_NGRAPH else ANYOF_GRAPH);
1879 Parse_Pos := Parse_Pos + Graph'Length;
1881 Fail ("Invalid
character class
: " & E);
1885 if Parse_Pos + Lower'Length - 1 <= Parse_End
1887 E (Parse_Pos .. Parse_Pos + Lower'Length - 1) = Lower
1889 Class := (if Invert then ANYOF_NLOWER else ANYOF_LOWER);
1890 Parse_Pos := Parse_Pos + Lower'Length;
1892 Fail ("Invalid
character class
: " & E);
1897 -- All 'p' classes have the same length
1899 if Parse_Pos + Print'Length - 1 <= Parse_End then
1901 E (Parse_Pos .. Parse_Pos + Print'Length - 1) = Print
1904 (if Invert then ANYOF_NPRINT else ANYOF_PRINT);
1905 Parse_Pos := Parse_Pos + Print'Length;
1908 E (Parse_Pos .. Parse_Pos + Punct'Length - 1) = Punct
1911 (if Invert then ANYOF_NPUNCT else ANYOF_PUNCT);
1912 Parse_Pos := Parse_Pos + Punct'Length;
1915 Fail ("Invalid
character class
: " & E);
1919 Fail ("Invalid
character class
: " & E);
1923 if Parse_Pos + Space'Length - 1 <= Parse_End
1925 E (Parse_Pos .. Parse_Pos + Space'Length - 1) = Space
1927 Class := (if Invert then ANYOF_NSPACE else ANYOF_SPACE);
1928 Parse_Pos := Parse_Pos + Space'Length;
1930 Fail ("Invalid
character class
: " & E);
1934 if Parse_Pos + Upper'Length - 1 <= Parse_End
1936 E (Parse_Pos .. Parse_Pos + Upper'Length - 1) = Upper
1938 Class := (if Invert then ANYOF_NUPPER else ANYOF_UPPER);
1939 Parse_Pos := Parse_Pos + Upper'Length;
1941 Fail ("Invalid
character class
: " & E);
1945 if Parse_Pos + Word'Length - 1 <= Parse_End
1947 E (Parse_Pos .. Parse_Pos + Word'Length - 1) = Word
1949 Class := (if Invert then ANYOF_NALNUM else ANYOF_ALNUM);
1950 Parse_Pos := Parse_Pos + Word'Length;
1952 Fail ("Invalid
character class
: " & E);
1956 if Parse_Pos + Xdigit'Length - 1 <= Parse_End
1958 E (Parse_Pos .. Parse_Pos + Xdigit'Length - 1) = Xdigit
1960 Class := (if Invert then ANYOF_NXDIGIT else ANYOF_XDIGIT);
1961 Parse_Pos := Parse_Pos + Xdigit'Length;
1964 Fail ("Invalid
character class
: " & E);
1968 Fail ("Invalid
character class
: " & E);
1971 -- Character class not specified
1978 end Parse_Posix_Character_Class;
1980 -- Local Declarations
1984 Expr_Flags : Expression_Flags;
1985 pragma Unreferenced (Expr_Flags);
1987 -- Start of processing for Compile
1991 Parse (False, Expr_Flags, Result);
1994 Fail ("Couldn
't compile expression
");
1997 Final_Code_Size := Emit_Ptr - 1;
1999 -- Do we want to actually compile the expression, or simply get the
2010 (Expression : String;
2011 Flags : Regexp_Flags := No_Flags) return Pattern_Matcher
2013 Size : Program_Size;
2014 Dummy : Pattern_Matcher (0);
2015 pragma Unreferenced (Dummy);
2018 Compile (Dummy, Expression, Size, Flags);
2021 Result : Pattern_Matcher (Size);
2023 Compile (Result, Expression, Size, Flags);
2029 (Matcher : out Pattern_Matcher;
2030 Expression : String;
2031 Flags : Regexp_Flags := No_Flags)
2033 Size : Program_Size;
2034 pragma Unreferenced (Size);
2036 Compile (Matcher, Expression, Size, Flags);
2043 procedure Dump (Self : Pattern_Matcher) is
2045 Program : Program_Data renames Self.Program;
2047 procedure Dump_Until
2050 Indent : Natural := 0);
2051 -- Dump the program until the node Till (not included) is met.
2052 -- Every line is indented with Index spaces at the beginning
2053 -- Dumps till the end if Till is 0.
2059 procedure Dump_Until
2062 Indent : Natural := 0)
2066 Local_Indent : Natural := Indent;
2071 while Index < Till loop
2072 Op := Opcode'Val (Character'Pos ((Self.Program (Index))));
2075 Local_Indent := Local_Indent - 3;
2079 Point : constant String := Pointer'Image (Index);
2082 for J in 1 .. 6 - Point'Length loop
2088 & (1 .. Local_Indent => ' ')
2089 & Opcode'Image (Op));
2092 -- Print the parenthesis number
2094 if Op = OPEN or else Op = CLOSE or else Op = REFF then
2095 Put (Natural'Image (Character'Pos (Program (Index + 3))));
2098 Next := Index + Get_Next_Offset (Program, Index);
2100 if Next = Index then
2101 Put (" (next
at 0)");
2103 Put (" (next
at " & Pointer'Image (Next) & ")");
2108 -- Character class operand
2112 Bitmap : Character_Class;
2113 Last : Character := ASCII.NUL;
2114 Current : Natural := 0;
2116 Current_Char : Character;
2119 Bitmap_Operand (Program, Index, Bitmap);
2122 while Current <= 255 loop
2123 Current_Char := Character'Val (Current);
2125 -- First item in a range
2127 if Get_From_Class (Bitmap, Current_Char) then
2128 Last := Current_Char;
2130 -- Search for the last item in the range
2133 Current := Current + 1;
2134 exit when Current > 255;
2135 Current_Char := Character'Val (Current);
2137 not Get_From_Class (Bitmap, Current_Char);
2147 if Character'Succ (Last) /= Current_Char then
2148 Put ("-" & Character'Pred (Current_Char));
2152 Current := Current + 1;
2157 Index := Index + 3 + Bitmap'Length;
2162 when EXACT | EXACTF =>
2163 Length := String_Length (Program, Index);
2164 Put (" operand
(length
:" & Program_Size'Image (Length + 1)
2166 & String (Program (String_Operand (Index)
2167 .. String_Operand (Index)
2169 Index := String_Operand (Index) + Length + 1;
2176 Dump_Until (Index + 3, Next, Local_Indent + 3);
2182 -- Only one instruction
2184 Dump_Until (Index + 3, Index + 4, Local_Indent + 3);
2187 when CURLY | CURLYX =>
2189 & Natural'Image (Read_Natural (Program, Index + 3))
2191 & Natural'Image (Read_Natural (Program, Index + 5))
2194 Dump_Until (Index + 7, Next, Local_Indent + 3);
2200 Local_Indent := Local_Indent + 3;
2202 when CLOSE | REFF =>
2220 -- Start of processing for Dump
2223 pragma Assert (Self.Program (Program_First) = MAGIC,
2224 "Corrupted Pattern_Matcher
");
2226 Put_Line ("Must start
with (Self
.First
) = "
2227 & Character'Image (Self.First));
2229 if (Self.Flags and Case_Insensitive) /= 0 then
2230 Put_Line (" Case_Insensitive mode
");
2233 if (Self.Flags and Single_Line) /= 0 then
2234 Put_Line (" Single_Line mode
");
2237 if (Self.Flags and Multiple_Lines) /= 0 then
2238 Put_Line (" Multiple_Lines mode
");
2241 Put_Line (" 1 : MAGIC
");
2242 Dump_Until (Program_First + 1, Self.Program'Last + 1);
2245 --------------------
2246 -- Get_From_Class --
2247 --------------------
2249 function Get_From_Class
2250 (Bitmap : Character_Class;
2251 C : Character) return Boolean
2253 Value : constant Class_Byte := Character'Pos (C);
2256 (Bitmap (Value / 8) and Bit_Conversion (Value mod 8)) /= 0;
2263 function Get_Next (Program : Program_Data; IP : Pointer) return Pointer is
2264 Offset : constant Pointer := Get_Next_Offset (Program, IP);
2273 ---------------------
2274 -- Get_Next_Offset --
2275 ---------------------
2277 function Get_Next_Offset
2278 (Program : Program_Data;
2279 IP : Pointer) return Pointer
2282 return Pointer (Read_Natural (Program, IP + 1));
2283 end Get_Next_Offset;
2289 function Is_Alnum (C : Character) return Boolean is
2291 return Is_Alphanumeric (C) or else C = '_';
2298 function Is_Printable (C : Character) return Boolean is
2300 -- Printable if space or graphic character or other whitespace
2301 -- Other white space includes (HT/LF/VT/FF/CR = codes 9-13)
2303 return C in Character'Val (32) .. Character'Val (126)
2304 or else C in ASCII.HT .. ASCII.CR;
2307 --------------------
2308 -- Is_White_Space --
2309 --------------------
2311 function Is_White_Space (C : Character) return Boolean is
2313 -- Note: HT = 9, LF = 10, VT = 11, FF = 12, CR = 13
2315 return C = ' ' or else C in ASCII.HT .. ASCII.CR;
2323 (Self : Pattern_Matcher;
2325 Matches : out Match_Array;
2326 Data_First : Integer := -1;
2327 Data_Last : Positive := Positive'Last)
2329 Program : Program_Data renames Self.Program; -- Shorter notation
2331 First_In_Data : constant Integer := Integer'Max (Data_First, Data'First);
2332 Last_In_Data : constant Integer := Integer'Min (Data_Last, Data'Last);
2334 -- Global work variables
2336 Input_Pos : Natural; -- String-input pointer
2337 BOL_Pos : Natural; -- Beginning of input, for ^ check
2338 Matched : Boolean := False; -- Until proven True
2340 Matches_Full : Match_Array (0 .. Natural'Max (Self.Paren_Count,
2342 -- Stores the value of all the parenthesis pairs.
2343 -- We do not use directly Matches, so that we can also use back
2344 -- references (REFF) even if Matches is too small.
2346 type Natural_Array is array (Match_Count range <>) of Natural;
2347 Matches_Tmp : Natural_Array (Matches_Full'Range);
2348 -- Save the opening position of parenthesis
2350 Last_Paren : Natural := 0;
2351 -- Last parenthesis seen
2353 Greedy : Boolean := True;
2354 -- True if the next operator should be greedy
2356 type Current_Curly_Record;
2357 type Current_Curly_Access is access all Current_Curly_Record;
2358 type Current_Curly_Record is record
2359 Paren_Floor : Natural; -- How far back to strip parenthesis data
2360 Cur : Integer; -- How many instances of scan we've matched
2361 Min : Natural; -- Minimal number of scans to match
2362 Max : Natural; -- Maximal number of scans to match
2363 Greedy : Boolean; -- Whether to work our way up or down
2364 Scan : Pointer; -- The thing to match
2365 Next : Pointer; -- What has to match after it
2366 Lastloc : Natural; -- Where we started matching this scan
2367 Old_Cc : Current_Curly_Access; -- Before we started this one
2369 -- Data used to handle the curly operator and the plus and star
2370 -- operators for complex expressions.
2372 Current_Curly : Current_Curly_Access := null;
2373 -- The curly currently being processed
2375 -----------------------
2376 -- Local Subprograms --
2377 -----------------------
2379 function Index (Start : Positive; C : Character) return Natural;
2380 -- Find character C in Data starting at Start and return position
2384 Max : Natural := Natural'Last) return Natural;
2385 -- Repeatedly match something simple, report how many
2386 -- It only matches on things of length 1.
2387 -- Starting from Input_Pos, it matches at most Max CURLY.
2389 function Try (Pos : Positive) return Boolean;
2390 -- Try to match at specific point
2392 function Match (IP : Pointer) return Boolean;
2393 -- This is the main matching routine. Conceptually the strategy
2394 -- is simple: check to see whether the current node matches,
2395 -- call self recursively to see whether the rest matches,
2396 -- and then act accordingly.
2398 -- In practice Match makes some effort to avoid recursion, in
2399 -- particular by going through "ordinary
" nodes (that don't
2400 -- need to know whether the rest of the match failed) by
2401 -- using a loop instead of recursion.
2402 -- Why is the above comment part of the spec rather than body ???
2404 function Match_Whilem (IP : Pointer) return Boolean;
2405 -- Return True if a WHILEM matches
2406 -- How come IP is unreferenced in the body ???
2408 function Recurse_Match (IP : Pointer; From : Natural) return Boolean;
2409 pragma Inline (Recurse_Match);
2410 -- Calls Match recursively. It saves and restores the parenthesis
2411 -- status and location in the input stream correctly, so that
2412 -- backtracking is possible
2414 function Match_Simple_Operator
2418 Greedy : Boolean) return Boolean;
2419 -- Return True it the simple operator (possibly non-greedy) matches
2421 pragma Inline (Index);
2422 pragma Inline (Repeat);
2424 -- These are two complex functions, but used only once
2426 pragma Inline (Match_Whilem);
2427 pragma Inline (Match_Simple_Operator);
2433 function Index (Start : Positive; C : Character) return Natural is
2435 for J in Start .. Last_In_Data loop
2436 if Data (J) = C then
2448 function Recurse_Match (IP : Pointer; From : Natural) return Boolean is
2449 L : constant Natural := Last_Paren;
2451 Tmp_F : constant Match_Array :=
2452 Matches_Full (From + 1 .. Matches_Full'Last);
2454 Start : constant Natural_Array :=
2455 Matches_Tmp (From + 1 .. Matches_Tmp'Last);
2456 Input : constant Natural := Input_Pos;
2464 Matches_Full (Tmp_F'Range) := Tmp_F;
2465 Matches_Tmp (Start'Range) := Start;
2474 function Match (IP : Pointer) return Boolean is
2475 Scan : Pointer := IP;
2482 pragma Assert (Scan /= 0);
2484 -- Determine current opcode and count its usage in debug mode
2486 Op := Opcode'Val (Character'Pos (Program (Scan)));
2488 -- Calculate offset of next instruction.
2489 -- Second character is most significant in Program_Data.
2491 Next := Get_Next (Program, Scan);
2495 return True; -- Success !
2498 if Program (Next) /= BRANCH then
2499 Next := Operand (Scan); -- No choice, avoid recursion
2503 if Recurse_Match (Operand (Scan), 0) then
2507 Scan := Get_Next (Program, Scan);
2508 exit when Scan = 0 or else Program (Scan) /= BRANCH;
2518 exit State_Machine when Input_Pos /= BOL_Pos
2519 and then ((Self.Flags and Multiple_Lines) = 0
2520 or else Data (Input_Pos - 1) /= ASCII.LF);
2523 exit State_Machine when Input_Pos /= BOL_Pos
2524 and then Data (Input_Pos - 1) /= ASCII.LF;
2527 exit State_Machine when Input_Pos /= BOL_Pos;
2530 exit State_Machine when Input_Pos <= Data'Last
2531 and then ((Self.Flags and Multiple_Lines) = 0
2532 or else Data (Input_Pos) /= ASCII.LF);
2535 exit State_Machine when Input_Pos <= Data'Last
2536 and then Data (Input_Pos) /= ASCII.LF;
2539 exit State_Machine when Input_Pos <= Data'Last;
2541 when BOUND | NBOUND =>
2543 -- Was last char in word ?
2546 N : Boolean := False;
2547 Ln : Boolean := False;
2550 if Input_Pos /= First_In_Data then
2551 N := Is_Alnum (Data (Input_Pos - 1));
2555 (if Input_Pos > Last_In_Data
2557 else Is_Alnum (Data (Input_Pos)));
2571 exit State_Machine when Input_Pos > Last_In_Data
2572 or else not Is_White_Space (Data (Input_Pos));
2573 Input_Pos := Input_Pos + 1;
2576 exit State_Machine when Input_Pos > Last_In_Data
2577 or else Is_White_Space (Data (Input_Pos));
2578 Input_Pos := Input_Pos + 1;
2581 exit State_Machine when Input_Pos > Last_In_Data
2582 or else not Is_Digit (Data (Input_Pos));
2583 Input_Pos := Input_Pos + 1;
2586 exit State_Machine when Input_Pos > Last_In_Data
2587 or else Is_Digit (Data (Input_Pos));
2588 Input_Pos := Input_Pos + 1;
2591 exit State_Machine when Input_Pos > Last_In_Data
2592 or else not Is_Alnum (Data (Input_Pos));
2593 Input_Pos := Input_Pos + 1;
2596 exit State_Machine when Input_Pos > Last_In_Data
2597 or else Is_Alnum (Data (Input_Pos));
2598 Input_Pos := Input_Pos + 1;
2601 exit State_Machine when Input_Pos > Last_In_Data
2602 or else Data (Input_Pos) = ASCII.LF;
2603 Input_Pos := Input_Pos + 1;
2606 exit State_Machine when Input_Pos > Last_In_Data;
2607 Input_Pos := Input_Pos + 1;
2611 Opnd : Pointer := String_Operand (Scan);
2612 Current : Positive := Input_Pos;
2614 Last : constant Pointer :=
2615 Opnd + String_Length (Program, Scan);
2618 while Opnd <= Last loop
2619 exit State_Machine when Current > Last_In_Data
2620 or else Program (Opnd) /= Data (Current);
2621 Current := Current + 1;
2625 Input_Pos := Current;
2630 Opnd : Pointer := String_Operand (Scan);
2631 Current : Positive := Input_Pos;
2633 Last : constant Pointer :=
2634 Opnd + String_Length (Program, Scan);
2637 while Opnd <= Last loop
2638 exit State_Machine when Current > Last_In_Data
2639 or else Program (Opnd) /= To_Lower (Data (Current));
2640 Current := Current + 1;
2644 Input_Pos := Current;
2649 Bitmap : Character_Class;
2651 Bitmap_Operand (Program, Scan, Bitmap);
2652 exit State_Machine when Input_Pos > Last_In_Data
2653 or else not Get_From_Class (Bitmap, Data (Input_Pos));
2654 Input_Pos := Input_Pos + 1;
2659 No : constant Natural :=
2660 Character'Pos (Program (Operand (Scan)));
2662 Matches_Tmp (No) := Input_Pos;
2667 No : constant Natural :=
2668 Character'Pos (Program (Operand (Scan)));
2671 Matches_Full (No) := (Matches_Tmp (No), Input_Pos - 1);
2673 if Last_Paren < No then
2680 No : constant Natural :=
2681 Character'Pos (Program (Operand (Scan)));
2686 -- If we haven't seen that parenthesis yet
2688 if Last_Paren < No then
2692 Data_Pos := Matches_Full (No).First;
2694 while Data_Pos <= Matches_Full (No).Last loop
2695 if Input_Pos > Last_In_Data
2696 or else Data (Input_Pos) /= Data (Data_Pos)
2701 Input_Pos := Input_Pos + 1;
2702 Data_Pos := Data_Pos + 1;
2709 when STAR | PLUS | CURLY =>
2711 Greed : constant Boolean := Greedy;
2714 return Match_Simple_Operator (Op, Scan, Next, Greed);
2719 -- Looking at something like:
2721 -- 1: CURLYX {n,m} (->4)
2722 -- 2: code for complex thing (->3)
2727 Min : constant Natural :=
2728 Read_Natural (Program, Scan + 3);
2729 Max : constant Natural :=
2730 Read_Natural (Program, Scan + 5);
2731 Cc : aliased Current_Curly_Record;
2733 Has_Match : Boolean;
2736 Cc := (Paren_Floor => Last_Paren,
2744 Old_Cc => Current_Curly);
2745 Current_Curly := Cc'Unchecked_Access;
2747 Has_Match := Match (Next - 3);
2749 -- Start on the WHILEM
2751 Current_Curly := Cc.Old_Cc;
2756 return Match_Whilem (IP);
2760 end loop State_Machine;
2762 -- If we get here, there is no match.
2763 -- For successful matches when EOP is the terminating point.
2768 ---------------------------
2769 -- Match_Simple_Operator --
2770 ---------------------------
2772 function Match_Simple_Operator
2776 Greedy : Boolean) return Boolean
2778 Next_Char : Character := ASCII.NUL;
2779 Next_Char_Known : Boolean := False;
2780 No : Integer; -- Can be negative
2782 Max : Natural := Natural'Last;
2783 Operand_Code : Pointer;
2786 Save : constant Natural := Input_Pos;
2789 -- Lookahead to avoid useless match attempts
2790 -- when we know what character comes next.
2792 if Program (Next) = EXACT then
2793 Next_Char := Program (String_Operand (Next));
2794 Next_Char_Known := True;
2797 -- Find the minimal and maximal values for the operator
2802 Operand_Code := Operand (Scan);
2806 Operand_Code := Operand (Scan);
2809 Min := Read_Natural (Program, Scan + 3);
2810 Max := Read_Natural (Program, Scan + 5);
2811 Operand_Code := Scan + 7;
2814 -- Non greedy operators
2818 -- Test the minimal repetitions
2821 and then Repeat (Operand_Code, Min) < Min
2828 -- Find the place where 'next' could work
2830 if Next_Char_Known then
2831 -- Last position to check
2833 if Max = Natural'Last then
2834 Last_Pos := Last_In_Data;
2836 Last_Pos := Input_Pos + Max;
2838 if Last_Pos > Last_In_Data then
2839 Last_Pos := Last_In_Data;
2843 -- Look for the first possible opportunity
2846 -- Find the next possible position
2848 while Input_Pos <= Last_Pos
2849 and then Data (Input_Pos) /= Next_Char
2851 Input_Pos := Input_Pos + 1;
2854 if Input_Pos > Last_Pos then
2858 -- Check that we still match if we stop
2859 -- at the position we just found.
2862 Num : constant Natural := Input_Pos - Old;
2867 if Repeat (Operand_Code, Num) < Num then
2872 -- Input_Pos now points to the new position
2874 if Match (Get_Next (Program, Scan)) then
2879 Input_Pos := Input_Pos + 1;
2882 -- We know what the next character is
2885 while Max >= Min loop
2887 -- If the next character matches
2889 if Match (Next) then
2893 Input_Pos := Save + Min;
2895 -- Could not or did not match -- move forward
2897 if Repeat (Operand_Code, 1) /= 0 then
2910 No := Repeat (Operand_Code, Max);
2912 -- ??? Perl has some special code here in case the
2913 -- next instruction is of type EOL, since $ and \Z
2914 -- can match before *and* after newline at the end.
2916 -- ??? Perl has some special code here in case (paren)
2919 -- Else, if we don't have any parenthesis
2921 while No >= Min loop
2922 if not Next_Char_Known
2923 or else (Input_Pos <= Last_In_Data
2924 and then Data (Input_Pos) = Next_Char)
2926 if Match (Next) then
2931 -- Could not or did not work, we back up
2934 Input_Pos := Save + No;
2939 end Match_Simple_Operator;
2945 -- This is really hard to understand, because after we match what we
2946 -- are trying to match, we must make sure the rest of the REx is going
2947 -- to match for sure, and to do that we have to go back UP the parse
2948 -- tree by recursing ever deeper. And if it fails, we have to reset
2949 -- our parent's current state that we can try again after backing off.
2951 function Match_Whilem (IP : Pointer) return Boolean is
2952 pragma Unreferenced (IP);
2954 Cc : constant Current_Curly_Access := Current_Curly;
2955 N : constant Natural := Cc.Cur + 1;
2958 Lastloc : constant Natural := Cc.Lastloc;
2959 -- Detection of 0-len
2962 -- If degenerate scan matches "", assume scan done
2964 if Input_Pos = Cc.Lastloc
2965 and then N >= Cc.Min
2967 -- Temporarily restore the old context, and check that we
2968 -- match was comes after CURLYX.
2970 Current_Curly := Cc.Old_Cc;
2972 if Current_Curly /= null then
2973 Ln := Current_Curly.Cur;
2976 if Match (Cc.Next) then
2980 if Current_Curly /= null then
2981 Current_Curly.Cur := Ln;
2984 Current_Curly := Cc;
2988 -- First, just match a string of min scans
2992 Cc.Lastloc := Input_Pos;
2994 if Match (Cc.Scan) then
2999 Cc.Lastloc := Lastloc;
3003 -- Prefer next over scan for minimal matching
3005 if not Cc.Greedy then
3006 Current_Curly := Cc.Old_Cc;
3008 if Current_Curly /= null then
3009 Ln := Current_Curly.Cur;
3012 if Recurse_Match (Cc.Next, Cc.Paren_Floor) then
3016 if Current_Curly /= null then
3017 Current_Curly.Cur := Ln;
3020 Current_Curly := Cc;
3022 -- Maximum greed exceeded ?
3028 -- Try scanning more and see if it helps
3030 Cc.Lastloc := Input_Pos;
3032 if Recurse_Match (Cc.Scan, Cc.Paren_Floor) then
3037 Cc.Lastloc := Lastloc;
3041 -- Prefer scan over next for maximal matching
3043 if N < Cc.Max then -- more greed allowed ?
3045 Cc.Lastloc := Input_Pos;
3047 if Recurse_Match (Cc.Scan, Cc.Paren_Floor) then
3052 -- Failed deeper matches of scan, so see if this one works
3054 Current_Curly := Cc.Old_Cc;
3056 if Current_Curly /= null then
3057 Ln := Current_Curly.Cur;
3060 if Match (Cc.Next) then
3064 if Current_Curly /= null then
3065 Current_Curly.Cur := Ln;
3068 Current_Curly := Cc;
3070 Cc.Lastloc := Lastloc;
3080 Max : Natural := Natural'Last) return Natural
3082 Scan : Natural := Input_Pos;
3084 Op : constant Opcode := Opcode'Val (Character'Pos (Program (IP)));
3087 Is_First : Boolean := True;
3088 Bitmap : Character_Class;
3091 if Max = Natural'Last or else Scan + Max - 1 > Last_In_Data then
3092 Last := Last_In_Data;
3094 Last := Scan + Max - 1;
3100 and then Data (Scan) /= ASCII.LF
3110 -- The string has only one character if Repeat was called
3112 C := Program (String_Operand (IP));
3114 and then C = Data (Scan)
3121 -- The string has only one character if Repeat was called
3123 C := Program (String_Operand (IP));
3125 and then To_Lower (C) = Data (Scan)
3132 Bitmap_Operand (Program, IP, Bitmap);
3137 and then Get_From_Class (Bitmap, Data (Scan))
3144 and then Is_Alnum (Data (Scan))
3151 and then not Is_Alnum (Data (Scan))
3158 and then Is_White_Space (Data (Scan))
3165 and then not Is_White_Space (Data (Scan))
3172 and then Is_Digit (Data (Scan))
3179 and then not Is_Digit (Data (Scan))
3185 raise Program_Error;
3188 Count := Scan - Input_Pos;
3197 function Try (Pos : Positive) return Boolean is
3201 Matches_Full := (others => No_Match);
3203 if Match (Program_First + 1) then
3204 Matches_Full (0) := (Pos, Input_Pos - 1);
3211 -- Start of processing for Match
3214 -- Do we have the regexp Never_Match?
3216 if Self.Size = 0 then
3217 Matches := (others => No_Match);
3221 -- Check validity of program
3224 (Program (Program_First) = MAGIC,
3225 "Corrupted Pattern_Matcher
");
3227 -- If there is a "must appear
" string, look for it
3229 if Self.Must_Have_Length > 0 then
3231 First : constant Character := Program (Self.Must_Have);
3232 Must_First : constant Pointer := Self.Must_Have;
3233 Must_Last : constant Pointer :=
3234 Must_First + Pointer (Self.Must_Have_Length - 1);
3235 Next_Try : Natural := Index (First_In_Data, First);
3239 and then Data (Next_Try .. Next_Try + Self.Must_Have_Length - 1)
3240 = String (Program (Must_First .. Must_Last))
3242 Next_Try := Index (Next_Try + 1, First);
3245 if Next_Try = 0 then
3246 Matches := (others => No_Match);
3247 return; -- Not present
3252 -- Mark beginning of line for ^
3254 BOL_Pos := Data'First;
3256 -- Simplest case first: an anchored match need be tried only once
3258 if Self.Anchored and then (Self.Flags and Multiple_Lines) = 0 then
3259 Matched := Try (First_In_Data);
3261 elsif Self.Anchored then
3263 Next_Try : Natural := First_In_Data;
3265 -- Test the first position in the buffer
3266 Matched := Try (Next_Try);
3268 -- Else only test after newlines
3271 while Next_Try <= Last_In_Data loop
3272 while Next_Try <= Last_In_Data
3273 and then Data (Next_Try) /= ASCII.LF
3275 Next_Try := Next_Try + 1;
3278 Next_Try := Next_Try + 1;
3280 if Next_Try <= Last_In_Data then
3281 Matched := Try (Next_Try);
3288 elsif Self.First /= ASCII.NUL then
3289 -- We know what char it must start with
3292 Next_Try : Natural := Index (First_In_Data, Self.First);
3295 while Next_Try /= 0 loop
3296 Matched := Try (Next_Try);
3298 Next_Try := Index (Next_Try + 1, Self.First);
3303 -- Messy cases: try all locations (including for the empty string)
3305 Matched := Try (First_In_Data);
3308 for S in First_In_Data + 1 .. Last_In_Data loop
3315 -- Matched has its value
3317 for J in Last_Paren + 1 .. Matches'Last loop
3318 Matches_Full (J) := No_Match;
3321 Matches := Matches_Full (Matches'Range);
3329 (Self : Pattern_Matcher;
3331 Data_First : Integer := -1;
3332 Data_Last : Positive := Positive'Last) return Natural
3334 Matches : Match_Array (0 .. 0);
3337 Match (Self, Data, Matches, Data_First, Data_Last);
3338 if Matches (0) = No_Match then
3339 return Data'First - 1;
3341 return Matches (0).First;
3346 (Self : Pattern_Matcher;
3348 Data_First : Integer := -1;
3349 Data_Last : Positive := Positive'Last) return Boolean
3351 Matches : Match_Array (0 .. 0);
3354 Match (Self, Data, Matches, Data_First, Data_Last);
3355 return Matches (0).First >= Data'First;
3359 (Expression : String;
3361 Matches : out Match_Array;
3362 Size : Program_Size := Auto_Size;
3363 Data_First : Integer := -1;
3364 Data_Last : Positive := Positive'Last)
3366 PM : Pattern_Matcher (Size);
3367 Finalize_Size : Program_Size;
3368 pragma Unreferenced (Finalize_Size);
3371 Match (Compile (Expression), Data, Matches, Data_First, Data_Last);
3373 Compile (PM, Expression, Finalize_Size);
3374 Match (PM, Data, Matches, Data_First, Data_Last);
3383 (Expression : String;
3385 Size : Program_Size := Auto_Size;
3386 Data_First : Integer := -1;
3387 Data_Last : Positive := Positive'Last) return Natural
3389 PM : Pattern_Matcher (Size);
3390 Final_Size : Program_Size;
3391 pragma Unreferenced (Final_Size);
3394 return Match (Compile (Expression), Data, Data_First, Data_Last);
3396 Compile (PM, Expression, Final_Size);
3397 return Match (PM, Data, Data_First, Data_Last);
3406 (Expression : String;
3408 Size : Program_Size := Auto_Size;
3409 Data_First : Integer := -1;
3410 Data_Last : Positive := Positive'Last) return Boolean
3412 Matches : Match_Array (0 .. 0);
3413 PM : Pattern_Matcher (Size);
3414 Final_Size : Program_Size;
3415 pragma Unreferenced (Final_Size);
3418 Match (Compile (Expression), Data, Matches, Data_First, Data_Last);
3420 Compile (PM, Expression, Final_Size);
3421 Match (PM, Data, Matches, Data_First, Data_Last);
3424 return Matches (0).First >= Data'First;
3431 function Operand (P : Pointer) return Pointer is
3440 procedure Optimize (Self : in out Pattern_Matcher) is
3442 Program : Program_Data renames Self.Program;
3445 -- Start with safe defaults (no optimization):
3446 -- * No known first character of match
3447 -- * Does not necessarily start at beginning of line
3448 -- * No string known that has to appear in data
3450 Self.First := ASCII.NUL;
3451 Self.Anchored := False;
3452 Self.Must_Have := Program'Last + 1;
3453 Self.Must_Have_Length := 0;
3455 Scan := Program_First + 1; -- First instruction (can be anything)
3457 if Program (Scan) = EXACT then
3458 Self.First := Program (String_Operand (Scan));
3460 elsif Program (Scan) = BOL
3461 or else Program (Scan) = SBOL
3462 or else Program (Scan) = MBOL
3464 Self.Anchored := True;
3472 function Paren_Count (Regexp : Pattern_Matcher) return Match_Count is
3474 return Regexp.Paren_Count;
3481 function Quote (Str : String) return String is
3482 S : String (1 .. Str'Length * 2);
3483 Last : Natural := 0;
3486 for J in Str'Range loop
3488 when '^' | '$' | '|' | '*' | '+' | '?' | '{' |
3489 '}' | '[' | ']' | '(' | ')' | '\' | '.' =>
3491 S (Last + 1) := '\';
3492 S (Last + 2) := Str (J);
3496 S (Last + 1) := Str (J);
3501 return S (1 .. Last);
3508 function Read_Natural
3509 (Program : Program_Data;
3510 IP : Pointer) return Natural
3513 return Character'Pos (Program (IP)) +
3514 256 * Character'Pos (Program (IP + 1));
3521 procedure Reset_Class (Bitmap : out Character_Class) is
3523 Bitmap := (others => 0);
3530 procedure Set_In_Class
3531 (Bitmap : in out Character_Class;
3534 Value : constant Class_Byte := Character'Pos (C);
3536 Bitmap (Value / 8) := Bitmap (Value / 8)
3537 or Bit_Conversion (Value mod 8);
3544 function String_Length
3545 (Program : Program_Data;
3546 P : Pointer) return Program_Size
3549 pragma Assert (Program (P) = EXACT or else Program (P) = EXACTF);
3550 return Character'Pos (Program (P + 3));
3553 --------------------
3554 -- String_Operand --
3555 --------------------
3557 function String_Operand (P : Pointer) return Pointer is