comment_strip-ruby 0.1.2.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c52ca8b12ff14075e62b2e7e6fce5249e2bb11271d9b457ba40f510c2d185ac5
4
- data.tar.gz: 934da47218a66a83e1f021581705d9412d40922f35df76cdfcaaed0eb81221b3
3
+ metadata.gz: e320f9b4b49802d9301139fadb06eaa71c7a28f641e31892c60475bca96d78d5
4
+ data.tar.gz: 055d162fb3675a3ede834009ae775b0704896bab3d7d865545dbdc7de18e6c32
5
5
  SHA512:
6
- metadata.gz: 936fd5d120b21214cda1dca24fdb8f26aad31ce4c152f832a2d67294f175a74ad677420cde36df3bfb0402835cbcdf9e358abb7a6194a17d24f2b24064fe0a16
7
- data.tar.gz: 67b00be7468b64475114aebd29d67f7d226d24eed187b2a808e1526966b9b2389938713c499eba8f09d17a6490cdf1c3b05058929ef11c60f84edb226c3a1d8a
6
+ metadata.gz: f4e58fe98de4d8592b565d7e3000f8cf7e53daf07029b0d566bc23c457e203c66f16a6162c8235302bed94b599ed1616722e57a97978096a14688f3fdba2b23c
7
+ data.tar.gz: d0ac2808f789296db1471688584e11508692578bf36608e577a34455ff3b3aedbb3a3378dca02ecce626f50a2e8ec9843abcc0bef5b31ed5b38e2ea0658c7b99
data/README.md CHANGED
@@ -30,8 +30,10 @@ gem 'comment_strip-ruby'
30
30
 
31
31
  ## Components
32
32
 
33
- T.B.C.
33
+ Current version supports following language families:
34
34
 
35
+ * `'C'` - C-family languages, recognising `//` line and `/* . . . */` block comments;
36
+ * `'Hash_Line'` - Generic support for `#` line comments, as found in shell scripts and languages such as **Perl**, **Python**, and **Ruby**. Does _NOT_ (yet) provide any language-specific smarts such as she-bang comments and directive comments;
35
37
  ## Examples
36
38
 
37
39
  It is as simple as the following:
@@ -1,14 +1,14 @@
1
1
  # ######################################################################## #
2
- # File: comment_strip/language_families/c.rb
2
+ # File: comment_strip/language_families/c.rb
3
3
  #
4
- # Purpose: Definition of strip() function for C-family languages.
4
+ # Purpose: Definition of strip() function for C-family languages.
5
5
  #
6
- # Created: 14th September 2020
7
- # Updated: 11th July 2022
6
+ # Created: 14th September 2020
7
+ # Updated: 31st March 2024
8
8
  #
9
- # Home: http://github.com/synesissoftware/comment_strip.r
9
+ # Home: http://github.com/synesissoftware/comment_strip.r
10
10
  #
11
- # Copyright (c) 2020-2022, Matthew Wilson and Synesis Information Systems
11
+ # Copyright (c) 2020-2024, 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
@@ -47,200 +47,222 @@ module LanguageFamilies
47
47
 
48
48
  module C
49
49
 
50
- def self.strip input, lf, **options
50
+ def self.strip input, lf, **options
51
51
 
52
- return nil if input.nil?
53
- return input if input.empty?
52
+ return input if input.nil?
53
+ return input if input.empty?
54
54
 
55
- line = 0
56
- column = 0
55
+ line = 0
56
+ column = 0
57
57
 
58
- state = :text
58
+ # States:
59
+ #
60
+ # - :c_comment - in a C comment, i.e. from immediately after "/*"
61
+ # - :c_comment_star - in a C comment when just received '*', e.g. from immediately after "/* comment *"
62
+ # - :cpp_comment - in a C++ comment, i.e. from immediately after "//"
63
+ # - :dq_string - within a double-quoted string
64
+ # - :dq_string_escape - within a double-quoted string when just received a '"', e.g. from immediately after '"the next word is quoted \'
65
+ # - :slash_start - having found a slash (not in a string)
66
+ # - :sq_string_closing - waiting for final '\'' in a single-quoted string
67
+ # - :sq_string_escape - within a escaped single-quoted string, i.e. from immediately after "'\"
68
+ # - :sq_string_open - within a single-quoted string, i.e. from immediately after "'"
69
+ # - :text - regular part of the code
59
70
 
