home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / adav313.zip / gnat-3_13p-os2-bin-20010916.zip / emx / gnatlib / g-spipat.ads < prev    next >
Text File  |  2000-07-19  |  56KB  |  1,205 lines

  1. ------------------------------------------------------------------------------
  2. --                                                                          --
  3. --                         GNAT LIBRARY COMPONENTS                          --
  4. --                                                                          --
  5. --                G N A T . S P I T B O L . P A T T E R N S                 --
  6. --                                                                          --
  7. --                                 S p e c                                  --
  8. --                                                                          --
  9. --                            $Revision: 1.17 $
  10. --                                                                          --
  11. --           Copyright (C) 1997-1999 Ada Core Technologies, Inc.            --
  12. --                                                                          --
  13. -- GNAT is free software;  you can  redistribute it  and/or modify it under --
  14. -- terms of the  GNU General Public License as published  by the Free Soft- --
  15. -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
  16. -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
  17. -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
  18. -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
  19. -- for  more details.  You should have  received  a copy of the GNU General --
  20. -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
  21. -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
  22. -- MA 02111-1307, USA.                                                      --
  23. --                                                                          --
  24. -- As a special exception,  if other files  instantiate  generics from this --
  25. -- unit, or you link  this unit with other files  to produce an executable, --
  26. -- this  unit  does not  by itself cause  the resulting  executable  to  be --
  27. -- covered  by the  GNU  General  Public  License.  This exception does not --
  28. -- however invalidate  any other reasons why  the executable file  might be --
  29. -- covered by the  GNU Public License.                                      --
  30. --                                                                          --
  31. -- GNAT is maintained by Ada Core Technologies Inc (http://www.gnat.com).   --
  32. --                                                                          --
  33. ------------------------------------------------------------------------------
  34.  
  35. --  SPITBOL-like pattern construction and matching
  36.  
  37. --  This child package of GNAT.SPITBOL provides a complete implementation
  38. --  of the SPITBOL-like pattern construction and matching operations. This
  39. --  package is based on Macro-SPITBOL created by Robert Dewar.
  40.  
  41. ------------------------------------------------------------
  42. -- Summary of Pattern Matching Packages in GNAT Hierarchy --
  43. ------------------------------------------------------------
  44.  
  45. --  There are three related packages that perform pattern maching functions.
  46. --  the following is an outline of these packages, to help you determine
  47. --  which is best for your needs.
  48.  
  49. --     GNAT.Regexp (files g-regexp.ads/g-regexp.adb)
  50. --       This is a simple package providing Unix-style regular expression
  51. --       matching with the restriction that it matches entire strings. It
  52. --       is particularly useful for file name matching, and in particular
  53. --       it provides "globbing patterns" that are useful in implementing
  54. --       unix or DOS style wild card matching for file names.
  55.  
  56. --     GNAT.Regpat (files g-regpat.ads/g-regpat.adb)
  57. --       This is a more complete implementation of Unix-style regular
  58. --       expressions, copied from the original V7 style regular expression
  59. --       library written in C by Henry Spencer. It is functionally the
  60. --       same as this library, and uses the same internal data structures
  61. --       stored in a binary compatible manner.
  62.  
  63. --     GNAT.Spitbol.Patterns (files g-spipat.ads/g-spipat.adb)
  64. --       This is a completely general patterm matching package based on the
  65. --       pattern language of SNOBOL4, as implemented in SPITBOL. The pattern
  66. --       language is modeled on context free grammars, with context sensitive
  67. --       extensions that provide full (type 0) computational capabilities.
  68.  
  69. with Ada.Finalization; use Ada.Finalization;
  70. with Ada.Strings.Maps; use Ada.Strings.Maps;
  71. with Ada.Text_IO;      use Ada.Text_IO;
  72.  
  73. package GNAT.Spitbol.Patterns is
  74. pragma Elaborate_Body (Patterns);
  75.  
  76.    -------------------------------
  77.    -- Pattern Matching Tutorial --
  78.    -------------------------------
  79.  
  80.    --  A pattern matching operation (a call to one of the Match subprograms)
  81.    --  takes a subject string and a pattern, and optionally a replacement
  82.    --  string. The replacement string option is only allowed if the subject
  83.    --  is a variable.
  84.  
  85.    --  The pattern is matched against the subject string, and either the
  86.    --  match fails, or it succeeds matching a contiguous substring. If a
  87.    --  replacement string is specified, then the subject string is modified
  88.    --  by replacing the matched substring with the given replacement.
  89.  
  90.  
  91.    --  Concatenation and Alternation
  92.    --  =============================
  93.  
  94.    --    A pattern consists of a series of pattern elements. The pattern is
  95.    --    built up using either the concatenation operator:
  96.  
  97.    --       A & B
  98.  
  99.    --    which means match A followed immediately by matching B, or the
  100.    --    alternation operator:
  101.  
  102.    --       A or B
  103.  
  104.    --    which means first attempt to match A, and then if that does not
  105.    --    succeed, match B.
  106.  
  107.    --    There is full backtracking, which means that if a given pattern
  108.    --    element fails to match, then previous alternatives are matched.
  109.    --    For example if we have the pattern:
  110.  
  111.    --      (A or B) & (C or D) & (E or F)
  112.  
  113.    --    First we attempt to match A, if that succeeds, then we go on to try
  114.    --    to match C, and if that succeeds, we go on to try to match E. If E
  115.    --    fails, then we try F. If F fails, then we go back and try matching
  116.    --    D instead of C. Let's make this explicit using a specific example,
  117.    --    and introducing the simplest kind of pattern element, which is a
  118.    --    literal string. The meaning of this pattern element is simply to
  119.    --    match the characters that correspond to the string characters. Now
  120.    --    let's rewrite the above pattern form with specific string literals
  121.    --    as the pattern elements:
  122.  
  123.    --      ("ABC" or "AB") & ("DEF" or "CDE") & ("GH" or "IJ")
  124.  
  125.    --    The following strings will be attempted in sequence:
  126.  
  127.    --       ABC . DEF . GH
  128.    --       ABC . DEF . IJ
  129.    --       ABC . CDE . GH
  130.    --       ABC . CDE . IJ
  131.    --       AB . DEF . GH
  132.    --       AB . DEF . IJ
  133.    --       AB . CDE . GH
  134.    --       AB . CDE . IJ
  135.  
  136.    --    Here we use the dot simply to separate the pieces of the string
  137.    --    matched by the three separate elements.
  138.  
  139.  
  140.    --  Moving the Start Point
  141.    --  ======================
  142.  
  143.    --    A pattern is not required to match starting at the first character
  144.    --    of the string, and is not required to match to the end of the string.
  145.    --    The first attempt does indeed attempt to match starting at the first
  146.    --    character of the string, trying all the possible alternatives. But
  147.    --    if all alternatives fail, then the starting point of the match is
  148.    --    moved one character, and all possible alternatives are attempted at
  149.    --    the new anchor point.
  150.  
  151.    --    The entire match fails only when every possible starting point has
  152.    --    been attempted. As an example, suppose that we had the subject
  153.    --    string
  154.  
  155.    --      "ABABCDEIJKL"
  156.  
  157.    --    matched using the pattern in the previous example:
  158.  
  159.    --      ("ABC" or "AB") & ("DEF" or "CDE") & ("GH" or "IJ")
  160.  
  161.    --    would succeed, afer two anchor point moves:
  162.  
  163.    --      "ABABCDEIJKL"
  164.    --         ^^^^^^^
  165.    --         matched
  166.    --         section
  167.  
  168.    --    This mode of pattern matching is called the unanchored mode. It is
  169.    --    also possible to put the pattern matcher into anchored mode by
  170.    --    setting the global variable Anchored_Mode to True. This will cause
  171.    --    all subsequent matches to be performed in anchored mode, where the
  172.    --    match is required to start at the first character.
  173.  
  174.    --    We will also see later how the effect of an anchored match can be
  175.    --    obtained for a single specified anchor point if this is desired.
  176.  
  177.  
  178.    --  Other Pattern Elements
  179.    --  ======================
  180.  
  181.    --    In addition to strings (or single characters), there are many special
  182.    --    pattern elements that correspond to special predefined alternations:
  183.  
  184.    --      Arb       Matches any string. First it matches the null string, and
  185.    --                then on a subsequent failure, matches one character, and
  186.    --                then two characters, and so on. It only fails if the
  187.    --                entire remaining string is matched.
  188.  
  189.    --      Bal       Matches a non-empty string that is parentheses balanced
  190.    --                with respect to ordinary () characters. Examples of
  191.    --                balanced strings are "ABC", "A((B)C)", and "A(B)C(D)E".
  192.    --                Bal matches the shortest possible balanced string on the
  193.    --                first attempt, and if there is a subsequent failure,
  194.    --                attempts to extend the string.
  195.  
  196.    --      Cancel    Immediately aborts the entire pattern match, signalling
  197.    --                failure. This is a specialized pattern element, which is
  198.    --                useful in conjunction with some of the special pattern
  199.    --                elements that have side effects.
  200.  
  201.    --      Fail      The null alternation. Matches no possible strings, so it
  202.    --                always signals failure. This is a specialized pattern
  203.    --                element, which is useful in conjunction with some of the
  204.    --                special pattern elements that have side effects.
  205.  
  206.    --      Fence     Matches the null string at first, and then if a failure
  207.    --                causes alternatives to be sought, aborts the match (like
  208.    --                a Cancel). Note that using Fence at the start of a pattern
  209.    --                has the same effect as matching in anchored mode.
  210.  
  211.    --      Rest      Matches from the current point to the last character in
  212.    --                the string. This is a specialized pattern element, which
  213.    --                is useful in conjunction with some of the special pattern
  214.    --                elements that have side effects.
  215.  
  216.    --      Succeed   Repeatedly matches the null string (it is equivalent to
  217.    --                the alternation ("" or "" or "" ....). This is a special
  218.    --                pattern element, which is useful in conjunction with some
  219.    --                of the special pattern elements that have side effects.
  220.  
  221.  
  222.    --  Pattern Construction Functions
  223.    --  ==============================
  224.  
  225.    --    The following functions construct additional pattern elements
  226.  
  227.    --      Any(S)    Where S is a string, matches a single character that is
  228.    --                any one of the characters in S. Fails if the current
  229.    --                character is not one of the given set of characters.
  230.  
  231.    --      Arbno(P)  Where P is any pattern, matches any number of instances
  232.    --                of the pattern, starting with zero occurrences. It is
  233.    --                thus equivalent to ("" or (P & ("" or (P & ("" ....)))).
  234.    --                The pattern P may contain any number of pattern elements
  235.    --                including the use of alternatiion and concatenation.
  236.  
  237.    --      Break(S)  Where S is a string, matches a string of zero or more
  238.    --                characters up to but not including a break character
  239.    --                that is one of the characters given in the string S.
  240.    --                Can match the null string, but cannot match the last
  241.    --                character in the string, since a break character is
  242.    --                required to be present.
  243.  
  244.    --      BreakX(S) Where S is a string, behaves exactly like Break(S) when
  245.    --                it first matches, but if a string is successfully matched,
  246.    --                then a susequent failure causes an attempt to extend the
  247.    --                matched string.
  248.  
  249.    --      Fence(P)  Where P is a pattern, attempts to match the pattern P
  250.    --                including trying all possible alternatives of P. If none
  251.    --                of these alternatives succeeds, then the Fence pattern
  252.    --                fails. If one alternative succeeds, then the pattern
  253.    --                match proceeds, but on a subsequent failure, no attempt
  254.    --                is made to search for alternative matches of P. The
  255.    --                pattern P may contain any number of pattern elements
  256.    --                including the use of alternatiion and concatenation.
  257.  
  258.    --      Len(N)    Where N is a natural number, matches the given number of
  259.    --                characters. For example, Len(10) matches any string that
  260.    --                is exactly ten characters long.
  261.  
  262.    --      NotAny(S) Where S is a string, matches a single character that is
  263.    --                not one of the characters of S. Fails if the current
  264.    --                characer is one of the given set of characters.
  265.  
  266.    --      NSpan(S)  Where S is a string, matches a string of zero or more
  267.    --                characters that is among the characters given in the
  268.    --                string. Always matches the longest possible such string.
  269.    --                Always succeeds, since it can match the null string.
  270.  
  271.    --      Pos(N)    Where N is a natural number, matches the null string
  272.    --                if exactly N characters have been matched so far, and
  273.    --                otherwise fails.
  274.  
  275.    --      Rpos(N)   Where N is a natural number, matches the null string
  276.    --                if exactly N characters remain to be matched, and
  277.    --                otherwise fails.
  278.  
  279.    --      Rtab(N)   Where N is a natural number, matches characters from
  280.    --                the current position until exactly N characters remain
  281.    --                to be matched in the string. Fails if fewer than N
  282.    --                unmatched characters remain in the string.
  283.  
  284.    --      Tab(N)    Where N is a natural number, matches characters from
  285.    --                the current position until exactly N characters have
  286.    --                been matched in all. Fails if more than N characters
  287.    --                have already been matched.
  288.  
  289.    --      Span(S)   Where S is a string, matches a string of one or more
  290.    --                characters that is among the characters given in the
  291.    --                string. Always matches the longest possible such string.
  292.    --                Fails if the current character is not one of the given
  293.    --                set of characters.
  294.  
  295.    --  Recursive Pattern Matching
  296.    --  ==========================
  297.  
  298.    --    The plus operator (+P) where P is a pattern variable, creates
  299.    --    a recursive pattern that will, at pattern matching time, follow
  300.    --    the pointer to obtain the referenced pattern, and then match this
  301.    --    pattern. This may be used to construct recursive patterns. Consider
  302.    --    for example:
  303.  
  304.    --       P := ("A" or ("B" & (+P)))
  305.  
  306.    --    On the first attempt, this pattern attempts to match the string "A".
  307.    --    If this fails, then the alternative matches a "B", followed by an
  308.    --    attempt to match P again. This second attempt first attempts to
  309.    --    match "A", and so on. The result is a pattern that will match a
  310.    --    string of B's followed by a single A.
  311.  
  312.    --    This particular example could simply be written as NSpan('B') & 'A',
  313.    --    but the use of recursive patterns in the general case can construct
  314.    --    complex patterns which could not otherwise be built.
  315.  
  316.  
  317.    --  Pattern Assignment Operations
  318.    --  =============================
  319.  
  320.    --    In addition to the overall result of a pattern match, which indicates
  321.    --    success or failure, it is often useful to be able to keep track of
  322.    --    the pieces of the subject string that are matched by individual
  323.    --    pattern elements, or subsections of the pattern.
  324.  
  325.    --    The pattern assignment operators allow this capability. The first
  326.    --    form is the immediate assignment:
  327.  
  328.    --       P * S
  329.  
  330.    --    Here P is an arbitrary pattern, and S is a variable of type VString
  331.    --    that will be set to the substring matched by P. This assignment
  332.    --    happens during pattern matching, so if P matches more than once,
  333.    --    then the assignment happens more than once.
  334.  
  335.    --    The deferred assignment operation:
  336.  
  337.    --      P ** S
  338.  
  339.    --    avoids these multiple assignments by deferring the assignment to the
  340.    --    end of the match. If the entire match is successful, and if the
  341.    --    pattern P was part of the successful match, then at the end of the
  342.    --    matching operation the assignment to S of the string matching P is
  343.    --    performed.
  344.  
  345.    --    The cursor assignment operation:
  346.  
  347.    --      Setcur(N'Access)
  348.  
  349.    --    assigns the current cursor position to the natural variable N. The
  350.    --    cursor position is defined as the count of characters that have been
  351.    --    matched so far (including any start point moves).
  352.  
  353.    --    Finally the operations * and ** may be used with values of type
  354.    --    Text_IO.File_Access. The effect is to do a Put_Line operation of
  355.    --    the matched substring. These are particularly useful in debugging
  356.    --    pattern matches.
  357.  
  358.  
  359.    --  Deferred Matching
  360.    --  =================
  361.  
  362.    --    The pattern construction functions (such as Len and Any) all permit
  363.    --    the use of pointers to natural or string values, or functions that
  364.    --    return natural or string values. These forms cause the actual value
  365.    --    to be obtained at pattern matching time. This allows interesting
  366.    --    possibilities for constructing dynamic patterns as illustrated in
  367.    --    the examples section.
  368.  
  369.    --    In addition the (+S) operator may be used where S is a pointer to
  370.    --    string or function returning string, with a similar deferred effect.
  371.  
  372.    --    A special use of deferred matching is the construction of predicate
  373.    --    functions. The element (+P) where P is an access to a function that
  374.    --    returns a Boolean value, causes the function to be called at the
  375.    --    time the element is matched. If the function returns True, then the
  376.    --    null string is matched, if the function returns False, then failure
  377.    --    is signalled and previous alternatives are sought.
  378.  
  379.    --  Deferred Replacement
  380.    --  ====================
  381.  
  382.    --    The simple model given for pattern replacement (where the matched
  383.    --    substring is replaced by the string given as the third argument to
  384.    --    Match) works fine in simple cases, but this approach does not work
  385.    --    in the case where the expression used as the replacement string is
  386.    --    dependent on values set by the match.
  387.  
  388.    --    For example, suppose we want to find an instance of a parenthesized
  389.    --    character, and replace the parentheses with square brackets. At first
  390.    --    glance it would seem that:
  391.  
  392.    --      Match (Subject, '(' & Len (1) * Char & ')', '[' & Char & ']');
  393.  
  394.    --    would do the trick, but that does not work, because the third
  395.    --    argument to Match gets evaluated too early, before the call to
  396.    --    Match, and before the pattern match has had a chance to set Char.
  397.  
  398.    --    To solve this problem we provide the deferred replacement capability.
  399.    --    With this approach, which of course is only needed if the pattern
  400.    --    involved has side effects, is to do the match in two stages. The
  401.    --    call to Match sets a pattern result in a variable of the private
  402.    --    type Match_Result, and then a subsequent Replace operation uses
  403.    --    this Match_Result object to perform the required replacement.
  404.  
  405.    --    Using this approach, we can now write the above operation properly
  406.    --    in a manner that will work:
  407.  
  408.    --      M : Match_Result;
  409.    --      ...
  410.    --      Match (Subject, '(' & Len (1) * Char & ')', M);
  411.    --      Replace (M, '[' & Char & ']');
  412.  
  413.    --    As with other Match cases, there is a function and procedure form
  414.    --    of this match call. A call to Replace after a failed match has no
  415.    --    effect. Note that Subject should not be modified between the calls.
  416.  
  417.    --  Examples of Pattern Matching
  418.    --  ============================
  419.  
  420.    --    First a simple example of the use of pattern replacement to remove
  421.    --    a line number from the start of a string. We assume that the line
  422.    --    number has the form of a string of decimal digits followed by a
  423.    --    period, followed by one or more spaces.
  424.  
  425.    --       Digs : constant Pattern := Span("0123456789");
  426.  
  427.    --       Lnum : constant Pattern := Pos(0) & Digs & '.' & Span(' ');
  428.  
  429.    --    Now to use this pattern we simply do a match with a replacement:
  430.  
  431.    --       Match (Line, Lnum, "");
  432.  
  433.    --    which replaces the line number by the null string. Note that it is
  434.    --    also possible to use an Ada.Strings.Maps.Character_Set value as an
  435.    --    argument to Span and similar functions, and in particular all the
  436.    --    useful constants 'in Ada.Strings.Maps.Constants are available. This
  437.    --    means that we could define Digs as:
  438.  
  439.    --       Digs : constant Pattern := Span(Decimal_Digit_Set);
  440.  
  441.    --    The style we use here, of defining constant patterns and then using
  442.    --    them is typical. It is possible to build up patterns dynamically,
  443.    --    but it is usually more efficient to build them in pieces in advance
  444.    --    using constant declarations. Note in particular that although it is
  445.    --    possible to construct a pattern directly as an argument for the
  446.    --    Match routine, it is much more efficient to preconstruct the pattern
  447.    --    as we did in this example.
  448.  
  449.    --    Now let's look at the use of pattern assignment to break a
  450.    --    string into sections. Suppose that the input string has two
  451.    --    unsigned decimal integers, separated by spaces or a comma,
  452.    --    with spaces allowed anywhere. Then we can isolate the two
  453.    --    numbers with the following pattern:
  454.  
  455.    --       Num1, Num2 : aliased VString;
  456.  
  457.    --       B : constant Pattern := NSpan(' ');
  458.  
  459.    --       N : constant Pattern := Span("0123456789");
  460.  
  461.    --       T : constant Pattern :=
  462.    --             NSpan(' ') & N * Num1 & Span(" ,") & N * Num2;
  463.  
  464.    --    The match operation Match (" 124, 257  ", T) would assign the
  465.    --    string 124 to Num1 and the string 257 to Num2.
  466.  
  467.    --    Now let's see how more complex elements can be built from the
  468.    --    set of primitive elements. The following pattern matches strings
  469.    --    that have the syntax of Ada 95 based literals:
  470.  
  471.    --       Digs  : constant Pattern := Span(Decimal_Digit_Set);
  472.    --       UDigs : constant Pattern := Digs & Arbno('_' & Digs);
  473.  
  474.    --       Edig  : constant Pattern := Span(Hexadecimal_Digit_Set);
  475.    --       UEdig : constant Pattern := Edig & Arbno('_' & Edig);
  476.  
  477.    --       Bnum  : constant Pattern := Udigs & '#' & UEdig & '#';
  478.  
  479.    --    A match against Bnum will now match the desired strings, e.g.
  480.    --    it will match 16#123_abc#, but not a#b#. However, this pattern
  481.    --    is not quite complete, since it does not allow colons to replace
  482.    --    the pound signs. The following is more complete:
  483.  
  484.    --       Bchar : constant Pattern := Any("#:");
  485.    --       Bnum  : constant Pattern := Udigs & Bchar & UEdig & Bchar;
  486.  
  487.    --    but that is still not quite right, since it allows # and : to be
  488.    --    mixed, and they are supposed to be used consistently. We solve
  489.    --    this by using a deferred match.
  490.  
  491.    --       Temp  : aliased VString;
  492.  
  493.    --       Bnum  : constant Pattern :=
  494.    --                 Udigs & Bchar * Temp & UEdig & (+Temp)
  495.  
  496.    --    Here the first instance of the base character is stored in Temp, and
  497.    --    then later in the pattern we rematch the value that was assigned.
  498.  
  499.    --    For an example of a recursive pattern, let's define a pattern
  500.    --    that is like the built in Bal, but the string matched is balanced
  501.    --    with respect to square brackets or curly brackets.
  502.  
  503.    --    The language for such strings might be defined in extended BNF as
  504.  
  505.    --      ELEMENT ::= <any character other than [] or {}>
  506.    --                  | '[' BALANCED_STRING ']'
  507.    --                  | '{' BALANCED_STRING '}'
  508.  
  509.    --      BALANCED_STRING ::= ELEMENT {ELEMENT}
  510.  
  511.    --    Here we use {} to indicate zero or more occurrences of a term, as
  512.    --    is common practice in extended BNF. Now we can translate the above
  513.    --    BNF into recursive patterns as follows:
  514.  
  515.    --      Element, Balanced_String : aliased Pattern;
  516.    --      .
  517.    --      .
  518.    --      .
  519.    --      Element := NotAny ("[]{}")
  520.    --                   or
  521.    --                 ('[' & (+Balanced_String) & ']')
  522.    --                   or
  523.    --                 ('{' & (+Balanced_String) & '}');
  524.  
  525.    --      Balanced_String := Element & Arbno (Element);
  526.  
  527.    --    Note the important use of + here to refer to a pattern not yet
  528.    --    defined. Note also that we use assignments precisely because we
  529.    --    cannot refer to as yet undeclared variables in initializations.
  530.  
  531.    --    Now that this pattern is constructed, we can use it as though it
  532.    --    were a new primitive pattern element, and for example, the match:
  533.  
  534.    --      Match ("xy[ab{cd}]", Balanced_String * Current_Output & Fail);
  535.  
  536.    --    will generate the output:
  537.  
  538.    --       x
  539.    --       xy
  540.    --       xy[ab{cd}]
  541.    --       y
  542.    --       y[ab{cd}]
  543.    --       [ab{cd}]
  544.    --       a
  545.    --       ab
  546.    --       ab{cd}
  547.    --       b
  548.    --       b{cd}
  549.    --       {cd}
  550.    --       c
  551.    --       cd
  552.    --       d
  553.  
  554.    --    Note that the function of the fail here is simply to force the
  555.    --    pattern Balanced_String to match all possible alternatives. Studying
  556.    --    the operation of this pattern in detail is highly instructive.
  557.  
  558.    --    Finally we give a rather elaborate example of the use of deferred
  559.    --    matching. The following declarations build up a pattern which will
  560.    --    find the longest string of decimal digits in the subject string.
  561.  
  562.    --       Max, Cur : VString;
  563.    --       Loc      : Natural;
  564.  
  565.    --       function GtS return Boolean is
  566.    --       begin
  567.    --          return Length (Cur) > Length (Max);
  568.    --       end GtS;
  569.  
  570.    --       Digit : constant Character_Set := Decimal_Digit_Set;
  571.  
  572.    --       Digs  : constant Pattern := Span(Digit);
  573.  
  574.    --       Find : constant Pattern :=
  575.    --         "" * Max & Fence            & -- initialize Max to null
  576.    --         BreakX (Digit)              & -- scan looking for digits
  577.    --         ((Span(Digit) * Cur         & -- assign next string to Cur
  578.    --          (+GtS'Unrestricted_Access) & -- check size(Cur) > Size(Max)
  579.    --          Setcur(Loc'Access))          -- if so, save location
  580.    --                   * Max)            & -- and assign to Max
  581.    --         Fail;                         -- seek all alternatives
  582.  
  583.    --    As we see from the comments here, complex patterns like this take
  584.    --    on aspects of sequential programs. In fact they are sequential
  585.    --    programs with general backtracking. In this pattern, we first use
  586.    --    a pattern assignment that matches null and assigns it to Max, so
  587.    --    that it is initialized for the new match. Now BreakX scans to the
  588.    --    next digit. Arb would do here, but BreakX will be more efficient.
  589.    --    Once we have found a digit, we scan out the longest string of
  590.    --    digits with Span, and assign it to Cur. The deferred call to GtS
  591.    --    tests if the string we assigned to Cur is the longest so far. If
  592.    --    not, then failure is signalled, and we seek alternatives (this
  593.    --    means that BreakX will extend and look for the next digit string).
  594.    --    If the call to GtS succeeds then the matched string is assigned
  595.    --    as the largest string so far into Max and its location is saved
  596.    --    in Loc. Finally Fail forces the match to fail and seek alternatives,
  597.    --    so that the entire string is searched.
  598.  
  599.    --    If the pattern Find is matched against a string, the variable Max
  600.    --    at the end of the pattern will have the longest string of digits,
  601.    --    and Loc will be the starting character location of the string. For
  602.    --    example, Match("ab123cd4657ef23", Find) will assign "4657" to Max
  603.    --    and 11 to Loc (indicating that the string ends with the eleventh
  604.    --    character of the string).
  605.  
  606.    --    Note: the use of Unrestricted_Access to reference GtS will not
  607.    --    be needed if GtS is defined at the outer level, but definitely
  608.    --    will be necessary if GtS is a nested function (in which case of
  609.    --    course the scope of the pattern Find will be restricted to this
  610.    --    nested scope, and this cannot be checked, i.e. use of the pattern
  611.    --    outside this scope is erroneous). Generally it is a good idea to
  612.    --    define patterns and the functions they call at the outer level
  613.    --    where possible, to avoid such problems.
  614.  
  615.  
  616.    --  Correspondence with Pattern Matching in SPITBOL
  617.    --  ===============================================
  618.  
  619.    --    Generally the Ada syntax and names correspond closely to SPITBOL
  620.    --    syntax for pattern matching construction.
  621.  
  622.    --      The basic pattern construction operators are renamed as follows:
  623.  
  624.    --          Spitbol     Ada
  625.  
  626.    --          (space)      &
  627.    --             |         or
  628.    --             $         *
  629.    --             .         **
  630.  
  631.    --      The Ada operators were chosen so that the relative precedences of
  632.    --      these operators corresponds to that of the Spitbol operators, but
  633.    --      as always, the use of parentheses is advisable to clarify.
  634.  
  635.    --    The pattern construction operators all have similar names except for
  636.  
  637.    --          Spitbol      Ada
  638.  
  639.    --          Abort        Cancel
  640.    --          Rem          Rest
  641.  
  642.    --    where we have clashes with Ada reserved names.
  643.  
  644.    --    Ada requires the use of 'Access to refer to functions used in the
  645.    --    pattern match, and often the use of 'Unrestricted_Access may be
  646.    --    necessary to get around the scope restrictions if the functions
  647.    --    are not declared at the outer level.
  648.  
  649.    --    The actual pattern matching syntax is modified in Ada as follows:
  650.  
  651.    --          Spitbol      Ada
  652.  
  653.    --          X Y          Match (X, Y);
  654.    --          X Y = Z      Match (X, Y, Z);
  655.  
  656.    --    and pattern failure is indicated by returning a Boolean result from
  657.    --    the Match function (True for success, False for failure).
  658.  
  659.    -----------------------
  660.    -- Type Declarations --
  661.    -----------------------
  662.  
  663.    type Pattern is private;
  664.    --  Type representing a pattern. This package provides a complete set of
  665.    --  operations for constructing patterns that can be used in the pattern
  666.    --  matching operations provided.
  667.  
  668.    type Boolean_Func is access function return Boolean;
  669.    --  General Boolean function type. When this type is used as a formal
  670.    --  parameter type in this package, it indicates a deferred predicate
  671.    --  pattern. The function will be called when the pattern element is
  672.    --  matched and failure signalled if False is returned.
  673.  
  674.    type Natural_Func is access function return Natural;
  675.    --  General Natural function type. When this type is used as a formal
  676.    --  parameter type in this package, it indicates a deferred pattern.
  677.    --  The function will be called when the pattern element is matched
  678.    --  to obtain the currently referenced Natural value.
  679.  
  680.    type VString_Func is access function return VString;
  681.    --  General VString function type. When this type is used as a formal
  682.    --  parameter type in this package, it indicates a deferred pattern.
  683.    --  The function will be called when the pattern element is matched
  684.    --  to obtain the currently referenced string value.
  685.  
  686.    subtype PString is String;
  687.    --  This subtype is used in the remainder of the package to indicate a
  688.    --  formal parameter that is converted to its corresponding pattern,
  689.    --  i.e. a pattern that matches the characters of the string.
  690.  
  691.    subtype PChar is Character;
  692.    --  Similarly, this subtype is used in the remainder of the package to
  693.    --  indicate a formal parameter that is converted to its corresponding
  694.    --  pattern, i.e. a pattern that matches this one character.
  695.  
  696.    subtype VString_Var is VString;
  697.    subtype Pattern_Var is Pattern;
  698.    --  These synonyms are used as formal parameter types to a function where,
  699.    --  if the language allowed, we would use in out parameters, but we are
  700.    --  not allowed to have in out parameters for functions. Instead we pass
  701.    --  actuals which must be variables, and with a bit of trickery in the
  702.    --  body, manage to interprete them properly as though they were indeed
  703.    --  in out parameters.
  704.  
  705.    --------------------------------
  706.    -- Basic Pattern Construction --
  707.    --------------------------------
  708.  
  709.    function "&"  (L : Pattern; R : Pattern) return Pattern;
  710.    function "&"  (L : PString; R : Pattern) return Pattern;
  711.    function "&"  (L : Pattern; R : PString) return Pattern;
  712.    function "&"  (L : PChar;   R : Pattern) return Pattern;
  713.    function "&"  (L : Pattern; R : PChar)   return Pattern;
  714.  
  715.    --  Pattern concatenation. Matches L followed by R.
  716.  
  717.    function "or" (L : Pattern; R : Pattern) return Pattern;
  718.    function "or" (L : PString; R : Pattern) return Pattern;
  719.    function "or" (L : Pattern; R : PString) return Pattern;
  720.    function "or" (L : PString; R : PString) return Pattern;
  721.    function "or" (L : PChar;   R : Pattern) return Pattern;
  722.    function "or" (L : Pattern; R : PChar)   return Pattern;
  723.    function "or" (L : PChar;   R : PChar)   return Pattern;
  724.    function "or" (L : PString; R : PChar)   return Pattern;
  725.    function "or" (L : PChar;   R : PString) return Pattern;
  726.    --  Pattern alternation. Creates a pattern that will first try to match
  727.    --  L and then on a subsequent failure, attempts to match R instead.
  728.  
  729.    ----------------------------------
  730.    -- Pattern Assignment Functions --
  731.    ----------------------------------
  732.  
  733.    function "*" (P : Pattern; Var : VString_Var)  return Pattern;
  734.    function "*" (P : PString; Var : VString_Var)  return Pattern;
  735.    function "*" (P : PChar;   Var : VString_Var)  return Pattern;
  736.    --  Matches P, and if the match succeeds, assigns the matched substring
  737.    --  to the given VString variable S. This assignment happens as soon as
  738.    --  the substring is matched, and if the pattern P1 is matched more than
  739.    --  once during the course of the match, then the assignment will occur
  740.    --  more than once.
  741.  
  742.    function "**" (P : Pattern; Var : VString_Var) return Pattern;
  743.    function "**" (P : PString; Var : VString_Var) return Pattern;
  744.    function "**" (P : PChar;   Var : VString_Var) return Pattern;
  745.    --  Like "*" above, except that the assignment happens at most once
  746.    --  after the entire match is completed successfully. If the match
  747.    --  fails, then no assignment takes place.
  748.  
  749.    ----------------------------------
  750.    -- Deferred Matching Operations --
  751.    ----------------------------------
  752.  
  753.    function "+" (Str : VString_Var)  return Pattern;
  754.    --  Here Str must be a VString variable. This function constructs a
  755.    --  pattern which at pattern matching time will access the current
  756.    --  value of this variable, and match against these characters.
  757.  
  758.    function "+" (Str : VString_Func) return Pattern;
  759.    --  Constructs a pattern which at pattern matching time calls the given
  760.    --  function, and then matches against the string or character value
  761.    --  that is returned by the call.
  762.  
  763.    function "+" (P : Pattern_Var)    return Pattern;
  764.    --  Here P must be a Pattern variable. This function constructs a
  765.    --  pattern which at pattern matching time will access the current
  766.    --  value of this variable, and match against the pattern value.
  767.  
  768.    function "+" (P : Boolean_Func)   return Pattern;
  769.    --  Constructs a predicate pattern function that at pattern matching time
  770.    --  calls the given function. If True is returned, then the pattern matches.
  771.    --  If False is returned, then failure is signalled.
  772.  
  773.    --------------------------------
  774.    -- Pattern Building Functions --
  775.    --------------------------------
  776.  
  777.    function Arb                                             return Pattern;
  778.    --  Constructs a pattern that will match any string. On the first attempt,
  779.    --  the pattern matches a null string, then on each successive failure, it
  780.    --  matches one more character, and only fails if matching the entire rest
  781.    --  of the string.
  782.  
  783.    function Arbno  (P : Pattern)                            return Pattern;
  784.    function Arbno  (P : PString)                            return Pattern;
  785.    function Arbno  (P : PChar)                              return Pattern;
  786.    --  Pattern repetition. First matches null, then on a subsequent failure
  787.    --  attempts to match an additional instance of the given pattern.
  788.    --  Equivalent to (but more efficient than) P & ("" or (P & ("" or ...
  789.  
  790.    function Any    (Str : String)                           return Pattern;
  791.    function Any    (Str : VString)                          return Pattern;
  792.    function Any    (Str : Character)                        return Pattern;
  793.    function Any    (Str : Character_Set)                    return Pattern;
  794.    function Any    (Str : access VString)                   return Pattern;
  795.    function Any    (Str : VString_Func)                     return Pattern;
  796.    --  Constructs a pattern that matches a single character that is one of
  797.    --  the characters in the given argument. The pattern fails if the current
  798.    --  character is not in Str.
  799.  
  800.    function Bal                                             return Pattern;
  801.    --  Constructs a pattern that will match any non-empty string that is
  802.    --  parentheses balanced with respect to the normal parentheses characters.
  803.    --  Attempts to extend the string if a subsequent failure occurs.
  804.  
  805.    function Break  (Str : String)                           return Pattern;
  806.    function Break  (Str : VString)                          return Pattern;
  807.    function Break  (Str : Character)                        return Pattern;
  808.    function Break  (Str : Character_Set)                    return Pattern;
  809.    function Break  (Str : access VString)                   return Pattern;
  810.    function Break  (Str : VString_Func)                     return Pattern;
  811.    --  Constructs a pattern that matches a (possibly null) string which
  812.    --  is immediately followed by a character in the given argument. This
  813.    --  character is not part of the matched string. The pattern fails if
  814.    --  the remaining characters to be matched do not include any of the
  815.    --  characters in Str.
  816.  
  817.    function BreakX (Str : String)                           return Pattern;
  818.    function BreakX (Str : VString)                          return Pattern;
  819.    function BreakX (Str : Character)                        return Pattern;
  820.    function BreakX (Str : Character_Set)                    return Pattern;
  821.    function BreakX (Str : access VString)                   return Pattern;
  822.    function BreakX (Str : VString_Func)                     return Pattern;
  823.    --  Like Break, but the pattern attempts to extend on a failure to find
  824.    --  the next occurrence of a character in Str, and only fails when the
  825.    --  last such instance causes a failure.
  826.  
  827.    function Cancel                                          return Pattern;
  828.    --  Constructs a pattern that immediately aborts the entire match
  829.  
  830.    function Fail                                            return Pattern;
  831.    --  Constructs a pattern that always fails.
  832.  
  833.    function Fence                                           return Pattern;
  834.    --  Constructs a pattern that matches null on the first attempt, and then
  835.    --  causes the entire match to be aborted if a subsequent failure occurs.
  836.  
  837.    function Fence  (P : Pattern)                            return Pattern;
  838.    --  Constructs a pattern that first matches P. if P fails, then the
  839.    --  constructed pattern fails. If P succeeds, then the match proceeds,
  840.    --  but if subsequent failure occurs, alternatives in P are not sought.
  841.    --  The idea of Fence is that each time the pattern is matched, just
  842.    --  one attempt is made to match P, without trying alternatives.
  843.  
  844.    function Len    (Count : Natural)                        return Pattern;
  845.    function Len    (Count : access Natural)                 return Pattern;
  846.    function Len    (Count : Natural_Func)                   return Pattern;
  847.    --  Constructs a pattern that matches exactly the given number of
  848.    --  characters. The pattern fails if fewer than this number of characters
  849.    --  remain to be matched in the string.
  850.  
  851.    function NotAny (Str : String)                           return Pattern;
  852.    function NotAny (Str : VString)                          return Pattern;
  853.    function NotAny (Str : Character)                        return Pattern;
  854.    function NotAny (Str : Character_Set)                    return Pattern;
  855.    function NotAny (Str : access VString)                   return Pattern;
  856.    function NotAny (Str : VString_Func)                     return Pattern;
  857.    --  Constructs a pattern that matches a single character that is not
  858.    --  one of the characters in the given argument. The pattern Fails if
  859.    --  the current character is in Str.
  860.  
  861.    function NSpan  (Str : String)                           return Pattern;
  862.    function NSpan  (Str : VString)                          return Pattern;
  863.    function NSpan  (Str : Character)                        return Pattern;
  864.    function NSpan  (Str : Character_Set)                    return Pattern;
  865.    function NSpan  (Str : access VString)                   return Pattern;
  866.    function NSpan  (Str : VString_Func)                     return Pattern;
  867.    --  Constructs a pattern that matches the longest possible string
  868.    --  consisting entirely of characters from the given argument. The
  869.    --  string may be empty, so this pattern always succeeds.
  870.  
  871.    function Pos    (Count : Natural)                        return Pattern;
  872.    function Pos    (Count : access Natural)                 return Pattern;
  873.    function Pos    (Count : Natural_Func)                   return Pattern;
  874.    --  Constructs a pattern that matches the null string if exactly Count
  875.    --  characters have already been matched, and otherwise fails.
  876.  
  877.    function Rest                                            return Pattern;
  878.    --  Constructs a pattern that always succeeds, matching the remaining
  879.    --  unmatched characters in the pattern.
  880.  
  881.    function Rpos   (Count : Natural)                        return Pattern;
  882.    function Rpos   (Count : access Natural)                 return Pattern;
  883.    function Rpos   (Count : Natural_Func)                   return Pattern;
  884.    --  Constructs a pattern that matches the null string if exactly Count
  885.    --  characters remain to be matched in the string, and otherwise fails.
  886.  
  887.    function Rtab   (Count : Natural)                        return Pattern;
  888.    function Rtab   (Count : access Natural)                 return Pattern;
  889.    function Rtab   (Count : Natural_Func)                   return Pattern;
  890.    --  Constructs a pattern that matches from the current location until
  891.    --  exactly Count characters remain to be matched in the string. The
  892.    --  pattern fails if fewer than Count characters remain to be matched.
  893.  
  894.    function Setcur (Var : access Natural)                   return Pattern;
  895.    --  Constructs a pattern that matches the null string, and assigns the
  896.    --  current cursor position in the string. This value is the number of
  897.    --  characters matched so far. So it is zero at the start of the match.
  898.  
  899.    function Span   (Str : String)                           return Pattern;
  900.    function Span   (Str : VString)                          return Pattern;
  901.    function Span   (Str : Character)                        return Pattern;
  902.    function Span   (Str : Character_Set)                    return Pattern;
  903.    function Span   (Str : access VString)                   return Pattern;
  904.    function Span   (Str : VString_Func)                     return Pattern;
  905.    --  Constructs a pattern that matches the longest possible string
  906.    --  consisting entirely of characters from the given argument. The
  907.    --  string cannot be empty , so the pattern fails if the current
  908.    --  character is not one of the characters in Str.
  909.  
  910.    function Succeed                                         return Pattern;
  911.    --  Constructs a pattern that succeeds matching null, both on the first
  912.    --  attempt, and on any rematch attempt, i.e. it is equivalent to an
  913.    --  infinite alternation of null strings.
  914.  
  915.    function Tab    (Count : Natural)                        return Pattern;
  916.    function Tab    (Count : access Natural)                 return Pattern;
  917.    function Tab    (Count : Natural_Func)                   return Pattern;
  918.    --  Constructs a pattern that from the current location until Count
  919.    --  characters have been matched. The pattern fails if more than Count
  920.    --  characters have already been matched.
  921.  
  922.    ---------------------------------
  923.    -- Pattern Matching Operations --
  924.    ---------------------------------
  925.  
  926.    --  The Match function performs an actual pattern matching operation.
  927.    --  The versions with three parameters perform a match without modifying
  928.    --  the subject string and return a Boolean result indicating if the
  929.    --  match is successful or not. The Anchor parameter is set to True to
  930.    --  obtain an anchored match in which the pattern is required to match
  931.    --  the first character of the string. In an unanchored match, which is
  932.  
  933.    --  the default, successive attempts are made to match the given pattern
  934.    --  at each character of the subject string until a match succeeds, or
  935.    --  until all possibilities have failed.
  936.  
  937.    --  Note that pattern assignment functions in the pattern may generate
  938.    --  side effects, so these functions are not necessarily pure.
  939.  
  940.    Anchored_Mode : Boolean := False;
  941.    --  This global variable can be set True to cause all subsequent pattern
  942.    --  matches to operate in anchored mode. In anchored mode, no attempt is
  943.    --  made to move the anchor point, so that if the match succeeds it must
  944.    --  succeed starting at the first character. Note that the effect of
  945.    --  anchored mode may be achieved in individual pattern matches by using
  946.    --  Fence or Pos(0) at the start of the pattern.
  947.  
  948.    Pattern_Stack_Overflow : exception;
  949.    --  Exception raised if internal pattern matching stack overflows. This
  950.    --  is typically the result of runaway pattern recursion. If there is a
  951.    --  genuine case of stack overflow, then either the match must be broken
  952.    --  down into simpler steps, or the stack limit must be reset.
  953.  
  954.    Stack_Size : constant Positive := 2000;
  955.    --  Size used for internal pattern matching stack. Increase this size if
  956.    --  complex patterns cause Pattern_Stack_Overflow to be raised.
  957.  
  958.    --  Simple match functions. The subject is matched against the pattern.
  959.    --  Any immediate or deferred assignments or writes are executed, and
  960.    --  the returned value indicates whether or not the match succeeded.
  961.  
  962.    function Match
  963.      (Subject : VString;
  964.       Pat     : Pattern)
  965.       return    Boolean;
  966.  
  967.    function Match
  968.      (Subject : VString;
  969.       Pat     : PString)
  970.       return    Boolean;
  971.  
  972.    function Match
  973.      (Subject : String;
  974.       Pat     : Pattern)
  975.       return    Boolean;
  976.  
  977.    function Match
  978.      (Subject : String;
  979.       Pat     : PString)
  980.       return    Boolean;
  981.  
  982.    --  Replacement functions. The subject is matched against the pattern.
  983.    --  Any immediate or deferred assignments or writes are executed, and
  984.    --  the returned value indicates whether or not the match succeeded.
  985.    --  If the match succeeds, then the matched part of the subject string
  986.    --  is replaced by the given Replace string.
  987.  
  988.    function Match
  989.      (Subject : VString_Var;
  990.       Pat     : Pattern;
  991.       Replace : VString)
  992.       return    Boolean;
  993.  
  994.    function Match
  995.      (Subject : VString_Var;
  996.       Pat     : PString;
  997.       Replace : VString)
  998.       return    Boolean;
  999.  
  1000.    function Match
  1001.      (Subject : VString_Var;
  1002.       Pat     : Pattern;
  1003.       Replace : String)
  1004.       return    Boolean;
  1005.  
  1006.    function Match
  1007.      (Subject : VString_Var;
  1008.       Pat     : PString;
  1009.       Replace : String)
  1010.       return    Boolean;
  1011.  
  1012.    --  Simple match procedures. The subject is matched against the pattern.
  1013.    --  Any immediate or deferred assignments or writes are executed. No
  1014.    --  indication of success or failure is returned.
  1015.  
  1016.    procedure Match
  1017.      (Subject : VString;
  1018.       Pat     : Pattern);
  1019.  
  1020.    procedure Match
  1021.      (Subject : VString;
  1022.       Pat     : PString);
  1023.  
  1024.    procedure Match
  1025.      (Subject : String;
  1026.       Pat     : Pattern);
  1027.  
  1028.    procedure Match
  1029.      (Subject : String;
  1030.       Pat     : PString);
  1031.  
  1032.    --  Replacement procedures. The subject is matched against the pattern.
  1033.    --  Any immediate or deferred assignments or writes are executed. No
  1034.    --  indication of success or failure is returned. If the match succeeds,
  1035.    --  then the matched part of the subject string is replaced by the given
  1036.    --  Replace string.
  1037.  
  1038.    procedure Match
  1039.      (Subject : in out VString;
  1040.       Pat     : Pattern;
  1041.       Replace : VString);
  1042.  
  1043.    procedure Match
  1044.      (Subject : in out VString;
  1045.       Pat     : PString;
  1046.       Replace : VString);
  1047.  
  1048.    procedure Match
  1049.      (Subject : in out VString;
  1050.       Pat     : Pattern;
  1051.       Replace : String);
  1052.  
  1053.    procedure Match
  1054.      (Subject : in out VString;
  1055.       Pat     : PString;
  1056.       Replace : String);
  1057.  
  1058.    --  Deferred Replacement
  1059.  
  1060.    type Match_Result is private;
  1061.    --  Type used to record result of pattern match
  1062.  
  1063.    subtype Match_Result_Var is Match_Result;
  1064.    --  This synonyms is used as a formal parameter type to a function where,
  1065.    --  if the language allowed, we would use an in out parameter, but we are
  1066.    --  not allowed to have in out parameters for functions. Instead we pass
  1067.    --  actuals which must be variables, and with a bit of trickery in the
  1068.    --  body, manage to interprete them properly as though they were indeed
  1069.    --  in out parameters.
  1070.  
  1071.    function Match
  1072.      (Subject : VString_Var;
  1073.       Pat     : Pattern;
  1074.       Result  : Match_Result_Var)
  1075.       return    Boolean;
  1076.  
  1077.    procedure Match
  1078.      (Subject : in out VString;
  1079.       Pat     : Pattern;
  1080.       Result  : out Match_Result);
  1081.  
  1082.    procedure Replace
  1083.      (Result  : in out Match_Result;
  1084.       Replace : VString);
  1085.    --  Given a previous call to Match which set Result, performs a pattern
  1086.    --  replacement if the match was successful. Has no effect if the match
  1087.    --  failed. This call should immediately follow the Match call.
  1088.  
  1089.    ------------------------
  1090.    -- Debugging Routines --
  1091.    ------------------------
  1092.  
  1093.    --  Debugging pattern matching operations can often be quite complex,
  1094.    --  since there is no obvious way to trace the progress of the match.
  1095.    --  The declarations in this section provide some debugging assistance.
  1096.  
  1097.    Debug_Mode : Boolean := False;
  1098.    --  This global variable can be set True to generate debugging on all
  1099.    --  subsequent calls to Match. The debugging output is a full trace of
  1100.    --  the actions of the pattern matcher, written to Standard_Output. The
  1101.    --  level of this information is intended to be comprehensible at the
  1102.    --  abstract level of this package declaration. However, note that the
  1103.    --  use of this switch often generates large amounts of output.
  1104.  
  1105.    function "*"  (P : Pattern; Fil : File_Access)           return Pattern;
  1106.    function "*"  (P : PString; Fil : File_Access)           return Pattern;
  1107.    function "*"  (P : PChar;   Fil : File_Access)           return Pattern;
  1108.    function "**" (P : Pattern; Fil : File_Access)           return Pattern;
  1109.    function "**" (P : PString; Fil : File_Access)           return Pattern;
  1110.    function "**" (P : PChar;   Fil : File_Access)           return Pattern;
  1111.    --  These are similar to the corresponding pattern assignment operations
  1112.    --  except that instead of setting the value of a variable, the matched
  1113.    --  substring is written to the appropriate file. This can be useful in
  1114.    --  following the progress of a match without generating the full amount
  1115.  
  1116.    --  of information obtained by setting Debug_Mode to True.
  1117.  
  1118.    Terminal : constant File_Access := Standard_Error;
  1119.    Output   : constant File_Access := Standard_Output;
  1120.    --  Two handy synonyms for use with the above pattern write operations.
  1121.  
  1122.    --  Finally we have some routines that are useful for determining what
  1123.    --  patterns are in use, particularly if they are constructed dynamically.
  1124.  
  1125.    function Image (P : Pattern) return String;
  1126.    function Image (P : Pattern) return VString;
  1127.    --  This procedures yield strings that corresponds to the syntax needed
  1128.    --  to create the given pattern using the functions in this package. The
  1129.    --  form of this string is such that it could actually be compiled and
  1130.    --  evaluated to yield the required pattern except for references to
  1131.    --  variables and functions, which are output using one of the following
  1132.    --  forms:
  1133.    --
  1134.    --     access Natural     NP(16#...#)
  1135.    --     access Pattern     PP(16#...#)
  1136.    --     access VString     VP(16#...#)
  1137.    --
  1138.    --     Natural_Func       NF(16#...#)
  1139.    --     VString_Func       VF(16#...#)
  1140.    --
  1141.    --  where 16#...# is the hex representation of the integer address that
  1142.    --  corresponds to the given access value
  1143.  
  1144.    procedure Dump (P : Pattern);
  1145.    --  This procedure writes information about the pattern to Standard_Out.
  1146.    --  The format of this information is keyed to the internal data structures
  1147.    --  used to implement patterns. The information provided by Dump is thus
  1148.    --  more precise than that yielded by Image, but is also a bit more obscure
  1149.    --  (i.e. it cannot be interpreted solely in terms of this spec, you have
  1150.    --  to know something about the data structures).
  1151.  
  1152.    ------------------
  1153.    -- Private Part --
  1154.    ------------------
  1155.  
  1156. private
  1157.    type PE;
  1158.    --  Pattern element, a pattern is a plex structure of PE's. This type
  1159.    --  is defined and sdescribed in the body of this package.
  1160.  
  1161.    type PE_Ptr is access all PE;
  1162.    --  Pattern reference. PE's use PE_Ptr values to reference other PE's
  1163.  
  1164.    type Pattern is new Controlled with record
  1165.  
  1166.       Stk : Natural;
  1167.       --  Maximum number of stack entries required for matching this
  1168.       --  pattern. See description of pattern history stack in body.
  1169.  
  1170.       P   : PE_Ptr;
  1171.       --  Pointer to initial pattern element for pattern
  1172.  
  1173.    end record;
  1174.  
  1175.    pragma Finalize_Storage_Only (Pattern);
  1176.  
  1177.    procedure Adjust (Object : in out Pattern);
  1178.    --  Adjust routine used to copy pattern objects
  1179.  
  1180.    procedure Finalize (Object : in out Pattern);
  1181.    --  Finalization routine used to release storage allocated for a pattern.
  1182.  
  1183.    type VString_Ptr is access all VString;
  1184.  
  1185.    type Match_Result is record
  1186.       Var   : VString_Ptr;
  1187.       --  Pointer to subject string. Set to null if match failed.
  1188.  
  1189.       Start : Natural;
  1190.       --  Starting index position (1's origin) of matched section of
  1191.       --  subject string. Only valid if Var is non-null.
  1192.  
  1193.       Stop  : Natural;
  1194.       --  Ending index position (1's origin) of matched section of
  1195.       --  subject string. Only valid if Var is non-null.
  1196.  
  1197.    end record;
  1198.  
  1199.    pragma Volatile (Match_Result);
  1200.    --  This ensures that the Result parameter is passed by reference, so
  1201.    --  that we can play our games with the bogus Match_Result_Var parameter
  1202.    --  in the function case to treat it as though it were an in out parameter.
  1203.  
  1204. end GNAT.Spitbol.Patterns;
  1205.