OSDN Git Service

* config/vax/vax.h (target_flags, MASK_UNIX_ASM, MASK_VAXC_ALIGNMENT)
[pf3gnuchains/gcc-fork.git] / gcc / ada / g-spipat.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT LIBRARY COMPONENTS                          --
4 --                                                                          --
5 --                G N A T . S P I T B O L . P A T T E R N S                 --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --           Copyright (C) 1998-2005, Ada Core Technologies, Inc.           --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
19 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
20 -- MA 02111-1307, USA.                                                      --
21 --                                                                          --
22 -- As a special exception,  if other files  instantiate  generics from this --
23 -- unit, or you link  this unit with other files  to produce an executable, --
24 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
25 -- covered  by the  GNU  General  Public  License.  This exception does not --
26 -- however invalidate  any other reasons why  the executable file  might be --
27 -- covered by the  GNU Public License.                                      --
28 --                                                                          --
29 -- GNAT was originally developed  by the GNAT team at  New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
31 --                                                                          --
32 ------------------------------------------------------------------------------
33
34 --  Note: the data structures and general approach used in this implementation
35 --  are derived from the original MINIMAL sources for SPITBOL. The code is not
36 --  a direct translation, but the approach is followed closely. In particular,
37 --  we use the one stack approach developed in the SPITBOL implementation.
38
39 with Ada.Exceptions;            use Ada.Exceptions;
40 with Ada.Strings.Maps;          use Ada.Strings.Maps;
41 with Ada.Strings.Unbounded.Aux; use Ada.Strings.Unbounded.Aux;
42
43 with GNAT.Debug_Utilities;      use GNAT.Debug_Utilities;
44
45 with System;                    use System;
46
47 with Unchecked_Conversion;
48 with Unchecked_Deallocation;
49
50 package body GNAT.Spitbol.Patterns is
51
52    ------------------------
53    -- Internal Debugging --
54    ------------------------
55
56    Internal_Debug : constant Boolean := False;
57    --  Set this flag to True to activate some built-in debugging traceback
58    --  These are all lines output with PutD and Put_LineD.
59
60    procedure New_LineD;
61    pragma Inline (New_LineD);
62    --  Output new blank line with New_Line if Internal_Debug is True
63
64    procedure PutD (Str : String);
65    pragma Inline (PutD);
66    --  Output string with Put if Internal_Debug is True
67
68    procedure Put_LineD (Str : String);
69    pragma Inline (Put_LineD);
70    --  Output string with Put_Line if Internal_Debug is True
71
72    -----------------------------
73    -- Local Type Declarations --
74    -----------------------------
75
76    subtype String_Ptr is Ada.Strings.Unbounded.String_Access;
77    subtype File_Ptr   is Ada.Text_IO.File_Access;
78
79    function To_Address is new Unchecked_Conversion (PE_Ptr, Address);
80    --  Used only for debugging output purposes
81
82    subtype AFC is Ada.Finalization.Controlled;
83
84    N : constant PE_Ptr := null;
85    --  Shorthand used to initialize Copy fields to null
86
87    type Natural_Ptr   is access all Natural;
88    type Pattern_Ptr   is access all Pattern;
89
90    --------------------------------------------------
91    -- Description of Algorithm and Data Structures --
92    --------------------------------------------------
93
94    --  A pattern structure is represented as a linked graph of nodes
95    --  with the following structure:
96
97    --      +------------------------------------+
98    --      I                Pcode               I
99    --      +------------------------------------+
100    --      I                Index               I
101    --      +------------------------------------+
102    --      I                Pthen               I
103    --      +------------------------------------+
104    --      I             parameter(s)           I
105    --      +------------------------------------+
106
107    --     Pcode is a code value indicating the type of the patterm node. This
108    --     code is used both as the discriminant value for the record, and as
109    --     the case index in the main match routine that branches to the proper
110    --     match code for the given element.
111
112    --     Index is a serial index number. The use of these serial index
113    --     numbers is described in a separate section.
114
115    --     Pthen is a pointer to the successor node, i.e the node to be matched
116    --     if the attempt to match the node succeeds. If this is the last node
117    --     of the pattern to be matched, then Pthen points to a dummy node
118    --     of kind PC_EOP (end of pattern), which initiales pattern exit.
119
120    --     The parameter or parameters are present for certain node types,
121    --     and the type varies with the pattern code.
122
123    type Pattern_Code is (
124       PC_Arb_Y,
125       PC_Assign,
126       PC_Bal,
127       PC_BreakX_X,
128       PC_Cancel,
129       PC_EOP,
130       PC_Fail,
131       PC_Fence,
132       PC_Fence_X,
133       PC_Fence_Y,
134       PC_R_Enter,
135       PC_R_Remove,
136       PC_R_Restore,
137       PC_Rest,
138       PC_Succeed,
139       PC_Unanchored,
140
141       PC_Alt,
142       PC_Arb_X,
143       PC_Arbno_S,
144       PC_Arbno_X,
145
146       PC_Rpat,
147
148       PC_Pred_Func,
149
150       PC_Assign_Imm,
151       PC_Assign_OnM,
152       PC_Any_VP,
153       PC_Break_VP,
154       PC_BreakX_VP,
155       PC_NotAny_VP,
156       PC_NSpan_VP,
157       PC_Span_VP,
158       PC_String_VP,
159
160       PC_Write_Imm,
161       PC_Write_OnM,
162
163       PC_Null,
164       PC_String,
165
166       PC_String_2,
167       PC_String_3,
168       PC_String_4,
169       PC_String_5,
170       PC_String_6,
171
172       PC_Setcur,
173
174       PC_Any_CH,
175       PC_Break_CH,
176       PC_BreakX_CH,
177       PC_Char,
178       PC_NotAny_CH,
179       PC_NSpan_CH,
180       PC_Span_CH,
181
182       PC_Any_CS,
183       PC_Break_CS,
184       PC_BreakX_CS,
185       PC_NotAny_CS,
186       PC_NSpan_CS,
187       PC_Span_CS,
188
189       PC_Arbno_Y,
190       PC_Len_Nat,
191       PC_Pos_Nat,
192       PC_RPos_Nat,
193       PC_RTab_Nat,
194       PC_Tab_Nat,
195
196       PC_Pos_NF,
197       PC_Len_NF,
198       PC_RPos_NF,
199       PC_RTab_NF,
200       PC_Tab_NF,
201
202       PC_Pos_NP,
203       PC_Len_NP,
204       PC_RPos_NP,
205       PC_RTab_NP,
206       PC_Tab_NP,
207
208       PC_Any_VF,
209       PC_Break_VF,
210       PC_BreakX_VF,
211       PC_NotAny_VF,
212       PC_NSpan_VF,
213       PC_Span_VF,
214       PC_String_VF);
215
216    type IndexT is range 0 .. +(2 **15 - 1);
217
218    type PE (Pcode : Pattern_Code) is record
219
220       Index : IndexT;
221       --  Serial index number of pattern element within pattern.
222
223       Pthen : PE_Ptr;
224       --  Successor element, to be matched after this one
225
226       case Pcode is
227
228          when PC_Arb_Y      |
229               PC_Assign     |
230               PC_Bal        |
231               PC_BreakX_X   |
232               PC_Cancel     |
233               PC_EOP        |
234               PC_Fail       |
235               PC_Fence      |
236               PC_Fence_X    |
237               PC_Fence_Y    |
238               PC_Null       |
239               PC_R_Enter    |
240               PC_R_Remove   |
241               PC_R_Restore  |
242               PC_Rest       |
243               PC_Succeed    |
244               PC_Unanchored => null;
245
246          when PC_Alt        |
247               PC_Arb_X      |
248               PC_Arbno_S    |
249               PC_Arbno_X    => Alt  : PE_Ptr;
250
251          when PC_Rpat       => PP   : Pattern_Ptr;
252
253          when PC_Pred_Func  => BF   : Boolean_Func;
254
255          when PC_Assign_Imm |
256               PC_Assign_OnM |
257               PC_Any_VP     |
258               PC_Break_VP   |
259               PC_BreakX_VP  |
260               PC_NotAny_VP  |
261               PC_NSpan_VP   |
262               PC_Span_VP    |
263               PC_String_VP  => VP   : VString_Ptr;
264
265          when PC_Write_Imm  |
266               PC_Write_OnM  => FP   : File_Ptr;
267
268          when PC_String     => Str  : String_Ptr;
269
270          when PC_String_2   => Str2 : String (1 .. 2);
271
272          when PC_String_3   => Str3 : String (1 .. 3);
273
274          when PC_String_4   => Str4 : String (1 .. 4);
275
276          when PC_String_5   => Str5 : String (1 .. 5);
277
278          when PC_String_6   => Str6 : String (1 .. 6);
279
280          when PC_Setcur     => Var  : Natural_Ptr;
281
282          when PC_Any_CH     |
283               PC_Break_CH   |
284               PC_BreakX_CH  |
285               PC_Char       |
286               PC_NotAny_CH  |
287               PC_NSpan_CH   |
288               PC_Span_CH    => Char : Character;
289
290          when PC_Any_CS     |
291               PC_Break_CS   |
292               PC_BreakX_CS  |
293               PC_NotAny_CS  |
294               PC_NSpan_CS   |
295               PC_Span_CS    => CS   : Character_Set;
296
297          when PC_Arbno_Y    |
298               PC_Len_Nat    |
299               PC_Pos_Nat    |
300               PC_RPos_Nat   |
301               PC_RTab_Nat   |
302               PC_Tab_Nat    => Nat  : Natural;
303
304          when PC_Pos_NF     |
305               PC_Len_NF     |
306               PC_RPos_NF    |
307               PC_RTab_NF    |
308               PC_Tab_NF     => NF   : Natural_Func;
309
310          when PC_Pos_NP     |
311               PC_Len_NP     |
312               PC_RPos_NP    |
313               PC_RTab_NP    |
314               PC_Tab_NP     => NP   : Natural_Ptr;
315
316          when PC_Any_VF     |
317               PC_Break_VF   |
318               PC_BreakX_VF  |
319               PC_NotAny_VF  |
320               PC_NSpan_VF   |
321               PC_Span_VF    |
322               PC_String_VF  => VF   : VString_Func;
323
324       end case;
325    end record;
326
327    subtype PC_Has_Alt is Pattern_Code range PC_Alt .. PC_Arbno_X;
328    --  Range of pattern codes that has an Alt field. This is used in the
329    --  recursive traversals, since these links must be followed.
330
331    EOP_Element : aliased constant PE := (PC_EOP, 0, N);
332    --  This is the end of pattern element, and is thus the representation of
333    --  a null pattern. It has a zero index element since it is never placed
334    --  inside a pattern. Furthermore it does not need a successor, since it
335    --  marks the end of the pattern, so that no more successors are needed.
336
337    EOP : constant PE_Ptr := EOP_Element'Unrestricted_Access;
338    --  This is the end of pattern pointer, that is used in the Pthen pointer
339    --  of other nodes to signal end of pattern.
340
341    --  The following array is used to determine if a pattern used as an
342    --  argument for Arbno is eligible for treatment using the simple Arbno
343    --  structure (i.e. it is a pattern that is guaranteed to match at least
344    --  one character on success, and not to make any entries on the stack.
345
346    OK_For_Simple_Arbno : constant array (Pattern_Code) of Boolean :=
347      (PC_Any_CS    |
348       PC_Any_CH    |
349       PC_Any_VF    |
350       PC_Any_VP    |
351       PC_Char      |
352       PC_Len_Nat   |
353       PC_NotAny_CS |
354       PC_NotAny_CH |
355       PC_NotAny_VF |
356       PC_NotAny_VP |
357       PC_Span_CS   |
358       PC_Span_CH   |
359       PC_Span_VF   |
360       PC_Span_VP   |
361       PC_String    |
362       PC_String_2  |
363       PC_String_3  |
364       PC_String_4  |
365       PC_String_5  |
366       PC_String_6   => True,
367       others        => False);
368
369    -------------------------------
370    -- The Pattern History Stack --
371    -------------------------------
372
373    --  The pattern history stack is used for controlling backtracking when
374    --  a match fails. The idea is to stack entries that give a cursor value
375    --  to be restored, and a node to be reestablished as the current node to
376    --  attempt an appropriate rematch operation. The processing for a pattern
377    --  element that has rematch alternatives pushes an appropriate entry or
378    --  entry on to the stack, and the proceeds. If a match fails at any point,
379    --  the top element of the stack is popped off, resetting the cursor and
380    --  the match continues by accessing the node stored with this entry.
381
382    type Stack_Entry is record
383
384       Cursor : Integer;
385       --  Saved cursor value that is restored when this entry is popped
386       --  from the stack if a match attempt fails. Occasionally, this
387       --  field is used to store a history stack pointer instead of a
388       --  cursor. Such cases are noted in the documentation and the value
389       --  stored is negative since stack pointer values are always negative.
390
391       Node : PE_Ptr;
392       --  This pattern element reference is reestablished as the current
393       --  Node to be matched (which will attempt an appropriate rematch).
394
395    end record;
396
397    subtype Stack_Range is Integer range -Stack_Size .. -1;
398
399    type Stack_Type is array (Stack_Range) of Stack_Entry;
400    --  The type used for a history stack. The actual instance of the stack
401    --  is declared as a local variable in the Match routine, to properly
402    --  handle recursive calls to Match. All stack pointer values are negative
403    --  to distinguish them from normal cursor values.
404
405    --  Note: the pattern matching stack is used only to handle backtracking.
406    --  If no backtracking occurs, its entries are never accessed, and never
407    --  popped off, and in particular it is normal for a successful match
408    --  to terminate with entries on the stack that are simply discarded.
409
410    --  Note: in subsequent diagrams of the stack, we always place element
411    --  zero (the deepest element) at the top of the page, then build the
412    --  stack down on the page with the most recent (top of stack) element
413    --  being the bottom-most entry on the page.
414
415    --  Stack checking is handled by labeling every pattern with the maximum
416    --  number of stack entries that are required, so a single check at the
417    --  start of matching the pattern suffices. There are two exceptions.
418
419    --  First, the count does not include entries for recursive pattern
420    --  references. Such recursions must therefore perform a specific
421    --  stack check with respect to the number of stack entries required
422    --  by the recursive pattern that is accessed and the amount of stack
423    --  that remains unused.
424
425    --  Second, the count includes only one iteration of an Arbno pattern,
426    --  so a specific check must be made on subsequent iterations that there
427    --  is still enough stack space left. The Arbno node has a field that
428    --  records the number of stack entries required by its argument for
429    --  this purpose.
430
431    ---------------------------------------------------
432    -- Use of Serial Index Field in Pattern Elements --
433    ---------------------------------------------------
434
435    --  The serial index numbers for the pattern elements are assigned as
436    --  a pattern is consructed from its constituent elements. Note that there
437    --  is never any sharing of pattern elements between patterns (copies are
438    --  always made), so the serial index numbers are unique to a particular
439    --  pattern as referenced from the P field of a value of type Pattern.
440
441    --  The index numbers meet three separate invariants, which are used for
442    --  various purposes as described in this section.
443
444    --  First, the numbers uniquely identify the pattern elements within a
445    --  pattern. If Num is the number of elements in a given pattern, then
446    --  the serial index numbers for the elements of this pattern will range
447    --  from 1 .. Num, so that each element has a separate value.
448
449    --  The purpose of this assignment is to provide a convenient auxiliary
450    --  data structure mechanism during operations which must traverse a
451    --  pattern (e.g. copy and finalization processing). Once constructed
452    --  patterns are strictly read only. This is necessary to allow sharing
453    --  of patterns between tasks. This means that we cannot go marking the
454    --  pattern (e.g. with a visited bit). Instead we cosntuct a separate
455    --  vector that contains the necessary information indexed by the Index
456    --  values in the pattern elements. For this purpose the only requirement
457    --  is that they be uniquely assigned.
458
459    --  Second, the pattern element referenced directly, i.e. the leading
460    --  pattern element, is always the maximum numbered element and therefore
461    --  indicates the total number of elements in the pattern. More precisely,
462    --  the element referenced by the P field of a pattern value, or the
463    --  element returned by any of the internal pattern construction routines
464    --  in the body (that return a value of type PE_Ptr) always is this
465    --  maximum element,
466
467    --  The purpose of this requirement is to allow an immediate determination
468    --  of the number of pattern elements within a pattern. This is used to
469    --  properly size the vectors used to contain auxiliary information for
470    --  traversal as described above.
471
472    --  Third, as compound pattern structures are constructed, the way in which
473    --  constituent parts of the pattern are constructed is stylized. This is
474    --  an automatic consequence of the way that these compounjd structures
475    --  are constructed, and basically what we are doing is simply documenting
476    --  and specifying the natural result of the pattern construction. The
477    --  section describing compound pattern structures gives details of the
478    --  numbering of each compound pattern structure.
479
480    --  The purpose of specifying the stylized numbering structures for the
481    --  compound patterns is to help simplify the processing in the Image
482    --  function, since it eases the task of retrieving the original recursive
483    --  structure of the pattern from the flat graph structure of elements.
484    --  This use in the Image function is the only point at which the code
485    --  makes use of the stylized structures.
486
487    type Ref_Array is array (IndexT range <>) of PE_Ptr;
488    --  This type is used to build an array whose N'th entry references the
489    --  element in a pattern whose Index value is N. See Build_Ref_Array.
490
491    procedure Build_Ref_Array (E : PE_Ptr; RA : out Ref_Array);
492    --  Given a pattern element which is the leading element of a pattern
493    --  structure, and a Ref_Array with bounds 1 .. E.Index, fills in the
494    --  Ref_Array so that its N'th entry references the element of the
495    --  referenced pattern whose Index value is N.
496
497    -------------------------------
498    -- Recursive Pattern Matches --
499    -------------------------------
500
501    --  The pattern primitive (+P) where P is a Pattern_Ptr or Pattern_Func
502    --  causes a recursive pattern match. This cannot be handled by an actual
503    --  recursive call to the outer level Match routine, since this would not
504    --  allow for possible backtracking into the region matched by the inner
505    --  pattern. Indeed this is the classical clash between recursion and
506    --  backtracking, and a simple recursive stack structure does not suffice.
507
508    --  This section describes how this recursion and the possible associated
509    --  backtracking is handled. We still use a single stack, but we establish
510    --  the concept of nested regions on this stack, each of which has a stack
511    --  base value pointing to the deepest stack entry of the region. The base
512    --  value for the outer level is zero.
513
514    --  When a recursive match is established, two special stack entries are
515    --  made. The first entry is used to save the original node that starts
516    --  the recursive match. This is saved so that the successor field of
517    --  this node is accessible at the end of the match, but it is never
518    --  popped and executed.
519
520    --  The second entry corresponds to a standard new region action. A
521    --  PC_R_Remove node is stacked, whose cursor field is used to store
522    --  the outer stack base, and the stack base is reset to point to
523    --  this PC_R_Remove node. Then the recursive pattern is matched and
524    --  it can make history stack entries in the normal matter, so now
525    --  the stack looks like:
526
527    --     (stack entries made by outer level)
528
529    --     (Special entry, node is (+P) successor
530    --      cursor entry is not used)
531
532    --     (PC_R_Remove entry, "cursor" value is (negative)     <-- Stack base
533    --      saved base value for the enclosing region)
534
535    --     (stack entries made by inner level)
536
537    --  If a subsequent failure occurs and pops the PC_R_Remove node, it
538    --  removes itself and the special entry immediately underneath it,
539    --  restores the stack base value for the enclosing region, and then
540    --  again signals failure to look for alternatives that were stacked
541    --  before the recursion was initiated.
542
543    --  Now we need to consider what happens if the inner pattern succeeds, as
544    --  signalled by accessing the special PC_EOP pattern primitive. First we
545    --  recognize the nested case by looking at the Base value. If this Base
546    --  value is Stack'First, then the entire match has succeeded, but if the
547    --  base value is greater than Stack'First, then we have successfully
548    --  matched an inner pattern, and processing continues at the outer level.
549
550    --  There are two cases. The simple case is when the inner pattern has made
551    --  no stack entries, as recognized by the fact that the current stack
552    --  pointer is equal to the current base value. In this case it is fine to
553    --  remove all trace of the recursion by restoring the outer base value and
554    --  using the special entry to find the appropriate successor node.
555
556    --  The more complex case arises when the inner match does make stack
557    --  entries. In this case, the PC_EOP processing stacks a special entry
558    --  whose cursor value saves the saved inner base value (the one that
559    --  references the corresponding PC_R_Remove value), and whose node
560    --  pointer references a PC_R_Restore node, so the stack looks like:
561
562    --     (stack entries made by outer level)
563
564    --     (Special entry, node is (+P) successor,
565    --      cursor entry is not used)
566
567    --     (PC_R_Remove entry, "cursor" value is (negative)
568    --      saved base value for the enclosing region)
569
570    --     (stack entries made by inner level)
571
572    --     (PC_Region_Replace entry, "cursor" value is (negative)
573    --      stack pointer value referencing the PC_R_Remove entry).
574
575    --  If the entire match succeeds, then these stack entries are, as usual,
576    --  ignored and abandoned. If on the other hand a subsequent failure
577    --  causes the PC_Region_Replace entry to be popped, it restores the
578    --  inner base value from its saved "cursor" value and then fails again.
579    --  Note that it is OK that the cursor is temporarily clobbered by this
580    --  pop, since the second failure will reestablish a proper cursor value.
581
582    ---------------------------------
583    -- Compound Pattern Structures --
584    ---------------------------------
585
586    --  This section discusses the compound structures used to represent
587    --  constructed patterns. It shows the graph structures of pattern
588    --  elements that are constructed, and in the case of patterns that
589    --  provide backtracking possibilities, describes how the history
590    --  stack is used to control the backtracking. Finally, it notes the
591    --  way in which the Index numbers are assigned to the structure.
592
593    --  In all diagrams, solid lines (built witth minus signs or vertical
594    --  bars, represent successor pointers (Pthen fields) with > or V used
595    --  to indicate the direction of the pointer. The initial node of the
596    --  structure is in the upper left of the diagram. A dotted line is an
597    --  alternative pointer from the element above it to the element below
598    --  it. See individual sections for details on how alternatives are used.
599
600       -------------------
601       -- Concatenation --
602       -------------------
603
604       --  In the pattern structures listed in this section, a line that looks
605       --  lile ----> with nothing to the right indicates an end of pattern
606       --  (EOP) pointer that represents the end of the match.
607
608       --  When a pattern concatenation (L & R) occurs, the resulting structure
609       --  is obtained by finding all such EOP pointers in L, and replacing
610       --  them to point to R. This is the most important flattening that
611       --  occurs in constructing a pattern, and it means that the pattern
612       --  matching circuitry does not have to keep track of the structure
613       --  of a pattern with respect to concatenation, since the appropriate
614       --  succesor is always at hand.
615
616       --  Concatenation itself generates no additional possibilities for
617       --  backtracking, but the constituent patterns of the concatenated
618       --  structure will make stack entries as usual. The maximum amount
619       --  of stack required by the structure is thus simply the sum of the
620       --  maximums required by L and R.
621
622       --  The index numbering of a concatenation structure works by leaving
623       --  the numbering of the right hand pattern, R, unchanged and adjusting
624       --  the numbers in the left hand pattern, L up by the count of elements
625       --  in R. This ensures that the maximum numbered element is the leading
626       --  element as required (given that it was the leading element in L).
627
628       -----------------
629       -- Alternation --
630       -----------------
631
632       --  A pattern (L or R) constructs the structure:
633
634       --    +---+     +---+
635       --    | A |---->| L |---->
636       --    +---+     +---+
637       --      .
638       --      .
639       --    +---+
640       --    | R |---->
641       --    +---+
642
643       --  The A element here is a PC_Alt node, and the dotted line represents
644       --  the contents of the Alt field. When the PC_Alt element is matched,
645       --  it stacks a pointer to the leading element of R on the history stack
646       --  so that on subsequent failure, a match of R is attempted.
647
648       --  The A node is the higest numbered element in the pattern. The
649       --  original index numbers of R are unchanged, but the index numbers
650       --  of the L pattern are adjusted up by the count of elements in R.
651
652       --  Note that the difference between the index of the L leading element
653       --  the index of the R leading element (after building the alt structure)
654       --  indicates the number of nodes in L, and this is true even after the
655       --  structure is incorporated into some larger structure. For example,
656       --  if the A node has index 16, and L has index 15 and R has index
657       --  5, then we know that L has 10 (15-5) elements in it.
658
659       --  Suppose that we now concatenate this structure to another pattern
660       --  with 9 elements in it. We will now have the A node with an index
661       --  of 25, L with an index of 24 and R with an index of 14. We still
662       --  know that L has 10 (24-14) elements in it, numbered 15-24, and
663       --  consequently the successor of the alternation structure has an
664       --  index with a value less than 15. This is used in Image to figure
665       --  out the original recursive structure of a pattern.
666
667       --  To clarify the interaction of the alternation and concatenation
668       --  structures, here is a more complex example of the structure built
669       --  for the pattern:
670
671       --      (V or W or X) (Y or Z)
672
673       --  where A,B,C,D,E are all single element patterns:
674
675       --    +---+     +---+       +---+     +---+
676       --    I A I---->I V I---+-->I A I---->I Y I---->
677       --    +---+     +---+   I   +---+     +---+
678       --      .               I     .
679       --      .               I     .
680       --    +---+     +---+   I   +---+
681       --    I A I---->I W I-->I   I Z I---->
682       --    +---+     +---+   I   +---+
683       --      .               I
684       --      .               I
685       --    +---+             I
686       --    I X I------------>+
687       --    +---+
688
689       --  The numbering of the nodes would be as follows:
690
691       --    +---+     +---+       +---+     +---+
692       --    I 8 I---->I 7 I---+-->I 3 I---->I 2 I---->
693       --    +---+     +---+   I   +---+     +---+
694       --      .               I     .
695       --      .               I     .
696       --    +---+     +---+   I   +---+
697       --    I 6 I---->I 5 I-->I   I 1 I---->
698       --    +---+     +---+   I   +---+
699       --      .               I
700       --      .               I
701       --    +---+             I
702       --    I 4 I------------>+
703       --    +---+
704
705       --  Note: The above structure actually corresponds to
706
707       --    (A or (B or C)) (D or E)
708
709       --  rather than
710
711       --    ((A or B) or C) (D or E)
712
713       --  which is the more natural interpretation, but in fact alternation
714       --  is associative, and the construction of an alternative changes the
715       --  left grouped pattern to the right grouped pattern in any case, so
716       --  that the Image function produces a more natural looking output.
717
718       ---------
719       -- Arb --
720       ---------
721
722       --  An Arb pattern builds the structure
723
724       --    +---+
725       --    | X |---->
726       --    +---+
727       --      .
728       --      .
729       --    +---+
730       --    | Y |---->
731       --    +---+
732
733       --  The X node is a PC_Arb_X node, which matches null, and stacks a
734       --  pointer to Y node, which is the PC_Arb_Y node that matches one
735       --  extra character and restacks itself.
736
737       --  The PC_Arb_X node is numbered 2, and the PC_Arb_Y node is 1.
738
739       -------------------------
740       -- Arbno (simple case) --
741       -------------------------
742
743       --  The simple form of Arbno can be used where the pattern always
744       --  matches at least one character if it succeeds, and it is known
745       --  not to make any history stack entries. In this case, Arbno (P)
746       --  can construct the following structure:
747
748       --      +-------------+
749       --      |             ^
750       --      V             |
751       --    +---+           |
752       --    | S |---->      |
753       --    +---+           |
754       --      .             |
755       --      .             |
756       --    +---+           |
757       --    | P |---------->+
758       --    +---+
759
760       --  The S (PC_Arbno_S) node matches null stacking a pointer to the
761       --  pattern P. If a subsequent failure causes P to be matched and
762       --  this match succeeds, then node A gets restacked to try another
763       --  instance if needed by a subsequent failure.
764
765       --  The node numbering of the constituent pattern P is not affected.
766       --  The S node has a node number of P.Index + 1.
767
768       --------------------------
769       -- Arbno (complex case) --
770       --------------------------
771
772       --  A call to Arbno (P), where P can match null (or at least is not
773       --  known to require a non-null string) and/or P requires pattern stack
774       --  entries, constructs the following structure:
775
776       --      +--------------------------+
777       --      |                          ^
778       --      V                          |
779       --    +---+                        |
780       --    | X |---->                   |
781       --    +---+                        |
782       --      .                          |
783       --      .                          |
784       --    +---+     +---+     +---+    |
785       --    | E |---->| P |---->| Y |--->+
786       --    +---+     +---+     +---+
787
788       --  The node X (PC_Arbno_X) matches null, stacking a pointer to the
789       --  E-P-X structure used to match one Arbno instance.
790
791       --  Here E is the PC_R_Enter node which matches null and creates two
792       --  stack entries. The first is a special entry whose node field is
793       --  not used at all, and whose cursor field has the initial cursor.
794
795       --  The second entry corresponds to a standard new region action. A
796       --  PC_R_Remove node is stacked, whose cursor field is used to store
797       --  the outer stack base, and the stack base is reset to point to
798       --  this PC_R_Remove node. Then the pattern P is matched, and it can
799       --  make history stack entries in the normal manner, so now the stack
800       --  looks like:
801
802       --     (stack entries made before assign pattern)
803
804       --     (Special entry, node field not used,
805       --      used only to save initial cursor)
806
807       --     (PC_R_Remove entry, "cursor" value is (negative)  <-- Stack Base
808       --      saved base value for the enclosing region)
809
810       --     (stack entries made by matching P)
811
812       --  If the match of P fails, then the PC_R_Remove entry is popped and
813       --  it removes both itself and the special entry underneath it,
814       --  restores the outer stack base, and signals failure.
815
816       --  If the match of P succeeds, then node Y, the PC_Arbno_Y node, pops
817       --  the inner region. There are two possibilities. If matching P left
818       --  no stack entries, then all traces of the inner region can be removed.
819       --  If there are stack entries, then we push an PC_Region_Replace stack
820       --  entry whose "cursor" value is the inner stack base value, and then
821       --  restore the outer stack base value, so the stack looks like:
822
823       --     (stack entries made before assign pattern)
824
825       --     (Special entry, node field not used,
826       --      used only to save initial cursor)
827
828       --     (PC_R_Remove entry, "cursor" value is (negative)
829       --      saved base value for the enclosing region)
830
831       --     (stack entries made by matching P)
832
833       --     (PC_Region_Replace entry, "cursor" value is (negative)
834       --      stack pointer value referencing the PC_R_Remove entry).
835
836       --  Now that we have matched another instance of the Arbno pattern,
837       --  we need to move to the successor. There are two cases. If the
838       --  Arbno pattern matched null, then there is no point in seeking
839       --  alternatives, since we would just match a whole bunch of nulls.
840       --  In this case we look through the alternative node, and move
841       --  directly to its successor (i.e. the successor of the Arbno
842       --  pattern). If on the other hand a non-null string was matched,
843       --  we simply follow the successor to the alternative node, which
844       --  sets up for another possible match of the Arbno pattern.
845
846       --  As noted in the section on stack checking, the stack count (and
847       --  hence the stack check) for a pattern includes only one iteration
848       --  of the Arbno pattern. To make sure that multiple iterations do not
849       --  overflow the stack, the Arbno node saves the stack count required
850       --  by a single iteration, and the Concat function increments this to
851       --  include stack entries required by any successor. The PC_Arbno_Y
852       --  node uses this count to ensure that sufficient stack remains
853       --  before proceeding after matching each new instance.
854
855       --  The node numbering of the constituent pattern P is not affected.
856       --  Where N is the number of nodes in P, the Y node is numbered N + 1,
857       --  the E node is N + 2, and the X node is N + 3.
858
859       ----------------------
860       -- Assign Immediate --
861       ----------------------
862
863       --  Immediate assignment (P * V) constructs the following structure
864
865       --    +---+     +---+     +---+
866       --    | E |---->| P |---->| A |---->
867       --    +---+     +---+     +---+
868
869       --  Here E is the PC_R_Enter node which matches null and creates two
870       --  stack entries. The first is a special entry whose node field is
871       --  not used at all, and whose cursor field has the initial cursor.
872
873       --  The second entry corresponds to a standard new region action. A
874       --  PC_R_Remove node is stacked, whose cursor field is used to store
875       --  the outer stack base, and the stack base is reset to point to
876       --  this PC_R_Remove node. Then the pattern P is matched, and it can
877       --  make history stack entries in the normal manner, so now the stack
878       --  looks like:
879
880       --     (stack entries made before assign pattern)
881
882       --     (Special entry, node field not used,
883       --      used only to save initial cursor)
884
885       --     (PC_R_Remove entry, "cursor" value is (negative)  <-- Stack Base
886       --      saved base value for the enclosing region)
887
888       --     (stack entries made by matching P)
889
890       --  If the match of P fails, then the PC_R_Remove entry is popped
891       --  and it removes both itself and the special entry underneath it,
892       --  restores the outer stack base, and signals failure.
893
894       --  If the match of P succeeds, then node A, which is the actual
895       --  PC_Assign_Imm node, executes the assignment (using the stack
896       --  base to locate the entry with the saved starting cursor value),
897       --  and the pops the inner region. There are two possibilities, if
898       --  matching P left no stack entries, then all traces of the inner
899       --  region can be removed. If there are stack entries, then we push
900       --  an PC_Region_Replace stack entry whose "cursor" value is the
901       --  inner stack base value, and then restore the outer stack base
902       --  value, so the stack looks like:
903
904       --     (stack entries made before assign pattern)
905
906       --     (Special entry, node field not used,
907       --      used only to save initial cursor)
908
909       --     (PC_R_Remove entry, "cursor" value is (negative)
910       --      saved base value for the enclosing region)
911
912       --     (stack entries made by matching P)
913
914       --     (PC_Region_Replace entry, "cursor" value is the (negative)
915       --      stack pointer value referencing the PC_R_Remove entry).
916
917       --  If a subsequent failure occurs, the PC_Region_Replace node restores
918       --  the inner stack base value and signals failure to explore rematches
919       --  of the pattern P.
920
921       --  The node numbering of the constituent pattern P is not affected.
922       --  Where N is the number of nodes in P, the A node is numbered N + 1,
923       --  and the E node is N + 2.
924
925       ---------------------
926       -- Assign On Match --
927       ---------------------
928
929       --  The assign on match (**) pattern is quite similar to the assign
930       --  immediate pattern, except that the actual assignment has to be
931       --  delayed. The following structure is constructed:
932
933       --    +---+     +---+     +---+
934       --    | E |---->| P |---->| A |---->
935       --    +---+     +---+     +---+
936
937       --  The operation of this pattern is identical to that described above
938       --  for deferred assignment, up to the point where P has been matched.
939
940       --  The A node, which is the PC_Assign_OnM node first pushes a
941       --  PC_Assign node onto the history stack. This node saves the ending
942       --  cursor and acts as a flag for the final assignment, as further
943       --  described below.
944
945       --  It then stores a pointer to itself in the special entry node field.
946       --  This was otherwise unused, and is now used to retrive the address
947       --  of the variable to be assigned at the end of the pattern.
948
949       --  After that the inner region is terminated in the usual manner,
950       --  by stacking a PC_R_Restore entry as described for the assign
951       --  immediate case. Note that the optimization of completely
952       --  removing the inner region does not happen in this case, since
953       --  we have at least one stack entry (the PC_Assign one we just made).
954       --  The stack now looks like:
955
956       --     (stack entries made before assign pattern)
957
958       --     (Special entry, node points to copy of
959       --      the PC_Assign_OnM node, and the
960       --      cursor field saves the initial cursor).
961
962       --     (PC_R_Remove entry, "cursor" value is (negative)
963       --      saved base value for the enclosing region)
964
965       --     (stack entries made by matching P)
966
967       --     (PC_Assign entry, saves final cursor)
968
969       --     (PC_Region_Replace entry, "cursor" value is (negative)
970       --      stack pointer value referencing the PC_R_Remove entry).
971
972       --  If a subsequent failure causes the PC_Assign node to execute it
973       --  simply removes itself and propagates the failure.
974
975       --  If the match succeeds, then the history stack is scanned for
976       --  PC_Assign nodes, and the assignments are executed (examination
977       --  of the above diagram will show that all the necessary data is
978       --  at hand for the assignment).
979
980       --  To optimize the common case where no assign-on-match operations
981       --  are present, a global flag Assign_OnM is maintained which is
982       --  initialize to False, and gets set True as part of the execution
983       --  of the PC_Assign_OnM node. The scan of the history stack for
984       --  PC_Assign entries is done only if this flag is set.
985
986       --  The node numbering of the constituent pattern P is not affected.
987       --  Where N is the number of nodes in P, the A node is numbered N + 1,
988       --  and the E node is N + 2.
989
990       ---------
991       -- Bal --
992       ---------
993
994       --  Bal builds a single node:
995
996       --    +---+
997       --    | B |---->
998       --    +---+
999
1000       --  The node B is the PC_Bal node which matches a parentheses balanced
1001       --  string, starting at the current cursor position. It then updates
1002       --  the cursor past this matched string, and stacks a pointer to itself
1003       --  with this updated cursor value on the history stack, to extend the
1004       --  matched string on a subequent failure.
1005
1006       --  Since this is a single node it is numbered 1 (the reason we include
1007       --  it in the compound patterns section is that it backtracks).
1008
1009       ------------
1010       -- BreakX --
1011       ------------
1012
1013       --  BreakX builds the structure
1014
1015       --    +---+     +---+
1016       --    | B |---->| A |---->
1017       --    +---+     +---+
1018       --      ^         .
1019       --      |         .
1020       --      |       +---+
1021       --      +<------| X |
1022       --              +---+
1023
1024       --  Here the B node is the BreakX_xx node that performs a normal Break
1025       --  function. The A node is an alternative (PC_Alt) node that matches
1026       --  null, but stacks a pointer to node X (the PC_BreakX_X node) which
1027       --  extends the match one character (to eat up the previously detected
1028       --  break character), and then rematches the break.
1029
1030       --  The B node is numbered 3, the alternative node is 1, and the X
1031       --  node is 2.
1032
1033       -----------
1034       -- Fence --
1035       -----------
1036
1037       --  Fence builds a single node:
1038
1039       --    +---+
1040       --    | F |---->
1041       --    +---+
1042
1043       --  The element F, PC_Fence,  matches null, and stacks a pointer to a
1044       --  PC_Cancel element which will abort the match on a subsequent failure.
1045
1046       --  Since this is a single element it is numbered 1 (the reason we
1047       --  include it in the compound patterns section is that it backtracks).
1048
1049       --------------------
1050       -- Fence Function --
1051       --------------------
1052
1053       --  A call to the Fence function builds the structure:
1054
1055       --    +---+     +---+     +---+
1056       --    | E |---->| P |---->| X |---->
1057       --    +---+     +---+     +---+
1058
1059       --  Here E is the PC_R_Enter node which matches null and creates two
1060       --  stack entries. The first is a special entry which is not used at
1061       --  all in the fence case (it is present merely for uniformity with
1062       --  other cases of region enter operations).
1063
1064       --  The second entry corresponds to a standard new region action. A
1065       --  PC_R_Remove node is stacked, whose cursor field is used to store
1066       --  the outer stack base, and the stack base is reset to point to
1067       --  this PC_R_Remove node. Then the pattern P is matched, and it can
1068       --  make history stack entries in the normal manner, so now the stack
1069       --  looks like:
1070
1071       --     (stack entries made before fence pattern)
1072
1073       --     (Special entry, not used at all)
1074
1075       --     (PC_R_Remove entry, "cursor" value is (negative)  <-- Stack Base
1076       --      saved base value for the enclosing region)
1077
1078       --     (stack entries made by matching P)
1079
1080       --  If the match of P fails, then the PC_R_Remove entry is popped
1081       --  and it removes both itself and the special entry underneath it,
1082       --  restores the outer stack base, and signals failure.
1083
1084       --  If the match of P succeeds, then node X, the PC_Fence_X node, gets
1085       --  control. One might be tempted to think that at this point, the
1086       --  history stack entries made by matching P can just be removed since
1087       --  they certainly are not going to be used for rematching (that is
1088       --  whole point of Fence after all!) However, this is wrong, because
1089       --  it would result in the loss of possible assign-on-match entries
1090       --  for deferred pattern assignments.
1091
1092       --  Instead what we do is to make a special entry whose node references
1093       --  PC_Fence_Y, and whose cursor saves the inner stack base value, i.e.
1094       --  the pointer to the PC_R_Remove entry. Then the outer stack base
1095       --  pointer is restored, so the stack looks like:
1096
1097       --     (stack entries made before assign pattern)
1098
1099       --     (Special entry, not used at all)
1100
1101       --     (PC_R_Remove entry, "cursor" value is (negative)
1102       --      saved base value for the enclosing region)
1103
1104       --     (stack entries made by matching P)
1105
1106       --     (PC_Fence_Y entry, "cursor" value is (negative) stack
1107       --      pointer value referencing the PC_R_Remove entry).
1108
1109       --  If a subsequent failure occurs, then the PC_Fence_Y entry removes
1110       --  the entire inner region, including all entries made by matching P,
1111       --  and alternatives prior to the Fence pattern are sought.
1112
1113       --  The node numbering of the constituent pattern P is not affected.
1114       --  Where N is the number of nodes in P, the X node is numbered N + 1,
1115       --  and the E node is N + 2.
1116
1117       -------------
1118       -- Succeed --
1119       -------------
1120
1121       --  Succeed builds a single node:
1122
1123       --    +---+
1124       --    | S |---->
1125       --    +---+
1126
1127       --  The node S is the PC_Succeed node which matches null, and stacks
1128       --  a pointer to itself on the history stack, so that a subsequent
1129       --  failure repeats the same match.
1130
1131       --  Since this is a single node it is numbered 1 (the reason we include
1132       --  it in the compound patterns section is that it backtracks).
1133
1134       ---------------------
1135       -- Write Immediate --
1136       ---------------------
1137
1138       --  The structure built for a write immediate operation (P * F, where
1139       --  F is a file access value) is:
1140
1141       --    +---+     +---+     +---+
1142       --    | E |---->| P |---->| W |---->
1143       --    +---+     +---+     +---+
1144
1145       --  Here E is the PC_R_Enter node and W is the PC_Write_Imm node. The
1146       --  handling is identical to that described above for Assign Immediate,
1147       --  except that at the point where a successful match occurs, the matched
1148       --  substring is written to the referenced file.
1149
1150       --  The node numbering of the constituent pattern P is not affected.
1151       --  Where N is the number of nodes in P, the W node is numbered N + 1,
1152       --  and the E node is N + 2.
1153
1154       --------------------
1155       -- Write On Match --
1156       --------------------
1157
1158       --  The structure built for a write on match operation (P ** F, where
1159       --  F is a file access value) is:
1160
1161       --    +---+     +---+     +---+
1162       --    | E |---->| P |---->| W |---->
1163       --    +---+     +---+     +---+
1164
1165       --  Here E is the PC_R_Enter node and W is the PC_Write_OnM node. The
1166       --  handling is identical to that described above for Assign On Match,
1167       --  except that at the point where a successful match has completed,
1168       --  the matched substring is written to the referenced file.
1169
1170       --  The node numbering of the constituent pattern P is not affected.
1171       --  Where N is the number of nodes in P, the W node is numbered N + 1,
1172       --  and the E node is N + 2.
1173    -----------------------
1174    -- Constant Patterns --
1175    -----------------------
1176
1177    --  The following pattern elements are referenced only from the pattern
1178    --  history stack. In each case the processing for the pattern element
1179    --  results in pattern match abort, or futher failure, so there is no
1180    --  need for a successor and no need for a node number
1181
1182    CP_Assign    : aliased PE := (PC_Assign,    0, N);
1183    CP_Cancel    : aliased PE := (PC_Cancel,    0, N);
1184    CP_Fence_Y   : aliased PE := (PC_Fence_Y,   0, N);
1185    CP_R_Remove  : aliased PE := (PC_R_Remove,  0, N);
1186    CP_R_Restore : aliased PE := (PC_R_Restore, 0, N);
1187
1188    -----------------------
1189    -- Local Subprograms --
1190    -----------------------
1191
1192    function Alternate (L, R : PE_Ptr) return PE_Ptr;
1193    function "or"      (L, R : PE_Ptr) return PE_Ptr renames Alternate;
1194    --  Build pattern structure corresponding to the alternation of L, R.
1195    --  (i.e. try to match L, and if that fails, try to match R).
1196
1197    function Arbno_Simple (P : PE_Ptr) return PE_Ptr;
1198    --  Build simple Arbno pattern, P is a pattern that is guaranteed to
1199    --  match at least one character if it succeeds and to require no
1200    --  stack entries under all circumstances. The result returned is
1201    --  a simple Arbno structure as previously described.
1202
1203    function Bracket (E, P, A : PE_Ptr) return PE_Ptr;
1204    --  Given two single node pattern elements E and A, and a (possible
1205    --  complex) pattern P, construct the concatenation E-->P-->A and
1206    --  return a pointer to E. The concatenation does not affect the
1207    --  node numbering in P. A has a number one higher than the maximum
1208    --  number in P, and E has a number two higher than the maximum
1209    --  number in P (see for example the Assign_Immediate structure to
1210    --  understand a typical use of this function).
1211
1212    function BreakX_Make (B : PE_Ptr) return Pattern;
1213    --  Given a pattern element for a Break patternx, returns the
1214    --  corresponding BreakX compound pattern structure.
1215
1216    function Concat (L, R : PE_Ptr; Incr : Natural) return PE_Ptr;
1217    --  Creates a pattern eelement that represents a concatenation of the
1218    --  two given pattern elements (i.e. the pattern L followed by R).
1219    --  The result returned is always the same as L, but the pattern
1220    --  referenced by L is modified to have R as a successor. This
1221    --  procedure does not copy L or R, so if a copy is required, it
1222    --  is the responsibility of the caller. The Incr parameter is an
1223    --  amount to be added to the Nat field of any P_Arbno_Y node that is
1224    --  in the left operand, it represents the additional stack space
1225    --  required by the right operand.
1226
1227    function C_To_PE (C : PChar) return PE_Ptr;
1228    --  Given a character, constructs a pattern element that matches
1229    --  the single character.
1230
1231    function Copy (P : PE_Ptr) return PE_Ptr;
1232    --  Creates a copy of the pattern element referenced by the given
1233    --  pattern element reference. This is a deep copy, which means that
1234    --  it follows the Next and Alt pointers.
1235
1236    function Image (P : PE_Ptr) return String;
1237    --  Returns the image of the address of the referenced pattern element.
1238    --  This is equivalent to Image (To_Address (P));
1239
1240    function Is_In (C : Character; Str : String) return Boolean;
1241    pragma Inline (Is_In);
1242    --  Determines if the character C is in string Str.
1243
1244    procedure Logic_Error;
1245    --  Called to raise Program_Error with an appropriate message if an
1246    --  internal logic error is detected.
1247
1248    function Str_BF (A : Boolean_Func)   return String;
1249    function Str_FP (A : File_Ptr)       return String;
1250    function Str_NF (A : Natural_Func)   return String;
1251    function Str_NP (A : Natural_Ptr)    return String;
1252    function Str_PP (A : Pattern_Ptr)    return String;
1253    function Str_VF (A : VString_Func)   return String;
1254    function Str_VP (A : VString_Ptr)    return String;
1255    --  These are debugging routines, which return a representation of the
1256    --  given access value (they are called only by Image and Dump)
1257
1258    procedure Set_Successor (Pat : PE_Ptr; Succ : PE_Ptr);
1259    --  Adjusts all EOP pointers in Pat to point to Succ. No other changes
1260    --  are made. In particular, Succ is unchanged, and no index numbers
1261    --  are modified. Note that Pat may not be equal to EOP on entry.
1262
1263    function S_To_PE (Str : PString) return PE_Ptr;
1264    --  Given a string, constructs a pattern element that matches the string
1265
1266    procedure Uninitialized_Pattern;
1267    pragma No_Return (Uninitialized_Pattern);
1268    --  Called to raise Program_Error with an appropriate error message if
1269    --  an uninitialized pattern is used in any pattern construction or
1270    --  pattern matching operation.
1271
1272    procedure XMatch
1273      (Subject : String;
1274       Pat_P   : PE_Ptr;
1275       Pat_S   : Natural;
1276       Start   : out Natural;
1277       Stop    : out Natural);
1278    --  This is the common pattern match routine. It is passed a string and
1279    --  a pattern, and it indicates success or failure, and on success the
1280    --  section of the string matched. It does not perform any assignments
1281    --  to the subject string, so pattern replacement is for the caller.
1282    --
1283    --  Subject The subject string. The lower bound is always one. In the
1284    --          Match procedures, it is fine to use strings whose lower bound
1285    --          is not one, but we perform a one time conversion before the
1286    --          call to XMatch, so that XMatch does not have to be bothered
1287    --          with strange lower bounds.
1288    --
1289    --  Pat_P   Points to initial pattern element of pattern to be matched
1290    --
1291    --  Pat_S   Maximum required stack entries for pattern to be matched
1292    --
1293    --  Start   If match is successful, starting index of matched section.
1294    --          This value is always non-zero. A value of zero is used to
1295    --          indicate a failed match.
1296    --
1297    --  Stop    If match is successful, ending index of matched section.
1298    --          This can be zero if we match the null string at the start,
1299    --          in which case Start is set to zero, and Stop to one. If the
1300    --          Match fails, then the contents of Stop is undefined.
1301
1302    procedure XMatchD
1303      (Subject : String;
1304       Pat_P   : PE_Ptr;
1305       Pat_S   : Natural;
1306       Start   : out Natural;
1307       Stop    : out Natural);
1308    --  Identical in all respects to XMatch, except that trace information is
1309    --  output on Standard_Ouput during execution of the match. This is the
1310    --  version that is called if the original Match call has Debug => True.
1311
1312    ---------
1313    -- "&" --
1314    ---------
1315
1316    function "&" (L : PString; R : Pattern) return Pattern is
1317    begin
1318       return (AFC with R.Stk, Concat (S_To_PE (L), Copy (R.P), R.Stk));
1319    end "&";
1320
1321    function "&" (L : Pattern; R : PString) return Pattern is
1322    begin
1323       return (AFC with L.Stk, Concat (Copy (L.P), S_To_PE (R), 0));
1324    end "&";
1325
1326    function "&" (L : PChar; R : Pattern) return Pattern is
1327    begin
1328       return (AFC with R.Stk, Concat (C_To_PE (L), Copy (R.P), R.Stk));
1329    end "&";
1330
1331    function "&" (L : Pattern; R : PChar) return Pattern is
1332    begin
1333       return (AFC with L.Stk, Concat (Copy (L.P), C_To_PE (R), 0));
1334    end "&";
1335
1336    function "&" (L : Pattern; R : Pattern) return Pattern is
1337    begin
1338       return (AFC with L.Stk + R.Stk, Concat (Copy (L.P), Copy (R.P), R.Stk));
1339    end "&";
1340
1341    ---------
1342    -- "*" --
1343    ---------
1344
1345    --  Assign immediate
1346
1347    --    +---+     +---+     +---+
1348    --    | E |---->| P |---->| A |---->
1349    --    +---+     +---+     +---+
1350
1351    --  The node numbering of the constituent pattern P is not affected.
1352    --  Where N is the number of nodes in P, the A node is numbered N + 1,
1353    --  and the E node is N + 2.
1354
1355    function "*" (P : Pattern; Var : VString_Var) return Pattern is
1356       Pat : constant PE_Ptr := Copy (P.P);
1357       E   : constant PE_Ptr := new PE'(PC_R_Enter,    0, EOP);
1358       A   : constant PE_Ptr :=
1359               new PE'(PC_Assign_Imm, 0, EOP, Var'Unrestricted_Access);
1360
1361    begin
1362       return (AFC with P.Stk + 3, Bracket (E, Pat, A));
1363    end "*";
1364
1365    function "*" (P : PString; Var : VString_Var) return Pattern is
1366       Pat : constant PE_Ptr := S_To_PE (P);
1367       E   : constant PE_Ptr := new PE'(PC_R_Enter,    0, EOP);
1368       A   : constant PE_Ptr :=
1369               new PE'(PC_Assign_Imm, 0, EOP, Var'Unrestricted_Access);
1370
1371    begin
1372       return (AFC with 3, Bracket (E, Pat, A));
1373    end "*";
1374
1375    function "*" (P : PChar; Var : VString_Var) return Pattern is
1376       Pat : constant PE_Ptr := C_To_PE (P);
1377       E   : constant PE_Ptr := new PE'(PC_R_Enter,    0, EOP);
1378       A   : constant PE_Ptr :=
1379               new PE'(PC_Assign_Imm, 0, EOP, Var'Unrestricted_Access);
1380
1381    begin
1382       return (AFC with 3, Bracket (E, Pat, A));
1383    end "*";
1384
1385    --  Write immediate
1386
1387    --    +---+     +---+     +---+
1388    --    | E |---->| P |---->| W |---->
1389    --    +---+     +---+     +---+
1390
1391    --  The node numbering of the constituent pattern P is not affected.
1392    --  Where N is the number of nodes in P, the W node is numbered N + 1,
1393    --  and the E node is N + 2.
1394
1395    function "*" (P : Pattern; Fil : File_Access) return Pattern is
1396       Pat : constant PE_Ptr := Copy (P.P);
1397       E   : constant PE_Ptr := new PE'(PC_R_Enter,   0, EOP);
1398       W   : constant PE_Ptr := new PE'(PC_Write_Imm, 0, EOP, Fil);
1399
1400    begin
1401       return (AFC with 3, Bracket (E, Pat, W));
1402    end "*";
1403
1404    function "*" (P : PString; Fil : File_Access) return Pattern is
1405       Pat : constant PE_Ptr := S_To_PE (P);
1406       E   : constant PE_Ptr := new PE'(PC_R_Enter,   0, EOP);
1407       W   : constant PE_Ptr := new PE'(PC_Write_Imm, 0, EOP, Fil);
1408
1409    begin
1410       return (AFC with 3, Bracket (E, Pat, W));
1411    end "*";
1412
1413    function "*" (P : PChar; Fil : File_Access) return Pattern is
1414       Pat : constant PE_Ptr := C_To_PE (P);
1415       E   : constant PE_Ptr := new PE'(PC_R_Enter,   0, EOP);
1416       W   : constant PE_Ptr := new PE'(PC_Write_Imm, 0, EOP, Fil);
1417
1418    begin
1419       return (AFC with 3, Bracket (E, Pat, W));
1420    end "*";
1421
1422    ----------
1423    -- "**" --
1424    ----------
1425
1426    --  Assign on match
1427
1428    --    +---+     +---+     +---+
1429    --    | E |---->| P |---->| A |---->
1430    --    +---+     +---+     +---+
1431
1432    --  The node numbering of the constituent pattern P is not affected.
1433    --  Where N is the number of nodes in P, the A node is numbered N + 1,
1434    --  and the E node is N + 2.
1435
1436    function "**" (P : Pattern; Var : VString_Var) return Pattern is
1437       Pat : constant PE_Ptr := Copy (P.P);
1438       E   : constant PE_Ptr := new PE'(PC_R_Enter,    0, EOP);
1439       A   : constant PE_Ptr :=
1440               new PE'(PC_Assign_OnM, 0, EOP, Var'Unrestricted_Access);
1441
1442    begin
1443       return (AFC with P.Stk + 3, Bracket (E, Pat, A));
1444    end "**";
1445
1446    function "**" (P : PString; Var : VString_Var) return Pattern is
1447       Pat : constant PE_Ptr := S_To_PE (P);
1448       E   : constant PE_Ptr := new PE'(PC_R_Enter,    0, EOP);
1449       A   : constant PE_Ptr :=
1450               new PE'(PC_Assign_OnM, 0, EOP, Var'Unrestricted_Access);
1451
1452    begin
1453       return (AFC with 3, Bracket (E, Pat, A));
1454    end "**";
1455
1456    function "**" (P : PChar; Var : VString_Var) return Pattern is
1457       Pat : constant PE_Ptr := C_To_PE (P);
1458       E   : constant PE_Ptr := new PE'(PC_R_Enter,    0, EOP);
1459       A   : constant PE_Ptr :=
1460               new PE'(PC_Assign_OnM, 0, EOP, Var'Unrestricted_Access);
1461
1462    begin
1463       return (AFC with 3, Bracket (E, Pat, A));
1464    end "**";
1465
1466    --  Write on match
1467
1468    --    +---+     +---+     +---+
1469    --    | E |---->| P |---->| W |---->
1470    --    +---+     +---+     +---+
1471
1472    --  The node numbering of the constituent pattern P is not affected.
1473    --  Where N is the number of nodes in P, the W node is numbered N + 1,
1474    --  and the E node is N + 2.
1475
1476    function "**" (P : Pattern; Fil : File_Access) return Pattern is
1477       Pat : constant PE_Ptr := Copy (P.P);
1478       E   : constant PE_Ptr := new PE'(PC_R_Enter,   0, EOP);
1479       W   : constant PE_Ptr := new PE'(PC_Write_OnM, 0, EOP, Fil);
1480
1481    begin
1482       return (AFC with P.Stk + 3, Bracket (E, Pat, W));
1483    end "**";
1484
1485    function "**" (P : PString; Fil : File_Access) return Pattern is
1486       Pat : constant PE_Ptr := S_To_PE (P);
1487       E   : constant PE_Ptr := new PE'(PC_R_Enter,   0, EOP);
1488       W   : constant PE_Ptr := new PE'(PC_Write_OnM, 0, EOP, Fil);
1489
1490    begin
1491       return (AFC with 3, Bracket (E, Pat, W));
1492    end "**";
1493
1494    function "**" (P : PChar; Fil : File_Access) return Pattern is
1495       Pat : constant PE_Ptr := C_To_PE (P);
1496       E   : constant PE_Ptr := new PE'(PC_R_Enter,   0, EOP);
1497       W   : constant PE_Ptr := new PE'(PC_Write_OnM, 0, EOP, Fil);
1498
1499    begin
1500       return (AFC with 3, Bracket (E, Pat, W));
1501    end "**";
1502
1503    ---------
1504    -- "+" --
1505    ---------
1506
1507    function "+" (Str : VString_Var) return Pattern is
1508    begin
1509       return
1510         (AFC with 0,
1511          new PE'(PC_String_VP, 1, EOP, Str'Unrestricted_Access));
1512    end "+";
1513
1514    function "+" (Str : VString_Func) return Pattern is
1515    begin
1516       return (AFC with 0, new PE'(PC_String_VF, 1, EOP, Str));
1517    end "+";
1518
1519    function "+" (P : Pattern_Var) return Pattern is
1520    begin
1521       return
1522         (AFC with 3,
1523          new PE'(PC_Rpat, 1, EOP, P'Unrestricted_Access));
1524    end "+";
1525
1526    function "+" (P : Boolean_Func) return Pattern is
1527    begin
1528       return (AFC with 3, new PE'(PC_Pred_Func, 1, EOP, P));
1529    end "+";
1530
1531    ----------
1532    -- "or" --
1533    ----------
1534
1535    function "or" (L : PString; R : Pattern) return Pattern is
1536    begin
1537       return (AFC with R.Stk + 1, S_To_PE (L) or Copy (R.P));
1538    end "or";
1539
1540    function "or" (L : Pattern; R : PString) return Pattern is
1541    begin
1542       return (AFC with L.Stk + 1, Copy (L.P) or S_To_PE (R));
1543    end "or";
1544
1545    function "or" (L : PString; R : PString) return Pattern is
1546    begin
1547       return (AFC with 1, S_To_PE (L) or S_To_PE (R));
1548    end "or";
1549
1550    function "or" (L : Pattern; R : Pattern) return Pattern is
1551    begin
1552       return (AFC with
1553                 Natural'Max (L.Stk, R.Stk) + 1, Copy (L.P) or Copy (R.P));
1554    end "or";
1555
1556    function "or" (L : PChar;   R : Pattern) return Pattern is
1557    begin
1558       return (AFC with 1, C_To_PE (L) or Copy (R.P));
1559    end "or";
1560
1561    function "or" (L : Pattern; R : PChar) return Pattern is
1562    begin
1563       return (AFC with 1, Copy (L.P) or C_To_PE (R));
1564    end "or";
1565
1566    function "or" (L : PChar;   R : PChar) return Pattern is
1567    begin
1568       return (AFC with 1, C_To_PE (L) or C_To_PE (R));
1569    end "or";
1570
1571    function "or" (L : PString; R : PChar) return Pattern is
1572    begin
1573       return (AFC with 1, S_To_PE (L) or C_To_PE (R));
1574    end "or";
1575
1576    function "or" (L : PChar;   R : PString) return Pattern is
1577    begin
1578       return (AFC with 1, C_To_PE (L) or S_To_PE (R));
1579    end "or";
1580
1581    ------------
1582    -- Adjust --
1583    ------------
1584
1585    --  No two patterns share the same pattern elements, so the adjust
1586    --  procedure for a Pattern assignment must do a deep copy of the
1587    --  pattern element structure.
1588
1589    procedure Adjust (Object : in out Pattern) is
1590    begin
1591       Object.P := Copy (Object.P);
1592    end Adjust;
1593
1594    ---------------
1595    -- Alternate --
1596    ---------------
1597
1598    function Alternate (L, R : PE_Ptr) return PE_Ptr is
1599    begin
1600       --  If the left pattern is null, then we just add the alternation
1601       --  node with an index one greater than the right hand pattern.
1602
1603       if L = EOP then
1604          return new PE'(PC_Alt, R.Index + 1, EOP, R);
1605
1606       --  If the left pattern is non-null, then build a reference vector
1607       --  for its elements, and adjust their index values to acccomodate
1608       --  the right hand elements. Then add the alternation node.
1609
1610       else
1611          declare
1612             Refs : Ref_Array (1 .. L.Index);
1613
1614          begin
1615             Build_Ref_Array (L, Refs);
1616
1617             for J in Refs'Range loop
1618                Refs (J).Index := Refs (J).Index + R.Index;
1619             end loop;
1620          end;
1621
1622          return new PE'(PC_Alt, L.Index + 1, L, R);
1623       end if;
1624    end Alternate;
1625
1626    ---------
1627    -- Any --
1628    ---------
1629
1630    function Any (Str : String) return Pattern is
1631    begin
1632       return (AFC with 0, new PE'(PC_Any_CS, 1, EOP, To_Set (Str)));
1633    end Any;
1634
1635    function Any (Str : VString) return Pattern is
1636    begin
1637       return Any (S (Str));
1638    end Any;
1639
1640    function Any (Str : Character) return Pattern is
1641    begin
1642       return (AFC with 0, new PE'(PC_Any_CH, 1, EOP, Str));
1643    end Any;
1644
1645    function Any (Str : Character_Set) return Pattern is
1646    begin
1647       return (AFC with 0, new PE'(PC_Any_CS, 1, EOP, Str));
1648    end Any;
1649
1650    function Any (Str : access VString) return Pattern is
1651    begin
1652       return (AFC with 0, new PE'(PC_Any_VP, 1, EOP, VString_Ptr (Str)));
1653    end Any;
1654
1655    function Any (Str : VString_Func) return Pattern is
1656    begin
1657       return (AFC with 0, new PE'(PC_Any_VF, 1, EOP, Str));
1658    end Any;
1659
1660    ---------
1661    -- Arb --
1662    ---------
1663
1664    --    +---+
1665    --    | X |---->
1666    --    +---+
1667    --      .
1668    --      .
1669    --    +---+
1670    --    | Y |---->
1671    --    +---+
1672
1673    --  The PC_Arb_X element is numbered 2, and the PC_Arb_Y element is 1.
1674
1675    function Arb return Pattern is
1676       Y : constant PE_Ptr := new PE'(PC_Arb_Y, 1, EOP);
1677       X : constant PE_Ptr := new PE'(PC_Arb_X, 2, EOP, Y);
1678
1679    begin
1680       return (AFC with 1, X);
1681    end Arb;
1682
1683    -----------
1684    -- Arbno --
1685    -----------
1686
1687    function Arbno (P : PString) return Pattern is
1688    begin
1689       if P'Length = 0 then
1690          return (AFC with 0, EOP);
1691
1692       else
1693          return (AFC with 0, Arbno_Simple (S_To_PE (P)));
1694       end if;
1695    end Arbno;
1696
1697    function Arbno (P : PChar) return Pattern is
1698    begin
1699       return (AFC with 0, Arbno_Simple (C_To_PE (P)));
1700    end Arbno;
1701
1702    function Arbno (P : Pattern) return Pattern is
1703       Pat : constant PE_Ptr := Copy (P.P);
1704
1705    begin
1706       if P.Stk = 0
1707         and then OK_For_Simple_Arbno (Pat.Pcode)
1708       then
1709          return (AFC with 0, Arbno_Simple (Pat));
1710       end if;
1711
1712       --  This is the complex case, either the pattern makes stack entries
1713       --  or it is possible for the pattern to match the null string (more
1714       --  accurately, we don't know that this is not the case).
1715
1716       --      +--------------------------+
1717       --      |                          ^
1718       --      V                          |
1719       --    +---+                        |
1720       --    | X |---->                   |
1721       --    +---+                        |
1722       --      .                          |
1723       --      .                          |
1724       --    +---+     +---+     +---+    |
1725       --    | E |---->| P |---->| Y |--->+
1726       --    +---+     +---+     +---+
1727
1728       --  The node numbering of the constituent pattern P is not affected.
1729       --  Where N is the number of nodes in P, the Y node is numbered N + 1,
1730       --  the E node is N + 2, and the X node is N + 3.
1731
1732       declare
1733          E   : constant PE_Ptr := new PE'(PC_R_Enter, 0, EOP);
1734          X   : constant PE_Ptr := new PE'(PC_Arbno_X, 0, EOP, E);
1735          Y   : constant PE_Ptr := new PE'(PC_Arbno_Y, 0, X,   P.Stk + 3);
1736          EPY : constant PE_Ptr := Bracket (E, Pat, Y);
1737
1738       begin
1739          X.Alt := EPY;
1740          X.Index := EPY.Index + 1;
1741          return (AFC with P.Stk + 3, X);
1742       end;
1743    end Arbno;
1744
1745    ------------------
1746    -- Arbno_Simple --
1747    ------------------
1748
1749       --      +-------------+
1750       --      |             ^
1751       --      V             |
1752       --    +---+           |
1753       --    | S |---->      |
1754       --    +---+           |
1755       --      .             |
1756       --      .             |
1757       --    +---+           |
1758       --    | P |---------->+
1759       --    +---+
1760
1761    --  The node numbering of the constituent pattern P is not affected.
1762    --  The S node has a node number of P.Index + 1.
1763
1764    --  Note that we know that P cannot be EOP, because a null pattern
1765    --  does not meet the requirements for simple Arbno.
1766
1767    function Arbno_Simple (P : PE_Ptr) return PE_Ptr is
1768       S : constant PE_Ptr := new PE'(PC_Arbno_S, P.Index + 1, EOP, P);
1769
1770    begin
1771       Set_Successor (P, S);
1772       return S;
1773    end Arbno_Simple;
1774
1775    ---------
1776    -- Bal --
1777    ---------
1778
1779    function Bal return Pattern is
1780    begin
1781       return (AFC with 1, new PE'(PC_Bal, 1, EOP));
1782    end Bal;
1783
1784    -------------
1785    -- Bracket --
1786    -------------
1787
1788    function Bracket (E, P, A : PE_Ptr) return PE_Ptr is
1789    begin
1790       if P = EOP then
1791          E.Pthen := A;
1792          E.Index := 2;
1793          A.Index := 1;
1794
1795       else
1796          E.Pthen := P;
1797          Set_Successor (P, A);
1798          E.Index := P.Index + 2;
1799          A.Index := P.Index + 1;
1800       end if;
1801
1802       return E;
1803    end Bracket;
1804
1805    -----------
1806    -- Break --
1807    -----------
1808
1809    function Break (Str : String) return Pattern is
1810    begin
1811       return (AFC with 0, new PE'(PC_Break_CS, 1, EOP, To_Set (Str)));
1812    end Break;
1813
1814    function Break (Str : VString) return Pattern is
1815    begin
1816       return Break (S (Str));
1817    end Break;
1818
1819    function Break (Str : Character) return Pattern is
1820    begin
1821       return (AFC with 0, new PE'(PC_Break_CH, 1, EOP, Str));
1822    end Break;
1823
1824    function Break (Str : Character_Set) return Pattern is
1825    begin
1826       return (AFC with 0, new PE'(PC_Break_CS, 1, EOP, Str));
1827    end Break;
1828
1829    function Break (Str : access VString) return Pattern is
1830    begin
1831       return (AFC with 0, new PE'(PC_Break_VP, 1, EOP, VString_Ptr (Str)));
1832    end Break;
1833
1834    function Break (Str : VString_Func) return Pattern is
1835    begin
1836       return (AFC with 0, new PE'(PC_Break_VF, 1, EOP, Str));
1837    end Break;
1838
1839    ------------
1840    -- BreakX --
1841    ------------
1842
1843    function BreakX (Str : String) return Pattern is
1844    begin
1845       return BreakX_Make (new PE'(PC_BreakX_CS, 3, N, To_Set (Str)));
1846    end BreakX;
1847
1848    function BreakX (Str : VString) return Pattern is
1849    begin
1850       return BreakX (S (Str));
1851    end BreakX;
1852
1853    function BreakX (Str : Character) return Pattern is
1854    begin
1855       return BreakX_Make (new PE'(PC_BreakX_CH, 3, N, Str));
1856    end BreakX;
1857
1858    function BreakX (Str : Character_Set) return Pattern is
1859    begin
1860       return BreakX_Make (new PE'(PC_BreakX_CS, 3, N, Str));
1861    end BreakX;
1862
1863    function BreakX (Str : access VString) return Pattern is
1864    begin
1865       return BreakX_Make (new PE'(PC_BreakX_VP, 3, N, VString_Ptr (Str)));
1866    end BreakX;
1867
1868    function BreakX (Str : VString_Func) return Pattern is
1869    begin
1870       return BreakX_Make (new PE'(PC_BreakX_VF, 3, N, Str));
1871    end BreakX;
1872
1873    -----------------
1874    -- BreakX_Make --
1875    -----------------
1876
1877    --    +---+     +---+
1878    --    | B |---->| A |---->
1879    --    +---+     +---+
1880    --      ^         .
1881    --      |         .
1882    --      |       +---+
1883    --      +<------| X |
1884    --              +---+
1885
1886    --  The B node is numbered 3, the alternative node is 1, and the X
1887    --  node is 2.
1888
1889    function BreakX_Make (B : PE_Ptr) return Pattern is
1890       X : constant PE_Ptr := new PE'(PC_BreakX_X, 2, B);
1891       A : constant PE_Ptr := new PE'(PC_Alt,      1, EOP, X);
1892
1893    begin
1894       B.Pthen := A;
1895       return (AFC with 2, B);
1896    end BreakX_Make;
1897
1898    ---------------------
1899    -- Build_Ref_Array --
1900    ---------------------
1901
1902    procedure Build_Ref_Array (E : PE_Ptr; RA : out Ref_Array) is
1903
1904       procedure Record_PE (E : PE_Ptr);
1905       --  Record given pattern element if not already recorded in RA,
1906       --  and also record any referenced pattern elements recursively.
1907
1908       procedure Record_PE (E : PE_Ptr) is
1909       begin
1910          PutD ("  Record_PE called with PE_Ptr = " & Image (E));
1911
1912          if E = EOP or else RA (E.Index) /= null then
1913             Put_LineD (", nothing to do");
1914             return;
1915
1916          else
1917             Put_LineD (", recording" & IndexT'Image (E.Index));
1918             RA (E.Index) := E;
1919             Record_PE (E.Pthen);
1920
1921             if E.Pcode in PC_Has_Alt then
1922                Record_PE (E.Alt);
1923             end if;
1924          end if;
1925       end Record_PE;
1926
1927    --  Start of processing for Build_Ref_Array
1928
1929    begin
1930       New_LineD;
1931       Put_LineD ("Entering Build_Ref_Array");
1932       Record_PE (E);
1933       New_LineD;
1934    end Build_Ref_Array;
1935
1936    -------------
1937    -- C_To_PE --
1938    -------------
1939
1940    function C_To_PE (C : PChar) return PE_Ptr is
1941    begin
1942       return new PE'(PC_Char, 1, EOP, C);
1943    end C_To_PE;
1944
1945    ------------
1946    -- Cancel --
1947    ------------
1948
1949    function Cancel return Pattern is
1950    begin
1951       return (AFC with 0, new PE'(PC_Cancel, 1, EOP));
1952    end Cancel;
1953
1954    ------------
1955    -- Concat --
1956    ------------
1957
1958    --  Concat needs to traverse the left operand performing the following
1959    --  set of fixups:
1960
1961    --    a) Any successor pointers (Pthen fields) that are set to EOP are
1962    --       reset to point to the second operand.
1963
1964    --    b) Any PC_Arbno_Y node has its stack count field incremented
1965    --       by the parameter Incr provided for this purpose.
1966
1967    --    d) Num fields of all pattern elements in the left operand are
1968    --       adjusted to include the elements of the right operand.
1969
1970    --  Note: we do not use Set_Successor in the processing for Concat, since
1971    --  there is no point in doing two traversals, we may as well do everything
1972    --  at the same time.
1973
1974    function Concat (L, R : PE_Ptr; Incr : Natural) return PE_Ptr is
1975    begin
1976       if L = EOP then
1977          return R;
1978
1979       elsif R = EOP then
1980          return L;
1981
1982       else
1983          declare
1984             Refs : Ref_Array (1 .. L.Index);
1985             --  We build a reference array for L whose N'th element points to
1986             --  the pattern element of L whose original Index value is N.
1987
1988             P : PE_Ptr;
1989
1990          begin
1991             Build_Ref_Array (L, Refs);
1992
1993             for J in Refs'Range loop
1994                P := Refs (J);
1995
1996                P.Index := P.Index + R.Index;
1997
1998                if P.Pcode = PC_Arbno_Y then
1999                   P.Nat := P.Nat + Incr;
2000                end if;
2001
2002                if P.Pthen = EOP then
2003                   P.Pthen := R;
2004                end if;
2005
2006                if P.Pcode in PC_Has_Alt and then P.Alt = EOP then
2007                   P.Alt := R;
2008                end if;
2009             end loop;
2010          end;
2011
2012          return L;
2013       end if;
2014    end Concat;
2015
2016    ----------
2017    -- Copy --
2018    ----------
2019
2020    function Copy (P : PE_Ptr) return PE_Ptr is
2021    begin
2022       if P = null then
2023          Uninitialized_Pattern;
2024
2025       else
2026          declare
2027             Refs : Ref_Array (1 .. P.Index);
2028             --  References to elements in P, indexed by Index field
2029
2030             Copy : Ref_Array (1 .. P.Index);
2031             --  Holds copies of elements of P, indexed by Index field.
2032
2033             E : PE_Ptr;
2034
2035          begin
2036             Build_Ref_Array (P, Refs);
2037
2038             --  Now copy all nodes
2039
2040             for J in Refs'Range loop
2041                Copy (J) := new PE'(Refs (J).all);
2042             end loop;
2043
2044             --  Adjust all internal references
2045
2046             for J in Copy'Range loop
2047                E := Copy (J);
2048
2049                --  Adjust successor pointer to point to copy
2050
2051                if E.Pthen /= EOP then
2052                   E.Pthen := Copy (E.Pthen.Index);
2053                end if;
2054
2055                --  Adjust Alt pointer if there is one to point to copy
2056
2057                if E.Pcode in PC_Has_Alt and then E.Alt /= EOP then
2058                   E.Alt := Copy (E.Alt.Index);
2059                end if;
2060
2061                --  Copy referenced string
2062
2063                if E.Pcode = PC_String then
2064                   E.Str := new String'(E.Str.all);
2065                end if;
2066             end loop;
2067
2068             return Copy (P.Index);
2069          end;
2070       end if;
2071    end Copy;
2072
2073    ----------
2074    -- Dump --
2075    ----------
2076
2077    procedure Dump (P : Pattern) is
2078
2079       subtype Count is Ada.Text_IO.Count;
2080       Scol : Count;
2081       --  Used to keep track of column in dump output
2082
2083       Refs : Ref_Array (1 .. P.P.Index);
2084       --  We build a reference array whose N'th element points to the
2085       --  pattern element whose Index value is N.
2086
2087       Cols : Natural := 2;
2088       --  Number of columns used for pattern numbers, minimum is 2
2089
2090       E : PE_Ptr;
2091
2092       procedure Write_Node_Id (E : PE_Ptr);
2093       --  Writes out a string identifying the given pattern element.
2094
2095       procedure Write_Node_Id (E : PE_Ptr) is
2096       begin
2097          if E = EOP then
2098             Put ("EOP");
2099
2100             for J in 4 .. Cols loop
2101                Put (' ');
2102             end loop;
2103
2104          else
2105             declare
2106                Str : String (1 .. Cols);
2107                N   : Natural := Natural (E.Index);
2108
2109             begin
2110                Put ("#");
2111
2112                for J in reverse Str'Range loop
2113                   Str (J) := Character'Val (48 + N mod 10);
2114                   N := N / 10;
2115                end loop;
2116
2117                Put (Str);
2118             end;
2119          end if;
2120       end Write_Node_Id;
2121
2122    begin
2123       New_Line;
2124       Put ("Pattern Dump Output (pattern at " &
2125            Image (P'Address) &
2126            ", S = " & Natural'Image (P.Stk) & ')');
2127
2128       Scol := Col;
2129       New_Line;
2130
2131       while Col < Scol loop
2132          Put ('-');
2133       end loop;
2134
2135       New_Line;
2136
2137       --  If uninitialized pattern, dump line and we are done
2138
2139       if P.P = null then
2140          Put_Line ("Uninitialized pattern value");
2141          return;
2142       end if;
2143
2144       --  If null pattern, just dump it and we are all done
2145
2146       if P.P = EOP then
2147          Put_Line ("EOP (null pattern)");
2148          return;
2149       end if;
2150
2151       Build_Ref_Array (P.P, Refs);
2152
2153       --  Set number of columns required for node numbers
2154
2155       while 10 ** Cols - 1 < Integer (P.P.Index) loop
2156          Cols := Cols + 1;
2157       end loop;
2158
2159       --  Now dump the nodes in reverse sequence. We output them in reverse
2160       --  sequence since this corresponds to the natural order used to
2161       --  construct the patterns.
2162
2163       for J in reverse Refs'Range loop
2164          E := Refs (J);
2165          Write_Node_Id (E);
2166          Set_Col (Count (Cols) + 4);
2167          Put (Image (E));
2168          Put ("  ");
2169          Put (Pattern_Code'Image (E.Pcode));
2170          Put ("  ");
2171          Set_Col (21 + Count (Cols) + Address_Image_Length);
2172          Write_Node_Id (E.Pthen);
2173          Set_Col (24 + 2 * Count (Cols) + Address_Image_Length);
2174
2175          case E.Pcode is
2176
2177             when PC_Alt     |
2178                  PC_Arb_X   |
2179                  PC_Arbno_S |
2180                  PC_Arbno_X =>
2181                Write_Node_Id (E.Alt);
2182
2183             when PC_Rpat =>
2184                Put (Str_PP (E.PP));
2185
2186             when PC_Pred_Func =>
2187                Put (Str_BF (E.BF));
2188
2189             when PC_Assign_Imm |
2190                  PC_Assign_OnM |
2191                  PC_Any_VP     |
2192                  PC_Break_VP   |
2193                  PC_BreakX_VP  |
2194                  PC_NotAny_VP  |
2195                  PC_NSpan_VP   |
2196                  PC_Span_VP    |
2197                  PC_String_VP  =>
2198                Put (Str_VP (E.VP));
2199
2200             when PC_Write_Imm  |
2201                  PC_Write_OnM =>
2202                Put (Str_FP (E.FP));
2203
2204             when PC_String =>
2205                Put (Image (E.Str.all));
2206
2207             when PC_String_2 =>
2208                Put (Image (E.Str2));
2209
2210             when PC_String_3 =>
2211                Put (Image (E.Str3));
2212
2213             when PC_String_4 =>
2214                Put (Image (E.Str4));
2215
2216             when PC_String_5 =>
2217                Put (Image (E.Str5));
2218
2219             when PC_String_6 =>
2220                Put (Image (E.Str6));
2221
2222             when PC_Setcur =>
2223                Put (Str_NP (E.Var));
2224
2225             when PC_Any_CH      |
2226                  PC_Break_CH    |
2227                  PC_BreakX_CH   |
2228                  PC_Char        |
2229                  PC_NotAny_CH   |
2230                  PC_NSpan_CH    |
2231                  PC_Span_CH     =>
2232                Put (''' & E.Char & ''');
2233
2234             when PC_Any_CS      |
2235                  PC_Break_CS    |
2236                  PC_BreakX_CS   |
2237                  PC_NotAny_CS   |
2238                  PC_NSpan_CS    |
2239                  PC_Span_CS     =>
2240                Put ('"' & To_Sequence (E.CS) & '"');
2241
2242             when PC_Arbno_Y     |
2243                  PC_Len_Nat     |
2244                  PC_Pos_Nat     |
2245                  PC_RPos_Nat    |
2246                  PC_RTab_Nat    |
2247                  PC_Tab_Nat     =>
2248                Put (S (E.Nat));
2249
2250             when PC_Pos_NF      |
2251                  PC_Len_NF      |
2252                  PC_RPos_NF     |
2253                  PC_RTab_NF     |
2254                  PC_Tab_NF      =>
2255                Put (Str_NF (E.NF));
2256
2257             when PC_Pos_NP      |
2258                  PC_Len_NP      |
2259                  PC_RPos_NP     |
2260                  PC_RTab_NP     |
2261                  PC_Tab_NP      =>
2262                Put (Str_NP (E.NP));
2263
2264             when PC_Any_VF      |
2265                  PC_Break_VF    |
2266                  PC_BreakX_VF   |
2267                  PC_NotAny_VF   |
2268                  PC_NSpan_VF    |
2269                  PC_Span_VF     |
2270                  PC_String_VF   =>
2271                Put (Str_VF (E.VF));
2272
2273             when others => null;
2274
2275          end case;
2276
2277          New_Line;
2278       end loop;
2279
2280       New_Line;
2281    end Dump;
2282
2283    ----------
2284    -- Fail --
2285    ----------
2286
2287    function Fail return Pattern is
2288    begin
2289       return (AFC with 0, new PE'(PC_Fail, 1, EOP));
2290    end Fail;
2291
2292    -----------
2293    -- Fence --
2294    -----------
2295
2296    --  Simple case
2297
2298    function Fence return Pattern is
2299    begin
2300       return (AFC with 1, new PE'(PC_Fence, 1, EOP));
2301    end Fence;
2302
2303    --  Function case
2304
2305    --    +---+     +---+     +---+
2306    --    | E |---->| P |---->| X |---->
2307    --    +---+     +---+     +---+
2308
2309    --  The node numbering of the constituent pattern P is not affected.
2310    --  Where N is the number of nodes in P, the X node is numbered N + 1,
2311    --  and the E node is N + 2.
2312
2313    function Fence (P : Pattern) return Pattern is
2314       Pat : constant PE_Ptr := Copy (P.P);
2315       E   : constant PE_Ptr := new PE'(PC_R_Enter, 0, EOP);
2316       X   : constant PE_Ptr := new PE'(PC_Fence_X, 0, EOP);
2317
2318    begin
2319       return (AFC with P.Stk + 1, Bracket (E, Pat, X));
2320    end Fence;
2321
2322    --------------
2323    -- Finalize --
2324    --------------
2325
2326    procedure Finalize (Object : in out Pattern) is
2327
2328       procedure Free is new Unchecked_Deallocation (PE, PE_Ptr);
2329       procedure Free is new Unchecked_Deallocation (String, String_Ptr);
2330
2331    begin
2332       --  Nothing to do if already freed
2333
2334       if Object.P = null then
2335          return;
2336
2337       --  Otherwise we must free all elements
2338
2339       else
2340          declare
2341             Refs : Ref_Array (1 .. Object.P.Index);
2342             --  References to elements in pattern to be finalized
2343
2344          begin
2345             Build_Ref_Array (Object.P, Refs);
2346
2347             for J in Refs'Range loop
2348                if Refs (J).Pcode = PC_String then
2349                   Free (Refs (J).Str);
2350                end if;
2351
2352                Free (Refs (J));
2353             end loop;
2354
2355             Object.P := null;
2356          end;
2357       end if;
2358    end Finalize;
2359
2360    -----------
2361    -- Image --
2362    -----------
2363
2364    function Image (P : PE_Ptr) return String is
2365    begin
2366       return Image (To_Address (P));
2367    end Image;
2368
2369    function Image (P : Pattern) return String is
2370    begin
2371       return S (Image (P));
2372    end Image;
2373
2374    function Image (P : Pattern) return VString is
2375
2376       Kill_Ampersand : Boolean := False;
2377       --  Set True to delete next & to be output to Result
2378
2379       Result : VString := Nul;
2380       --  The result is accumulated here, using Append
2381
2382       Refs : Ref_Array (1 .. P.P.Index);
2383       --  We build a reference array whose N'th element points to the
2384       --  pattern element whose Index value is N.
2385
2386       procedure Delete_Ampersand;
2387       --  Deletes the ampersand at the end of Result
2388
2389       procedure Image_Seq (E : PE_Ptr; Succ : PE_Ptr; Paren : Boolean);
2390       --  E refers to a pattern structure whose successor is given by Succ.
2391       --  This procedure appends to Result a representation of this pattern.
2392       --  The Paren parameter indicates whether parentheses are required if
2393       --  the output is more than one element.
2394
2395       procedure Image_One (E : in out PE_Ptr);
2396       --  E refers to a pattern structure. This procedure appends to Result
2397       --  a representation of the single simple or compound pattern structure
2398       --  at the start of E and updates E to point to its successor.
2399
2400       ----------------------
2401       -- Delete_Ampersand --
2402       ----------------------
2403
2404       procedure Delete_Ampersand is
2405          L : constant Natural := Length (Result);
2406
2407       begin
2408          if L > 2 then
2409             Delete (Result, L - 1, L);
2410          end if;
2411       end Delete_Ampersand;
2412
2413       ---------------
2414       -- Image_One --
2415       ---------------
2416
2417       procedure Image_One (E : in out PE_Ptr) is
2418
2419          ER : PE_Ptr := E.Pthen;
2420          --  Successor set as result in E unless reset
2421
2422       begin
2423          case E.Pcode is
2424
2425             when PC_Cancel =>
2426                Append (Result, "Cancel");
2427
2428             when PC_Alt => Alt : declare
2429
2430                Elmts_In_L : constant IndexT := E.Pthen.Index - E.Alt.Index;
2431                --  Number of elements in left pattern of alternation.
2432
2433                Lowest_In_L : constant IndexT := E.Index - Elmts_In_L;
2434                --  Number of lowest index in elements of left pattern
2435
2436                E1 : PE_Ptr;
2437
2438             begin
2439                --  The successor of the alternation node must have a lower
2440                --  index than any node that is in the left pattern or a
2441                --  higher index than the alternation node itself.
2442
2443                while ER /= EOP
2444                  and then ER.Index >= Lowest_In_L
2445                  and then ER.Index < E.Index
2446                loop
2447                   ER := ER.Pthen;
2448                end loop;
2449
2450                Append (Result, '(');
2451
2452                E1 := E;
2453                loop
2454                   Image_Seq (E1.Pthen, ER, False);
2455                   Append (Result, " or ");
2456                   E1 := E1.Alt;
2457                   exit when E1.Pcode /= PC_Alt;
2458                end loop;
2459
2460                Image_Seq (E1, ER, False);
2461                Append (Result, ')');
2462             end Alt;
2463
2464             when PC_Any_CS =>
2465                Append (Result, "Any (" & Image (To_Sequence (E.CS)) & ')');
2466
2467             when PC_Any_VF =>
2468                Append (Result, "Any (" & Str_VF (E.VF) & ')');
2469
2470             when PC_Any_VP =>
2471                Append (Result, "Any (" & Str_VP (E.VP) & ')');
2472
2473             when PC_Arb_X =>
2474                Append (Result, "Arb");
2475
2476             when PC_Arbno_S =>
2477                Append (Result, "Arbno (");
2478                Image_Seq (E.Alt, E, False);
2479                Append (Result, ')');
2480
2481             when PC_Arbno_X =>
2482                Append (Result, "Arbno (");
2483                Image_Seq (E.Alt.Pthen, Refs (E.Index - 2), False);
2484                Append (Result, ')');
2485
2486             when PC_Assign_Imm =>
2487                Delete_Ampersand;
2488                Append (Result, "* " & Str_VP (Refs (E.Index - 1).VP));
2489
2490             when PC_Assign_OnM =>
2491                Delete_Ampersand;
2492                Append (Result, "** " & Str_VP (Refs (E.Index - 1).VP));
2493
2494             when PC_Any_CH =>
2495                Append (Result, "Any ('" & E.Char & "')");
2496
2497             when PC_Bal =>
2498                Append (Result, "Bal");
2499
2500             when PC_Break_CH =>
2501                Append (Result, "Break ('" & E.Char & "')");
2502
2503             when PC_Break_CS =>
2504                Append (Result, "Break (" & Image (To_Sequence (E.CS)) & ')');
2505
2506             when PC_Break_VF =>
2507                Append (Result, "Break (" & Str_VF (E.VF) & ')');
2508
2509             when PC_Break_VP =>
2510                Append (Result, "Break (" & Str_VP (E.VP) & ')');
2511
2512             when PC_BreakX_CH =>
2513                Append (Result, "BreakX ('" & E.Char & "')");
2514                ER := ER.Pthen;
2515
2516             when PC_BreakX_CS =>
2517                Append (Result, "BreakX (" & Image (To_Sequence (E.CS)) & ')');
2518                ER := ER.Pthen;
2519
2520             when PC_BreakX_VF =>
2521                Append (Result, "BreakX (" & Str_VF (E.VF) & ')');
2522                ER := ER.Pthen;
2523
2524             when PC_BreakX_VP =>
2525                Append (Result, "BreakX (" & Str_VP (E.VP) & ')');
2526                ER := ER.Pthen;
2527
2528             when PC_Char =>
2529                Append (Result, ''' & E.Char & ''');
2530
2531             when PC_Fail =>
2532                Append (Result, "Fail");
2533
2534             when PC_Fence =>
2535                Append (Result, "Fence");
2536
2537             when PC_Fence_X =>
2538                Append (Result, "Fence (");
2539                Image_Seq (E.Pthen, Refs (E.Index - 1), False);
2540                Append (Result, ")");
2541                ER := Refs (E.Index - 1).Pthen;
2542
2543             when PC_Len_Nat =>
2544                Append (Result, "Len (" & E.Nat & ')');
2545
2546             when PC_Len_NF =>
2547                Append (Result, "Len (" & Str_NF (E.NF) & ')');
2548
2549             when PC_Len_NP =>
2550                Append (Result, "Len (" & Str_NP (E.NP) & ')');
2551
2552             when PC_NotAny_CH =>
2553                Append (Result, "NotAny ('" & E.Char & "')");
2554
2555             when PC_NotAny_CS =>
2556                Append (Result, "NotAny (" & Image (To_Sequence (E.CS)) & ')');
2557
2558             when PC_NotAny_VF =>
2559                Append (Result, "NotAny (" & Str_VF (E.VF) & ')');
2560
2561             when PC_NotAny_VP =>
2562                Append (Result, "NotAny (" & Str_VP (E.VP) & ')');
2563
2564             when PC_NSpan_CH =>
2565                Append (Result, "NSpan ('" & E.Char & "')");
2566
2567             when PC_NSpan_CS =>
2568                Append (Result, "NSpan (" & Image (To_Sequence (E.CS)) & ')');
2569
2570             when PC_NSpan_VF =>
2571                Append (Result, "NSpan (" & Str_VF (E.VF) & ')');
2572
2573             when PC_NSpan_VP =>
2574                Append (Result, "NSpan (" & Str_VP (E.VP) & ')');
2575
2576             when PC_Null =>
2577                Append (Result, """""");
2578
2579             when PC_Pos_Nat =>
2580                Append (Result, "Pos (" & E.Nat & ')');
2581
2582             when PC_Pos_NF =>
2583                Append (Result, "Pos (" & Str_NF (E.NF) & ')');
2584
2585             when PC_Pos_NP =>
2586                Append (Result, "Pos (" & Str_NP (E.NP) & ')');
2587
2588             when PC_R_Enter =>
2589                Kill_Ampersand := True;
2590
2591             when PC_Rest =>
2592                Append (Result, "Rest");
2593
2594             when PC_Rpat =>
2595                Append (Result, "(+ " & Str_PP (E.PP) & ')');
2596
2597             when PC_Pred_Func =>
2598                Append (Result, "(+ " & Str_BF (E.BF) & ')');
2599
2600             when PC_RPos_Nat =>
2601                Append (Result, "RPos (" & E.Nat & ')');
2602
2603             when PC_RPos_NF =>
2604                Append (Result, "RPos (" & Str_NF (E.NF) & ')');
2605
2606             when PC_RPos_NP =>
2607                Append (Result, "RPos (" & Str_NP (E.NP) & ')');
2608
2609             when PC_RTab_Nat =>
2610                Append (Result, "RTab (" & E.Nat & ')');
2611
2612             when PC_RTab_NF =>
2613                Append (Result, "RTab (" & Str_NF (E.NF) & ')');
2614
2615             when PC_RTab_NP =>
2616                Append (Result, "RTab (" & Str_NP (E.NP) & ')');
2617
2618             when PC_Setcur =>
2619                Append (Result, "Setcur (" & Str_NP (E.Var) & ')');
2620
2621             when PC_Span_CH =>
2622                Append (Result, "Span ('" & E.Char & "')");
2623
2624             when PC_Span_CS =>
2625                Append (Result, "Span (" & Image (To_Sequence (E.CS)) & ')');
2626
2627             when PC_Span_VF =>
2628                Append (Result, "Span (" & Str_VF (E.VF) & ')');
2629
2630             when PC_Span_VP =>
2631                Append (Result, "Span (" & Str_VP (E.VP) & ')');
2632
2633             when PC_String =>
2634                Append (Result, Image (E.Str.all));
2635
2636             when PC_String_2 =>
2637                Append (Result, Image (E.Str2));
2638
2639             when PC_String_3 =>
2640                Append (Result, Image (E.Str3));
2641
2642             when PC_String_4 =>
2643                Append (Result, Image (E.Str4));
2644
2645             when PC_String_5 =>
2646                Append (Result, Image (E.Str5));
2647
2648             when PC_String_6 =>
2649                Append (Result, Image (E.Str6));
2650
2651             when PC_String_VF =>
2652                Append (Result, "(+" &  Str_VF (E.VF) & ')');
2653
2654             when PC_String_VP =>
2655                Append (Result, "(+" & Str_VP (E.VP) & ')');
2656
2657             when PC_Succeed =>
2658                Append (Result, "Succeed");
2659
2660             when PC_Tab_Nat =>
2661                Append (Result, "Tab (" & E.Nat & ')');
2662
2663             when PC_Tab_NF =>
2664                Append (Result, "Tab (" & Str_NF (E.NF) & ')');
2665
2666             when PC_Tab_NP =>
2667                Append (Result, "Tab (" & Str_NP (E.NP) & ')');
2668
2669             when PC_Write_Imm =>
2670                Append (Result, '(');
2671                Image_Seq (E, Refs (E.Index - 1), True);
2672                Append (Result, " * " & Str_FP (Refs (E.Index - 1).FP));
2673                ER := Refs (E.Index - 1).Pthen;
2674
2675             when PC_Write_OnM =>
2676                Append (Result, '(');
2677                Image_Seq (E.Pthen, Refs (E.Index - 1), True);
2678                Append (Result, " ** " & Str_FP (Refs (E.Index - 1).FP));
2679                ER := Refs (E.Index - 1).Pthen;
2680
2681             --  Other pattern codes should not appear as leading elements
2682
2683             when PC_Arb_Y      |
2684                  PC_Arbno_Y    |
2685                  PC_Assign     |
2686                  PC_BreakX_X   |
2687                  PC_EOP        |
2688                  PC_Fence_Y    |
2689                  PC_R_Remove   |
2690                  PC_R_Restore  |
2691                  PC_Unanchored =>
2692                Append (Result, "???");
2693
2694          end case;
2695
2696          E := ER;
2697       end Image_One;
2698
2699       ---------------
2700       -- Image_Seq --
2701       ---------------
2702
2703       procedure Image_Seq (E : PE_Ptr; Succ : PE_Ptr; Paren : Boolean) is
2704          Indx : constant Natural := Length (Result);
2705          E1   : PE_Ptr  := E;
2706          Mult : Boolean := False;
2707
2708       begin
2709          --  The image of EOP is "" (the null string)
2710
2711          if E = EOP then
2712             Append (Result, """""");
2713
2714          --  Else generate appropriate concatenation sequence
2715
2716          else
2717             loop
2718                Image_One (E1);
2719                exit when E1 = Succ;
2720                exit when E1 = EOP;
2721                Mult := True;
2722
2723                if Kill_Ampersand then
2724                   Kill_Ampersand := False;
2725                else
2726                   Append (Result, " & ");
2727                end if;
2728             end loop;
2729          end if;
2730
2731          if Mult and Paren then
2732             Insert (Result, Indx + 1, "(");
2733             Append (Result, ")");
2734          end if;
2735       end Image_Seq;
2736
2737    --  Start of processing for Image
2738
2739    begin
2740       Build_Ref_Array (P.P, Refs);
2741       Image_Seq (P.P, EOP, False);
2742       return Result;
2743    end Image;
2744
2745    -----------
2746    -- Is_In --
2747    -----------
2748
2749    function Is_In (C : Character; Str : String) return Boolean is
2750    begin
2751       for J in Str'Range loop
2752          if Str (J) = C then
2753             return True;
2754          end if;
2755       end loop;
2756
2757       return False;
2758    end Is_In;
2759
2760    ---------
2761    -- Len --
2762    ---------
2763
2764    function Len (Count : Natural) return Pattern is
2765    begin
2766       --  Note, the following is not just an optimization, it is needed
2767       --  to ensure that Arbno (Len (0)) does not generate an infinite
2768       --  matching loop (since PC_Len_Nat is OK_For_Simple_Arbno).
2769
2770       if Count = 0 then
2771          return (AFC with 0, new PE'(PC_Null, 1, EOP));
2772
2773       else
2774          return (AFC with 0, new PE'(PC_Len_Nat, 1, EOP, Count));
2775       end if;
2776    end Len;
2777
2778    function Len (Count : Natural_Func) return Pattern is
2779    begin
2780       return (AFC with 0, new PE'(PC_Len_NF, 1, EOP, Count));
2781    end Len;
2782
2783    function Len (Count : access Natural) return Pattern is
2784    begin
2785       return (AFC with 0, new PE'(PC_Len_NP, 1, EOP, Natural_Ptr (Count)));
2786    end Len;
2787
2788    -----------------
2789    -- Logic_Error --
2790    -----------------
2791
2792    procedure Logic_Error is
2793    begin
2794       Raise_Exception
2795         (Program_Error'Identity,
2796          "Internal logic error in GNAT.Spitbol.Patterns");
2797    end Logic_Error;
2798
2799    -----------
2800    -- Match --
2801    -----------
2802
2803    function Match
2804      (Subject : VString;
2805       Pat     : Pattern) return Boolean
2806    is
2807       Start : Natural;
2808       Stop  : Natural;
2809       S     : String_Access;
2810       L     : Natural;
2811
2812    begin
2813       Get_String (Subject, S, L);
2814
2815       if Debug_Mode then
2816          XMatchD (S (1 .. L), Pat.P, Pat.Stk, Start, Stop);
2817       else
2818          XMatch  (S (1 .. L), Pat.P, Pat.Stk, Start, Stop);
2819       end if;
2820
2821       return Start /= 0;
2822    end Match;
2823
2824    function Match
2825      (Subject : String;
2826       Pat     : Pattern) return Boolean
2827    is
2828       Start, Stop : Natural;
2829       subtype String1 is String (1 .. Subject'Length);
2830
2831    begin
2832       if Debug_Mode then
2833          XMatchD (String1 (Subject), Pat.P, Pat.Stk, Start, Stop);
2834       else
2835          XMatch  (String1 (Subject), Pat.P, Pat.Stk, Start, Stop);
2836       end if;
2837
2838       return Start /= 0;
2839    end Match;
2840
2841    function Match
2842      (Subject : VString_Var;
2843       Pat     : Pattern;
2844       Replace : VString) return Boolean
2845    is
2846       Start : Natural;
2847       Stop  : Natural;
2848       S     : String_Access;
2849       L     : Natural;
2850
2851    begin
2852       Get_String (Subject, S, L);
2853
2854       if Debug_Mode then
2855          XMatchD (S (1 .. L), Pat.P, Pat.Stk, Start, Stop);
2856       else
2857          XMatch  (S (1 .. L), Pat.P, Pat.Stk, Start, Stop);
2858       end if;
2859
2860       if Start = 0 then
2861          return False;
2862       else
2863          Get_String (Replace, S, L);
2864          Replace_Slice
2865            (Subject'Unrestricted_Access.all, Start, Stop, S (1 .. L));
2866          return True;
2867       end if;
2868    end Match;
2869
2870    function Match
2871      (Subject : VString_Var;
2872       Pat     : Pattern;
2873       Replace : String) return Boolean
2874    is
2875       Start : Natural;
2876       Stop  : Natural;
2877       S     : String_Access;
2878       L     : Natural;
2879
2880    begin
2881       Get_String (Subject, S, L);
2882
2883       if Debug_Mode then
2884          XMatchD (S (1 .. L), Pat.P, Pat.Stk, Start, Stop);
2885       else
2886          XMatch  (S (1 .. L), Pat.P, Pat.Stk, Start, Stop);
2887       end if;
2888
2889       if Start = 0 then
2890          return False;
2891       else
2892          Replace_Slice
2893            (Subject'Unrestricted_Access.all, Start, Stop, Replace);
2894          return True;
2895       end if;
2896    end Match;
2897
2898    procedure Match
2899      (Subject : VString;
2900       Pat     : Pattern)
2901    is
2902       Start : Natural;
2903       Stop  : Natural;
2904       S     : String_Access;
2905       L     : Natural;
2906
2907    begin
2908       Get_String (Subject, S, L);
2909
2910       if Debug_Mode then
2911          XMatchD (S (1 .. L), Pat.P, Pat.Stk, Start, Stop);
2912       else
2913          XMatch  (S (1 .. L), Pat.P, Pat.Stk, Start, Stop);
2914       end if;
2915    end Match;
2916
2917    procedure Match
2918      (Subject : String;
2919       Pat     : Pattern)
2920    is
2921       Start, Stop : Natural;
2922       subtype String1 is String (1 .. Subject'Length);
2923    begin
2924       if Debug_Mode then
2925          XMatchD (String1 (Subject), Pat.P, Pat.Stk, Start, Stop);
2926       else
2927          XMatch  (String1 (Subject), Pat.P, Pat.Stk, Start, Stop);
2928       end if;
2929    end Match;
2930
2931    procedure Match
2932      (Subject : in out VString;
2933       Pat     : Pattern;
2934       Replace : VString)
2935    is
2936       Start : Natural;
2937       Stop  : Natural;
2938       S     : String_Access;
2939       L     : Natural;
2940
2941    begin
2942       Get_String (Subject, S, L);
2943
2944       if Debug_Mode then
2945          XMatchD (S (1 .. L), Pat.P, Pat.Stk, Start, Stop);
2946       else
2947          XMatch  (S (1 .. L), Pat.P, Pat.Stk, Start, Stop);
2948       end if;
2949
2950       if Start /= 0 then
2951          Get_String (Replace, S, L);
2952          Replace_Slice (Subject, Start, Stop, S (1 .. L));
2953       end if;
2954    end Match;
2955
2956    procedure Match
2957      (Subject : in out VString;
2958       Pat     : Pattern;
2959       Replace : String)
2960    is
2961       Start : Natural;
2962       Stop  : Natural;
2963       S     : String_Access;
2964       L     : Natural;
2965
2966    begin
2967       Get_String (Subject, S, L);
2968
2969       if Debug_Mode then
2970          XMatchD (S (1 .. L), Pat.P, Pat.Stk, Start, Stop);
2971       else
2972          XMatch  (S (1 .. L), Pat.P, Pat.Stk, Start, Stop);
2973       end if;
2974
2975       if Start /= 0 then
2976          Replace_Slice (Subject, Start, Stop, Replace);
2977       end if;
2978    end Match;
2979
2980    function Match
2981      (Subject : VString;
2982       Pat     : PString) return Boolean
2983    is
2984       Pat_Len : constant Natural := Pat'Length;
2985       S       : String_Access;
2986       L       : Natural;
2987
2988    begin
2989       Get_String (Subject, S, L);
2990
2991       if Anchored_Mode then
2992          if Pat_Len > L then
2993             return False;
2994          else
2995             return Pat = S (1 .. Pat_Len);
2996          end if;
2997
2998       else
2999          for J in 1 .. L - Pat_Len + 1 loop
3000             if Pat = S (J .. J + (Pat_Len - 1)) then
3001                return True;
3002             end if;
3003          end loop;
3004
3005          return False;
3006       end if;
3007    end Match;
3008
3009    function Match
3010      (Subject : String;
3011       Pat     : PString) return Boolean
3012    is
3013       Pat_Len : constant Natural := Pat'Length;
3014       Sub_Len : constant Natural := Subject'Length;
3015       SFirst  : constant Natural := Subject'First;
3016
3017    begin
3018       if Anchored_Mode then
3019          if Pat_Len > Sub_Len then
3020             return False;
3021          else
3022             return Pat = Subject (SFirst .. SFirst + Pat_Len - 1);
3023          end if;
3024
3025       else
3026          for J in SFirst .. SFirst + Sub_Len - Pat_Len loop
3027             if Pat = Subject (J .. J + (Pat_Len - 1)) then
3028                return True;
3029             end if;
3030          end loop;
3031
3032          return False;
3033       end if;
3034    end Match;
3035
3036    function Match
3037      (Subject : VString_Var;
3038       Pat     : PString;
3039       Replace : VString) return Boolean
3040    is
3041       Start : Natural;
3042       Stop  : Natural;
3043       S     : String_Access;
3044       L     : Natural;
3045
3046    begin
3047       Get_String (Subject, S, L);
3048
3049       if Debug_Mode then
3050          XMatchD (S (1 .. L), S_To_PE (Pat), 0, Start, Stop);
3051       else
3052          XMatch  (S (1 .. L), S_To_PE (Pat), 0, Start, Stop);
3053       end if;
3054
3055       if Start = 0 then
3056          return False;
3057       else
3058          Get_String (Replace, S, L);
3059          Replace_Slice
3060            (Subject'Unrestricted_Access.all, Start, Stop, S (1 .. L));
3061          return True;
3062       end if;
3063    end Match;
3064
3065    function Match
3066      (Subject : VString_Var;
3067       Pat     : PString;
3068       Replace : String) return Boolean
3069    is
3070       Start : Natural;
3071       Stop  : Natural;
3072       S     : String_Access;
3073       L     : Natural;
3074
3075    begin
3076       Get_String (Subject, S, L);
3077
3078       if Debug_Mode then
3079          XMatchD (S (1 .. L), S_To_PE (Pat), 0, Start, Stop);
3080       else
3081          XMatch  (S (1 .. L), S_To_PE (Pat), 0, Start, Stop);
3082       end if;
3083
3084       if Start = 0 then
3085          return False;
3086       else
3087          Replace_Slice
3088            (Subject'Unrestricted_Access.all, Start, Stop, Replace);
3089          return True;
3090       end if;
3091    end Match;
3092
3093    procedure Match
3094      (Subject : VString;
3095       Pat     : PString)
3096    is
3097       Start : Natural;
3098       Stop  : Natural;
3099       S     : String_Access;
3100       L     : Natural;
3101
3102    begin
3103       Get_String (Subject, S, L);
3104
3105       if Debug_Mode then
3106          XMatchD (S (1 .. L), S_To_PE (Pat), 0, Start, Stop);
3107       else
3108          XMatch  (S (1 .. L), S_To_PE (Pat), 0, Start, Stop);
3109       end if;
3110    end Match;
3111
3112    procedure Match
3113      (Subject : String;
3114       Pat     : PString)
3115    is
3116       Start, Stop : Natural;
3117       subtype String1 is String (1 .. Subject'Length);
3118
3119    begin
3120       if Debug_Mode then
3121          XMatchD (String1 (Subject), S_To_PE (Pat), 0, Start, Stop);
3122       else
3123          XMatch  (String1 (Subject), S_To_PE (Pat), 0, Start, Stop);
3124       end if;
3125    end Match;
3126
3127    procedure Match
3128      (Subject : in out VString;
3129       Pat     : PString;
3130       Replace : VString)
3131    is
3132       Start : Natural;
3133       Stop  : Natural;
3134       S     : String_Access;
3135       L     : Natural;
3136
3137    begin
3138       Get_String (Subject, S, L);
3139
3140       if Debug_Mode then
3141          XMatchD (S (1 .. L), S_To_PE (Pat), 0, Start, Stop);
3142       else
3143          XMatch  (S (1 .. L), S_To_PE (Pat), 0, Start, Stop);
3144       end if;
3145
3146       if Start /= 0 then
3147          Get_String (Replace, S, L);
3148          Replace_Slice (Subject, Start, Stop, S (1 .. L));
3149       end if;
3150    end Match;
3151
3152    procedure Match
3153      (Subject : in out VString;
3154       Pat     : PString;
3155       Replace : String)
3156    is
3157       Start : Natural;
3158       Stop  : Natural;
3159       S     : String_Access;
3160       L     : Natural;
3161
3162    begin
3163       Get_String (Subject, S, L);
3164
3165       if Debug_Mode then
3166          XMatchD (S (1 .. L), S_To_PE (Pat), 0, Start, Stop);
3167       else
3168          XMatch  (S (1 .. L), S_To_PE (Pat), 0, Start, Stop);
3169       end if;
3170
3171       if Start /= 0 then
3172          Replace_Slice (Subject, Start, Stop, Replace);
3173       end if;
3174    end Match;
3175
3176    function Match
3177      (Subject : VString_Var;
3178       Pat     : Pattern;
3179       Result  : Match_Result_Var) return Boolean
3180    is
3181       Start : Natural;
3182       Stop  : Natural;
3183       S     : String_Access;
3184       L     : Natural;
3185
3186    begin
3187       Get_String (Subject, S, L);
3188
3189       if Debug_Mode then
3190          XMatchD (S (1 .. L), Pat.P, Pat.Stk, Start, Stop);
3191       else
3192          XMatch  (S (1 .. L), Pat.P, Pat.Stk, Start, Stop);
3193       end if;
3194
3195       if Start = 0 then
3196          Result'Unrestricted_Access.all.Var := null;
3197          return False;
3198
3199       else
3200          Result'Unrestricted_Access.all.Var   := Subject'Unrestricted_Access;
3201          Result'Unrestricted_Access.all.Start := Start;
3202          Result'Unrestricted_Access.all.Stop  := Stop;
3203          return True;
3204       end if;
3205    end Match;
3206
3207    procedure Match
3208      (Subject : in out VString;
3209       Pat     : Pattern;
3210       Result  : out Match_Result)
3211    is
3212       Start : Natural;
3213       Stop  : Natural;
3214       S     : String_Access;
3215       L     : Natural;
3216
3217    begin
3218       Get_String (Subject, S, L);
3219
3220       if Debug_Mode then
3221          XMatchD (S (1 .. L), Pat.P, Pat.Stk, Start, Stop);
3222       else
3223          XMatch  (S (1 .. L), Pat.P, Pat.Stk, Start, Stop);
3224       end if;
3225
3226       if Start = 0 then
3227          Result.Var := null;
3228       else
3229          Result.Var   := Subject'Unrestricted_Access;
3230          Result.Start := Start;
3231          Result.Stop  := Stop;
3232       end if;
3233    end Match;
3234
3235    ---------------
3236    -- New_LineD --
3237    ---------------
3238
3239    procedure New_LineD is
3240    begin
3241       if Internal_Debug then
3242          New_Line;
3243       end if;
3244    end New_LineD;
3245
3246    ------------
3247    -- NotAny --
3248    ------------
3249
3250    function NotAny (Str : String) return Pattern is
3251    begin
3252       return (AFC with 0, new PE'(PC_NotAny_CS, 1, EOP, To_Set (Str)));
3253    end NotAny;
3254
3255    function NotAny (Str : VString) return Pattern is
3256    begin
3257       return NotAny (S (Str));
3258    end NotAny;
3259
3260    function NotAny (Str : Character) return Pattern is
3261    begin
3262       return (AFC with 0, new PE'(PC_NotAny_CH, 1, EOP, Str));
3263    end NotAny;
3264
3265    function NotAny (Str : Character_Set) return Pattern is
3266    begin
3267       return (AFC with 0, new PE'(PC_NotAny_CS, 1, EOP, Str));
3268    end NotAny;
3269
3270    function NotAny (Str : access VString) return Pattern is
3271    begin
3272       return (AFC with 0, new PE'(PC_NotAny_VP, 1, EOP, VString_Ptr (Str)));
3273    end NotAny;
3274
3275    function NotAny (Str : VString_Func) return Pattern is
3276    begin
3277       return (AFC with 0, new PE'(PC_NotAny_VF, 1, EOP, Str));
3278    end NotAny;
3279
3280    -----------
3281    -- NSpan --
3282    -----------
3283
3284    function NSpan (Str : String) return Pattern is
3285    begin
3286       return (AFC with 0, new PE'(PC_NSpan_CS, 1, EOP, To_Set (Str)));
3287    end NSpan;
3288
3289    function NSpan (Str : VString) return Pattern is
3290    begin
3291       return NSpan (S (Str));
3292    end NSpan;
3293
3294    function NSpan (Str : Character) return Pattern is
3295    begin
3296       return (AFC with 0, new PE'(PC_NSpan_CH, 1, EOP, Str));
3297    end NSpan;
3298
3299    function NSpan (Str : Character_Set) return Pattern is
3300    begin
3301       return (AFC with 0, new PE'(PC_NSpan_CS, 1, EOP, Str));
3302    end NSpan;
3303
3304    function NSpan (Str : access VString) return Pattern is
3305    begin
3306       return (AFC with 0, new PE'(PC_NSpan_VP, 1, EOP, VString_Ptr (Str)));
3307    end NSpan;
3308
3309    function NSpan (Str : VString_Func) return Pattern is
3310    begin
3311       return (AFC with 0, new PE'(PC_NSpan_VF, 1, EOP, Str));
3312    end NSpan;
3313
3314    ---------
3315    -- Pos --
3316    ---------
3317
3318    function Pos (Count : Natural) return Pattern is
3319    begin
3320       return (AFC with 0, new PE'(PC_Pos_Nat, 1, EOP, Count));
3321    end Pos;
3322
3323    function Pos (Count : Natural_Func) return Pattern is
3324    begin
3325       return (AFC with 0, new PE'(PC_Pos_NF, 1, EOP, Count));
3326    end Pos;
3327
3328    function Pos (Count : access Natural) return Pattern is
3329    begin
3330       return (AFC with 0, new PE'(PC_Pos_NP, 1, EOP, Natural_Ptr (Count)));
3331    end Pos;
3332
3333    ----------
3334    -- PutD --
3335    ----------
3336
3337    procedure PutD (Str : String) is
3338    begin
3339       if Internal_Debug then
3340          Put (Str);
3341       end if;
3342    end PutD;
3343
3344    ---------------
3345    -- Put_LineD --
3346    ---------------
3347
3348    procedure Put_LineD (Str : String) is
3349    begin
3350       if Internal_Debug then
3351          Put_Line (Str);
3352       end if;
3353    end Put_LineD;
3354
3355    -------------
3356    -- Replace --
3357    -------------
3358
3359    procedure Replace
3360      (Result  : in out Match_Result;
3361       Replace : VString)
3362    is
3363       S : String_Access;
3364       L : Natural;
3365
3366    begin
3367       Get_String (Replace, S, L);
3368
3369       if Result.Var /= null then
3370          Replace_Slice (Result.Var.all, Result.Start, Result.Stop, S (1 .. L));
3371          Result.Var := null;
3372       end if;
3373    end Replace;
3374
3375    ----------
3376    -- Rest --
3377    ----------
3378
3379    function Rest return Pattern is
3380    begin
3381       return (AFC with 0, new PE'(PC_Rest, 1, EOP));
3382    end Rest;
3383
3384    ----------
3385    -- Rpos --
3386    ----------
3387
3388    function Rpos (Count : Natural) return Pattern is
3389    begin
3390       return (AFC with 0, new PE'(PC_RPos_Nat, 1, EOP, Count));
3391    end Rpos;
3392
3393    function Rpos (Count : Natural_Func) return Pattern is
3394    begin
3395       return (AFC with 0, new PE'(PC_RPos_NF, 1, EOP, Count));
3396    end Rpos;
3397
3398    function Rpos (Count : access Natural) return Pattern is
3399    begin
3400       return (AFC with 0, new PE'(PC_RPos_NP, 1, EOP, Natural_Ptr (Count)));
3401    end Rpos;
3402
3403    ----------
3404    -- Rtab --
3405    ----------
3406
3407    function Rtab (Count : Natural) return Pattern is
3408    begin
3409       return (AFC with 0, new PE'(PC_RTab_Nat, 1, EOP, Count));
3410    end Rtab;
3411
3412    function Rtab (Count : Natural_Func) return Pattern is
3413    begin
3414       return (AFC with 0, new PE'(PC_RTab_NF, 1, EOP, Count));
3415    end Rtab;
3416
3417    function Rtab (Count : access Natural) return Pattern is
3418    begin
3419       return (AFC with 0, new PE'(PC_RTab_NP, 1, EOP, Natural_Ptr (Count)));
3420    end Rtab;
3421
3422    -------------
3423    -- S_To_PE --
3424    -------------
3425
3426    function S_To_PE (Str : PString) return PE_Ptr is
3427       Len : constant Natural := Str'Length;
3428
3429    begin
3430       case Len is
3431          when 0 =>
3432             return new PE'(PC_Null,     1, EOP);
3433
3434          when 1 =>
3435             return new PE'(PC_Char,     1, EOP, Str (1));
3436
3437          when 2 =>
3438             return new PE'(PC_String_2, 1, EOP, Str);
3439
3440          when 3 =>
3441             return new PE'(PC_String_3, 1, EOP, Str);
3442
3443          when 4 =>
3444             return new PE'(PC_String_4, 1, EOP, Str);
3445
3446          when 5 =>
3447             return new PE'(PC_String_5, 1, EOP, Str);
3448
3449          when 6 =>
3450             return new PE'(PC_String_6, 1, EOP, Str);
3451
3452          when others =>
3453             return new PE'(PC_String, 1, EOP, new String'(Str));
3454
3455       end case;
3456    end S_To_PE;
3457
3458    -------------------
3459    -- Set_Successor --
3460    -------------------
3461
3462    --  Note: this procedure is not used by the normal concatenation circuit,
3463    --  since other fixups are required on the left operand in this case, and
3464    --  they might as well be done all together.
3465
3466    procedure Set_Successor (Pat : PE_Ptr; Succ : PE_Ptr) is
3467    begin
3468       if Pat = null then
3469          Uninitialized_Pattern;
3470
3471       elsif Pat = EOP then
3472          Logic_Error;
3473
3474       else
3475          declare
3476             Refs : Ref_Array (1 .. Pat.Index);
3477             --  We build a reference array for L whose N'th element points to
3478             --  the pattern element of L whose original Index value is N.
3479
3480             P : PE_Ptr;
3481
3482          begin
3483             Build_Ref_Array (Pat, Refs);
3484
3485             for J in Refs'Range loop
3486                P := Refs (J);
3487
3488                if P.Pthen = EOP then
3489                   P.Pthen := Succ;
3490                end if;
3491
3492                if P.Pcode in PC_Has_Alt and then P.Alt = EOP then
3493                   P.Alt := Succ;
3494                end if;
3495             end loop;
3496          end;
3497       end if;
3498    end Set_Successor;
3499
3500    ------------
3501    -- Setcur --
3502    ------------
3503
3504    function Setcur (Var : access Natural) return Pattern is
3505    begin
3506       return (AFC with 0, new PE'(PC_Setcur, 1, EOP, Natural_Ptr (Var)));
3507    end Setcur;
3508
3509    ----------
3510    -- Span --
3511    ----------
3512
3513    function Span (Str : String) return Pattern is
3514    begin
3515       return (AFC with 0, new PE'(PC_Span_CS, 1, EOP, To_Set (Str)));
3516    end Span;
3517
3518    function Span (Str : VString) return Pattern is
3519    begin
3520       return Span (S (Str));
3521    end Span;
3522
3523    function Span (Str : Character) return Pattern is
3524    begin
3525       return (AFC with 0, new PE'(PC_Span_CH, 1, EOP, Str));
3526    end Span;
3527
3528    function Span (Str : Character_Set) return Pattern is
3529    begin
3530       return (AFC with 0, new PE'(PC_Span_CS, 1, EOP, Str));
3531    end Span;
3532
3533    function Span (Str : access VString) return Pattern is
3534    begin
3535       return (AFC with 0, new PE'(PC_Span_VP, 1, EOP, VString_Ptr (Str)));
3536    end Span;
3537
3538    function Span (Str : VString_Func) return Pattern is
3539    begin
3540       return (AFC with 0, new PE'(PC_Span_VF, 1, EOP, Str));
3541    end Span;
3542
3543    ------------
3544    -- Str_BF --
3545    ------------
3546
3547    function Str_BF (A : Boolean_Func) return String is
3548       function To_A is new Unchecked_Conversion (Boolean_Func, Address);
3549    begin
3550       return "BF(" & Image (To_A (A)) & ')';
3551    end Str_BF;
3552
3553    ------------
3554    -- Str_FP --
3555    ------------
3556
3557    function Str_FP (A : File_Ptr) return String is
3558    begin
3559       return "FP(" & Image (A.all'Address) & ')';
3560    end Str_FP;
3561
3562    ------------
3563    -- Str_NF --
3564    ------------
3565
3566    function Str_NF (A : Natural_Func) return String is
3567       function To_A is new Unchecked_Conversion (Natural_Func, Address);
3568    begin
3569       return "NF(" & Image (To_A (A)) & ')';
3570    end Str_NF;
3571
3572    ------------
3573    -- Str_NP --
3574    ------------
3575
3576    function Str_NP (A : Natural_Ptr) return String is
3577    begin
3578       return "NP(" & Image (A.all'Address) & ')';
3579    end Str_NP;
3580
3581    ------------
3582    -- Str_PP --
3583    ------------
3584
3585    function Str_PP (A : Pattern_Ptr) return String is
3586    begin
3587       return "PP(" & Image (A.all'Address) & ')';
3588    end Str_PP;
3589
3590    ------------
3591    -- Str_VF --
3592    ------------
3593
3594    function Str_VF (A : VString_Func) return String is
3595       function To_A is new Unchecked_Conversion (VString_Func, Address);
3596    begin
3597       return "VF(" & Image (To_A (A)) & ')';
3598    end Str_VF;
3599
3600    ------------
3601    -- Str_VP --
3602    ------------
3603
3604    function Str_VP (A : VString_Ptr) return String is
3605    begin
3606       return "VP(" & Image (A.all'Address) & ')';
3607    end Str_VP;
3608
3609    -------------
3610    -- Succeed --
3611    -------------
3612
3613    function Succeed return Pattern is
3614    begin
3615       return (AFC with 1, new PE'(PC_Succeed, 1, EOP));
3616    end Succeed;
3617
3618    ---------
3619    -- Tab --
3620    ---------
3621
3622    function Tab (Count : Natural) return Pattern is
3623    begin
3624       return (AFC with 0, new PE'(PC_Tab_Nat, 1, EOP, Count));
3625    end Tab;
3626
3627    function Tab (Count : Natural_Func) return Pattern is
3628    begin
3629       return (AFC with 0, new PE'(PC_Tab_NF, 1, EOP, Count));
3630    end Tab;
3631
3632    function Tab (Count : access Natural) return Pattern is
3633    begin
3634       return (AFC with 0, new PE'(PC_Tab_NP, 1, EOP, Natural_Ptr (Count)));
3635    end Tab;
3636
3637    ---------------------------
3638    -- Uninitialized_Pattern --
3639    ---------------------------
3640
3641    procedure Uninitialized_Pattern is
3642    begin
3643       Raise_Exception
3644         (Program_Error'Identity,
3645          "uninitialized value of type GNAT.Spitbol.Patterns.Pattern");
3646    end Uninitialized_Pattern;
3647
3648    ------------
3649    -- XMatch --
3650    ------------
3651
3652    procedure XMatch
3653      (Subject : String;
3654       Pat_P   : PE_Ptr;
3655       Pat_S   : Natural;
3656       Start   : out Natural;
3657       Stop    : out Natural)
3658    is
3659       Node : PE_Ptr;
3660       --  Pointer to current pattern node. Initialized from Pat_P, and then
3661       --  updated as the match proceeds through its constituent elements.
3662
3663       Length : constant Natural := Subject'Length;
3664       --  Length of string (= Subject'Last, since Subject'First is always 1)
3665
3666       Cursor : Integer := 0;
3667       --  If the value is non-negative, then this value is the index showing
3668       --  the current position of the match in the subject string. The next
3669       --  character to be matched is at Subject (Cursor + 1). Note that since
3670       --  our view of the subject string in XMatch always has a lower bound
3671       --  of one, regardless of original bounds, that this definition exactly
3672       --  corresponds to the cursor value as referenced by functions like Pos.
3673       --
3674       --  If the value is negative, then this is a saved stack pointer,
3675       --  typically a base pointer of an inner or outer region. Cursor
3676       --  temporarily holds such a value when it is popped from the stack
3677       --  by Fail. In all cases, Cursor is reset to a proper non-negative
3678       --  cursor value before the match proceeds (e.g. by propagating the
3679       --  failure and popping a "real" cursor value from the stack.
3680
3681       PE_Unanchored : aliased PE := (PC_Unanchored, 0, Pat_P);
3682       --  Dummy pattern element used in the unanchored case.
3683
3684       Stack : Stack_Type;
3685       --  The pattern matching failure stack for this call to Match
3686
3687       Stack_Ptr : Stack_Range;
3688       --  Current stack pointer. This points to the top element of the stack
3689       --  that is currently in use. At the outer level this is the special
3690       --  entry placed on the stack according to the anchor mode.
3691
3692       Stack_Init : constant Stack_Range := Stack'First + 1;
3693       --  This is the initial value of the Stack_Ptr and Stack_Base. The
3694       --  initial (Stack'First) element of the stack is not used so that
3695       --  when we pop the last element off, Stack_Ptr is still in range.
3696
3697       Stack_Base : Stack_Range;
3698       --  This value is the stack base value, i.e. the stack pointer for the
3699       --  first history stack entry in the current stack region. See separate
3700       --  section on handling of recursive pattern matches.
3701
3702       Assign_OnM : Boolean := False;
3703       --  Set True if assign-on-match or write-on-match operations may be
3704       --  present in the history stack, which must then be scanned on a
3705       --  successful match.
3706
3707       procedure Pop_Region;
3708       pragma Inline (Pop_Region);
3709       --  Used at the end of processing of an inner region. if the inner
3710       --  region left no stack entries, then all trace of it is removed.
3711       --  Otherwise a PC_Restore_Region entry is pushed to ensure proper
3712       --  handling of alternatives in the inner region.
3713
3714       procedure Push (Node : PE_Ptr);
3715       pragma Inline (Push);
3716       --  Make entry in pattern matching stack with current cursor valeu
3717
3718       procedure Push_Region;
3719       pragma Inline (Push_Region);
3720       --  This procedure makes a new region on the history stack. The
3721       --  caller first establishes the special entry on the stack, but
3722       --  does not push the stack pointer. Then this call stacks a
3723       --  PC_Remove_Region node, on top of this entry, using the cursor
3724       --  field of the PC_Remove_Region entry to save the outer level
3725       --  stack base value, and resets the stack base to point to this
3726       --  PC_Remove_Region node.
3727
3728       ----------------
3729       -- Pop_Region --
3730       ----------------
3731
3732       procedure Pop_Region is
3733       begin
3734          --  If nothing was pushed in the inner region, we can just get
3735          --  rid of it entirely, leaving no traces that it was ever there
3736
3737          if Stack_Ptr = Stack_Base then
3738             Stack_Ptr := Stack_Base - 2;
3739             Stack_Base := Stack (Stack_Ptr + 2).Cursor;
3740
3741          --  If stuff was pushed in the inner region, then we have to
3742          --  push a PC_R_Restore node so that we properly handle possible
3743          --  rematches within the region.
3744
3745          else
3746             Stack_Ptr := Stack_Ptr + 1;
3747             Stack (Stack_Ptr).Cursor := Stack_Base;
3748             Stack (Stack_Ptr).Node   := CP_R_Restore'Access;
3749             Stack_Base := Stack (Stack_Base).Cursor;
3750          end if;
3751       end Pop_Region;
3752
3753       ----------
3754       -- Push --
3755       ----------
3756
3757       procedure Push (Node : PE_Ptr) is
3758       begin
3759          Stack_Ptr := Stack_Ptr + 1;
3760          Stack (Stack_Ptr).Cursor := Cursor;
3761          Stack (Stack_Ptr).Node   := Node;
3762       end Push;
3763
3764       -----------------
3765       -- Push_Region --
3766       -----------------
3767
3768       procedure Push_Region is
3769       begin
3770          Stack_Ptr := Stack_Ptr + 2;
3771          Stack (Stack_Ptr).Cursor := Stack_Base;
3772          Stack (Stack_Ptr).Node   := CP_R_Remove'Access;
3773          Stack_Base := Stack_Ptr;
3774       end Push_Region;
3775
3776    --  Start of processing for XMatch
3777
3778    begin
3779       if Pat_P = null then
3780          Uninitialized_Pattern;
3781       end if;
3782
3783       --  Check we have enough stack for this pattern. This check deals with
3784       --  every possibility except a match of a recursive pattern, where we
3785       --  make a check at each recursion level.
3786
3787       if Pat_S >= Stack_Size - 1 then
3788          raise Pattern_Stack_Overflow;
3789       end if;
3790
3791       --  In anchored mode, the bottom entry on the stack is an abort entry
3792
3793       if Anchored_Mode then
3794          Stack (Stack_Init).Node   := CP_Cancel'Access;
3795          Stack (Stack_Init).Cursor := 0;
3796
3797       --  In unanchored more, the bottom entry on the stack references
3798       --  the special pattern element PE_Unanchored, whose Pthen field
3799       --  points to the initial pattern element. The cursor value in this
3800       --  entry is the number of anchor moves so far.
3801
3802       else
3803          Stack (Stack_Init).Node   := PE_Unanchored'Unchecked_Access;
3804          Stack (Stack_Init).Cursor := 0;
3805       end if;
3806
3807       Stack_Ptr    := Stack_Init;
3808       Stack_Base   := Stack_Ptr;
3809       Cursor       := 0;
3810       Node         := Pat_P;
3811       goto Match;
3812
3813       -----------------------------------------
3814       -- Main Pattern Matching State Control --
3815       -----------------------------------------
3816
3817       --  This is a state machine which uses gotos to change state. The
3818       --  initial state is Match, to initiate the matching of the first
3819       --  element, so the goto Match above starts the match. In the
3820       --  following descriptions, we indicate the global values that
3821       --  are relevant for the state transition.
3822
3823       --  Come here if entire match fails
3824
3825       <<Match_Fail>>
3826          Start := 0;
3827          Stop  := 0;
3828          return;
3829
3830       --  Come here if entire match succeeds
3831
3832       --    Cursor        current position in subject string
3833
3834       <<Match_Succeed>>
3835          Start := Stack (Stack_Init).Cursor + 1;
3836          Stop  := Cursor;
3837
3838          --  Scan history stack for deferred assignments or writes
3839
3840          if Assign_OnM then
3841             for S in Stack_Init .. Stack_Ptr loop
3842                if Stack (S).Node = CP_Assign'Access then
3843                   declare
3844                      Inner_Base    : constant Stack_Range :=
3845                                        Stack (S + 1).Cursor;
3846                      Special_Entry : constant Stack_Range :=
3847                                        Inner_Base - 1;
3848                      Node_OnM      : constant PE_Ptr  :=
3849                                        Stack (Special_Entry).Node;
3850                      Start         : constant Natural :=
3851                                        Stack (Special_Entry).Cursor + 1;
3852                      Stop          : constant Natural := Stack (S).Cursor;
3853
3854                   begin
3855                      if Node_OnM.Pcode = PC_Assign_OnM then
3856                         Set_String (Node_OnM.VP.all, Subject (Start .. Stop));
3857
3858                      elsif Node_OnM.Pcode = PC_Write_OnM then
3859                         Put_Line (Node_OnM.FP.all, Subject (Start .. Stop));
3860
3861                      else
3862                         Logic_Error;
3863                      end if;
3864                   end;
3865                end if;
3866             end loop;
3867          end if;
3868
3869          return;
3870
3871       --  Come here if attempt to match current element fails
3872
3873       --    Stack_Base    current stack base
3874       --    Stack_Ptr     current stack pointer
3875
3876       <<Fail>>
3877          Cursor := Stack (Stack_Ptr).Cursor;
3878          Node   := Stack (Stack_Ptr).Node;
3879          Stack_Ptr := Stack_Ptr - 1;
3880          goto Match;
3881
3882       --  Come here if attempt to match current element succeeds
3883
3884       --    Cursor        current position in subject string
3885       --    Node          pointer to node successfully matched
3886       --    Stack_Base    current stack base
3887       --    Stack_Ptr     current stack pointer
3888
3889       <<Succeed>>
3890          Node := Node.Pthen;
3891
3892       --  Come here to match the next pattern element
3893
3894       --    Cursor        current position in subject string
3895       --    Node          pointer to node to be matched
3896       --    Stack_Base    current stack base
3897       --    Stack_Ptr     current stack pointer
3898
3899       <<Match>>
3900
3901       --------------------------------------------------
3902       -- Main Pattern Match Element Matching Routines --
3903       --------------------------------------------------
3904
3905       --  Here is the case statement that processes the current node. The
3906       --  processing for each element does one of five things:
3907
3908       --    goto Succeed        to move to the successor
3909       --    goto Match_Succeed  if the entire match succeeds
3910       --    goto Match_Fail     if the entire match fails
3911       --    goto Fail           to signal failure of current match
3912
3913       --  Processing is NOT allowed to fall through
3914
3915       case Node.Pcode is
3916
3917          --  Cancel
3918
3919          when PC_Cancel =>
3920             goto Match_Fail;
3921
3922          --  Alternation
3923
3924          when PC_Alt =>
3925             Push (Node.Alt);
3926             Node := Node.Pthen;
3927             goto Match;
3928
3929          --  Any (one character case)
3930
3931          when PC_Any_CH =>
3932             if Cursor < Length
3933               and then Subject (Cursor + 1) = Node.Char
3934             then
3935                Cursor := Cursor + 1;
3936                goto Succeed;
3937             else
3938                goto Fail;
3939             end if;
3940
3941          --  Any (character set case)
3942
3943          when PC_Any_CS =>
3944             if Cursor < Length
3945               and then Is_In (Subject (Cursor + 1), Node.CS)
3946             then
3947                Cursor := Cursor + 1;
3948                goto Succeed;
3949             else
3950                goto Fail;
3951             end if;
3952
3953          --  Any (string function case)
3954
3955          when PC_Any_VF => declare
3956             U : constant VString := Node.VF.all;
3957             S : String_Access;
3958             L : Natural;
3959
3960          begin
3961             Get_String (U, S, L);
3962
3963             if Cursor < Length
3964               and then Is_In (Subject (Cursor + 1), S (1 .. L))
3965             then
3966                Cursor := Cursor + 1;
3967                goto Succeed;
3968             else
3969                goto Fail;
3970             end if;
3971          end;
3972
3973          --  Any (string pointer case)
3974
3975          when PC_Any_VP => declare
3976             U : constant VString := Node.VP.all;
3977             S : String_Access;
3978             L : Natural;
3979
3980          begin
3981             Get_String (U, S, L);
3982
3983             if Cursor < Length
3984               and then Is_In (Subject (Cursor + 1), S (1 .. L))
3985             then
3986                Cursor := Cursor + 1;
3987                goto Succeed;
3988             else
3989                goto Fail;
3990             end if;
3991          end;
3992
3993          --  Arb (initial match)
3994
3995          when PC_Arb_X =>
3996             Push (Node.Alt);
3997             Node := Node.Pthen;
3998             goto Match;
3999
4000          --  Arb (extension)
4001
4002          when PC_Arb_Y  =>
4003             if Cursor < Length then
4004                Cursor := Cursor + 1;
4005                Push (Node);
4006                goto Succeed;
4007             else
4008                goto Fail;
4009             end if;
4010
4011          --  Arbno_S (simple Arbno initialize). This is the node that
4012          --  initiates the match of a simple Arbno structure.
4013
4014          when PC_Arbno_S =>
4015             Push (Node.Alt);
4016             Node := Node.Pthen;
4017             goto Match;
4018
4019          --  Arbno_X (Arbno initialize). This is the node that initiates
4020          --  the match of a complex Arbno structure.
4021
4022          when PC_Arbno_X =>
4023             Push (Node.Alt);
4024             Node := Node.Pthen;
4025             goto Match;
4026
4027          --  Arbno_Y (Arbno rematch). This is the node that is executed
4028          --  following successful matching of one instance of a complex
4029          --  Arbno pattern.
4030
4031          when PC_Arbno_Y => declare
4032             Null_Match : constant Boolean :=
4033                            Cursor = Stack (Stack_Base - 1).Cursor;
4034
4035          begin
4036             Pop_Region;
4037
4038             --  If arbno extension matched null, then immediately fail
4039
4040             if Null_Match then
4041                goto Fail;
4042             end if;
4043
4044             --  Here we must do a stack check to make sure enough stack
4045             --  is left. This check will happen once for each instance of
4046             --  the Arbno pattern that is matched. The Nat field of a
4047             --  PC_Arbno pattern contains the maximum stack entries needed
4048             --  for the Arbno with one instance and the successor pattern
4049
4050             if Stack_Ptr + Node.Nat >= Stack'Last then
4051                raise Pattern_Stack_Overflow;
4052             end if;
4053
4054             goto Succeed;
4055          end;
4056
4057          --  Assign. If this node is executed, it means the assign-on-match
4058          --  or write-on-match operation will not happen after all, so we
4059          --  is propagate the failure, removing the PC_Assign node.
4060
4061          when PC_Assign =>
4062             goto Fail;
4063
4064          --  Assign immediate. This node performs the actual assignment.
4065
4066          when PC_Assign_Imm =>
4067             Set_String
4068               (Node.VP.all,
4069                Subject (Stack (Stack_Base - 1).Cursor + 1 .. Cursor));
4070             Pop_Region;
4071             goto Succeed;
4072
4073          --  Assign on match. This node sets up for the eventual assignment
4074
4075          when PC_Assign_OnM =>
4076             Stack (Stack_Base - 1).Node := Node;
4077             Push (CP_Assign'Access);
4078             Pop_Region;
4079             Assign_OnM := True;
4080             goto Succeed;
4081
4082          --  Bal
4083
4084          when PC_Bal =>
4085             if Cursor >= Length or else Subject (Cursor + 1) = ')' then
4086                goto Fail;
4087
4088             elsif Subject (Cursor + 1) = '(' then
4089                declare
4090                   Paren_Count : Natural := 1;
4091
4092                begin
4093                   loop
4094                      Cursor := Cursor + 1;
4095
4096                      if Cursor >= Length then
4097                         goto Fail;
4098
4099                      elsif Subject (Cursor + 1) = '(' then
4100                         Paren_Count := Paren_Count + 1;
4101
4102                      elsif Subject (Cursor + 1) = ')' then
4103                         Paren_Count := Paren_Count - 1;
4104                         exit when Paren_Count = 0;
4105                      end if;
4106                   end loop;
4107                end;
4108             end if;
4109
4110             Cursor := Cursor + 1;
4111             Push (Node);
4112             goto Succeed;
4113
4114          --  Break (one character case)
4115
4116          when PC_Break_CH =>
4117             while Cursor < Length loop
4118                if Subject (Cursor + 1) = Node.Char then
4119                   goto Succeed;
4120                else
4121                   Cursor := Cursor + 1;
4122                end if;
4123             end loop;
4124
4125             goto Fail;
4126
4127          --  Break (character set case)
4128
4129          when PC_Break_CS =>
4130             while Cursor < Length loop
4131                if Is_In (Subject (Cursor + 1), Node.CS) then
4132                   goto Succeed;
4133                else
4134                   Cursor := Cursor + 1;
4135                end if;
4136             end loop;
4137
4138             goto Fail;
4139
4140          --  Break (string function case)
4141
4142          when PC_Break_VF => declare
4143             U : constant VString := Node.VF.all;
4144             S : String_Access;
4145             L : Natural;
4146
4147          begin
4148             Get_String (U, S, L);
4149
4150             while Cursor < Length loop
4151                if Is_In (Subject (Cursor + 1), S (1 .. L)) then
4152                   goto Succeed;
4153                else
4154                   Cursor := Cursor + 1;
4155                end if;
4156             end loop;
4157
4158             goto Fail;
4159          end;
4160
4161          --  Break (string pointer case)
4162
4163          when PC_Break_VP => declare
4164             U : constant VString := Node.VP.all;
4165             S : String_Access;
4166             L : Natural;
4167
4168          begin
4169             Get_String (U, S, L);
4170
4171             while Cursor < Length loop
4172                if Is_In (Subject (Cursor + 1), S (1 .. L)) then
4173                   goto Succeed;
4174                else
4175                   Cursor := Cursor + 1;
4176                end if;
4177             end loop;
4178
4179             goto Fail;
4180          end;
4181
4182          --  BreakX (one character case)
4183
4184          when PC_BreakX_CH =>
4185             while Cursor < Length loop
4186                if Subject (Cursor + 1) = Node.Char then
4187                   goto Succeed;
4188                else
4189                   Cursor := Cursor + 1;
4190                end if;
4191             end loop;
4192
4193             goto Fail;
4194
4195          --  BreakX (character set case)
4196
4197          when PC_BreakX_CS =>
4198             while Cursor < Length loop
4199                if Is_In (Subject (Cursor + 1), Node.CS) then
4200                   goto Succeed;
4201                else
4202                   Cursor := Cursor + 1;
4203                end if;
4204             end loop;
4205
4206             goto Fail;
4207
4208          --  BreakX (string function case)
4209
4210          when PC_BreakX_VF => declare
4211             U : constant VString := Node.VF.all;
4212             S : String_Access;
4213             L : Natural;
4214
4215          begin
4216             Get_String (U, S, L);
4217
4218             while Cursor < Length loop
4219                if Is_In (Subject (Cursor + 1), S (1 .. L)) then
4220                   goto Succeed;
4221                else
4222                   Cursor := Cursor + 1;
4223                end if;
4224             end loop;
4225
4226             goto Fail;
4227          end;
4228
4229          --  BreakX (string pointer case)
4230
4231          when PC_BreakX_VP => declare
4232             U : constant VString := Node.VP.all;
4233             S : String_Access;
4234             L : Natural;
4235
4236          begin
4237             Get_String (U, S, L);
4238
4239             while Cursor < Length loop
4240                if Is_In (Subject (Cursor + 1), S (1 .. L)) then
4241                   goto Succeed;
4242                else
4243                   Cursor := Cursor + 1;
4244                end if;
4245             end loop;
4246
4247             goto Fail;
4248          end;
4249
4250          --  BreakX_X (BreakX extension). See section on "Compound Pattern
4251          --  Structures". This node is the alternative that is stacked to
4252          --  skip past the break character and extend the break.
4253
4254          when PC_BreakX_X =>
4255             Cursor := Cursor + 1;
4256             goto Succeed;
4257
4258          --  Character (one character string)
4259
4260          when PC_Char =>
4261             if Cursor < Length
4262               and then Subject (Cursor + 1) = Node.Char
4263             then
4264                Cursor := Cursor + 1;
4265                goto Succeed;
4266             else
4267                goto Fail;
4268             end if;
4269
4270          --  End of Pattern
4271
4272          when PC_EOP =>
4273             if Stack_Base = Stack_Init then
4274                goto Match_Succeed;
4275
4276             --  End of recursive inner match. See separate section on
4277             --  handing of recursive pattern matches for details.
4278
4279             else
4280                Node := Stack (Stack_Base - 1).Node;
4281                Pop_Region;
4282                goto Match;
4283             end if;
4284
4285          --  Fail
4286
4287          when PC_Fail =>
4288             goto Fail;
4289
4290          --  Fence (built in pattern)
4291
4292          when PC_Fence =>
4293             Push (CP_Cancel'Access);
4294             goto Succeed;
4295
4296          --  Fence function node X. This is the node that gets control
4297          --  after a successful match of the fenced pattern.
4298
4299          when PC_Fence_X =>
4300             Stack_Ptr := Stack_Ptr + 1;
4301             Stack (Stack_Ptr).Cursor := Stack_Base;
4302             Stack (Stack_Ptr).Node   := CP_Fence_Y'Access;
4303             Stack_Base := Stack (Stack_Base).Cursor;
4304             goto Succeed;
4305
4306          --  Fence function node Y. This is the node that gets control on
4307          --  a failure that occurs after the fenced pattern has matched.
4308
4309          --  Note: the Cursor at this stage is actually the inner stack
4310          --  base value. We don't reset this, but we do use it to strip
4311          --  off all the entries made by the fenced pattern.
4312
4313          when PC_Fence_Y =>
4314             Stack_Ptr := Cursor - 2;
4315             goto Fail;
4316
4317          --  Len (integer case)
4318
4319          when PC_Len_Nat =>
4320             if Cursor + Node.Nat > Length then
4321                goto Fail;
4322             else
4323                Cursor := Cursor + Node.Nat;
4324                goto Succeed;
4325             end if;
4326
4327          --  Len (Integer function case)
4328
4329          when PC_Len_NF => declare
4330             N : constant Natural := Node.NF.all;
4331
4332          begin
4333             if Cursor + N > Length then
4334                goto Fail;
4335             else
4336                Cursor := Cursor + N;
4337                goto Succeed;
4338             end if;
4339          end;
4340
4341          --  Len (integer pointer case)
4342
4343          when PC_Len_NP =>
4344             if Cursor + Node.NP.all > Length then
4345                goto Fail;
4346             else
4347                Cursor := Cursor + Node.NP.all;
4348                goto Succeed;
4349             end if;
4350
4351          --  NotAny (one character case)
4352
4353          when PC_NotAny_CH =>
4354             if Cursor < Length
4355               and then Subject (Cursor + 1) /= Node.Char
4356             then
4357                Cursor := Cursor + 1;
4358                goto Succeed;
4359             else
4360                goto Fail;
4361             end if;
4362
4363          --  NotAny (character set case)
4364
4365          when PC_NotAny_CS =>
4366             if Cursor < Length
4367               and then not Is_In (Subject (Cursor + 1), Node.CS)
4368             then
4369                Cursor := Cursor + 1;
4370                goto Succeed;
4371             else
4372                goto Fail;
4373             end if;
4374
4375          --  NotAny (string function case)
4376
4377          when PC_NotAny_VF => declare
4378             U : constant VString := Node.VF.all;
4379             S : String_Access;
4380             L : Natural;
4381
4382          begin
4383             Get_String (U, S, L);
4384
4385             if Cursor < Length
4386               and then
4387                 not Is_In (Subject (Cursor + 1), S (1 .. L))
4388             then
4389                Cursor := Cursor + 1;
4390                goto Succeed;
4391             else
4392                goto Fail;
4393             end if;
4394          end;
4395
4396          --  NotAny (string pointer case)
4397
4398          when PC_NotAny_VP => declare
4399             U : constant VString := Node.VP.all;
4400             S : String_Access;
4401             L : Natural;
4402
4403          begin
4404             Get_String (U, S, L);
4405
4406             if Cursor < Length
4407               and then
4408                 not Is_In (Subject (Cursor + 1), S (1 .. L))
4409             then
4410                Cursor := Cursor + 1;
4411                goto Succeed;
4412             else
4413                goto Fail;
4414             end if;
4415          end;
4416
4417          --  NSpan (one character case)
4418
4419          when PC_NSpan_CH =>
4420             while Cursor < Length
4421               and then Subject (Cursor + 1) = Node.Char
4422             loop
4423                Cursor := Cursor + 1;
4424             end loop;
4425
4426             goto Succeed;
4427
4428          --  NSpan (character set case)
4429
4430          when PC_NSpan_CS =>
4431             while Cursor < Length
4432               and then Is_In (Subject (Cursor + 1), Node.CS)
4433             loop
4434                Cursor := Cursor + 1;
4435             end loop;
4436
4437             goto Succeed;
4438
4439          --  NSpan (string function case)
4440
4441          when PC_NSpan_VF => declare
4442             U : constant VString := Node.VF.all;
4443             S : String_Access;
4444             L : Natural;
4445
4446          begin
4447             Get_String (U, S, L);
4448
4449             while Cursor < Length
4450               and then Is_In (Subject (Cursor + 1), S (1 .. L))
4451             loop
4452                Cursor := Cursor + 1;
4453             end loop;
4454
4455             goto Succeed;
4456          end;
4457
4458          --  NSpan (string pointer case)
4459
4460          when PC_NSpan_VP => declare
4461             U : constant VString := Node.VP.all;
4462             S : String_Access;
4463             L : Natural;
4464
4465          begin
4466             Get_String (U, S, L);
4467
4468             while Cursor < Length
4469               and then Is_In (Subject (Cursor + 1), S (1 .. L))
4470             loop
4471                Cursor := Cursor + 1;
4472             end loop;
4473
4474             goto Succeed;
4475          end;
4476
4477          --  Null string
4478
4479          when PC_Null =>
4480             goto Succeed;
4481
4482          --  Pos (integer case)
4483
4484          when PC_Pos_Nat =>
4485             if Cursor = Node.Nat then
4486                goto Succeed;
4487             else
4488                goto Fail;
4489             end if;
4490
4491          --  Pos (Integer function case)
4492
4493          when PC_Pos_NF => declare
4494             N : constant Natural := Node.NF.all;
4495
4496          begin
4497             if Cursor = N then
4498                goto Succeed;
4499             else
4500                goto Fail;
4501             end if;
4502          end;
4503
4504          --  Pos (integer pointer case)
4505
4506          when PC_Pos_NP =>
4507             if Cursor = Node.NP.all then
4508                goto Succeed;
4509             else
4510                goto Fail;
4511             end if;
4512
4513          --  Predicate function
4514
4515          when PC_Pred_Func =>
4516             if Node.BF.all then
4517                goto Succeed;
4518             else
4519                goto Fail;
4520             end if;
4521
4522          --  Region Enter. Initiate new pattern history stack region
4523
4524          when PC_R_Enter =>
4525             Stack (Stack_Ptr + 1).Cursor := Cursor;
4526             Push_Region;
4527             goto Succeed;
4528
4529          --  Region Remove node. This is the node stacked by an R_Enter.
4530          --  It removes the special format stack entry right underneath, and
4531          --  then restores the outer level stack base and signals failure.
4532
4533          --  Note: the cursor value at this stage is actually the (negative)
4534          --  stack base value for the outer level.
4535
4536          when PC_R_Remove =>
4537             Stack_Base := Cursor;
4538             Stack_Ptr := Stack_Ptr - 1;
4539             goto Fail;
4540
4541          --  Region restore node. This is the node stacked at the end of an
4542          --  inner level match. Its function is to restore the inner level
4543          --  region, so that alternatives in this region can be sought.
4544
4545          --  Note: the Cursor at this stage is actually the negative of the
4546          --  inner stack base value, which we use to restore the inner region.
4547
4548          when PC_R_Restore =>
4549             Stack_Base := Cursor;
4550             goto Fail;
4551
4552          --  Rest
4553
4554          when PC_Rest =>
4555             Cursor := Length;
4556             goto Succeed;
4557
4558          --  Initiate recursive match (pattern pointer case)
4559
4560          when PC_Rpat =>
4561             Stack (Stack_Ptr + 1).Node := Node.Pthen;
4562             Push_Region;
4563
4564             if Stack_Ptr + Node.PP.all.Stk >= Stack_Size then
4565                raise Pattern_Stack_Overflow;
4566             else
4567                Node := Node.PP.all.P;
4568                goto Match;
4569             end if;
4570
4571          --  RPos (integer case)
4572
4573          when PC_RPos_Nat =>
4574             if Cursor = (Length - Node.Nat) then
4575                goto Succeed;
4576             else
4577                goto Fail;
4578             end if;
4579
4580          --  RPos (integer function case)
4581
4582          when PC_RPos_NF => declare
4583             N : constant Natural := Node.NF.all;
4584
4585          begin
4586             if Length - Cursor = N then
4587                goto Succeed;
4588             else
4589                goto Fail;
4590             end if;
4591          end;
4592
4593          --  RPos (integer pointer case)
4594
4595          when PC_RPos_NP =>
4596             if Cursor = (Length - Node.NP.all) then
4597                goto Succeed;
4598             else
4599                goto Fail;
4600             end if;
4601
4602          --  RTab (integer case)
4603
4604          when PC_RTab_Nat =>
4605             if Cursor <= (Length - Node.Nat) then
4606                Cursor := Length - Node.Nat;
4607                goto Succeed;
4608             else
4609                goto Fail;
4610             end if;
4611
4612          --  RTab (integer function case)
4613
4614          when PC_RTab_NF => declare
4615             N : constant Natural := Node.NF.all;
4616
4617          begin
4618             if Length - Cursor >= N then
4619                Cursor := Length - N;
4620                goto Succeed;
4621             else
4622                goto Fail;
4623             end if;
4624          end;
4625
4626          --  RTab (integer pointer case)
4627
4628          when PC_RTab_NP =>
4629             if Cursor <= (Length - Node.NP.all) then
4630                Cursor := Length - Node.NP.all;
4631                goto Succeed;
4632             else
4633                goto Fail;
4634             end if;
4635
4636          --  Cursor assignment
4637
4638          when PC_Setcur =>
4639             Node.Var.all := Cursor;
4640             goto Succeed;
4641
4642          --  Span (one character case)
4643
4644          when PC_Span_CH => declare
4645             P : Natural := Cursor;
4646
4647          begin
4648             while P < Length
4649               and then Subject (P + 1) = Node.Char
4650             loop
4651                P := P + 1;
4652             end loop;
4653
4654             if P /= Cursor then
4655                Cursor := P;
4656                goto Succeed;
4657             else
4658                goto Fail;
4659             end if;
4660          end;
4661
4662          --  Span (character set case)
4663
4664          when PC_Span_CS => declare
4665             P : Natural := Cursor;
4666
4667          begin
4668             while P < Length
4669               and then Is_In (Subject (P + 1), Node.CS)
4670             loop
4671                P := P + 1;
4672             end loop;
4673
4674             if P /= Cursor then
4675                Cursor := P;
4676                goto Succeed;
4677             else
4678                goto Fail;
4679             end if;
4680          end;
4681
4682          --  Span (string function case)
4683
4684          when PC_Span_VF => declare
4685             U : constant VString := Node.VF.all;
4686             S : String_Access;
4687             L : Natural;
4688             P : Natural;
4689
4690          begin
4691             Get_String (U, S, L);
4692
4693             P := Cursor;
4694             while P < Length
4695               and then Is_In (Subject (P + 1), S (1 .. L))
4696             loop
4697                P := P + 1;
4698             end loop;
4699
4700             if P /= Cursor then
4701                Cursor := P;
4702                goto Succeed;
4703             else
4704                goto Fail;
4705             end if;
4706          end;
4707
4708          --  Span (string pointer case)
4709
4710          when PC_Span_VP => declare
4711             U : constant VString := Node.VP.all;
4712             S : String_Access;
4713             L : Natural;
4714             P : Natural;
4715
4716          begin
4717             Get_String (U, S, L);
4718
4719             P := Cursor;
4720             while P < Length
4721               and then Is_In (Subject (P + 1), S (1 .. L))
4722             loop
4723                P := P + 1;
4724             end loop;
4725
4726             if P /= Cursor then
4727                Cursor := P;
4728                goto Succeed;
4729             else
4730                goto Fail;
4731             end if;
4732          end;
4733
4734          --  String (two character case)
4735
4736          when PC_String_2 =>
4737             if (Length - Cursor) >= 2
4738               and then Subject (Cursor + 1 .. Cursor + 2) = Node.Str2
4739             then
4740                Cursor := Cursor + 2;
4741                goto Succeed;
4742             else
4743                goto Fail;
4744             end if;
4745
4746          --  String (three character case)
4747
4748          when PC_String_3 =>
4749             if (Length - Cursor) >= 3
4750               and then Subject (Cursor + 1 .. Cursor + 3) = Node.Str3
4751             then
4752                Cursor := Cursor + 3;
4753                goto Succeed;
4754             else
4755                goto Fail;
4756             end if;
4757
4758          --  String (four character case)
4759
4760          when PC_String_4 =>
4761             if (Length - Cursor) >= 4
4762               and then Subject (Cursor + 1 .. Cursor + 4) = Node.Str4
4763             then
4764                Cursor := Cursor + 4;
4765                goto Succeed;
4766             else
4767                goto Fail;
4768             end if;
4769
4770          --  String (five character case)
4771
4772          when PC_String_5 =>
4773             if (Length - Cursor) >= 5
4774               and then Subject (Cursor + 1 .. Cursor + 5) = Node.Str5
4775             then
4776                Cursor := Cursor + 5;
4777                goto Succeed;
4778             else
4779                goto Fail;
4780             end if;
4781
4782          --  String (six character case)
4783
4784          when PC_String_6 =>
4785             if (Length - Cursor) >= 6
4786               and then Subject (Cursor + 1 .. Cursor + 6) = Node.Str6
4787             then
4788                Cursor := Cursor + 6;
4789                goto Succeed;
4790             else
4791                goto Fail;
4792             end if;
4793
4794          --  String (case of more than six characters)
4795
4796          when PC_String => declare
4797             Len : constant Natural := Node.Str'Length;
4798
4799          begin
4800             if (Length - Cursor) >= Len
4801               and then Node.Str.all = Subject (Cursor + 1 .. Cursor + Len)
4802             then
4803                Cursor := Cursor + Len;
4804                goto Succeed;
4805             else
4806                goto Fail;
4807             end if;
4808          end;
4809
4810          --  String (function case)
4811
4812          when PC_String_VF => declare
4813             U : constant VString := Node.VF.all;
4814             S : String_Access;
4815             L : Natural;
4816
4817          begin
4818             Get_String (U, S, L);
4819
4820             if (Length - Cursor) >= L
4821               and then S (1 .. L) = Subject (Cursor + 1 .. Cursor + L)
4822             then
4823                Cursor := Cursor + L;
4824                goto Succeed;
4825             else
4826                goto Fail;
4827             end if;
4828          end;
4829
4830          --  String (pointer case)
4831
4832          when PC_String_VP => declare
4833             U : constant VString := Node.VP.all;
4834             S : String_Access;
4835             L : Natural;
4836
4837          begin
4838             Get_String (U, S, L);
4839
4840             if (Length - Cursor) >= L
4841               and then S (1 .. L) = Subject (Cursor + 1 .. Cursor + L)
4842             then
4843                Cursor := Cursor + L;
4844                goto Succeed;
4845             else
4846                goto Fail;
4847             end if;
4848          end;
4849
4850          --  Succeed
4851
4852          when PC_Succeed =>
4853             Push (Node);
4854             goto Succeed;
4855
4856          --  Tab (integer case)
4857
4858          when PC_Tab_Nat =>
4859             if Cursor <= Node.Nat then
4860                Cursor := Node.Nat;
4861                goto Succeed;
4862             else
4863                goto Fail;
4864             end if;
4865
4866          --  Tab (integer function case)
4867
4868          when PC_Tab_NF => declare
4869             N : constant Natural := Node.NF.all;
4870
4871          begin
4872             if Cursor <= N then
4873                Cursor := N;
4874                goto Succeed;
4875             else
4876                goto Fail;
4877             end if;
4878          end;
4879
4880          --  Tab (integer pointer case)
4881
4882          when PC_Tab_NP =>
4883             if Cursor <= Node.NP.all then
4884                Cursor := Node.NP.all;
4885                goto Succeed;
4886             else
4887                goto Fail;
4888             end if;
4889
4890          --  Unanchored movement
4891
4892          when PC_Unanchored =>
4893
4894             --  All done if we tried every position
4895
4896             if Cursor > Length then
4897                goto Match_Fail;
4898
4899             --  Otherwise extend the anchor point, and restack ourself
4900
4901             else
4902                Cursor := Cursor + 1;
4903                Push (Node);
4904                goto Succeed;
4905             end if;
4906
4907          --  Write immediate. This node performs the actual write
4908
4909          when PC_Write_Imm =>
4910             Put_Line
4911               (Node.FP.all,
4912                Subject (Stack (Stack_Base - 1).Cursor + 1 .. Cursor));
4913             Pop_Region;
4914             goto Succeed;
4915
4916          --  Write on match. This node sets up for the eventual write
4917
4918          when PC_Write_OnM =>
4919             Stack (Stack_Base - 1).Node := Node;
4920             Push (CP_Assign'Access);
4921             Pop_Region;
4922             Assign_OnM := True;
4923             goto Succeed;
4924
4925       end case;
4926
4927       --  We are NOT allowed to fall though this case statement, since every
4928       --  match routine must end by executing a goto to the appropriate point
4929       --  in the finite state machine model.
4930
4931       pragma Warnings (Off);
4932       Logic_Error;
4933       pragma Warnings (On);
4934    end XMatch;
4935
4936    -------------
4937    -- XMatchD --
4938    -------------
4939
4940    --  Maintenance note: There is a LOT of code duplication between XMatch
4941    --  and XMatchD. This is quite intentional, the point is to avoid any
4942    --  unnecessary debugging overhead in the XMatch case, but this does mean
4943    --  that any changes to XMatchD must be mirrored in XMatch. In case of
4944    --  any major changes, the proper approach is to delete XMatch, make the
4945    --  changes to XMatchD, and then make a copy of XMatchD, removing all
4946    --  calls to Dout, and all Put and Put_Line operations. This copy becomes
4947    --  the new XMatch.
4948
4949    procedure XMatchD
4950      (Subject : String;
4951       Pat_P   : PE_Ptr;
4952       Pat_S   : Natural;
4953       Start   : out Natural;
4954       Stop    : out Natural)
4955    is
4956       Node : PE_Ptr;
4957       --  Pointer to current pattern node. Initialized from Pat_P, and then
4958       --  updated as the match proceeds through its constituent elements.
4959
4960       Length : constant Natural := Subject'Length;
4961       --  Length of string (= Subject'Last, since Subject'First is always 1)
4962
4963       Cursor : Integer := 0;
4964       --  If the value is non-negative, then this value is the index showing
4965       --  the current position of the match in the subject string. The next
4966       --  character to be matched is at Subject (Cursor + 1). Note that since
4967       --  our view of the subject string in XMatch always has a lower bound
4968       --  of one, regardless of original bounds, that this definition exactly
4969       --  corresponds to the cursor value as referenced by functions like Pos.
4970       --
4971       --  If the value is negative, then this is a saved stack pointer,
4972       --  typically a base pointer of an inner or outer region. Cursor
4973       --  temporarily holds such a value when it is popped from the stack
4974       --  by Fail. In all cases, Cursor is reset to a proper non-negative
4975       --  cursor value before the match proceeds (e.g. by propagating the
4976       --  failure and popping a "real" cursor value from the stack.
4977
4978       PE_Unanchored : aliased PE := (PC_Unanchored, 0, Pat_P);
4979       --  Dummy pattern element used in the unanchored case.
4980
4981       Region_Level : Natural := 0;
4982       --  Keeps track of recursive region level. This is used only for
4983       --  debugging, it is the number of saved history stack base values.
4984
4985       Stack : Stack_Type;
4986       --  The pattern matching failure stack for this call to Match
4987
4988       Stack_Ptr : Stack_Range;
4989       --  Current stack pointer. This points to the top element of the stack
4990       --  that is currently in use. At the outer level this is the special
4991       --  entry placed on the stack according to the anchor mode.
4992
4993       Stack_Init : constant Stack_Range := Stack'First + 1;
4994       --  This is the initial value of the Stack_Ptr and Stack_Base. The
4995       --  initial (Stack'First) element of the stack is not used so that
4996       --  when we pop the last element off, Stack_Ptr is still in range.
4997
4998       Stack_Base : Stack_Range;
4999       --  This value is the stack base value, i.e. the stack pointer for the
5000       --  first history stack entry in the current stack region. See separate
5001       --  section on handling of recursive pattern matches.
5002
5003       Assign_OnM : Boolean := False;
5004       --  Set True if assign-on-match or write-on-match operations may be
5005       --  present in the history stack, which must then be scanned on a
5006       --  successful match.
5007
5008       procedure Dout (Str : String);
5009       --  Output string to standard error with bars indicating region level.
5010
5011       procedure Dout (Str : String; A : Character);
5012       --  Calls Dout with the string S ('A')
5013
5014       procedure Dout (Str : String; A : Character_Set);
5015       --  Calls Dout with the string S ("A")
5016
5017       procedure Dout (Str : String; A : Natural);
5018       --  Calls Dout with the string S (A)
5019
5020       procedure Dout (Str : String; A : String);
5021       --  Calls Dout with the string S ("A")
5022
5023       function Img (P : PE_Ptr) return String;
5024       --  Returns a string of the form #nnn where nnn is P.Index
5025
5026       procedure Pop_Region;
5027       pragma Inline (Pop_Region);
5028       --  Used at the end of processing of an inner region. if the inner
5029       --  region left no stack entries, then all trace of it is removed.
5030       --  Otherwise a PC_Restore_Region entry is pushed to ensure proper
5031       --  handling of alternatives in the inner region.
5032
5033       procedure Push (Node : PE_Ptr);
5034       pragma Inline (Push);
5035       --  Make entry in pattern matching stack with current cursor valeu
5036
5037       procedure Push_Region;
5038       pragma Inline (Push_Region);
5039       --  This procedure makes a new region on the history stack. The
5040       --  caller first establishes the special entry on the stack, but
5041       --  does not push the stack pointer. Then this call stacks a
5042       --  PC_Remove_Region node, on top of this entry, using the cursor
5043       --  field of the PC_Remove_Region entry to save the outer level
5044       --  stack base value, and resets the stack base to point to this
5045       --  PC_Remove_Region node.
5046
5047       ----------
5048       -- Dout --
5049       ----------
5050
5051       procedure Dout (Str : String) is
5052       begin
5053          for J in 1 .. Region_Level loop
5054             Put ("| ");
5055          end loop;
5056
5057          Put_Line (Str);
5058       end Dout;
5059
5060       procedure Dout (Str : String; A : Character) is
5061       begin
5062          Dout (Str & " ('" & A & "')");
5063       end Dout;
5064
5065       procedure Dout (Str : String; A : Character_Set) is
5066       begin
5067          Dout (Str & " (" & Image (To_Sequence (A)) & ')');
5068       end Dout;
5069
5070       procedure Dout (Str : String; A : Natural) is
5071       begin
5072          Dout (Str & " (" & A & ')');
5073       end Dout;
5074
5075       procedure Dout (Str : String; A : String) is
5076       begin
5077          Dout (Str & " (" & Image (A) & ')');
5078       end Dout;
5079
5080       ---------
5081       -- Img --
5082       ---------
5083
5084       function Img (P : PE_Ptr) return String is
5085       begin
5086          return "#" & Integer (P.Index) & " ";
5087       end Img;
5088
5089       ----------------
5090       -- Pop_Region --
5091       ----------------
5092
5093       procedure Pop_Region is
5094       begin
5095          Region_Level := Region_Level - 1;
5096
5097          --  If nothing was pushed in the inner region, we can just get
5098          --  rid of it entirely, leaving no traces that it was ever there
5099
5100          if Stack_Ptr = Stack_Base then
5101             Stack_Ptr := Stack_Base - 2;
5102             Stack_Base := Stack (Stack_Ptr + 2).Cursor;
5103
5104          --  If stuff was pushed in the inner region, then we have to
5105          --  push a PC_R_Restore node so that we properly handle possible
5106          --  rematches within the region.
5107
5108          else
5109             Stack_Ptr := Stack_Ptr + 1;
5110             Stack (Stack_Ptr).Cursor := Stack_Base;
5111             Stack (Stack_Ptr).Node   := CP_R_Restore'Access;
5112             Stack_Base := Stack (Stack_Base).Cursor;
5113          end if;
5114       end Pop_Region;
5115
5116       ----------
5117       -- Push --
5118       ----------
5119
5120       procedure Push (Node : PE_Ptr) is
5121       begin
5122          Stack_Ptr := Stack_Ptr + 1;
5123          Stack (Stack_Ptr).Cursor := Cursor;
5124          Stack (Stack_Ptr).Node   := Node;
5125       end Push;
5126
5127       -----------------
5128       -- Push_Region --
5129       -----------------
5130
5131       procedure Push_Region is
5132       begin
5133          Region_Level := Region_Level + 1;
5134          Stack_Ptr := Stack_Ptr + 2;
5135          Stack (Stack_Ptr).Cursor := Stack_Base;
5136          Stack (Stack_Ptr).Node   := CP_R_Remove'Access;
5137          Stack_Base := Stack_Ptr;
5138       end Push_Region;
5139
5140    --  Start of processing for XMatchD
5141
5142    begin
5143       New_Line;
5144       Put_Line ("Initiating pattern match, subject = " & Image (Subject));
5145       Put      ("--------------------------------------");
5146
5147       for J in 1 .. Length loop
5148          Put ('-');
5149       end loop;
5150
5151       New_Line;
5152       Put_Line ("subject length = " & Length);
5153
5154       if Pat_P = null then
5155          Uninitialized_Pattern;
5156       end if;
5157
5158       --  Check we have enough stack for this pattern. This check deals with
5159       --  every possibility except a match of a recursive pattern, where we
5160       --  make a check at each recursion level.
5161
5162       if Pat_S >= Stack_Size - 1 then
5163          raise Pattern_Stack_Overflow;
5164       end if;
5165
5166       --  In anchored mode, the bottom entry on the stack is an abort entry
5167
5168       if Anchored_Mode then
5169          Stack (Stack_Init).Node   := CP_Cancel'Access;
5170          Stack (Stack_Init).Cursor := 0;
5171
5172       --  In unanchored more, the bottom entry on the stack references
5173       --  the special pattern element PE_Unanchored, whose Pthen field
5174       --  points to the initial pattern element. The cursor value in this
5175       --  entry is the number of anchor moves so far.
5176
5177       else
5178          Stack (Stack_Init).Node   := PE_Unanchored'Unchecked_Access;
5179          Stack (Stack_Init).Cursor := 0;
5180       end if;
5181
5182       Stack_Ptr    := Stack_Init;
5183       Stack_Base   := Stack_Ptr;
5184       Cursor       := 0;
5185       Node         := Pat_P;
5186       goto Match;
5187
5188       -----------------------------------------
5189       -- Main Pattern Matching State Control --
5190       -----------------------------------------
5191
5192       --  This is a state machine which uses gotos to change state. The
5193       --  initial state is Match, to initiate the matching of the first
5194       --  element, so the goto Match above starts the match. In the
5195       --  following descriptions, we indicate the global values that
5196       --  are relevant for the state transition.
5197
5198       --  Come here if entire match fails
5199
5200       <<Match_Fail>>
5201          Dout ("match fails");
5202          New_Line;
5203          Start := 0;
5204          Stop  := 0;
5205          return;
5206
5207       --  Come here if entire match succeeds
5208
5209       --    Cursor        current position in subject string
5210
5211       <<Match_Succeed>>
5212          Dout ("match succeeds");
5213          Start := Stack (Stack_Init).Cursor + 1;
5214          Stop  := Cursor;
5215          Dout ("first matched character index = " & Start);
5216          Dout ("last matched character index = " & Stop);
5217          Dout ("matched substring = " & Image (Subject (Start .. Stop)));
5218
5219          --  Scan history stack for deferred assignments or writes
5220
5221          if Assign_OnM then
5222             for S in Stack'First .. Stack_Ptr loop
5223                if Stack (S).Node = CP_Assign'Access then
5224                   declare
5225                      Inner_Base    : constant Stack_Range :=
5226                                        Stack (S + 1).Cursor;
5227                      Special_Entry : constant Stack_Range :=
5228                                        Inner_Base - 1;
5229                      Node_OnM      : constant PE_Ptr  :=
5230                                        Stack (Special_Entry).Node;
5231                      Start         : constant Natural :=
5232                                        Stack (Special_Entry).Cursor + 1;
5233                      Stop          : constant Natural := Stack (S).Cursor;
5234
5235                   begin
5236                      if Node_OnM.Pcode = PC_Assign_OnM then
5237                         Set_String (Node_OnM.VP.all, Subject (Start .. Stop));
5238                         Dout
5239                           (Img (Stack (S).Node) &
5240                            "deferred assignment of " &
5241                            Image (Subject (Start .. Stop)));
5242
5243                      elsif Node_OnM.Pcode = PC_Write_OnM then
5244                         Put_Line (Node_OnM.FP.all, Subject (Start .. Stop));
5245                         Dout
5246                           (Img (Stack (S).Node) &
5247                            "deferred write of " &
5248                            Image (Subject (Start .. Stop)));
5249
5250                      else
5251                         Logic_Error;
5252                      end if;
5253                   end;
5254                end if;
5255             end loop;
5256          end if;
5257
5258          New_Line;
5259          return;
5260
5261       --  Come here if attempt to match current element fails
5262
5263       --    Stack_Base    current stack base
5264       --    Stack_Ptr     current stack pointer
5265
5266       <<Fail>>
5267          Cursor := Stack (Stack_Ptr).Cursor;
5268          Node   := Stack (Stack_Ptr).Node;
5269          Stack_Ptr := Stack_Ptr - 1;
5270
5271          if Cursor >= 0 then
5272             Dout ("failure, cursor reset to " & Cursor);
5273          end if;
5274
5275          goto Match;
5276
5277       --  Come here if attempt to match current element succeeds
5278
5279       --    Cursor        current position in subject string
5280       --    Node          pointer to node successfully matched
5281       --    Stack_Base    current stack base
5282       --    Stack_Ptr     current stack pointer
5283
5284       <<Succeed>>
5285          Dout ("success, cursor = " & Cursor);
5286          Node := Node.Pthen;
5287
5288       --  Come here to match the next pattern element
5289
5290       --    Cursor        current position in subject string
5291       --    Node          pointer to node to be matched
5292       --    Stack_Base    current stack base
5293       --    Stack_Ptr     current stack pointer
5294
5295       <<Match>>
5296
5297       --------------------------------------------------
5298       -- Main Pattern Match Element Matching Routines --
5299       --------------------------------------------------
5300
5301       --  Here is the case statement that processes the current node. The
5302       --  processing for each element does one of five things:
5303
5304       --    goto Succeed        to move to the successor
5305       --    goto Match_Succeed  if the entire match succeeds
5306       --    goto Match_Fail     if the entire match fails
5307       --    goto Fail           to signal failure of current match
5308
5309       --  Processing is NOT allowed to fall through
5310
5311       case Node.Pcode is
5312
5313          --  Cancel
5314
5315          when PC_Cancel =>
5316             Dout (Img (Node) & "matching Cancel");
5317             goto Match_Fail;
5318
5319          --  Alternation
5320
5321          when PC_Alt =>
5322             Dout
5323               (Img (Node) & "setting up alternative " & Img (Node.Alt));
5324             Push (Node.Alt);
5325             Node := Node.Pthen;
5326             goto Match;
5327
5328          --  Any (one character case)
5329
5330          when PC_Any_CH =>
5331             Dout (Img (Node) & "matching Any", Node.Char);
5332
5333             if Cursor < Length
5334               and then Subject (Cursor + 1) = Node.Char
5335             then
5336                Cursor := Cursor + 1;
5337                goto Succeed;
5338             else
5339                goto Fail;
5340             end if;
5341
5342          --  Any (character set case)
5343
5344          when PC_Any_CS =>
5345             Dout (Img (Node) & "matching Any", Node.CS);
5346
5347             if Cursor < Length
5348               and then Is_In (Subject (Cursor + 1), Node.CS)
5349             then
5350                Cursor := Cursor + 1;
5351                goto Succeed;
5352             else
5353                goto Fail;
5354             end if;
5355
5356          --  Any (string function case)
5357
5358          when PC_Any_VF => declare
5359             U : constant VString := Node.VF.all;
5360             S : String_Access;
5361             L : Natural;
5362
5363          begin
5364             Get_String (U, S, L);
5365
5366             Dout (Img (Node) & "matching Any", S (1 .. L));
5367
5368             if Cursor < Length
5369               and then Is_In (Subject (Cursor + 1), S (1 .. L))
5370             then
5371                Cursor := Cursor + 1;
5372                goto Succeed;
5373             else
5374                goto Fail;
5375             end if;
5376          end;
5377
5378          --  Any (string pointer case)
5379
5380          when PC_Any_VP => declare
5381             U : constant VString := Node.VP.all;
5382             S : String_Access;
5383             L : Natural;
5384
5385          begin
5386             Get_String (U, S, L);
5387             Dout (Img (Node) & "matching Any", S (1 .. L));
5388
5389             if Cursor < Length
5390               and then Is_In (Subject (Cursor + 1), S (1 .. L))
5391             then
5392                Cursor := Cursor + 1;
5393                goto Succeed;
5394             else
5395                goto Fail;
5396             end if;
5397          end;
5398
5399          --  Arb (initial match)
5400
5401          when PC_Arb_X =>
5402             Dout (Img (Node) & "matching Arb");
5403             Push (Node.Alt);
5404             Node := Node.Pthen;
5405             goto Match;
5406
5407          --  Arb (extension)
5408
5409          when PC_Arb_Y  =>
5410             Dout (Img (Node) & "extending Arb");
5411
5412             if Cursor < Length then
5413                Cursor := Cursor + 1;
5414                Push (Node);
5415                goto Succeed;
5416             else
5417                goto Fail;
5418             end if;
5419
5420          --  Arbno_S (simple Arbno initialize). This is the node that
5421          --  initiates the match of a simple Arbno structure.
5422
5423          when PC_Arbno_S =>
5424             Dout (Img (Node) &
5425                   "setting up Arbno alternative " & Img (Node.Alt));
5426             Push (Node.Alt);
5427             Node := Node.Pthen;
5428             goto Match;
5429
5430          --  Arbno_X (Arbno initialize). This is the node that initiates
5431          --  the match of a complex Arbno structure.
5432
5433          when PC_Arbno_X =>
5434             Dout (Img (Node) &
5435                   "setting up Arbno alternative " & Img (Node.Alt));
5436             Push (Node.Alt);
5437             Node := Node.Pthen;
5438             goto Match;
5439
5440          --  Arbno_Y (Arbno rematch). This is the node that is executed
5441          --  following successful matching of one instance of a complex
5442          --  Arbno pattern.
5443
5444          when PC_Arbno_Y => declare
5445             Null_Match : constant Boolean :=
5446                            Cursor = Stack (Stack_Base - 1).Cursor;
5447
5448          begin
5449             Dout (Img (Node) & "extending Arbno");
5450             Pop_Region;
5451
5452             --  If arbno extension matched null, then immediately fail
5453
5454             if Null_Match then
5455                Dout ("Arbno extension matched null, so fails");
5456                goto Fail;
5457             end if;
5458
5459             --  Here we must do a stack check to make sure enough stack
5460             --  is left. This check will happen once for each instance of
5461             --  the Arbno pattern that is matched. The Nat field of a
5462             --  PC_Arbno pattern contains the maximum stack entries needed
5463             --  for the Arbno with one instance and the successor pattern
5464
5465             if Stack_Ptr + Node.Nat >= Stack'Last then
5466                raise Pattern_Stack_Overflow;
5467             end if;
5468
5469             goto Succeed;
5470          end;
5471
5472          --  Assign. If this node is executed, it means the assign-on-match
5473          --  or write-on-match operation will not happen after all, so we
5474          --  is propagate the failure, removing the PC_Assign node.
5475
5476          when PC_Assign =>
5477             Dout (Img (Node) & "deferred assign/write cancelled");
5478             goto Fail;
5479
5480          --  Assign immediate. This node performs the actual assignment.
5481
5482          when PC_Assign_Imm =>
5483             Dout
5484               (Img (Node) & "executing immediate assignment of " &
5485                Image (Subject (Stack (Stack_Base - 1).Cursor + 1 .. Cursor)));
5486             Set_String
5487               (Node.VP.all,
5488                Subject (Stack (Stack_Base - 1).Cursor + 1 .. Cursor));
5489             Pop_Region;
5490             goto Succeed;
5491
5492          --  Assign on match. This node sets up for the eventual assignment
5493
5494          when PC_Assign_OnM =>
5495             Dout (Img (Node) & "registering deferred assignment");
5496             Stack (Stack_Base - 1).Node := Node;
5497             Push (CP_Assign'Access);
5498             Pop_Region;
5499             Assign_OnM := True;
5500             goto Succeed;
5501
5502          --  Bal
5503
5504          when PC_Bal =>
5505             Dout (Img (Node) & "matching or extending Bal");
5506             if Cursor >= Length or else Subject (Cursor + 1) = ')' then
5507                goto Fail;
5508
5509             elsif Subject (Cursor + 1) = '(' then
5510                declare
5511                   Paren_Count : Natural := 1;
5512
5513                begin
5514                   loop
5515                      Cursor := Cursor + 1;
5516
5517                      if Cursor >= Length then
5518                         goto Fail;
5519
5520                      elsif Subject (Cursor + 1) = '(' then
5521                         Paren_Count := Paren_Count + 1;
5522
5523                      elsif Subject (Cursor + 1) = ')' then
5524                         Paren_Count := Paren_Count - 1;
5525                         exit when Paren_Count = 0;
5526                      end if;
5527                   end loop;
5528                end;
5529             end if;
5530
5531             Cursor := Cursor + 1;
5532             Push (Node);
5533             goto Succeed;
5534
5535          --  Break (one character case)
5536
5537          when PC_Break_CH =>
5538             Dout (Img (Node) & "matching Break", Node.Char);
5539
5540             while Cursor < Length loop
5541                if Subject (Cursor + 1) = Node.Char then
5542                   goto Succeed;
5543                else
5544                   Cursor := Cursor + 1;
5545                end if;
5546             end loop;
5547
5548             goto Fail;
5549
5550          --  Break (character set case)
5551
5552          when PC_Break_CS =>
5553             Dout (Img (Node) & "matching Break", Node.CS);
5554
5555             while Cursor < Length loop
5556                if Is_In (Subject (Cursor + 1), Node.CS) then
5557                   goto Succeed;
5558                else
5559                   Cursor := Cursor + 1;
5560                end if;
5561             end loop;
5562
5563             goto Fail;
5564
5565          --  Break (string function case)
5566
5567          when PC_Break_VF => declare
5568             U : constant VString := Node.VF.all;
5569             S : String_Access;
5570             L : Natural;
5571
5572          begin
5573             Get_String (U, S, L);
5574             Dout (Img (Node) & "matching Break", S (1 .. L));
5575
5576             while Cursor < Length loop
5577                if Is_In (Subject (Cursor + 1), S (1 .. L)) then
5578                   goto Succeed;
5579                else
5580                   Cursor := Cursor + 1;
5581                end if;
5582             end loop;
5583
5584             goto Fail;
5585          end;
5586
5587          --  Break (string pointer case)
5588
5589          when PC_Break_VP => declare
5590             U : constant VString := Node.VP.all;
5591             S : String_Access;
5592             L : Natural;
5593
5594          begin
5595             Get_String (U, S, L);
5596             Dout (Img (Node) & "matching Break", S (1 .. L));
5597
5598             while Cursor < Length loop
5599                if Is_In (Subject (Cursor + 1), S (1 .. L)) then
5600                   goto Succeed;
5601                else
5602                   Cursor := Cursor + 1;
5603                end if;
5604             end loop;
5605
5606             goto Fail;
5607          end;
5608
5609          --  BreakX (one character case)
5610
5611          when PC_BreakX_CH =>
5612             Dout (Img (Node) & "matching BreakX", Node.Char);
5613
5614             while Cursor < Length loop
5615                if Subject (Cursor + 1) = Node.Char then
5616                   goto Succeed;
5617                else
5618                   Cursor := Cursor + 1;
5619                end if;
5620             end loop;
5621
5622             goto Fail;
5623
5624          --  BreakX (character set case)
5625
5626          when PC_BreakX_CS =>
5627             Dout (Img (Node) & "matching BreakX", Node.CS);
5628
5629             while Cursor < Length loop
5630                if Is_In (Subject (Cursor + 1), Node.CS) then
5631                   goto Succeed;
5632                else
5633                   Cursor := Cursor + 1;
5634                end if;
5635             end loop;
5636
5637             goto Fail;
5638
5639          --  BreakX (string function case)
5640
5641          when PC_BreakX_VF => declare
5642             U : constant VString := Node.VF.all;
5643             S : String_Access;
5644             L : Natural;
5645
5646          begin
5647             Get_String (U, S, L);
5648             Dout (Img (Node) & "matching BreakX", S (1 .. L));
5649
5650             while Cursor < Length loop
5651                if Is_In (Subject (Cursor + 1), S (1 .. L)) then
5652                   goto Succeed;
5653                else
5654                   Cursor := Cursor + 1;
5655                end if;
5656             end loop;
5657
5658             goto Fail;
5659          end;
5660
5661          --  BreakX (string pointer case)
5662
5663          when PC_BreakX_VP => declare
5664             U : constant VString := Node.VP.all;
5665             S : String_Access;
5666             L : Natural;
5667
5668          begin
5669             Get_String (U, S, L);
5670             Dout (Img (Node) & "matching BreakX", S (1 .. L));
5671
5672             while Cursor < Length loop
5673                if Is_In (Subject (Cursor + 1), S (1 .. L)) then
5674                   goto Succeed;
5675                else
5676                   Cursor := Cursor + 1;
5677                end if;
5678             end loop;
5679
5680             goto Fail;
5681          end;
5682
5683          --  BreakX_X (BreakX extension). See section on "Compound Pattern
5684          --  Structures". This node is the alternative that is stacked
5685          --  to skip past the break character and extend the break.
5686
5687          when PC_BreakX_X =>
5688             Dout (Img (Node) & "extending BreakX");
5689             Cursor := Cursor + 1;
5690             goto Succeed;
5691
5692          --  Character (one character string)
5693
5694          when PC_Char =>
5695             Dout (Img (Node) & "matching '" & Node.Char & ''');
5696
5697             if Cursor < Length
5698               and then Subject (Cursor + 1) = Node.Char
5699             then
5700                Cursor := Cursor + 1;
5701                goto Succeed;
5702             else
5703                goto Fail;
5704             end if;
5705
5706          --  End of Pattern
5707
5708          when PC_EOP =>
5709             if Stack_Base = Stack_Init then
5710                Dout ("end of pattern");
5711                goto Match_Succeed;
5712
5713             --  End of recursive inner match. See separate section on
5714             --  handing of recursive pattern matches for details.
5715
5716             else
5717                Dout ("terminating recursive match");
5718                Node := Stack (Stack_Base - 1).Node;
5719                Pop_Region;
5720                goto Match;
5721             end if;
5722
5723          --  Fail
5724
5725          when PC_Fail =>
5726             Dout (Img (Node) & "matching Fail");
5727             goto Fail;
5728
5729          --  Fence (built in pattern)
5730
5731          when PC_Fence =>
5732             Dout (Img (Node) & "matching Fence");
5733             Push (CP_Cancel'Access);
5734             goto Succeed;
5735
5736          --  Fence function node X. This is the node that gets control
5737          --  after a successful match of the fenced pattern.
5738
5739          when PC_Fence_X =>
5740             Dout (Img (Node) & "matching Fence function");
5741             Stack_Ptr := Stack_Ptr + 1;
5742             Stack (Stack_Ptr).Cursor := Stack_Base;
5743             Stack (Stack_Ptr).Node   := CP_Fence_Y'Access;
5744             Stack_Base := Stack (Stack_Base).Cursor;
5745             Region_Level := Region_Level - 1;
5746             goto Succeed;
5747
5748          --  Fence function node Y. This is the node that gets control on
5749          --  a failure that occurs after the fenced pattern has matched.
5750
5751          --  Note: the Cursor at this stage is actually the inner stack
5752          --  base value. We don't reset this, but we do use it to strip
5753          --  off all the entries made by the fenced pattern.
5754
5755          when PC_Fence_Y =>
5756             Dout (Img (Node) & "pattern matched by Fence caused failure");
5757             Stack_Ptr := Cursor - 2;
5758             goto Fail;
5759
5760          --  Len (integer case)
5761
5762          when PC_Len_Nat =>
5763             Dout (Img (Node) & "matching Len", Node.Nat);
5764
5765             if Cursor + Node.Nat > Length then
5766                goto Fail;
5767             else
5768                Cursor := Cursor + Node.Nat;
5769                goto Succeed;
5770             end if;
5771
5772          --  Len (Integer function case)
5773
5774          when PC_Len_NF => declare
5775             N : constant Natural := Node.NF.all;
5776
5777          begin
5778             Dout (Img (Node) & "matching Len", N);
5779
5780             if Cursor + N > Length then
5781                goto Fail;
5782             else
5783                Cursor := Cursor + N;
5784                goto Succeed;
5785             end if;
5786          end;
5787
5788          --  Len (integer pointer case)
5789
5790          when PC_Len_NP =>
5791             Dout (Img (Node) & "matching Len", Node.NP.all);
5792
5793             if Cursor + Node.NP.all > Length then
5794                goto Fail;
5795             else
5796                Cursor := Cursor + Node.NP.all;
5797                goto Succeed;
5798             end if;
5799
5800          --  NotAny (one character case)
5801
5802          when PC_NotAny_CH =>
5803             Dout (Img (Node) & "matching NotAny", Node.Char);
5804
5805             if Cursor < Length
5806               and then Subject (Cursor + 1) /= Node.Char
5807             then
5808                Cursor := Cursor + 1;
5809                goto Succeed;
5810             else
5811                goto Fail;
5812             end if;
5813
5814          --  NotAny (character set case)
5815
5816          when PC_NotAny_CS =>
5817             Dout (Img (Node) & "matching NotAny", Node.CS);
5818
5819             if Cursor < Length
5820               and then not Is_In (Subject (Cursor + 1), Node.CS)
5821             then
5822                Cursor := Cursor + 1;
5823                goto Succeed;
5824             else
5825                goto Fail;
5826             end if;
5827
5828          --  NotAny (string function case)
5829
5830          when PC_NotAny_VF => declare
5831             U : constant VString := Node.VF.all;
5832             S : String_Access;
5833             L : Natural;
5834
5835          begin
5836             Get_String (U, S, L);
5837             Dout (Img (Node) & "matching NotAny", S (1 .. L));
5838
5839             if Cursor < Length
5840               and then
5841                 not Is_In (Subject (Cursor + 1), S (1 .. L))
5842             then
5843                Cursor := Cursor + 1;
5844                goto Succeed;
5845             else
5846                goto Fail;
5847             end if;
5848          end;
5849
5850          --  NotAny (string pointer case)
5851
5852          when PC_NotAny_VP => declare
5853             U : constant VString := Node.VP.all;
5854             S : String_Access;
5855             L : Natural;
5856
5857          begin
5858             Get_String (U, S, L);
5859             Dout (Img (Node) & "matching NotAny", S (1 .. L));
5860
5861             if Cursor < Length
5862               and then
5863                 not Is_In (Subject (Cursor + 1), S (1 .. L))
5864             then
5865                Cursor := Cursor + 1;
5866                goto Succeed;
5867             else
5868                goto Fail;
5869             end if;
5870          end;
5871
5872          --  NSpan (one character case)
5873
5874          when PC_NSpan_CH =>
5875             Dout (Img (Node) & "matching NSpan", Node.Char);
5876
5877             while Cursor < Length
5878               and then Subject (Cursor + 1) = Node.Char
5879             loop
5880                Cursor := Cursor + 1;
5881             end loop;
5882
5883             goto Succeed;
5884
5885          --  NSpan (character set case)
5886
5887          when PC_NSpan_CS =>
5888             Dout (Img (Node) & "matching NSpan", Node.CS);
5889
5890             while Cursor < Length
5891               and then Is_In (Subject (Cursor + 1), Node.CS)
5892             loop
5893                Cursor := Cursor + 1;
5894             end loop;
5895
5896             goto Succeed;
5897
5898          --  NSpan (string function case)
5899
5900          when PC_NSpan_VF => declare
5901             U : constant VString := Node.VF.all;
5902             S : String_Access;
5903             L : Natural;
5904
5905          begin
5906             Get_String (U, S, L);
5907             Dout (Img (Node) & "matching NSpan", S (1 .. L));
5908
5909             while Cursor < Length
5910               and then Is_In (Subject (Cursor + 1), S (1 .. L))
5911             loop
5912                Cursor := Cursor + 1;
5913             end loop;
5914
5915             goto Succeed;
5916          end;
5917
5918          --  NSpan (string pointer case)
5919
5920          when PC_NSpan_VP => declare
5921             U : constant VString := Node.VP.all;
5922             S : String_Access;
5923             L : Natural;
5924
5925          begin
5926             Get_String (U, S, L);
5927             Dout (Img (Node) & "matching NSpan", S (1 .. L));
5928
5929             while Cursor < Length
5930               and then Is_In (Subject (Cursor + 1), S (1 .. L))
5931             loop
5932                Cursor := Cursor + 1;
5933             end loop;
5934
5935             goto Succeed;
5936          end;
5937
5938          when PC_Null =>
5939             Dout (Img (Node) & "matching null");
5940             goto Succeed;
5941
5942          --  Pos (integer case)
5943
5944          when PC_Pos_Nat =>
5945             Dout (Img (Node) & "matching Pos", Node.Nat);
5946
5947             if Cursor = Node.Nat then
5948                goto Succeed;
5949             else
5950                goto Fail;
5951             end if;
5952
5953          --  Pos (Integer function case)
5954
5955          when PC_Pos_NF => declare
5956             N : constant Natural := Node.NF.all;
5957
5958          begin
5959             Dout (Img (Node) & "matching Pos", N);
5960
5961             if Cursor = N then
5962                goto Succeed;
5963             else
5964                goto Fail;
5965             end if;
5966          end;
5967
5968          --  Pos (integer pointer case)
5969
5970          when PC_Pos_NP =>
5971             Dout (Img (Node) & "matching Pos", Node.NP.all);
5972
5973             if Cursor = Node.NP.all then
5974                goto Succeed;
5975             else
5976                goto Fail;
5977             end if;
5978
5979          --  Predicate function
5980
5981          when PC_Pred_Func =>
5982             Dout (Img (Node) & "matching predicate function");
5983
5984             if Node.BF.all then
5985                goto Succeed;
5986             else
5987                goto Fail;
5988             end if;
5989
5990          --  Region Enter. Initiate new pattern history stack region
5991
5992          when PC_R_Enter =>
5993             Dout (Img (Node) & "starting match of nested pattern");
5994             Stack (Stack_Ptr + 1).Cursor := Cursor;
5995             Push_Region;
5996             goto Succeed;
5997
5998          --  Region Remove node. This is the node stacked by an R_Enter.
5999          --  It removes the special format stack entry right underneath, and
6000          --  then restores the outer level stack base and signals failure.
6001
6002          --  Note: the cursor value at this stage is actually the (negative)
6003          --  stack base value for the outer level.
6004
6005          when PC_R_Remove =>
6006             Dout ("failure, match of nested pattern terminated");
6007             Stack_Base := Cursor;
6008             Region_Level := Region_Level - 1;
6009             Stack_Ptr := Stack_Ptr - 1;
6010             goto Fail;
6011
6012          --  Region restore node. This is the node stacked at the end of an
6013          --  inner level match. Its function is to restore the inner level
6014          --  region, so that alternatives in this region can be sought.
6015
6016          --  Note: the Cursor at this stage is actually the negative of the
6017          --  inner stack base value, which we use to restore the inner region.
6018
6019          when PC_R_Restore =>
6020             Dout ("failure, search for alternatives in nested pattern");
6021             Region_Level := Region_Level + 1;
6022             Stack_Base := Cursor;
6023             goto Fail;
6024
6025          --  Rest
6026
6027          when PC_Rest =>
6028             Dout (Img (Node) & "matching Rest");
6029             Cursor := Length;
6030             goto Succeed;
6031
6032          --  Initiate recursive match (pattern pointer case)
6033
6034          when PC_Rpat =>
6035             Stack (Stack_Ptr + 1).Node := Node.Pthen;
6036             Push_Region;
6037             Dout (Img (Node) & "initiating recursive match");
6038
6039             if Stack_Ptr + Node.PP.all.Stk >= Stack_Size then
6040                raise Pattern_Stack_Overflow;
6041             else
6042                Node := Node.PP.all.P;
6043                goto Match;
6044             end if;
6045
6046          --  RPos (integer case)
6047
6048          when PC_RPos_Nat =>
6049             Dout (Img (Node) & "matching RPos", Node.Nat);
6050
6051             if Cursor = (Length - Node.Nat) then
6052                goto Succeed;
6053             else
6054                goto Fail;
6055             end if;
6056
6057          --  RPos (integer function case)
6058
6059          when PC_RPos_NF => declare
6060             N : constant Natural := Node.NF.all;
6061
6062          begin
6063             Dout (Img (Node) & "matching RPos", N);
6064
6065             if Length - Cursor = N then
6066                goto Succeed;
6067             else
6068                goto Fail;
6069             end if;
6070          end;
6071
6072          --  RPos (integer pointer case)
6073
6074          when PC_RPos_NP =>
6075             Dout (Img (Node) & "matching RPos", Node.NP.all);
6076
6077             if Cursor = (Length - Node.NP.all) then
6078                goto Succeed;
6079             else
6080                goto Fail;
6081             end if;
6082
6083          --  RTab (integer case)
6084
6085          when PC_RTab_Nat =>
6086             Dout (Img (Node) & "matching RTab", Node.Nat);
6087
6088             if Cursor <= (Length - Node.Nat) then
6089                Cursor := Length - Node.Nat;
6090                goto Succeed;
6091             else
6092                goto Fail;
6093             end if;
6094
6095          --  RTab (integer function case)
6096
6097          when PC_RTab_NF => declare
6098             N : constant Natural := Node.NF.all;
6099
6100          begin
6101             Dout (Img (Node) & "matching RPos", N);
6102
6103             if Length - Cursor >= N then
6104                Cursor := Length - N;
6105                goto Succeed;
6106             else
6107                goto Fail;
6108             end if;
6109          end;
6110
6111          --  RTab (integer pointer case)
6112
6113          when PC_RTab_NP =>
6114             Dout (Img (Node) & "matching RPos", Node.NP.all);
6115
6116             if Cursor <= (Length - Node.NP.all) then
6117                Cursor := Length - Node.NP.all;
6118                goto Succeed;
6119             else
6120                goto Fail;
6121             end if;
6122
6123          --  Cursor assignment
6124
6125          when PC_Setcur =>
6126             Dout (Img (Node) & "matching Setcur");
6127             Node.Var.all := Cursor;
6128             goto Succeed;
6129
6130          --  Span (one character case)
6131
6132          when PC_Span_CH => declare
6133             P : Natural := Cursor;
6134
6135          begin
6136             Dout (Img (Node) & "matching Span", Node.Char);
6137
6138             while P < Length
6139               and then Subject (P + 1) = Node.Char
6140             loop
6141                P := P + 1;
6142             end loop;
6143
6144             if P /= Cursor then
6145                Cursor := P;
6146                goto Succeed;
6147             else
6148                goto Fail;
6149             end if;
6150          end;
6151
6152          --  Span (character set case)
6153
6154          when PC_Span_CS => declare
6155             P : Natural := Cursor;
6156
6157          begin
6158             Dout (Img (Node) & "matching Span", Node.CS);
6159
6160             while P < Length
6161               and then Is_In (Subject (P + 1), Node.CS)
6162             loop
6163                P := P + 1;
6164             end loop;
6165
6166             if P /= Cursor then
6167                Cursor := P;
6168                goto Succeed;
6169             else
6170                goto Fail;
6171             end if;
6172          end;
6173
6174          --  Span (string function case)
6175
6176          when PC_Span_VF => declare
6177             U : constant VString := Node.VF.all;
6178             S : String_Access;
6179             L : Natural;
6180             P : Natural;
6181
6182          begin
6183             Get_String (U, S, L);
6184             Dout (Img (Node) & "matching Span", S (1 .. L));
6185
6186             P := Cursor;
6187             while P < Length
6188               and then Is_In (Subject (P + 1), S (1 .. L))
6189             loop
6190                P := P + 1;
6191             end loop;
6192
6193             if P /= Cursor then
6194                Cursor := P;
6195                goto Succeed;
6196             else
6197                goto Fail;
6198             end if;
6199          end;
6200
6201          --  Span (string pointer case)
6202
6203          when PC_Span_VP => declare
6204             U : constant VString := Node.VP.all;
6205             S : String_Access;
6206             L : Natural;
6207             P : Natural;
6208
6209          begin
6210             Get_String (U, S, L);
6211             Dout (Img (Node) & "matching Span", S (1 .. L));
6212
6213             P := Cursor;
6214             while P < Length
6215               and then Is_In (Subject (P + 1), S (1 .. L))
6216             loop
6217                P := P + 1;
6218             end loop;
6219
6220             if P /= Cursor then
6221                Cursor := P;
6222                goto Succeed;
6223             else
6224                goto Fail;
6225             end if;
6226          end;
6227
6228          --  String (two character case)
6229
6230          when PC_String_2 =>
6231             Dout (Img (Node) & "matching " & Image (Node.Str2));
6232
6233             if (Length - Cursor) >= 2
6234               and then Subject (Cursor + 1 .. Cursor + 2) = Node.Str2
6235             then
6236                Cursor := Cursor + 2;
6237                goto Succeed;
6238             else
6239                goto Fail;
6240             end if;
6241
6242          --  String (three character case)
6243
6244          when PC_String_3 =>
6245             Dout (Img (Node) & "matching " & Image (Node.Str3));
6246
6247             if (Length - Cursor) >= 3
6248               and then Subject (Cursor + 1 .. Cursor + 3) = Node.Str3
6249             then
6250                Cursor := Cursor + 3;
6251                goto Succeed;
6252             else
6253                goto Fail;
6254             end if;
6255
6256          --  String (four character case)
6257
6258          when PC_String_4 =>
6259             Dout (Img (Node) & "matching " & Image (Node.Str4));
6260
6261             if (Length - Cursor) >= 4
6262               and then Subject (Cursor + 1 .. Cursor + 4) = Node.Str4
6263             then
6264                Cursor := Cursor + 4;
6265                goto Succeed;
6266             else
6267                goto Fail;
6268             end if;
6269
6270          --  String (five character case)
6271
6272          when PC_String_5 =>
6273             Dout (Img (Node) & "matching " & Image (Node.Str5));
6274
6275             if (Length - Cursor) >= 5
6276               and then Subject (Cursor + 1 .. Cursor + 5) = Node.Str5
6277             then
6278                Cursor := Cursor + 5;
6279                goto Succeed;
6280             else
6281                goto Fail;
6282             end if;
6283
6284          --  String (six character case)
6285
6286          when PC_String_6 =>
6287             Dout (Img (Node) & "matching " & Image (Node.Str6));
6288
6289             if (Length - Cursor) >= 6
6290               and then Subject (Cursor + 1 .. Cursor + 6) = Node.Str6
6291             then
6292                Cursor := Cursor + 6;
6293                goto Succeed;
6294             else
6295                goto Fail;
6296             end if;
6297
6298          --  String (case of more than six characters)
6299
6300          when PC_String => declare
6301             Len : constant Natural := Node.Str'Length;
6302
6303          begin
6304             Dout (Img (Node) & "matching " & Image (Node.Str.all));
6305
6306             if (Length - Cursor) >= Len
6307               and then Node.Str.all = Subject (Cursor + 1 .. Cursor + Len)
6308             then
6309                Cursor := Cursor + Len;
6310                goto Succeed;
6311             else
6312                goto Fail;
6313             end if;
6314          end;
6315
6316          --  String (function case)
6317
6318          when PC_String_VF => declare
6319             U : constant VString := Node.VF.all;
6320             S : String_Access;
6321             L : Natural;
6322
6323          begin
6324             Get_String (U, S, L);
6325             Dout (Img (Node) & "matching " & Image (S (1 .. L)));
6326
6327             if (Length - Cursor) >= L
6328               and then S (1 .. L) = Subject (Cursor + 1 .. Cursor + L)
6329             then
6330                Cursor := Cursor + L;
6331                goto Succeed;
6332             else
6333                goto Fail;
6334             end if;
6335          end;
6336
6337          --  String (vstring pointer case)
6338
6339          when PC_String_VP => declare
6340             U : constant VString := Node.VP.all;
6341             S : String_Access;
6342             L : Natural;
6343
6344          begin
6345             Get_String (U, S, L);
6346             Dout (Img (Node) & "matching " & Image (S (1 .. L)));
6347
6348             if (Length - Cursor) >= L
6349               and then S (1 .. L) = Subject (Cursor + 1 .. Cursor + L)
6350             then
6351                Cursor := Cursor + L;
6352                goto Succeed;
6353             else
6354                goto Fail;
6355             end if;
6356          end;
6357
6358          --  Succeed
6359
6360          when PC_Succeed =>
6361             Dout (Img (Node) & "matching Succeed");
6362             Push (Node);
6363             goto Succeed;
6364
6365          --  Tab (integer case)
6366
6367          when PC_Tab_Nat =>
6368             Dout (Img (Node) & "matching Tab", Node.Nat);
6369
6370             if Cursor <= Node.Nat then
6371                Cursor := Node.Nat;
6372                goto Succeed;
6373             else
6374                goto Fail;
6375             end if;
6376
6377          --  Tab (integer function case)
6378
6379          when PC_Tab_NF => declare
6380             N : constant Natural := Node.NF.all;
6381
6382          begin
6383             Dout (Img (Node) & "matching Tab ", N);
6384
6385             if Cursor <= N then
6386                Cursor := N;
6387                goto Succeed;
6388             else
6389                goto Fail;
6390             end if;
6391          end;
6392
6393          --  Tab (integer pointer case)
6394
6395          when PC_Tab_NP =>
6396             Dout (Img (Node) & "matching Tab ", Node.NP.all);
6397
6398             if Cursor <= Node.NP.all then
6399                Cursor := Node.NP.all;
6400                goto Succeed;
6401             else
6402                goto Fail;
6403             end if;
6404
6405          --  Unanchored movement
6406
6407          when PC_Unanchored =>
6408             Dout ("attempting to move anchor point");
6409
6410             --  All done if we tried every position
6411
6412             if Cursor > Length then
6413                goto Match_Fail;
6414
6415             --  Otherwise extend the anchor point, and restack ourself
6416
6417             else
6418                Cursor := Cursor + 1;
6419                Push (Node);
6420                goto Succeed;
6421             end if;
6422
6423          --  Write immediate. This node performs the actual write
6424
6425          when PC_Write_Imm =>
6426             Dout (Img (Node) & "executing immediate write of " &
6427                    Subject (Stack (Stack_Base - 1).Cursor + 1 .. Cursor));
6428
6429             Put_Line
6430               (Node.FP.all,
6431                Subject (Stack (Stack_Base - 1).Cursor + 1 .. Cursor));
6432             Pop_Region;
6433             goto Succeed;
6434
6435          --  Write on match. This node sets up for the eventual write
6436
6437          when PC_Write_OnM =>
6438             Dout (Img (Node) & "registering deferred write");
6439             Stack (Stack_Base - 1).Node := Node;
6440             Push (CP_Assign'Access);
6441             Pop_Region;
6442             Assign_OnM := True;
6443             goto Succeed;
6444
6445       end case;
6446
6447       --  We are NOT allowed to fall though this case statement, since every
6448       --  match routine must end by executing a goto to the appropriate point
6449       --  in the finite state machine model.
6450
6451       pragma Warnings (Off);
6452       Logic_Error;
6453       pragma Warnings (On);
6454    end XMatchD;
6455
6456 end GNAT.Spitbol.Patterns;