home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / OL.LZH / PROCS.LZH / SLASHBAL.ICN < prev    next >
Text File  |  1991-09-05  |  2KB  |  84 lines

  1. ############################################################################
  2. #
  3. #    Name:     slashbal.icn
  4. #
  5. #    Title:     Bal() with backslash escaping
  6. #
  7. #    Author:     Richard L. Goerwitz
  8. #
  9. #    Version: 1.9
  10. #
  11. #    Date:     June 1, 1991
  12. #
  13. ############################################################################
  14. #
  15. #  I am often frustrated at bal()'s inability to deal elegantly with
  16. #  the common \backslash escaping convention (a way of telling Unix
  17. #  Bourne and C shells, for instance, not to interpret a given
  18. #  character as a "metacharacter").  I recognize that bal()'s generic
  19. #  behavior is a must, and so I wrote slashbal() to fill the gap.
  20. #
  21. #  Slashbal behaves like bal, except that it ignores, for purposes of
  22. #  balancing, any c2/c3 char which is preceded by a backslash.  Note
  23. #  that we are talking about internally represented backslashes, and
  24. #  not necessarily the backslashes used in Icon string literals.  If
  25. #  you have "\(" in your source code, the string produced will have no
  26. #  backslash.  To get this effect, you would need to write "\\(."
  27. #
  28. #  BUGS:  Note that, like bal() (v8), slashbal() cannot correctly
  29. #  handle cases where c2 and c3 intersect.
  30. #
  31. ############################################################################
  32.  
  33. procedure slashbal(c1, c2, c3, s, i, j)
  34.  
  35.     local twocs, allcs, chr2, count
  36.  
  37.     /c1 := &cset
  38.     /c2 := '('
  39.     /c3 := ')'
  40.     twocs := c2 ++ c3
  41.     allcs := c1 ++ c2 ++ c3 ++ '\\'
  42.  
  43.     /s := &subject
  44.     if \i then {
  45.     if i < 1 then
  46.         i := *s + (i+1)
  47.     }
  48.     else i := \&pos | 1
  49.     if \j then {
  50.     if j < 1 then
  51.         j := *s + (j+1)
  52.     }
  53.     else j := *s + 1
  54.  
  55.     count := 0
  56.     s ? {
  57.     while tab(upto(allcs)) do {
  58.         chr := move(1)
  59.         &pos > j & fail
  60.         if chr == "\\" & any(twocs) then {
  61.         chr2 := move(1)
  62.         &pos > j & fail
  63.         &pos > i | next
  64.         if any(c1, chr) & count = 0 then
  65.             suspend .&pos - 2
  66.         if any(c1, chr2) & count = 0 then
  67.             suspend .&pos - 1
  68.         }
  69.         else {
  70.         &pos > j & fail
  71.         if any(c1, chr) & count = 0 then {
  72.             &pos > i | next
  73.             suspend .&pos - 1
  74.         }
  75.         if any(c2, chr) then
  76.             count +:= 1
  77.         else if any(c3, chr) & count > 0 then
  78.             count -:= 1
  79.         }
  80.     }
  81.     }
  82.  
  83. end
  84.