PR c/79855: add full stop to store merging param descriptions
[official-gcc.git] / gcc / ada / g-spipat.adb
blob348c8e4e00a078ecd9d8d7b51a87c39dbaa404bd
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-2016, 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 ------------------------------------------------------------------------------
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.
37 with Ada.Strings.Unbounded.Aux; use Ada.Strings.Unbounded.Aux;
39 with GNAT.Debug_Utilities; use GNAT.Debug_Utilities;
41 with System; use System;
43 with Ada.Unchecked_Conversion;
44 with Ada.Unchecked_Deallocation;
46 package body GNAT.Spitbol.Patterns is
48 ------------------------
49 -- Internal Debugging --
50 ------------------------
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.
56 procedure New_LineD;
57 pragma Inline (New_LineD);
58 -- Output new blank line with New_Line if Internal_Debug is True
60 procedure PutD (Str : String);
61 pragma Inline (PutD);
62 -- Output string with Put if Internal_Debug is True
64 procedure Put_LineD (Str : String);
65 pragma Inline (Put_LineD);
66 -- Output string with Put_Line if Internal_Debug is True
68 -----------------------------
69 -- Local Type Declarations --
70 -----------------------------
72 subtype String_Ptr is Ada.Strings.Unbounded.String_Access;
73 subtype File_Ptr is Ada.Text_IO.File_Access;
75 function To_Address is new Ada.Unchecked_Conversion (PE_Ptr, Address);
76 -- Used only for debugging output purposes
78 subtype AFC is Ada.Finalization.Controlled;
80 N : constant PE_Ptr := null;
81 -- Shorthand used to initialize Copy fields to null
83 type Natural_Ptr is access all Natural;
84 type Pattern_Ptr is access all Pattern;
86 --------------------------------------------------
87 -- Description of Algorithm and Data Structures --
88 --------------------------------------------------
90 -- A pattern structure is represented as a linked graph of nodes
91 -- with the following structure:
93 -- +------------------------------------+
94 -- I Pcode I
95 -- +------------------------------------+
96 -- I Index I
97 -- +------------------------------------+
98 -- I Pthen I
99 -- +------------------------------------+
100 -- I parameter(s) I
101 -- +------------------------------------+
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.
108 -- Index is a serial index number. The use of these serial index
109 -- numbers is described in a separate section.
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.
116 -- The parameter or parameters are present for certain node types,
117 -- and the type varies with the pattern code.
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,
137 PC_Alt,
138 PC_Arb_X,
139 PC_Arbno_S,
140 PC_Arbno_X,
142 PC_Rpat,
144 PC_Pred_Func,
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,
156 PC_Write_Imm,
157 PC_Write_OnM,
159 PC_Null,
160 PC_String,
162 PC_String_2,
163 PC_String_3,
164 PC_String_4,
165 PC_String_5,
166 PC_String_6,
168 PC_Setcur,
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,
178 PC_Any_CS,
179 PC_Break_CS,
180 PC_BreakX_CS,
181 PC_NotAny_CS,
182 PC_NSpan_CS,
183 PC_Span_CS,
185 PC_Arbno_Y,
186 PC_Len_Nat,
187 PC_Pos_Nat,
188 PC_RPos_Nat,
189 PC_RTab_Nat,
190 PC_Tab_Nat,
192 PC_Pos_NF,
193 PC_Len_NF,
194 PC_RPos_NF,
195 PC_RTab_NF,
196 PC_Tab_NF,
198 PC_Pos_NP,
199 PC_Len_NP,
200 PC_RPos_NP,
201 PC_RTab_NP,
202 PC_Tab_NP,
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);
212 type IndexT is range 0 .. +(2 **15 - 1);
214 type PE (Pcode : Pattern_Code) is record
216 Index : IndexT;
217 -- Serial index number of pattern element within pattern
219 Pthen : PE_Ptr;
220 -- Successor element, to be matched after this one
222 case Pcode is
223 when PC_Arb_Y
224 | PC_Assign
225 | PC_Bal
226 | PC_BreakX_X
227 | PC_Cancel
228 | PC_EOP
229 | PC_Fail
230 | PC_Fence
231 | PC_Fence_X
232 | PC_Fence_Y
233 | PC_Null
234 | PC_R_Enter
235 | PC_R_Remove
236 | PC_R_Restore
237 | PC_Rest
238 | PC_Succeed
239 | PC_Unanchored
241 null;
243 when PC_Alt
244 | PC_Arb_X
245 | PC_Arbno_S
246 | PC_Arbno_X
248 Alt : PE_Ptr;
250 when PC_Rpat =>
251 PP : Pattern_Ptr;
253 when PC_Pred_Func =>
254 BF : Boolean_Func;
256 when PC_Assign_Imm
257 | PC_Assign_OnM
258 | PC_Any_VP
259 | PC_Break_VP
260 | PC_BreakX_VP
261 | PC_NotAny_VP
262 | PC_NSpan_VP
263 | PC_Span_VP
264 | PC_String_VP
266 VP : VString_Ptr;
268 when PC_Write_Imm
269 | PC_Write_OnM
271 FP : File_Ptr;
273 when PC_String =>
274 Str : String_Ptr;
276 when PC_String_2 =>
277 Str2 : String (1 .. 2);
279 when PC_String_3 =>
280 Str3 : String (1 .. 3);
282 when PC_String_4 =>
283 Str4 : String (1 .. 4);
285 when PC_String_5 =>
286 Str5 : String (1 .. 5);
288 when PC_String_6 =>
289 Str6 : String (1 .. 6);
291 when PC_Setcur =>
292 Var : Natural_Ptr;
294 when PC_Any_CH
295 | PC_Break_CH
296 | PC_BreakX_CH
297 | PC_Char
298 | PC_NotAny_CH
299 | PC_NSpan_CH
300 | PC_Span_CH
302 Char : Character;
304 when PC_Any_CS
305 | PC_Break_CS
306 | PC_BreakX_CS
307 | PC_NotAny_CS
308 | PC_NSpan_CS
309 | PC_Span_CS
311 CS : Character_Set;
313 when PC_Arbno_Y
314 | PC_Len_Nat
315 | PC_Pos_Nat
316 | PC_RPos_Nat
317 | PC_RTab_Nat
318 | PC_Tab_Nat
320 Nat : Natural;
322 when PC_Pos_NF
323 | PC_Len_NF
324 | PC_RPos_NF
325 | PC_RTab_NF
326 | PC_Tab_NF
328 NF : Natural_Func;
330 when PC_Pos_NP
331 | PC_Len_NP
332 | PC_RPos_NP
333 | PC_RTab_NP
334 | PC_Tab_NP
336 NP : Natural_Ptr;
338 when PC_Any_VF
339 | PC_Break_VF
340 | PC_BreakX_VF
341 | PC_NotAny_VF
342 | PC_NSpan_VF
343 | PC_Span_VF
344 | PC_String_VF
346 VF : VString_Func;
347 end case;
348 end record;
350 subtype PC_Has_Alt is Pattern_Code range PC_Alt .. PC_Arbno_X;
351 -- Range of pattern codes that has an Alt field. This is used in the
352 -- recursive traversals, since these links must be followed.
354 EOP_Element : aliased constant PE := (PC_EOP, 0, N);
355 -- This is the end of pattern element, and is thus the representation of
356 -- a null pattern. It has a zero index element since it is never placed
357 -- inside a pattern. Furthermore it does not need a successor, since it
358 -- marks the end of the pattern, so that no more successors are needed.
360 EOP : constant PE_Ptr := EOP_Element'Unrestricted_Access;
361 -- This is the end of pattern pointer, that is used in the Pthen pointer
362 -- of other nodes to signal end of pattern.
364 -- The following array is used to determine if a pattern used as an
365 -- argument for Arbno is eligible for treatment using the simple Arbno
366 -- structure (i.e. it is a pattern that is guaranteed to match at least
367 -- one character on success, and not to make any entries on the stack.
369 OK_For_Simple_Arbno : constant array (Pattern_Code) of Boolean :=
370 (PC_Any_CS |
371 PC_Any_CH |
372 PC_Any_VF |
373 PC_Any_VP |
374 PC_Char |
375 PC_Len_Nat |
376 PC_NotAny_CS |
377 PC_NotAny_CH |
378 PC_NotAny_VF |
379 PC_NotAny_VP |
380 PC_Span_CS |
381 PC_Span_CH |
382 PC_Span_VF |
383 PC_Span_VP |
384 PC_String |
385 PC_String_2 |
386 PC_String_3 |
387 PC_String_4 |
388 PC_String_5 |
389 PC_String_6 => True,
390 others => False);
392 -------------------------------
393 -- The Pattern History Stack --
394 -------------------------------
396 -- The pattern history stack is used for controlling backtracking when
397 -- a match fails. The idea is to stack entries that give a cursor value
398 -- to be restored, and a node to be reestablished as the current node to
399 -- attempt an appropriate rematch operation. The processing for a pattern
400 -- element that has rematch alternatives pushes an appropriate entry or
401 -- entry on to the stack, and the proceeds. If a match fails at any point,
402 -- the top element of the stack is popped off, resetting the cursor and
403 -- the match continues by accessing the node stored with this entry.
405 type Stack_Entry is record
407 Cursor : Integer;
408 -- Saved cursor value that is restored when this entry is popped
409 -- from the stack if a match attempt fails. Occasionally, this
410 -- field is used to store a history stack pointer instead of a
411 -- cursor. Such cases are noted in the documentation and the value
412 -- stored is negative since stack pointer values are always negative.
414 Node : PE_Ptr;
415 -- This pattern element reference is reestablished as the current
416 -- Node to be matched (which will attempt an appropriate rematch).
418 end record;
420 subtype Stack_Range is Integer range -Stack_Size .. -1;
422 type Stack_Type is array (Stack_Range) of Stack_Entry;
423 -- The type used for a history stack. The actual instance of the stack
424 -- is declared as a local variable in the Match routine, to properly
425 -- handle recursive calls to Match. All stack pointer values are negative
426 -- to distinguish them from normal cursor values.
428 -- Note: the pattern matching stack is used only to handle backtracking.
429 -- If no backtracking occurs, its entries are never accessed, and never
430 -- popped off, and in particular it is normal for a successful match
431 -- to terminate with entries on the stack that are simply discarded.
433 -- Note: in subsequent diagrams of the stack, we always place element
434 -- zero (the deepest element) at the top of the page, then build the
435 -- stack down on the page with the most recent (top of stack) element
436 -- being the bottom-most entry on the page.
438 -- Stack checking is handled by labeling every pattern with the maximum
439 -- number of stack entries that are required, so a single check at the
440 -- start of matching the pattern suffices. There are two exceptions.
442 -- First, the count does not include entries for recursive pattern
443 -- references. Such recursions must therefore perform a specific
444 -- stack check with respect to the number of stack entries required
445 -- by the recursive pattern that is accessed and the amount of stack
446 -- that remains unused.
448 -- Second, the count includes only one iteration of an Arbno pattern,
449 -- so a specific check must be made on subsequent iterations that there
450 -- is still enough stack space left. The Arbno node has a field that
451 -- records the number of stack entries required by its argument for
452 -- this purpose.
454 ---------------------------------------------------
455 -- Use of Serial Index Field in Pattern Elements --
456 ---------------------------------------------------
458 -- The serial index numbers for the pattern elements are assigned as
459 -- a pattern is constructed from its constituent elements. Note that there
460 -- is never any sharing of pattern elements between patterns (copies are
461 -- always made), so the serial index numbers are unique to a particular
462 -- pattern as referenced from the P field of a value of type Pattern.
464 -- The index numbers meet three separate invariants, which are used for
465 -- various purposes as described in this section.
467 -- First, the numbers uniquely identify the pattern elements within a
468 -- pattern. If Num is the number of elements in a given pattern, then
469 -- the serial index numbers for the elements of this pattern will range
470 -- from 1 .. Num, so that each element has a separate value.
472 -- The purpose of this assignment is to provide a convenient auxiliary
473 -- data structure mechanism during operations which must traverse a
474 -- pattern (e.g. copy and finalization processing). Once constructed
475 -- patterns are strictly read only. This is necessary to allow sharing
476 -- of patterns between tasks. This means that we cannot go marking the
477 -- pattern (e.g. with a visited bit). Instead we construct a separate
478 -- vector that contains the necessary information indexed by the Index
479 -- values in the pattern elements. For this purpose the only requirement
480 -- is that they be uniquely assigned.
482 -- Second, the pattern element referenced directly, i.e. the leading
483 -- pattern element, is always the maximum numbered element and therefore
484 -- indicates the total number of elements in the pattern. More precisely,
485 -- the element referenced by the P field of a pattern value, or the
486 -- element returned by any of the internal pattern construction routines
487 -- in the body (that return a value of type PE_Ptr) always is this
488 -- maximum element,
490 -- The purpose of this requirement is to allow an immediate determination
491 -- of the number of pattern elements within a pattern. This is used to
492 -- properly size the vectors used to contain auxiliary information for
493 -- traversal as described above.
495 -- Third, as compound pattern structures are constructed, the way in which
496 -- constituent parts of the pattern are constructed is stylized. This is
497 -- an automatic consequence of the way that these compound structures
498 -- are constructed, and basically what we are doing is simply documenting
499 -- and specifying the natural result of the pattern construction. The
500 -- section describing compound pattern structures gives details of the
501 -- numbering of each compound pattern structure.
503 -- The purpose of specifying the stylized numbering structures for the
504 -- compound patterns is to help simplify the processing in the Image
505 -- function, since it eases the task of retrieving the original recursive
506 -- structure of the pattern from the flat graph structure of elements.
507 -- This use in the Image function is the only point at which the code
508 -- makes use of the stylized structures.
510 type Ref_Array is array (IndexT range <>) of PE_Ptr;
511 -- This type is used to build an array whose N'th entry references the
512 -- element in a pattern whose Index value is N. See Build_Ref_Array.
514 procedure Build_Ref_Array (E : PE_Ptr; RA : out Ref_Array);
515 -- Given a pattern element which is the leading element of a pattern
516 -- structure, and a Ref_Array with bounds 1 .. E.Index, fills in the
517 -- Ref_Array so that its N'th entry references the element of the
518 -- referenced pattern whose Index value is N.
520 -------------------------------
521 -- Recursive Pattern Matches --
522 -------------------------------
524 -- The pattern primitive (+P) where P is a Pattern_Ptr or Pattern_Func
525 -- causes a recursive pattern match. This cannot be handled by an actual
526 -- recursive call to the outer level Match routine, since this would not
527 -- allow for possible backtracking into the region matched by the inner
528 -- pattern. Indeed this is the classical clash between recursion and
529 -- backtracking, and a simple recursive stack structure does not suffice.
531 -- This section describes how this recursion and the possible associated
532 -- backtracking is handled. We still use a single stack, but we establish
533 -- the concept of nested regions on this stack, each of which has a stack
534 -- base value pointing to the deepest stack entry of the region. The base
535 -- value for the outer level is zero.
537 -- When a recursive match is established, two special stack entries are
538 -- made. The first entry is used to save the original node that starts
539 -- the recursive match. This is saved so that the successor field of
540 -- this node is accessible at the end of the match, but it is never
541 -- popped and executed.
543 -- The second entry corresponds to a standard new region action. A
544 -- PC_R_Remove node is stacked, whose cursor field is used to store
545 -- the outer stack base, and the stack base is reset to point to
546 -- this PC_R_Remove node. Then the recursive pattern is matched and
547 -- it can make history stack entries in the normal matter, so now
548 -- the stack looks like:
550 -- (stack entries made by outer level)
552 -- (Special entry, node is (+P) successor
553 -- cursor entry is not used)
555 -- (PC_R_Remove entry, "cursor" value is (negative) <-- Stack base
556 -- saved base value for the enclosing region)
558 -- (stack entries made by inner level)
560 -- If a subsequent failure occurs and pops the PC_R_Remove node, it
561 -- removes itself and the special entry immediately underneath it,
562 -- restores the stack base value for the enclosing region, and then
563 -- again signals failure to look for alternatives that were stacked
564 -- before the recursion was initiated.
566 -- Now we need to consider what happens if the inner pattern succeeds, as
567 -- signalled by accessing the special PC_EOP pattern primitive. First we
568 -- recognize the nested case by looking at the Base value. If this Base
569 -- value is Stack'First, then the entire match has succeeded, but if the
570 -- base value is greater than Stack'First, then we have successfully
571 -- matched an inner pattern, and processing continues at the outer level.
573 -- There are two cases. The simple case is when the inner pattern has made
574 -- no stack entries, as recognized by the fact that the current stack
575 -- pointer is equal to the current base value. In this case it is fine to
576 -- remove all trace of the recursion by restoring the outer base value and
577 -- using the special entry to find the appropriate successor node.
579 -- The more complex case arises when the inner match does make stack
580 -- entries. In this case, the PC_EOP processing stacks a special entry
581 -- whose cursor value saves the saved inner base value (the one that
582 -- references the corresponding PC_R_Remove value), and whose node
583 -- pointer references a PC_R_Restore node, so the stack looks like:
585 -- (stack entries made by outer level)
587 -- (Special entry, node is (+P) successor,
588 -- cursor entry is not used)
590 -- (PC_R_Remove entry, "cursor" value is (negative)
591 -- saved base value for the enclosing region)
593 -- (stack entries made by inner level)
595 -- (PC_Region_Replace entry, "cursor" value is (negative)
596 -- stack pointer value referencing the PC_R_Remove entry).
598 -- If the entire match succeeds, then these stack entries are, as usual,
599 -- ignored and abandoned. If on the other hand a subsequent failure
600 -- causes the PC_Region_Replace entry to be popped, it restores the
601 -- inner base value from its saved "cursor" value and then fails again.
602 -- Note that it is OK that the cursor is temporarily clobbered by this
603 -- pop, since the second failure will reestablish a proper cursor value.
605 ---------------------------------
606 -- Compound Pattern Structures --
607 ---------------------------------
609 -- This section discusses the compound structures used to represent
610 -- constructed patterns. It shows the graph structures of pattern
611 -- elements that are constructed, and in the case of patterns that
612 -- provide backtracking possibilities, describes how the history
613 -- stack is used to control the backtracking. Finally, it notes the
614 -- way in which the Index numbers are assigned to the structure.
616 -- In all diagrams, solid lines (built with minus signs or vertical
617 -- bars, represent successor pointers (Pthen fields) with > or V used
618 -- to indicate the direction of the pointer. The initial node of the
619 -- structure is in the upper left of the diagram. A dotted line is an
620 -- alternative pointer from the element above it to the element below
621 -- it. See individual sections for details on how alternatives are used.
623 -------------------
624 -- Concatenation --
625 -------------------
627 -- In the pattern structures listed in this section, a line that looks
628 -- like ----> with nothing to the right indicates an end of pattern
629 -- (EOP) pointer that represents the end of the match.
631 -- When a pattern concatenation (L & R) occurs, the resulting structure
632 -- is obtained by finding all such EOP pointers in L, and replacing
633 -- them to point to R. This is the most important flattening that
634 -- occurs in constructing a pattern, and it means that the pattern
635 -- matching circuitry does not have to keep track of the structure
636 -- of a pattern with respect to concatenation, since the appropriate
637 -- successor is always at hand.
639 -- Concatenation itself generates no additional possibilities for
640 -- backtracking, but the constituent patterns of the concatenated
641 -- structure will make stack entries as usual. The maximum amount
642 -- of stack required by the structure is thus simply the sum of the
643 -- maximums required by L and R.
645 -- The index numbering of a concatenation structure works by leaving
646 -- the numbering of the right hand pattern, R, unchanged and adjusting
647 -- the numbers in the left hand pattern, L up by the count of elements
648 -- in R. This ensures that the maximum numbered element is the leading
649 -- element as required (given that it was the leading element in L).
651 -----------------
652 -- Alternation --
653 -----------------
655 -- A pattern (L or R) constructs the structure:
657 -- +---+ +---+
658 -- | A |---->| L |---->
659 -- +---+ +---+
660 -- .
661 -- .
662 -- +---+
663 -- | R |---->
664 -- +---+
666 -- The A element here is a PC_Alt node, and the dotted line represents
667 -- the contents of the Alt field. When the PC_Alt element is matched,
668 -- it stacks a pointer to the leading element of R on the history stack
669 -- so that on subsequent failure, a match of R is attempted.
671 -- The A node is the highest numbered element in the pattern. The
672 -- original index numbers of R are unchanged, but the index numbers
673 -- of the L pattern are adjusted up by the count of elements in R.
675 -- Note that the difference between the index of the L leading element
676 -- the index of the R leading element (after building the alt structure)
677 -- indicates the number of nodes in L, and this is true even after the
678 -- structure is incorporated into some larger structure. For example,
679 -- if the A node has index 16, and L has index 15 and R has index
680 -- 5, then we know that L has 10 (15-5) elements in it.
682 -- Suppose that we now concatenate this structure to another pattern
683 -- with 9 elements in it. We will now have the A node with an index
684 -- of 25, L with an index of 24 and R with an index of 14. We still
685 -- know that L has 10 (24-14) elements in it, numbered 15-24, and
686 -- consequently the successor of the alternation structure has an
687 -- index with a value less than 15. This is used in Image to figure
688 -- out the original recursive structure of a pattern.
690 -- To clarify the interaction of the alternation and concatenation
691 -- structures, here is a more complex example of the structure built
692 -- for the pattern:
694 -- (V or W or X) (Y or Z)
696 -- where A,B,C,D,E are all single element patterns:
698 -- +---+ +---+ +---+ +---+
699 -- I A I---->I V I---+-->I A I---->I Y I---->
700 -- +---+ +---+ I +---+ +---+
701 -- . I .
702 -- . I .
703 -- +---+ +---+ I +---+
704 -- I A I---->I W I-->I I Z I---->
705 -- +---+ +---+ I +---+
706 -- . I
707 -- . I
708 -- +---+ I
709 -- I X I------------>+
710 -- +---+
712 -- The numbering of the nodes would be as follows:
714 -- +---+ +---+ +---+ +---+
715 -- I 8 I---->I 7 I---+-->I 3 I---->I 2 I---->
716 -- +---+ +---+ I +---+ +---+
717 -- . I .
718 -- . I .
719 -- +---+ +---+ I +---+
720 -- I 6 I---->I 5 I-->I I 1 I---->
721 -- +---+ +---+ I +---+
722 -- . I
723 -- . I
724 -- +---+ I
725 -- I 4 I------------>+
726 -- +---+
728 -- Note: The above structure actually corresponds to
730 -- (A or (B or C)) (D or E)
732 -- rather than
734 -- ((A or B) or C) (D or E)
736 -- which is the more natural interpretation, but in fact alternation
737 -- is associative, and the construction of an alternative changes the
738 -- left grouped pattern to the right grouped pattern in any case, so
739 -- that the Image function produces a more natural looking output.
741 ---------
742 -- Arb --
743 ---------
745 -- An Arb pattern builds the structure
747 -- +---+
748 -- | X |---->
749 -- +---+
750 -- .
751 -- .
752 -- +---+
753 -- | Y |---->
754 -- +---+
756 -- The X node is a PC_Arb_X node, which matches null, and stacks a
757 -- pointer to Y node, which is the PC_Arb_Y node that matches one
758 -- extra character and restacks itself.
760 -- The PC_Arb_X node is numbered 2, and the PC_Arb_Y node is 1
762 -------------------------
763 -- Arbno (simple case) --
764 -------------------------
766 -- The simple form of Arbno can be used where the pattern always
767 -- matches at least one character if it succeeds, and it is known
768 -- not to make any history stack entries. In this case, Arbno (P)
769 -- can construct the following structure:
771 -- +-------------+
772 -- | ^
773 -- V |
774 -- +---+ |
775 -- | S |----> |
776 -- +---+ |
777 -- . |
778 -- . |
779 -- +---+ |
780 -- | P |---------->+
781 -- +---+
783 -- The S (PC_Arbno_S) node matches null stacking a pointer to the
784 -- pattern P. If a subsequent failure causes P to be matched and
785 -- this match succeeds, then node A gets restacked to try another
786 -- instance if needed by a subsequent failure.
788 -- The node numbering of the constituent pattern P is not affected.
789 -- The S node has a node number of P.Index + 1.
791 --------------------------
792 -- Arbno (complex case) --
793 --------------------------
795 -- A call to Arbno (P), where P can match null (or at least is not
796 -- known to require a non-null string) and/or P requires pattern stack
797 -- entries, constructs the following structure:
799 -- +--------------------------+
800 -- | ^
801 -- V |
802 -- +---+ |
803 -- | X |----> |
804 -- +---+ |
805 -- . |
806 -- . |
807 -- +---+ +---+ +---+ |
808 -- | E |---->| P |---->| Y |--->+
809 -- +---+ +---+ +---+
811 -- The node X (PC_Arbno_X) matches null, stacking a pointer to the
812 -- E-P-X structure used to match one Arbno instance.
814 -- Here E is the PC_R_Enter node which matches null and creates two
815 -- stack entries. The first is a special entry whose node field is
816 -- not used at all, and whose cursor field has the initial cursor.
818 -- The second entry corresponds to a standard new region action. A
819 -- PC_R_Remove node is stacked, whose cursor field is used to store
820 -- the outer stack base, and the stack base is reset to point to
821 -- this PC_R_Remove node. Then the pattern P is matched, and it can
822 -- make history stack entries in the normal manner, so now the stack
823 -- looks like:
825 -- (stack entries made before assign pattern)
827 -- (Special entry, node field not used,
828 -- used only to save initial cursor)
830 -- (PC_R_Remove entry, "cursor" value is (negative) <-- Stack Base
831 -- saved base value for the enclosing region)
833 -- (stack entries made by matching P)
835 -- If the match of P fails, then the PC_R_Remove entry is popped and
836 -- it removes both itself and the special entry underneath it,
837 -- restores the outer stack base, and signals failure.
839 -- If the match of P succeeds, then node Y, the PC_Arbno_Y node, pops
840 -- the inner region. There are two possibilities. If matching P left
841 -- no stack entries, then all traces of the inner region can be removed.
842 -- If there are stack entries, then we push an PC_Region_Replace stack
843 -- entry whose "cursor" value is the inner stack base value, and then
844 -- restore the outer stack base value, so the stack looks like:
846 -- (stack entries made before assign pattern)
848 -- (Special entry, node field not used,
849 -- used only to save initial cursor)
851 -- (PC_R_Remove entry, "cursor" value is (negative)
852 -- saved base value for the enclosing region)
854 -- (stack entries made by matching P)
856 -- (PC_Region_Replace entry, "cursor" value is (negative)
857 -- stack pointer value referencing the PC_R_Remove entry).
859 -- Now that we have matched another instance of the Arbno pattern,
860 -- we need to move to the successor. There are two cases. If the
861 -- Arbno pattern matched null, then there is no point in seeking
862 -- alternatives, since we would just match a whole bunch of nulls.
863 -- In this case we look through the alternative node, and move
864 -- directly to its successor (i.e. the successor of the Arbno
865 -- pattern). If on the other hand a non-null string was matched,
866 -- we simply follow the successor to the alternative node, which
867 -- sets up for another possible match of the Arbno pattern.
869 -- As noted in the section on stack checking, the stack count (and
870 -- hence the stack check) for a pattern includes only one iteration
871 -- of the Arbno pattern. To make sure that multiple iterations do not
872 -- overflow the stack, the Arbno node saves the stack count required
873 -- by a single iteration, and the Concat function increments this to
874 -- include stack entries required by any successor. The PC_Arbno_Y
875 -- node uses this count to ensure that sufficient stack remains
876 -- before proceeding after matching each new instance.
878 -- The node numbering of the constituent pattern P is not affected.
879 -- Where N is the number of nodes in P, the Y node is numbered N + 1,
880 -- the E node is N + 2, and the X node is N + 3.
882 ----------------------
883 -- Assign Immediate --
884 ----------------------
886 -- Immediate assignment (P * V) constructs the following structure
888 -- +---+ +---+ +---+
889 -- | E |---->| P |---->| A |---->
890 -- +---+ +---+ +---+
892 -- Here E is the PC_R_Enter node which matches null and creates two
893 -- stack entries. The first is a special entry whose node field is
894 -- not used at all, and whose cursor field has the initial cursor.
896 -- The second entry corresponds to a standard new region action. A
897 -- PC_R_Remove node is stacked, whose cursor field is used to store
898 -- the outer stack base, and the stack base is reset to point to
899 -- this PC_R_Remove node. Then the pattern P is matched, and it can
900 -- make history stack entries in the normal manner, so now the stack
901 -- looks like:
903 -- (stack entries made before assign pattern)
905 -- (Special entry, node field not used,
906 -- used only to save initial cursor)
908 -- (PC_R_Remove entry, "cursor" value is (negative) <-- Stack Base
909 -- saved base value for the enclosing region)
911 -- (stack entries made by matching P)
913 -- If the match of P fails, then the PC_R_Remove entry is popped
914 -- and it removes both itself and the special entry underneath it,
915 -- restores the outer stack base, and signals failure.
917 -- If the match of P succeeds, then node A, which is the actual
918 -- PC_Assign_Imm node, executes the assignment (using the stack
919 -- base to locate the entry with the saved starting cursor value),
920 -- and the pops the inner region. There are two possibilities, if
921 -- matching P left no stack entries, then all traces of the inner
922 -- region can be removed. If there are stack entries, then we push
923 -- an PC_Region_Replace stack entry whose "cursor" value is the
924 -- inner stack base value, and then restore the outer stack base
925 -- value, so the stack looks like:
927 -- (stack entries made before assign pattern)
929 -- (Special entry, node field not used,
930 -- used only to save initial cursor)
932 -- (PC_R_Remove entry, "cursor" value is (negative)
933 -- saved base value for the enclosing region)
935 -- (stack entries made by matching P)
937 -- (PC_Region_Replace entry, "cursor" value is the (negative)
938 -- stack pointer value referencing the PC_R_Remove entry).
940 -- If a subsequent failure occurs, the PC_Region_Replace node restores
941 -- the inner stack base value and signals failure to explore rematches
942 -- of the pattern P.
944 -- The node numbering of the constituent pattern P is not affected.
945 -- Where N is the number of nodes in P, the A node is numbered N + 1,
946 -- and the E node is N + 2.
948 ---------------------
949 -- Assign On Match --
950 ---------------------
952 -- The assign on match (**) pattern is quite similar to the assign
953 -- immediate pattern, except that the actual assignment has to be
954 -- delayed. The following structure is constructed:
956 -- +---+ +---+ +---+
957 -- | E |---->| P |---->| A |---->
958 -- +---+ +---+ +---+
960 -- The operation of this pattern is identical to that described above
961 -- for deferred assignment, up to the point where P has been matched.
963 -- The A node, which is the PC_Assign_OnM node first pushes a
964 -- PC_Assign node onto the history stack. This node saves the ending
965 -- cursor and acts as a flag for the final assignment, as further
966 -- described below.
968 -- It then stores a pointer to itself in the special entry node field.
969 -- This was otherwise unused, and is now used to retrieve the address
970 -- of the variable to be assigned at the end of the pattern.
972 -- After that the inner region is terminated in the usual manner,
973 -- by stacking a PC_R_Restore entry as described for the assign
974 -- immediate case. Note that the optimization of completely
975 -- removing the inner region does not happen in this case, since
976 -- we have at least one stack entry (the PC_Assign one we just made).
977 -- The stack now looks like:
979 -- (stack entries made before assign pattern)
981 -- (Special entry, node points to copy of
982 -- the PC_Assign_OnM node, and the
983 -- cursor field saves the initial cursor).
985 -- (PC_R_Remove entry, "cursor" value is (negative)
986 -- saved base value for the enclosing region)
988 -- (stack entries made by matching P)
990 -- (PC_Assign entry, saves final cursor)
992 -- (PC_Region_Replace entry, "cursor" value is (negative)
993 -- stack pointer value referencing the PC_R_Remove entry).
995 -- If a subsequent failure causes the PC_Assign node to execute it
996 -- simply removes itself and propagates the failure.
998 -- If the match succeeds, then the history stack is scanned for
999 -- PC_Assign nodes, and the assignments are executed (examination
1000 -- of the above diagram will show that all the necessary data is
1001 -- at hand for the assignment).
1003 -- To optimize the common case where no assign-on-match operations
1004 -- are present, a global flag Assign_OnM is maintained which is
1005 -- initialize to False, and gets set True as part of the execution
1006 -- of the PC_Assign_OnM node. The scan of the history stack for
1007 -- PC_Assign entries is done only if this flag is set.
1009 -- The node numbering of the constituent pattern P is not affected.
1010 -- Where N is the number of nodes in P, the A node is numbered N + 1,
1011 -- and the E node is N + 2.
1013 ---------
1014 -- Bal --
1015 ---------
1017 -- Bal builds a single node:
1019 -- +---+
1020 -- | B |---->
1021 -- +---+
1023 -- The node B is the PC_Bal node which matches a parentheses balanced
1024 -- string, starting at the current cursor position. It then updates
1025 -- the cursor past this matched string, and stacks a pointer to itself
1026 -- with this updated cursor value on the history stack, to extend the
1027 -- matched string on a subsequent failure.
1029 -- Since this is a single node it is numbered 1 (the reason we include
1030 -- it in the compound patterns section is that it backtracks).
1032 ------------
1033 -- BreakX --
1034 ------------
1036 -- BreakX builds the structure
1038 -- +---+ +---+
1039 -- | B |---->| A |---->
1040 -- +---+ +---+
1041 -- ^ .
1042 -- | .
1043 -- | +---+
1044 -- +<------| X |
1045 -- +---+
1047 -- Here the B node is the BreakX_xx node that performs a normal Break
1048 -- function. The A node is an alternative (PC_Alt) node that matches
1049 -- null, but stacks a pointer to node X (the PC_BreakX_X node) which
1050 -- extends the match one character (to eat up the previously detected
1051 -- break character), and then rematches the break.
1053 -- The B node is numbered 3, the alternative node is 1, and the X
1054 -- node is 2.
1056 -----------
1057 -- Fence --
1058 -----------
1060 -- Fence builds a single node:
1062 -- +---+
1063 -- | F |---->
1064 -- +---+
1066 -- The element F, PC_Fence, matches null, and stacks a pointer to a
1067 -- PC_Cancel element which will abort the match on a subsequent failure.
1069 -- Since this is a single element it is numbered 1 (the reason we
1070 -- include it in the compound patterns section is that it backtracks).
1072 --------------------
1073 -- Fence Function --
1074 --------------------
1076 -- A call to the Fence function builds the structure:
1078 -- +---+ +---+ +---+
1079 -- | E |---->| P |---->| X |---->
1080 -- +---+ +---+ +---+
1082 -- Here E is the PC_R_Enter node which matches null and creates two
1083 -- stack entries. The first is a special entry which is not used at
1084 -- all in the fence case (it is present merely for uniformity with
1085 -- other cases of region enter operations).
1087 -- The second entry corresponds to a standard new region action. A
1088 -- PC_R_Remove node is stacked, whose cursor field is used to store
1089 -- the outer stack base, and the stack base is reset to point to
1090 -- this PC_R_Remove node. Then the pattern P is matched, and it can
1091 -- make history stack entries in the normal manner, so now the stack
1092 -- looks like:
1094 -- (stack entries made before fence pattern)
1096 -- (Special entry, not used at all)
1098 -- (PC_R_Remove entry, "cursor" value is (negative) <-- Stack Base
1099 -- saved base value for the enclosing region)
1101 -- (stack entries made by matching P)
1103 -- If the match of P fails, then the PC_R_Remove entry is popped
1104 -- and it removes both itself and the special entry underneath it,
1105 -- restores the outer stack base, and signals failure.
1107 -- If the match of P succeeds, then node X, the PC_Fence_X node, gets
1108 -- control. One might be tempted to think that at this point, the
1109 -- history stack entries made by matching P can just be removed since
1110 -- they certainly are not going to be used for rematching (that is
1111 -- whole point of Fence after all). However, this is wrong, because
1112 -- it would result in the loss of possible assign-on-match entries
1113 -- for deferred pattern assignments.
1115 -- Instead what we do is to make a special entry whose node references
1116 -- PC_Fence_Y, and whose cursor saves the inner stack base value, i.e.
1117 -- the pointer to the PC_R_Remove entry. Then the outer stack base
1118 -- pointer is restored, so the stack looks like:
1120 -- (stack entries made before assign pattern)
1122 -- (Special entry, not used at all)
1124 -- (PC_R_Remove entry, "cursor" value is (negative)
1125 -- saved base value for the enclosing region)
1127 -- (stack entries made by matching P)
1129 -- (PC_Fence_Y entry, "cursor" value is (negative) stack
1130 -- pointer value referencing the PC_R_Remove entry).
1132 -- If a subsequent failure occurs, then the PC_Fence_Y entry removes
1133 -- the entire inner region, including all entries made by matching P,
1134 -- and alternatives prior to the Fence pattern are sought.
1136 -- The node numbering of the constituent pattern P is not affected.
1137 -- Where N is the number of nodes in P, the X node is numbered N + 1,
1138 -- and the E node is N + 2.
1140 -------------
1141 -- Succeed --
1142 -------------
1144 -- Succeed builds a single node:
1146 -- +---+
1147 -- | S |---->
1148 -- +---+
1150 -- The node S is the PC_Succeed node which matches null, and stacks
1151 -- a pointer to itself on the history stack, so that a subsequent
1152 -- failure repeats the same match.
1154 -- Since this is a single node it is numbered 1 (the reason we include
1155 -- it in the compound patterns section is that it backtracks).
1157 ---------------------
1158 -- Write Immediate --
1159 ---------------------
1161 -- The structure built for a write immediate operation (P * F, where
1162 -- F is a file access value) is:
1164 -- +---+ +---+ +---+
1165 -- | E |---->| P |---->| W |---->
1166 -- +---+ +---+ +---+
1168 -- Here E is the PC_R_Enter node and W is the PC_Write_Imm node. The
1169 -- handling is identical to that described above for Assign Immediate,
1170 -- except that at the point where a successful match occurs, the matched
1171 -- substring is written to the referenced file.
1173 -- The node numbering of the constituent pattern P is not affected.
1174 -- Where N is the number of nodes in P, the W node is numbered N + 1,
1175 -- and the E node is N + 2.
1177 --------------------
1178 -- Write On Match --
1179 --------------------
1181 -- The structure built for a write on match operation (P ** F, where
1182 -- F is a file access value) is:
1184 -- +---+ +---+ +---+
1185 -- | E |---->| P |---->| W |---->
1186 -- +---+ +---+ +---+
1188 -- Here E is the PC_R_Enter node and W is the PC_Write_OnM node. The
1189 -- handling is identical to that described above for Assign On Match,
1190 -- except that at the point where a successful match has completed,
1191 -- the matched substring is written to the referenced file.
1193 -- The node numbering of the constituent pattern P is not affected.
1194 -- Where N is the number of nodes in P, the W node is numbered N + 1,
1195 -- and the E node is N + 2.
1196 -----------------------
1197 -- Constant Patterns --
1198 -----------------------
1200 -- The following pattern elements are referenced only from the pattern
1201 -- history stack. In each case the processing for the pattern element
1202 -- results in pattern match abort, or further failure, so there is no
1203 -- need for a successor and no need for a node number
1205 CP_Assign : aliased PE := (PC_Assign, 0, N);
1206 CP_Cancel : aliased PE := (PC_Cancel, 0, N);
1207 CP_Fence_Y : aliased PE := (PC_Fence_Y, 0, N);
1208 CP_R_Remove : aliased PE := (PC_R_Remove, 0, N);
1209 CP_R_Restore : aliased PE := (PC_R_Restore, 0, N);
1211 -----------------------
1212 -- Local Subprograms --
1213 -----------------------
1215 function Alternate (L, R : PE_Ptr) return PE_Ptr;
1216 function "or" (L, R : PE_Ptr) return PE_Ptr renames Alternate;
1217 -- Build pattern structure corresponding to the alternation of L, R.
1218 -- (i.e. try to match L, and if that fails, try to match R).
1220 function Arbno_Simple (P : PE_Ptr) return PE_Ptr;
1221 -- Build simple Arbno pattern, P is a pattern that is guaranteed to
1222 -- match at least one character if it succeeds and to require no
1223 -- stack entries under all circumstances. The result returned is
1224 -- a simple Arbno structure as previously described.
1226 function Bracket (E, P, A : PE_Ptr) return PE_Ptr;
1227 -- Given two single node pattern elements E and A, and a (possible
1228 -- complex) pattern P, construct the concatenation E-->P-->A and
1229 -- return a pointer to E. The concatenation does not affect the
1230 -- node numbering in P. A has a number one higher than the maximum
1231 -- number in P, and E has a number two higher than the maximum
1232 -- number in P (see for example the Assign_Immediate structure to
1233 -- understand a typical use of this function).
1235 function BreakX_Make (B : PE_Ptr) return Pattern;
1236 -- Given a pattern element for a Break pattern, returns the
1237 -- corresponding BreakX compound pattern structure.
1239 function Concat (L, R : PE_Ptr; Incr : Natural) return PE_Ptr;
1240 -- Creates a pattern element that represents a concatenation of the
1241 -- two given pattern elements (i.e. the pattern L followed by R).
1242 -- The result returned is always the same as L, but the pattern
1243 -- referenced by L is modified to have R as a successor. This
1244 -- procedure does not copy L or R, so if a copy is required, it
1245 -- is the responsibility of the caller. The Incr parameter is an
1246 -- amount to be added to the Nat field of any P_Arbno_Y node that is
1247 -- in the left operand, it represents the additional stack space
1248 -- required by the right operand.
1250 function C_To_PE (C : PChar) return PE_Ptr;
1251 -- Given a character, constructs a pattern element that matches
1252 -- the single character.
1254 function Copy (P : PE_Ptr) return PE_Ptr;
1255 -- Creates a copy of the pattern element referenced by the given
1256 -- pattern element reference. This is a deep copy, which means that
1257 -- it follows the Next and Alt pointers.
1259 function Image (P : PE_Ptr) return String;
1260 -- Returns the image of the address of the referenced pattern element.
1261 -- This is equivalent to Image (To_Address (P));
1263 function Is_In (C : Character; Str : String) return Boolean;
1264 pragma Inline (Is_In);
1265 -- Determines if the character C is in string Str
1267 procedure Logic_Error;
1268 -- Called to raise Program_Error with an appropriate message if an
1269 -- internal logic error is detected.
1271 function Str_BF (A : Boolean_Func) return String;
1272 function Str_FP (A : File_Ptr) return String;
1273 function Str_NF (A : Natural_Func) return String;
1274 function Str_NP (A : Natural_Ptr) return String;
1275 function Str_PP (A : Pattern_Ptr) return String;
1276 function Str_VF (A : VString_Func) return String;
1277 function Str_VP (A : VString_Ptr) return String;
1278 -- These are debugging routines, which return a representation of the
1279 -- given access value (they are called only by Image and Dump)
1281 procedure Set_Successor (Pat : PE_Ptr; Succ : PE_Ptr);
1282 -- Adjusts all EOP pointers in Pat to point to Succ. No other changes
1283 -- are made. In particular, Succ is unchanged, and no index numbers
1284 -- are modified. Note that Pat may not be equal to EOP on entry.
1286 function S_To_PE (Str : PString) return PE_Ptr;
1287 -- Given a string, constructs a pattern element that matches the string
1289 procedure Uninitialized_Pattern;
1290 pragma No_Return (Uninitialized_Pattern);
1291 -- Called to raise Program_Error with an appropriate error message if
1292 -- an uninitialized pattern is used in any pattern construction or
1293 -- pattern matching operation.
1295 procedure XMatch
1296 (Subject : String;
1297 Pat_P : PE_Ptr;
1298 Pat_S : Natural;
1299 Start : out Natural;
1300 Stop : out Natural);
1301 -- This is the common pattern match routine. It is passed a string and
1302 -- a pattern, and it indicates success or failure, and on success the
1303 -- section of the string matched. It does not perform any assignments
1304 -- to the subject string, so pattern replacement is for the caller.
1306 -- Subject The subject string. The lower bound is always one. In the
1307 -- Match procedures, it is fine to use strings whose lower bound
1308 -- is not one, but we perform a one time conversion before the
1309 -- call to XMatch, so that XMatch does not have to be bothered
1310 -- with strange lower bounds.
1312 -- Pat_P Points to initial pattern element of pattern to be matched
1314 -- Pat_S Maximum required stack entries for pattern to be matched
1316 -- Start If match is successful, starting index of matched section.
1317 -- This value is always non-zero. A value of zero is used to
1318 -- indicate a failed match.
1320 -- Stop If match is successful, ending index of matched section.
1321 -- This can be zero if we match the null string at the start,
1322 -- in which case Start is set to zero, and Stop to one. If the
1323 -- Match fails, then the contents of Stop is undefined.
1325 procedure XMatchD
1326 (Subject : String;
1327 Pat_P : PE_Ptr;
1328 Pat_S : Natural;
1329 Start : out Natural;
1330 Stop : out Natural);
1331 -- Identical in all respects to XMatch, except that trace information is
1332 -- output on Standard_Output during execution of the match. This is the
1333 -- version that is called if the original Match call has Debug => True.
1335 ---------
1336 -- "&" --
1337 ---------
1339 function "&" (L : PString; R : Pattern) return Pattern is
1340 begin
1341 return (AFC with R.Stk, Concat (S_To_PE (L), Copy (R.P), R.Stk));
1342 end "&";
1344 function "&" (L : Pattern; R : PString) return Pattern is
1345 begin
1346 return (AFC with L.Stk, Concat (Copy (L.P), S_To_PE (R), 0));
1347 end "&";
1349 function "&" (L : PChar; R : Pattern) return Pattern is
1350 begin
1351 return (AFC with R.Stk, Concat (C_To_PE (L), Copy (R.P), R.Stk));
1352 end "&";
1354 function "&" (L : Pattern; R : PChar) return Pattern is
1355 begin
1356 return (AFC with L.Stk, Concat (Copy (L.P), C_To_PE (R), 0));
1357 end "&";
1359 function "&" (L : Pattern; R : Pattern) return Pattern is
1360 begin
1361 return (AFC with L.Stk + R.Stk, Concat (Copy (L.P), Copy (R.P), R.Stk));
1362 end "&";
1364 ---------
1365 -- "*" --
1366 ---------
1368 -- Assign immediate
1370 -- +---+ +---+ +---+
1371 -- | E |---->| P |---->| A |---->
1372 -- +---+ +---+ +---+
1374 -- The node numbering of the constituent pattern P is not affected.
1375 -- Where N is the number of nodes in P, the A node is numbered N + 1,
1376 -- and the E node is N + 2.
1378 function "*" (P : Pattern; Var : VString_Var) return Pattern is
1379 Pat : constant PE_Ptr := Copy (P.P);
1380 E : constant PE_Ptr := new PE'(PC_R_Enter, 0, EOP);
1381 A : constant PE_Ptr :=
1382 new PE'(PC_Assign_Imm, 0, EOP, Var'Unrestricted_Access);
1383 begin
1384 return (AFC with P.Stk + 3, Bracket (E, Pat, A));
1385 end "*";
1387 function "*" (P : PString; Var : VString_Var) return Pattern is
1388 Pat : constant PE_Ptr := S_To_PE (P);
1389 E : constant PE_Ptr := new PE'(PC_R_Enter, 0, EOP);
1390 A : constant PE_Ptr :=
1391 new PE'(PC_Assign_Imm, 0, EOP, Var'Unrestricted_Access);
1392 begin
1393 return (AFC with 3, Bracket (E, Pat, A));
1394 end "*";
1396 function "*" (P : PChar; Var : VString_Var) return Pattern is
1397 Pat : constant PE_Ptr := C_To_PE (P);
1398 E : constant PE_Ptr := new PE'(PC_R_Enter, 0, EOP);
1399 A : constant PE_Ptr :=
1400 new PE'(PC_Assign_Imm, 0, EOP, Var'Unrestricted_Access);
1401 begin
1402 return (AFC with 3, Bracket (E, Pat, A));
1403 end "*";
1405 -- Write immediate
1407 -- +---+ +---+ +---+
1408 -- | E |---->| P |---->| W |---->
1409 -- +---+ +---+ +---+
1411 -- The node numbering of the constituent pattern P is not affected.
1412 -- Where N is the number of nodes in P, the W node is numbered N + 1,
1413 -- and the E node is N + 2.
1415 function "*" (P : Pattern; Fil : File_Access) return Pattern is
1416 Pat : constant PE_Ptr := Copy (P.P);
1417 E : constant PE_Ptr := new PE'(PC_R_Enter, 0, EOP);
1418 W : constant PE_Ptr := new PE'(PC_Write_Imm, 0, EOP, Fil);
1419 begin
1420 return (AFC with 3, Bracket (E, Pat, W));
1421 end "*";
1423 function "*" (P : PString; Fil : File_Access) return Pattern is
1424 Pat : constant PE_Ptr := S_To_PE (P);
1425 E : constant PE_Ptr := new PE'(PC_R_Enter, 0, EOP);
1426 W : constant PE_Ptr := new PE'(PC_Write_Imm, 0, EOP, Fil);
1427 begin
1428 return (AFC with 3, Bracket (E, Pat, W));
1429 end "*";
1431 function "*" (P : PChar; Fil : File_Access) return Pattern is
1432 Pat : constant PE_Ptr := C_To_PE (P);
1433 E : constant PE_Ptr := new PE'(PC_R_Enter, 0, EOP);
1434 W : constant PE_Ptr := new PE'(PC_Write_Imm, 0, EOP, Fil);
1435 begin
1436 return (AFC with 3, Bracket (E, Pat, W));
1437 end "*";
1439 ----------
1440 -- "**" --
1441 ----------
1443 -- Assign on match
1445 -- +---+ +---+ +---+
1446 -- | E |---->| P |---->| A |---->
1447 -- +---+ +---+ +---+
1449 -- The node numbering of the constituent pattern P is not affected.
1450 -- Where N is the number of nodes in P, the A node is numbered N + 1,
1451 -- and the E node is N + 2.
1453 function "**" (P : Pattern; Var : VString_Var) return Pattern is
1454 Pat : constant PE_Ptr := Copy (P.P);
1455 E : constant PE_Ptr := new PE'(PC_R_Enter, 0, EOP);
1456 A : constant PE_Ptr :=
1457 new PE'(PC_Assign_OnM, 0, EOP, Var'Unrestricted_Access);
1458 begin
1459 return (AFC with P.Stk + 3, Bracket (E, Pat, A));
1460 end "**";
1462 function "**" (P : PString; Var : VString_Var) return Pattern is
1463 Pat : constant PE_Ptr := S_To_PE (P);
1464 E : constant PE_Ptr := new PE'(PC_R_Enter, 0, EOP);
1465 A : constant PE_Ptr :=
1466 new PE'(PC_Assign_OnM, 0, EOP, Var'Unrestricted_Access);
1467 begin
1468 return (AFC with 3, Bracket (E, Pat, A));
1469 end "**";
1471 function "**" (P : PChar; Var : VString_Var) return Pattern is
1472 Pat : constant PE_Ptr := C_To_PE (P);
1473 E : constant PE_Ptr := new PE'(PC_R_Enter, 0, EOP);
1474 A : constant PE_Ptr :=
1475 new PE'(PC_Assign_OnM, 0, EOP, Var'Unrestricted_Access);
1476 begin
1477 return (AFC with 3, Bracket (E, Pat, A));
1478 end "**";
1480 -- Write on match
1482 -- +---+ +---+ +---+
1483 -- | E |---->| P |---->| W |---->
1484 -- +---+ +---+ +---+
1486 -- The node numbering of the constituent pattern P is not affected.
1487 -- Where N is the number of nodes in P, the W node is numbered N + 1,
1488 -- and the E node is N + 2.
1490 function "**" (P : Pattern; Fil : File_Access) return Pattern is
1491 Pat : constant PE_Ptr := Copy (P.P);
1492 E : constant PE_Ptr := new PE'(PC_R_Enter, 0, EOP);
1493 W : constant PE_Ptr := new PE'(PC_Write_OnM, 0, EOP, Fil);
1494 begin
1495 return (AFC with P.Stk + 3, Bracket (E, Pat, W));
1496 end "**";
1498 function "**" (P : PString; Fil : File_Access) return Pattern is
1499 Pat : constant PE_Ptr := S_To_PE (P);
1500 E : constant PE_Ptr := new PE'(PC_R_Enter, 0, EOP);
1501 W : constant PE_Ptr := new PE'(PC_Write_OnM, 0, EOP, Fil);
1502 begin
1503 return (AFC with 3, Bracket (E, Pat, W));
1504 end "**";
1506 function "**" (P : PChar; Fil : File_Access) return Pattern is
1507 Pat : constant PE_Ptr := C_To_PE (P);
1508 E : constant PE_Ptr := new PE'(PC_R_Enter, 0, EOP);
1509 W : constant PE_Ptr := new PE'(PC_Write_OnM, 0, EOP, Fil);
1510 begin
1511 return (AFC with 3, Bracket (E, Pat, W));
1512 end "**";
1514 ---------
1515 -- "+" --
1516 ---------
1518 function "+" (Str : VString_Var) return Pattern is
1519 begin
1520 return
1521 (AFC with 0,
1522 new PE'(PC_String_VP, 1, EOP, Str'Unrestricted_Access));
1523 end "+";
1525 function "+" (Str : VString_Func) return Pattern is
1526 begin
1527 return (AFC with 0, new PE'(PC_String_VF, 1, EOP, Str));
1528 end "+";
1530 function "+" (P : Pattern_Var) return Pattern is
1531 begin
1532 return
1533 (AFC with 3,
1534 new PE'(PC_Rpat, 1, EOP, P'Unrestricted_Access));
1535 end "+";
1537 function "+" (P : Boolean_Func) return Pattern is
1538 begin
1539 return (AFC with 3, new PE'(PC_Pred_Func, 1, EOP, P));
1540 end "+";
1542 ----------
1543 -- "or" --
1544 ----------
1546 function "or" (L : PString; R : Pattern) return Pattern is
1547 begin
1548 return (AFC with R.Stk + 1, S_To_PE (L) or Copy (R.P));
1549 end "or";
1551 function "or" (L : Pattern; R : PString) return Pattern is
1552 begin
1553 return (AFC with L.Stk + 1, Copy (L.P) or S_To_PE (R));
1554 end "or";
1556 function "or" (L : PString; R : PString) return Pattern is
1557 begin
1558 return (AFC with 1, S_To_PE (L) or S_To_PE (R));
1559 end "or";
1561 function "or" (L : Pattern; R : Pattern) return Pattern is
1562 begin
1563 return (AFC with
1564 Natural'Max (L.Stk, R.Stk) + 1, Copy (L.P) or Copy (R.P));
1565 end "or";
1567 function "or" (L : PChar; R : Pattern) return Pattern is
1568 begin
1569 return (AFC with 1, C_To_PE (L) or Copy (R.P));
1570 end "or";
1572 function "or" (L : Pattern; R : PChar) return Pattern is
1573 begin
1574 return (AFC with 1, Copy (L.P) or C_To_PE (R));
1575 end "or";
1577 function "or" (L : PChar; R : PChar) return Pattern is
1578 begin
1579 return (AFC with 1, C_To_PE (L) or C_To_PE (R));
1580 end "or";
1582 function "or" (L : PString; R : PChar) return Pattern is
1583 begin
1584 return (AFC with 1, S_To_PE (L) or C_To_PE (R));
1585 end "or";
1587 function "or" (L : PChar; R : PString) return Pattern is
1588 begin
1589 return (AFC with 1, C_To_PE (L) or S_To_PE (R));
1590 end "or";
1592 ------------
1593 -- Adjust --
1594 ------------
1596 -- No two patterns share the same pattern elements, so the adjust
1597 -- procedure for a Pattern assignment must do a deep copy of the
1598 -- pattern element structure.
1600 procedure Adjust (Object : in out Pattern) is
1601 begin
1602 Object.P := Copy (Object.P);
1603 end Adjust;
1605 ---------------
1606 -- Alternate --
1607 ---------------
1609 function Alternate (L, R : PE_Ptr) return PE_Ptr is
1610 begin
1611 -- If the left pattern is null, then we just add the alternation
1612 -- node with an index one greater than the right hand pattern.
1614 if L = EOP then
1615 return new PE'(PC_Alt, R.Index + 1, EOP, R);
1617 -- If the left pattern is non-null, then build a reference vector
1618 -- for its elements, and adjust their index values to accommodate
1619 -- the right hand elements. Then add the alternation node.
1621 else
1622 declare
1623 Refs : Ref_Array (1 .. L.Index);
1625 begin
1626 Build_Ref_Array (L, Refs);
1628 for J in Refs'Range loop
1629 Refs (J).Index := Refs (J).Index + R.Index;
1630 end loop;
1631 end;
1633 return new PE'(PC_Alt, L.Index + 1, L, R);
1634 end if;
1635 end Alternate;
1637 ---------
1638 -- Any --
1639 ---------
1641 function Any (Str : String) return Pattern is
1642 begin
1643 return (AFC with 0, new PE'(PC_Any_CS, 1, EOP, To_Set (Str)));
1644 end Any;
1646 function Any (Str : VString) return Pattern is
1647 begin
1648 return Any (S (Str));
1649 end Any;
1651 function Any (Str : Character) return Pattern is
1652 begin
1653 return (AFC with 0, new PE'(PC_Any_CH, 1, EOP, Str));
1654 end Any;
1656 function Any (Str : Character_Set) return Pattern is
1657 begin
1658 return (AFC with 0, new PE'(PC_Any_CS, 1, EOP, Str));
1659 end Any;
1661 function Any (Str : not null access VString) return Pattern is
1662 begin
1663 return (AFC with 0, new PE'(PC_Any_VP, 1, EOP, VString_Ptr (Str)));
1664 end Any;
1666 function Any (Str : VString_Func) return Pattern is
1667 begin
1668 return (AFC with 0, new PE'(PC_Any_VF, 1, EOP, Str));
1669 end Any;
1671 ---------
1672 -- Arb --
1673 ---------
1675 -- +---+
1676 -- | X |---->
1677 -- +---+
1678 -- .
1679 -- .
1680 -- +---+
1681 -- | Y |---->
1682 -- +---+
1684 -- The PC_Arb_X element is numbered 2, and the PC_Arb_Y element is 1
1686 function Arb return Pattern is
1687 Y : constant PE_Ptr := new PE'(PC_Arb_Y, 1, EOP);
1688 X : constant PE_Ptr := new PE'(PC_Arb_X, 2, EOP, Y);
1689 begin
1690 return (AFC with 1, X);
1691 end Arb;
1693 -----------
1694 -- Arbno --
1695 -----------
1697 function Arbno (P : PString) return Pattern is
1698 begin
1699 if P'Length = 0 then
1700 return (AFC with 0, EOP);
1701 else
1702 return (AFC with 0, Arbno_Simple (S_To_PE (P)));
1703 end if;
1704 end Arbno;
1706 function Arbno (P : PChar) return Pattern is
1707 begin
1708 return (AFC with 0, Arbno_Simple (C_To_PE (P)));
1709 end Arbno;
1711 function Arbno (P : Pattern) return Pattern is
1712 Pat : constant PE_Ptr := Copy (P.P);
1714 begin
1715 if P.Stk = 0
1716 and then OK_For_Simple_Arbno (Pat.Pcode)
1717 then
1718 return (AFC with 0, Arbno_Simple (Pat));
1719 end if;
1721 -- This is the complex case, either the pattern makes stack entries
1722 -- or it is possible for the pattern to match the null string (more
1723 -- accurately, we don't know that this is not the case).
1725 -- +--------------------------+
1726 -- | ^
1727 -- V |
1728 -- +---+ |
1729 -- | X |----> |
1730 -- +---+ |
1731 -- . |
1732 -- . |
1733 -- +---+ +---+ +---+ |
1734 -- | E |---->| P |---->| Y |--->+
1735 -- +---+ +---+ +---+
1737 -- The node numbering of the constituent pattern P is not affected.
1738 -- Where N is the number of nodes in P, the Y node is numbered N + 1,
1739 -- the E node is N + 2, and the X node is N + 3.
1741 declare
1742 E : constant PE_Ptr := new PE'(PC_R_Enter, 0, EOP);
1743 X : constant PE_Ptr := new PE'(PC_Arbno_X, 0, EOP, E);
1744 Y : constant PE_Ptr := new PE'(PC_Arbno_Y, 0, X, P.Stk + 3);
1745 EPY : constant PE_Ptr := Bracket (E, Pat, Y);
1746 begin
1747 X.Alt := EPY;
1748 X.Index := EPY.Index + 1;
1749 return (AFC with P.Stk + 3, X);
1750 end;
1751 end Arbno;
1753 ------------------
1754 -- Arbno_Simple --
1755 ------------------
1757 -- +-------------+
1758 -- | ^
1759 -- V |
1760 -- +---+ |
1761 -- | S |----> |
1762 -- +---+ |
1763 -- . |
1764 -- . |
1765 -- +---+ |
1766 -- | P |---------->+
1767 -- +---+
1769 -- The node numbering of the constituent pattern P is not affected.
1770 -- The S node has a node number of P.Index + 1.
1772 -- Note that we know that P cannot be EOP, because a null pattern
1773 -- does not meet the requirements for simple Arbno.
1775 function Arbno_Simple (P : PE_Ptr) return PE_Ptr is
1776 S : constant PE_Ptr := new PE'(PC_Arbno_S, P.Index + 1, EOP, P);
1777 begin
1778 Set_Successor (P, S);
1779 return S;
1780 end Arbno_Simple;
1782 ---------
1783 -- Bal --
1784 ---------
1786 function Bal return Pattern is
1787 begin
1788 return (AFC with 1, new PE'(PC_Bal, 1, EOP));
1789 end Bal;
1791 -------------
1792 -- Bracket --
1793 -------------
1795 function Bracket (E, P, A : PE_Ptr) return PE_Ptr is
1796 begin
1797 if P = EOP then
1798 E.Pthen := A;
1799 E.Index := 2;
1800 A.Index := 1;
1802 else
1803 E.Pthen := P;
1804 Set_Successor (P, A);
1805 E.Index := P.Index + 2;
1806 A.Index := P.Index + 1;
1807 end if;
1809 return E;
1810 end Bracket;
1812 -----------
1813 -- Break --
1814 -----------
1816 function Break (Str : String) return Pattern is
1817 begin
1818 return (AFC with 0, new PE'(PC_Break_CS, 1, EOP, To_Set (Str)));
1819 end Break;
1821 function Break (Str : VString) return Pattern is
1822 begin
1823 return Break (S (Str));
1824 end Break;
1826 function Break (Str : Character) return Pattern is
1827 begin
1828 return (AFC with 0, new PE'(PC_Break_CH, 1, EOP, Str));
1829 end Break;
1831 function Break (Str : Character_Set) return Pattern is
1832 begin
1833 return (AFC with 0, new PE'(PC_Break_CS, 1, EOP, Str));
1834 end Break;
1836 function Break (Str : not null access VString) return Pattern is
1837 begin
1838 return (AFC with 0,
1839 new PE'(PC_Break_VP, 1, EOP, Str.all'Unchecked_Access));
1840 end Break;
1842 function Break (Str : VString_Func) return Pattern is
1843 begin
1844 return (AFC with 0, new PE'(PC_Break_VF, 1, EOP, Str));
1845 end Break;
1847 ------------
1848 -- BreakX --
1849 ------------
1851 function BreakX (Str : String) return Pattern is
1852 begin
1853 return BreakX_Make (new PE'(PC_BreakX_CS, 3, N, To_Set (Str)));
1854 end BreakX;
1856 function BreakX (Str : VString) return Pattern is
1857 begin
1858 return BreakX (S (Str));
1859 end BreakX;
1861 function BreakX (Str : Character) return Pattern is
1862 begin
1863 return BreakX_Make (new PE'(PC_BreakX_CH, 3, N, Str));
1864 end BreakX;
1866 function BreakX (Str : Character_Set) return Pattern is
1867 begin
1868 return BreakX_Make (new PE'(PC_BreakX_CS, 3, N, Str));
1869 end BreakX;
1871 function BreakX (Str : not null access VString) return Pattern is
1872 begin
1873 return BreakX_Make (new PE'(PC_BreakX_VP, 3, N, VString_Ptr (Str)));
1874 end BreakX;
1876 function BreakX (Str : VString_Func) return Pattern is
1877 begin
1878 return BreakX_Make (new PE'(PC_BreakX_VF, 3, N, Str));
1879 end BreakX;
1881 -----------------
1882 -- BreakX_Make --
1883 -----------------
1885 -- +---+ +---+
1886 -- | B |---->| A |---->
1887 -- +---+ +---+
1888 -- ^ .
1889 -- | .
1890 -- | +---+
1891 -- +<------| X |
1892 -- +---+
1894 -- The B node is numbered 3, the alternative node is 1, and the X
1895 -- node is 2.
1897 function BreakX_Make (B : PE_Ptr) return Pattern is
1898 X : constant PE_Ptr := new PE'(PC_BreakX_X, 2, B);
1899 A : constant PE_Ptr := new PE'(PC_Alt, 1, EOP, X);
1900 begin
1901 B.Pthen := A;
1902 return (AFC with 2, B);
1903 end BreakX_Make;
1905 ---------------------
1906 -- Build_Ref_Array --
1907 ---------------------
1909 procedure Build_Ref_Array (E : PE_Ptr; RA : out Ref_Array) is
1911 procedure Record_PE (E : PE_Ptr);
1912 -- Record given pattern element if not already recorded in RA,
1913 -- and also record any referenced pattern elements recursively.
1915 ---------------
1916 -- Record_PE --
1917 ---------------
1919 procedure Record_PE (E : PE_Ptr) is
1920 begin
1921 PutD (" Record_PE called with PE_Ptr = " & Image (E));
1923 if E = EOP or else RA (E.Index) /= null then
1924 Put_LineD (", nothing to do");
1925 return;
1927 else
1928 Put_LineD (", recording" & IndexT'Image (E.Index));
1929 RA (E.Index) := E;
1930 Record_PE (E.Pthen);
1932 if E.Pcode in PC_Has_Alt then
1933 Record_PE (E.Alt);
1934 end if;
1935 end if;
1936 end Record_PE;
1938 -- Start of processing for Build_Ref_Array
1940 begin
1941 New_LineD;
1942 Put_LineD ("Entering Build_Ref_Array");
1943 Record_PE (E);
1944 New_LineD;
1945 end Build_Ref_Array;
1947 -------------
1948 -- C_To_PE --
1949 -------------
1951 function C_To_PE (C : PChar) return PE_Ptr is
1952 begin
1953 return new PE'(PC_Char, 1, EOP, C);
1954 end C_To_PE;
1956 ------------
1957 -- Cancel --
1958 ------------
1960 function Cancel return Pattern is
1961 begin
1962 return (AFC with 0, new PE'(PC_Cancel, 1, EOP));
1963 end Cancel;
1965 ------------
1966 -- Concat --
1967 ------------
1969 -- Concat needs to traverse the left operand performing the following
1970 -- set of fixups:
1972 -- a) Any successor pointers (Pthen fields) that are set to EOP are
1973 -- reset to point to the second operand.
1975 -- b) Any PC_Arbno_Y node has its stack count field incremented
1976 -- by the parameter Incr provided for this purpose.
1978 -- d) Num fields of all pattern elements in the left operand are
1979 -- adjusted to include the elements of the right operand.
1981 -- Note: we do not use Set_Successor in the processing for Concat, since
1982 -- there is no point in doing two traversals, we may as well do everything
1983 -- at the same time.
1985 function Concat (L, R : PE_Ptr; Incr : Natural) return PE_Ptr is
1986 begin
1987 if L = EOP then
1988 return R;
1990 elsif R = EOP then
1991 return L;
1993 else
1994 declare
1995 Refs : Ref_Array (1 .. L.Index);
1996 -- We build a reference array for L whose N'th element points to
1997 -- the pattern element of L whose original Index value is N.
1999 P : PE_Ptr;
2001 begin
2002 Build_Ref_Array (L, Refs);
2004 for J in Refs'Range loop
2005 P := Refs (J);
2007 P.Index := P.Index + R.Index;
2009 if P.Pcode = PC_Arbno_Y then
2010 P.Nat := P.Nat + Incr;
2011 end if;
2013 if P.Pthen = EOP then
2014 P.Pthen := R;
2015 end if;
2017 if P.Pcode in PC_Has_Alt and then P.Alt = EOP then
2018 P.Alt := R;
2019 end if;
2020 end loop;
2021 end;
2023 return L;
2024 end if;
2025 end Concat;
2027 ----------
2028 -- Copy --
2029 ----------
2031 function Copy (P : PE_Ptr) return PE_Ptr is
2032 begin
2033 if P = null then
2034 Uninitialized_Pattern;
2036 else
2037 declare
2038 Refs : Ref_Array (1 .. P.Index);
2039 -- References to elements in P, indexed by Index field
2041 Copy : Ref_Array (1 .. P.Index);
2042 -- Holds copies of elements of P, indexed by Index field
2044 E : PE_Ptr;
2046 begin
2047 Build_Ref_Array (P, Refs);
2049 -- Now copy all nodes
2051 for J in Refs'Range loop
2052 Copy (J) := new PE'(Refs (J).all);
2053 end loop;
2055 -- Adjust all internal references
2057 for J in Copy'Range loop
2058 E := Copy (J);
2060 -- Adjust successor pointer to point to copy
2062 if E.Pthen /= EOP then
2063 E.Pthen := Copy (E.Pthen.Index);
2064 end if;
2066 -- Adjust Alt pointer if there is one to point to copy
2068 if E.Pcode in PC_Has_Alt and then E.Alt /= EOP then
2069 E.Alt := Copy (E.Alt.Index);
2070 end if;
2072 -- Copy referenced string
2074 if E.Pcode = PC_String then
2075 E.Str := new String'(E.Str.all);
2076 end if;
2077 end loop;
2079 return Copy (P.Index);
2080 end;
2081 end if;
2082 end Copy;
2084 ----------
2085 -- Dump --
2086 ----------
2088 procedure Dump (P : Pattern) is
2090 subtype Count is Ada.Text_IO.Count;
2091 Scol : Count;
2092 -- Used to keep track of column in dump output
2094 Refs : Ref_Array (1 .. P.P.Index);
2095 -- We build a reference array whose N'th element points to the
2096 -- pattern element whose Index value is N.
2098 Cols : Natural := 2;
2099 -- Number of columns used for pattern numbers, minimum is 2
2101 E : PE_Ptr;
2103 procedure Write_Node_Id (E : PE_Ptr);
2104 -- Writes out a string identifying the given pattern element
2106 -------------------
2107 -- Write_Node_Id --
2108 -------------------
2110 procedure Write_Node_Id (E : PE_Ptr) is
2111 begin
2112 if E = EOP then
2113 Put ("EOP");
2115 for J in 4 .. Cols loop
2116 Put (' ');
2117 end loop;
2119 else
2120 declare
2121 Str : String (1 .. Cols);
2122 N : Natural := Natural (E.Index);
2124 begin
2125 Put ("#");
2127 for J in reverse Str'Range loop
2128 Str (J) := Character'Val (48 + N mod 10);
2129 N := N / 10;
2130 end loop;
2132 Put (Str);
2133 end;
2134 end if;
2135 end Write_Node_Id;
2137 -- Start of processing for Dump
2139 begin
2140 New_Line;
2141 Put ("Pattern Dump Output (pattern at " &
2142 Image (P'Address) &
2143 ", S = " & Natural'Image (P.Stk) & ')');
2145 Scol := Col;
2146 New_Line;
2148 while Col < Scol loop
2149 Put ('-');
2150 end loop;
2152 New_Line;
2154 -- If uninitialized pattern, dump line and we are done
2156 if P.P = null then
2157 Put_Line ("Uninitialized pattern value");
2158 return;
2159 end if;
2161 -- If null pattern, just dump it and we are all done
2163 if P.P = EOP then
2164 Put_Line ("EOP (null pattern)");
2165 return;
2166 end if;
2168 Build_Ref_Array (P.P, Refs);
2170 -- Set number of columns required for node numbers
2172 while 10 ** Cols - 1 < Integer (P.P.Index) loop
2173 Cols := Cols + 1;
2174 end loop;
2176 -- Now dump the nodes in reverse sequence. We output them in reverse
2177 -- sequence since this corresponds to the natural order used to
2178 -- construct the patterns.
2180 for J in reverse Refs'Range loop
2181 E := Refs (J);
2182 Write_Node_Id (E);
2183 Set_Col (Count (Cols) + 4);
2184 Put (Image (E));
2185 Put (" ");
2186 Put (Pattern_Code'Image (E.Pcode));
2187 Put (" ");
2188 Set_Col (21 + Count (Cols) + Address_Image_Length);
2189 Write_Node_Id (E.Pthen);
2190 Set_Col (24 + 2 * Count (Cols) + Address_Image_Length);
2192 case E.Pcode is
2193 when PC_Alt
2194 | PC_Arb_X
2195 | PC_Arbno_S
2196 | PC_Arbno_X
2198 Write_Node_Id (E.Alt);
2200 when PC_Rpat =>
2201 Put (Str_PP (E.PP));
2203 when PC_Pred_Func =>
2204 Put (Str_BF (E.BF));
2206 when PC_Assign_Imm
2207 | PC_Assign_OnM
2208 | PC_Any_VP
2209 | PC_Break_VP
2210 | PC_BreakX_VP
2211 | PC_NotAny_VP
2212 | PC_NSpan_VP
2213 | PC_Span_VP
2214 | PC_String_VP
2216 Put (Str_VP (E.VP));
2218 when PC_Write_Imm
2219 | PC_Write_OnM
2221 Put (Str_FP (E.FP));
2223 when PC_String =>
2224 Put (Image (E.Str.all));
2226 when PC_String_2 =>
2227 Put (Image (E.Str2));
2229 when PC_String_3 =>
2230 Put (Image (E.Str3));
2232 when PC_String_4 =>
2233 Put (Image (E.Str4));
2235 when PC_String_5 =>
2236 Put (Image (E.Str5));
2238 when PC_String_6 =>
2239 Put (Image (E.Str6));
2241 when PC_Setcur =>
2242 Put (Str_NP (E.Var));
2244 when PC_Any_CH
2245 | PC_Break_CH
2246 | PC_BreakX_CH
2247 | PC_Char
2248 | PC_NotAny_CH
2249 | PC_NSpan_CH
2250 | PC_Span_CH
2252 Put (''' & E.Char & ''');
2254 when PC_Any_CS
2255 | PC_Break_CS
2256 | PC_BreakX_CS
2257 | PC_NotAny_CS
2258 | PC_NSpan_CS
2259 | PC_Span_CS
2261 Put ('"' & To_Sequence (E.CS) & '"');
2263 when PC_Arbno_Y
2264 | PC_Len_Nat
2265 | PC_Pos_Nat
2266 | PC_RPos_Nat
2267 | PC_RTab_Nat
2268 | PC_Tab_Nat
2270 Put (S (E.Nat));
2272 when PC_Pos_NF
2273 | PC_Len_NF
2274 | PC_RPos_NF
2275 | PC_RTab_NF
2276 | PC_Tab_NF
2278 Put (Str_NF (E.NF));
2280 when PC_Pos_NP
2281 | PC_Len_NP
2282 | PC_RPos_NP
2283 | PC_RTab_NP
2284 | PC_Tab_NP
2286 Put (Str_NP (E.NP));
2288 when PC_Any_VF
2289 | PC_Break_VF
2290 | PC_BreakX_VF
2291 | PC_NotAny_VF
2292 | PC_NSpan_VF
2293 | PC_Span_VF
2294 | PC_String_VF
2296 Put (Str_VF (E.VF));
2298 when others =>
2299 null;
2300 end case;
2302 New_Line;
2303 end loop;
2305 New_Line;
2306 end Dump;
2308 ----------
2309 -- Fail --
2310 ----------
2312 function Fail return Pattern is
2313 begin
2314 return (AFC with 0, new PE'(PC_Fail, 1, EOP));
2315 end Fail;
2317 -----------
2318 -- Fence --
2319 -----------
2321 -- Simple case
2323 function Fence return Pattern is
2324 begin
2325 return (AFC with 1, new PE'(PC_Fence, 1, EOP));
2326 end Fence;
2328 -- Function case
2330 -- +---+ +---+ +---+
2331 -- | E |---->| P |---->| X |---->
2332 -- +---+ +---+ +---+
2334 -- The node numbering of the constituent pattern P is not affected.
2335 -- Where N is the number of nodes in P, the X node is numbered N + 1,
2336 -- and the E node is N + 2.
2338 function Fence (P : Pattern) return Pattern is
2339 Pat : constant PE_Ptr := Copy (P.P);
2340 E : constant PE_Ptr := new PE'(PC_R_Enter, 0, EOP);
2341 X : constant PE_Ptr := new PE'(PC_Fence_X, 0, EOP);
2342 begin
2343 return (AFC with P.Stk + 1, Bracket (E, Pat, X));
2344 end Fence;
2346 --------------
2347 -- Finalize --
2348 --------------
2350 procedure Finalize (Object : in out Pattern) is
2352 procedure Free is new Ada.Unchecked_Deallocation (PE, PE_Ptr);
2353 procedure Free is new Ada.Unchecked_Deallocation (String, String_Ptr);
2355 begin
2356 -- Nothing to do if already freed
2358 if Object.P = null then
2359 return;
2361 -- Otherwise we must free all elements
2363 else
2364 declare
2365 Refs : Ref_Array (1 .. Object.P.Index);
2366 -- References to elements in pattern to be finalized
2368 begin
2369 Build_Ref_Array (Object.P, Refs);
2371 for J in Refs'Range loop
2372 if Refs (J).Pcode = PC_String then
2373 Free (Refs (J).Str);
2374 end if;
2376 Free (Refs (J));
2377 end loop;
2379 Object.P := null;
2380 end;
2381 end if;
2382 end Finalize;
2384 -----------
2385 -- Image --
2386 -----------
2388 function Image (P : PE_Ptr) return String is
2389 begin
2390 return Image (To_Address (P));
2391 end Image;
2393 function Image (P : Pattern) return String is
2394 begin
2395 return S (Image (P));
2396 end Image;
2398 function Image (P : Pattern) return VString is
2400 Kill_Ampersand : Boolean := False;
2401 -- Set True to delete next & to be output to Result
2403 Result : VString := Nul;
2404 -- The result is accumulated here, using Append
2406 Refs : Ref_Array (1 .. P.P.Index);
2407 -- We build a reference array whose N'th element points to the
2408 -- pattern element whose Index value is N.
2410 procedure Delete_Ampersand;
2411 -- Deletes the ampersand at the end of Result
2413 procedure Image_Seq (E : PE_Ptr; Succ : PE_Ptr; Paren : Boolean);
2414 -- E refers to a pattern structure whose successor is given by Succ.
2415 -- This procedure appends to Result a representation of this pattern.
2416 -- The Paren parameter indicates whether parentheses are required if
2417 -- the output is more than one element.
2419 procedure Image_One (E : in out PE_Ptr);
2420 -- E refers to a pattern structure. This procedure appends to Result
2421 -- a representation of the single simple or compound pattern structure
2422 -- at the start of E and updates E to point to its successor.
2424 ----------------------
2425 -- Delete_Ampersand --
2426 ----------------------
2428 procedure Delete_Ampersand is
2429 L : constant Natural := Length (Result);
2430 begin
2431 if L > 2 then
2432 Delete (Result, L - 1, L);
2433 end if;
2434 end Delete_Ampersand;
2436 ---------------
2437 -- Image_One --
2438 ---------------
2440 procedure Image_One (E : in out PE_Ptr) is
2442 ER : PE_Ptr := E.Pthen;
2443 -- Successor set as result in E unless reset
2445 begin
2446 case E.Pcode is
2447 when PC_Cancel =>
2448 Append (Result, "Cancel");
2450 when PC_Alt => Alt : declare
2452 Elmts_In_L : constant IndexT := E.Pthen.Index - E.Alt.Index;
2453 -- Number of elements in left pattern of alternation
2455 Lowest_In_L : constant IndexT := E.Index - Elmts_In_L;
2456 -- Number of lowest index in elements of left pattern
2458 E1 : PE_Ptr;
2460 begin
2461 -- The successor of the alternation node must have a lower
2462 -- index than any node that is in the left pattern or a
2463 -- higher index than the alternation node itself.
2465 while ER /= EOP
2466 and then ER.Index >= Lowest_In_L
2467 and then ER.Index < E.Index
2468 loop
2469 ER := ER.Pthen;
2470 end loop;
2472 Append (Result, '(');
2474 E1 := E;
2475 loop
2476 Image_Seq (E1.Pthen, ER, False);
2477 Append (Result, " or ");
2478 E1 := E1.Alt;
2479 exit when E1.Pcode /= PC_Alt;
2480 end loop;
2482 Image_Seq (E1, ER, False);
2483 Append (Result, ')');
2484 end Alt;
2486 when PC_Any_CS =>
2487 Append (Result, "Any (" & Image (To_Sequence (E.CS)) & ')');
2489 when PC_Any_VF =>
2490 Append (Result, "Any (" & Str_VF (E.VF) & ')');
2492 when PC_Any_VP =>
2493 Append (Result, "Any (" & Str_VP (E.VP) & ')');
2495 when PC_Arb_X =>
2496 Append (Result, "Arb");
2498 when PC_Arbno_S =>
2499 Append (Result, "Arbno (");
2500 Image_Seq (E.Alt, E, False);
2501 Append (Result, ')');
2503 when PC_Arbno_X =>
2504 Append (Result, "Arbno (");
2505 Image_Seq (E.Alt.Pthen, Refs (E.Index - 2), False);
2506 Append (Result, ')');
2508 when PC_Assign_Imm =>
2509 Delete_Ampersand;
2510 Append (Result, "* " & Str_VP (Refs (E.Index).VP));
2512 when PC_Assign_OnM =>
2513 Delete_Ampersand;
2514 Append (Result, "** " & Str_VP (Refs (E.Index).VP));
2516 when PC_Any_CH =>
2517 Append (Result, "Any ('" & E.Char & "')");
2519 when PC_Bal =>
2520 Append (Result, "Bal");
2522 when PC_Break_CH =>
2523 Append (Result, "Break ('" & E.Char & "')");
2525 when PC_Break_CS =>
2526 Append (Result, "Break (" & Image (To_Sequence (E.CS)) & ')');
2528 when PC_Break_VF =>
2529 Append (Result, "Break (" & Str_VF (E.VF) & ')');
2531 when PC_Break_VP =>
2532 Append (Result, "Break (" & Str_VP (E.VP) & ')');
2534 when PC_BreakX_CH =>
2535 Append (Result, "BreakX ('" & E.Char & "')");
2536 ER := ER.Pthen;
2538 when PC_BreakX_CS =>
2539 Append (Result, "BreakX (" & Image (To_Sequence (E.CS)) & ')');
2540 ER := ER.Pthen;
2542 when PC_BreakX_VF =>
2543 Append (Result, "BreakX (" & Str_VF (E.VF) & ')');
2544 ER := ER.Pthen;
2546 when PC_BreakX_VP =>
2547 Append (Result, "BreakX (" & Str_VP (E.VP) & ')');
2548 ER := ER.Pthen;
2550 when PC_Char =>
2551 Append (Result, ''' & E.Char & ''');
2553 when PC_Fail =>
2554 Append (Result, "Fail");
2556 when PC_Fence =>
2557 Append (Result, "Fence");
2559 when PC_Fence_X =>
2560 Append (Result, "Fence (");
2561 Image_Seq (E.Pthen, Refs (E.Index - 1), False);
2562 Append (Result, ")");
2563 ER := Refs (E.Index - 1).Pthen;
2565 when PC_Len_Nat =>
2566 Append (Result, "Len (" & E.Nat & ')');
2568 when PC_Len_NF =>
2569 Append (Result, "Len (" & Str_NF (E.NF) & ')');
2571 when PC_Len_NP =>
2572 Append (Result, "Len (" & Str_NP (E.NP) & ')');
2574 when PC_NotAny_CH =>
2575 Append (Result, "NotAny ('" & E.Char & "')");
2577 when PC_NotAny_CS =>
2578 Append (Result, "NotAny (" & Image (To_Sequence (E.CS)) & ')');
2580 when PC_NotAny_VF =>
2581 Append (Result, "NotAny (" & Str_VF (E.VF) & ')');
2583 when PC_NotAny_VP =>
2584 Append (Result, "NotAny (" & Str_VP (E.VP) & ')');
2586 when PC_NSpan_CH =>
2587 Append (Result, "NSpan ('" & E.Char & "')");
2589 when PC_NSpan_CS =>
2590 Append (Result, "NSpan (" & Image (To_Sequence (E.CS)) & ')');
2592 when PC_NSpan_VF =>
2593 Append (Result, "NSpan (" & Str_VF (E.VF) & ')');
2595 when PC_NSpan_VP =>
2596 Append (Result, "NSpan (" & Str_VP (E.VP) & ')');
2598 when PC_Null =>
2599 Append (Result, """""");
2601 when PC_Pos_Nat =>
2602 Append (Result, "Pos (" & E.Nat & ')');
2604 when PC_Pos_NF =>
2605 Append (Result, "Pos (" & Str_NF (E.NF) & ')');
2607 when PC_Pos_NP =>
2608 Append (Result, "Pos (" & Str_NP (E.NP) & ')');
2610 when PC_R_Enter =>
2611 Kill_Ampersand := True;
2613 when PC_Rest =>
2614 Append (Result, "Rest");
2616 when PC_Rpat =>
2617 Append (Result, "(+ " & Str_PP (E.PP) & ')');
2619 when PC_Pred_Func =>
2620 Append (Result, "(+ " & Str_BF (E.BF) & ')');
2622 when PC_RPos_Nat =>
2623 Append (Result, "RPos (" & E.Nat & ')');
2625 when PC_RPos_NF =>
2626 Append (Result, "RPos (" & Str_NF (E.NF) & ')');
2628 when PC_RPos_NP =>
2629 Append (Result, "RPos (" & Str_NP (E.NP) & ')');
2631 when PC_RTab_Nat =>
2632 Append (Result, "RTab (" & E.Nat & ')');
2634 when PC_RTab_NF =>
2635 Append (Result, "RTab (" & Str_NF (E.NF) & ')');
2637 when PC_RTab_NP =>
2638 Append (Result, "RTab (" & Str_NP (E.NP) & ')');
2640 when PC_Setcur =>
2641 Append (Result, "Setcur (" & Str_NP (E.Var) & ')');
2643 when PC_Span_CH =>
2644 Append (Result, "Span ('" & E.Char & "')");
2646 when PC_Span_CS =>
2647 Append (Result, "Span (" & Image (To_Sequence (E.CS)) & ')');
2649 when PC_Span_VF =>
2650 Append (Result, "Span (" & Str_VF (E.VF) & ')');
2652 when PC_Span_VP =>
2653 Append (Result, "Span (" & Str_VP (E.VP) & ')');
2655 when PC_String =>
2656 Append (Result, Image (E.Str.all));
2658 when PC_String_2 =>
2659 Append (Result, Image (E.Str2));
2661 when PC_String_3 =>
2662 Append (Result, Image (E.Str3));
2664 when PC_String_4 =>
2665 Append (Result, Image (E.Str4));
2667 when PC_String_5 =>
2668 Append (Result, Image (E.Str5));
2670 when PC_String_6 =>
2671 Append (Result, Image (E.Str6));
2673 when PC_String_VF =>
2674 Append (Result, "(+" & Str_VF (E.VF) & ')');
2676 when PC_String_VP =>
2677 Append (Result, "(+" & Str_VP (E.VP) & ')');
2679 when PC_Succeed =>
2680 Append (Result, "Succeed");
2682 when PC_Tab_Nat =>
2683 Append (Result, "Tab (" & E.Nat & ')');
2685 when PC_Tab_NF =>
2686 Append (Result, "Tab (" & Str_NF (E.NF) & ')');
2688 when PC_Tab_NP =>
2689 Append (Result, "Tab (" & Str_NP (E.NP) & ')');
2691 when PC_Write_Imm =>
2692 Append (Result, '(');
2693 Image_Seq (E, Refs (E.Index - 1), True);
2694 Append (Result, " * " & Str_FP (Refs (E.Index - 1).FP));
2695 ER := Refs (E.Index - 1).Pthen;
2697 when PC_Write_OnM =>
2698 Append (Result, '(');
2699 Image_Seq (E.Pthen, Refs (E.Index - 1), True);
2700 Append (Result, " ** " & Str_FP (Refs (E.Index - 1).FP));
2701 ER := Refs (E.Index - 1).Pthen;
2703 -- Other pattern codes should not appear as leading elements
2705 when PC_Arb_Y
2706 | PC_Arbno_Y
2707 | PC_Assign
2708 | PC_BreakX_X
2709 | PC_EOP
2710 | PC_Fence_Y
2711 | PC_R_Remove
2712 | PC_R_Restore
2713 | PC_Unanchored
2715 Append (Result, "???");
2716 end case;
2718 E := ER;
2719 end Image_One;
2721 ---------------
2722 -- Image_Seq --
2723 ---------------
2725 procedure Image_Seq (E : PE_Ptr; Succ : PE_Ptr; Paren : Boolean) is
2726 Indx : constant Natural := Length (Result);
2727 E1 : PE_Ptr := E;
2728 Mult : Boolean := False;
2730 begin
2731 -- The image of EOP is "" (the null string)
2733 if E = EOP then
2734 Append (Result, """""");
2736 -- Else generate appropriate concatenation sequence
2738 else
2739 loop
2740 Image_One (E1);
2741 exit when E1 = Succ;
2742 exit when E1 = EOP;
2743 Mult := True;
2745 if Kill_Ampersand then
2746 Kill_Ampersand := False;
2747 else
2748 Append (Result, " & ");
2749 end if;
2750 end loop;
2751 end if;
2753 if Mult and Paren then
2754 Insert (Result, Indx + 1, "(");
2755 Append (Result, ")");
2756 end if;
2757 end Image_Seq;
2759 -- Start of processing for Image
2761 begin
2762 Build_Ref_Array (P.P, Refs);
2763 Image_Seq (P.P, EOP, False);
2764 return Result;
2765 end Image;
2767 -----------
2768 -- Is_In --
2769 -----------
2771 function Is_In (C : Character; Str : String) return Boolean is
2772 begin
2773 for J in Str'Range loop
2774 if Str (J) = C then
2775 return True;
2776 end if;
2777 end loop;
2779 return False;
2780 end Is_In;
2782 ---------
2783 -- Len --
2784 ---------
2786 function Len (Count : Natural) return Pattern is
2787 begin
2788 -- Note, the following is not just an optimization, it is needed
2789 -- to ensure that Arbno (Len (0)) does not generate an infinite
2790 -- matching loop (since PC_Len_Nat is OK_For_Simple_Arbno).
2792 if Count = 0 then
2793 return (AFC with 0, new PE'(PC_Null, 1, EOP));
2795 else
2796 return (AFC with 0, new PE'(PC_Len_Nat, 1, EOP, Count));
2797 end if;
2798 end Len;
2800 function Len (Count : Natural_Func) return Pattern is
2801 begin
2802 return (AFC with 0, new PE'(PC_Len_NF, 1, EOP, Count));
2803 end Len;
2805 function Len (Count : not null access Natural) return Pattern is
2806 begin
2807 return (AFC with 0, new PE'(PC_Len_NP, 1, EOP, Natural_Ptr (Count)));
2808 end Len;
2810 -----------------
2811 -- Logic_Error --
2812 -----------------
2814 procedure Logic_Error is
2815 begin
2816 raise Program_Error with
2817 "Internal logic error in GNAT.Spitbol.Patterns";
2818 end Logic_Error;
2820 -----------
2821 -- Match --
2822 -----------
2824 function Match
2825 (Subject : VString;
2826 Pat : Pattern) return Boolean
2828 S : Big_String_Access;
2829 L : Natural;
2830 Start : Natural;
2831 Stop : Natural;
2832 pragma Unreferenced (Stop);
2834 begin
2835 Get_String (Subject, S, L);
2837 if Debug_Mode then
2838 XMatchD (S (1 .. L), Pat.P, Pat.Stk, Start, Stop);
2839 else
2840 XMatch (S (1 .. L), Pat.P, Pat.Stk, Start, Stop);
2841 end if;
2843 return Start /= 0;
2844 end Match;
2846 function Match
2847 (Subject : String;
2848 Pat : Pattern) return Boolean
2850 Start, Stop : Natural;
2851 pragma Unreferenced (Stop);
2853 subtype String1 is String (1 .. Subject'Length);
2855 begin
2856 if Debug_Mode then
2857 XMatchD (String1 (Subject), Pat.P, Pat.Stk, Start, Stop);
2858 else
2859 XMatch (String1 (Subject), Pat.P, Pat.Stk, Start, Stop);
2860 end if;
2862 return Start /= 0;
2863 end Match;
2865 function Match
2866 (Subject : VString_Var;
2867 Pat : Pattern;
2868 Replace : VString) return Boolean
2870 Start : Natural;
2871 Stop : Natural;
2872 S : Big_String_Access;
2873 L : Natural;
2875 begin
2876 Get_String (Subject, S, L);
2878 if Debug_Mode then
2879 XMatchD (S (1 .. L), Pat.P, Pat.Stk, Start, Stop);
2880 else
2881 XMatch (S (1 .. L), Pat.P, Pat.Stk, Start, Stop);
2882 end if;
2884 if Start = 0 then
2885 return False;
2886 else
2887 Get_String (Replace, S, L);
2888 Replace_Slice
2889 (Subject'Unrestricted_Access.all, Start, Stop, S (1 .. L));
2890 return True;
2891 end if;
2892 end Match;
2894 function Match
2895 (Subject : VString_Var;
2896 Pat : Pattern;
2897 Replace : String) return Boolean
2899 Start : Natural;
2900 Stop : Natural;
2901 S : Big_String_Access;
2902 L : Natural;
2904 begin
2905 Get_String (Subject, S, L);
2907 if Debug_Mode then
2908 XMatchD (S (1 .. L), Pat.P, Pat.Stk, Start, Stop);
2909 else
2910 XMatch (S (1 .. L), Pat.P, Pat.Stk, Start, Stop);
2911 end if;
2913 if Start = 0 then
2914 return False;
2915 else
2916 Replace_Slice
2917 (Subject'Unrestricted_Access.all, Start, Stop, Replace);
2918 return True;
2919 end if;
2920 end Match;
2922 procedure Match
2923 (Subject : VString;
2924 Pat : Pattern)
2926 S : Big_String_Access;
2927 L : Natural;
2929 Start : Natural;
2930 Stop : Natural;
2931 pragma Unreferenced (Start, Stop);
2933 begin
2934 Get_String (Subject, S, L);
2936 if Debug_Mode then
2937 XMatchD (S (1 .. L), Pat.P, Pat.Stk, Start, Stop);
2938 else
2939 XMatch (S (1 .. L), Pat.P, Pat.Stk, Start, Stop);
2940 end if;
2941 end Match;
2943 procedure Match
2944 (Subject : String;
2945 Pat : Pattern)
2947 Start, Stop : Natural;
2948 pragma Unreferenced (Start, Stop);
2950 subtype String1 is String (1 .. Subject'Length);
2952 begin
2953 if Debug_Mode then
2954 XMatchD (String1 (Subject), Pat.P, Pat.Stk, Start, Stop);
2955 else
2956 XMatch (String1 (Subject), Pat.P, Pat.Stk, Start, Stop);
2957 end if;
2958 end Match;
2960 procedure Match
2961 (Subject : in out VString;
2962 Pat : Pattern;
2963 Replace : VString)
2965 Start : Natural;
2966 Stop : Natural;
2967 S : Big_String_Access;
2968 L : Natural;
2970 begin
2971 Get_String (Subject, S, L);
2973 if Debug_Mode then
2974 XMatchD (S (1 .. L), Pat.P, Pat.Stk, Start, Stop);
2975 else
2976 XMatch (S (1 .. L), Pat.P, Pat.Stk, Start, Stop);
2977 end if;
2979 if Start /= 0 then
2980 Get_String (Replace, S, L);
2981 Replace_Slice (Subject, Start, Stop, S (1 .. L));
2982 end if;
2983 end Match;
2985 procedure Match
2986 (Subject : in out VString;
2987 Pat : Pattern;
2988 Replace : String)
2990 Start : Natural;
2991 Stop : Natural;
2992 S : Big_String_Access;
2993 L : Natural;
2995 begin
2996 Get_String (Subject, S, L);
2998 if Debug_Mode then
2999 XMatchD (S (1 .. L), Pat.P, Pat.Stk, Start, Stop);
3000 else
3001 XMatch (S (1 .. L), Pat.P, Pat.Stk, Start, Stop);
3002 end if;
3004 if Start /= 0 then
3005 Replace_Slice (Subject, Start, Stop, Replace);
3006 end if;
3007 end Match;
3009 function Match
3010 (Subject : VString;
3011 Pat : PString) return Boolean
3013 Pat_Len : constant Natural := Pat'Length;
3014 S : Big_String_Access;
3015 L : Natural;
3017 begin
3018 Get_String (Subject, S, L);
3020 if Anchored_Mode then
3021 if Pat_Len > L then
3022 return False;
3023 else
3024 return Pat = S (1 .. Pat_Len);
3025 end if;
3027 else
3028 for J in 1 .. L - Pat_Len + 1 loop
3029 if Pat = S (J .. J + (Pat_Len - 1)) then
3030 return True;
3031 end if;
3032 end loop;
3034 return False;
3035 end if;
3036 end Match;
3038 function Match
3039 (Subject : String;
3040 Pat : PString) return Boolean
3042 Pat_Len : constant Natural := Pat'Length;
3043 Sub_Len : constant Natural := Subject'Length;
3044 SFirst : constant Natural := Subject'First;
3046 begin
3047 if Anchored_Mode then
3048 if Pat_Len > Sub_Len then
3049 return False;
3050 else
3051 return Pat = Subject (SFirst .. SFirst + Pat_Len - 1);
3052 end if;
3054 else
3055 for J in SFirst .. SFirst + Sub_Len - Pat_Len loop
3056 if Pat = Subject (J .. J + (Pat_Len - 1)) then
3057 return True;
3058 end if;
3059 end loop;
3061 return False;
3062 end if;
3063 end Match;
3065 function Match
3066 (Subject : VString_Var;
3067 Pat : PString;
3068 Replace : VString) return Boolean
3070 Start : Natural;
3071 Stop : Natural;
3072 S : Big_String_Access;
3073 L : Natural;
3075 begin
3076 Get_String (Subject, S, L);
3078 if Debug_Mode then
3079 XMatchD (S (1 .. L), S_To_PE (Pat), 0, Start, Stop);
3080 else
3081 XMatch (S (1 .. L), S_To_PE (Pat), 0, Start, Stop);
3082 end if;
3084 if Start = 0 then
3085 return False;
3086 else
3087 Get_String (Replace, S, L);
3088 Replace_Slice
3089 (Subject'Unrestricted_Access.all, Start, Stop, S (1 .. L));
3090 return True;
3091 end if;
3092 end Match;
3094 function Match
3095 (Subject : VString_Var;
3096 Pat : PString;
3097 Replace : String) return Boolean
3099 Start : Natural;
3100 Stop : Natural;
3101 S : Big_String_Access;
3102 L : Natural;
3104 begin
3105 Get_String (Subject, S, L);
3107 if Debug_Mode then
3108 XMatchD (S (1 .. L), S_To_PE (Pat), 0, Start, Stop);
3109 else
3110 XMatch (S (1 .. L), S_To_PE (Pat), 0, Start, Stop);
3111 end if;
3113 if Start = 0 then
3114 return False;
3115 else
3116 Replace_Slice
3117 (Subject'Unrestricted_Access.all, Start, Stop, Replace);
3118 return True;
3119 end if;
3120 end Match;
3122 procedure Match
3123 (Subject : VString;
3124 Pat : PString)
3126 S : Big_String_Access;
3127 L : Natural;
3129 Start : Natural;
3130 Stop : Natural;
3131 pragma Unreferenced (Start, Stop);
3133 begin
3134 Get_String (Subject, S, L);
3136 if Debug_Mode then
3137 XMatchD (S (1 .. L), S_To_PE (Pat), 0, Start, Stop);
3138 else
3139 XMatch (S (1 .. L), S_To_PE (Pat), 0, Start, Stop);
3140 end if;
3141 end Match;
3143 procedure Match
3144 (Subject : String;
3145 Pat : PString)
3147 Start, Stop : Natural;
3148 pragma Unreferenced (Start, Stop);
3150 subtype String1 is String (1 .. Subject'Length);
3152 begin
3153 if Debug_Mode then
3154 XMatchD (String1 (Subject), S_To_PE (Pat), 0, Start, Stop);
3155 else
3156 XMatch (String1 (Subject), S_To_PE (Pat), 0, Start, Stop);
3157 end if;
3158 end Match;
3160 procedure Match
3161 (Subject : in out VString;
3162 Pat : PString;
3163 Replace : VString)
3165 Start : Natural;
3166 Stop : Natural;
3167 S : Big_String_Access;
3168 L : Natural;
3170 begin
3171 Get_String (Subject, S, L);
3173 if Debug_Mode then
3174 XMatchD (S (1 .. L), S_To_PE (Pat), 0, Start, Stop);
3175 else
3176 XMatch (S (1 .. L), S_To_PE (Pat), 0, Start, Stop);
3177 end if;
3179 if Start /= 0 then
3180 Get_String (Replace, S, L);
3181 Replace_Slice (Subject, Start, Stop, S (1 .. L));
3182 end if;
3183 end Match;
3185 procedure Match
3186 (Subject : in out VString;
3187 Pat : PString;
3188 Replace : String)
3190 Start : Natural;
3191 Stop : Natural;
3192 S : Big_String_Access;
3193 L : Natural;
3195 begin
3196 Get_String (Subject, S, L);
3198 if Debug_Mode then
3199 XMatchD (S (1 .. L), S_To_PE (Pat), 0, Start, Stop);
3200 else
3201 XMatch (S (1 .. L), S_To_PE (Pat), 0, Start, Stop);
3202 end if;
3204 if Start /= 0 then
3205 Replace_Slice (Subject, Start, Stop, Replace);
3206 end if;
3207 end Match;
3209 function Match
3210 (Subject : VString_Var;
3211 Pat : Pattern;
3212 Result : Match_Result_Var) return Boolean
3214 Start : Natural;
3215 Stop : Natural;
3216 S : Big_String_Access;
3217 L : Natural;
3219 begin
3220 Get_String (Subject, S, L);
3222 if Debug_Mode then
3223 XMatchD (S (1 .. L), Pat.P, Pat.Stk, Start, Stop);
3224 else
3225 XMatch (S (1 .. L), Pat.P, Pat.Stk, Start, Stop);
3226 end if;
3228 if Start = 0 then
3229 Result'Unrestricted_Access.all.Var := null;
3230 return False;
3232 else
3233 Result'Unrestricted_Access.all.Var := Subject'Unrestricted_Access;
3234 Result'Unrestricted_Access.all.Start := Start;
3235 Result'Unrestricted_Access.all.Stop := Stop;
3236 return True;
3237 end if;
3238 end Match;
3240 procedure Match
3241 (Subject : in out VString;
3242 Pat : Pattern;
3243 Result : out Match_Result)
3245 Start : Natural;
3246 Stop : Natural;
3247 S : Big_String_Access;
3248 L : Natural;
3250 begin
3251 Get_String (Subject, S, L);
3253 if Debug_Mode then
3254 XMatchD (S (1 .. L), Pat.P, Pat.Stk, Start, Stop);
3255 else
3256 XMatch (S (1 .. L), Pat.P, Pat.Stk, Start, Stop);
3257 end if;
3259 if Start = 0 then
3260 Result.Var := null;
3261 else
3262 Result.Var := Subject'Unrestricted_Access;
3263 Result.Start := Start;
3264 Result.Stop := Stop;
3265 end if;
3266 end Match;
3268 ---------------
3269 -- New_LineD --
3270 ---------------
3272 procedure New_LineD is
3273 begin
3274 if Internal_Debug then
3275 New_Line;
3276 end if;
3277 end New_LineD;
3279 ------------
3280 -- NotAny --
3281 ------------
3283 function NotAny (Str : String) return Pattern is
3284 begin
3285 return (AFC with 0, new PE'(PC_NotAny_CS, 1, EOP, To_Set (Str)));
3286 end NotAny;
3288 function NotAny (Str : VString) return Pattern is
3289 begin
3290 return NotAny (S (Str));
3291 end NotAny;
3293 function NotAny (Str : Character) return Pattern is
3294 begin
3295 return (AFC with 0, new PE'(PC_NotAny_CH, 1, EOP, Str));
3296 end NotAny;
3298 function NotAny (Str : Character_Set) return Pattern is
3299 begin
3300 return (AFC with 0, new PE'(PC_NotAny_CS, 1, EOP, Str));
3301 end NotAny;
3303 function NotAny (Str : not null access VString) return Pattern is
3304 begin
3305 return (AFC with 0, new PE'(PC_NotAny_VP, 1, EOP, VString_Ptr (Str)));
3306 end NotAny;
3308 function NotAny (Str : VString_Func) return Pattern is
3309 begin
3310 return (AFC with 0, new PE'(PC_NotAny_VF, 1, EOP, Str));
3311 end NotAny;
3313 -----------
3314 -- NSpan --
3315 -----------
3317 function NSpan (Str : String) return Pattern is
3318 begin
3319 return (AFC with 0, new PE'(PC_NSpan_CS, 1, EOP, To_Set (Str)));
3320 end NSpan;
3322 function NSpan (Str : VString) return Pattern is
3323 begin
3324 return NSpan (S (Str));
3325 end NSpan;
3327 function NSpan (Str : Character) return Pattern is
3328 begin
3329 return (AFC with 0, new PE'(PC_NSpan_CH, 1, EOP, Str));
3330 end NSpan;
3332 function NSpan (Str : Character_Set) return Pattern is
3333 begin
3334 return (AFC with 0, new PE'(PC_NSpan_CS, 1, EOP, Str));
3335 end NSpan;
3337 function NSpan (Str : not null access VString) return Pattern is
3338 begin
3339 return (AFC with 0, new PE'(PC_NSpan_VP, 1, EOP, VString_Ptr (Str)));
3340 end NSpan;
3342 function NSpan (Str : VString_Func) return Pattern is
3343 begin
3344 return (AFC with 0, new PE'(PC_NSpan_VF, 1, EOP, Str));
3345 end NSpan;
3347 ---------
3348 -- Pos --
3349 ---------
3351 function Pos (Count : Natural) return Pattern is
3352 begin
3353 return (AFC with 0, new PE'(PC_Pos_Nat, 1, EOP, Count));
3354 end Pos;
3356 function Pos (Count : Natural_Func) return Pattern is
3357 begin
3358 return (AFC with 0, new PE'(PC_Pos_NF, 1, EOP, Count));
3359 end Pos;
3361 function Pos (Count : not null access Natural) return Pattern is
3362 begin
3363 return (AFC with 0, new PE'(PC_Pos_NP, 1, EOP, Natural_Ptr (Count)));
3364 end Pos;
3366 ----------
3367 -- PutD --
3368 ----------
3370 procedure PutD (Str : String) is
3371 begin
3372 if Internal_Debug then
3373 Put (Str);
3374 end if;
3375 end PutD;
3377 ---------------
3378 -- Put_LineD --
3379 ---------------
3381 procedure Put_LineD (Str : String) is
3382 begin
3383 if Internal_Debug then
3384 Put_Line (Str);
3385 end if;
3386 end Put_LineD;
3388 -------------
3389 -- Replace --
3390 -------------
3392 procedure Replace
3393 (Result : in out Match_Result;
3394 Replace : VString)
3396 S : Big_String_Access;
3397 L : Natural;
3399 begin
3400 Get_String (Replace, S, L);
3402 if Result.Var /= null then
3403 Replace_Slice (Result.Var.all, Result.Start, Result.Stop, S (1 .. L));
3404 Result.Var := null;
3405 end if;
3406 end Replace;
3408 ----------
3409 -- Rest --
3410 ----------
3412 function Rest return Pattern is
3413 begin
3414 return (AFC with 0, new PE'(PC_Rest, 1, EOP));
3415 end Rest;
3417 ----------
3418 -- Rpos --
3419 ----------
3421 function Rpos (Count : Natural) return Pattern is
3422 begin
3423 return (AFC with 0, new PE'(PC_RPos_Nat, 1, EOP, Count));
3424 end Rpos;
3426 function Rpos (Count : Natural_Func) return Pattern is
3427 begin
3428 return (AFC with 0, new PE'(PC_RPos_NF, 1, EOP, Count));
3429 end Rpos;
3431 function Rpos (Count : not null access Natural) return Pattern is
3432 begin
3433 return (AFC with 0, new PE'(PC_RPos_NP, 1, EOP, Natural_Ptr (Count)));
3434 end Rpos;
3436 ----------
3437 -- Rtab --
3438 ----------
3440 function Rtab (Count : Natural) return Pattern is
3441 begin
3442 return (AFC with 0, new PE'(PC_RTab_Nat, 1, EOP, Count));
3443 end Rtab;
3445 function Rtab (Count : Natural_Func) return Pattern is
3446 begin
3447 return (AFC with 0, new PE'(PC_RTab_NF, 1, EOP, Count));
3448 end Rtab;
3450 function Rtab (Count : not null access Natural) return Pattern is
3451 begin
3452 return (AFC with 0, new PE'(PC_RTab_NP, 1, EOP, Natural_Ptr (Count)));
3453 end Rtab;
3455 -------------
3456 -- S_To_PE --
3457 -------------
3459 function S_To_PE (Str : PString) return PE_Ptr is
3460 Len : constant Natural := Str'Length;
3462 begin
3463 case Len is
3464 when 0 =>
3465 return new PE'(PC_Null, 1, EOP);
3467 when 1 =>
3468 return new PE'(PC_Char, 1, EOP, Str (Str'First));
3470 when 2 =>
3471 return new PE'(PC_String_2, 1, EOP, Str);
3473 when 3 =>
3474 return new PE'(PC_String_3, 1, EOP, Str);
3476 when 4 =>
3477 return new PE'(PC_String_4, 1, EOP, Str);
3479 when 5 =>
3480 return new PE'(PC_String_5, 1, EOP, Str);
3482 when 6 =>
3483 return new PE'(PC_String_6, 1, EOP, Str);
3485 when others =>
3486 return new PE'(PC_String, 1, EOP, new String'(Str));
3487 end case;
3488 end S_To_PE;
3490 -------------------
3491 -- Set_Successor --
3492 -------------------
3494 -- Note: this procedure is not used by the normal concatenation circuit,
3495 -- since other fixups are required on the left operand in this case, and
3496 -- they might as well be done all together.
3498 procedure Set_Successor (Pat : PE_Ptr; Succ : PE_Ptr) is
3499 begin
3500 if Pat = null then
3501 Uninitialized_Pattern;
3503 elsif Pat = EOP then
3504 Logic_Error;
3506 else
3507 declare
3508 Refs : Ref_Array (1 .. Pat.Index);
3509 -- We build a reference array for L whose N'th element points to
3510 -- the pattern element of L whose original Index value is N.
3512 P : PE_Ptr;
3514 begin
3515 Build_Ref_Array (Pat, Refs);
3517 for J in Refs'Range loop
3518 P := Refs (J);
3520 if P.Pthen = EOP then
3521 P.Pthen := Succ;
3522 end if;
3524 if P.Pcode in PC_Has_Alt and then P.Alt = EOP then
3525 P.Alt := Succ;
3526 end if;
3527 end loop;
3528 end;
3529 end if;
3530 end Set_Successor;
3532 ------------
3533 -- Setcur --
3534 ------------
3536 function Setcur (Var : not null access Natural) return Pattern is
3537 begin
3538 return (AFC with 0, new PE'(PC_Setcur, 1, EOP, Natural_Ptr (Var)));
3539 end Setcur;
3541 ----------
3542 -- Span --
3543 ----------
3545 function Span (Str : String) return Pattern is
3546 begin
3547 return (AFC with 0, new PE'(PC_Span_CS, 1, EOP, To_Set (Str)));
3548 end Span;
3550 function Span (Str : VString) return Pattern is
3551 begin
3552 return Span (S (Str));
3553 end Span;
3555 function Span (Str : Character) return Pattern is
3556 begin
3557 return (AFC with 0, new PE'(PC_Span_CH, 1, EOP, Str));
3558 end Span;
3560 function Span (Str : Character_Set) return Pattern is
3561 begin
3562 return (AFC with 0, new PE'(PC_Span_CS, 1, EOP, Str));
3563 end Span;
3565 function Span (Str : not null access VString) return Pattern is
3566 begin
3567 return (AFC with 0, new PE'(PC_Span_VP, 1, EOP, VString_Ptr (Str)));
3568 end Span;
3570 function Span (Str : VString_Func) return Pattern is
3571 begin
3572 return (AFC with 0, new PE'(PC_Span_VF, 1, EOP, Str));
3573 end Span;
3575 ------------
3576 -- Str_BF --
3577 ------------
3579 function Str_BF (A : Boolean_Func) return String is
3580 function To_A is new Ada.Unchecked_Conversion (Boolean_Func, Address);
3581 begin
3582 return "BF(" & Image (To_A (A)) & ')';
3583 end Str_BF;
3585 ------------
3586 -- Str_FP --
3587 ------------
3589 function Str_FP (A : File_Ptr) return String is
3590 begin
3591 return "FP(" & Image (A.all'Address) & ')';
3592 end Str_FP;
3594 ------------
3595 -- Str_NF --
3596 ------------
3598 function Str_NF (A : Natural_Func) return String is
3599 function To_A is new Ada.Unchecked_Conversion (Natural_Func, Address);
3600 begin
3601 return "NF(" & Image (To_A (A)) & ')';
3602 end Str_NF;
3604 ------------
3605 -- Str_NP --
3606 ------------
3608 function Str_NP (A : Natural_Ptr) return String is
3609 begin
3610 return "NP(" & Image (A.all'Address) & ')';
3611 end Str_NP;
3613 ------------
3614 -- Str_PP --
3615 ------------
3617 function Str_PP (A : Pattern_Ptr) return String is
3618 begin
3619 return "PP(" & Image (A.all'Address) & ')';
3620 end Str_PP;
3622 ------------
3623 -- Str_VF --
3624 ------------
3626 function Str_VF (A : VString_Func) return String is
3627 function To_A is new Ada.Unchecked_Conversion (VString_Func, Address);
3628 begin
3629 return "VF(" & Image (To_A (A)) & ')';
3630 end Str_VF;
3632 ------------
3633 -- Str_VP --
3634 ------------
3636 function Str_VP (A : VString_Ptr) return String is
3637 begin
3638 return "VP(" & Image (A.all'Address) & ')';
3639 end Str_VP;
3641 -------------
3642 -- Succeed --
3643 -------------
3645 function Succeed return Pattern is
3646 begin
3647 return (AFC with 1, new PE'(PC_Succeed, 1, EOP));
3648 end Succeed;
3650 ---------
3651 -- Tab --
3652 ---------
3654 function Tab (Count : Natural) return Pattern is
3655 begin
3656 return (AFC with 0, new PE'(PC_Tab_Nat, 1, EOP, Count));
3657 end Tab;
3659 function Tab (Count : Natural_Func) return Pattern is
3660 begin
3661 return (AFC with 0, new PE'(PC_Tab_NF, 1, EOP, Count));
3662 end Tab;
3664 function Tab (Count : not null access Natural) return Pattern is
3665 begin
3666 return (AFC with 0, new PE'(PC_Tab_NP, 1, EOP, Natural_Ptr (Count)));
3667 end Tab;
3669 ---------------------------
3670 -- Uninitialized_Pattern --
3671 ---------------------------
3673 procedure Uninitialized_Pattern is
3674 begin
3675 raise Program_Error with
3676 "uninitialized value of type GNAT.Spitbol.Patterns.Pattern";
3677 end Uninitialized_Pattern;
3679 ------------
3680 -- XMatch --
3681 ------------
3683 procedure XMatch
3684 (Subject : String;
3685 Pat_P : PE_Ptr;
3686 Pat_S : Natural;
3687 Start : out Natural;
3688 Stop : out Natural)
3690 Node : PE_Ptr;
3691 -- Pointer to current pattern node. Initialized from Pat_P, and then
3692 -- updated as the match proceeds through its constituent elements.
3694 Length : constant Natural := Subject'Length;
3695 -- Length of string (= Subject'Last, since Subject'First is always 1)
3697 Cursor : Integer := 0;
3698 -- If the value is non-negative, then this value is the index showing
3699 -- the current position of the match in the subject string. The next
3700 -- character to be matched is at Subject (Cursor + 1). Note that since
3701 -- our view of the subject string in XMatch always has a lower bound
3702 -- of one, regardless of original bounds, that this definition exactly
3703 -- corresponds to the cursor value as referenced by functions like Pos.
3705 -- If the value is negative, then this is a saved stack pointer,
3706 -- typically a base pointer of an inner or outer region. Cursor
3707 -- temporarily holds such a value when it is popped from the stack
3708 -- by Fail. In all cases, Cursor is reset to a proper non-negative
3709 -- cursor value before the match proceeds (e.g. by propagating the
3710 -- failure and popping a "real" cursor value from the stack.
3712 PE_Unanchored : aliased PE := (PC_Unanchored, 0, Pat_P);
3713 -- Dummy pattern element used in the unanchored case
3715 Stack : Stack_Type;
3716 -- The pattern matching failure stack for this call to Match
3718 Stack_Ptr : Stack_Range;
3719 -- Current stack pointer. This points to the top element of the stack
3720 -- that is currently in use. At the outer level this is the special
3721 -- entry placed on the stack according to the anchor mode.
3723 Stack_Init : constant Stack_Range := Stack'First + 1;
3724 -- This is the initial value of the Stack_Ptr and Stack_Base. The
3725 -- initial (Stack'First) element of the stack is not used so that
3726 -- when we pop the last element off, Stack_Ptr is still in range.
3728 Stack_Base : Stack_Range;
3729 -- This value is the stack base value, i.e. the stack pointer for the
3730 -- first history stack entry in the current stack region. See separate
3731 -- section on handling of recursive pattern matches.
3733 Assign_OnM : Boolean := False;
3734 -- Set True if assign-on-match or write-on-match operations may be
3735 -- present in the history stack, which must then be scanned on a
3736 -- successful match.
3738 procedure Pop_Region;
3739 pragma Inline (Pop_Region);
3740 -- Used at the end of processing of an inner region. If the inner
3741 -- region left no stack entries, then all trace of it is removed.
3742 -- Otherwise a PC_Restore_Region entry is pushed to ensure proper
3743 -- handling of alternatives in the inner region.
3745 procedure Push (Node : PE_Ptr);
3746 pragma Inline (Push);
3747 -- Make entry in pattern matching stack with current cursor value
3749 procedure Push_Region;
3750 pragma Inline (Push_Region);
3751 -- This procedure makes a new region on the history stack. The
3752 -- caller first establishes the special entry on the stack, but
3753 -- does not push the stack pointer. Then this call stacks a
3754 -- PC_Remove_Region node, on top of this entry, using the cursor
3755 -- field of the PC_Remove_Region entry to save the outer level
3756 -- stack base value, and resets the stack base to point to this
3757 -- PC_Remove_Region node.
3759 ----------------
3760 -- Pop_Region --
3761 ----------------
3763 procedure Pop_Region is
3764 begin
3765 -- If nothing was pushed in the inner region, we can just get
3766 -- rid of it entirely, leaving no traces that it was ever there
3768 if Stack_Ptr = Stack_Base then
3769 Stack_Ptr := Stack_Base - 2;
3770 Stack_Base := Stack (Stack_Ptr + 2).Cursor;
3772 -- If stuff was pushed in the inner region, then we have to
3773 -- push a PC_R_Restore node so that we properly handle possible
3774 -- rematches within the region.
3776 else
3777 Stack_Ptr := Stack_Ptr + 1;
3778 Stack (Stack_Ptr).Cursor := Stack_Base;
3779 Stack (Stack_Ptr).Node := CP_R_Restore'Access;
3780 Stack_Base := Stack (Stack_Base).Cursor;
3781 end if;
3782 end Pop_Region;
3784 ----------
3785 -- Push --
3786 ----------
3788 procedure Push (Node : PE_Ptr) is
3789 begin
3790 Stack_Ptr := Stack_Ptr + 1;
3791 Stack (Stack_Ptr).Cursor := Cursor;
3792 Stack (Stack_Ptr).Node := Node;
3793 end Push;
3795 -----------------
3796 -- Push_Region --
3797 -----------------
3799 procedure Push_Region is
3800 begin
3801 Stack_Ptr := Stack_Ptr + 2;
3802 Stack (Stack_Ptr).Cursor := Stack_Base;
3803 Stack (Stack_Ptr).Node := CP_R_Remove'Access;
3804 Stack_Base := Stack_Ptr;
3805 end Push_Region;
3807 -- Start of processing for XMatch
3809 begin
3810 if Pat_P = null then
3811 Uninitialized_Pattern;
3812 end if;
3814 -- Check we have enough stack for this pattern. This check deals with
3815 -- every possibility except a match of a recursive pattern, where we
3816 -- make a check at each recursion level.
3818 if Pat_S >= Stack_Size - 1 then
3819 raise Pattern_Stack_Overflow;
3820 end if;
3822 -- In anchored mode, the bottom entry on the stack is an abort entry
3824 if Anchored_Mode then
3825 Stack (Stack_Init).Node := CP_Cancel'Access;
3826 Stack (Stack_Init).Cursor := 0;
3828 -- In unanchored more, the bottom entry on the stack references
3829 -- the special pattern element PE_Unanchored, whose Pthen field
3830 -- points to the initial pattern element. The cursor value in this
3831 -- entry is the number of anchor moves so far.
3833 else
3834 Stack (Stack_Init).Node := PE_Unanchored'Unchecked_Access;
3835 Stack (Stack_Init).Cursor := 0;
3836 end if;
3838 Stack_Ptr := Stack_Init;
3839 Stack_Base := Stack_Ptr;
3840 Cursor := 0;
3841 Node := Pat_P;
3842 goto Match;
3844 -----------------------------------------
3845 -- Main Pattern Matching State Control --
3846 -----------------------------------------
3848 -- This is a state machine which uses gotos to change state. The
3849 -- initial state is Match, to initiate the matching of the first
3850 -- element, so the goto Match above starts the match. In the
3851 -- following descriptions, we indicate the global values that
3852 -- are relevant for the state transition.
3854 -- Come here if entire match fails
3856 <<Match_Fail>>
3857 Start := 0;
3858 Stop := 0;
3859 return;
3861 -- Come here if entire match succeeds
3863 -- Cursor current position in subject string
3865 <<Match_Succeed>>
3866 Start := Stack (Stack_Init).Cursor + 1;
3867 Stop := Cursor;
3869 -- Scan history stack for deferred assignments or writes
3871 if Assign_OnM then
3872 for S in Stack_Init .. Stack_Ptr loop
3873 if Stack (S).Node = CP_Assign'Access then
3874 declare
3875 Inner_Base : constant Stack_Range :=
3876 Stack (S + 1).Cursor;
3877 Special_Entry : constant Stack_Range :=
3878 Inner_Base - 1;
3879 Node_OnM : constant PE_Ptr :=
3880 Stack (Special_Entry).Node;
3881 Start : constant Natural :=
3882 Stack (Special_Entry).Cursor + 1;
3883 Stop : constant Natural := Stack (S).Cursor;
3885 begin
3886 if Node_OnM.Pcode = PC_Assign_OnM then
3887 Set_Unbounded_String
3888 (Node_OnM.VP.all, Subject (Start .. Stop));
3890 elsif Node_OnM.Pcode = PC_Write_OnM then
3891 Put_Line (Node_OnM.FP.all, Subject (Start .. Stop));
3893 else
3894 Logic_Error;
3895 end if;
3896 end;
3897 end if;
3898 end loop;
3899 end if;
3901 return;
3903 -- Come here if attempt to match current element fails
3905 -- Stack_Base current stack base
3906 -- Stack_Ptr current stack pointer
3908 <<Fail>>
3909 Cursor := Stack (Stack_Ptr).Cursor;
3910 Node := Stack (Stack_Ptr).Node;
3911 Stack_Ptr := Stack_Ptr - 1;
3912 goto Match;
3914 -- Come here if attempt to match current element succeeds
3916 -- Cursor current position in subject string
3917 -- Node pointer to node successfully matched
3918 -- Stack_Base current stack base
3919 -- Stack_Ptr current stack pointer
3921 <<Succeed>>
3922 Node := Node.Pthen;
3924 -- Come here to match the next pattern element
3926 -- Cursor current position in subject string
3927 -- Node pointer to node to be matched
3928 -- Stack_Base current stack base
3929 -- Stack_Ptr current stack pointer
3931 <<Match>>
3933 --------------------------------------------------
3934 -- Main Pattern Match Element Matching Routines --
3935 --------------------------------------------------
3937 -- Here is the case statement that processes the current node. The
3938 -- processing for each element does one of five things:
3940 -- goto Succeed to move to the successor
3941 -- goto Match_Succeed if the entire match succeeds
3942 -- goto Match_Fail if the entire match fails
3943 -- goto Fail to signal failure of current match
3945 -- Processing is NOT allowed to fall through
3947 case Node.Pcode is
3949 -- Cancel
3951 when PC_Cancel =>
3952 goto Match_Fail;
3954 -- Alternation
3956 when PC_Alt =>
3957 Push (Node.Alt);
3958 Node := Node.Pthen;
3959 goto Match;
3961 -- Any (one character case)
3963 when PC_Any_CH =>
3964 if Cursor < Length
3965 and then Subject (Cursor + 1) = Node.Char
3966 then
3967 Cursor := Cursor + 1;
3968 goto Succeed;
3969 else
3970 goto Fail;
3971 end if;
3973 -- Any (character set case)
3975 when PC_Any_CS =>
3976 if Cursor < Length
3977 and then Is_In (Subject (Cursor + 1), Node.CS)
3978 then
3979 Cursor := Cursor + 1;
3980 goto Succeed;
3981 else
3982 goto Fail;
3983 end if;
3985 -- Any (string function case)
3987 when PC_Any_VF => declare
3988 U : constant VString := Node.VF.all;
3989 S : Big_String_Access;
3990 L : Natural;
3992 begin
3993 Get_String (U, S, L);
3995 if Cursor < Length
3996 and then Is_In (Subject (Cursor + 1), S (1 .. L))
3997 then
3998 Cursor := Cursor + 1;
3999 goto Succeed;
4000 else
4001 goto Fail;
4002 end if;
4003 end;
4005 -- Any (string pointer case)
4007 when PC_Any_VP => declare
4008 U : constant VString := Node.VP.all;
4009 S : Big_String_Access;
4010 L : Natural;
4012 begin
4013 Get_String (U, S, L);
4015 if Cursor < Length
4016 and then Is_In (Subject (Cursor + 1), S (1 .. L))
4017 then
4018 Cursor := Cursor + 1;
4019 goto Succeed;
4020 else
4021 goto Fail;
4022 end if;
4023 end;
4025 -- Arb (initial match)
4027 when PC_Arb_X =>
4028 Push (Node.Alt);
4029 Node := Node.Pthen;
4030 goto Match;
4032 -- Arb (extension)
4034 when PC_Arb_Y =>
4035 if Cursor < Length then
4036 Cursor := Cursor + 1;
4037 Push (Node);
4038 goto Succeed;
4039 else
4040 goto Fail;
4041 end if;
4043 -- Arbno_S (simple Arbno initialize). This is the node that
4044 -- initiates the match of a simple Arbno structure.
4046 when PC_Arbno_S =>
4047 Push (Node.Alt);
4048 Node := Node.Pthen;
4049 goto Match;
4051 -- Arbno_X (Arbno initialize). This is the node that initiates
4052 -- the match of a complex Arbno structure.
4054 when PC_Arbno_X =>
4055 Push (Node.Alt);
4056 Node := Node.Pthen;
4057 goto Match;
4059 -- Arbno_Y (Arbno rematch). This is the node that is executed
4060 -- following successful matching of one instance of a complex
4061 -- Arbno pattern.
4063 when PC_Arbno_Y => declare
4064 Null_Match : constant Boolean :=
4065 Cursor = Stack (Stack_Base - 1).Cursor;
4067 begin
4068 Pop_Region;
4070 -- If arbno extension matched null, then immediately fail
4072 if Null_Match then
4073 goto Fail;
4074 end if;
4076 -- Here we must do a stack check to make sure enough stack
4077 -- is left. This check will happen once for each instance of
4078 -- the Arbno pattern that is matched. The Nat field of a
4079 -- PC_Arbno pattern contains the maximum stack entries needed
4080 -- for the Arbno with one instance and the successor pattern
4082 if Stack_Ptr + Node.Nat >= Stack'Last then
4083 raise Pattern_Stack_Overflow;
4084 end if;
4086 goto Succeed;
4087 end;
4089 -- Assign. If this node is executed, it means the assign-on-match
4090 -- or write-on-match operation will not happen after all, so we
4091 -- is propagate the failure, removing the PC_Assign node.
4093 when PC_Assign =>
4094 goto Fail;
4096 -- Assign immediate. This node performs the actual assignment
4098 when PC_Assign_Imm =>
4099 Set_Unbounded_String
4100 (Node.VP.all,
4101 Subject (Stack (Stack_Base - 1).Cursor + 1 .. Cursor));
4102 Pop_Region;
4103 goto Succeed;
4105 -- Assign on match. This node sets up for the eventual assignment
4107 when PC_Assign_OnM =>
4108 Stack (Stack_Base - 1).Node := Node;
4109 Push (CP_Assign'Access);
4110 Pop_Region;
4111 Assign_OnM := True;
4112 goto Succeed;
4114 -- Bal
4116 when PC_Bal =>
4117 if Cursor >= Length or else Subject (Cursor + 1) = ')' then
4118 goto Fail;
4120 elsif Subject (Cursor + 1) = '(' then
4121 declare
4122 Paren_Count : Natural := 1;
4124 begin
4125 loop
4126 Cursor := Cursor + 1;
4128 if Cursor >= Length then
4129 goto Fail;
4131 elsif Subject (Cursor + 1) = '(' then
4132 Paren_Count := Paren_Count + 1;
4134 elsif Subject (Cursor + 1) = ')' then
4135 Paren_Count := Paren_Count - 1;
4136 exit when Paren_Count = 0;
4137 end if;
4138 end loop;
4139 end;
4140 end if;
4142 Cursor := Cursor + 1;
4143 Push (Node);
4144 goto Succeed;
4146 -- Break (one character case)
4148 when PC_Break_CH =>
4149 while Cursor < Length loop
4150 if Subject (Cursor + 1) = Node.Char then
4151 goto Succeed;
4152 else
4153 Cursor := Cursor + 1;
4154 end if;
4155 end loop;
4157 goto Fail;
4159 -- Break (character set case)
4161 when PC_Break_CS =>
4162 while Cursor < Length loop
4163 if Is_In (Subject (Cursor + 1), Node.CS) then
4164 goto Succeed;
4165 else
4166 Cursor := Cursor + 1;
4167 end if;
4168 end loop;
4170 goto Fail;
4172 -- Break (string function case)
4174 when PC_Break_VF => declare
4175 U : constant VString := Node.VF.all;
4176 S : Big_String_Access;
4177 L : Natural;
4179 begin
4180 Get_String (U, S, L);
4182 while Cursor < Length loop
4183 if Is_In (Subject (Cursor + 1), S (1 .. L)) then
4184 goto Succeed;
4185 else
4186 Cursor := Cursor + 1;
4187 end if;
4188 end loop;
4190 goto Fail;
4191 end;
4193 -- Break (string pointer case)
4195 when PC_Break_VP => declare
4196 U : constant VString := Node.VP.all;
4197 S : Big_String_Access;
4198 L : Natural;
4200 begin
4201 Get_String (U, S, L);
4203 while Cursor < Length loop
4204 if Is_In (Subject (Cursor + 1), S (1 .. L)) then
4205 goto Succeed;
4206 else
4207 Cursor := Cursor + 1;
4208 end if;
4209 end loop;
4211 goto Fail;
4212 end;
4214 -- BreakX (one character case)
4216 when PC_BreakX_CH =>
4217 while Cursor < Length loop
4218 if Subject (Cursor + 1) = Node.Char then
4219 goto Succeed;
4220 else
4221 Cursor := Cursor + 1;
4222 end if;
4223 end loop;
4225 goto Fail;
4227 -- BreakX (character set case)
4229 when PC_BreakX_CS =>
4230 while Cursor < Length loop
4231 if Is_In (Subject (Cursor + 1), Node.CS) then
4232 goto Succeed;
4233 else
4234 Cursor := Cursor + 1;
4235 end if;
4236 end loop;
4238 goto Fail;
4240 -- BreakX (string function case)
4242 when PC_BreakX_VF => declare
4243 U : constant VString := Node.VF.all;
4244 S : Big_String_Access;
4245 L : Natural;
4247 begin
4248 Get_String (U, S, L);
4250 while Cursor < Length loop
4251 if Is_In (Subject (Cursor + 1), S (1 .. L)) then
4252 goto Succeed;
4253 else
4254 Cursor := Cursor + 1;
4255 end if;
4256 end loop;
4258 goto Fail;
4259 end;
4261 -- BreakX (string pointer case)
4263 when PC_BreakX_VP => declare
4264 U : constant VString := Node.VP.all;
4265 S : Big_String_Access;
4266 L : Natural;
4268 begin
4269 Get_String (U, S, L);
4271 while Cursor < Length loop
4272 if Is_In (Subject (Cursor + 1), S (1 .. L)) then
4273 goto Succeed;
4274 else
4275 Cursor := Cursor + 1;
4276 end if;
4277 end loop;
4279 goto Fail;
4280 end;
4282 -- BreakX_X (BreakX extension). See section on "Compound Pattern
4283 -- Structures". This node is the alternative that is stacked to
4284 -- skip past the break character and extend the break.
4286 when PC_BreakX_X =>
4287 Cursor := Cursor + 1;
4288 goto Succeed;
4290 -- Character (one character string)
4292 when PC_Char =>
4293 if Cursor < Length
4294 and then Subject (Cursor + 1) = Node.Char
4295 then
4296 Cursor := Cursor + 1;
4297 goto Succeed;
4298 else
4299 goto Fail;
4300 end if;
4302 -- End of Pattern
4304 when PC_EOP =>
4305 if Stack_Base = Stack_Init then
4306 goto Match_Succeed;
4308 -- End of recursive inner match. See separate section on
4309 -- handing of recursive pattern matches for details.
4311 else
4312 Node := Stack (Stack_Base - 1).Node;
4313 Pop_Region;
4314 goto Match;
4315 end if;
4317 -- Fail
4319 when PC_Fail =>
4320 goto Fail;
4322 -- Fence (built in pattern)
4324 when PC_Fence =>
4325 Push (CP_Cancel'Access);
4326 goto Succeed;
4328 -- Fence function node X. This is the node that gets control
4329 -- after a successful match of the fenced pattern.
4331 when PC_Fence_X =>
4332 Stack_Ptr := Stack_Ptr + 1;
4333 Stack (Stack_Ptr).Cursor := Stack_Base;
4334 Stack (Stack_Ptr).Node := CP_Fence_Y'Access;
4335 Stack_Base := Stack (Stack_Base).Cursor;
4336 goto Succeed;
4338 -- Fence function node Y. This is the node that gets control on
4339 -- a failure that occurs after the fenced pattern has matched.
4341 -- Note: the Cursor at this stage is actually the inner stack
4342 -- base value. We don't reset this, but we do use it to strip
4343 -- off all the entries made by the fenced pattern.
4345 when PC_Fence_Y =>
4346 Stack_Ptr := Cursor - 2;
4347 goto Fail;
4349 -- Len (integer case)
4351 when PC_Len_Nat =>
4352 if Cursor + Node.Nat > Length then
4353 goto Fail;
4354 else
4355 Cursor := Cursor + Node.Nat;
4356 goto Succeed;
4357 end if;
4359 -- Len (Integer function case)
4361 when PC_Len_NF => declare
4362 N : constant Natural := Node.NF.all;
4363 begin
4364 if Cursor + N > Length then
4365 goto Fail;
4366 else
4367 Cursor := Cursor + N;
4368 goto Succeed;
4369 end if;
4370 end;
4372 -- Len (integer pointer case)
4374 when PC_Len_NP =>
4375 if Cursor + Node.NP.all > Length then
4376 goto Fail;
4377 else
4378 Cursor := Cursor + Node.NP.all;
4379 goto Succeed;
4380 end if;
4382 -- NotAny (one character case)
4384 when PC_NotAny_CH =>
4385 if Cursor < Length
4386 and then Subject (Cursor + 1) /= Node.Char
4387 then
4388 Cursor := Cursor + 1;
4389 goto Succeed;
4390 else
4391 goto Fail;
4392 end if;
4394 -- NotAny (character set case)
4396 when PC_NotAny_CS =>
4397 if Cursor < Length
4398 and then not Is_In (Subject (Cursor + 1), Node.CS)
4399 then
4400 Cursor := Cursor + 1;
4401 goto Succeed;
4402 else
4403 goto Fail;
4404 end if;
4406 -- NotAny (string function case)
4408 when PC_NotAny_VF => declare
4409 U : constant VString := Node.VF.all;
4410 S : Big_String_Access;
4411 L : Natural;
4413 begin
4414 Get_String (U, S, L);
4416 if Cursor < Length
4417 and then
4418 not Is_In (Subject (Cursor + 1), S (1 .. L))
4419 then
4420 Cursor := Cursor + 1;
4421 goto Succeed;
4422 else
4423 goto Fail;
4424 end if;
4425 end;
4427 -- NotAny (string pointer case)
4429 when PC_NotAny_VP => declare
4430 U : constant VString := Node.VP.all;
4431 S : Big_String_Access;
4432 L : Natural;
4434 begin
4435 Get_String (U, S, L);
4437 if Cursor < Length
4438 and then
4439 not Is_In (Subject (Cursor + 1), S (1 .. L))
4440 then
4441 Cursor := Cursor + 1;
4442 goto Succeed;
4443 else
4444 goto Fail;
4445 end if;
4446 end;
4448 -- NSpan (one character case)
4450 when PC_NSpan_CH =>
4451 while Cursor < Length
4452 and then Subject (Cursor + 1) = Node.Char
4453 loop
4454 Cursor := Cursor + 1;
4455 end loop;
4457 goto Succeed;
4459 -- NSpan (character set case)
4461 when PC_NSpan_CS =>
4462 while Cursor < Length
4463 and then Is_In (Subject (Cursor + 1), Node.CS)
4464 loop
4465 Cursor := Cursor + 1;
4466 end loop;
4468 goto Succeed;
4470 -- NSpan (string function case)
4472 when PC_NSpan_VF => declare
4473 U : constant VString := Node.VF.all;
4474 S : Big_String_Access;
4475 L : Natural;
4477 begin
4478 Get_String (U, S, L);
4480 while Cursor < Length
4481 and then Is_In (Subject (Cursor + 1), S (1 .. L))
4482 loop
4483 Cursor := Cursor + 1;
4484 end loop;
4486 goto Succeed;
4487 end;
4489 -- NSpan (string pointer case)
4491 when PC_NSpan_VP => declare
4492 U : constant VString := Node.VP.all;
4493 S : Big_String_Access;
4494 L : Natural;
4496 begin
4497 Get_String (U, S, L);
4499 while Cursor < Length
4500 and then Is_In (Subject (Cursor + 1), S (1 .. L))
4501 loop
4502 Cursor := Cursor + 1;
4503 end loop;
4505 goto Succeed;
4506 end;
4508 -- Null string
4510 when PC_Null =>
4511 goto Succeed;
4513 -- Pos (integer case)
4515 when PC_Pos_Nat =>
4516 if Cursor = Node.Nat then
4517 goto Succeed;
4518 else
4519 goto Fail;
4520 end if;
4522 -- Pos (Integer function case)
4524 when PC_Pos_NF => declare
4525 N : constant Natural := Node.NF.all;
4526 begin
4527 if Cursor = N then
4528 goto Succeed;
4529 else
4530 goto Fail;
4531 end if;
4532 end;
4534 -- Pos (integer pointer case)
4536 when PC_Pos_NP =>
4537 if Cursor = Node.NP.all then
4538 goto Succeed;
4539 else
4540 goto Fail;
4541 end if;
4543 -- Predicate function
4545 when PC_Pred_Func =>
4546 if Node.BF.all then
4547 goto Succeed;
4548 else
4549 goto Fail;
4550 end if;
4552 -- Region Enter. Initiate new pattern history stack region
4554 when PC_R_Enter =>
4555 Stack (Stack_Ptr + 1).Cursor := Cursor;
4556 Push_Region;
4557 goto Succeed;
4559 -- Region Remove node. This is the node stacked by an R_Enter.
4560 -- It removes the special format stack entry right underneath, and
4561 -- then restores the outer level stack base and signals failure.
4563 -- Note: the cursor value at this stage is actually the (negative)
4564 -- stack base value for the outer level.
4566 when PC_R_Remove =>
4567 Stack_Base := Cursor;
4568 Stack_Ptr := Stack_Ptr - 1;
4569 goto Fail;
4571 -- Region restore node. This is the node stacked at the end of an
4572 -- inner level match. Its function is to restore the inner level
4573 -- region, so that alternatives in this region can be sought.
4575 -- Note: the Cursor at this stage is actually the negative of the
4576 -- inner stack base value, which we use to restore the inner region.
4578 when PC_R_Restore =>
4579 Stack_Base := Cursor;
4580 goto Fail;
4582 -- Rest
4584 when PC_Rest =>
4585 Cursor := Length;
4586 goto Succeed;
4588 -- Initiate recursive match (pattern pointer case)
4590 when PC_Rpat =>
4591 Stack (Stack_Ptr + 1).Node := Node.Pthen;
4592 Push_Region;
4594 if Stack_Ptr + Node.PP.all.Stk >= Stack_Size then
4595 raise Pattern_Stack_Overflow;
4596 else
4597 Node := Node.PP.all.P;
4598 goto Match;
4599 end if;
4601 -- RPos (integer case)
4603 when PC_RPos_Nat =>
4604 if Cursor = (Length - Node.Nat) then
4605 goto Succeed;
4606 else
4607 goto Fail;
4608 end if;
4610 -- RPos (integer function case)
4612 when PC_RPos_NF => declare
4613 N : constant Natural := Node.NF.all;
4614 begin
4615 if Length - Cursor = N then
4616 goto Succeed;
4617 else
4618 goto Fail;
4619 end if;
4620 end;
4622 -- RPos (integer pointer case)
4624 when PC_RPos_NP =>
4625 if Cursor = (Length - Node.NP.all) then
4626 goto Succeed;
4627 else
4628 goto Fail;
4629 end if;
4631 -- RTab (integer case)
4633 when PC_RTab_Nat =>
4634 if Cursor <= (Length - Node.Nat) then
4635 Cursor := Length - Node.Nat;
4636 goto Succeed;
4637 else
4638 goto Fail;
4639 end if;
4641 -- RTab (integer function case)
4643 when PC_RTab_NF => declare
4644 N : constant Natural := Node.NF.all;
4645 begin
4646 if Length - Cursor >= N then
4647 Cursor := Length - N;
4648 goto Succeed;
4649 else
4650 goto Fail;
4651 end if;
4652 end;
4654 -- RTab (integer pointer case)
4656 when PC_RTab_NP =>
4657 if Cursor <= (Length - Node.NP.all) then
4658 Cursor := Length - Node.NP.all;
4659 goto Succeed;
4660 else
4661 goto Fail;
4662 end if;
4664 -- Cursor assignment
4666 when PC_Setcur =>
4667 Node.Var.all := Cursor;
4668 goto Succeed;
4670 -- Span (one character case)
4672 when PC_Span_CH => declare
4673 P : Natural;
4675 begin
4676 P := Cursor;
4677 while P < Length
4678 and then Subject (P + 1) = Node.Char
4679 loop
4680 P := P + 1;
4681 end loop;
4683 if P /= Cursor then
4684 Cursor := P;
4685 goto Succeed;
4686 else
4687 goto Fail;
4688 end if;
4689 end;
4691 -- Span (character set case)
4693 when PC_Span_CS => declare
4694 P : Natural;
4696 begin
4697 P := Cursor;
4698 while P < Length
4699 and then Is_In (Subject (P + 1), Node.CS)
4700 loop
4701 P := P + 1;
4702 end loop;
4704 if P /= Cursor then
4705 Cursor := P;
4706 goto Succeed;
4707 else
4708 goto Fail;
4709 end if;
4710 end;
4712 -- Span (string function case)
4714 when PC_Span_VF => declare
4715 U : constant VString := Node.VF.all;
4716 S : Big_String_Access;
4717 L : Natural;
4718 P : Natural;
4720 begin
4721 Get_String (U, S, L);
4723 P := Cursor;
4724 while P < Length
4725 and then Is_In (Subject (P + 1), S (1 .. L))
4726 loop
4727 P := P + 1;
4728 end loop;
4730 if P /= Cursor then
4731 Cursor := P;
4732 goto Succeed;
4733 else
4734 goto Fail;
4735 end if;
4736 end;
4738 -- Span (string pointer case)
4740 when PC_Span_VP => declare
4741 U : constant VString := Node.VP.all;
4742 S : Big_String_Access;
4743 L : Natural;
4744 P : Natural;
4746 begin
4747 Get_String (U, S, L);
4749 P := Cursor;
4750 while P < Length
4751 and then Is_In (Subject (P + 1), S (1 .. L))
4752 loop
4753 P := P + 1;
4754 end loop;
4756 if P /= Cursor then
4757 Cursor := P;
4758 goto Succeed;
4759 else
4760 goto Fail;
4761 end if;
4762 end;
4764 -- String (two character case)
4766 when PC_String_2 =>
4767 if (Length - Cursor) >= 2
4768 and then Subject (Cursor + 1 .. Cursor + 2) = Node.Str2
4769 then
4770 Cursor := Cursor + 2;
4771 goto Succeed;
4772 else
4773 goto Fail;
4774 end if;
4776 -- String (three character case)
4778 when PC_String_3 =>
4779 if (Length - Cursor) >= 3
4780 and then Subject (Cursor + 1 .. Cursor + 3) = Node.Str3
4781 then
4782 Cursor := Cursor + 3;
4783 goto Succeed;
4784 else
4785 goto Fail;
4786 end if;
4788 -- String (four character case)
4790 when PC_String_4 =>
4791 if (Length - Cursor) >= 4
4792 and then Subject (Cursor + 1 .. Cursor + 4) = Node.Str4
4793 then
4794 Cursor := Cursor + 4;
4795 goto Succeed;
4796 else
4797 goto Fail;
4798 end if;
4800 -- String (five character case)
4802 when PC_String_5 =>
4803 if (Length - Cursor) >= 5
4804 and then Subject (Cursor + 1 .. Cursor + 5) = Node.Str5
4805 then
4806 Cursor := Cursor + 5;
4807 goto Succeed;
4808 else
4809 goto Fail;
4810 end if;
4812 -- String (six character case)
4814 when PC_String_6 =>
4815 if (Length - Cursor) >= 6
4816 and then Subject (Cursor + 1 .. Cursor + 6) = Node.Str6
4817 then
4818 Cursor := Cursor + 6;
4819 goto Succeed;
4820 else
4821 goto Fail;
4822 end if;
4824 -- String (case of more than six characters)
4826 when PC_String => declare
4827 Len : constant Natural := Node.Str'Length;
4828 begin
4829 if (Length - Cursor) >= Len
4830 and then Node.Str.all = Subject (Cursor + 1 .. Cursor + Len)
4831 then
4832 Cursor := Cursor + Len;
4833 goto Succeed;
4834 else
4835 goto Fail;
4836 end if;
4837 end;
4839 -- String (function case)
4841 when PC_String_VF => declare
4842 U : constant VString := Node.VF.all;
4843 S : Big_String_Access;
4844 L : Natural;
4846 begin
4847 Get_String (U, S, L);
4849 if (Length - Cursor) >= L
4850 and then S (1 .. L) = Subject (Cursor + 1 .. Cursor + L)
4851 then
4852 Cursor := Cursor + L;
4853 goto Succeed;
4854 else
4855 goto Fail;
4856 end if;
4857 end;
4859 -- String (pointer case)
4861 when PC_String_VP => declare
4862 U : constant VString := Node.VP.all;
4863 S : Big_String_Access;
4864 L : Natural;
4866 begin
4867 Get_String (U, S, L);
4869 if (Length - Cursor) >= L
4870 and then S (1 .. L) = Subject (Cursor + 1 .. Cursor + L)
4871 then
4872 Cursor := Cursor + L;
4873 goto Succeed;
4874 else
4875 goto Fail;
4876 end if;
4877 end;
4879 -- Succeed
4881 when PC_Succeed =>
4882 Push (Node);
4883 goto Succeed;
4885 -- Tab (integer case)
4887 when PC_Tab_Nat =>
4888 if Cursor <= Node.Nat then
4889 Cursor := Node.Nat;
4890 goto Succeed;
4891 else
4892 goto Fail;
4893 end if;
4895 -- Tab (integer function case)
4897 when PC_Tab_NF => declare
4898 N : constant Natural := Node.NF.all;
4899 begin
4900 if Cursor <= N then
4901 Cursor := N;
4902 goto Succeed;
4903 else
4904 goto Fail;
4905 end if;
4906 end;
4908 -- Tab (integer pointer case)
4910 when PC_Tab_NP =>
4911 if Cursor <= Node.NP.all then
4912 Cursor := Node.NP.all;
4913 goto Succeed;
4914 else
4915 goto Fail;
4916 end if;
4918 -- Unanchored movement
4920 when PC_Unanchored =>
4922 -- All done if we tried every position
4924 if Cursor > Length then
4925 goto Match_Fail;
4927 -- Otherwise extend the anchor point, and restack ourself
4929 else
4930 Cursor := Cursor + 1;
4931 Push (Node);
4932 goto Succeed;
4933 end if;
4935 -- Write immediate. This node performs the actual write
4937 when PC_Write_Imm =>
4938 Put_Line
4939 (Node.FP.all,
4940 Subject (Stack (Stack_Base - 1).Cursor + 1 .. Cursor));
4941 Pop_Region;
4942 goto Succeed;
4944 -- Write on match. This node sets up for the eventual write
4946 when PC_Write_OnM =>
4947 Stack (Stack_Base - 1).Node := Node;
4948 Push (CP_Assign'Access);
4949 Pop_Region;
4950 Assign_OnM := True;
4951 goto Succeed;
4952 end case;
4954 -- We are NOT allowed to fall though this case statement, since every
4955 -- match routine must end by executing a goto to the appropriate point
4956 -- in the finite state machine model.
4958 pragma Warnings (Off);
4959 Logic_Error;
4960 pragma Warnings (On);
4961 end XMatch;
4963 -------------
4964 -- XMatchD --
4965 -------------
4967 -- Maintenance note: There is a LOT of code duplication between XMatch
4968 -- and XMatchD. This is quite intentional, the point is to avoid any
4969 -- unnecessary debugging overhead in the XMatch case, but this does mean
4970 -- that any changes to XMatchD must be mirrored in XMatch. In case of
4971 -- any major changes, the proper approach is to delete XMatch, make the
4972 -- changes to XMatchD, and then make a copy of XMatchD, removing all
4973 -- calls to Dout, and all Put and Put_Line operations. This copy becomes
4974 -- the new XMatch.
4976 procedure XMatchD
4977 (Subject : String;
4978 Pat_P : PE_Ptr;
4979 Pat_S : Natural;
4980 Start : out Natural;
4981 Stop : out Natural)
4983 Node : PE_Ptr;
4984 -- Pointer to current pattern node. Initialized from Pat_P, and then
4985 -- updated as the match proceeds through its constituent elements.
4987 Length : constant Natural := Subject'Length;
4988 -- Length of string (= Subject'Last, since Subject'First is always 1)
4990 Cursor : Integer := 0;
4991 -- If the value is non-negative, then this value is the index showing
4992 -- the current position of the match in the subject string. The next
4993 -- character to be matched is at Subject (Cursor + 1). Note that since
4994 -- our view of the subject string in XMatch always has a lower bound
4995 -- of one, regardless of original bounds, that this definition exactly
4996 -- corresponds to the cursor value as referenced by functions like Pos.
4998 -- If the value is negative, then this is a saved stack pointer,
4999 -- typically a base pointer of an inner or outer region. Cursor
5000 -- temporarily holds such a value when it is popped from the stack
5001 -- by Fail. In all cases, Cursor is reset to a proper non-negative
5002 -- cursor value before the match proceeds (e.g. by propagating the
5003 -- failure and popping a "real" cursor value from the stack.
5005 PE_Unanchored : aliased PE := (PC_Unanchored, 0, Pat_P);
5006 -- Dummy pattern element used in the unanchored case
5008 Region_Level : Natural := 0;
5009 -- Keeps track of recursive region level. This is used only for
5010 -- debugging, it is the number of saved history stack base values.
5012 Stack : Stack_Type;
5013 -- The pattern matching failure stack for this call to Match
5015 Stack_Ptr : Stack_Range;
5016 -- Current stack pointer. This points to the top element of the stack
5017 -- that is currently in use. At the outer level this is the special
5018 -- entry placed on the stack according to the anchor mode.
5020 Stack_Init : constant Stack_Range := Stack'First + 1;
5021 -- This is the initial value of the Stack_Ptr and Stack_Base. The
5022 -- initial (Stack'First) element of the stack is not used so that
5023 -- when we pop the last element off, Stack_Ptr is still in range.
5025 Stack_Base : Stack_Range;
5026 -- This value is the stack base value, i.e. the stack pointer for the
5027 -- first history stack entry in the current stack region. See separate
5028 -- section on handling of recursive pattern matches.
5030 Assign_OnM : Boolean := False;
5031 -- Set True if assign-on-match or write-on-match operations may be
5032 -- present in the history stack, which must then be scanned on a
5033 -- successful match.
5035 procedure Dout (Str : String);
5036 -- Output string to standard error with bars indicating region level
5038 procedure Dout (Str : String; A : Character);
5039 -- Calls Dout with the string S ('A')
5041 procedure Dout (Str : String; A : Character_Set);
5042 -- Calls Dout with the string S ("A")
5044 procedure Dout (Str : String; A : Natural);
5045 -- Calls Dout with the string S (A)
5047 procedure Dout (Str : String; A : String);
5048 -- Calls Dout with the string S ("A")
5050 function Img (P : PE_Ptr) return String;
5051 -- Returns a string of the form #nnn where nnn is P.Index
5053 procedure Pop_Region;
5054 pragma Inline (Pop_Region);
5055 -- Used at the end of processing of an inner region. If the inner
5056 -- region left no stack entries, then all trace of it is removed.
5057 -- Otherwise a PC_Restore_Region entry is pushed to ensure proper
5058 -- handling of alternatives in the inner region.
5060 procedure Push (Node : PE_Ptr);
5061 pragma Inline (Push);
5062 -- Make entry in pattern matching stack with current cursor value
5064 procedure Push_Region;
5065 pragma Inline (Push_Region);
5066 -- This procedure makes a new region on the history stack. The
5067 -- caller first establishes the special entry on the stack, but
5068 -- does not push the stack pointer. Then this call stacks a
5069 -- PC_Remove_Region node, on top of this entry, using the cursor
5070 -- field of the PC_Remove_Region entry to save the outer level
5071 -- stack base value, and resets the stack base to point to this
5072 -- PC_Remove_Region node.
5074 ----------
5075 -- Dout --
5076 ----------
5078 procedure Dout (Str : String) is
5079 begin
5080 for J in 1 .. Region_Level loop
5081 Put ("| ");
5082 end loop;
5084 Put_Line (Str);
5085 end Dout;
5087 procedure Dout (Str : String; A : Character) is
5088 begin
5089 Dout (Str & " ('" & A & "')");
5090 end Dout;
5092 procedure Dout (Str : String; A : Character_Set) is
5093 begin
5094 Dout (Str & " (" & Image (To_Sequence (A)) & ')');
5095 end Dout;
5097 procedure Dout (Str : String; A : Natural) is
5098 begin
5099 Dout (Str & " (" & A & ')');
5100 end Dout;
5102 procedure Dout (Str : String; A : String) is
5103 begin
5104 Dout (Str & " (" & Image (A) & ')');
5105 end Dout;
5107 ---------
5108 -- Img --
5109 ---------
5111 function Img (P : PE_Ptr) return String is
5112 begin
5113 return "#" & Integer (P.Index) & " ";
5114 end Img;
5116 ----------------
5117 -- Pop_Region --
5118 ----------------
5120 procedure Pop_Region is
5121 begin
5122 Region_Level := Region_Level - 1;
5124 -- If nothing was pushed in the inner region, we can just get
5125 -- rid of it entirely, leaving no traces that it was ever there
5127 if Stack_Ptr = Stack_Base then
5128 Stack_Ptr := Stack_Base - 2;
5129 Stack_Base := Stack (Stack_Ptr + 2).Cursor;
5131 -- If stuff was pushed in the inner region, then we have to
5132 -- push a PC_R_Restore node so that we properly handle possible
5133 -- rematches within the region.
5135 else
5136 Stack_Ptr := Stack_Ptr + 1;
5137 Stack (Stack_Ptr).Cursor := Stack_Base;
5138 Stack (Stack_Ptr).Node := CP_R_Restore'Access;
5139 Stack_Base := Stack (Stack_Base).Cursor;
5140 end if;
5141 end Pop_Region;
5143 ----------
5144 -- Push --
5145 ----------
5147 procedure Push (Node : PE_Ptr) is
5148 begin
5149 Stack_Ptr := Stack_Ptr + 1;
5150 Stack (Stack_Ptr).Cursor := Cursor;
5151 Stack (Stack_Ptr).Node := Node;
5152 end Push;
5154 -----------------
5155 -- Push_Region --
5156 -----------------
5158 procedure Push_Region is
5159 begin
5160 Region_Level := Region_Level + 1;
5161 Stack_Ptr := Stack_Ptr + 2;
5162 Stack (Stack_Ptr).Cursor := Stack_Base;
5163 Stack (Stack_Ptr).Node := CP_R_Remove'Access;
5164 Stack_Base := Stack_Ptr;
5165 end Push_Region;
5167 -- Start of processing for XMatchD
5169 begin
5170 New_Line;
5171 Put_Line ("Initiating pattern match, subject = " & Image (Subject));
5172 Put ("--------------------------------------");
5174 for J in 1 .. Length loop
5175 Put ('-');
5176 end loop;
5178 New_Line;
5179 Put_Line ("subject length = " & Length);
5181 if Pat_P = null then
5182 Uninitialized_Pattern;
5183 end if;
5185 -- Check we have enough stack for this pattern. This check deals with
5186 -- every possibility except a match of a recursive pattern, where we
5187 -- make a check at each recursion level.
5189 if Pat_S >= Stack_Size - 1 then
5190 raise Pattern_Stack_Overflow;
5191 end if;
5193 -- In anchored mode, the bottom entry on the stack is an abort entry
5195 if Anchored_Mode then
5196 Stack (Stack_Init).Node := CP_Cancel'Access;
5197 Stack (Stack_Init).Cursor := 0;
5199 -- In unanchored more, the bottom entry on the stack references
5200 -- the special pattern element PE_Unanchored, whose Pthen field
5201 -- points to the initial pattern element. The cursor value in this
5202 -- entry is the number of anchor moves so far.
5204 else
5205 Stack (Stack_Init).Node := PE_Unanchored'Unchecked_Access;
5206 Stack (Stack_Init).Cursor := 0;
5207 end if;
5209 Stack_Ptr := Stack_Init;
5210 Stack_Base := Stack_Ptr;
5211 Cursor := 0;
5212 Node := Pat_P;
5213 goto Match;
5215 -----------------------------------------
5216 -- Main Pattern Matching State Control --
5217 -----------------------------------------
5219 -- This is a state machine which uses gotos to change state. The
5220 -- initial state is Match, to initiate the matching of the first
5221 -- element, so the goto Match above starts the match. In the
5222 -- following descriptions, we indicate the global values that
5223 -- are relevant for the state transition.
5225 -- Come here if entire match fails
5227 <<Match_Fail>>
5228 Dout ("match fails");
5229 New_Line;
5230 Start := 0;
5231 Stop := 0;
5232 return;
5234 -- Come here if entire match succeeds
5236 -- Cursor current position in subject string
5238 <<Match_Succeed>>
5239 Dout ("match succeeds");
5240 Start := Stack (Stack_Init).Cursor + 1;
5241 Stop := Cursor;
5242 Dout ("first matched character index = " & Start);
5243 Dout ("last matched character index = " & Stop);
5244 Dout ("matched substring = " & Image (Subject (Start .. Stop)));
5246 -- Scan history stack for deferred assignments or writes
5248 if Assign_OnM then
5249 for S in Stack'First .. Stack_Ptr loop
5250 if Stack (S).Node = CP_Assign'Access then
5251 declare
5252 Inner_Base : constant Stack_Range :=
5253 Stack (S + 1).Cursor;
5254 Special_Entry : constant Stack_Range :=
5255 Inner_Base - 1;
5256 Node_OnM : constant PE_Ptr :=
5257 Stack (Special_Entry).Node;
5258 Start : constant Natural :=
5259 Stack (Special_Entry).Cursor + 1;
5260 Stop : constant Natural := Stack (S).Cursor;
5262 begin
5263 if Node_OnM.Pcode = PC_Assign_OnM then
5264 Set_Unbounded_String
5265 (Node_OnM.VP.all, Subject (Start .. Stop));
5266 Dout
5267 (Img (Stack (S).Node) &
5268 "deferred assignment of " &
5269 Image (Subject (Start .. Stop)));
5271 elsif Node_OnM.Pcode = PC_Write_OnM then
5272 Put_Line (Node_OnM.FP.all, Subject (Start .. Stop));
5273 Dout
5274 (Img (Stack (S).Node) &
5275 "deferred write of " &
5276 Image (Subject (Start .. Stop)));
5278 else
5279 Logic_Error;
5280 end if;
5281 end;
5282 end if;
5283 end loop;
5284 end if;
5286 New_Line;
5287 return;
5289 -- Come here if attempt to match current element fails
5291 -- Stack_Base current stack base
5292 -- Stack_Ptr current stack pointer
5294 <<Fail>>
5295 Cursor := Stack (Stack_Ptr).Cursor;
5296 Node := Stack (Stack_Ptr).Node;
5297 Stack_Ptr := Stack_Ptr - 1;
5299 if Cursor >= 0 then
5300 Dout ("failure, cursor reset to " & Cursor);
5301 end if;
5303 goto Match;
5305 -- Come here if attempt to match current element succeeds
5307 -- Cursor current position in subject string
5308 -- Node pointer to node successfully matched
5309 -- Stack_Base current stack base
5310 -- Stack_Ptr current stack pointer
5312 <<Succeed>>
5313 Dout ("success, cursor = " & Cursor);
5314 Node := Node.Pthen;
5316 -- Come here to match the next pattern element
5318 -- Cursor current position in subject string
5319 -- Node pointer to node to be matched
5320 -- Stack_Base current stack base
5321 -- Stack_Ptr current stack pointer
5323 <<Match>>
5325 --------------------------------------------------
5326 -- Main Pattern Match Element Matching Routines --
5327 --------------------------------------------------
5329 -- Here is the case statement that processes the current node. The
5330 -- processing for each element does one of five things:
5332 -- goto Succeed to move to the successor
5333 -- goto Match_Succeed if the entire match succeeds
5334 -- goto Match_Fail if the entire match fails
5335 -- goto Fail to signal failure of current match
5337 -- Processing is NOT allowed to fall through
5339 case Node.Pcode is
5341 -- Cancel
5343 when PC_Cancel =>
5344 Dout (Img (Node) & "matching Cancel");
5345 goto Match_Fail;
5347 -- Alternation
5349 when PC_Alt =>
5350 Dout (Img (Node) & "setting up alternative " & Img (Node.Alt));
5351 Push (Node.Alt);
5352 Node := Node.Pthen;
5353 goto Match;
5355 -- Any (one character case)
5357 when PC_Any_CH =>
5358 Dout (Img (Node) & "matching Any", Node.Char);
5360 if Cursor < Length
5361 and then Subject (Cursor + 1) = Node.Char
5362 then
5363 Cursor := Cursor + 1;
5364 goto Succeed;
5365 else
5366 goto Fail;
5367 end if;
5369 -- Any (character set case)
5371 when PC_Any_CS =>
5372 Dout (Img (Node) & "matching Any", Node.CS);
5374 if Cursor < Length
5375 and then Is_In (Subject (Cursor + 1), Node.CS)
5376 then
5377 Cursor := Cursor + 1;
5378 goto Succeed;
5379 else
5380 goto Fail;
5381 end if;
5383 -- Any (string function case)
5385 when PC_Any_VF => declare
5386 U : constant VString := Node.VF.all;
5387 S : Big_String_Access;
5388 L : Natural;
5390 begin
5391 Get_String (U, S, L);
5393 Dout (Img (Node) & "matching Any", S (1 .. L));
5395 if Cursor < Length
5396 and then Is_In (Subject (Cursor + 1), S (1 .. L))
5397 then
5398 Cursor := Cursor + 1;
5399 goto Succeed;
5400 else
5401 goto Fail;
5402 end if;
5403 end;
5405 -- Any (string pointer case)
5407 when PC_Any_VP => declare
5408 U : constant VString := Node.VP.all;
5409 S : Big_String_Access;
5410 L : Natural;
5412 begin
5413 Get_String (U, S, L);
5414 Dout (Img (Node) & "matching Any", S (1 .. L));
5416 if Cursor < Length
5417 and then Is_In (Subject (Cursor + 1), S (1 .. L))
5418 then
5419 Cursor := Cursor + 1;
5420 goto Succeed;
5421 else
5422 goto Fail;
5423 end if;
5424 end;
5426 -- Arb (initial match)
5428 when PC_Arb_X =>
5429 Dout (Img (Node) & "matching Arb");
5430 Push (Node.Alt);
5431 Node := Node.Pthen;
5432 goto Match;
5434 -- Arb (extension)
5436 when PC_Arb_Y =>
5437 Dout (Img (Node) & "extending Arb");
5439 if Cursor < Length then
5440 Cursor := Cursor + 1;
5441 Push (Node);
5442 goto Succeed;
5443 else
5444 goto Fail;
5445 end if;
5447 -- Arbno_S (simple Arbno initialize). This is the node that
5448 -- initiates the match of a simple Arbno structure.
5450 when PC_Arbno_S =>
5451 Dout (Img (Node) &
5452 "setting up Arbno alternative " & Img (Node.Alt));
5453 Push (Node.Alt);
5454 Node := Node.Pthen;
5455 goto Match;
5457 -- Arbno_X (Arbno initialize). This is the node that initiates
5458 -- the match of a complex Arbno structure.
5460 when PC_Arbno_X =>
5461 Dout (Img (Node) &
5462 "setting up Arbno alternative " & Img (Node.Alt));
5463 Push (Node.Alt);
5464 Node := Node.Pthen;
5465 goto Match;
5467 -- Arbno_Y (Arbno rematch). This is the node that is executed
5468 -- following successful matching of one instance of a complex
5469 -- Arbno pattern.
5471 when PC_Arbno_Y => declare
5472 Null_Match : constant Boolean :=
5473 Cursor = Stack (Stack_Base - 1).Cursor;
5475 begin
5476 Dout (Img (Node) & "extending Arbno");
5477 Pop_Region;
5479 -- If arbno extension matched null, then immediately fail
5481 if Null_Match then
5482 Dout ("Arbno extension matched null, so fails");
5483 goto Fail;
5484 end if;
5486 -- Here we must do a stack check to make sure enough stack
5487 -- is left. This check will happen once for each instance of
5488 -- the Arbno pattern that is matched. The Nat field of a
5489 -- PC_Arbno pattern contains the maximum stack entries needed
5490 -- for the Arbno with one instance and the successor pattern
5492 if Stack_Ptr + Node.Nat >= Stack'Last then
5493 raise Pattern_Stack_Overflow;
5494 end if;
5496 goto Succeed;
5497 end;
5499 -- Assign. If this node is executed, it means the assign-on-match
5500 -- or write-on-match operation will not happen after all, so we
5501 -- is propagate the failure, removing the PC_Assign node.
5503 when PC_Assign =>
5504 Dout (Img (Node) & "deferred assign/write cancelled");
5505 goto Fail;
5507 -- Assign immediate. This node performs the actual assignment
5509 when PC_Assign_Imm =>
5510 Dout
5511 (Img (Node) & "executing immediate assignment of " &
5512 Image (Subject (Stack (Stack_Base - 1).Cursor + 1 .. Cursor)));
5513 Set_Unbounded_String
5514 (Node.VP.all,
5515 Subject (Stack (Stack_Base - 1).Cursor + 1 .. Cursor));
5516 Pop_Region;
5517 goto Succeed;
5519 -- Assign on match. This node sets up for the eventual assignment
5521 when PC_Assign_OnM =>
5522 Dout (Img (Node) & "registering deferred assignment");
5523 Stack (Stack_Base - 1).Node := Node;
5524 Push (CP_Assign'Access);
5525 Pop_Region;
5526 Assign_OnM := True;
5527 goto Succeed;
5529 -- Bal
5531 when PC_Bal =>
5532 Dout (Img (Node) & "matching or extending Bal");
5533 if Cursor >= Length or else Subject (Cursor + 1) = ')' then
5534 goto Fail;
5536 elsif Subject (Cursor + 1) = '(' then
5537 declare
5538 Paren_Count : Natural := 1;
5540 begin
5541 loop
5542 Cursor := Cursor + 1;
5544 if Cursor >= Length then
5545 goto Fail;
5547 elsif Subject (Cursor + 1) = '(' then
5548 Paren_Count := Paren_Count + 1;
5550 elsif Subject (Cursor + 1) = ')' then
5551 Paren_Count := Paren_Count - 1;
5552 exit when Paren_Count = 0;
5553 end if;
5554 end loop;
5555 end;
5556 end if;
5558 Cursor := Cursor + 1;
5559 Push (Node);
5560 goto Succeed;
5562 -- Break (one character case)
5564 when PC_Break_CH =>
5565 Dout (Img (Node) & "matching Break", Node.Char);
5567 while Cursor < Length loop
5568 if Subject (Cursor + 1) = Node.Char then
5569 goto Succeed;
5570 else
5571 Cursor := Cursor + 1;
5572 end if;
5573 end loop;
5575 goto Fail;
5577 -- Break (character set case)
5579 when PC_Break_CS =>
5580 Dout (Img (Node) & "matching Break", Node.CS);
5582 while Cursor < Length loop
5583 if Is_In (Subject (Cursor + 1), Node.CS) then
5584 goto Succeed;
5585 else
5586 Cursor := Cursor + 1;
5587 end if;
5588 end loop;
5590 goto Fail;
5592 -- Break (string function case)
5594 when PC_Break_VF => declare
5595 U : constant VString := Node.VF.all;
5596 S : Big_String_Access;
5597 L : Natural;
5599 begin
5600 Get_String (U, S, L);
5601 Dout (Img (Node) & "matching Break", S (1 .. L));
5603 while Cursor < Length loop
5604 if Is_In (Subject (Cursor + 1), S (1 .. L)) then
5605 goto Succeed;
5606 else
5607 Cursor := Cursor + 1;
5608 end if;
5609 end loop;
5611 goto Fail;
5612 end;
5614 -- Break (string pointer case)
5616 when PC_Break_VP => declare
5617 U : constant VString := Node.VP.all;
5618 S : Big_String_Access;
5619 L : Natural;
5621 begin
5622 Get_String (U, S, L);
5623 Dout (Img (Node) & "matching Break", S (1 .. L));
5625 while Cursor < Length loop
5626 if Is_In (Subject (Cursor + 1), S (1 .. L)) then
5627 goto Succeed;
5628 else
5629 Cursor := Cursor + 1;
5630 end if;
5631 end loop;
5633 goto Fail;
5634 end;
5636 -- BreakX (one character case)
5638 when PC_BreakX_CH =>
5639 Dout (Img (Node) & "matching BreakX", Node.Char);
5641 while Cursor < Length loop
5642 if Subject (Cursor + 1) = Node.Char then
5643 goto Succeed;
5644 else
5645 Cursor := Cursor + 1;
5646 end if;
5647 end loop;
5649 goto Fail;
5651 -- BreakX (character set case)
5653 when PC_BreakX_CS =>
5654 Dout (Img (Node) & "matching BreakX", Node.CS);
5656 while Cursor < Length loop
5657 if Is_In (Subject (Cursor + 1), Node.CS) then
5658 goto Succeed;
5659 else
5660 Cursor := Cursor + 1;
5661 end if;
5662 end loop;
5664 goto Fail;
5666 -- BreakX (string function case)
5668 when PC_BreakX_VF => declare
5669 U : constant VString := Node.VF.all;
5670 S : Big_String_Access;
5671 L : Natural;
5673 begin
5674 Get_String (U, S, L);
5675 Dout (Img (Node) & "matching BreakX", S (1 .. L));
5677 while Cursor < Length loop
5678 if Is_In (Subject (Cursor + 1), S (1 .. L)) then
5679 goto Succeed;
5680 else
5681 Cursor := Cursor + 1;
5682 end if;
5683 end loop;
5685 goto Fail;
5686 end;
5688 -- BreakX (string pointer case)
5690 when PC_BreakX_VP => declare
5691 U : constant VString := Node.VP.all;
5692 S : Big_String_Access;
5693 L : Natural;
5695 begin
5696 Get_String (U, S, L);
5697 Dout (Img (Node) & "matching BreakX", S (1 .. L));
5699 while Cursor < Length loop
5700 if Is_In (Subject (Cursor + 1), S (1 .. L)) then
5701 goto Succeed;
5702 else
5703 Cursor := Cursor + 1;
5704 end if;
5705 end loop;
5707 goto Fail;
5708 end;
5710 -- BreakX_X (BreakX extension). See section on "Compound Pattern
5711 -- Structures". This node is the alternative that is stacked
5712 -- to skip past the break character and extend the break.
5714 when PC_BreakX_X =>
5715 Dout (Img (Node) & "extending BreakX");
5716 Cursor := Cursor + 1;
5717 goto Succeed;
5719 -- Character (one character string)
5721 when PC_Char =>
5722 Dout (Img (Node) & "matching '" & Node.Char & ''');
5724 if Cursor < Length
5725 and then Subject (Cursor + 1) = Node.Char
5726 then
5727 Cursor := Cursor + 1;
5728 goto Succeed;
5729 else
5730 goto Fail;
5731 end if;
5733 -- End of Pattern
5735 when PC_EOP =>
5736 if Stack_Base = Stack_Init then
5737 Dout ("end of pattern");
5738 goto Match_Succeed;
5740 -- End of recursive inner match. See separate section on
5741 -- handing of recursive pattern matches for details.
5743 else
5744 Dout ("terminating recursive match");
5745 Node := Stack (Stack_Base - 1).Node;
5746 Pop_Region;
5747 goto Match;
5748 end if;
5750 -- Fail
5752 when PC_Fail =>
5753 Dout (Img (Node) & "matching Fail");
5754 goto Fail;
5756 -- Fence (built in pattern)
5758 when PC_Fence =>
5759 Dout (Img (Node) & "matching Fence");
5760 Push (CP_Cancel'Access);
5761 goto Succeed;
5763 -- Fence function node X. This is the node that gets control
5764 -- after a successful match of the fenced pattern.
5766 when PC_Fence_X =>
5767 Dout (Img (Node) & "matching Fence function");
5768 Stack_Ptr := Stack_Ptr + 1;
5769 Stack (Stack_Ptr).Cursor := Stack_Base;
5770 Stack (Stack_Ptr).Node := CP_Fence_Y'Access;
5771 Stack_Base := Stack (Stack_Base).Cursor;
5772 Region_Level := Region_Level - 1;
5773 goto Succeed;
5775 -- Fence function node Y. This is the node that gets control on
5776 -- a failure that occurs after the fenced pattern has matched.
5778 -- Note: the Cursor at this stage is actually the inner stack
5779 -- base value. We don't reset this, but we do use it to strip
5780 -- off all the entries made by the fenced pattern.
5782 when PC_Fence_Y =>
5783 Dout (Img (Node) & "pattern matched by Fence caused failure");
5784 Stack_Ptr := Cursor - 2;
5785 goto Fail;
5787 -- Len (integer case)
5789 when PC_Len_Nat =>
5790 Dout (Img (Node) & "matching Len", Node.Nat);
5792 if Cursor + Node.Nat > Length then
5793 goto Fail;
5794 else
5795 Cursor := Cursor + Node.Nat;
5796 goto Succeed;
5797 end if;
5799 -- Len (Integer function case)
5801 when PC_Len_NF => declare
5802 N : constant Natural := Node.NF.all;
5804 begin
5805 Dout (Img (Node) & "matching Len", N);
5807 if Cursor + N > Length then
5808 goto Fail;
5809 else
5810 Cursor := Cursor + N;
5811 goto Succeed;
5812 end if;
5813 end;
5815 -- Len (integer pointer case)
5817 when PC_Len_NP =>
5818 Dout (Img (Node) & "matching Len", Node.NP.all);
5820 if Cursor + Node.NP.all > Length then
5821 goto Fail;
5822 else
5823 Cursor := Cursor + Node.NP.all;
5824 goto Succeed;
5825 end if;
5827 -- NotAny (one character case)
5829 when PC_NotAny_CH =>
5830 Dout (Img (Node) & "matching NotAny", Node.Char);
5832 if Cursor < Length
5833 and then Subject (Cursor + 1) /= Node.Char
5834 then
5835 Cursor := Cursor + 1;
5836 goto Succeed;
5837 else
5838 goto Fail;
5839 end if;
5841 -- NotAny (character set case)
5843 when PC_NotAny_CS =>
5844 Dout (Img (Node) & "matching NotAny", Node.CS);
5846 if Cursor < Length
5847 and then not Is_In (Subject (Cursor + 1), Node.CS)
5848 then
5849 Cursor := Cursor + 1;
5850 goto Succeed;
5851 else
5852 goto Fail;
5853 end if;
5855 -- NotAny (string function case)
5857 when PC_NotAny_VF => declare
5858 U : constant VString := Node.VF.all;
5859 S : Big_String_Access;
5860 L : Natural;
5862 begin
5863 Get_String (U, S, L);
5864 Dout (Img (Node) & "matching NotAny", S (1 .. L));
5866 if Cursor < Length
5867 and then
5868 not Is_In (Subject (Cursor + 1), S (1 .. L))
5869 then
5870 Cursor := Cursor + 1;
5871 goto Succeed;
5872 else
5873 goto Fail;
5874 end if;
5875 end;
5877 -- NotAny (string pointer case)
5879 when PC_NotAny_VP => declare
5880 U : constant VString := Node.VP.all;
5881 S : Big_String_Access;
5882 L : Natural;
5884 begin
5885 Get_String (U, S, L);
5886 Dout (Img (Node) & "matching NotAny", S (1 .. L));
5888 if Cursor < Length
5889 and then
5890 not Is_In (Subject (Cursor + 1), S (1 .. L))
5891 then
5892 Cursor := Cursor + 1;
5893 goto Succeed;
5894 else
5895 goto Fail;
5896 end if;
5897 end;
5899 -- NSpan (one character case)
5901 when PC_NSpan_CH =>
5902 Dout (Img (Node) & "matching NSpan", Node.Char);
5904 while Cursor < Length
5905 and then Subject (Cursor + 1) = Node.Char
5906 loop
5907 Cursor := Cursor + 1;
5908 end loop;
5910 goto Succeed;
5912 -- NSpan (character set case)
5914 when PC_NSpan_CS =>
5915 Dout (Img (Node) & "matching NSpan", Node.CS);
5917 while Cursor < Length
5918 and then Is_In (Subject (Cursor + 1), Node.CS)
5919 loop
5920 Cursor := Cursor + 1;
5921 end loop;
5923 goto Succeed;
5925 -- NSpan (string function case)
5927 when PC_NSpan_VF => declare
5928 U : constant VString := Node.VF.all;
5929 S : Big_String_Access;
5930 L : Natural;
5932 begin
5933 Get_String (U, S, L);
5934 Dout (Img (Node) & "matching NSpan", S (1 .. L));
5936 while Cursor < Length
5937 and then Is_In (Subject (Cursor + 1), S (1 .. L))
5938 loop
5939 Cursor := Cursor + 1;
5940 end loop;
5942 goto Succeed;
5943 end;
5945 -- NSpan (string pointer case)
5947 when PC_NSpan_VP => declare
5948 U : constant VString := Node.VP.all;
5949 S : Big_String_Access;
5950 L : Natural;
5952 begin
5953 Get_String (U, S, L);
5954 Dout (Img (Node) & "matching NSpan", S (1 .. L));
5956 while Cursor < Length
5957 and then Is_In (Subject (Cursor + 1), S (1 .. L))
5958 loop
5959 Cursor := Cursor + 1;
5960 end loop;
5962 goto Succeed;
5963 end;
5965 when PC_Null =>
5966 Dout (Img (Node) & "matching null");
5967 goto Succeed;
5969 -- Pos (integer case)
5971 when PC_Pos_Nat =>
5972 Dout (Img (Node) & "matching Pos", Node.Nat);
5974 if Cursor = Node.Nat then
5975 goto Succeed;
5976 else
5977 goto Fail;
5978 end if;
5980 -- Pos (Integer function case)
5982 when PC_Pos_NF => declare
5983 N : constant Natural := Node.NF.all;
5985 begin
5986 Dout (Img (Node) & "matching Pos", N);
5988 if Cursor = N then
5989 goto Succeed;
5990 else
5991 goto Fail;
5992 end if;
5993 end;
5995 -- Pos (integer pointer case)
5997 when PC_Pos_NP =>
5998 Dout (Img (Node) & "matching Pos", Node.NP.all);
6000 if Cursor = Node.NP.all then
6001 goto Succeed;
6002 else
6003 goto Fail;
6004 end if;
6006 -- Predicate function
6008 when PC_Pred_Func =>
6009 Dout (Img (Node) & "matching predicate function");
6011 if Node.BF.all then
6012 goto Succeed;
6013 else
6014 goto Fail;
6015 end if;
6017 -- Region Enter. Initiate new pattern history stack region
6019 when PC_R_Enter =>
6020 Dout (Img (Node) & "starting match of nested pattern");
6021 Stack (Stack_Ptr + 1).Cursor := Cursor;
6022 Push_Region;
6023 goto Succeed;
6025 -- Region Remove node. This is the node stacked by an R_Enter.
6026 -- It removes the special format stack entry right underneath, and
6027 -- then restores the outer level stack base and signals failure.
6029 -- Note: the cursor value at this stage is actually the (negative)
6030 -- stack base value for the outer level.
6032 when PC_R_Remove =>
6033 Dout ("failure, match of nested pattern terminated");
6034 Stack_Base := Cursor;
6035 Region_Level := Region_Level - 1;
6036 Stack_Ptr := Stack_Ptr - 1;
6037 goto Fail;
6039 -- Region restore node. This is the node stacked at the end of an
6040 -- inner level match. Its function is to restore the inner level
6041 -- region, so that alternatives in this region can be sought.
6043 -- Note: the Cursor at this stage is actually the negative of the
6044 -- inner stack base value, which we use to restore the inner region.
6046 when PC_R_Restore =>
6047 Dout ("failure, search for alternatives in nested pattern");
6048 Region_Level := Region_Level + 1;
6049 Stack_Base := Cursor;
6050 goto Fail;
6052 -- Rest
6054 when PC_Rest =>
6055 Dout (Img (Node) & "matching Rest");
6056 Cursor := Length;
6057 goto Succeed;
6059 -- Initiate recursive match (pattern pointer case)
6061 when PC_Rpat =>
6062 Stack (Stack_Ptr + 1).Node := Node.Pthen;
6063 Push_Region;
6064 Dout (Img (Node) & "initiating recursive match");
6066 if Stack_Ptr + Node.PP.all.Stk >= Stack_Size then
6067 raise Pattern_Stack_Overflow;
6068 else
6069 Node := Node.PP.all.P;
6070 goto Match;
6071 end if;
6073 -- RPos (integer case)
6075 when PC_RPos_Nat =>
6076 Dout (Img (Node) & "matching RPos", Node.Nat);
6078 if Cursor = (Length - Node.Nat) then
6079 goto Succeed;
6080 else
6081 goto Fail;
6082 end if;
6084 -- RPos (integer function case)
6086 when PC_RPos_NF => declare
6087 N : constant Natural := Node.NF.all;
6089 begin
6090 Dout (Img (Node) & "matching RPos", N);
6092 if Length - Cursor = N then
6093 goto Succeed;
6094 else
6095 goto Fail;
6096 end if;
6097 end;
6099 -- RPos (integer pointer case)
6101 when PC_RPos_NP =>
6102 Dout (Img (Node) & "matching RPos", Node.NP.all);
6104 if Cursor = (Length - Node.NP.all) then
6105 goto Succeed;
6106 else
6107 goto Fail;
6108 end if;
6110 -- RTab (integer case)
6112 when PC_RTab_Nat =>
6113 Dout (Img (Node) & "matching RTab", Node.Nat);
6115 if Cursor <= (Length - Node.Nat) then
6116 Cursor := Length - Node.Nat;
6117 goto Succeed;
6118 else
6119 goto Fail;
6120 end if;
6122 -- RTab (integer function case)
6124 when PC_RTab_NF => declare
6125 N : constant Natural := Node.NF.all;
6127 begin
6128 Dout (Img (Node) & "matching RPos", N);
6130 if Length - Cursor >= N then
6131 Cursor := Length - N;
6132 goto Succeed;
6133 else
6134 goto Fail;
6135 end if;
6136 end;
6138 -- RTab (integer pointer case)
6140 when PC_RTab_NP =>
6141 Dout (Img (Node) & "matching RPos", Node.NP.all);
6143 if Cursor <= (Length - Node.NP.all) then
6144 Cursor := Length - Node.NP.all;
6145 goto Succeed;
6146 else
6147 goto Fail;
6148 end if;
6150 -- Cursor assignment
6152 when PC_Setcur =>
6153 Dout (Img (Node) & "matching Setcur");
6154 Node.Var.all := Cursor;
6155 goto Succeed;
6157 -- Span (one character case)
6159 when PC_Span_CH => declare
6160 P : Natural := Cursor;
6162 begin
6163 Dout (Img (Node) & "matching Span", Node.Char);
6165 while P < Length
6166 and then Subject (P + 1) = Node.Char
6167 loop
6168 P := P + 1;
6169 end loop;
6171 if P /= Cursor then
6172 Cursor := P;
6173 goto Succeed;
6174 else
6175 goto Fail;
6176 end if;
6177 end;
6179 -- Span (character set case)
6181 when PC_Span_CS => declare
6182 P : Natural := Cursor;
6184 begin
6185 Dout (Img (Node) & "matching Span", Node.CS);
6187 while P < Length
6188 and then Is_In (Subject (P + 1), Node.CS)
6189 loop
6190 P := P + 1;
6191 end loop;
6193 if P /= Cursor then
6194 Cursor := P;
6195 goto Succeed;
6196 else
6197 goto Fail;
6198 end if;
6199 end;
6201 -- Span (string function case)
6203 when PC_Span_VF => declare
6204 U : constant VString := Node.VF.all;
6205 S : Big_String_Access;
6206 L : Natural;
6207 P : Natural;
6209 begin
6210 Get_String (U, S, L);
6211 Dout (Img (Node) & "matching Span", S (1 .. L));
6213 P := Cursor;
6214 while P < Length
6215 and then Is_In (Subject (P + 1), S (1 .. L))
6216 loop
6217 P := P + 1;
6218 end loop;
6220 if P /= Cursor then
6221 Cursor := P;
6222 goto Succeed;
6223 else
6224 goto Fail;
6225 end if;
6226 end;
6228 -- Span (string pointer case)
6230 when PC_Span_VP => declare
6231 U : constant VString := Node.VP.all;
6232 S : Big_String_Access;
6233 L : Natural;
6234 P : Natural;
6236 begin
6237 Get_String (U, S, L);
6238 Dout (Img (Node) & "matching Span", S (1 .. L));
6240 P := Cursor;
6241 while P < Length
6242 and then Is_In (Subject (P + 1), S (1 .. L))
6243 loop
6244 P := P + 1;
6245 end loop;
6247 if P /= Cursor then
6248 Cursor := P;
6249 goto Succeed;
6250 else
6251 goto Fail;
6252 end if;
6253 end;
6255 -- String (two character case)
6257 when PC_String_2 =>
6258 Dout (Img (Node) & "matching " & Image (Node.Str2));
6260 if (Length - Cursor) >= 2
6261 and then Subject (Cursor + 1 .. Cursor + 2) = Node.Str2
6262 then
6263 Cursor := Cursor + 2;
6264 goto Succeed;
6265 else
6266 goto Fail;
6267 end if;
6269 -- String (three character case)
6271 when PC_String_3 =>
6272 Dout (Img (Node) & "matching " & Image (Node.Str3));
6274 if (Length - Cursor) >= 3
6275 and then Subject (Cursor + 1 .. Cursor + 3) = Node.Str3
6276 then
6277 Cursor := Cursor + 3;
6278 goto Succeed;
6279 else
6280 goto Fail;
6281 end if;
6283 -- String (four character case)
6285 when PC_String_4 =>
6286 Dout (Img (Node) & "matching " & Image (Node.Str4));
6288 if (Length - Cursor) >= 4
6289 and then Subject (Cursor + 1 .. Cursor + 4) = Node.Str4
6290 then
6291 Cursor := Cursor + 4;
6292 goto Succeed;
6293 else
6294 goto Fail;
6295 end if;
6297 -- String (five character case)
6299 when PC_String_5 =>
6300 Dout (Img (Node) & "matching " & Image (Node.Str5));
6302 if (Length - Cursor) >= 5
6303 and then Subject (Cursor + 1 .. Cursor + 5) = Node.Str5
6304 then
6305 Cursor := Cursor + 5;
6306 goto Succeed;
6307 else
6308 goto Fail;
6309 end if;
6311 -- String (six character case)
6313 when PC_String_6 =>
6314 Dout (Img (Node) & "matching " & Image (Node.Str6));
6316 if (Length - Cursor) >= 6
6317 and then Subject (Cursor + 1 .. Cursor + 6) = Node.Str6
6318 then
6319 Cursor := Cursor + 6;
6320 goto Succeed;
6321 else
6322 goto Fail;
6323 end if;
6325 -- String (case of more than six characters)
6327 when PC_String => declare
6328 Len : constant Natural := Node.Str'Length;
6330 begin
6331 Dout (Img (Node) & "matching " & Image (Node.Str.all));
6333 if (Length - Cursor) >= Len
6334 and then Node.Str.all = Subject (Cursor + 1 .. Cursor + Len)
6335 then
6336 Cursor := Cursor + Len;
6337 goto Succeed;
6338 else
6339 goto Fail;
6340 end if;
6341 end;
6343 -- String (function case)
6345 when PC_String_VF => declare
6346 U : constant VString := Node.VF.all;
6347 S : Big_String_Access;
6348 L : Natural;
6350 begin
6351 Get_String (U, S, L);
6352 Dout (Img (Node) & "matching " & Image (S (1 .. L)));
6354 if (Length - Cursor) >= L
6355 and then S (1 .. L) = Subject (Cursor + 1 .. Cursor + L)
6356 then
6357 Cursor := Cursor + L;
6358 goto Succeed;
6359 else
6360 goto Fail;
6361 end if;
6362 end;
6364 -- String (vstring pointer case)
6366 when PC_String_VP => declare
6367 U : constant VString := Node.VP.all;
6368 S : Big_String_Access;
6369 L : Natural;
6371 begin
6372 Get_String (U, S, L);
6373 Dout (Img (Node) & "matching " & Image (S (1 .. L)));
6375 if (Length - Cursor) >= L
6376 and then S (1 .. L) = Subject (Cursor + 1 .. Cursor + L)
6377 then
6378 Cursor := Cursor + L;
6379 goto Succeed;
6380 else
6381 goto Fail;
6382 end if;
6383 end;
6385 -- Succeed
6387 when PC_Succeed =>
6388 Dout (Img (Node) & "matching Succeed");
6389 Push (Node);
6390 goto Succeed;
6392 -- Tab (integer case)
6394 when PC_Tab_Nat =>
6395 Dout (Img (Node) & "matching Tab", Node.Nat);
6397 if Cursor <= Node.Nat then
6398 Cursor := Node.Nat;
6399 goto Succeed;
6400 else
6401 goto Fail;
6402 end if;
6404 -- Tab (integer function case)
6406 when PC_Tab_NF => declare
6407 N : constant Natural := Node.NF.all;
6409 begin
6410 Dout (Img (Node) & "matching Tab ", N);
6412 if Cursor <= N then
6413 Cursor := N;
6414 goto Succeed;
6415 else
6416 goto Fail;
6417 end if;
6418 end;
6420 -- Tab (integer pointer case)
6422 when PC_Tab_NP =>
6423 Dout (Img (Node) & "matching Tab ", Node.NP.all);
6425 if Cursor <= Node.NP.all then
6426 Cursor := Node.NP.all;
6427 goto Succeed;
6428 else
6429 goto Fail;
6430 end if;
6432 -- Unanchored movement
6434 when PC_Unanchored =>
6435 Dout ("attempting to move anchor point");
6437 -- All done if we tried every position
6439 if Cursor > Length then
6440 goto Match_Fail;
6442 -- Otherwise extend the anchor point, and restack ourself
6444 else
6445 Cursor := Cursor + 1;
6446 Push (Node);
6447 goto Succeed;
6448 end if;
6450 -- Write immediate. This node performs the actual write
6452 when PC_Write_Imm =>
6453 Dout (Img (Node) & "executing immediate write of " &
6454 Subject (Stack (Stack_Base - 1).Cursor + 1 .. Cursor));
6456 Put_Line
6457 (Node.FP.all,
6458 Subject (Stack (Stack_Base - 1).Cursor + 1 .. Cursor));
6459 Pop_Region;
6460 goto Succeed;
6462 -- Write on match. This node sets up for the eventual write
6464 when PC_Write_OnM =>
6465 Dout (Img (Node) & "registering deferred write");
6466 Stack (Stack_Base - 1).Node := Node;
6467 Push (CP_Assign'Access);
6468 Pop_Region;
6469 Assign_OnM := True;
6470 goto Succeed;
6471 end case;
6473 -- We are NOT allowed to fall though this case statement, since every
6474 -- match routine must end by executing a goto to the appropriate point
6475 -- in the finite state machine model.
6477 pragma Warnings (Off);
6478 Logic_Error;
6479 pragma Warnings (On);
6480 end XMatchD;
6482 end GNAT.Spitbol.Patterns;