home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / OL.LZH / PROCS.LZH / PATTERNS.ICN < prev    next >
Text File  |  1991-07-13  |  5KB  |  245 lines

  1. ############################################################################
  2. #
  3. #    Name:    patterns.icn
  4. #
  5. #    Title:    Pattern matching in the style of SNOBOL4
  6. #
  7. #    Author:    Ralph E. Griswold
  8. #
  9. #    Date:    June 10, 1988
  10. #
  11. ############################################################################
  12. #  
  13. #  These procedures provide procedural equivalents for most SNOBOL4
  14. #  patterns and some extensions. 
  15. #
  16. #  Procedures and their pattern equivalents are:
  17. #  
  18. #       Any(s)         ANY(S)
  19. #  
  20. #       Arb()          ARB
  21. #  
  22. #       Arbno(p)       ARBNO(P)
  23. #  
  24. #       Arbx(i)        ARB(I)
  25. #  
  26. #       Bal()          BAL
  27. #  
  28. #       Break(s)       BREAK(S)
  29. #  
  30. #       Breakx(s)      BREAKX(S)
  31. #  
  32. #       Cat(p1,p2)     P1 P2
  33. #  
  34. #       Discard(p)     /P
  35. #  
  36. #       Exog(s)        \S
  37. #  
  38. #       Find(s)        FIND(S)
  39. #  
  40. #       Len(i)         LEN(I)
  41. #  
  42. #       Limit(p,i)     P \ i
  43. #  
  44. #       Locate(p)      LOCATE(P)
  45. #  
  46. #       Marb()         longest-first ARB
  47. #  
  48. #       Notany(s)      NOTANY(S)
  49. #  
  50. #       Pos(i)         POS(I)
  51. #  
  52. #       Replace(p,s)   P = S
  53. #  
  54. #       Rpos(i)        RPOS(I)
  55. #  
  56. #       Rtab(i)        RTAB(I)
  57. #  
  58. #       Span(s)        SPAN(S)
  59. #  
  60. #       String(s)      S
  61. #  
  62. #       Succeed()      SUCCEED
  63. #  
  64. #       Tab(i)         TAB(I)
  65. #  
  66. #       Xform(f,p)     F(P)
  67. #  
  68. #     The following procedures relate to the application and control
  69. #  of pattern matching:
  70. #  
  71. #       Apply(s,p)     S ? P
  72. #  
  73. #       Mode()         anchored or unanchored matching (see Anchor
  74. #                      and Float)
  75. #  
  76. #       Anchor()       &ANCHOR = 1  if Mode := Anchor
  77. #  
  78. #       Float()        &ANCHOR = 0  if Mode := Float
  79. #  
  80. #  In addition to the procedures above, the following expressions
  81. #  can be used:
  82. #  
  83. #       p1() | p2()    P1 | P2
  84. #  
  85. #       v <- p()       P . V  (approximate)
  86. #  
  87. #       v := p()       P $ V  (approximate)
  88. #  
  89. #       fail           FAIL
  90. #  
  91. #       =s             S  (in place of String(s))
  92. #  
  93. #       p1() || p2()   P1 P2  (in place of Cat(p1,p2))
  94. #  
  95. #  Using this system, most SNOBOL4 patterns can be satisfactorily
  96. #  transliterated into Icon procedures and expressions. For example,
  97. #  the pattern
  98. #  
  99. #          SPAN("0123456789") $ N "H" LEN(*N) $ LIT
  100. #  
  101. #  can be transliterated into
  102. #  
  103. #          (n <- Span('0123456789')) || ="H" ||
  104. #             (lit <- Len(n))
  105. #  
  106. #  Concatenation of components is necessary to preserve the
  107. #  pattern-matching properties of SNOBOL4.
  108. #  
  109. #  Caveats: Simulating SNOBOL4 pattern matching using the procedures
  110. #  above is inefficient.
  111. #  
  112. ############################################################################
  113.  
  114. global Mode, Float
  115.  
  116. procedure Anchor()            #  &ANCHOR = 1
  117.    suspend ""
  118. end
  119.  
  120. procedure Any(s)            #  ANY(S)
  121.    suspend tab(any(s))
  122. end
  123.  
  124. procedure Apply(s,p)            #  S ? P
  125.    local tsubject, tpos, value
  126.    initial {
  127.       Float := Arb
  128.       /Mode := Float            #  &ANCHOR = 0 if not already set
  129.       }
  130.    suspend (
  131.       (tsubject := &subject) &
  132.       (tpos := &pos) &
  133.       (&subject <- s) &
  134.       (&pos <- 1) &
  135.       (Mode() & (value := p())) &
  136.       (&pos <- tpos) &            # to restore on backtracking
  137.       (&subject <- tsubject) &        # note this sets &pos
  138.       (&pos <- tpos) &            # to restore on evaluation
  139.       value
  140.       )
  141. end
  142.  
  143. procedure Arb()                #  ARB
  144.    suspend tab(&pos to *&subject + 1)
  145. end
  146.  
  147. procedure Arbno(p)            #  ARBNO(P)
  148.    suspend "" | (p() || Arbno(p))
  149. end
  150.  
  151. procedure Arbx(i)            #  ARB(I)
  152.    suspend tab(&pos to *&subject + 1 by i)
  153. end
  154.  
  155. procedure Bal()                #  BAL
  156.    suspend Bbal() || Arbno(Bbal)
  157. end
  158.  
  159. procedure Bbal()            #  used by Bal()
  160.    suspend (="(" || Arbno(Bbal) || =")") | Notany("()")
  161. end
  162.  
  163. procedure Break(s)            #  BREAK(S)
  164.    suspend tab(upto(s) \ 1)
  165. end
  166.  
  167. procedure Breakx(s)            #  BREAKX(S)
  168.    suspend tab(upto(s))
  169. end
  170.  
  171. procedure Cat(p1,p2)            #  P1 P2
  172.    suspend p1() || p2()
  173. end
  174.  
  175. procedure Discard(p)            #  /P
  176.    suspend p() & ""
  177. end
  178.  
  179. procedure Exog(s)            #  \S
  180.    suspend s
  181. end
  182.  
  183. procedure Find(s)            #  FIND(S)
  184.    suspend tab(find(s) + 1)
  185. end
  186.  
  187. procedure Len(i)            #  LEN(I)
  188.    suspend move(i)
  189. end
  190.  
  191. procedure Limit(p,i)            #  P \ i
  192.    local j
  193.    j := &pos
  194.    suspend p() \ i
  195.    &pos := j
  196. end
  197.  
  198. procedure Locate(p)            #  LOCATE(P)
  199.    suspend tab(&pos to *&subject + 1) & p()
  200. end
  201.  
  202. procedure Marb()            # max-first ARB
  203.    suspend tab(*&subject + 1 to &pos by -1)
  204. end
  205.  
  206. procedure Notany(s)            #  NOTANY(S)
  207.    suspend tab(any(~s))
  208. end
  209.  
  210. procedure Pos(i)            #  POS(I)
  211.    suspend pos(i + 1) & ""
  212. end
  213.  
  214. procedure Replace(p,s)            #  P = S
  215.    suspend p() & s
  216. end
  217.  
  218. procedure Rpos(i)            #  RPOS(I)
  219.    suspend pos(-i) & ""
  220. end
  221.  
  222. procedure Rtab(i)            #  RTAB(I)
  223.    suspend tab(-i)
  224. end
  225.  
  226. procedure Span(s)            #  SPAN(S)
  227.    suspend tab(many(s))
  228. end
  229.  
  230. procedure String(s)            #  S
  231.    suspend =s
  232. end
  233.  
  234. procedure Succeed()            #  SUCCEED
  235.    suspend |""
  236. end
  237.  
  238. procedure Tab(i)            #  TAB(I)
  239.    suspend tab(i + 1)
  240. end
  241.  
  242. procedure Xform(f,p)            #  F(P)
  243.    suspend f(p())
  244. end
  245.