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

  1. ############################################################################
  2. #
  3. #    Name:     stripcom.icn
  4. #
  5. #    Title:     Strip out comments from a line of Icon code
  6. #
  7. #    Author:     Richard L. Goerwitz
  8. #
  9. #    Date:     June 3, 1991
  10. #
  11. #    Version: 1.4
  12. #
  13. ############################################################################
  14. #  
  15. #  Strip commented-out portion of a line of Icon code.  Fails on lines
  16. #  which, either stripped or otherwise, come out as an empty string.
  17. #
  18. ############################################################################
  19. #
  20. #  BUGS:  Can't handle lines ending in an underscore as part of a
  21. #  broken string literal, since stripcom is not intended to be used
  22. #  on sequentially read files.  It simply removes comments from indi-
  23. #  vidual lines.
  24. #
  25. ############################################################################
  26.  
  27.  
  28. # To preserve parallelism between file and procedure names.
  29. procedure stripcom(s)
  30.     return strip_comments(s)
  31. end
  32.  
  33.  
  34. # The original name -
  35. procedure strip_comments(s)
  36.  
  37.     local i, j, c, c2, s2
  38.  
  39.     s ? {
  40.     tab(many(' \t'))
  41.     pos(0) & fail
  42.         find("#") | (return trim(tab(0),' \t'))
  43.     match("#") & fail
  44.     (s2 <- tab(find("#"))) ? {
  45.         c2 := &null
  46.         while tab(upto('\\"\'')) do {
  47.         case c := move(1) of {
  48.             "\\"   : {
  49.             if match("^")
  50.             then move(2)
  51.             else move(1)
  52.             }
  53.             default: {
  54.             if \c2
  55.             then (c == c2, c2 := &null)
  56.             else c2 := c
  57.             }
  58.         }
  59.         }
  60.         /c2
  61.     }
  62.     return "" ~== trim((\s2 | tab(0)) \ 1, ' \t')
  63.     }
  64.  
  65. end
  66.