60
- r = ''
71
+ state = :text
61
72
 
62
- cc_lines = 0
73
+ r = ''
63
74
 
64
- input.each_char do |c|
75
+ cc_lines = 0
65
76
 
66
- case c
67
- when ?\r, ?\n
68
-
69
- line += 1
70
- column = 0
71
- else
72
-
73
- column += 1
74
- end
77
+ input.each_char do |c|
75
78
 
76
- skip = false
79
+ case c
80
+ when ?\r, ?\n
77
81
 
78
- case c
79
- when ?\r, ?\n
82
+ line += 1
83
+ column = 0
84
+ else
80
85
 
81
- case state
82
- when :c_comment, :c_comment_star
86
+ column += 1
87
+ end
83
88
 
84
- cc_lines += 1
89
+ skip = false
85
90
 
86
- state = :c_comment
87
- when :cpp_comment
91
+ case c
92
+ when ?\r, ?\n
88
93
 
89
- state = :text
90
- when :sq_string, :sq_string_escape, :sq_string_closing
94
+ case state
95
+ when :c_comment, :c_comment_star
91
96
 
92
- state = :text
93
- when :dq_string_escape
97
+ cc_lines += 1
94
98
 
95
- state = :dq_string
96
- when :slash_start
99
+ state = :c_comment
100
+ when :cpp_comment
97
101
 
98
- r << '/'
102
+ state = :text
103
+ when :sq_string_escape, :sq_string_closing
99
104
 
100
- state = :text
101
- end
102
- else
105
+ state = :text
106
+ when :dq_string_escape
103
107
 
104
- # special cases:
105
- #
106
- # - for escaped single/double quote
107
- # - for slash-start
108
- # - for comment-star
108
+ state = :dq_string
109
+ when :slash_start
109
110
 
110
- case state
111
- when :sq_string_open
111
+ r << '/'
112
112
 
113
- state = (?\\ == c) ? :sq_string_escape : :sq_string_closing
114
- when :sq_string_escape
113
+ state = :text
114
+ end
115
+ else
115
116
 
116
- state = :sq_string_closing
117
- when :dq_string_escape
117
+ # special cases:
118
+ #
119
+ # - for escaped single/double quote
120
+ # - for slash-start
121
+ # - for comment-star
118
122
 
119
- state = :dq_string
120
- when :c_comment_star
123
+ case state
124
+ when :sq_string_open
121
125
 
122
- case c
123
- when ?/
126
+ state = (?\\ == c) ? :sq_string_escape : :sq_string_closing
127
+ when :sq_string_escape
124
128
 
125
- r << ?\n * cc_lines
126
- cc_lines = 0
129
+ state = :sq_string_closing
130
+ when :dq_string_escape
127
131
 
128
- state = :text
129
- skip = true
130
- when '*'
132
+ state = :dq_string
133
+ when :c_comment_star
131
134
 
132
- ;
133
- else
135
+ case c
136
+ when ?/
134
137
 
135
- state = :c_comment
136
- end
137
- else
138
+ r << ?\n * cc_lines
139
+ cc_lines = 0
138
140
 
139
- if false
140
- elsif state == :slash_start && ('/' != c && '*' != c)
141
+ state = :text
142
+ skip = true
143
+ when '*'
141
144
 
142
- state = :text
143
- r << '/'
144
- else
145
+ ;
146
+ else
145
147
 
146
- case c
147
- when '/'
148
+ state = :c_comment
149
+ end
150
+ else
148
151
 
