summaryrefslogtreecommitdiff
path: root/ipl/procs/stripcom.icn
blob: a9fa89f2e67a5d2c9b2090f7a4c5ed7d41cff70c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
############################################################################
#
#	File:     stripcom.icn
#
#	Subject:  Procedures to strip comments from Icon line
#
#	Author:   Richard L. Goerwitz
#
#	Date:	  March 3, 1996
#
############################################################################
#
#   This file is in the public domain.
#
############################################################################
#
#	Version:  1.5
#
############################################################################
#  
#  Strip commented-out portion of a line of Icon code.  Fails on lines
#  which, either stripped or otherwise, come out as an empty string.
#
############################################################################
#
#  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