OSDN Git Service

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