149
- case state
150
- when :text
152
+ if false
153
+ elsif state == :slash_start && ('/' != c && '*' != c)
151
154
 
152
- state = :slash_start
153
- when :slash_start
155
+ state = :text
156
+ r << '/'
157
+ else
154
158
 
155
- state = :cpp_comment
156
- when :c_comment_star
159
+ case c
160
+ when '/'
157
161
 
158
- r << ?\n * cc_lines
159
- cc_lines = 0
162
+ case state
163
+ when :text
160
164
 
161
- state = :text
162
- skip = true
163
- end
164
- when '*'
165
+ state = :slash_start
166
+ when :slash_start
165
167
 
166
- case state
167
- when :slash_start
168
+ state = :cpp_comment
169
+ when :c_comment_star
168
170
 
169
- state = :c_comment
170
- when :c_comment
171
+ r << ?\n * cc_lines
172
+ cc_lines = 0
171
173
 
172
- state = :c_comment_star
173
- else
174
+ state = :text
175
+ skip = true
176
+ else
174
177
 
175
- end
178
+ ;
179
+ end
180
+ when '*'
176
181
 
177
- when ?\'
182
+ case state
183
+ when :slash_start
178
184
 
179
- case state
180
- when :text
185
+ state = :c_comment
186
+ when :c_comment
181
187
 
182
- state = :sq_string_open
183
- when :sq_string_closing
188
+ state = :c_comment_star
189
+ else
184
190
 
185
- state = :text
186
- else
191
+ ;
192
+ end
193
+ when ?\'
187
194
 
188
- end
189
- when '"'
195
+ case state
196
+ when :text
190
197
 
191
- case state
192
- when :text
198
+ state = :sq_string_open
199
+ when :sq_string_closing
193
200
 
194
- state = :dq_string
195
- when :dq_string
201
+ state = :text
202
+ else
196
203
 
197
- state = :text
198
- else
199
- end
200
- when ?\\
204
+ ;
205
+ end
206
+ when '"'
201
207
 
202
- case state
203
- when :sq_string_open
208
+ case state
209
+ when :text
204
210
 
205
- state = :sq_string_escape
206
- when :sq_string_escape
211
+ state = :dq_string
212
+ when :dq_string
207
213
 
208
- state = :sq_string
209
- when :dq_string
214
+ state = :text
215
+ else
210
216
 
211
- state = :dq_string_escape
212
- else
217
+ ;
218
+ end
219
+ when ?\\
213
220
 
214
- end
215
- else
221
+ case state
222
+ when :sq_string_open
216
223
 
217
- case state
218
- when :sq_string_escape
224
+ state = :sq_string_escape
225
+ when :sq_string_escape
219
226
 
220
- state = :sq_string_closing
221
- else
227
+ state = :sq_string_closing
228
+ when :dq_string
222
229
 
223
- end
224
- end
225
- end
226
- end
227
- end
230
+ state = :dq_string_escape
231
+ else
228
232
 
233
+ ;
234
+ end
235
+ else
229
236
 
230
- case state
231
- when :slash_start
232
- when :cpp_comment
233
- when :c_comment
234
- when :c_comment_star
237
+ case state
238
+ when :sq_string_escape
235
239
 
236
- else
240
+ state = :sq_string_closing
241
+ else
237
242
 
238
- r << c unless skip
243
+ ;
244
+ end
239
245
  end
246
+ end
240
247
  end
248
+ end
241
249
 
242
- r
250
+
251
+ case state
252
+ when :slash_start
253
+ when :cpp_comment
254
+ when :c_comment
255
+ when :c_comment_star
256
+
257
+ ;
258
+ else
259
+
260
+ r << c unless skip
261
+ end
243
262
  end
263
+
264
+ r
265
+ end
244
266
  end # module C
245
267
 
246
268
  end # module LanguageFamilies
