rb_cfg_parser 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/LICENSE.txt +1 -1
- data/lib/parser/cfg_parser.rb +50 -4
- data/lib/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 67671d34a5ab6b2e94b06ca3152ba1b820fb91bbb44a2cd8c75de155eae3da1b
|
4
|
+
data.tar.gz: c98c2337de6184a209f499d29b569f9836350e22c57d09602378cfe76b215382
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6801e106a59a9df556acb29c253b577befe47014f93f3fb215cec04ad232546b2cd27fcde5031e78ea8cee778c51f3bd2995d3faa2e330595496c959dd83c1ec
|
7
|
+
data.tar.gz: c6120c344a91322f903afb7e9ca4700c997eb09d63e115f90474610332bec605206a30307aa7ed3e221a482bda88dcebab52f1824ad43c9bb5e98625950d01a2
|
data/LICENSE.txt
CHANGED
data/lib/parser/cfg_parser.rb
CHANGED
@@ -9,6 +9,7 @@ class RbCfgParser
|
|
9
9
|
attr_writer :log_cb
|
10
10
|
|
11
11
|
###
|
12
|
+
# constructor
|
12
13
|
def initialize
|
13
14
|
@rex = CfgLexer.new
|
14
15
|
@fsm = :start
|
@@ -20,18 +21,22 @@ class RbCfgParser
|
|
20
21
|
@curr_values = nil
|
21
22
|
@curr_section = nil
|
22
23
|
@curr_section_lineno = 0
|
23
|
-
@log_cb = method(:
|
24
|
+
@log_cb = method(:_print_raw_line)
|
24
25
|
@verbose = 0
|
25
26
|
@content = {}
|
26
27
|
end
|
27
28
|
|
28
29
|
###
|
30
|
+
# parse the given file
|
31
|
+
# @return the content as a hash
|
29
32
|
def parse_file(path)
|
30
33
|
file_content = File.read(path)
|
31
34
|
parse_str(file_content)
|
32
35
|
end
|
33
36
|
|
34
37
|
###
|
38
|
+
# parse the given string
|
39
|
+
# @return the content as a hash
|
35
40
|
def parse_str(text)
|
36
41
|
text = text.join("\n") if text.is_a?(Array)
|
37
42
|
@rex.scan_setup(text)
|
@@ -42,7 +47,7 @@ class RbCfgParser
|
|
42
47
|
|
43
48
|
loop do
|
44
49
|
break if @token.nil?
|
45
|
-
print_dbg(format('fsm:%-17s tok[%d]:%-13s val:%-10s next[%d]=%s', @fsm, @tok_posn, @token[0], @token[1], @next_tok_posn, @next_token))
|
50
|
+
# print_dbg(format('fsm:%-17s tok[%d]:%-13s val:%-10s next[%d]=%s', @fsm, @tok_posn, @token[0], @token[1], @next_tok_posn, @next_token))
|
46
51
|
|
47
52
|
if @token[0] == :newline
|
48
53
|
@lineno += 1
|
@@ -77,6 +82,9 @@ class RbCfgParser
|
|
77
82
|
end
|
78
83
|
|
79
84
|
###
|
85
|
+
# in the start fsm state:
|
86
|
+
# read and handle the next value
|
87
|
+
# @return None
|
80
88
|
def fsm_start
|
81
89
|
if @token[0] == :newline
|
82
90
|
@lineno += 1
|
@@ -99,6 +107,9 @@ class RbCfgParser
|
|
99
107
|
end
|
100
108
|
|
101
109
|
###
|
110
|
+
# in the read_section fsm state:
|
111
|
+
# read and handle the next value
|
112
|
+
# @return None
|
102
113
|
def fsm_read_section
|
103
114
|
if @token[0] == :vrbl
|
104
115
|
@curr_section = @token[1]
|
@@ -118,6 +129,9 @@ class RbCfgParser
|
|
118
129
|
end
|
119
130
|
|
120
131
|
###
|
132
|
+
# in the assignments fsm state:
|
133
|
+
# read and handle the next value
|
134
|
+
# @return None
|
121
135
|
def fsm_assignments
|
122
136
|
if @token[0] == :newline
|
123
137
|
# skip
|
@@ -141,6 +155,9 @@ class RbCfgParser
|
|
141
155
|
end
|
142
156
|
|
143
157
|
###
|
158
|
+
# in the asgmt_read_values fsm state:
|
159
|
+
# read and handle the next value
|
160
|
+
# @return None
|
144
161
|
def fsm_asgmt_read_values
|
145
162
|
if @token[0] == :newline
|
146
163
|
# handle empty value
|
@@ -193,6 +210,8 @@ class RbCfgParser
|
|
193
210
|
end
|
194
211
|
|
195
212
|
###
|
213
|
+
# strip any quotes (single or double) from the given value
|
214
|
+
# @return the updated value
|
196
215
|
def strip_quotes(val)
|
197
216
|
if (val[0] == "'" && val[-1] == "'") || (val[0] == '"' && val[-1] == '"')
|
198
217
|
val[1..-2]
|
@@ -202,6 +221,8 @@ class RbCfgParser
|
|
202
221
|
end
|
203
222
|
|
204
223
|
###
|
224
|
+
# reset the section info
|
225
|
+
# @return None
|
205
226
|
def reset_section
|
206
227
|
@curr_section = nil
|
207
228
|
@curr_section_lineno = @lineno
|
@@ -210,6 +231,8 @@ class RbCfgParser
|
|
210
231
|
end
|
211
232
|
|
212
233
|
###
|
234
|
+
# save the current section that was found
|
235
|
+
# @return None
|
213
236
|
def save_curr_section
|
214
237
|
if @content.include?(@curr_section)
|
215
238
|
raise_err("found duplicate section: \"#{@curr_section}\"")
|
@@ -219,6 +242,8 @@ class RbCfgParser
|
|
219
242
|
end
|
220
243
|
|
221
244
|
###
|
245
|
+
# save the current assignment that found
|
246
|
+
# @return None
|
222
247
|
def save_curr_assignment
|
223
248
|
if @content[@curr_section].include?(@curr_vrbl)
|
224
249
|
raise_err("found duplicate variable: \"#{@curr_vrbl}\" in section: \"#{@curr_section}\"")
|
@@ -229,6 +254,8 @@ class RbCfgParser
|
|
229
254
|
end
|
230
255
|
|
231
256
|
###
|
257
|
+
# get the next token from the input stream, set token and tok_posn
|
258
|
+
# @return None
|
232
259
|
def get_next_token
|
233
260
|
@token = @next_token
|
234
261
|
@tok_posn = @next_tok_posn
|
@@ -246,22 +273,41 @@ class RbCfgParser
|
|
246
273
|
end
|
247
274
|
|
248
275
|
###
|
276
|
+
# raise an exception indicating an unexpected token was found
|
277
|
+
# @return does not return
|
249
278
|
def raise_unexpected_token
|
250
279
|
raise_err("Unexpected token #{@token}")
|
251
280
|
end
|
252
281
|
|
253
282
|
###
|
254
|
-
|
255
|
-
|
283
|
+
# raise an exception with the given text
|
284
|
+
# @param msg additional message to print
|
285
|
+
# @return does not return
|
286
|
+
def raise_err(msg)
|
287
|
+
raise("\nline: #{@lineno}] in state: #{@fsm}, #{msg}")
|
256
288
|
end
|
257
289
|
|
258
290
|
###
|
291
|
+
# print a debug line
|
292
|
+
# @param msg line to print
|
293
|
+
# @return None
|
259
294
|
def print_dbg(msg)
|
260
295
|
@log_cb.call(format('DBG line: %3d] %s', @lineno, msg)) if @verbose > 1
|
261
296
|
end
|
262
297
|
|
263
298
|
###
|
299
|
+
# print a line
|
300
|
+
# @param msg line to print
|
301
|
+
# @return None
|
264
302
|
def print_line(msg)
|
265
303
|
@log_cb.call(format(' --> line: %3d] %s', @lineno, msg)) if @verbose.positive?
|
266
304
|
end
|
305
|
+
|
306
|
+
###
|
307
|
+
# default callback to print a simple line to stdout
|
308
|
+
# @param msg line to print
|
309
|
+
# @return None
|
310
|
+
def _print_raw_line(msg)
|
311
|
+
# skip
|
312
|
+
end
|
267
313
|
end
|
data/lib/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rb_cfg_parser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- J. Arrizza
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-
|
11
|
+
date: 2025-04-12 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: ruby gem to parse and load .cfg files
|
14
14
|
email: cppgent0@gmail.com
|