[comp.lang.icon] write

goer%sophist@GARGOYLE.UCHICAGO.EDU (Richard Goerwitz) (09/28/90)

I just tried compiling

    write("hel_   # this is a partial comment
          lo")

and it didn't work.  It seems obvious why.  What a nightmare to parse!
Anyway, that makes our comment-stripping that much easier.  How about
something like:

procedure strip_comments(s)

    #######
    #
    # Commented-out portions of Icon code - strip 'em.  Fails on lines
    # which, either stripped or otherwise, come out as an empty string.
    #
    # I'd expect strip_comments to be used typically as follows:
    #
    #     every write(strip_comments(!&input))
    #
    #######

    local i, j, c, c2

    s ? {
	tab(many(' \t'))
        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)
		    }
		    "_"    : {
			if c2 == "\"" &
			   tab(many(' \t')) | &null &
			   pos(0)
			then return trim(&subject, ' \t')
		    }
		    default: {
			if \c2
			then (c == c2, c2 := &null)
			else c2 := c
		    }
		}
	    }
	    /c2
	}
	return "" ~== trim((\s2 | tab(0)) \ 1, ' \t')
    }

end


    -Richard L. Goerwitz              goer%sophist@uchicago.bitnet
    goer@sophist.uchicago.edu         rutgers!oddjob!gide!sophist!goer