@@ -0,0 +1,211 @@
1
+ # ######################################################################## #
2
+ # File: comment_strip/language_families/hash_line.rb
3
+ #
4
+ # Purpose: Definition of strip() function for languages that support single
5
+ # line comments beginning at the # character.
6
+ #
7
+ # Created: 1st December 2023
8
+ # Updated: 31st March 2024
9
+ #
10
+ # Home: http://github.com/synesissoftware/comment_strip.r
11
+ #
12
+ # Copyright (c) 2023-2024, Matthew Wilson and Synesis Information Systems
13
+ # All rights reserved.
14
+ #
15
+ # Redistribution and use in source and binary forms, with or without
16
+ # modification, are permitted provided that the following conditions are
17
+ # met:
18
+ #
19
+ # * Redistributions of source code must retain the above copyright notice,
20
+ # this list of conditions and the following disclaimer.
21
+ #
22
+ # * Redistributions in binary form must reproduce the above copyright
23
+ # notice, this list of conditions and the following disclaimer in the
24
+ # documentation and/or other materials provided with the distribution.
25
+ #
26
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
27
+ # IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
28
+ # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29
+ # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
30
+ # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31
+ # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32
+ # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33
+ # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34
+ # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35
+ # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36
+ # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37
+ #
38
+ # ######################################################################## #
39
+
40
+
41
+ require 'xqsr3/quality/parameter_checking'
42
+
43
+ =begin
44
+ =end
45
+
46
+ module CommentStrip
47
+ module LanguageFamilies
48
+
49
+ module HashLine
50
+
51
+ def self.strip input, lf, **options
52
+
53
+ return input if input.nil?
54
+ return input if input.empty?
55
+
56
+ line = 0
57
+ column = 0
58
+
59
+ # States:
60
+ #
61
+ # - :dq_string - within a double-quoted string
62
+ # - :dq_string_escape - within a double-quoted string when just received a '"', e.g. from immediately after '"the next word is quoted \'
63
+ # - :hash_comment - in a #-comment, i.e. from immediately after "#"
64
+ # - :sq_string_closing - waiting for final '\'' in a single-quoted string
65
+ # - :sq_string_escape - within a escaped single-quoted string, i.e. from immediately after "'\"
66
+ # - :sq_string_open - within a single-quoted string, i.e. from immediately after "'"
67
+ # - :text - regular part of the code
68
+
69
+ state = :text
70
+
71
+ r = ''
72
+
73
+ cc_lines = 0
74
+
75
+ input.each_char do |c|
76
+
77
+ case c
78
+ when ?\r, ?\n
79
+
80
+ line += 1
81
+ column = 0
82
+ else
83
+
84
+ column += 1
85
+ end
86
+
87
+ skip = false
88
+
89
+ case c
90
+ when ?\r, ?\n
91
+
92
+ case state
93
+ when :hash_comment
94
+
95
+ state = :text
96
+ when :sq_string_escape, :sq_string_closing
97
+
98
+ state = :text
99
+ when :dq_string_escape
100
+
101
+ state = :dq_string
102
+ when :hash_comment
103
+
104
+ state = :text
105
+ end
106
+ else
107
+
108
+ # special cases:
109
+ #
110
+ # - for escaped single/double quote
111
+ # - for slash-start
112
+
113
+ case state
114
+ when :sq_string_open
115
+
116
+ state = (?\\ == c) ? :sq_string_escape : :sq_string_closing
117
+ when :sq_string_escape
118
+
119
+ state = :sq_string_closing
120
+ when :dq_string_escape
121
+
122
+ state = :dq_string
123
+ else
124
+
125
+ case c
126
+ when '#'
127
+
128
+ case state
129
+ when :text
130
+
131
+ state = :hash_comment
132
+ else
133
+
134
+ ;
135
+ end
136
+ when ?\'
137
+
138
+ case state
139
+ when :text
140
+
141
+ state = :sq_string_open
142
+ when :sq_string_closing
143
+
144
+ state = :text
145
+ else
146
+
147
+ ;
148
+ end
149
+ when '"'
150
+
151
+ case state
152
+ when :text
153
+
154
+ state = :dq_string
155
+ when :dq_string
156
+
157
+ state = :text
158
+ else
159
+
160
+ ;
161
+ end
162
+ when ?\\
163
+
164
+ case state
165
+ when :sq_string_open
166
+
167
+ state = :sq_string_escape
168
+ when :sq_string_escape
169
+
170
+ state = :sq_string_closing
171
+ when :dq_string
172
+
173
+ state = :dq_string_escape
174
+ else
175
+
176
+ ;
177
+ end
178
+ else
179
+
180
+ case state
181
+ when :sq_string_escape
182
+
183
+ state = :sq_string_closing
184
+ else
185
+
186
+ ;
187
+ end
188
+ end
189
+ end
190
+ end
191
+
192
+
193
+ case state
194
+ when :hash_comment
195
+
196
+ ;
197
+ else
198
+
199
+ r << c unless skip
200
+ end
201
+ end
202
+
203
+ r
204
+ end
205
+ end # module HashLine
206
+
207
+ end # module LanguageFamilies
208
+ end # module CommentStrip
209
+
210
+ # ############################## end of file ############################# #
211
+
@@ -1,14 +1,14 @@
1
1
  # ######################################################################## #
