nysol 3.0.1

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.
@@ -0,0 +1,538 @@
1
+ #!/usr/bin/env ruby
2
+ #/* ////////// LICENSE INFO ////////////////////
3
+ #
4
+ # * Copyright (C) 2013 by NYSOL CORPORATION
5
+ # *
6
+ # * Unless you have received this program directly from NYSOL pursuant
7
+ # * to the terms of a commercial license agreement with NYSOL, then
8
+ # * this program is licensed to you under the terms of the GNU Affero General
9
+ # * Public License (AGPL) as published by the Free Software Foundation,
10
+ # * either version 3 of the License, or (at your option) any later version.
11
+ # *
12
+ # * This program is distributed in the hope that it will be useful, but
13
+ # * WITHOUT ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF
14
+ # * NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
15
+ # *
16
+ # * Please refer to the AGPL (http://www.gnu.org/licenses/agpl-3.0.txt)
17
+ # * for more details.
18
+ #
19
+ # ////////// LICENSE INFO ////////////////////*/
20
+ require 'nysol/mcmd'
21
+
22
+ module MCMD
23
+ class MrubyParse
24
+ @@ST_RESERVE_WORD = ["class","module","if","unless","def"]
25
+ @@ED_RESERVE_WORD = ["end"]
26
+
27
+ @@ST_RESERVE_REG = @@ST_RESERVE_WORD.maddPrefix(["^","\\s+"]).maddSuffix(["$","\\s+"])
28
+ @@ED_RESERVE_REG = @@ED_RESERVE_WORD.maddPrefix(["^","\\s+"]).maddSuffix(["$","\\s+","\\."])
29
+ @@RESERVE_REG = @@ST_RESERVE_REG.concat(@@ED_RESERVE_REG)
30
+
31
+
32
+ @@ST_BLOCK_WORD = [["{"],["do"]]
33
+ @@ED_BLOCK_WORD = [["}"],["end"]]
34
+ #@@ST_BLOCK_REG = @@ST_BLOCK_WORD.maddPrefix(["^","\\s+"]).maddSuffix(["$","\\s+"])
35
+ #@@ED_BLOCK_REG = @@ED_BLOCK_WORD.maddPrefix(["^","\\s+"]).maddSuffix(["$","\\s+"])
36
+ @@ST_BLOCK_PTN = [@@ST_BLOCK_WORD[0].maddPrefix("\\s*").maddSuffix("\\s*"),@@ST_BLOCK_WORD[1].maddPrefix(["^","\\s+"]).maddSuffix(["$","\\s+"])]
37
+ @@ED_BLOCK_PTN = [@@ED_BLOCK_WORD[0].maddPrefix("\\s*").maddSuffix("\\s*"),@@ED_BLOCK_WORD[1].maddPrefix(["^","\\s+"]).maddSuffix(["$","\\s+"])]
38
+
39
+
40
+ def initialize(infn,lin,kwd,value)
41
+ @infn = infn
42
+ @limLin = lin
43
+ @kwd =kwd
44
+ @values =value
45
+ @ldata = ""
46
+ @blktype = 0
47
+ @stCnt = 0
48
+ @edCnt = 0
49
+ @stBLKCnt = 0
50
+ @edBLKCnt = 0
51
+ @recCnt = 0
52
+ @outFLG = false
53
+ @argST_FLG = false
54
+ @argED_FLG = false
55
+ @reqinfo = false
56
+ @state = 0
57
+ @outdata =[]
58
+ @reqdata =[]
59
+ @argsV =[]
60
+ @outline =""
61
+ @blkPtn = 0
62
+
63
+ end
64
+
65
+ def skip_space(pos)
66
+ return nil if pos==nil
67
+ rpos = @ldata.index(/\S/,pos)
68
+ if rpos == nil then
69
+ if @ldata.size == 0 then
70
+ rpos = pos
71
+ else
72
+ rpos = @ldata.size-1
73
+ end
74
+ end
75
+ return rpos
76
+ end
77
+
78
+ def match_Reserve(ldata,pos)
79
+ return nil if pos ==nil
80
+ lldata = ldata[pos..-1]
81
+
82
+ @@ST_RESERVE_WORD.each{|reserv|
83
+ return reserv if lldata.index(/^#{reserv}/)
84
+ }
85
+ @@ED_RESERVE_WORD.each{|reserv|
86
+ return reserv if lldata.index(/^#{reserv}/)
87
+ }
88
+ return nil
89
+ end
90
+
91
+ def match_BlkReserve(ldata,pos)
92
+ return nil if pos ==nil
93
+ lldata = ldata[pos..-1]
94
+ @@ST_BLOCK_WORD[@blktype].each{|reserv|
95
+ return reserv if lldata.index(/^#{reserv}/)
96
+ }
97
+ @@ED_BLOCK_WORD[@blktype].each{|reserv|
98
+ return reserv if lldata.index(/^#{reserv}/)
99
+ }
100
+ return nil
101
+ end
102
+
103
+
104
+ def analyze_blk_sub
105
+ if @ldata =~ /^\s*#/ then
106
+ @ldata =""
107
+ return
108
+ end
109
+ if @edCnt == @stCnt and @ldata =~ /^\s*require\s/ then
110
+ @outdata << @ldata
111
+ @ldata =""
112
+ return
113
+ end
114
+ pos = 0
115
+ pos = @ldata.index(/#{@@RESERVE_REG.join('|')}/,pos)
116
+ pos = skip_space(pos)
117
+ kwd = match_Reserve(@ldata,pos)
118
+ if pos == nil then
119
+ @outline << @ldata if @outFLG
120
+ pos = @ldata.size
121
+ elsif kwd == "end" then
122
+ @edCnt +=1
123
+ pos += kwd.size
124
+ @outline << @ldata[0...pos] if @outFLG
125
+ @outFLG =false if @edCnt == @stCnt
126
+ elsif kwd == "if" || kwd == "unless" then
127
+ @stCnt +=1 if @ldata[0...pos].index(/\S/) == nil
128
+ pos += kwd.size
129
+ @outline << @ldata[0...pos] if @outFLG
130
+ else
131
+ @stCnt +=1
132
+ pos += kwd.size
133
+ @outFLG =true
134
+ @outline << @ldata[0...pos] if @outFLG
135
+ end
136
+ @ldata = @ldata[pos..-1]
137
+ # 行終了
138
+ if @ldata.size==0 then
139
+ if @outline.size != 0 then
140
+ @outdata << @outline
141
+ @recCnt += 1
142
+ end
143
+ @recCnt = 0 if @edCnt == @stCnt
144
+ end
145
+ end
146
+
147
+ # skip,add_stcnt,add_edcnt,chgoutF
148
+ def analyze_blk_start
149
+ sp = @ldata.match(/(.*)#{@kwd}(#{@@ST_BLOCK_PTN[0].join('|')}|#{@@ST_BLOCK_PTN[1].join('|')})(.*)/)
150
+ sp = @ldata.match(/(.*)#{@kwd}\s*\(.*\)\s*(#{@@ST_BLOCK_PTN[0].join('|')}|#{@@ST_BLOCK_PTN[1].join('|')})(.*)/) if sp == nil
151
+ @blkPtn = 1 if sp[2].include?("do")
152
+ @ldata = sp[3]
153
+ end
154
+
155
+ def analyze_args
156
+ @ldata.lstrip!
157
+ return if @ldata.size==0
158
+ unless @argST_FLG then
159
+ raise "format ERROR" if @ldata[0]!="|"
160
+ @ldata=@ldata[1..-1]
161
+ @argST_FLG = true
162
+ end
163
+ out=""
164
+ unless @argED_FLG then
165
+ pos=0
166
+ while pos < @ldata.size do
167
+ case @ldata[pos]
168
+ when ","
169
+ @argsV << out
170
+ out =""
171
+ when "|"
172
+ @argsV << out
173
+ out =""
174
+ @argED_FLG = true
175
+ else
176
+ out << @ldata[pos]
177
+ end
178
+ pos += 1
179
+ break if @argED_FLG
180
+ end
181
+ @ldata = @ldata[pos..-1]
182
+ end
183
+ end
184
+
185
+ def analyze_blk
186
+ pos = 0
187
+ loop{
188
+ pos = @ldata.index(/(#{@@ST_BLOCK_PTN[@blkPtn].join('|')}|#{@@ED_BLOCK_PTN[@blkPtn].join('|')})(.*)/)
189
+ pos = skip_space(pos)
190
+ kwd = match_BlkReserve(@ldata,pos)
191
+ if pos == nil then
192
+ @outline << @ldata
193
+ pos = @ldata.size
194
+ else
195
+ case kwd
196
+ when "do","{"
197
+ @stBLKCnt+=1
198
+ when "end","}"
199
+ @edBLKCnt+=1
200
+ end
201
+ if @stBLKCnt == @edBLKCnt then
202
+ @outline << @ldata[0...pos]
203
+ pos = pos+kwd.size
204
+ else
205
+ pos = pos+kwd.size
206
+ @outline << @ldata[0...pos]
207
+ end
208
+ end
209
+ @ldata = @ldata[pos..-1]
210
+ break if @ldata.size == 0
211
+ break if @stBLKCnt == @edBLKCnt
212
+ }
213
+ end
214
+
215
+ def output(outfn)
216
+ File.open(outfn,"w"){|ofp|
217
+ ofp.puts("#!/usr/bin/env ruby")
218
+ ofp.puts("# -*- coding: utf-8 -*- ")
219
+ File.open(@infn,"r"){|ifp|
220
+ ifp.each_line do | ldata |
221
+ @outline = ""
222
+ @ldata = ldata.chomp
223
+ loop{
224
+ break if @ldata.size==0
225
+ case @state
226
+ when 0 # kwd(m2each) block前
227
+ if ifp.lineno == @limLin then
228
+ @state = 1 ; next ;
229
+ end
230
+ analyze_blk_sub()
231
+ when 1 # kwd(m2each) 行
232
+ analyze_blk_start()
233
+ @outFLG = false
234
+ @stBLKCnt += 1
235
+ @state = 2
236
+ outrqCHK =[]
237
+ @recCnt.times{ #requireは出力する
238
+ chkword = @outdata.pop
239
+ outrqCHK << chkword if @ldata =~ /^\s*require\s/
240
+ }
241
+ @outdata.each{|ld| ofp.puts(ld) }
242
+ outrqCHK.each{|ld| ofp.puts(ld) }
243
+ @values.each {|ld| ofp.puts(ld) }
244
+ @outdata=[]
245
+ when 2 # kwd(m2each) 引数
246
+ analyze_args()
247
+ if @argED_FLG then
248
+ @argsV.each_with_index{|v,i|
249
+ if i==0 then
250
+ ofp.puts("#{v} = ARGV[#{i}].split(',')")
251
+ ofp.puts("#{v} = #{v}[0] if #{v}.size==1")
252
+ else
253
+ ofp.puts("#{v} = ARGV[#{i}].to_i")
254
+ end
255
+ }
256
+ @state = 3
257
+ end
258
+ when 3 # kwd(m2each) ブロック
259
+ analyze_blk()
260
+ if @stBLKCnt == @edBLKCnt || @ldata.size == 0 then
261
+ ofp.puts(@outline)
262
+ @state = 4 if @stBLKCnt == @edBLKCnt
263
+ end
264
+ when 4 # end check
265
+ analyze_blk_sub()
266
+ if @edCnt == @stCnt then
267
+ @outdata=[]
268
+ @state = 5
269
+ end
270
+ when 5 # end check
271
+ analyze_blk_sub()
272
+ end
273
+ break if @ldata.size==0
274
+ }
275
+ end
276
+ @outdata.each{|ld| ofp.puts(ld) }
277
+ }
278
+ }
279
+ end
280
+ end
281
+
282
+ # inf:ソースファイル , lin:読み込み開始業 out:出力ファイル名 kwd:m2each,出力するローカル変数
283
+ def MCMD::outputblk(inf,lin,outf,kwd,values=[])
284
+
285
+
286
+ s_char_ex = ["\\s+class\\s+|^class\\s+|\\s+class$|^class$",
287
+ "\\s+module\\s+|^module\\s+|\\s+module$|^module$",
288
+ "\\s+if\\s+|^if\\s+|\\s+if$|^if$",
289
+ "\\s+def\\s+|^def\\s+|\\s+def$|^def$" ]
290
+ e_char_ex = ["\\s+end\\s+|^end\\s+|\\s+end\.|^end\.|\\s+end$|^end$"]
291
+
292
+
293
+ s_char = ["\\s*{\\s*","\\s+do\\s+|^do\\s+|\\s+do$|^do$" ]
294
+ e_char = ["\\s*}\\s*","\\s+end\\s+|^end\\s+|\\s+end\.|^end\.|\\s+end$|^end$"]
295
+ regpos = 0
296
+ nowL=0
297
+ start = false
298
+ arg = false
299
+ arg_s = false
300
+ blk_e = false
301
+ s_ch_cnt= 0
302
+ e_ch_cnt= 0
303
+ oscript = ""
304
+ args=[]
305
+ reqiureinfo=[]
306
+ afterinfo=[]
307
+ stcnt_ex =0
308
+ edcnt_ex =0
309
+ outF=false
310
+ reccnt=0
311
+ #開始行kwdの位置を決定してその後ブロックを抜き出す
312
+ File.open(outf,"w"){|ofp|
313
+ File.open(inf,"r"){|ifp|
314
+ while ldata = ifp.gets do
315
+ nowL+=1
316
+ if nowL < lin then
317
+ if ldata =~ /^\s*require\s|^\s*#/
318
+ reqiureinfo << ldata
319
+ reccnt +=1
320
+ next
321
+ end
322
+ ofset_e=0
323
+ out_e=""
324
+ loop {
325
+ pos_e = ldata.index(/(#{s_char_ex.join('|')}|#{e_char_ex[0]})/,ofset_e)
326
+ if pos_e != nil then
327
+ while pos_e < ldata.length do
328
+ break unless ldata[pos_e].match(/\s/)
329
+ pos_e+=1
330
+ end
331
+ case ldata[pos_e]
332
+ when "c" then
333
+ pos_e+=5
334
+ stcnt_ex+=1
335
+ outF = true
336
+ out_e << ldata[0...pos_e] if outF
337
+ when "m" then
338
+ pos_e+=6
339
+ stcnt_ex+=1
340
+ outF = true
341
+ out_e << ldata[0...pos_e] if outF
342
+ when "d" then
343
+ pos_e+=3
344
+ stcnt_ex+=1
345
+ outF = true
346
+ out_e << ldata[0...pos_e] if outF
347
+ when "i" then
348
+ ## 後ろif(空白以外あり)
349
+ if ldata[0...pos_e].index(/\S/) != nil then
350
+ pos_e+=2
351
+ out_e << ldata[0...pos_e] if outF
352
+ else
353
+ pos_e+=2
354
+ out_e << ldata[0...pos_e] if outF
355
+ stcnt_ex+=1
356
+ end
357
+
358
+ when "e" then
359
+ pos_e+=3
360
+ edcnt_ex+=1
361
+ out_e << ldata[0...pos_e] if outF
362
+ outF = false if stcnt_ex == edcnt_ex
363
+
364
+ end
365
+ else
366
+ out_e << ldata if outF
367
+ reqiureinfo << out_e
368
+ reccnt +=1
369
+ reccnt =0 if stcnt_ex == edcnt_ex
370
+ break
371
+ end
372
+ ldata=ldata[pos_e..-1]
373
+ }
374
+ next
375
+ end
376
+ # 開始チェック
377
+ unless start then
378
+ # start 位置 "v1".m2each"v2" "v3"
379
+ sp = ldata.match(/(.*)#{kwd}(#{s_char[0]}|#{s_char[1]})(.*)/)
380
+ sp = ldata.match(/(.*)#{kwd}\s*\(.*\)\s*(#{s_char[0]}|#{s_char[1]})(.*)/) if sp == nil
381
+ regpos = 1 if sp[2].include?("do")
382
+ ldata = sp[3]
383
+ start = true
384
+ outF =false
385
+ s_ch_cnt+=1
386
+ reccnt.times{ reqiureinfo.pop }
387
+ reqiureinfo.each{|ld| ofp.puts(ld) }
388
+ values.each{|ld| ofp.puts(ld) }
389
+ end
390
+ # argsチェック
391
+ unless arg then
392
+ ldata.lstrip!
393
+ next if ldata.length == 0
394
+ unless arg_s then
395
+ raise "format error" if ldata[0]!="|"
396
+ ldata = ldata[1..-1]
397
+ arg_s =true
398
+ end
399
+ pos=0
400
+ out =""
401
+ while pos < ldata.length do
402
+ case ldata[pos]
403
+ when ","
404
+ args << out
405
+ out =""
406
+ when "|"
407
+ args << out
408
+ out =""
409
+ arg = true
410
+ pos += 1
411
+ break
412
+ else
413
+ out << ldata[pos]
414
+ end
415
+ pos += 1
416
+ end
417
+ next unless arg
418
+ ldata =ldata[pos..-1]
419
+ args.each_with_index{|d,i|
420
+ if i==0 then
421
+ ofp.puts("#{d} = ARGV[#{i}].split(',')")
422
+ ofp.puts("#{d} = #{d}[0] if #{d}.size==1")
423
+
424
+ else
425
+ ofp.puts("#{d} = ARGV[#{i}].to_i")
426
+ end
427
+ }
428
+ end
429
+ unless blk_e then
430
+ offset=0
431
+ out =""
432
+ loop {
433
+ pos = ldata.index(/#{s_char[regpos]}|#{e_char[regpos]}/,offset)
434
+ if pos == nil then
435
+ out = ldata
436
+ break
437
+ end
438
+ while pos < ldata.length do
439
+ break unless ldata[pos].match(/\s/)
440
+ pos+=1
441
+ end
442
+ case ldata[pos]
443
+ when "d" then
444
+ s_ch_cnt+= 1
445
+ offset = pos+2
446
+ when "e" then
447
+ e_ch_cnt+= 1
448
+ offset = pos+3
449
+ when "{" then
450
+ s_ch_cnt+= 1
451
+ offset = pos+1
452
+ when "}" then
453
+ e_ch_cnt+= 1
454
+ offset = pos+1
455
+ end
456
+ if s_ch_cnt == e_ch_cnt then
457
+ out = ldata[0...pos]
458
+ ldata =ldata[pos..-1]
459
+ break
460
+ end
461
+ }
462
+ ofp.puts out
463
+ blk_e = true if s_ch_cnt == e_ch_cnt
464
+ next unless blk_e
465
+ end
466
+ # block以降
467
+ if ldata =~ /^\s*require\s|^\s*#/
468
+ afterinfo << ldata
469
+ reccnt +=1
470
+ next
471
+ end
472
+ ofset_e=0
473
+ out_e=""
474
+ loop {
475
+ pos_e = ldata.index(/(#{s_char_ex.join('|')}|#{e_char_ex[0]})/,ofset_e)
476
+ if pos_e != nil then
477
+ while pos_e < ldata.length do
478
+ break unless ldata[pos_e].match(/\s/)
479
+ pos_e+=1
480
+ end
481
+ case ldata[pos_e]
482
+ when "c" then
483
+ pos_e+=5
484
+ stcnt_ex+=1
485
+ out_e << ldata[0...pos_e] if outF
486
+ when "m" then
487
+ pos_e+=6
488
+ stcnt_ex+=1
489
+ out_e << ldata[0...pos_e] if outF
490
+ when "d" then
491
+ pos_e+=3
492
+ stcnt_ex+=1
493
+ out_e << ldata[0...pos_e] if outF
494
+ when "i" then
495
+ ## 後ろif(空白以外あり)
496
+ if ldata[0...pos_e].index(/\S/) != nil then
497
+ pos_e+=2
498
+ out_e << ldata[0...pos_e] if outF
499
+ else
500
+ pos_e+=2
501
+ out_e << ldata[0...pos_e] if outF
502
+ stcnt_ex+=1
503
+ end
504
+
505
+ when "e" then
506
+ pos_e+=3
507
+ edcnt_ex+=1
508
+ out_e << ldata[0...pos_e] if outF
509
+ outF = false if stcnt_ex == edcnt_ex
510
+
511
+ end
512
+ else
513
+ out_e << ldata if stcnt_ex != edcnt_ex && outF
514
+ afterinfo << out_e
515
+ reccnt +=1
516
+ reccnt =0 if stcnt_ex == edcnt_ex
517
+ break
518
+ end
519
+ ldata=ldata[pos_e..-1]
520
+ }
521
+ end
522
+ afterinfo.each{|ld| ofp.puts(ld) }
523
+ }
524
+ }
525
+
526
+ end
527
+
528
+
529
+ end
530
+
531
+ if __FILE__ == $0 then
532
+ infn= ARGV[0]
533
+ lin= ARGV[1].to_i
534
+ tag= ARGV[2]
535
+
536
+ MCMD::MrubyParse.new(infn,lin,tag,[]).output("xxxx_#{tag}_#{lin}")
537
+
538
+ end