home *** CD-ROM | disk | FTP | other *** search
- ############################################################################
- #
- # File: stripcom.icn
- #
- # Subject: Procedures to strip comments out of Icon line
- #
- # Author: Richard L. Goerwitz
- #
- # Date: June 3, 1991
- #
- ###########################################################################
- #
- # Version: 1.4
- #
- ###########################################################################
- #
- # Strip commented-out portion of a line of Icon code. Fails on lines
- # which, either stripped or otherwise, come out as an empty string.
- #
- # Note: It also removes whitespace, leaving the program un-indented. REG
- #
- ############################################################################
- #
- # BUGS: Can't handle lines ending in an underscore as part of a
- # broken string literal, since stripcom is not intended to be used
- # on sequentially read files. It simply removes comments from indi-
- # vidual lines.
- #
- ############################################################################
-
-
- # To preserve parallelism between file and procedure names.
- procedure stripcom(s)
- return strip_comments(s)
- end
-
-
- # The original name -
- procedure strip_comments(s)
-
- local i, j, c, c2, s2
-
- s ? {
- tab(many(' \t'))
- pos(0) & fail
- find("#") | (return trim(tab(0),' \t'))
- match("#") & fail
- (s2 <- tab(find("#"))) ? {
- c2 := &null
- while tab(upto('\\"\'')) do {
- case c := move(1) of {
- "\\" : {
- if match("^")
- then move(2)
- else move(1)
- }
- default: {
- if \c2
- then (c == c2, c2 := &null)
- else c2 := c
- }
- }
- }
- /c2
- }
- return "" ~== trim((\s2 | tab(0)) \ 1, ' \t')
- }
-
- end
-