2
- # File: comment_strip/strip.rb
2
+ # File: comment_strip/strip.rb
3
3
  #
4
- # Purpose: Definition of strip() function
4
+ # Purpose: Definition of strip() function
5
5
  #
6
- # Created: 14th September 2020
7
- # Updated: 11th July 2022
6
+ # Created: 14th September 2020
7
+ # Updated: 30th March 2024
8
8
  #
9
- # Home: http://github.com/synesissoftware/comment_strip.r
9
+ # Home: http://github.com/synesissoftware/comment_strip.r
10
10
  #
11
- # Copyright (c) 2020-2022, Matthew Wilson and Synesis Information Systems
11
+ # Copyright (c) 2020-2024, 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
@@ -37,7 +37,15 @@
37
37
  # ######################################################################## #
38
38
 
39
39
 
40
- require File.join(__dir__, 'language_families', 'c')
40
+
41
+ %w{
42
+
43
+ c
44
+ hash_line
45
+ }.each do |name|
46
+
47
+ require File.join(File.dirname(__FILE__), 'language_families', name)
48
+ end
41
49
 
42
50
  require 'xqsr3/quality/parameter_checking'
43
51
 
@@ -54,16 +62,17 @@ module CommentStrip
54
62
  # === Signature
55
63
  #
56
64
  # * *Parameters:*
57
- # - +input+ (::String, +nil+) the input source code
58
- # - +lf+ (::String) the name of the language family. Currently only
59
- # the value +'C'+ is accepted
60
- # - +options+ (::Hash) options that moderate the behaviour
65
+ # - +input+ (+String+, +nil+) the input source code
66
+ # - +lf+ (+String+) the name of the language family, which must be one of the following:
67
+ # - +'C'+
68
+ # - +'Hash_Line'+
69
+ # - +options+ (+Hash+) options that moderate the behaviour
61
70
  #
62
71
  # * *Options:*
63
72
  # None currently defined.
64
73
  #
65
74
  # === Signature
66
- # (String) The stripped for of the input.
75
+ # (+String+) The stripped for of the input.
67
76
  def strip input, lf, **options
68
77
 
69
78
  check_parameter input, 'input', responds_to: [ :each_char, :empty?, :nil?, ], nil: true
@@ -73,6 +82,9 @@ module CommentStrip
73
82
  when 'C'
74
83
 
75
84
  LanguageFamilies::C.strip input, lf, **options
85
+ when 'HASH_LINE'
86
+
87
+ LanguageFamilies::HashLine.strip input, lf, **options
76
88
  else
77
89
 
78
90
  raise "language family '#{lf}' unrecognised or not supported1"