comment_strip-ruby 0.0.13 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/lib/comment_strip/strip.rb +155 -135
- data/lib/comment_strip/version.rb +3 -3
- data/test/unit/tc_strip.rb +8 -0
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 7b8c5c74b1296b3feccf05d110b933c7c6c2a024931dacd9080b0d59f25623b8
|
4
|
+
data.tar.gz: bf8eb299b541fb6dba2407408f0b1a663de7adc7b891aa544fa0c2f115ba9831
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d1ee8150a44c22dfca14eb74bc09b1af58c85c698e8efb8f0794762b413418b0996fc2548dfeb297d8f58a54a4763900eb9e1da139c9f2c216024e51d919e324
|
7
|
+
data.tar.gz: f6bdf317e9704995260334a4b30a880a003a7c4aedbb6bca83bfc16ca98bb875de7fbcbbbc9081367bfad787a55c802bb94181a35d70c76cc092aba6c77ef0bc
|
data/lib/comment_strip/strip.rb
CHANGED
@@ -4,11 +4,11 @@
|
|
4
4
|
# Purpose: Definition of strip() function
|
5
5
|
#
|
6
6
|
# Created: 14th September 2020
|
7
|
-
# Updated:
|
7
|
+
# Updated: 10th February 2021
|
8
8
|
#
|
9
9
|
# Home: http://github.com/synesissoftware/comment_strip.r
|
10
10
|
#
|
11
|
-
# Copyright (c) 2020, Matthew Wilson and Synesis Information Systems
|
11
|
+
# Copyright (c) 2020-2021, Matthew Wilson and Synesis Information Systems
|
12
12
|
# All rights reserved.
|
13
13
|
#
|
14
14
|
# Redistribution and use in source and binary forms, with or without
|
@@ -43,209 +43,229 @@
|
|
43
43
|
|
44
44
|
module CommentStrip
|
45
45
|
|
46
|
-
# Strips comments
|
47
|
-
|
46
|
+
# Strips comments from an input string, according to the rules and
|
47
|
+
# conventions of a given language-family
|
48
|
+
#
|
49
|
+
# === Signature
|
50
|
+
#
|
51
|
+
# * *Parameters:*
|
52
|
+
# - +s+ (::String) the source code
|
53
|
+
# - +lf+ (::String) the name of the language family. Currently only
|
54
|
+
# the value +'C'+ is accepted
|
55
|
+
# - +options+ (::Hash) options that moderate the behaviour
|
56
|
+
#
|
57
|
+
# * *Options:*
|
58
|
+
#
|
59
|
+
def self.strip s, lf, **options
|
60
|
+
|
61
|
+
case lf.upcase
|
62
|
+
when 'C'
|
63
|
+
|
64
|
+
;
|
65
|
+
else
|
48
66
|
|
49
|
-
|
50
|
-
|
67
|
+
raise "language family '#{lf}' unrecognised"
|
68
|
+
end
|
51
69
|
|
52
|
-
|
53
|
-
|
70
|
+
return nil if s.nil?
|
71
|
+
return s if s.empty?
|
54
72
|
|
55
|
-
|
56
|
-
|
73
|
+
line = 0
|
74
|
+
column = 0
|
57
75
|
|
58
|
-
|
59
|
-
return s if s.empty?
|
76
|
+
state = :text
|
60
77
|
|
61
|
-
|
62
|
-
column = 0
|
78
|
+
r = ''
|
63
79
|
|
64
|
-
|
80
|
+
cc_lines = 0
|
65
81
|
|
66
|
-
|
82
|
+
s.each_char do |c|
|
67
83
|
|
68
|
-
|
84
|
+
case c
|
85
|
+
when ?\r, ?\n
|
69
86
|
|
70
|
-
|
87
|
+
line += 1
|
88
|
+
column = 0
|
89
|
+
else
|
71
90
|
|
72
|
-
|
73
|
-
|
91
|
+
column += 1
|
92
|
+
end
|
74
93
|
|
75
|
-
|
76
|
-
column = 0
|
77
|
-
else
|
94
|
+
skip = false
|
78
95
|
|
79
|
-
|
80
|
-
|
96
|
+
case c
|
97
|
+
when ?\r, ?\n
|
81
98
|
|
82
|
-
|
99
|
+
case state
|
100
|
+
when :c_comment, :c_comment_star
|
83
101
|
|
84
|
-
|
85
|
-
when ?\r, ?\n
|
102
|
+
cc_lines += 1
|
86
103
|
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
cc_lines += 1
|
104
|
+
state = :c_comment
|
105
|
+
when :cpp_comment
|
91
106
|
|
92
|
-
|
93
|
-
|
107
|
+
state = :text
|
108
|
+
when :sq_string, :sq_string_escape, :sq_string_closing
|
94
109
|
|
95
|
-
|
96
|
-
|
110
|
+
state = :text
|
111
|
+
when :dq_string_escape
|
97
112
|
|
98
|
-
|
99
|
-
|
113
|
+
state = :dq_string
|
114
|
+
when :slash_start
|
100
115
|
|
101
|
-
|
102
|
-
when :slash_start
|
116
|
+
r << '/'
|
103
117
|
|
104
|
-
|
118
|
+
state = :text
|
119
|
+
end
|
120
|
+
else
|
105
121
|
|
106
|
-
|
107
|
-
|
108
|
-
|
122
|
+
# special cases:
|
123
|
+
#
|
124
|
+
# - for escaped single/double quote
|
125
|
+
# - for slash-start
|
126
|
+
# - for comment-star
|
109
127
|
|
110
|
-
|
111
|
-
|
112
|
-
# - for escaped single/double quote
|
113
|
-
# - for slash-start
|
114
|
-
# - for comment-star
|
128
|
+
case state
|
129
|
+
when :sq_string_open
|
115
130
|
|
116
|
-
|
117
|
-
|
131
|
+
state = (?\\ == c) ? :sq_string_escape : :sq_string_closing
|
132
|
+
when :sq_string_escape
|
118
133
|
|
119
|
-
|
120
|
-
|
134
|
+
state = :sq_string_closing
|
135
|
+
when :dq_string_escape
|
121
136
|
|
122
|
-
|
123
|
-
|
137
|
+
state = :dq_string
|
138
|
+
when :c_comment_star
|
124
139
|
|
125
|
-
|
126
|
-
|
140
|
+
case c
|
141
|
+
when ?/
|
127
142
|
|
128
|
-
|
129
|
-
|
143
|
+
r << ?\n * cc_lines
|
144
|
+
cc_lines = 0
|
130
145
|
|
131
|
-
|
132
|
-
|
146
|
+
state = :text
|
147
|
+
skip = true
|
148
|
+
when '*'
|
133
149
|
|
134
|
-
|
135
|
-
|
136
|
-
when '*'
|
150
|
+
;
|
151
|
+
else
|
137
152
|
|
138
|
-
|
153
|
+
state = :c_comment
|
154
|
+
end
|
139
155
|
else
|
140
156
|
|
141
|
-
|
142
|
-
|
143
|
-
else
|
157
|
+
if false
|
158
|
+
elsif state == :slash_start && ('/' != c && '*' != c)
|
144
159
|
|
145
|
-
|
146
|
-
|
160
|
+
state = :text
|
161
|
+
r << '/'
|
162
|
+
else
|
147
163
|
|
148
|
-
|
149
|
-
|
150
|
-
else
|
164
|
+
case c
|
165
|
+
when '/'
|
151
166
|
|
152
|
-
|
153
|
-
|
167
|
+
case state
|
168
|
+
when :text
|
154
169
|
|
155
|
-
|
156
|
-
|
170
|
+
state = :slash_start
|
171
|
+
when :slash_start
|
157
172
|
|
158
|
-
|
159
|
-
|
173
|
+
state = :cpp_comment
|
174
|
+
when :c_comment_star
|
160
175
|
|
161
|
-
|
162
|
-
|
176
|
+
r << ?\n * cc_lines
|
177
|
+
cc_lines = 0
|
163
178
|
|
164
|
-
|
165
|
-
|
179
|
+
state = :text
|
180
|
+
skip = true
|
181
|
+
end
|
182
|
+
when '*'
|
166
183
|
|
167
|
-
state
|
168
|
-
|
169
|
-
end
|
170
|
-
when '*'
|
184
|
+
case state
|
185
|
+
when :slash_start
|
171
186
|
|
172
|
-
|
173
|
-
|
187
|
+
state = :c_comment
|
188
|
+
when :c_comment
|
174
189
|
|
175
|
-
|
176
|
-
|
190
|
+
state = :c_comment_star
|
191
|
+
else
|
177
192
|
|
178
|
-
|
179
|
-
else
|
193
|
+
end
|
180
194
|
|
181
|
-
|
195
|
+
when ?\'
|
182
196
|
|
183
|
-
|
197
|
+
case state
|
198
|
+
when :text
|
184
199
|
|
185
|
-
|
186
|
-
|
200
|
+
state = :sq_string_open
|
201
|
+
when :sq_string_closing
|
187
202
|
|
188
|
-
|
189
|
-
|
203
|
+
state = :text
|
204
|
+
else
|
190
205
|
|
191
|
-
|
192
|
-
|
206
|
+
end
|
207
|
+
when '"'
|
193
208
|
|
194
|
-
|
195
|
-
|
209
|
+
case state
|
210
|
+
when :text
|
196
211
|
|
197
|
-
|
198
|
-
|
212
|
+
state = :dq_string
|
213
|
+
when :dq_string
|
199
214
|
|
200
|
-
|
201
|
-
|
215
|
+
state = :text
|
216
|
+
else
|
217
|
+
end
|
218
|
+
when ?\\
|
202
219
|
|
203
|
-
state
|
204
|
-
|
205
|
-
end
|
206
|
-
when ?\\
|
220
|
+
case state
|
221
|
+
when :sq_string_open
|
207
222
|
|
208
|
-
|
209
|
-
|
223
|
+
state = :sq_string_escape
|
224
|
+
when :sq_string_escape
|
210
225
|
|
211
|
-
|
212
|
-
|
226
|
+
state = :sq_string
|
227
|
+
when :dq_string
|
213
228
|
|
214
|
-
|
215
|
-
|
229
|
+
state = :dq_string_escape
|
230
|
+
else
|
216
231
|
|
217
|
-
|
232
|
+
end
|
218
233
|
else
|
219
234
|
|
220
|
-
|
221
|
-
|
235
|
+
case state
|
236
|
+
when :sq_string_escape
|
222
237
|
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
state = :sq_string_closing
|
227
|
-
else
|
238
|
+
state = :sq_string_closing
|
239
|
+
else
|
228
240
|
|
241
|
+
end
|
229
242
|
end
|
230
243
|
end
|
231
244
|
end
|
232
245
|
end
|
233
|
-
end
|
234
246
|
|
235
|
-
|
236
|
-
when :slash_start
|
237
|
-
when :cpp_comment
|
238
|
-
when :c_comment
|
239
|
-
when :c_comment_star
|
247
|
+
$stderr.puts "[#{line}, #{column}] state=#{state}, c=#{c} (#{c.ord}); skip=#{skip} [#cc lines: #{cc_lines}]\n" if $DEBUG
|
240
248
|
|
241
|
-
|
249
|
+
case state
|
250
|
+
when :slash_start
|
251
|
+
when :cpp_comment
|
252
|
+
when :c_comment
|
253
|
+
when :c_comment_star
|
254
|
+
|
255
|
+
else
|
242
256
|
|
243
|
-
|
257
|
+
r << c unless skip
|
258
|
+
end
|
244
259
|
end
|
260
|
+
|
261
|
+
r
|
245
262
|
end
|
246
263
|
|
247
|
-
|
248
|
-
|
264
|
+
# Strips comments
|
265
|
+
def strip s, lf, **options
|
266
|
+
|
267
|
+
::CommentStrip.strip s, lf, **options
|
268
|
+
end
|
249
269
|
|
250
270
|
end # module CommentStrip
|
251
271
|
|
@@ -4,11 +4,11 @@
|
|
4
4
|
# Purpose: Version for comment_strip.r library
|
5
5
|
#
|
6
6
|
# Created: 14th September 2020
|
7
|
-
# Updated:
|
7
|
+
# Updated: 10th February 2021
|
8
8
|
#
|
9
9
|
# Home: http://github.com/synesissoftware/comment_strip.r
|
10
10
|
#
|
11
|
-
# Copyright (c) 2020, Matthew Wilson and Synesis Information Systems
|
11
|
+
# Copyright (c) 2020-2021, Matthew Wilson and Synesis Information Systems
|
12
12
|
# All rights reserved.
|
13
13
|
#
|
14
14
|
# Redistribution and use in source and binary forms, with or without
|
@@ -43,7 +43,7 @@
|
|
43
43
|
module CommentStrip
|
44
44
|
|
45
45
|
# Current version of the comment_strip.r library
|
46
|
-
VERSION = '0.0
|
46
|
+
VERSION = '0.1.0'
|
47
47
|
|
48
48
|
private
|
49
49
|
VERSION_PARTS_ = VERSION.split(/[.]/).collect { |n| n.to_i } # :nodoc:
|
data/test/unit/tc_strip.rb
CHANGED
@@ -16,6 +16,8 @@ class Test_strip_1 < Test::Unit::TestCase
|
|
16
16
|
def test_nil
|
17
17
|
|
18
18
|
assert_nil strip(nil, 'C')
|
19
|
+
|
20
|
+
assert_nil ::CommentStrip.strip(nil, 'C')
|
19
21
|
end
|
20
22
|
|
21
23
|
def test_unrecognised_families
|
@@ -36,12 +38,14 @@ class Test_strip_1 < Test::Unit::TestCase
|
|
36
38
|
unrecognised_families.each do |family|
|
37
39
|
|
38
40
|
assert_raise_with_message(::RuntimeError, /family.*unrecognised/) { strip('', family) }
|
41
|
+
assert_raise_with_message(::RuntimeError, /family.*unrecognised/) { ::CommentStrip.strip('', family) }
|
39
42
|
end
|
40
43
|
end
|
41
44
|
|
42
45
|
def test_empty
|
43
46
|
|
44
47
|
assert_equal "", strip('', 'C')
|
48
|
+
assert_equal "", ::CommentStrip.strip('', 'C')
|
45
49
|
end
|
46
50
|
|
47
51
|
def test_simple_main
|
@@ -56,6 +60,7 @@ EOF_main
|
|
56
60
|
expected = input
|
57
61
|
|
58
62
|
assert_equal expected, strip(input, 'C')
|
63
|
+
assert_equal expected, ::CommentStrip.strip(input, 'C')
|
59
64
|
end
|
60
65
|
|
61
66
|
def test_x_1
|
@@ -88,6 +93,7 @@ EOF_main
|
|
88
93
|
EOF_main
|
89
94
|
|
90
95
|
actual = strip(input, 'C')
|
96
|
+
actual = ::CommentStrip.strip(input, 'C')
|
91
97
|
|
92
98
|
assert_equal expected, actual
|
93
99
|
end
|
@@ -104,6 +110,7 @@ EOF_main
|
|
104
110
|
EOF_main
|
105
111
|
|
106
112
|
actual = strip(input, 'C')
|
113
|
+
actual = ::CommentStrip.strip(input, 'C')
|
107
114
|
|
108
115
|
assert_equal expected, actual
|
109
116
|
end
|
@@ -120,6 +127,7 @@ EOF_main
|
|
120
127
|
EOF_main
|
121
128
|
|
122
129
|
actual = strip(input, 'C')
|
130
|
+
actual = ::CommentStrip.strip(input, 'C')
|
123
131
|
|
124
132
|
assert_equal expected, actual
|
125
133
|
end
|
metadata
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: comment_strip-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matt Wilson
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
date: 2021-02-10 00:00:00.000000000 Z
|
@@ -85,7 +85,7 @@ homepage: https://github.com/synesissoftware/comment_strip.r
|
|
85
85
|
licenses:
|
86
86
|
- BSD-3-Clause
|
87
87
|
metadata: {}
|
88
|
-
post_install_message:
|
88
|
+
post_install_message:
|
89
89
|
rdoc_options: []
|
90
90
|
require_paths:
|
91
91
|
- lib
|
@@ -100,9 +100,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
100
100
|
- !ruby/object:Gem::Version
|
101
101
|
version: '0'
|
102
102
|
requirements: []
|
103
|
-
rubyforge_project:
|
104
|
-
rubygems_version: 2.
|
105
|
-
signing_key:
|
103
|
+
rubyforge_project:
|
104
|
+
rubygems_version: 2.7.6.2
|
105
|
+
signing_key:
|
106
106
|
specification_version: 4
|
107
107
|
summary: comment_strip.r
|
108
108
|
test_files: []
|