home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2296 / slashbal.icn < prev    next >
Encoding:
Text File  |  1990-12-28  |  2.2 KB  |  86 lines

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