home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / CLIPPER / MISC / BIPL.ZIP / PROCS.ZIP / SLASHBAL.ICN < prev    next >
Encoding:
Text File  |  1993-01-27  |  2.6 KB  |  87 lines

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