1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- G N A T . S P I T B O L . P A T T E R N S --
9 -- Copyright (C) 1998-2002, Ada Core Technologies, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- GNAT is maintained by Ada Core Technologies Inc (http://www.gnat.com). --
31 ------------------------------------------------------------------------------
33 -- Note: the data structures and general approach used in this implementation
34 -- are derived from the original MINIMAL sources for SPITBOL. The code is not
35 -- a direct translation, but the approach is followed closely. In particular,
36 -- we use the one stack approach developed in the SPITBOL implementation.
38 with Ada
.Exceptions
; use Ada
.Exceptions
;
39 with Ada
.Strings
.Maps
; use Ada
.Strings
.Maps
;
40 with Ada
.Strings
.Unbounded
.Aux
; use Ada
.Strings
.Unbounded
.Aux
;
42 with GNAT
.Debug_Utilities
; use GNAT
.Debug_Utilities
;
44 with System
; use System
;
46 with Unchecked_Conversion
;
47 with Unchecked_Deallocation
;
49 package body GNAT
.Spitbol
.Patterns
is
51 ------------------------
52 -- Internal Debugging --
53 ------------------------
55 Internal_Debug
: constant Boolean := False;
56 -- Set this flag to True to activate some built-in debugging traceback
57 -- These are all lines output with PutD and Put_LineD.
60 pragma Inline
(New_LineD
);
61 -- Output new blank line with New_Line if Internal_Debug is True
63 procedure PutD
(Str
: String);
65 -- Output string with Put if Internal_Debug is True
67 procedure Put_LineD
(Str
: String);
68 pragma Inline
(Put_LineD
);
69 -- Output string with Put_Line if Internal_Debug is True
71 -----------------------------
72 -- Local Type Declarations --
73 -----------------------------
75 subtype String_Ptr
is Ada
.Strings
.Unbounded
.String_Access
;
76 subtype File_Ptr
is Ada
.Text_IO
.File_Access
;
78 function To_Address
is new Unchecked_Conversion
(PE_Ptr
, Address
);
79 -- Used only for debugging output purposes
81 subtype AFC
is Ada
.Finalization
.Controlled
;
83 N
: constant PE_Ptr
:= null;
84 -- Shorthand used to initialize Copy fields to null
86 type Natural_Ptr
is access all Natural;
87 type Pattern_Ptr
is access all Pattern
;
89 --------------------------------------------------
90 -- Description of Algorithm and Data Structures --
91 --------------------------------------------------
93 -- A pattern structure is represented as a linked graph of nodes
94 -- with the following structure:
96 -- +------------------------------------+
98 -- +------------------------------------+
100 -- +------------------------------------+
102 -- +------------------------------------+
104 -- +------------------------------------+
106 -- Pcode is a code value indicating the type of the patterm node. This
107 -- code is used both as the discriminant value for the record, and as
108 -- the case index in the main match routine that branches to the proper
109 -- match code for the given element.
111 -- Index is a serial index number. The use of these serial index
112 -- numbers is described in a separate section.
114 -- Pthen is a pointer to the successor node, i.e the node to be matched
115 -- if the attempt to match the node succeeds. If this is the last node
116 -- of the pattern to be matched, then Pthen points to a dummy node
117 -- of kind PC_EOP (end of pattern), which initiales pattern exit.
119 -- The parameter or parameters are present for certain node types,
120 -- and the type varies with the pattern code.
122 type Pattern_Code
is (
215 type IndexT
is range 0 .. +(2 **15 - 1);
217 type PE
(Pcode
: Pattern_Code
) is record
220 -- Serial index number of pattern element within pattern.
223 -- Successor element, to be matched after this one
243 PC_Unanchored
=> null;
248 PC_Arbno_X
=> Alt
: PE_Ptr
;
250 when PC_Rpat
=> PP
: Pattern_Ptr
;
252 when PC_Pred_Func
=> BF
: Boolean_Func
;
262 PC_String_VP
=> VP
: VString_Ptr
;
265 PC_Write_OnM
=> FP
: File_Ptr
;
267 when PC_String
=> Str
: String_Ptr
;
269 when PC_String_2
=> Str2
: String (1 .. 2);
271 when PC_String_3
=> Str3
: String (1 .. 3);
273 when PC_String_4
=> Str4
: String (1 .. 4);
275 when PC_String_5
=> Str5
: String (1 .. 5);
277 when PC_String_6
=> Str6
: String (1 .. 6);
279 when PC_Setcur
=> Var
: Natural_Ptr
;
287 PC_Span_CH
=> Char
: Character;
294 PC_Span_CS
=> CS
: Character_Set
;
301 PC_Tab_Nat
=> Nat
: Natural;
307 PC_Tab_NF
=> NF
: Natural_Func
;
313 PC_Tab_NP
=> NP
: Natural_Ptr
;
321 PC_String_VF
=> VF
: VString_Func
;
326 subtype PC_Has_Alt
is Pattern_Code
range PC_Alt
.. PC_Arbno_X
;
327 -- Range of pattern codes that has an Alt field. This is used in the
328 -- recursive traversals, since these links must be followed.
330 EOP_Element
: aliased constant PE
:= (PC_EOP
, 0, N
);
331 -- This is the end of pattern element, and is thus the representation of
332 -- a null pattern. It has a zero index element since it is never placed
333 -- inside a pattern. Furthermore it does not need a successor, since it
334 -- marks the end of the pattern, so that no more successors are needed.
336 EOP
: constant PE_Ptr
:= EOP_Element
'Unrestricted_Access;
337 -- This is the end of pattern pointer, that is used in the Pthen pointer
338 -- of other nodes to signal end of pattern.
340 -- The following array is used to determine if a pattern used as an
341 -- argument for Arbno is eligible for treatment using the simple Arbno
342 -- structure (i.e. it is a pattern that is guaranteed to match at least
343 -- one character on success, and not to make any entries on the stack.
345 OK_For_Simple_Arbno
:
346 array (Pattern_Code
) of Boolean := (
370 -------------------------------
371 -- The Pattern History Stack --
372 -------------------------------
374 -- The pattern history stack is used for controlling backtracking when
375 -- a match fails. The idea is to stack entries that give a cursor value
376 -- to be restored, and a node to be reestablished as the current node to
377 -- attempt an appropriate rematch operation. The processing for a pattern
378 -- element that has rematch alternatives pushes an appropriate entry or
379 -- entry on to the stack, and the proceeds. If a match fails at any point,
380 -- the top element of the stack is popped off, resetting the cursor and
381 -- the match continues by accessing the node stored with this entry.
383 type Stack_Entry
is record
386 -- Saved cursor value that is restored when this entry is popped
387 -- from the stack if a match attempt fails. Occasionally, this
388 -- field is used to store a history stack pointer instead of a
389 -- cursor. Such cases are noted in the documentation and the value
390 -- stored is negative since stack pointer values are always negative.
393 -- This pattern element reference is reestablished as the current
394 -- Node to be matched (which will attempt an appropriate rematch).
398 subtype Stack_Range
is Integer range -Stack_Size
.. -1;
400 type Stack_Type
is array (Stack_Range
) of Stack_Entry
;
401 -- The type used for a history stack. The actual instance of the stack
402 -- is declared as a local variable in the Match routine, to properly
403 -- handle recursive calls to Match. All stack pointer values are negative
404 -- to distinguish them from normal cursor values.
406 -- Note: the pattern matching stack is used only to handle backtracking.
407 -- If no backtracking occurs, its entries are never accessed, and never
408 -- popped off, and in particular it is normal for a successful match
409 -- to terminate with entries on the stack that are simply discarded.
411 -- Note: in subsequent diagrams of the stack, we always place element
412 -- zero (the deepest element) at the top of the page, then build the
413 -- stack down on the page with the most recent (top of stack) element
414 -- being the bottom-most entry on the page.
416 -- Stack checking is handled by labeling every pattern with the maximum
417 -- number of stack entries that are required, so a single check at the
418 -- start of matching the pattern suffices. There are two exceptions.
420 -- First, the count does not include entries for recursive pattern
421 -- references. Such recursions must therefore perform a specific
422 -- stack check with respect to the number of stack entries required
423 -- by the recursive pattern that is accessed and the amount of stack
424 -- that remains unused.
426 -- Second, the count includes only one iteration of an Arbno pattern,
427 -- so a specific check must be made on subsequent iterations that there
428 -- is still enough stack space left. The Arbno node has a field that
429 -- records the number of stack entries required by its argument for
432 ---------------------------------------------------
433 -- Use of Serial Index Field in Pattern Elements --
434 ---------------------------------------------------
436 -- The serial index numbers for the pattern elements are assigned as
437 -- a pattern is consructed from its constituent elements. Note that there
438 -- is never any sharing of pattern elements between patterns (copies are
439 -- always made), so the serial index numbers are unique to a particular
440 -- pattern as referenced from the P field of a value of type Pattern.
442 -- The index numbers meet three separate invariants, which are used for
443 -- various purposes as described in this section.
445 -- First, the numbers uniquely identify the pattern elements within a
446 -- pattern. If Num is the number of elements in a given pattern, then
447 -- the serial index numbers for the elements of this pattern will range
448 -- from 1 .. Num, so that each element has a separate value.
450 -- The purpose of this assignment is to provide a convenient auxiliary
451 -- data structure mechanism during operations which must traverse a
452 -- pattern (e.g. copy and finalization processing). Once constructed
453 -- patterns are strictly read only. This is necessary to allow sharing
454 -- of patterns between tasks. This means that we cannot go marking the
455 -- pattern (e.g. with a visited bit). Instead we cosntuct a separate
456 -- vector that contains the necessary information indexed by the Index
457 -- values in the pattern elements. For this purpose the only requirement
458 -- is that they be uniquely assigned.
460 -- Second, the pattern element referenced directly, i.e. the leading
461 -- pattern element, is always the maximum numbered element and therefore
462 -- indicates the total number of elements in the pattern. More precisely,
463 -- the element referenced by the P field of a pattern value, or the
464 -- element returned by any of the internal pattern construction routines
465 -- in the body (that return a value of type PE_Ptr) always is this
468 -- The purpose of this requirement is to allow an immediate determination
469 -- of the number of pattern elements within a pattern. This is used to
470 -- properly size the vectors used to contain auxiliary information for
471 -- traversal as described above.
473 -- Third, as compound pattern structures are constructed, the way in which
474 -- constituent parts of the pattern are constructed is stylized. This is
475 -- an automatic consequence of the way that these compounjd structures
476 -- are constructed, and basically what we are doing is simply documenting
477 -- and specifying the natural result of the pattern construction. The
478 -- section describing compound pattern structures gives details of the
479 -- numbering of each compound pattern structure.
481 -- The purpose of specifying the stylized numbering structures for the
482 -- compound patterns is to help simplify the processing in the Image
483 -- function, since it eases the task of retrieving the original recursive
484 -- structure of the pattern from the flat graph structure of elements.
485 -- This use in the Image function is the only point at which the code
486 -- makes use of the stylized structures.
488 type Ref_Array
is array (IndexT
range <>) of PE_Ptr
;
489 -- This type is used to build an array whose N'th entry references the
490 -- element in a pattern whose Index value is N. See Build_Ref_Array.
492 procedure Build_Ref_Array
(E
: PE_Ptr
; RA
: out Ref_Array
);
493 -- Given a pattern element which is the leading element of a pattern
494 -- structure, and a Ref_Array with bounds 1 .. E.Index, fills in the
495 -- Ref_Array so that its N'th entry references the element of the
496 -- referenced pattern whose Index value is N.
498 -------------------------------
499 -- Recursive Pattern Matches --
500 -------------------------------
502 -- The pattern primitive (+P) where P is a Pattern_Ptr or Pattern_Func
503 -- causes a recursive pattern match. This cannot be handled by an actual
504 -- recursive call to the outer level Match routine, since this would not
505 -- allow for possible backtracking into the region matched by the inner
506 -- pattern. Indeed this is the classical clash between recursion and
507 -- backtracking, and a simple recursive stack structure does not suffice.
509 -- This section describes how this recursion and the possible associated
510 -- backtracking is handled. We still use a single stack, but we establish
511 -- the concept of nested regions on this stack, each of which has a stack
512 -- base value pointing to the deepest stack entry of the region. The base
513 -- value for the outer level is zero.
515 -- When a recursive match is established, two special stack entries are
516 -- made. The first entry is used to save the original node that starts
517 -- the recursive match. This is saved so that the successor field of
518 -- this node is accessible at the end of the match, but it is never
519 -- popped and executed.
521 -- The second entry corresponds to a standard new region action. A
522 -- PC_R_Remove node is stacked, whose cursor field is used to store
523 -- the outer stack base, and the stack base is reset to point to
524 -- this PC_R_Remove node. Then the recursive pattern is matched and
525 -- it can make history stack entries in the normal matter, so now
526 -- the stack looks like:
528 -- (stack entries made by outer level)
530 -- (Special entry, node is (+P) successor
531 -- cursor entry is not used)
533 -- (PC_R_Remove entry, "cursor" value is (negative) <-- Stack base
534 -- saved base value for the enclosing region)
536 -- (stack entries made by inner level)
538 -- If a subsequent failure occurs and pops the PC_R_Remove node, it
539 -- removes itself and the special entry immediately underneath it,
540 -- restores the stack base value for the enclosing region, and then
541 -- again signals failure to look for alternatives that were stacked
542 -- before the recursion was initiated.
544 -- Now we need to consider what happens if the inner pattern succeeds, as
545 -- signalled by accessing the special PC_EOP pattern primitive. First we
546 -- recognize the nested case by looking at the Base value. If this Base
547 -- value is Stack'First, then the entire match has succeeded, but if the
548 -- base value is greater than Stack'First, then we have successfully
549 -- matched an inner pattern, and processing continues at the outer level.
551 -- There are two cases. The simple case is when the inner pattern has made
552 -- no stack entries, as recognized by the fact that the current stack
553 -- pointer is equal to the current base value. In this case it is fine to
554 -- remove all trace of the recursion by restoring the outer base value and
555 -- using the special entry to find the appropriate successor node.
557 -- The more complex case arises when the inner match does make stack
558 -- entries. In this case, the PC_EOP processing stacks a special entry
559 -- whose cursor value saves the saved inner base value (the one that
560 -- references the corresponding PC_R_Remove value), and whose node
561 -- pointer references a PC_R_Restore node, so the stack looks like:
563 -- (stack entries made by outer level)
565 -- (Special entry, node is (+P) successor,
566 -- cursor entry is not used)
568 -- (PC_R_Remove entry, "cursor" value is (negative)
569 -- saved base value for the enclosing region)
571 -- (stack entries made by inner level)
573 -- (PC_Region_Replace entry, "cursor" value is (negative)
574 -- stack pointer value referencing the PC_R_Remove entry).
576 -- If the entire match succeeds, then these stack entries are, as usual,
577 -- ignored and abandoned. If on the other hand a subsequent failure
578 -- causes the PC_Region_Replace entry to be popped, it restores the
579 -- inner base value from its saved "cursor" value and then fails again.
580 -- Note that it is OK that the cursor is temporarily clobbered by this
581 -- pop, since the second failure will reestablish a proper cursor value.
583 ---------------------------------
584 -- Compound Pattern Structures --
585 ---------------------------------
587 -- This section discusses the compound structures used to represent
588 -- constructed patterns. It shows the graph structures of pattern
589 -- elements that are constructed, and in the case of patterns that
590 -- provide backtracking possibilities, describes how the history
591 -- stack is used to control the backtracking. Finally, it notes the
592 -- way in which the Index numbers are assigned to the structure.
594 -- In all diagrams, solid lines (built witth minus signs or vertical
595 -- bars, represent successor pointers (Pthen fields) with > or V used
596 -- to indicate the direction of the pointer. The initial node of the
597 -- structure is in the upper left of the diagram. A dotted line is an
598 -- alternative pointer from the element above it to the element below
599 -- it. See individual sections for details on how alternatives are used.
605 -- In the pattern structures listed in this section, a line that looks
606 -- lile ----> with nothing to the right indicates an end of pattern
607 -- (EOP) pointer that represents the end of the match.
609 -- When a pattern concatenation (L & R) occurs, the resulting structure
610 -- is obtained by finding all such EOP pointers in L, and replacing
611 -- them to point to R. This is the most important flattening that
612 -- occurs in constructing a pattern, and it means that the pattern
613 -- matching circuitry does not have to keep track of the structure
614 -- of a pattern with respect to concatenation, since the appropriate
615 -- successor is always at hand.
617 -- Concatenation itself generates no additional possibilities for
618 -- backtracking, but the constituent patterns of the concatenated
619 -- structure will make stack entries as usual. The maximum amount
620 -- of stack required by the structure is thus simply the sum of the
621 -- maximums required by L and R.
623 -- The index numbering of a concatenation structure works by leaving
624 -- the numbering of the right hand pattern, R, unchanged and adjusting
625 -- the numbers in the left hand pattern, L up by the count of elements
626 -- in R. This ensures that the maximum numbered element is the leading
627 -- element as required (given that it was the leading element in L).
633 -- A pattern (L or R) constructs the structure:
636 -- | A |---->| L |---->
644 -- The A element here is a PC_Alt node, and the dotted line represents
645 -- the contents of the Alt field. When the PC_Alt element is matched,
646 -- it stacks a pointer to the leading element of R on the history stack
647 -- so that on subsequent failure, a match of R is attempted.
649 -- The A node is the higest numbered element in the pattern. The
650 -- original index numbers of R are unchanged, but the index numbers
651 -- of the L pattern are adjusted up by the count of elements in R.
653 -- Note that the difference between the index of the L leading element
654 -- the index of the R leading element (after building the alt structure)
655 -- indicates the number of nodes in L, and this is true even after the
656 -- structure is incorporated into some larger structure. For example,
657 -- if the A node has index 16, and L has index 15 and R has index
658 -- 5, then we know that L has 10 (15-5) elements in it.
660 -- Suppose that we now concatenate this structure to another pattern
661 -- with 9 elements in it. We will now have the A node with an index
662 -- of 25, L with an index of 24 and R with an index of 14. We still
663 -- know that L has 10 (24-14) elements in it, numbered 15-24, and
664 -- consequently the successor of the alternation structure has an
665 -- index with a value less than 15. This is used in Image to figure
666 -- out the original recursive structure of a pattern.
668 -- To clarify the interaction of the alternation and concatenation
669 -- structures, here is a more complex example of the structure built
672 -- (V or W or X) (Y or Z)
674 -- where A,B,C,D,E are all single element patterns:
676 -- +---+ +---+ +---+ +---+
677 -- I A I---->I V I---+-->I A I---->I Y I---->
678 -- +---+ +---+ I +---+ +---+
681 -- +---+ +---+ I +---+
682 -- I A I---->I W I-->I I Z I---->
683 -- +---+ +---+ I +---+
687 -- I X I------------>+
690 -- The numbering of the nodes would be as follows:
692 -- +---+ +---+ +---+ +---+
693 -- I 8 I---->I 7 I---+-->I 3 I---->I 2 I---->
694 -- +---+ +---+ I +---+ +---+
697 -- +---+ +---+ I +---+
698 -- I 6 I---->I 5 I-->I I 1 I---->
699 -- +---+ +---+ I +---+
703 -- I 4 I------------>+
706 -- Note: The above structure actually corresponds to
708 -- (A or (B or C)) (D or E)
712 -- ((A or B) or C) (D or E)
714 -- which is the more natural interpretation, but in fact alternation
715 -- is associative, and the construction of an alternative changes the
716 -- left grouped pattern to the right grouped pattern in any case, so
717 -- that the Image function produces a more natural looking output.
723 -- An Arb pattern builds the structure
734 -- The X node is a PC_Arb_X node, which matches null, and stacks a
735 -- pointer to Y node, which is the PC_Arb_Y node that matches one
736 -- extra character and restacks itself.
738 -- The PC_Arb_X node is numbered 2, and the PC_Arb_Y node is 1.
740 -------------------------
741 -- Arbno (simple case) --
742 -------------------------
744 -- The simple form of Arbno can be used where the pattern always
745 -- matches at least one character if it succeeds, and it is known
746 -- not to make any history stack entries. In this case, Arbno (P)
747 -- can construct the following structure:
761 -- The S (PC_Arbno_S) node matches null stacking a pointer to the
762 -- pattern P. If a subsequent failure causes P to be matched and
763 -- this match succeeds, then node A gets restacked to try another
764 -- instance if needed by a subsequent failure.
766 -- The node numbering of the constituent pattern P is not affected.
767 -- The S node has a node number of P.Index + 1.
769 --------------------------
770 -- Arbno (complex case) --
771 --------------------------
773 -- A call to Arbno (P), where P can match null (or at least is not
774 -- known to require a non-null string) and/or P requires pattern stack
775 -- entries, constructs the following structure:
777 -- +--------------------------+
785 -- +---+ +---+ +---+ |
786 -- | E |---->| P |---->| Y |--->+
789 -- The node X (PC_Arbno_X) matches null, stacking a pointer to the
790 -- E-P-X structure used to match one Arbno instance.
792 -- Here E is the PC_R_Enter node which matches null and creates two
793 -- stack entries. The first is a special entry whose node field is
794 -- not used at all, and whose cursor field has the initial cursor.
796 -- The second entry corresponds to a standard new region action. A
797 -- PC_R_Remove node is stacked, whose cursor field is used to store
798 -- the outer stack base, and the stack base is reset to point to
799 -- this PC_R_Remove node. Then the pattern P is matched, and it can
800 -- make history stack entries in the normal manner, so now the stack
803 -- (stack entries made before assign pattern)
805 -- (Special entry, node field not used,
806 -- used only to save initial cursor)
808 -- (PC_R_Remove entry, "cursor" value is (negative) <-- Stack Base
809 -- saved base value for the enclosing region)
811 -- (stack entries made by matching P)
813 -- If the match of P fails, then the PC_R_Remove entry is popped and
814 -- it removes both itself and the special entry underneath it,
815 -- restores the outer stack base, and signals failure.
817 -- If the match of P succeeds, then node Y, the PC_Arbno_Y node, pops
818 -- the inner region. There are two possibilities. If matching P left
819 -- no stack entries, then all traces of the inner region can be removed.
820 -- If there are stack entries, then we push an PC_Region_Replace stack
821 -- entry whose "cursor" value is the inner stack base value, and then
822 -- restore the outer stack base value, so the stack looks like:
824 -- (stack entries made before assign pattern)
826 -- (Special entry, node field not used,
827 -- used only to save initial cursor)
829 -- (PC_R_Remove entry, "cursor" value is (negative)
830 -- saved base value for the enclosing region)
832 -- (stack entries made by matching P)
834 -- (PC_Region_Replace entry, "cursor" value is (negative)
835 -- stack pointer value referencing the PC_R_Remove entry).
837 -- Now that we have matched another instance of the Arbno pattern,
838 -- we need to move to the successor. There are two cases. If the
839 -- Arbno pattern matched null, then there is no point in seeking
840 -- alternatives, since we would just match a whole bunch of nulls.
841 -- In this case we look through the alternative node, and move
842 -- directly to its successor (i.e. the successor of the Arbno
843 -- pattern). If on the other hand a non-null string was matched,
844 -- we simply follow the successor to the alternative node, which
845 -- sets up for another possible match of the Arbno pattern.
847 -- As noted in the section on stack checking, the stack count (and
848 -- hence the stack check) for a pattern includes only one iteration
849 -- of the Arbno pattern. To make sure that multiple iterations do not
850 -- overflow the stack, the Arbno node saves the stack count required
851 -- by a single iteration, and the Concat function increments this to
852 -- include stack entries required by any successor. The PC_Arbno_Y
853 -- node uses this count to ensure that sufficient stack remains
854 -- before proceeding after matching each new instance.
856 -- The node numbering of the constituent pattern P is not affected.
857 -- Where N is the number of nodes in P, the Y node is numbered N + 1,
858 -- the E node is N + 2, and the X node is N + 3.
860 ----------------------
861 -- Assign Immediate --
862 ----------------------
864 -- Immediate assignment (P * V) constructs the following structure
867 -- | E |---->| P |---->| A |---->
870 -- Here E is the PC_R_Enter node which matches null and creates two
871 -- stack entries. The first is a special entry whose node field is
872 -- not used at all, and whose cursor field has the initial cursor.
874 -- The second entry corresponds to a standard new region action. A
875 -- PC_R_Remove node is stacked, whose cursor field is used to store
876 -- the outer stack base, and the stack base is reset to point to
877 -- this PC_R_Remove node. Then the pattern P is matched, and it can
878 -- make history stack entries in the normal manner, so now the stack
881 -- (stack entries made before assign pattern)
883 -- (Special entry, node field not used,
884 -- used only to save initial cursor)
886 -- (PC_R_Remove entry, "cursor" value is (negative) <-- Stack Base
887 -- saved base value for the enclosing region)
889 -- (stack entries made by matching P)
891 -- If the match of P fails, then the PC_R_Remove entry is popped
892 -- and it removes both itself and the special entry underneath it,
893 -- restores the outer stack base, and signals failure.
895 -- If the match of P succeeds, then node A, which is the actual
896 -- PC_Assign_Imm node, executes the assignment (using the stack
897 -- base to locate the entry with the saved starting cursor value),
898 -- and the pops the inner region. There are two possibilities, if
899 -- matching P left no stack entries, then all traces of the inner
900 -- region can be removed. If there are stack entries, then we push
901 -- an PC_Region_Replace stack entry whose "cursor" value is the
902 -- inner stack base value, and then restore the outer stack base
903 -- value, so the stack looks like:
905 -- (stack entries made before assign pattern)
907 -- (Special entry, node field not used,
908 -- used only to save initial cursor)
910 -- (PC_R_Remove entry, "cursor" value is (negative)
911 -- saved base value for the enclosing region)
913 -- (stack entries made by matching P)
915 -- (PC_Region_Replace entry, "cursor" value is the (negative)
916 -- stack pointer value referencing the PC_R_Remove entry).
918 -- If a subsequent failure occurs, the PC_Region_Replace node restores
919 -- the inner stack base value and signals failure to explore rematches
922 -- The node numbering of the constituent pattern P is not affected.
923 -- Where N is the number of nodes in P, the A node is numbered N + 1,
924 -- and the E node is N + 2.
926 ---------------------
927 -- Assign On Match --
928 ---------------------
930 -- The assign on match (**) pattern is quite similar to the assign
931 -- immediate pattern, except that the actual assignment has to be
932 -- delayed. The following structure is constructed:
935 -- | E |---->| P |---->| A |---->
938 -- The operation of this pattern is identical to that described above
939 -- for deferred assignment, up to the point where P has been matched.
941 -- The A node, which is the PC_Assign_OnM node first pushes a
942 -- PC_Assign node onto the history stack. This node saves the ending
943 -- cursor and acts as a flag for the final assignment, as further
946 -- It then stores a pointer to itself in the special entry node field.
947 -- This was otherwise unused, and is now used to retrive the address
948 -- of the variable to be assigned at the end of the pattern.
950 -- After that the inner region is terminated in the usual manner,
951 -- by stacking a PC_R_Restore entry as described for the assign
952 -- immediate case. Note that the optimization of completely
953 -- removing the inner region does not happen in this case, since
954 -- we have at least one stack entry (the PC_Assign one we just made).
955 -- The stack now looks like:
957 -- (stack entries made before assign pattern)
959 -- (Special entry, node points to copy of
960 -- the PC_Assign_OnM node, and the
961 -- cursor field saves the initial cursor).
963 -- (PC_R_Remove entry, "cursor" value is (negative)
964 -- saved base value for the enclosing region)
966 -- (stack entries made by matching P)
968 -- (PC_Assign entry, saves final cursor)
970 -- (PC_Region_Replace entry, "cursor" value is (negative)
971 -- stack pointer value referencing the PC_R_Remove entry).
973 -- If a subsequent failure causes the PC_Assign node to execute it
974 -- simply removes itself and propagates the failure.
976 -- If the match succeeds, then the history stack is scanned for
977 -- PC_Assign nodes, and the assignments are executed (examination
978 -- of the above diagram will show that all the necessary data is
979 -- at hand for the assignment).
981 -- To optimize the common case where no assign-on-match operations
982 -- are present, a global flag Assign_OnM is maintained which is
983 -- initialize to False, and gets set True as part of the execution
984 -- of the PC_Assign_OnM node. The scan of the history stack for
985 -- PC_Assign entries is done only if this flag is set.
987 -- The node numbering of the constituent pattern P is not affected.
988 -- Where N is the number of nodes in P, the A node is numbered N + 1,
989 -- and the E node is N + 2.
995 -- Bal builds a single node:
1001 -- The node B is the PC_Bal node which matches a parentheses balanced
1002 -- string, starting at the current cursor position. It then updates
1003 -- the cursor past this matched string, and stacks a pointer to itself
1004 -- with this updated cursor value on the history stack, to extend the
1005 -- matched string on a subequent failure.
1007 -- Since this is a single node it is numbered 1 (the reason we include
1008 -- it in the compound patterns section is that it backtracks).
1014 -- BreakX builds the structure
1017 -- | B |---->| A |---->
1025 -- Here the B node is the BreakX_xx node that performs a normal Break
1026 -- function. The A node is an alternative (PC_Alt) node that matches
1027 -- null, but stacks a pointer to node X (the PC_BreakX_X node) which
1028 -- extends the match one character (to eat up the previously detected
1029 -- break character), and then rematches the break.
1031 -- The B node is numbered 3, the alternative node is 1, and the X
1038 -- Fence builds a single node:
1044 -- The element F, PC_Fence, matches null, and stacks a pointer to a
1045 -- PC_Cancel element which will abort the match on a subsequent failure.
1047 -- Since this is a single element it is numbered 1 (the reason we
1048 -- include it in the compound patterns section is that it backtracks).
1050 --------------------
1051 -- Fence Function --
1052 --------------------
1054 -- A call to the Fence function builds the structure:
1056 -- +---+ +---+ +---+
1057 -- | E |---->| P |---->| X |---->
1058 -- +---+ +---+ +---+
1060 -- Here E is the PC_R_Enter node which matches null and creates two
1061 -- stack entries. The first is a special entry which is not used at
1062 -- all in the fence case (it is present merely for uniformity with
1063 -- other cases of region enter operations).
1065 -- The second entry corresponds to a standard new region action. A
1066 -- PC_R_Remove node is stacked, whose cursor field is used to store
1067 -- the outer stack base, and the stack base is reset to point to
1068 -- this PC_R_Remove node. Then the pattern P is matched, and it can
1069 -- make history stack entries in the normal manner, so now the stack
1072 -- (stack entries made before fence pattern)
1074 -- (Special entry, not used at all)
1076 -- (PC_R_Remove entry, "cursor" value is (negative) <-- Stack Base
1077 -- saved base value for the enclosing region)
1079 -- (stack entries made by matching P)
1081 -- If the match of P fails, then the PC_R_Remove entry is popped
1082 -- and it removes both itself and the special entry underneath it,
1083 -- restores the outer stack base, and signals failure.
1085 -- If the match of P succeeds, then node X, the PC_Fence_X node, gets
1086 -- control. One might be tempted to think that at this point, the
1087 -- history stack entries made by matching P can just be removed since
1088 -- they certainly are not going to be used for rematching (that is
1089 -- whole point of Fence after all!) However, this is wrong, because
1090 -- it would result in the loss of possible assign-on-match entries
1091 -- for deferred pattern assignments.
1093 -- Instead what we do is to make a special entry whose node references
1094 -- PC_Fence_Y, and whose cursor saves the inner stack base value, i.e.
1095 -- the pointer to the PC_R_Remove entry. Then the outer stack base
1096 -- pointer is restored, so the stack looks like:
1098 -- (stack entries made before assign pattern)
1100 -- (Special entry, not used at all)
1102 -- (PC_R_Remove entry, "cursor" value is (negative)
1103 -- saved base value for the enclosing region)
1105 -- (stack entries made by matching P)
1107 -- (PC_Fence_Y entry, "cursor" value is (negative) stack
1108 -- pointer value referencing the PC_R_Remove entry).
1110 -- If a subsequent failure occurs, then the PC_Fence_Y entry removes
1111 -- the entire inner region, including all entries made by matching P,
1112 -- and alternatives prior to the Fence pattern are sought.
1114 -- The node numbering of the constituent pattern P is not affected.
1115 -- Where N is the number of nodes in P, the X node is numbered N + 1,
1116 -- and the E node is N + 2.
1122 -- Succeed builds a single node:
1128 -- The node S is the PC_Succeed node which matches null, and stacks
1129 -- a pointer to itself on the history stack, so that a subsequent
1130 -- failure repeats the same match.
1132 -- Since this is a single node it is numbered 1 (the reason we include
1133 -- it in the compound patterns section is that it backtracks).
1135 ---------------------
1136 -- Write Immediate --
1137 ---------------------
1139 -- The structure built for a write immediate operation (P * F, where
1140 -- F is a file access value) is:
1142 -- +---+ +---+ +---+
1143 -- | E |---->| P |---->| W |---->
1144 -- +---+ +---+ +---+
1146 -- Here E is the PC_R_Enter node and W is the PC_Write_Imm node. The
1147 -- handling is identical to that described above for Assign Immediate,
1148 -- except that at the point where a successful match occurs, the matched
1149 -- substring is written to the referenced file.
1151 -- The node numbering of the constituent pattern P is not affected.
1152 -- Where N is the number of nodes in P, the W node is numbered N + 1,
1153 -- and the E node is N + 2.
1155 --------------------
1156 -- Write On Match --
1157 --------------------
1159 -- The structure built for a write on match operation (P ** F, where
1160 -- F is a file access value) is:
1162 -- +---+ +---+ +---+
1163 -- | E |---->| P |---->| W |---->
1164 -- +---+ +---+ +---+
1166 -- Here E is the PC_R_Enter node and W is the PC_Write_OnM node. The
1167 -- handling is identical to that described above for Assign On Match,
1168 -- except that at the point where a successful match has completed,
1169 -- the matched substring is written to the referenced file.
1171 -- The node numbering of the constituent pattern P is not affected.
1172 -- Where N is the number of nodes in P, the W node is numbered N + 1,
1173 -- and the E node is N + 2.
1174 -----------------------
1175 -- Constant Patterns --
1176 -----------------------
1178 -- The following pattern elements are referenced only from the pattern
1179 -- history stack. In each case the processing for the pattern element
1180 -- results in pattern match abort, or futher failure, so there is no
1181 -- need for a successor and no need for a node number
1183 CP_Assign
: aliased PE
:= (PC_Assign
, 0, N
);
1184 CP_Cancel
: aliased PE
:= (PC_Cancel
, 0, N
);
1185 CP_Fence_Y
: aliased PE
:= (PC_Fence_Y
, 0, N
);
1186 CP_R_Remove
: aliased PE
:= (PC_R_Remove
, 0, N
);
1187 CP_R_Restore
: aliased PE
:= (PC_R_Restore
, 0, N
);
1189 -----------------------
1190 -- Local Subprograms --
1191 -----------------------
1193 function Alternate
(L
, R
: PE_Ptr
) return PE_Ptr
;
1194 function "or" (L
, R
: PE_Ptr
) return PE_Ptr
renames Alternate
;
1195 -- Build pattern structure corresponding to the alternation of L, R.
1196 -- (i.e. try to match L, and if that fails, try to match R).
1198 function Arbno_Simple
(P
: PE_Ptr
) return PE_Ptr
;
1199 -- Build simple Arbno pattern, P is a pattern that is guaranteed to
1200 -- match at least one character if it succeeds and to require no
1201 -- stack entries under all circumstances. The result returned is
1202 -- a simple Arbno structure as previously described.
1204 function Bracket
(E
, P
, A
: PE_Ptr
) return PE_Ptr
;
1205 -- Given two single node pattern elements E and A, and a (possible
1206 -- complex) pattern P, construct the concatenation E-->P-->A and
1207 -- return a pointer to E. The concatenation does not affect the
1208 -- node numbering in P. A has a number one higher than the maximum
1209 -- number in P, and E has a number two higher than the maximum
1210 -- number in P (see for example the Assign_Immediate structure to
1211 -- understand a typical use of this function).
1213 function BreakX_Make
(B
: PE_Ptr
) return Pattern
;
1214 -- Given a pattern element for a Break patternx, returns the
1215 -- corresponding BreakX compound pattern structure.
1217 function Concat
(L
, R
: PE_Ptr
; Incr
: Natural) return PE_Ptr
;
1218 -- Creates a pattern eelement that represents a concatenation of the
1219 -- two given pattern elements (i.e. the pattern L followed by R).
1220 -- The result returned is always the same as L, but the pattern
1221 -- referenced by L is modified to have R as a successor. This
1222 -- procedure does not copy L or R, so if a copy is required, it
1223 -- is the responsibility of the caller. The Incr parameter is an
1224 -- amount to be added to the Nat field of any P_Arbno_Y node that is
1225 -- in the left operand, it represents the additional stack space
1226 -- required by the right operand.
1228 function C_To_PE
(C
: PChar
) return PE_Ptr
;
1229 -- Given a character, constructs a pattern element that matches
1230 -- the single character.
1232 function Copy
(P
: PE_Ptr
) return PE_Ptr
;
1233 -- Creates a copy of the pattern element referenced by the given
1234 -- pattern element reference. This is a deep copy, which means that
1235 -- it follows the Next and Alt pointers.
1237 function Image
(P
: PE_Ptr
) return String;
1238 -- Returns the image of the address of the referenced pattern element.
1239 -- This is equivalent to Image (To_Address (P));
1241 function Is_In
(C
: Character; Str
: String) return Boolean;
1242 pragma Inline
(Is_In
);
1243 -- Determines if the character C is in string Str.
1245 procedure Logic_Error
;
1246 -- Called to raise Program_Error with an appropriate message if an
1247 -- internal logic error is detected.
1249 function Str_BF
(A
: Boolean_Func
) return String;
1250 function Str_FP
(A
: File_Ptr
) return String;
1251 function Str_NF
(A
: Natural_Func
) return String;
1252 function Str_NP
(A
: Natural_Ptr
) return String;
1253 function Str_PP
(A
: Pattern_Ptr
) return String;
1254 function Str_VF
(A
: VString_Func
) return String;
1255 function Str_VP
(A
: VString_Ptr
) return String;
1256 -- These are debugging routines, which return a representation of the
1257 -- given access value (they are called only by Image and Dump)
1259 procedure Set_Successor
(Pat
: PE_Ptr
; Succ
: PE_Ptr
);
1260 -- Adjusts all EOP pointers in Pat to point to Succ. No other changes
1261 -- are made. In particular, Succ is unchanged, and no index numbers
1262 -- are modified. Note that Pat may not be equal to EOP on entry.
1264 function S_To_PE
(Str
: PString
) return PE_Ptr
;
1265 -- Given a string, constructs a pattern element that matches the string
1267 procedure Uninitialized_Pattern
;
1268 pragma No_Return
(Uninitialized_Pattern
);
1269 -- Called to raise Program_Error with an appropriate error message if
1270 -- an uninitialized pattern is used in any pattern construction or
1271 -- pattern matching operation.
1277 Start
: out Natural;
1278 Stop
: out Natural);
1279 -- This is the common pattern match routine. It is passed a string and
1280 -- a pattern, and it indicates success or failure, and on success the
1281 -- section of the string matched. It does not perform any assignments
1282 -- to the subject string, so pattern replacement is for the caller.
1284 -- Subject The subject string. The lower bound is always one. In the
1285 -- Match procedures, it is fine to use strings whose lower bound
1286 -- is not one, but we perform a one time conversion before the
1287 -- call to XMatch, so that XMatch does not have to be bothered
1288 -- with strange lower bounds.
1290 -- Pat_P Points to initial pattern element of pattern to be matched
1292 -- Pat_S Maximum required stack entries for pattern to be matched
1294 -- Start If match is successful, starting index of matched section.
1295 -- This value is always non-zero. A value of zero is used to
1296 -- indicate a failed match.
1298 -- Stop If match is successful, ending index of matched section.
1299 -- This can be zero if we match the null string at the start,
1300 -- in which case Start is set to zero, and Stop to one. If the
1301 -- Match fails, then the contents of Stop is undefined.
1307 Start
: out Natural;
1308 Stop
: out Natural);
1309 -- Identical in all respects to XMatch, except that trace information is
1310 -- output on Standard_Output during execution of the match. This is the
1311 -- version that is called if the original Match call has Debug => True.
1317 function "&" (L
: PString
; R
: Pattern
) return Pattern
is
1319 return (AFC
with R
.Stk
, Concat
(S_To_PE
(L
), Copy
(R
.P
), R
.Stk
));
1322 function "&" (L
: Pattern
; R
: PString
) return Pattern
is
1324 return (AFC
with L
.Stk
, Concat
(Copy
(L
.P
), S_To_PE
(R
), 0));
1327 function "&" (L
: PChar
; R
: Pattern
) return Pattern
is
1329 return (AFC
with R
.Stk
, Concat
(C_To_PE
(L
), Copy
(R
.P
), R
.Stk
));
1332 function "&" (L
: Pattern
; R
: PChar
) return Pattern
is
1334 return (AFC
with L
.Stk
, Concat
(Copy
(L
.P
), C_To_PE
(R
), 0));
1337 function "&" (L
: Pattern
; R
: Pattern
) return Pattern
is
1339 return (AFC
with L
.Stk
+ R
.Stk
, Concat
(Copy
(L
.P
), Copy
(R
.P
), R
.Stk
));
1348 -- +---+ +---+ +---+
1349 -- | E |---->| P |---->| A |---->
1350 -- +---+ +---+ +---+
1352 -- The node numbering of the constituent pattern P is not affected.
1353 -- Where N is the number of nodes in P, the A node is numbered N + 1,
1354 -- and the E node is N + 2.
1356 function "*" (P
: Pattern
; Var
: VString_Var
) return Pattern
is
1357 Pat
: constant PE_Ptr
:= Copy
(P
.P
);
1358 E
: constant PE_Ptr
:= new PE
'(PC_R_Enter, 0, EOP);
1359 A : constant PE_Ptr :=
1360 new PE'(PC_Assign_Imm
, 0, EOP
, Var
'Unrestricted_Access);
1363 return (AFC
with P
.Stk
+ 3, Bracket
(E
, Pat
, A
));
1366 function "*" (P
: PString
; Var
: VString_Var
) return Pattern
is
1367 Pat
: constant PE_Ptr
:= S_To_PE
(P
);
1368 E
: constant PE_Ptr
:= new PE
'(PC_R_Enter, 0, EOP);
1369 A : constant PE_Ptr :=
1370 new PE'(PC_Assign_Imm
, 0, EOP
, Var
'Unrestricted_Access);
1373 return (AFC
with 3, Bracket
(E
, Pat
, A
));
1376 function "*" (P
: PChar
; Var
: VString_Var
) return Pattern
is
1377 Pat
: constant PE_Ptr
:= C_To_PE
(P
);
1378 E
: constant PE_Ptr
:= new PE
'(PC_R_Enter, 0, EOP);
1379 A : constant PE_Ptr :=
1380 new PE'(PC_Assign_Imm
, 0, EOP
, Var
'Unrestricted_Access);
1383 return (AFC
with 3, Bracket
(E
, Pat
, A
));
1388 -- +---+ +---+ +---+
1389 -- | E |---->| P |---->| W |---->
1390 -- +---+ +---+ +---+
1392 -- The node numbering of the constituent pattern P is not affected.
1393 -- Where N is the number of nodes in P, the W node is numbered N + 1,
1394 -- and the E node is N + 2.
1396 function "*" (P
: Pattern
; Fil
: File_Access
) return Pattern
is
1397 Pat
: constant PE_Ptr
:= Copy
(P
.P
);
1398 E
: constant PE_Ptr
:= new PE
'(PC_R_Enter, 0, EOP);
1399 W : constant PE_Ptr := new PE'(PC_Write_Imm
, 0, EOP
, Fil
);
1402 return (AFC
with 3, Bracket
(E
, Pat
, W
));
1405 function "*" (P
: PString
; Fil
: File_Access
) return Pattern
is
1406 Pat
: constant PE_Ptr
:= S_To_PE
(P
);
1407 E
: constant PE_Ptr
:= new PE
'(PC_R_Enter, 0, EOP);
1408 W : constant PE_Ptr := new PE'(PC_Write_Imm
, 0, EOP
, Fil
);
1411 return (AFC
with 3, Bracket
(E
, Pat
, W
));
1414 function "*" (P
: PChar
; Fil
: File_Access
) return Pattern
is
1415 Pat
: constant PE_Ptr
:= C_To_PE
(P
);
1416 E
: constant PE_Ptr
:= new PE
'(PC_R_Enter, 0, EOP);
1417 W : constant PE_Ptr := new PE'(PC_Write_Imm
, 0, EOP
, Fil
);
1420 return (AFC
with 3, Bracket
(E
, Pat
, W
));
1429 -- +---+ +---+ +---+
1430 -- | E |---->| P |---->| A |---->
1431 -- +---+ +---+ +---+
1433 -- The node numbering of the constituent pattern P is not affected.
1434 -- Where N is the number of nodes in P, the A node is numbered N + 1,
1435 -- and the E node is N + 2.
1437 function "**" (P
: Pattern
; Var
: VString_Var
) return Pattern
is
1438 Pat
: constant PE_Ptr
:= Copy
(P
.P
);
1439 E
: constant PE_Ptr
:= new PE
'(PC_R_Enter, 0, EOP);
1440 A : constant PE_Ptr :=
1441 new PE'(PC_Assign_OnM
, 0, EOP
, Var
'Unrestricted_Access);
1444 return (AFC
with P
.Stk
+ 3, Bracket
(E
, Pat
, A
));
1447 function "**" (P
: PString
; Var
: VString_Var
) return Pattern
is
1448 Pat
: constant PE_Ptr
:= S_To_PE
(P
);
1449 E
: constant PE_Ptr
:= new PE
'(PC_R_Enter, 0, EOP);
1450 A : constant PE_Ptr :=
1451 new PE'(PC_Assign_OnM
, 0, EOP
, Var
'Unrestricted_Access);
1454 return (AFC
with 3, Bracket
(E
, Pat
, A
));
1457 function "**" (P
: PChar
; Var
: VString_Var
) return Pattern
is
1458 Pat
: constant PE_Ptr
:= C_To_PE
(P
);
1459 E
: constant PE_Ptr
:= new PE
'(PC_R_Enter, 0, EOP);
1460 A : constant PE_Ptr :=
1461 new PE'(PC_Assign_OnM
, 0, EOP
, Var
'Unrestricted_Access);
1464 return (AFC
with 3, Bracket
(E
, Pat
, A
));
1469 -- +---+ +---+ +---+
1470 -- | E |---->| P |---->| W |---->
1471 -- +---+ +---+ +---+
1473 -- The node numbering of the constituent pattern P is not affected.
1474 -- Where N is the number of nodes in P, the W node is numbered N + 1,
1475 -- and the E node is N + 2.
1477 function "**" (P
: Pattern
; Fil
: File_Access
) return Pattern
is
1478 Pat
: constant PE_Ptr
:= Copy
(P
.P
);
1479 E
: constant PE_Ptr
:= new PE
'(PC_R_Enter, 0, EOP);
1480 W : constant PE_Ptr := new PE'(PC_Write_OnM
, 0, EOP
, Fil
);
1483 return (AFC
with P
.Stk
+ 3, Bracket
(E
, Pat
, W
));
1486 function "**" (P
: PString
; Fil
: File_Access
) return Pattern
is
1487 Pat
: constant PE_Ptr
:= S_To_PE
(P
);
1488 E
: constant PE_Ptr
:= new PE
'(PC_R_Enter, 0, EOP);
1489 W : constant PE_Ptr := new PE'(PC_Write_OnM
, 0, EOP
, Fil
);
1492 return (AFC
with 3, Bracket
(E
, Pat
, W
));
1495 function "**" (P
: PChar
; Fil
: File_Access
) return Pattern
is
1496 Pat
: constant PE_Ptr
:= C_To_PE
(P
);
1497 E
: constant PE_Ptr
:= new PE
'(PC_R_Enter, 0, EOP);
1498 W : constant PE_Ptr := new PE'(PC_Write_OnM
, 0, EOP
, Fil
);
1501 return (AFC
with 3, Bracket
(E
, Pat
, W
));
1508 function "+" (Str
: VString_Var
) return Pattern
is
1512 new PE
'(PC_String_VP, 1, EOP, Str'Unrestricted_Access));
1515 function "+" (Str : VString_Func) return Pattern is
1517 return (AFC with 0, new PE'(PC_String_VF
, 1, EOP
, Str
));
1520 function "+" (P
: Pattern_Var
) return Pattern
is
1524 new PE
'(PC_Rpat, 1, EOP, P'Unrestricted_Access));
1527 function "+" (P : Boolean_Func) return Pattern is
1529 return (AFC with 3, new PE'(PC_Pred_Func
, 1, EOP
, P
));
1536 function "or" (L
: PString
; R
: Pattern
) return Pattern
is
1538 return (AFC
with R
.Stk
+ 1, S_To_PE
(L
) or Copy
(R
.P
));
1541 function "or" (L
: Pattern
; R
: PString
) return Pattern
is
1543 return (AFC
with L
.Stk
+ 1, Copy
(L
.P
) or S_To_PE
(R
));
1546 function "or" (L
: PString
; R
: PString
) return Pattern
is
1548 return (AFC
with 1, S_To_PE
(L
) or S_To_PE
(R
));
1551 function "or" (L
: Pattern
; R
: Pattern
) return Pattern
is
1554 Natural'Max (L
.Stk
, R
.Stk
) + 1, Copy
(L
.P
) or Copy
(R
.P
));
1557 function "or" (L
: PChar
; R
: Pattern
) return Pattern
is
1559 return (AFC
with 1, C_To_PE
(L
) or Copy
(R
.P
));
1562 function "or" (L
: Pattern
; R
: PChar
) return Pattern
is
1564 return (AFC
with 1, Copy
(L
.P
) or C_To_PE
(R
));
1567 function "or" (L
: PChar
; R
: PChar
) return Pattern
is
1569 return (AFC
with 1, C_To_PE
(L
) or C_To_PE
(R
));
1572 function "or" (L
: PString
; R
: PChar
) return Pattern
is
1574 return (AFC
with 1, S_To_PE
(L
) or C_To_PE
(R
));
1577 function "or" (L
: PChar
; R
: PString
) return Pattern
is
1579 return (AFC
with 1, C_To_PE
(L
) or S_To_PE
(R
));
1586 -- No two patterns share the same pattern elements, so the adjust
1587 -- procedure for a Pattern assignment must do a deep copy of the
1588 -- pattern element structure.
1590 procedure Adjust
(Object
: in out Pattern
) is
1592 Object
.P
:= Copy
(Object
.P
);
1599 function Alternate
(L
, R
: PE_Ptr
) return PE_Ptr
is
1601 -- If the left pattern is null, then we just add the alternation
1602 -- node with an index one greater than the right hand pattern.
1605 return new PE
'(PC_Alt, R.Index + 1, EOP, R);
1607 -- If the left pattern is non-null, then build a reference vector
1608 -- for its elements, and adjust their index values to acccomodate
1609 -- the right hand elements. Then add the alternation node.
1613 Refs : Ref_Array (1 .. L.Index);
1616 Build_Ref_Array (L, Refs);
1618 for J in Refs'Range loop
1619 Refs (J).Index := Refs (J).Index + R.Index;
1623 return new PE'(PC_Alt
, L
.Index
+ 1, L
, R
);
1631 function Any
(Str
: String) return Pattern
is
1633 return (AFC
with 0, new PE
'(PC_Any_CS, 1, EOP, To_Set (Str)));
1636 function Any (Str : VString) return Pattern is
1638 return Any (S (Str));
1641 function Any (Str : Character) return Pattern is
1643 return (AFC with 0, new PE'(PC_Any_CH
, 1, EOP
, Str
));
1646 function Any
(Str
: Character_Set
) return Pattern
is
1648 return (AFC
with 0, new PE
'(PC_Any_CS, 1, EOP, Str));
1651 function Any (Str : access VString) return Pattern is
1653 return (AFC with 0, new PE'(PC_Any_VP
, 1, EOP
, VString_Ptr
(Str
)));
1656 function Any
(Str
: VString_Func
) return Pattern
is
1658 return (AFC
with 0, new PE
'(PC_Any_VF, 1, EOP, Str));
1674 -- The PC_Arb_X element is numbered 2, and the PC_Arb_Y element is 1.
1676 function Arb return Pattern is
1677 Y : constant PE_Ptr := new PE'(PC_Arb_Y
, 1, EOP
);
1678 X
: constant PE_Ptr
:= new PE
'(PC_Arb_X, 2, EOP, Y);
1681 return (AFC with 1, X);
1688 function Arbno (P : PString) return Pattern is
1690 if P'Length = 0 then
1691 return (AFC with 0, EOP);
1694 return (AFC with 0, Arbno_Simple (S_To_PE (P)));
1698 function Arbno (P : PChar) return Pattern is
1700 return (AFC with 0, Arbno_Simple (C_To_PE (P)));
1703 function Arbno (P : Pattern) return Pattern is
1704 Pat : constant PE_Ptr := Copy (P.P);
1708 and then OK_For_Simple_Arbno (Pat.Pcode)
1710 return (AFC with 0, Arbno_Simple (Pat));
1713 -- This is the complex case, either the pattern makes stack entries
1714 -- or it is possible for the pattern to match the null string (more
1715 -- accurately, we don't know that this is not the case).
1717 -- +--------------------------+
1725 -- +---+ +---+ +---+ |
1726 -- | E |---->| P |---->| Y |--->+
1727 -- +---+ +---+ +---+
1729 -- The node numbering of the constituent pattern P is not affected.
1730 -- Where N is the number of nodes in P, the Y node is numbered N + 1,
1731 -- the E node is N + 2, and the X node is N + 3.
1734 E : constant PE_Ptr := new PE'(PC_R_Enter
, 0, EOP
);
1735 X
: constant PE_Ptr
:= new PE
'(PC_Arbno_X, 0, EOP, E);
1736 Y : constant PE_Ptr := new PE'(PC_Arbno_Y
, 0, X
, P
.Stk
+ 3);
1737 EPY
: constant PE_Ptr
:= Bracket
(E
, Pat
, Y
);
1741 X
.Index
:= EPY
.Index
+ 1;
1742 return (AFC
with P
.Stk
+ 3, X
);
1759 -- | P |---------->+
1762 -- The node numbering of the constituent pattern P is not affected.
1763 -- The S node has a node number of P.Index + 1.
1765 -- Note that we know that P cannot be EOP, because a null pattern
1766 -- does not meet the requirements for simple Arbno.
1768 function Arbno_Simple
(P
: PE_Ptr
) return PE_Ptr
is
1769 S
: constant PE_Ptr
:= new PE
'(PC_Arbno_S, P.Index + 1, EOP, P);
1772 Set_Successor (P, S);
1780 function Bal return Pattern is
1782 return (AFC with 1, new PE'(PC_Bal
, 1, EOP
));
1789 function Bracket
(E
, P
, A
: PE_Ptr
) return PE_Ptr
is
1798 Set_Successor
(P
, A
);
1799 E
.Index
:= P
.Index
+ 2;
1800 A
.Index
:= P
.Index
+ 1;
1810 function Break
(Str
: String) return Pattern
is
1812 return (AFC
with 0, new PE
'(PC_Break_CS, 1, EOP, To_Set (Str)));
1815 function Break (Str : VString) return Pattern is
1817 return Break (S (Str));
1820 function Break (Str : Character) return Pattern is
1822 return (AFC with 0, new PE'(PC_Break_CH
, 1, EOP
, Str
));
1825 function Break
(Str
: Character_Set
) return Pattern
is
1827 return (AFC
with 0, new PE
'(PC_Break_CS, 1, EOP, Str));
1830 function Break (Str : access VString) return Pattern is
1832 return (AFC with 0, new PE'(PC_Break_VP
, 1, EOP
, VString_Ptr
(Str
)));
1835 function Break
(Str
: VString_Func
) return Pattern
is
1837 return (AFC
with 0, new PE
'(PC_Break_VF, 1, EOP, Str));
1844 function BreakX (Str : String) return Pattern is
1846 return BreakX_Make (new PE'(PC_BreakX_CS
, 3, N
, To_Set
(Str
)));
1849 function BreakX
(Str
: VString
) return Pattern
is
1851 return BreakX
(S
(Str
));
1854 function BreakX
(Str
: Character) return Pattern
is
1856 return BreakX_Make
(new PE
'(PC_BreakX_CH, 3, N, Str));
1859 function BreakX (Str : Character_Set) return Pattern is
1861 return BreakX_Make (new PE'(PC_BreakX_CS
, 3, N
, Str
));
1864 function BreakX
(Str
: access VString
) return Pattern
is
1866 return BreakX_Make
(new PE
'(PC_BreakX_VP, 3, N, VString_Ptr (Str)));
1869 function BreakX (Str : VString_Func) return Pattern is
1871 return BreakX_Make (new PE'(PC_BreakX_VF
, 3, N
, Str
));
1879 -- | B |---->| A |---->
1887 -- The B node is numbered 3, the alternative node is 1, and the X
1890 function BreakX_Make
(B
: PE_Ptr
) return Pattern
is
1891 X
: constant PE_Ptr
:= new PE
'(PC_BreakX_X, 2, B);
1892 A : constant PE_Ptr := new PE'(PC_Alt
, 1, EOP
, X
);
1896 return (AFC
with 2, B
);
1899 ---------------------
1900 -- Build_Ref_Array --
1901 ---------------------
1903 procedure Build_Ref_Array
(E
: PE_Ptr
; RA
: out Ref_Array
) is
1905 procedure Record_PE
(E
: PE_Ptr
);
1906 -- Record given pattern element if not already recorded in RA,
1907 -- and also record any referenced pattern elements recursively.
1909 procedure Record_PE
(E
: PE_Ptr
) is
1911 PutD
(" Record_PE called with PE_Ptr = " & Image
(E
));
1913 if E
= EOP
or else RA
(E
.Index
) /= null then
1914 Put_LineD
(", nothing to do");
1918 Put_LineD
(", recording" & IndexT
'Image (E
.Index
));
1920 Record_PE
(E
.Pthen
);
1922 if E
.Pcode
in PC_Has_Alt
then
1928 -- Start of processing for Build_Ref_Array
1932 Put_LineD
("Entering Build_Ref_Array");
1935 end Build_Ref_Array
;
1941 function C_To_PE
(C
: PChar
) return PE_Ptr
is
1943 return new PE
'(PC_Char, 1, EOP, C);
1950 function Cancel return Pattern is
1952 return (AFC with 0, new PE'(PC_Cancel
, 1, EOP
));
1959 -- Concat needs to traverse the left operand performing the following
1962 -- a) Any successor pointers (Pthen fields) that are set to EOP are
1963 -- reset to point to the second operand.
1965 -- b) Any PC_Arbno_Y node has its stack count field incremented
1966 -- by the parameter Incr provided for this purpose.
1968 -- d) Num fields of all pattern elements in the left operand are
1969 -- adjusted to include the elements of the right operand.
1971 -- Note: we do not use Set_Successor in the processing for Concat, since
1972 -- there is no point in doing two traversals, we may as well do everything
1973 -- at the same time.
1975 function Concat
(L
, R
: PE_Ptr
; Incr
: Natural) return PE_Ptr
is
1985 Refs
: Ref_Array
(1 .. L
.Index
);
1986 -- We build a reference array for L whose N'th element points to
1987 -- the pattern element of L whose original Index value is N.
1992 Build_Ref_Array
(L
, Refs
);
1994 for J
in Refs
'Range loop
1997 P
.Index
:= P
.Index
+ R
.Index
;
1999 if P
.Pcode
= PC_Arbno_Y
then
2000 P
.Nat
:= P
.Nat
+ Incr
;
2003 if P
.Pthen
= EOP
then
2007 if P
.Pcode
in PC_Has_Alt
and then P
.Alt
= EOP
then
2021 function Copy
(P
: PE_Ptr
) return PE_Ptr
is
2024 Uninitialized_Pattern
;
2028 Refs
: Ref_Array
(1 .. P
.Index
);
2029 -- References to elements in P, indexed by Index field
2031 Copy
: Ref_Array
(1 .. P
.Index
);
2032 -- Holds copies of elements of P, indexed by Index field.
2037 Build_Ref_Array
(P
, Refs
);
2039 -- Now copy all nodes
2041 for J
in Refs
'Range loop
2042 Copy
(J
) := new PE
'(Refs (J).all);
2045 -- Adjust all internal references
2047 for J in Copy'Range loop
2050 -- Adjust successor pointer to point to copy
2052 if E.Pthen /= EOP then
2053 E.Pthen := Copy (E.Pthen.Index);
2056 -- Adjust Alt pointer if there is one to point to copy
2058 if E.Pcode in PC_Has_Alt and then E.Alt /= EOP then
2059 E.Alt := Copy (E.Alt.Index);
2062 -- Copy referenced string
2064 if E.Pcode = PC_String then
2065 E.Str := new String'(E
.Str
.all);
2069 return Copy
(P
.Index
);
2078 procedure Dump
(P
: Pattern
) is
2080 subtype Count
is Ada
.Text_IO
.Count
;
2082 -- Used to keep track of column in dump output
2084 Refs
: Ref_Array
(1 .. P
.P
.Index
);
2085 -- We build a reference array whose N'th element points to the
2086 -- pattern element whose Index value is N.
2088 Cols
: Natural := 2;
2089 -- Number of columns used for pattern numbers, minimum is 2
2093 procedure Write_Node_Id
(E
: PE_Ptr
);
2094 -- Writes out a string identifying the given pattern element.
2096 procedure Write_Node_Id
(E
: PE_Ptr
) is
2101 for J
in 4 .. Cols
loop
2107 Str
: String (1 .. Cols
);
2108 N
: Natural := Natural (E
.Index
);
2113 for J
in reverse Str
'Range loop
2114 Str
(J
) := Character'Val (48 + N
mod 10);
2125 Put
("Pattern Dump Output (pattern at " &
2127 ", S = " & Natural'Image (P
.Stk
) & ')');
2132 while Col
< Scol
loop
2138 -- If uninitialized pattern, dump line and we are done
2141 Put_Line
("Uninitialized pattern value");
2145 -- If null pattern, just dump it and we are all done
2148 Put_Line
("EOP (null pattern)");
2152 Build_Ref_Array
(P
.P
, Refs
);
2154 -- Set number of columns required for node numbers
2156 while 10 ** Cols
- 1 < Integer (P
.P
.Index
) loop
2160 -- Now dump the nodes in reverse sequence. We output them in reverse
2161 -- sequence since this corresponds to the natural order used to
2162 -- construct the patterns.
2164 for J
in reverse Refs
'Range loop
2167 Set_Col
(Count
(Cols
) + 4);
2170 Put
(Pattern_Code
'Image (E
.Pcode
));
2172 Set_Col
(21 + Count
(Cols
) + Address_Image_Length
);
2173 Write_Node_Id
(E
.Pthen
);
2174 Set_Col
(24 + 2 * Count
(Cols
) + Address_Image_Length
);
2182 Write_Node_Id
(E
.Alt
);
2185 Put
(Str_PP
(E
.PP
));
2187 when PC_Pred_Func
=>
2188 Put
(Str_BF
(E
.BF
));
2190 when PC_Assign_Imm |
2199 Put
(Str_VP
(E
.VP
));
2203 Put
(Str_FP
(E
.FP
));
2206 Put
(Image
(E
.Str
.all));
2209 Put
(Image
(E
.Str2
));
2212 Put
(Image
(E
.Str3
));
2215 Put
(Image
(E
.Str4
));
2218 Put
(Image
(E
.Str5
));
2221 Put
(Image
(E
.Str6
));
2224 Put
(Str_NP
(E
.Var
));
2233 Put
(''' & E
.Char
& ''');
2241 Put
('"' & To_Sequence
(E
.CS
) & '"');
2256 Put
(Str_NF
(E
.NF
));
2263 Put
(Str_NP
(E
.NP
));
2272 Put
(Str_VF
(E
.VF
));
2274 when others => null;
2288 function Fail
return Pattern
is
2290 return (AFC
with 0, new PE
'(PC_Fail, 1, EOP));
2299 function Fence return Pattern is
2301 return (AFC with 1, new PE'(PC_Fence
, 1, EOP
));
2306 -- +---+ +---+ +---+
2307 -- | E |---->| P |---->| X |---->
2308 -- +---+ +---+ +---+
2310 -- The node numbering of the constituent pattern P is not affected.
2311 -- Where N is the number of nodes in P, the X node is numbered N + 1,
2312 -- and the E node is N + 2.
2314 function Fence
(P
: Pattern
) return Pattern
is
2315 Pat
: constant PE_Ptr
:= Copy
(P
.P
);
2316 E
: constant PE_Ptr
:= new PE
'(PC_R_Enter, 0, EOP);
2317 X : constant PE_Ptr := new PE'(PC_Fence_X
, 0, EOP
);
2320 return (AFC
with P
.Stk
+ 1, Bracket
(E
, Pat
, X
));
2327 procedure Finalize
(Object
: in out Pattern
) is
2329 procedure Free
is new Unchecked_Deallocation
(PE
, PE_Ptr
);
2330 procedure Free
is new Unchecked_Deallocation
(String, String_Ptr
);
2333 -- Nothing to do if already freed
2335 if Object
.P
= null then
2338 -- Otherwise we must free all elements
2342 Refs
: Ref_Array
(1 .. Object
.P
.Index
);
2343 -- References to elements in pattern to be finalized
2346 Build_Ref_Array
(Object
.P
, Refs
);
2348 for J
in Refs
'Range loop
2349 if Refs
(J
).Pcode
= PC_String
then
2350 Free
(Refs
(J
).Str
);
2365 function Image
(P
: PE_Ptr
) return String is
2367 return Image
(To_Address
(P
));
2370 function Image
(P
: Pattern
) return String is
2372 return S
(Image
(P
));
2375 function Image
(P
: Pattern
) return VString
is
2377 Kill_Ampersand
: Boolean := False;
2378 -- Set True to delete next & to be output to Result
2380 Result
: VString
:= Nul
;
2381 -- The result is accumulated here, using Append
2383 Refs
: Ref_Array
(1 .. P
.P
.Index
);
2384 -- We build a reference array whose N'th element points to the
2385 -- pattern element whose Index value is N.
2387 procedure Delete_Ampersand
;
2388 -- Deletes the ampersand at the end of Result
2390 procedure Image_Seq
(E
: PE_Ptr
; Succ
: PE_Ptr
; Paren
: Boolean);
2391 -- E refers to a pattern structure whose successor is given by Succ.
2392 -- This procedure appends to Result a representation of this pattern.
2393 -- The Paren parameter indicates whether parentheses are required if
2394 -- the output is more than one element.
2396 procedure Image_One
(E
: in out PE_Ptr
);
2397 -- E refers to a pattern structure. This procedure appends to Result
2398 -- a representation of the single simple or compound pattern structure
2399 -- at the start of E and updates E to point to its successor.
2401 ----------------------
2402 -- Delete_Ampersand --
2403 ----------------------
2405 procedure Delete_Ampersand
is
2406 L
: Natural := Length
(Result
);
2410 Delete
(Result
, L
- 1, L
);
2412 end Delete_Ampersand
;
2418 procedure Image_One
(E
: in out PE_Ptr
) is
2420 ER
: PE_Ptr
:= E
.Pthen
;
2421 -- Successor set as result in E unless reset
2427 Append
(Result
, "Cancel");
2429 when PC_Alt
=> Alt
: declare
2431 Elmts_In_L
: constant IndexT
:= E
.Pthen
.Index
- E
.Alt
.Index
;
2432 -- Number of elements in left pattern of alternation.
2434 Lowest_In_L
: constant IndexT
:= E
.Index
- Elmts_In_L
;
2435 -- Number of lowest index in elements of left pattern
2440 -- The successor of the alternation node must have a lower
2441 -- index than any node that is in the left pattern or a
2442 -- higher index than the alternation node itself.
2445 and then ER
.Index
>= Lowest_In_L
2446 and then ER
.Index
< E
.Index
2451 Append
(Result
, '(');
2455 Image_Seq
(E1
.Pthen
, ER
, False);
2456 Append
(Result
, " or ");
2458 exit when E1
.Pcode
/= PC_Alt
;
2461 Image_Seq
(E1
, ER
, False);
2462 Append
(Result
, ')');
2466 Append
(Result
, "Any (" & Image
(To_Sequence
(E
.CS
)) & ')');
2469 Append
(Result
, "Any (" & Str_VF
(E
.VF
) & ')');
2472 Append
(Result
, "Any (" & Str_VP
(E
.VP
) & ')');
2475 Append
(Result
, "Arb");
2478 Append
(Result
, "Arbno (");
2479 Image_Seq
(E
.Alt
, E
, False);
2480 Append
(Result
, ')');
2483 Append
(Result
, "Arbno (");
2484 Image_Seq
(E
.Alt
.Pthen
, Refs
(E
.Index
- 2), False);
2485 Append
(Result
, ')');
2487 when PC_Assign_Imm
=>
2489 Append
(Result
, "* " & Str_VP
(Refs
(E
.Index
- 1).VP
));
2491 when PC_Assign_OnM
=>
2493 Append
(Result
, "** " & Str_VP
(Refs
(E
.Index
- 1).VP
));
2496 Append
(Result
, "Any ('" & E
.Char
& "')");
2499 Append
(Result
, "Bal");
2502 Append
(Result
, "Break ('" & E
.Char
& "')");
2505 Append
(Result
, "Break (" & Image
(To_Sequence
(E
.CS
)) & ')');
2508 Append
(Result
, "Break (" & Str_VF
(E
.VF
) & ')');
2511 Append
(Result
, "Break (" & Str_VP
(E
.VP
) & ')');
2513 when PC_BreakX_CH
=>
2514 Append
(Result
, "BreakX ('" & E
.Char
& "')");
2517 when PC_BreakX_CS
=>
2518 Append
(Result
, "BreakX (" & Image
(To_Sequence
(E
.CS
)) & ')');
2521 when PC_BreakX_VF
=>
2522 Append
(Result
, "BreakX (" & Str_VF
(E
.VF
) & ')');
2525 when PC_BreakX_VP
=>
2526 Append
(Result
, "BreakX (" & Str_VP
(E
.VP
) & ')');
2530 Append
(Result
, ''' & E
.Char
& ''');
2533 Append
(Result
, "Fail");
2536 Append
(Result
, "Fence");
2539 Append
(Result
, "Fence (");
2540 Image_Seq
(E
.Pthen
, Refs
(E
.Index
- 1), False);
2541 Append
(Result
, ")");
2542 ER
:= Refs
(E
.Index
- 1).Pthen
;
2545 Append
(Result
, "Len (" & E
.Nat
& ')');
2548 Append
(Result
, "Len (" & Str_NF
(E
.NF
) & ')');
2551 Append
(Result
, "Len (" & Str_NP
(E
.NP
) & ')');
2553 when PC_NotAny_CH
=>
2554 Append
(Result
, "NotAny ('" & E
.Char
& "')");
2556 when PC_NotAny_CS
=>
2557 Append
(Result
, "NotAny (" & Image
(To_Sequence
(E
.CS
)) & ')');
2559 when PC_NotAny_VF
=>
2560 Append
(Result
, "NotAny (" & Str_VF
(E
.VF
) & ')');
2562 when PC_NotAny_VP
=>
2563 Append
(Result
, "NotAny (" & Str_VP
(E
.VP
) & ')');
2566 Append
(Result
, "NSpan ('" & E
.Char
& "')");
2569 Append
(Result
, "NSpan (" & Image
(To_Sequence
(E
.CS
)) & ')');
2572 Append
(Result
, "NSpan (" & Str_VF
(E
.VF
) & ')');
2575 Append
(Result
, "NSpan (" & Str_VP
(E
.VP
) & ')');
2578 Append
(Result
, """""");
2581 Append
(Result
, "Pos (" & E
.Nat
& ')');
2584 Append
(Result
, "Pos (" & Str_NF
(E
.NF
) & ')');
2587 Append
(Result
, "Pos (" & Str_NP
(E
.NP
) & ')');
2590 Kill_Ampersand
:= True;
2593 Append
(Result
, "Rest");
2596 Append
(Result
, "(+ " & Str_PP
(E
.PP
) & ')');
2598 when PC_Pred_Func
=>
2599 Append
(Result
, "(+ " & Str_BF
(E
.BF
) & ')');
2602 Append
(Result
, "RPos (" & E
.Nat
& ')');
2605 Append
(Result
, "RPos (" & Str_NF
(E
.NF
) & ')');
2608 Append
(Result
, "RPos (" & Str_NP
(E
.NP
) & ')');
2611 Append
(Result
, "RTab (" & E
.Nat
& ')');
2614 Append
(Result
, "RTab (" & Str_NF
(E
.NF
) & ')');
2617 Append
(Result
, "RTab (" & Str_NP
(E
.NP
) & ')');
2620 Append
(Result
, "Setcur (" & Str_NP
(E
.Var
) & ')');
2623 Append
(Result
, "Span ('" & E
.Char
& "')");
2626 Append
(Result
, "Span (" & Image
(To_Sequence
(E
.CS
)) & ')');
2629 Append
(Result
, "Span (" & Str_VF
(E
.VF
) & ')');
2632 Append
(Result
, "Span (" & Str_VP
(E
.VP
) & ')');
2635 Append
(Result
, Image
(E
.Str
.all));
2638 Append
(Result
, Image
(E
.Str2
));
2641 Append
(Result
, Image
(E
.Str3
));
2644 Append
(Result
, Image
(E
.Str4
));
2647 Append
(Result
, Image
(E
.Str5
));
2650 Append
(Result
, Image
(E
.Str6
));
2652 when PC_String_VF
=>
2653 Append
(Result
, "(+" & Str_VF
(E
.VF
) & ')');
2655 when PC_String_VP
=>
2656 Append
(Result
, "(+" & Str_VP
(E
.VP
) & ')');
2659 Append
(Result
, "Succeed");
2662 Append
(Result
, "Tab (" & E
.Nat
& ')');
2665 Append
(Result
, "Tab (" & Str_NF
(E
.NF
) & ')');
2668 Append
(Result
, "Tab (" & Str_NP
(E
.NP
) & ')');
2670 when PC_Write_Imm
=>
2671 Append
(Result
, '(');
2672 Image_Seq
(E
, Refs
(E
.Index
- 1), True);
2673 Append
(Result
, " * " & Str_FP
(Refs
(E
.Index
- 1).FP
));
2674 ER
:= Refs
(E
.Index
- 1).Pthen
;
2676 when PC_Write_OnM
=>
2677 Append
(Result
, '(');
2678 Image_Seq
(E
.Pthen
, Refs
(E
.Index
- 1), True);
2679 Append
(Result
, " ** " & Str_FP
(Refs
(E
.Index
- 1).FP
));
2680 ER
:= Refs
(E
.Index
- 1).Pthen
;
2682 -- Other pattern codes should not appear as leading elements
2693 Append
(Result
, "???");
2704 procedure Image_Seq
(E
: PE_Ptr
; Succ
: PE_Ptr
; Paren
: Boolean) is
2706 Mult
: Boolean := False;
2707 Indx
: Natural := Length
(Result
);
2710 -- The image of EOP is "" (the null string)
2713 Append
(Result
, """""");
2715 -- Else generate appropriate concatenation sequence
2720 exit when E1
= Succ
;
2724 if Kill_Ampersand
then
2725 Kill_Ampersand
:= False;
2727 Append
(Result
, " & ");
2732 if Mult
and Paren
then
2733 Insert
(Result
, Indx
+ 1, "(");
2734 Append
(Result
, ")");
2738 -- Start of processing for Image
2741 Build_Ref_Array
(P
.P
, Refs
);
2742 Image_Seq
(P
.P
, EOP
, False);
2750 function Is_In
(C
: Character; Str
: String) return Boolean is
2752 for J
in Str
'Range loop
2765 function Len
(Count
: Natural) return Pattern
is
2767 -- Note, the following is not just an optimization, it is needed
2768 -- to ensure that Arbno (Len (0)) does not generate an infinite
2769 -- matching loop (since PC_Len_Nat is OK_For_Simple_Arbno).
2772 return (AFC
with 0, new PE
'(PC_Null, 1, EOP));
2775 return (AFC with 0, new PE'(PC_Len_Nat
, 1, EOP
, Count
));
2779 function Len
(Count
: Natural_Func
) return Pattern
is
2781 return (AFC
with 0, new PE
'(PC_Len_NF, 1, EOP, Count));
2784 function Len (Count : access Natural) return Pattern is
2786 return (AFC with 0, new PE'(PC_Len_NP
, 1, EOP
, Natural_Ptr
(Count
)));
2793 procedure Logic_Error
is
2796 (Program_Error
'Identity,
2797 "Internal logic error in GNAT.Spitbol.Patterns");
2809 Start
, Stop
: Natural;
2813 XMatchD
(Get_String
(Subject
).all, Pat
.P
, Pat
.Stk
, Start
, Stop
);
2815 XMatch
(Get_String
(Subject
).all, Pat
.P
, Pat
.Stk
, Start
, Stop
);
2826 Start
, Stop
: Natural;
2827 subtype String1
is String (1 .. Subject
'Length);
2831 XMatchD
(String1
(Subject
), Pat
.P
, Pat
.Stk
, Start
, Stop
);
2833 XMatch
(String1
(Subject
), Pat
.P
, Pat
.Stk
, Start
, Stop
);
2840 (Subject
: VString_Var
;
2845 Start
, Stop
: Natural;
2849 XMatchD
(Get_String
(Subject
).all, Pat
.P
, Pat
.Stk
, Start
, Stop
);
2851 XMatch
(Get_String
(Subject
).all, Pat
.P
, Pat
.Stk
, Start
, Stop
);
2858 (Subject
'Unrestricted_Access.all,
2859 Start
, Stop
, Get_String
(Replace
).all);
2865 (Subject
: VString_Var
;
2870 Start
, Stop
: Natural;
2874 XMatchD
(Get_String
(Subject
).all, Pat
.P
, Pat
.Stk
, Start
, Stop
);
2876 XMatch
(Get_String
(Subject
).all, Pat
.P
, Pat
.Stk
, Start
, Stop
);
2883 (Subject
'Unrestricted_Access.all, Start
, Stop
, Replace
);
2892 Start
, Stop
: Natural;
2896 XMatchD
(Get_String
(Subject
).all, Pat
.P
, Pat
.Stk
, Start
, Stop
);
2898 XMatch
(Get_String
(Subject
).all, Pat
.P
, Pat
.Stk
, Start
, Stop
);
2907 Start
, Stop
: Natural;
2908 subtype String1
is String (1 .. Subject
'Length);
2911 XMatchD
(String1
(Subject
), Pat
.P
, Pat
.Stk
, Start
, Stop
);
2913 XMatch
(String1
(Subject
), Pat
.P
, Pat
.Stk
, Start
, Stop
);
2918 (Subject
: in out VString
;
2922 Start
, Stop
: Natural;
2926 XMatchD
(Get_String
(Subject
).all, Pat
.P
, Pat
.Stk
, Start
, Stop
);
2928 XMatch
(Get_String
(Subject
).all, Pat
.P
, Pat
.Stk
, Start
, Stop
);
2932 Replace_Slice
(Subject
, Start
, Stop
, Get_String
(Replace
).all);
2937 (Subject
: in out VString
;
2941 Start
, Stop
: Natural;
2945 XMatchD
(Get_String
(Subject
).all, Pat
.P
, Pat
.Stk
, Start
, Stop
);
2947 XMatch
(Get_String
(Subject
).all, Pat
.P
, Pat
.Stk
, Start
, Stop
);
2951 Replace_Slice
(Subject
, Start
, Stop
, Replace
);
2960 Pat_Len
: constant Natural := Pat
'Length;
2961 Sub_Len
: constant Natural := Length
(Subject
);
2962 Sub_Str
: constant String_Access
:= Get_String
(Subject
);
2965 if Anchored_Mode
then
2966 if Pat_Len
> Sub_Len
then
2969 return Pat
= Sub_Str
.all (1 .. Pat_Len
);
2973 for J
in 1 .. Sub_Len
- Pat_Len
+ 1 loop
2974 if Pat
= Sub_Str
.all (J
.. J
+ (Pat_Len
- 1)) then
2988 Pat_Len
: constant Natural := Pat
'Length;
2989 Sub_Len
: constant Natural := Subject
'Length;
2990 SFirst
: constant Natural := Subject
'First;
2993 if Anchored_Mode
then
2994 if Pat_Len
> Sub_Len
then
2997 return Pat
= Subject
(SFirst
.. SFirst
+ Pat_Len
- 1);
3001 for J
in SFirst
.. SFirst
+ Sub_Len
- Pat_Len
loop
3002 if Pat
= Subject
(J
.. J
+ (Pat_Len
- 1)) then
3012 (Subject
: VString_Var
;
3017 Start
, Stop
: Natural;
3021 XMatchD
(Get_String
(Subject
).all, S_To_PE
(Pat
), 0, Start
, Stop
);
3023 XMatch
(Get_String
(Subject
).all, S_To_PE
(Pat
), 0, Start
, Stop
);
3030 (Subject
'Unrestricted_Access.all,
3031 Start
, Stop
, Get_String
(Replace
).all);
3037 (Subject
: VString_Var
;
3042 Start
, Stop
: Natural;
3046 XMatchD
(Get_String
(Subject
).all, S_To_PE
(Pat
), 0, Start
, Stop
);
3048 XMatch
(Get_String
(Subject
).all, S_To_PE
(Pat
), 0, Start
, Stop
);
3055 (Subject
'Unrestricted_Access.all, Start
, Stop
, Replace
);
3064 Start
, Stop
: Natural;
3068 XMatchD
(Get_String
(Subject
).all, S_To_PE
(Pat
), 0, Start
, Stop
);
3070 XMatch
(Get_String
(Subject
).all, S_To_PE
(Pat
), 0, Start
, Stop
);
3078 Start
, Stop
: Natural;
3079 subtype String1
is String (1 .. Subject
'Length);
3083 XMatchD
(String1
(Subject
), S_To_PE
(Pat
), 0, Start
, Stop
);
3085 XMatch
(String1
(Subject
), S_To_PE
(Pat
), 0, Start
, Stop
);
3090 (Subject
: in out VString
;
3094 Start
, Stop
: Natural;
3098 XMatchD
(Get_String
(Subject
).all, S_To_PE
(Pat
), 0, Start
, Stop
);
3100 XMatch
(Get_String
(Subject
).all, S_To_PE
(Pat
), 0, Start
, Stop
);
3104 Replace_Slice
(Subject
, Start
, Stop
, Get_String
(Replace
).all);
3109 (Subject
: in out VString
;
3113 Start
, Stop
: Natural;
3117 XMatchD
(Get_String
(Subject
).all, S_To_PE
(Pat
), 0, Start
, Stop
);
3119 XMatch
(Get_String
(Subject
).all, S_To_PE
(Pat
), 0, Start
, Stop
);
3123 Replace_Slice
(Subject
, Start
, Stop
, Replace
);
3128 (Subject
: VString_Var
;
3130 Result
: Match_Result_Var
)
3133 Start
, Stop
: Natural;
3137 XMatchD
(Get_String
(Subject
).all, Pat
.P
, Pat
.Stk
, Start
, Stop
);
3139 XMatch
(Get_String
(Subject
).all, Pat
.P
, Pat
.Stk
, Start
, Stop
);
3143 Result
'Unrestricted_Access.all.Var
:= null;
3147 Result
'Unrestricted_Access.all.Var
:= Subject
'Unrestricted_Access;
3148 Result
'Unrestricted_Access.all.Start
:= Start
;
3149 Result
'Unrestricted_Access.all.Stop
:= Stop
;
3155 (Subject
: in out VString
;
3157 Result
: out Match_Result
)
3159 Start
, Stop
: Natural;
3163 XMatchD
(Get_String
(Subject
).all, Pat
.P
, Pat
.Stk
, Start
, Stop
);
3165 XMatch
(Get_String
(Subject
).all, Pat
.P
, Pat
.Stk
, Start
, Stop
);
3172 Result
.Var
:= Subject
'Unrestricted_Access;
3173 Result
.Start
:= Start
;
3174 Result
.Stop
:= Stop
;
3182 procedure New_LineD
is
3184 if Internal_Debug
then
3193 function NotAny
(Str
: String) return Pattern
is
3195 return (AFC
with 0, new PE
'(PC_NotAny_CS, 1, EOP, To_Set (Str)));
3198 function NotAny (Str : VString) return Pattern is
3200 return NotAny (S (Str));
3203 function NotAny (Str : Character) return Pattern is
3205 return (AFC with 0, new PE'(PC_NotAny_CH
, 1, EOP
, Str
));
3208 function NotAny
(Str
: Character_Set
) return Pattern
is
3210 return (AFC
with 0, new PE
'(PC_NotAny_CS, 1, EOP, Str));
3213 function NotAny (Str : access VString) return Pattern is
3215 return (AFC with 0, new PE'(PC_NotAny_VP
, 1, EOP
, VString_Ptr
(Str
)));
3218 function NotAny
(Str
: VString_Func
) return Pattern
is
3220 return (AFC
with 0, new PE
'(PC_NotAny_VF, 1, EOP, Str));
3227 function NSpan (Str : String) return Pattern is
3229 return (AFC with 0, new PE'(PC_NSpan_CS
, 1, EOP
, To_Set
(Str
)));
3232 function NSpan
(Str
: VString
) return Pattern
is
3234 return NSpan
(S
(Str
));
3237 function NSpan
(Str
: Character) return Pattern
is
3239 return (AFC
with 0, new PE
'(PC_NSpan_CH, 1, EOP, Str));
3242 function NSpan (Str : Character_Set) return Pattern is
3244 return (AFC with 0, new PE'(PC_NSpan_CS
, 1, EOP
, Str
));
3247 function NSpan
(Str
: access VString
) return Pattern
is
3249 return (AFC
with 0, new PE
'(PC_NSpan_VP, 1, EOP, VString_Ptr (Str)));
3252 function NSpan (Str : VString_Func) return Pattern is
3254 return (AFC with 0, new PE'(PC_NSpan_VF
, 1, EOP
, Str
));
3261 function Pos
(Count
: Natural) return Pattern
is
3263 return (AFC
with 0, new PE
'(PC_Pos_Nat, 1, EOP, Count));
3266 function Pos (Count : Natural_Func) return Pattern is
3268 return (AFC with 0, new PE'(PC_Pos_NF
, 1, EOP
, Count
));
3271 function Pos
(Count
: access Natural) return Pattern
is
3273 return (AFC
with 0, new PE
'(PC_Pos_NP, 1, EOP, Natural_Ptr (Count)));
3280 procedure PutD (Str : String) is
3282 if Internal_Debug then
3291 procedure Put_LineD (Str : String) is
3293 if Internal_Debug then
3303 (Result : in out Match_Result;
3307 if Result.Var /= null then
3312 Get_String (Replace).all);
3321 function Rest return Pattern is
3323 return (AFC with 0, new PE'(PC_Rest
, 1, EOP
));
3330 function Rpos
(Count
: Natural) return Pattern
is
3332 return (AFC
with 0, new PE
'(PC_RPos_Nat, 1, EOP, Count));
3335 function Rpos (Count : Natural_Func) return Pattern is
3337 return (AFC with 0, new PE'(PC_RPos_NF
, 1, EOP
, Count
));
3340 function Rpos
(Count
: access Natural) return Pattern
is
3342 return (AFC
with 0, new PE
'(PC_RPos_NP, 1, EOP, Natural_Ptr (Count)));
3349 function Rtab (Count : Natural) return Pattern is
3351 return (AFC with 0, new PE'(PC_RTab_Nat
, 1, EOP
, Count
));
3354 function Rtab
(Count
: Natural_Func
) return Pattern
is
3356 return (AFC
with 0, new PE
'(PC_RTab_NF, 1, EOP, Count));
3359 function Rtab (Count : access Natural) return Pattern is
3361 return (AFC with 0, new PE'(PC_RTab_NP
, 1, EOP
, Natural_Ptr
(Count
)));
3368 function S_To_PE
(Str
: PString
) return PE_Ptr
is
3369 Len
: constant Natural := Str
'Length;
3374 return new PE
'(PC_Null, 1, EOP);
3377 return new PE'(PC_Char
, 1, EOP
, Str
(1));
3380 return new PE
'(PC_String_2, 1, EOP, Str);
3383 return new PE'(PC_String_3
, 1, EOP
, Str
);
3386 return new PE
'(PC_String_4, 1, EOP, Str);
3389 return new PE'(PC_String_5
, 1, EOP
, Str
);
3392 return new PE
'(PC_String_6, 1, EOP, Str);
3395 return new PE'(PC_String
, 1, EOP
, new String'(Str));
3404 -- Note: this procedure is not used by the normal concatenation circuit,
3405 -- since other fixups are required on the left operand in this case, and
3406 -- they might as well be done all together.
3408 procedure Set_Successor (Pat : PE_Ptr; Succ : PE_Ptr) is
3411 Uninitialized_Pattern;
3413 elsif Pat = EOP then
3418 Refs : Ref_Array (1 .. Pat.Index);
3419 -- We build a reference array for L whose N'th element points to
3420 -- the pattern element of L whose original Index value is N.
3425 Build_Ref_Array (Pat, Refs);
3427 for J in Refs'Range loop
3430 if P.Pthen = EOP then
3434 if P.Pcode in PC_Has_Alt and then P.Alt = EOP then
3446 function Setcur (Var : access Natural) return Pattern is
3448 return (AFC with 0, new PE'(PC_Setcur
, 1, EOP
, Natural_Ptr
(Var
)));
3455 function Span
(Str
: String) return Pattern
is
3457 return (AFC
with 0, new PE
'(PC_Span_CS, 1, EOP, To_Set (Str)));
3460 function Span (Str : VString) return Pattern is
3462 return Span (S (Str));
3465 function Span (Str : Character) return Pattern is
3467 return (AFC with 0, new PE'(PC_Span_CH
, 1, EOP
, Str
));
3470 function Span
(Str
: Character_Set
) return Pattern
is
3472 return (AFC
with 0, new PE
'(PC_Span_CS, 1, EOP, Str));
3475 function Span (Str : access VString) return Pattern is
3477 return (AFC with 0, new PE'(PC_Span_VP
, 1, EOP
, VString_Ptr
(Str
)));
3480 function Span
(Str
: VString_Func
) return Pattern
is
3482 return (AFC
with 0, new PE
'(PC_Span_VF, 1, EOP, Str));
3489 function Str_BF (A : Boolean_Func) return String is
3490 function To_A is new Unchecked_Conversion (Boolean_Func, Address);
3493 return "BF(" & Image (To_A (A)) & ')';
3500 function Str_FP (A : File_Ptr) return String is
3502 return "FP(" & Image (A.all'Address) & ')';
3509 function Str_NF (A : Natural_Func) return String is
3510 function To_A is new Unchecked_Conversion (Natural_Func, Address);
3513 return "NF(" & Image (To_A (A)) & ')';
3520 function Str_NP (A : Natural_Ptr) return String is
3522 return "NP(" & Image (A.all'Address) & ')';
3529 function Str_PP (A : Pattern_Ptr) return String is
3531 return "PP(" & Image (A.all'Address) & ')';
3538 function Str_VF (A : VString_Func) return String is
3539 function To_A is new Unchecked_Conversion (VString_Func, Address);
3542 return "VF(" & Image (To_A (A)) & ')';
3549 function Str_VP (A : VString_Ptr) return String is
3551 return "VP(" & Image (A.all'Address) & ')';
3558 function Succeed return Pattern is
3560 return (AFC with 1, new PE'(PC_Succeed
, 1, EOP
));
3567 function Tab
(Count
: Natural) return Pattern
is
3569 return (AFC
with 0, new PE
'(PC_Tab_Nat, 1, EOP, Count));
3572 function Tab (Count : Natural_Func) return Pattern is
3574 return (AFC with 0, new PE'(PC_Tab_NF
, 1, EOP
, Count
));
3577 function Tab
(Count
: access Natural) return Pattern
is
3579 return (AFC
with 0, new PE
'(PC_Tab_NP, 1, EOP, Natural_Ptr (Count)));
3582 ---------------------------
3583 -- Uninitialized_Pattern --
3584 ---------------------------
3586 procedure Uninitialized_Pattern is
3589 (Program_Error'Identity,
3590 "uninitialized value of type GNAT.Spitbol.Patterns.Pattern");
3591 end Uninitialized_Pattern;
3601 Start : out Natural;
3605 -- Pointer to current pattern node. Initialized from Pat_P, and then
3606 -- updated as the match proceeds through its constituent elements.
3608 Length : constant Natural := Subject'Length;
3609 -- Length of string (= Subject'Last, since Subject'First is always 1)
3611 Cursor : Integer := 0;
3612 -- If the value is non-negative, then this value is the index showing
3613 -- the current position of the match in the subject string. The next
3614 -- character to be matched is at Subject (Cursor + 1). Note that since
3615 -- our view of the subject string in XMatch always has a lower bound
3616 -- of one, regardless of original bounds, that this definition exactly
3617 -- corresponds to the cursor value as referenced by functions like Pos.
3619 -- If the value is negative, then this is a saved stack pointer,
3620 -- typically a base pointer of an inner or outer region. Cursor
3621 -- temporarily holds such a value when it is popped from the stack
3622 -- by Fail. In all cases, Cursor is reset to a proper non-negative
3623 -- cursor value before the match proceeds (e.g. by propagating the
3624 -- failure and popping a "real" cursor value from the stack.
3626 PE_Unanchored : aliased PE := (PC_Unanchored, 0, Pat_P);
3627 -- Dummy pattern element used in the unanchored case.
3630 -- The pattern matching failure stack for this call to Match
3632 Stack_Ptr : Stack_Range;
3633 -- Current stack pointer. This points to the top element of the stack
3634 -- that is currently in use. At the outer level this is the special
3635 -- entry placed on the stack according to the anchor mode.
3637 Stack_Init : constant Stack_Range := Stack'First + 1;
3638 -- This is the initial value of the Stack_Ptr and Stack_Base. The
3639 -- initial (Stack'First) element of the stack is not used so that
3640 -- when we pop the last element off, Stack_Ptr is still in range.
3642 Stack_Base : Stack_Range;
3643 -- This value is the stack base value, i.e. the stack pointer for the
3644 -- first history stack entry in the current stack region. See separate
3645 -- section on handling of recursive pattern matches.
3647 Assign_OnM : Boolean := False;
3648 -- Set True if assign-on-match or write-on-match operations may be
3649 -- present in the history stack, which must then be scanned on a
3650 -- successful match.
3652 procedure Pop_Region;
3653 pragma Inline (Pop_Region);
3654 -- Used at the end of processing of an inner region. if the inner
3655 -- region left no stack entries, then all trace of it is removed.
3656 -- Otherwise a PC_Restore_Region entry is pushed to ensure proper
3657 -- handling of alternatives in the inner region.
3659 procedure Push (Node : PE_Ptr);
3660 pragma Inline (Push);
3661 -- Make entry in pattern matching stack with current cursor valeu
3663 procedure Push_Region;
3664 pragma Inline (Push_Region);
3665 -- This procedure makes a new region on the history stack. The
3666 -- caller first establishes the special entry on the stack, but
3667 -- does not push the stack pointer. Then this call stacks a
3668 -- PC_Remove_Region node, on top of this entry, using the cursor
3669 -- field of the PC_Remove_Region entry to save the outer level
3670 -- stack base value, and resets the stack base to point to this
3671 -- PC_Remove_Region node.
3677 procedure Pop_Region is
3679 -- If nothing was pushed in the inner region, we can just get
3680 -- rid of it entirely, leaving no traces that it was ever there
3682 if Stack_Ptr = Stack_Base then
3683 Stack_Ptr := Stack_Base - 2;
3684 Stack_Base := Stack (Stack_Ptr + 2).Cursor;
3686 -- If stuff was pushed in the inner region, then we have to
3687 -- push a PC_R_Restore node so that we properly handle possible
3688 -- rematches within the region.
3691 Stack_Ptr := Stack_Ptr + 1;
3692 Stack (Stack_Ptr).Cursor := Stack_Base;
3693 Stack (Stack_Ptr).Node := CP_R_Restore'Access;
3694 Stack_Base := Stack (Stack_Base).Cursor;
3702 procedure Push (Node : PE_Ptr) is
3704 Stack_Ptr := Stack_Ptr + 1;
3705 Stack (Stack_Ptr).Cursor := Cursor;
3706 Stack (Stack_Ptr).Node := Node;
3713 procedure Push_Region is
3715 Stack_Ptr := Stack_Ptr + 2;
3716 Stack (Stack_Ptr).Cursor := Stack_Base;
3717 Stack (Stack_Ptr).Node := CP_R_Remove'Access;
3718 Stack_Base := Stack_Ptr;
3721 -- Start of processing for XMatch
3724 if Pat_P = null then
3725 Uninitialized_Pattern;
3728 -- Check we have enough stack for this pattern. This check deals with
3729 -- every possibility except a match of a recursive pattern, where we
3730 -- make a check at each recursion level.
3732 if Pat_S >= Stack_Size - 1 then
3733 raise Pattern_Stack_Overflow;
3736 -- In anchored mode, the bottom entry on the stack is an abort entry
3738 if Anchored_Mode then
3739 Stack (Stack_Init).Node := CP_Cancel'Access;
3740 Stack (Stack_Init).Cursor := 0;
3742 -- In unanchored more, the bottom entry on the stack references
3743 -- the special pattern element PE_Unanchored, whose Pthen field
3744 -- points to the initial pattern element. The cursor value in this
3745 -- entry is the number of anchor moves so far.
3748 Stack (Stack_Init).Node := PE_Unanchored'Unchecked_Access;
3749 Stack (Stack_Init).Cursor := 0;
3752 Stack_Ptr := Stack_Init;
3753 Stack_Base := Stack_Ptr;
3758 -----------------------------------------
3759 -- Main Pattern Matching State Control --
3760 -----------------------------------------
3762 -- This is a state machine which uses gotos to change state. The
3763 -- initial state is Match, to initiate the matching of the first
3764 -- element, so the goto Match above starts the match. In the
3765 -- following descriptions, we indicate the global values that
3766 -- are relevant for the state transition.
3768 -- Come here if entire match fails
3775 -- Come here if entire match succeeds
3777 -- Cursor current position in subject string
3780 Start := Stack (Stack_Init).Cursor + 1;
3783 -- Scan history stack for deferred assignments or writes
3786 for S in Stack_Init .. Stack_Ptr loop
3787 if Stack (S).Node = CP_Assign'Access then
3789 Inner_Base : constant Stack_Range :=
3790 Stack (S + 1).Cursor;
3791 Special_Entry : constant Stack_Range :=
3793 Node_OnM : constant PE_Ptr :=
3794 Stack (Special_Entry).Node;
3795 Start : constant Natural :=
3796 Stack (Special_Entry).Cursor + 1;
3797 Stop : constant Natural := Stack (S).Cursor;
3800 if Node_OnM.Pcode = PC_Assign_OnM then
3801 Set_String (Node_OnM.VP.all, Subject (Start .. Stop));
3803 elsif Node_OnM.Pcode = PC_Write_OnM then
3804 Put_Line (Node_OnM.FP.all, Subject (Start .. Stop));
3816 -- Come here if attempt to match current element fails
3818 -- Stack_Base current stack base
3819 -- Stack_Ptr current stack pointer
3822 Cursor := Stack (Stack_Ptr).Cursor;
3823 Node := Stack (Stack_Ptr).Node;
3824 Stack_Ptr := Stack_Ptr - 1;
3827 -- Come here if attempt to match current element succeeds
3829 -- Cursor current position in subject string
3830 -- Node pointer to node successfully matched
3831 -- Stack_Base current stack base
3832 -- Stack_Ptr current stack pointer
3837 -- Come here to match the next pattern element
3839 -- Cursor current position in subject string
3840 -- Node pointer to node to be matched
3841 -- Stack_Base current stack base
3842 -- Stack_Ptr current stack pointer
3846 --------------------------------------------------
3847 -- Main Pattern Match Element Matching Routines --
3848 --------------------------------------------------
3850 -- Here is the case statement that processes the current node. The
3851 -- processing for each element does one of five things:
3853 -- goto Succeed to move to the successor
3854 -- goto Match_Succeed if the entire match succeeds
3855 -- goto Match_Fail if the entire match fails
3856 -- goto Fail to signal failure of current match
3858 -- Processing is NOT allowed to fall through
3874 -- Any (one character case)
3878 and then Subject (Cursor + 1) = Node.Char
3880 Cursor := Cursor + 1;
3886 -- Any (character set case)
3890 and then Is_In (Subject (Cursor + 1), Node.CS)
3892 Cursor := Cursor + 1;
3898 -- Any (string function case)
3900 when PC_Any_VF => declare
3901 U : constant VString := Node.VF.all;
3902 Str : constant String_Access := Get_String (U);
3906 and then Is_In (Subject (Cursor + 1), Str.all)
3908 Cursor := Cursor + 1;
3915 -- Any (string pointer case)
3917 when PC_Any_VP => declare
3918 Str : constant String_Access := Get_String (Node.VP.all);
3922 and then Is_In (Subject (Cursor + 1), Str.all)
3924 Cursor := Cursor + 1;
3931 -- Arb (initial match)
3941 if Cursor < Length then
3942 Cursor := Cursor + 1;
3949 -- Arbno_S (simple Arbno initialize). This is the node that
3950 -- initiates the match of a simple Arbno structure.
3957 -- Arbno_X (Arbno initialize). This is the node that initiates
3958 -- the match of a complex Arbno structure.
3965 -- Arbno_Y (Arbno rematch). This is the node that is executed
3966 -- following successful matching of one instance of a complex
3969 when PC_Arbno_Y => declare
3970 Null_Match : Boolean := (Cursor = Stack (Stack_Base - 1).Cursor);
3975 -- If arbno extension matched null, then immediately fail
3981 -- Here we must do a stack check to make sure enough stack
3982 -- is left. This check will happen once for each instance of
3983 -- the Arbno pattern that is matched. The Nat field of a
3984 -- PC_Arbno pattern contains the maximum stack entries needed
3985 -- for the Arbno with one instance and the successor pattern
3987 if Stack_Ptr + Node.Nat >= Stack'Last then
3988 raise Pattern_Stack_Overflow;
3994 -- Assign. If this node is executed, it means the assign-on-match
3995 -- or write-on-match operation will not happen after all, so we
3996 -- is propagate the failure, removing the PC_Assign node.
4001 -- Assign immediate. This node performs the actual assignment.
4003 when PC_Assign_Imm =>
4006 Subject (Stack (Stack_Base - 1).Cursor + 1 .. Cursor));
4010 -- Assign on match. This node sets up for the eventual assignment
4012 when PC_Assign_OnM =>
4013 Stack (Stack_Base - 1).Node := Node;
4014 Push (CP_Assign'Access);
4022 if Cursor >= Length or else Subject (Cursor + 1) = ')' then
4025 elsif Subject (Cursor + 1) = '(' then
4027 Paren_Count : Natural := 1;
4031 Cursor := Cursor + 1;
4033 if Cursor >= Length then
4036 elsif Subject (Cursor + 1) = '(' then
4037 Paren_Count := Paren_Count + 1;
4039 elsif Subject (Cursor + 1) = ')' then
4040 Paren_Count := Paren_Count - 1;
4041 exit when Paren_Count = 0;
4047 Cursor := Cursor + 1;
4051 -- Break (one character case)
4054 while Cursor < Length loop
4055 if Subject (Cursor + 1) = Node.Char then
4058 Cursor := Cursor + 1;
4064 -- Break (character set case)
4067 while Cursor < Length loop
4068 if Is_In (Subject (Cursor + 1), Node.CS) then
4071 Cursor := Cursor + 1;
4077 -- Break (string function case)
4079 when PC_Break_VF => declare
4080 U : constant VString := Node.VF.all;
4081 Str : constant String_Access := Get_String (U);
4084 while Cursor < Length loop
4085 if Is_In (Subject (Cursor + 1), Str.all) then
4088 Cursor := Cursor + 1;
4095 -- Break (string pointer case)
4097 when PC_Break_VP => declare
4098 Str : String_Access := Get_String (Node.VP.all);
4101 while Cursor < Length loop
4102 if Is_In (Subject (Cursor + 1), Str.all) then
4105 Cursor := Cursor + 1;
4112 -- BreakX (one character case)
4114 when PC_BreakX_CH =>
4115 while Cursor < Length loop
4116 if Subject (Cursor + 1) = Node.Char then
4119 Cursor := Cursor + 1;
4125 -- BreakX (character set case)
4127 when PC_BreakX_CS =>
4128 while Cursor < Length loop
4129 if Is_In (Subject (Cursor + 1), Node.CS) then
4132 Cursor := Cursor + 1;
4138 -- BreakX (string function case)
4140 when PC_BreakX_VF => declare
4141 U : constant VString := Node.VF.all;
4142 Str : constant String_Access := Get_String (U);
4145 while Cursor < Length loop
4146 if Is_In (Subject (Cursor + 1), Str.all) then
4149 Cursor := Cursor + 1;
4156 -- BreakX (string pointer case)
4158 when PC_BreakX_VP => declare
4159 Str : String_Access := Get_String (Node.VP.all);
4162 while Cursor < Length loop
4163 if Is_In (Subject (Cursor + 1), Str.all) then
4166 Cursor := Cursor + 1;
4173 -- BreakX_X (BreakX extension). See section on "Compound Pattern
4174 -- Structures". This node is the alternative that is stacked to
4175 -- skip past the break character and extend the break.
4178 Cursor := Cursor + 1;
4181 -- Character (one character string)
4185 and then Subject (Cursor + 1) = Node.Char
4187 Cursor := Cursor + 1;
4196 if Stack_Base = Stack_Init then
4199 -- End of recursive inner match. See separate section on
4200 -- handing of recursive pattern matches for details.
4203 Node := Stack (Stack_Base - 1).Node;
4213 -- Fence (built in pattern)
4216 Push (CP_Cancel'Access);
4219 -- Fence function node X. This is the node that gets control
4220 -- after a successful match of the fenced pattern.
4223 Stack_Ptr := Stack_Ptr + 1;
4224 Stack (Stack_Ptr).Cursor := Stack_Base;
4225 Stack (Stack_Ptr).Node := CP_Fence_Y'Access;
4226 Stack_Base := Stack (Stack_Base).Cursor;
4229 -- Fence function node Y. This is the node that gets control on
4230 -- a failure that occurs after the fenced pattern has matched.
4232 -- Note: the Cursor at this stage is actually the inner stack
4233 -- base value. We don't reset this, but we do use it to strip
4234 -- off all the entries made by the fenced pattern.
4237 Stack_Ptr := Cursor - 2;
4240 -- Len (integer case)
4243 if Cursor + Node.Nat > Length then
4246 Cursor := Cursor + Node.Nat;
4250 -- Len (Integer function case)
4252 when PC_Len_NF => declare
4253 N : constant Natural := Node.NF.all;
4256 if Cursor + N > Length then
4259 Cursor := Cursor + N;
4264 -- Len (integer pointer case)
4267 if Cursor + Node.NP.all > Length then
4270 Cursor := Cursor + Node.NP.all;
4274 -- NotAny (one character case)
4276 when PC_NotAny_CH =>
4278 and then Subject (Cursor + 1) /= Node.Char
4280 Cursor := Cursor + 1;
4286 -- NotAny (character set case)
4288 when PC_NotAny_CS =>
4290 and then not Is_In (Subject (Cursor + 1), Node.CS)
4292 Cursor := Cursor + 1;
4298 -- NotAny (string function case)
4300 when PC_NotAny_VF => declare
4301 U : constant VString := Node.VF.all;
4302 Str : constant String_Access := Get_String (U);
4307 not Is_In (Subject (Cursor + 1), Str.all)
4309 Cursor := Cursor + 1;
4316 -- NotAny (string pointer case)
4318 when PC_NotAny_VP => declare
4319 Str : String_Access := Get_String (Node.VP.all);
4324 not Is_In (Subject (Cursor + 1), Str.all)
4326 Cursor := Cursor + 1;
4333 -- NSpan (one character case)
4336 while Cursor < Length
4337 and then Subject (Cursor + 1) = Node.Char
4339 Cursor := Cursor + 1;
4344 -- NSpan (character set case)
4347 while Cursor < Length
4348 and then Is_In (Subject (Cursor + 1), Node.CS)
4350 Cursor := Cursor + 1;
4355 -- NSpan (string function case)
4357 when PC_NSpan_VF => declare
4358 U : constant VString := Node.VF.all;
4359 Str : constant String_Access := Get_String (U);
4362 while Cursor < Length
4363 and then Is_In (Subject (Cursor + 1), Str.all)
4365 Cursor := Cursor + 1;
4371 -- NSpan (string pointer case)
4373 when PC_NSpan_VP => declare
4374 Str : String_Access := Get_String (Node.VP.all);
4377 while Cursor < Length
4378 and then Is_In (Subject (Cursor + 1), Str.all)
4380 Cursor := Cursor + 1;
4391 -- Pos (integer case)
4394 if Cursor = Node.Nat then
4400 -- Pos (Integer function case)
4402 when PC_Pos_NF => declare
4403 N : constant Natural := Node.NF.all;
4413 -- Pos (integer pointer case)
4416 if Cursor = Node.NP.all then
4422 -- Predicate function
4424 when PC_Pred_Func =>
4431 -- Region Enter. Initiate new pattern history stack region
4434 Stack (Stack_Ptr + 1).Cursor := Cursor;
4438 -- Region Remove node. This is the node stacked by an R_Enter.
4439 -- It removes the special format stack entry right underneath, and
4440 -- then restores the outer level stack base and signals failure.
4442 -- Note: the cursor value at this stage is actually the (negative)
4443 -- stack base value for the outer level.
4446 Stack_Base := Cursor;
4447 Stack_Ptr := Stack_Ptr - 1;
4450 -- Region restore node. This is the node stacked at the end of an
4451 -- inner level match. Its function is to restore the inner level
4452 -- region, so that alternatives in this region can be sought.
4454 -- Note: the Cursor at this stage is actually the negative of the
4455 -- inner stack base value, which we use to restore the inner region.
4457 when PC_R_Restore =>
4458 Stack_Base := Cursor;
4467 -- Initiate recursive match (pattern pointer case)
4470 Stack (Stack_Ptr + 1).Node := Node.Pthen;
4473 if Stack_Ptr + Node.PP.all.Stk >= Stack_Size then
4474 raise Pattern_Stack_Overflow;
4476 Node := Node.PP.all.P;
4480 -- RPos (integer case)
4483 if Cursor = (Length - Node.Nat) then
4489 -- RPos (integer function case)
4491 when PC_RPos_NF => declare
4492 N : constant Natural := Node.NF.all;
4495 if Length - Cursor = N then
4502 -- RPos (integer pointer case)
4505 if Cursor = (Length - Node.NP.all) then
4511 -- RTab (integer case)
4514 if Cursor <= (Length - Node.Nat) then
4515 Cursor := Length - Node.Nat;
4521 -- RTab (integer function case)
4523 when PC_RTab_NF => declare
4524 N : constant Natural := Node.NF.all;
4527 if Length - Cursor >= N then
4528 Cursor := Length - N;
4535 -- RTab (integer pointer case)
4538 if Cursor <= (Length - Node.NP.all) then
4539 Cursor := Length - Node.NP.all;
4545 -- Cursor assignment
4548 Node.Var.all := Cursor;
4551 -- Span (one character case)
4553 when PC_Span_CH => declare
4554 P : Natural := Cursor;
4558 and then Subject (P + 1) = Node.Char
4571 -- Span (character set case)
4573 when PC_Span_CS => declare
4574 P : Natural := Cursor;
4578 and then Is_In (Subject (P + 1), Node.CS)
4591 -- Span (string function case)
4593 when PC_Span_VF => declare
4594 U : constant VString := Node.VF.all;
4595 Str : constant String_Access := Get_String (U);
4596 P : Natural := Cursor;
4600 and then Is_In (Subject (P + 1), Str.all)
4613 -- Span (string pointer case)
4615 when PC_Span_VP => declare
4616 Str : String_Access := Get_String (Node.VP.all);
4617 P : Natural := Cursor;
4621 and then Is_In (Subject (P + 1), Str.all)
4634 -- String (two character case)
4637 if (Length - Cursor) >= 2
4638 and then Subject (Cursor + 1 .. Cursor + 2) = Node.Str2
4640 Cursor := Cursor + 2;
4646 -- String (three character case)
4649 if (Length - Cursor) >= 3
4650 and then Subject (Cursor + 1 .. Cursor + 3) = Node.Str3
4652 Cursor := Cursor + 3;
4658 -- String (four character case)
4661 if (Length - Cursor) >= 4
4662 and then Subject (Cursor + 1 .. Cursor + 4) = Node.Str4
4664 Cursor := Cursor + 4;
4670 -- String (five character case)
4673 if (Length - Cursor) >= 5
4674 and then Subject (Cursor + 1 .. Cursor + 5) = Node.Str5
4676 Cursor := Cursor + 5;
4682 -- String (six character case)
4685 if (Length - Cursor) >= 6
4686 and then Subject (Cursor + 1 .. Cursor + 6) = Node.Str6
4688 Cursor := Cursor + 6;
4694 -- String (case of more than six characters)
4696 when PC_String => declare
4697 Len : constant Natural := Node.Str'Length;
4700 if (Length - Cursor) >= Len
4701 and then Node.Str.all = Subject (Cursor + 1 .. Cursor + Len)
4703 Cursor := Cursor + Len;
4710 -- String (function case)
4712 when PC_String_VF => declare
4713 U : constant VString := Node.VF.all;
4714 Str : constant String_Access := Get_String (U);
4715 Len : constant Natural := Str'Length;
4718 if (Length - Cursor) >= Len
4719 and then Str.all = Subject (Cursor + 1 .. Cursor + Len)
4721 Cursor := Cursor + Len;
4728 -- String (pointer case)
4730 when PC_String_VP => declare
4731 S : String_Access := Get_String (Node.VP.all);
4732 Len : constant Natural := S'Length;
4735 if (Length - Cursor) >= Len
4736 and then S.all = Subject (Cursor + 1 .. Cursor + Len)
4738 Cursor := Cursor + Len;
4751 -- Tab (integer case)
4754 if Cursor <= Node.Nat then
4761 -- Tab (integer function case)
4763 when PC_Tab_NF => declare
4764 N : constant Natural := Node.NF.all;
4775 -- Tab (integer pointer case)
4778 if Cursor <= Node.NP.all then
4779 Cursor := Node.NP.all;
4785 -- Unanchored movement
4787 when PC_Unanchored =>
4789 -- All done if we tried every position
4791 if Cursor > Length then
4794 -- Otherwise extend the anchor point, and restack ourself
4797 Cursor := Cursor + 1;
4802 -- Write immediate. This node performs the actual write
4804 when PC_Write_Imm =>
4807 Subject (Stack (Stack_Base - 1).Cursor + 1 .. Cursor));
4811 -- Write on match. This node sets up for the eventual write
4813 when PC_Write_OnM =>
4814 Stack (Stack_Base - 1).Node := Node;
4815 Push (CP_Assign'Access);
4822 -- We are NOT allowed to fall though this case statement, since every
4823 -- match routine must end by executing a goto to the appropriate point
4824 -- in the finite state machine model.
4834 -- Maintenance note: There is a LOT of code duplication between XMatch
4835 -- and XMatchD. This is quite intentional, the point is to avoid any
4836 -- unnecessary debugging overhead in the XMatch case, but this does mean
4837 -- that any changes to XMatchD must be mirrored in XMatch. In case of
4838 -- any major changes, the proper approach is to delete XMatch, make the
4839 -- changes to XMatchD, and then make a copy of XMatchD, removing all
4840 -- calls to Dout, and all Put and Put_Line operations. This copy becomes
4847 Start : out Natural;
4851 -- Pointer to current pattern node. Initialized from Pat_P, and then
4852 -- updated as the match proceeds through its constituent elements.
4854 Length : constant Natural := Subject'Length;
4855 -- Length of string (= Subject'Last, since Subject'First is always 1)
4857 Cursor : Integer := 0;
4858 -- If the value is non-negative, then this value is the index showing
4859 -- the current position of the match in the subject string. The next
4860 -- character to be matched is at Subject (Cursor + 1). Note that since
4861 -- our view of the subject string in XMatch always has a lower bound
4862 -- of one, regardless of original bounds, that this definition exactly
4863 -- corresponds to the cursor value as referenced by functions like Pos.
4865 -- If the value is negative, then this is a saved stack pointer,
4866 -- typically a base pointer of an inner or outer region. Cursor
4867 -- temporarily holds such a value when it is popped from the stack
4868 -- by Fail. In all cases, Cursor is reset to a proper non-negative
4869 -- cursor value before the match proceeds (e.g. by propagating the
4870 -- failure and popping a "real" cursor value from the stack.
4872 PE_Unanchored : aliased PE := (PC_Unanchored, 0, Pat_P);
4873 -- Dummy pattern element used in the unanchored case.
4875 Region_Level : Natural := 0;
4876 -- Keeps track of recursive region level. This is used only for
4877 -- debugging, it is the number of saved history stack base values.
4880 -- The pattern matching failure stack for this call to Match
4882 Stack_Ptr : Stack_Range;
4883 -- Current stack pointer. This points to the top element of the stack
4884 -- that is currently in use. At the outer level this is the special
4885 -- entry placed on the stack according to the anchor mode.
4887 Stack_Init : constant Stack_Range := Stack'First + 1;
4888 -- This is the initial value of the Stack_Ptr and Stack_Base. The
4889 -- initial (Stack'First) element of the stack is not used so that
4890 -- when we pop the last element off, Stack_Ptr is still in range.
4892 Stack_Base : Stack_Range;
4893 -- This value is the stack base value, i.e. the stack pointer for the
4894 -- first history stack entry in the current stack region. See separate
4895 -- section on handling of recursive pattern matches.
4897 Assign_OnM : Boolean := False;
4898 -- Set True if assign-on-match or write-on-match operations may be
4899 -- present in the history stack, which must then be scanned on a
4900 -- successful match.
4902 procedure Dout (Str : String);
4903 -- Output string to standard error with bars indicating region level.
4905 procedure Dout (Str : String; A : Character);
4906 -- Calls Dout with the string S ('A
')
4908 procedure Dout (Str : String; A : Character_Set);
4909 -- Calls Dout with the string S ("A")
4911 procedure Dout (Str : String; A : Natural);
4912 -- Calls Dout with the string S (A)
4914 procedure Dout (Str : String; A : String);
4915 -- Calls Dout with the string S ("A")
4917 function Img (P : PE_Ptr) return String;
4918 -- Returns a string of the form #nnn where nnn is P.Index
4920 procedure Pop_Region;
4921 pragma Inline (Pop_Region);
4922 -- Used at the end of processing of an inner region. if the inner
4923 -- region left no stack entries, then all trace of it is removed.
4924 -- Otherwise a PC_Restore_Region entry is pushed to ensure proper
4925 -- handling of alternatives in the inner region.
4927 procedure Push (Node : PE_Ptr);
4928 pragma Inline (Push);
4929 -- Make entry in pattern matching stack with current cursor valeu
4931 procedure Push_Region;
4932 pragma Inline (Push_Region);
4933 -- This procedure makes a new region on the history stack. The
4934 -- caller first establishes the special entry on the stack, but
4935 -- does not push the stack pointer. Then this call stacks a
4936 -- PC_Remove_Region node, on top of this entry, using the cursor
4937 -- field of the PC_Remove_Region entry to save the outer level
4938 -- stack base value, and resets the stack base to point to this
4939 -- PC_Remove_Region node.
4945 procedure Dout (Str : String) is
4947 for J in 1 .. Region_Level loop
4954 procedure Dout (Str : String; A : Character) is
4956 Dout (Str & " ('" & A & "')");
4959 procedure Dout (Str : String; A : Character_Set) is
4961 Dout (Str & " (" & Image (To_Sequence (A)) & ')');
4964 procedure Dout (Str : String; A : Natural) is
4966 Dout (Str & " (" & A & ')');
4969 procedure Dout (Str : String; A : String) is
4971 Dout (Str & " (" & Image (A) & ')');
4978 function Img (P : PE_Ptr) return String is
4980 return "#" & Integer (P.Index) & " ";
4987 procedure Pop_Region is
4989 Region_Level := Region_Level - 1;
4991 -- If nothing was pushed in the inner region, we can just get
4992 -- rid of it entirely, leaving no traces that it was ever there
4994 if Stack_Ptr = Stack_Base then
4995 Stack_Ptr := Stack_Base - 2;
4996 Stack_Base := Stack (Stack_Ptr + 2).Cursor;
4998 -- If stuff was pushed in the inner region, then we have to
4999 -- push a PC_R_Restore node so that we properly handle possible
5000 -- rematches within the region.
5003 Stack_Ptr := Stack_Ptr + 1;
5004 Stack (Stack_Ptr).Cursor := Stack_Base;
5005 Stack (Stack_Ptr).Node := CP_R_Restore'Access;
5006 Stack_Base := Stack (Stack_Base).Cursor;
5014 procedure Push (Node : PE_Ptr) is
5016 Stack_Ptr := Stack_Ptr + 1;
5017 Stack (Stack_Ptr).Cursor := Cursor;
5018 Stack (Stack_Ptr).Node := Node;
5025 procedure Push_Region is
5027 Region_Level := Region_Level + 1;
5028 Stack_Ptr := Stack_Ptr + 2;
5029 Stack (Stack_Ptr).Cursor := Stack_Base;
5030 Stack (Stack_Ptr).Node := CP_R_Remove'Access;
5031 Stack_Base := Stack_Ptr;
5034 -- Start of processing for XMatchD
5038 Put_Line ("Initiating pattern match, subject = " & Image (Subject));
5039 Put ("--------------------------------------");
5041 for J in 1 .. Length loop
5046 Put_Line ("subject length = " & Length);
5048 if Pat_P = null then
5049 Uninitialized_Pattern;
5052 -- Check we have enough stack for this pattern. This check deals with
5053 -- every possibility except a match of a recursive pattern, where we
5054 -- make a check at each recursion level.
5056 if Pat_S >= Stack_Size - 1 then
5057 raise Pattern_Stack_Overflow;
5060 -- In anchored mode, the bottom entry on the stack is an abort entry
5062 if Anchored_Mode then
5063 Stack (Stack_Init).Node := CP_Cancel'Access;
5064 Stack (Stack_Init).Cursor := 0;
5066 -- In unanchored more, the bottom entry on the stack references
5067 -- the special pattern element PE_Unanchored, whose Pthen field
5068 -- points to the initial pattern element. The cursor value in this
5069 -- entry is the number of anchor moves so far.
5072 Stack (Stack_Init).Node := PE_Unanchored'Unchecked_Access;
5073 Stack (Stack_Init).Cursor := 0;
5076 Stack_Ptr := Stack_Init;
5077 Stack_Base := Stack_Ptr;
5082 -----------------------------------------
5083 -- Main Pattern Matching State Control --
5084 -----------------------------------------
5086 -- This is a state machine which uses gotos to change state. The
5087 -- initial state is Match, to initiate the matching of the first
5088 -- element, so the goto Match above starts the match. In the
5089 -- following descriptions, we indicate the global values that
5090 -- are relevant for the state transition.
5092 -- Come here if entire match fails
5095 Dout ("match fails");
5101 -- Come here if entire match succeeds
5103 -- Cursor current position in subject string
5106 Dout ("match succeeds");
5107 Start := Stack (Stack_Init).Cursor + 1;
5109 Dout ("first matched character index = " & Start);
5110 Dout ("last matched character index = " & Stop);
5111 Dout ("matched substring = " & Image (Subject (Start .. Stop)));
5113 -- Scan history stack for deferred assignments or writes
5116 for S in Stack'First .. Stack_Ptr loop
5117 if Stack (S).Node = CP_Assign'Access then
5119 Inner_Base : constant Stack_Range :=
5120 Stack (S + 1).Cursor;
5121 Special_Entry : constant Stack_Range :=
5123 Node_OnM : constant PE_Ptr :=
5124 Stack (Special_Entry).Node;
5125 Start : constant Natural :=
5126 Stack (Special_Entry).Cursor + 1;
5127 Stop : constant Natural := Stack (S).Cursor;
5130 if Node_OnM.Pcode = PC_Assign_OnM then
5131 Set_String (Node_OnM.VP.all, Subject (Start .. Stop));
5133 (Img (Stack (S).Node) &
5134 "deferred assignment of " &
5135 Image (Subject (Start .. Stop)));
5137 elsif Node_OnM.Pcode = PC_Write_OnM then
5138 Put_Line (Node_OnM.FP.all, Subject (Start .. Stop));
5140 (Img (Stack (S).Node) &
5141 "deferred write of " &
5142 Image (Subject (Start .. Stop)));
5155 -- Come here if attempt to match current element fails
5157 -- Stack_Base current stack base
5158 -- Stack_Ptr current stack pointer
5161 Cursor := Stack (Stack_Ptr).Cursor;
5162 Node := Stack (Stack_Ptr).Node;
5163 Stack_Ptr := Stack_Ptr - 1;
5166 Dout ("failure, cursor reset to " & Cursor);
5171 -- Come here if attempt to match current element succeeds
5173 -- Cursor current position in subject string
5174 -- Node pointer to node successfully matched
5175 -- Stack_Base current stack base
5176 -- Stack_Ptr current stack pointer
5179 Dout ("success, cursor = " & Cursor);
5182 -- Come here to match the next pattern element
5184 -- Cursor current position in subject string
5185 -- Node pointer to node to be matched
5186 -- Stack_Base current stack base
5187 -- Stack_Ptr current stack pointer
5191 --------------------------------------------------
5192 -- Main Pattern Match Element Matching Routines --
5193 --------------------------------------------------
5195 -- Here is the case statement that processes the current node. The
5196 -- processing for each element does one of five things:
5198 -- goto Succeed to move to the successor
5199 -- goto Match_Succeed if the entire match succeeds
5200 -- goto Match_Fail if the entire match fails
5201 -- goto Fail to signal failure of current match
5203 -- Processing is NOT allowed to fall through
5210 Dout (Img (Node) & "matching Cancel");
5217 (Img (Node) & "setting up alternative " & Img (Node.Alt));
5222 -- Any (one character case)
5225 Dout (Img (Node) & "matching Any", Node.Char);
5228 and then Subject (Cursor + 1) = Node.Char
5230 Cursor := Cursor + 1;
5236 -- Any (character set case)
5239 Dout (Img (Node) & "matching Any", Node.CS);
5242 and then Is_In (Subject (Cursor + 1), Node.CS)
5244 Cursor := Cursor + 1;
5250 -- Any (string function case)
5252 when PC_Any_VF => declare
5253 U : constant VString := Node.VF.all;
5254 Str : constant String_Access := Get_String (U);
5257 Dout (Img (Node) & "matching Any", Str.all);
5260 and then Is_In (Subject (Cursor + 1), Str.all)
5262 Cursor := Cursor + 1;
5269 -- Any (string pointer case)
5271 when PC_Any_VP => declare
5272 Str : String_Access := Get_String (Node.VP.all);
5275 Dout (Img (Node) & "matching Any", Str.all);
5278 and then Is_In (Subject (Cursor + 1), Str.all)
5280 Cursor := Cursor + 1;
5287 -- Arb (initial match)
5290 Dout (Img (Node) & "matching Arb");
5298 Dout (Img (Node) & "extending Arb");
5300 if Cursor < Length then
5301 Cursor := Cursor + 1;
5308 -- Arbno_S (simple Arbno initialize). This is the node that
5309 -- initiates the match of a simple Arbno structure.
5313 "setting up Arbno alternative " & Img (Node.Alt));
5318 -- Arbno_X (Arbno initialize). This is the node that initiates
5319 -- the match of a complex Arbno structure.
5323 "setting up Arbno alternative " & Img (Node.Alt));
5328 -- Arbno_Y (Arbno rematch). This is the node that is executed
5329 -- following successful matching of one instance of a complex
5332 when PC_Arbno_Y => declare
5333 Null_Match : Boolean := (Cursor = Stack (Stack_Base - 1).Cursor);
5336 Dout (Img (Node) & "extending Arbno");
5339 -- If arbno extension matched null, then immediately fail
5342 Dout ("Arbno extension matched null, so fails");
5346 -- Here we must do a stack check to make sure enough stack
5347 -- is left. This check will happen once for each instance of
5348 -- the Arbno pattern that is matched. The Nat field of a
5349 -- PC_Arbno pattern contains the maximum stack entries needed
5350 -- for the Arbno with one instance and the successor pattern
5352 if Stack_Ptr + Node.Nat >= Stack'Last then
5353 raise Pattern_Stack_Overflow;
5359 -- Assign. If this node is executed, it means the assign-on-match
5360 -- or write-on-match operation will not happen after all, so we
5361 -- is propagate the failure, removing the PC_Assign node.
5364 Dout (Img (Node) & "deferred assign/write cancelled");
5367 -- Assign immediate. This node performs the actual assignment.
5369 when PC_Assign_Imm =>
5371 (Img (Node) & "executing immediate assignment of " &
5372 Image (Subject (Stack (Stack_Base - 1).Cursor + 1 .. Cursor)));
5375 Subject (Stack (Stack_Base - 1).Cursor + 1 .. Cursor));
5379 -- Assign on match. This node sets up for the eventual assignment
5381 when PC_Assign_OnM =>
5382 Dout (Img (Node) & "registering deferred assignment");
5383 Stack (Stack_Base - 1).Node := Node;
5384 Push (CP_Assign'Access);
5392 Dout (Img (Node) & "matching or extending Bal");
5393 if Cursor >= Length or else Subject (Cursor + 1) = ')' then
5396 elsif Subject (Cursor + 1) = '(' then
5398 Paren_Count : Natural := 1;
5402 Cursor := Cursor + 1;
5404 if Cursor >= Length then
5407 elsif Subject (Cursor + 1) = '(' then
5408 Paren_Count := Paren_Count + 1;
5410 elsif Subject (Cursor + 1) = ')' then
5411 Paren_Count := Paren_Count - 1;
5412 exit when Paren_Count = 0;
5418 Cursor := Cursor + 1;
5422 -- Break (one character case)
5425 Dout (Img (Node) & "matching Break", Node.Char);
5427 while Cursor < Length loop
5428 if Subject (Cursor + 1) = Node.Char then
5431 Cursor := Cursor + 1;
5437 -- Break (character set case)
5440 Dout (Img (Node) & "matching Break", Node.CS);
5442 while Cursor < Length loop
5443 if Is_In (Subject (Cursor + 1), Node.CS) then
5446 Cursor := Cursor + 1;
5452 -- Break (string function case)
5454 when PC_Break_VF => declare
5455 U : constant VString := Node.VF.all;
5456 Str : constant String_Access := Get_String (U);
5459 Dout (Img (Node) & "matching Break", Str.all);
5461 while Cursor < Length loop
5462 if Is_In (Subject (Cursor + 1), Str.all) then
5465 Cursor := Cursor + 1;
5472 -- Break (string pointer case)
5474 when PC_Break_VP => declare
5475 Str : String_Access := Get_String (Node.VP.all);
5478 Dout (Img (Node) & "matching Break", Str.all);
5480 while Cursor < Length loop
5481 if Is_In (Subject (Cursor + 1), Str.all) then
5484 Cursor := Cursor + 1;
5491 -- BreakX (one character case)
5493 when PC_BreakX_CH =>
5494 Dout (Img (Node) & "matching BreakX", Node.Char);
5496 while Cursor < Length loop
5497 if Subject (Cursor + 1) = Node.Char then
5500 Cursor := Cursor + 1;
5506 -- BreakX (character set case)
5508 when PC_BreakX_CS =>
5509 Dout (Img (Node) & "matching BreakX", Node.CS);
5511 while Cursor < Length loop
5512 if Is_In (Subject (Cursor + 1), Node.CS) then
5515 Cursor := Cursor + 1;
5521 -- BreakX (string function case)
5523 when PC_BreakX_VF => declare
5524 U : constant VString := Node.VF.all;
5525 Str : constant String_Access := Get_String (U);
5528 Dout (Img (Node) & "matching BreakX", Str.all);
5530 while Cursor < Length loop
5531 if Is_In (Subject (Cursor + 1), Str.all) then
5534 Cursor := Cursor + 1;
5541 -- BreakX (string pointer case)
5543 when PC_BreakX_VP => declare
5544 Str : String_Access := Get_String (Node.VP.all);
5547 Dout (Img (Node) & "matching BreakX", Str.all);
5549 while Cursor < Length loop
5550 if Is_In (Subject (Cursor + 1), Str.all) then
5553 Cursor := Cursor + 1;
5560 -- BreakX_X (BreakX extension). See section on "Compound Pattern
5561 -- Structures". This node is the alternative that is stacked
5562 -- to skip past the break character and extend the break.
5565 Dout (Img (Node) & "extending BreakX");
5567 Cursor := Cursor + 1;
5570 -- Character (one character string)
5573 Dout (Img (Node) & "matching '" & Node.Char & ''');
5576 and then Subject (Cursor + 1) = Node.Char
5578 Cursor := Cursor + 1;
5587 if Stack_Base = Stack_Init then
5588 Dout ("end of pattern
");
5591 -- End of recursive inner match. See separate section on
5592 -- handing of recursive pattern matches for details.
5595 Dout ("terminating recursive match
");
5596 Node := Stack (Stack_Base - 1).Node;
5604 Dout (Img (Node) & "matching Fail
");
5607 -- Fence (built in pattern)
5610 Dout (Img (Node) & "matching Fence
");
5611 Push (CP_Cancel'Access);
5614 -- Fence function node X. This is the node that gets control
5615 -- after a successful match of the fenced pattern.
5618 Dout (Img (Node) & "matching Fence
function");
5619 Stack_Ptr := Stack_Ptr + 1;
5620 Stack (Stack_Ptr).Cursor := Stack_Base;
5621 Stack (Stack_Ptr).Node := CP_Fence_Y'Access;
5622 Stack_Base := Stack (Stack_Base).Cursor;
5623 Region_Level := Region_Level - 1;
5626 -- Fence function node Y. This is the node that gets control on
5627 -- a failure that occurs after the fenced pattern has matched.
5629 -- Note: the Cursor at this stage is actually the inner stack
5630 -- base value. We don't reset this, but we do use it to strip
5631 -- off all the entries made by the fenced pattern.
5634 Dout (Img (Node) & "pattern matched by Fence caused failure
");
5635 Stack_Ptr := Cursor - 2;
5638 -- Len (integer case)
5641 Dout (Img (Node) & "matching Len
", Node.Nat);
5643 if Cursor + Node.Nat > Length then
5646 Cursor := Cursor + Node.Nat;
5650 -- Len (Integer function case)
5652 when PC_Len_NF => declare
5653 N : constant Natural := Node.NF.all;
5656 Dout (Img (Node) & "matching Len
", N);
5658 if Cursor + N > Length then
5661 Cursor := Cursor + N;
5666 -- Len (integer pointer case)
5669 Dout (Img (Node) & "matching Len
", Node.NP.all);
5671 if Cursor + Node.NP.all > Length then
5674 Cursor := Cursor + Node.NP.all;
5678 -- NotAny (one character case)
5680 when PC_NotAny_CH =>
5681 Dout (Img (Node) & "matching NotAny
", Node.Char);
5684 and then Subject (Cursor + 1) /= Node.Char
5686 Cursor := Cursor + 1;
5692 -- NotAny (character set case)
5694 when PC_NotAny_CS =>
5695 Dout (Img (Node) & "matching NotAny
", Node.CS);
5698 and then not Is_In (Subject (Cursor + 1), Node.CS)
5700 Cursor := Cursor + 1;
5706 -- NotAny (string function case)
5708 when PC_NotAny_VF => declare
5709 U : constant VString := Node.VF.all;
5710 Str : constant String_Access := Get_String (U);
5713 Dout (Img (Node) & "matching NotAny
", Str.all);
5717 not Is_In (Subject (Cursor + 1), Str.all)
5719 Cursor := Cursor + 1;
5726 -- NotAny (string pointer case)
5728 when PC_NotAny_VP => declare
5729 Str : String_Access := Get_String (Node.VP.all);
5732 Dout (Img (Node) & "matching NotAny
", Str.all);
5736 not Is_In (Subject (Cursor + 1), Str.all)
5738 Cursor := Cursor + 1;
5745 -- NSpan (one character case)
5748 Dout (Img (Node) & "matching NSpan
", Node.Char);
5750 while Cursor < Length
5751 and then Subject (Cursor + 1) = Node.Char
5753 Cursor := Cursor + 1;
5758 -- NSpan (character set case)
5761 Dout (Img (Node) & "matching NSpan
", Node.CS);
5763 while Cursor < Length
5764 and then Is_In (Subject (Cursor + 1), Node.CS)
5766 Cursor := Cursor + 1;
5771 -- NSpan (string function case)
5773 when PC_NSpan_VF => declare
5774 U : constant VString := Node.VF.all;
5775 Str : constant String_Access := Get_String (U);
5778 Dout (Img (Node) & "matching NSpan
", Str.all);
5780 while Cursor < Length
5781 and then Is_In (Subject (Cursor + 1), Str.all)
5783 Cursor := Cursor + 1;
5789 -- NSpan (string pointer case)
5791 when PC_NSpan_VP => declare
5792 Str : String_Access := Get_String (Node.VP.all);
5795 Dout (Img (Node) & "matching NSpan
", Str.all);
5797 while Cursor < Length
5798 and then Is_In (Subject (Cursor + 1), Str.all)
5800 Cursor := Cursor + 1;
5807 Dout (Img (Node) & "matching
null");
5810 -- Pos (integer case)
5813 Dout (Img (Node) & "matching Pos
", Node.Nat);
5815 if Cursor = Node.Nat then
5821 -- Pos (Integer function case)
5823 when PC_Pos_NF => declare
5824 N : constant Natural := Node.NF.all;
5827 Dout (Img (Node) & "matching Pos
", N);
5836 -- Pos (integer pointer case)
5839 Dout (Img (Node) & "matching Pos
", Node.NP.all);
5841 if Cursor = Node.NP.all then
5847 -- Predicate function
5849 when PC_Pred_Func =>
5850 Dout (Img (Node) & "matching predicate
function");
5858 -- Region Enter. Initiate new pattern history stack region
5861 Dout (Img (Node) & "starting match
of nested pattern
");
5862 Stack (Stack_Ptr + 1).Cursor := Cursor;
5866 -- Region Remove node. This is the node stacked by an R_Enter.
5867 -- It removes the special format stack entry right underneath, and
5868 -- then restores the outer level stack base and signals failure.
5870 -- Note: the cursor value at this stage is actually the (negative)
5871 -- stack base value for the outer level.
5874 Dout ("failure
, match
of nested pattern terminated
");
5875 Stack_Base := Cursor;
5876 Region_Level := Region_Level - 1;
5877 Stack_Ptr := Stack_Ptr - 1;
5880 -- Region restore node. This is the node stacked at the end of an
5881 -- inner level match. Its function is to restore the inner level
5882 -- region, so that alternatives in this region can be sought.
5884 -- Note: the Cursor at this stage is actually the negative of the
5885 -- inner stack base value, which we use to restore the inner region.
5887 when PC_R_Restore =>
5888 Dout ("failure
, search
for alternatives
in nested pattern
");
5889 Region_Level := Region_Level + 1;
5890 Stack_Base := Cursor;
5896 Dout (Img (Node) & "matching Rest
");
5900 -- Initiate recursive match (pattern pointer case)
5903 Stack (Stack_Ptr + 1).Node := Node.Pthen;
5905 Dout (Img (Node) & "initiating recursive match
");
5907 if Stack_Ptr + Node.PP.all.Stk >= Stack_Size then
5908 raise Pattern_Stack_Overflow;
5910 Node := Node.PP.all.P;
5914 -- RPos (integer case)
5917 Dout (Img (Node) & "matching RPos
", Node.Nat);
5919 if Cursor = (Length - Node.Nat) then
5925 -- RPos (integer function case)
5927 when PC_RPos_NF => declare
5928 N : constant Natural := Node.NF.all;
5931 Dout (Img (Node) & "matching RPos
", N);
5933 if Length - Cursor = N then
5940 -- RPos (integer pointer case)
5943 Dout (Img (Node) & "matching RPos
", Node.NP.all);
5945 if Cursor = (Length - Node.NP.all) then
5951 -- RTab (integer case)
5954 Dout (Img (Node) & "matching RTab
", Node.Nat);
5956 if Cursor <= (Length - Node.Nat) then
5957 Cursor := Length - Node.Nat;
5963 -- RTab (integer function case)
5965 when PC_RTab_NF => declare
5966 N : constant Natural := Node.NF.all;
5969 Dout (Img (Node) & "matching RPos
", N);
5971 if Length - Cursor >= N then
5972 Cursor := Length - N;
5979 -- RTab (integer pointer case)
5982 Dout (Img (Node) & "matching RPos
", Node.NP.all);
5984 if Cursor <= (Length - Node.NP.all) then
5985 Cursor := Length - Node.NP.all;
5991 -- Cursor assignment
5994 Dout (Img (Node) & "matching Setcur
");
5995 Node.Var.all := Cursor;
5998 -- Span (one character case)
6000 when PC_Span_CH => declare
6001 P : Natural := Cursor;
6004 Dout (Img (Node) & "matching Span
", Node.Char);
6007 and then Subject (P + 1) = Node.Char
6020 -- Span (character set case)
6022 when PC_Span_CS => declare
6023 P : Natural := Cursor;
6026 Dout (Img (Node) & "matching Span
", Node.CS);
6029 and then Is_In (Subject (P + 1), Node.CS)
6042 -- Span (string function case)
6044 when PC_Span_VF => declare
6045 U : constant VString := Node.VF.all;
6046 Str : constant String_Access := Get_String (U);
6047 P : Natural := Cursor;
6050 Dout (Img (Node) & "matching Span
", Str.all);
6053 and then Is_In (Subject (P + 1), Str.all)
6066 -- Span (string pointer case)
6068 when PC_Span_VP => declare
6069 Str : String_Access := Get_String (Node.VP.all);
6070 P : Natural := Cursor;
6073 Dout (Img (Node) & "matching Span
", Str.all);
6076 and then Is_In (Subject (P + 1), Str.all)
6089 -- String (two character case)
6092 Dout (Img (Node) & "matching
" & Image (Node.Str2));
6094 if (Length - Cursor) >= 2
6095 and then Subject (Cursor + 1 .. Cursor + 2) = Node.Str2
6097 Cursor := Cursor + 2;
6103 -- String (three character case)
6106 Dout (Img (Node) & "matching
" & Image (Node.Str3));
6108 if (Length - Cursor) >= 3
6109 and then Subject (Cursor + 1 .. Cursor + 3) = Node.Str3
6111 Cursor := Cursor + 3;
6117 -- String (four character case)
6120 Dout (Img (Node) & "matching
" & Image (Node.Str4));
6122 if (Length - Cursor) >= 4
6123 and then Subject (Cursor + 1 .. Cursor + 4) = Node.Str4
6125 Cursor := Cursor + 4;
6131 -- String (five character case)
6134 Dout (Img (Node) & "matching
" & Image (Node.Str5));
6136 if (Length - Cursor) >= 5
6137 and then Subject (Cursor + 1 .. Cursor + 5) = Node.Str5
6139 Cursor := Cursor + 5;
6145 -- String (six character case)
6148 Dout (Img (Node) & "matching
" & Image (Node.Str6));
6150 if (Length - Cursor) >= 6
6151 and then Subject (Cursor + 1 .. Cursor + 6) = Node.Str6
6153 Cursor := Cursor + 6;
6159 -- String (case of more than six characters)
6161 when PC_String => declare
6162 Len : constant Natural := Node.Str'Length;
6165 Dout (Img (Node) & "matching
" & Image (Node.Str.all));
6167 if (Length - Cursor) >= Len
6168 and then Node.Str.all = Subject (Cursor + 1 .. Cursor + Len)
6170 Cursor := Cursor + Len;
6177 -- String (function case)
6179 when PC_String_VF => declare
6180 U : constant VString := Node.VF.all;
6181 Str : constant String_Access := Get_String (U);
6182 Len : constant Natural := Str'Length;
6185 Dout (Img (Node) & "matching
" & Image (Str.all));
6187 if (Length - Cursor) >= Len
6188 and then Str.all = Subject (Cursor + 1 .. Cursor + Len)
6190 Cursor := Cursor + Len;
6197 -- String (vstring pointer case)
6199 when PC_String_VP => declare
6200 S : String_Access := Get_String (Node.VP.all);
6201 Len : constant Natural :=
6202 Ada.Strings.Unbounded.Length (Node.VP.all);
6206 (Img (Node) & "matching
" & Image (S.all));
6208 if (Length - Cursor) >= Len
6209 and then S.all = Subject (Cursor + 1 .. Cursor + Len)
6211 Cursor := Cursor + Len;
6221 Dout (Img (Node) & "matching Succeed
");
6225 -- Tab (integer case)
6228 Dout (Img (Node) & "matching Tab
", Node.Nat);
6230 if Cursor <= Node.Nat then
6237 -- Tab (integer function case)
6239 when PC_Tab_NF => declare
6240 N : constant Natural := Node.NF.all;
6243 Dout (Img (Node) & "matching Tab
", N);
6253 -- Tab (integer pointer case)
6256 Dout (Img (Node) & "matching Tab
", Node.NP.all);
6258 if Cursor <= Node.NP.all then
6259 Cursor := Node.NP.all;
6265 -- Unanchored movement
6267 when PC_Unanchored =>
6268 Dout ("attempting to move anchor point
");
6270 -- All done if we tried every position
6272 if Cursor > Length then
6275 -- Otherwise extend the anchor point, and restack ourself
6278 Cursor := Cursor + 1;
6283 -- Write immediate. This node performs the actual write
6285 when PC_Write_Imm =>
6286 Dout (Img (Node) & "executing immediate write
of " &
6287 Subject (Stack (Stack_Base - 1).Cursor + 1 .. Cursor));
6291 Subject (Stack (Stack_Base - 1).Cursor + 1 .. Cursor));
6295 -- Write on match. This node sets up for the eventual write
6297 when PC_Write_OnM =>
6298 Dout (Img (Node) & "registering deferred write
");
6299 Stack (Stack_Base - 1).Node := Node;
6300 Push (CP_Assign'Access);
6307 -- We are NOT allowed to fall though this case statement, since every
6308 -- match routine must end by executing a goto to the appropriate point
6309 -- in the finite state machine model.
6315 end GNAT.Spitbol.Patterns;