Class: MCMD::MrubyParse

Inherits:
Object
  • Object
show all
Defined in:
lib/nysol/mrubyparse.rb

Constant Summary collapse

@@ST_RESERVE_WORD =
["class","module","if","unless","def"]
@@ED_RESERVE_WORD =
["end"]
@@ST_RESERVE_REG =
@@ST_RESERVE_WORD.maddPrefix(["^","\\s+"]).maddSuffix(["$","\\s+"])
@@ED_RESERVE_REG =
@@ED_RESERVE_WORD.maddPrefix(["^","\\s+"]).maddSuffix(["$","\\s+","\\."])
@@RESERVE_REG =
@@ST_RESERVE_REG.concat(@@ED_RESERVE_REG)
@@ST_BLOCK_WORD =
[["{"],["do"]]
@@ED_BLOCK_WORD =
[["}"],["end"]]
@@ST_BLOCK_PTN =
[@@ST_BLOCK_WORD[0].maddPrefix("\\s*").maddSuffix("\\s*"),@@ST_BLOCK_WORD[1].maddPrefix(["^","\\s+"]).maddSuffix(["$","\\s+"])]
@@ED_BLOCK_PTN =
[@@ED_BLOCK_WORD[0].maddPrefix("\\s*").maddSuffix("\\s*"),@@ED_BLOCK_WORD[1].maddPrefix(["^","\\s+"]).maddSuffix(["$","\\s+"])]

Instance Method Summary collapse

Constructor Details

#initialize(infn, lin, kwd, value) ⇒ MrubyParse

Returns a new instance of MrubyParse.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/nysol/mrubyparse.rb', line 40

def initialize(infn,lin,kwd,value)
	@infn = infn
	@limLin = lin 
	@kwd =kwd
	@values =value
	@ldata = ""
	@blktype = 0 
	@stCnt = 0
	@edCnt = 0 
	@stBLKCnt = 0
	@edBLKCnt = 0 
	@recCnt = 0
	@outFLG = false
	@argST_FLG = false
	@argED_FLG = false
	@reqinfo = false
	@state = 0
	@outdata =[]
	@reqdata =[]
	@argsV =[]
	@outline =""
	@blkPtn = 0

end

Instance Method Details

#analyze_argsObject



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/nysol/mrubyparse.rb', line 155

def analyze_args
	@ldata.lstrip!
	return if @ldata.size==0
	unless @argST_FLG then
		raise "format ERROR" if @ldata[0]!="|"
		@ldata=@ldata[1..-1]
		@argST_FLG = true
	end
	out=""
	unless @argED_FLG then
		pos=0
		while pos <  @ldata.size do
			case @ldata[pos]
			when ","
				@argsV << out
				out =""
			when "|"
				@argsV << out
				out =""
				@argED_FLG = true
			else
				out << @ldata[pos]
			end
			pos += 1
			break if @argED_FLG
		end
		@ldata = @ldata[pos..-1]
	end
end

#analyze_blkObject



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/nysol/mrubyparse.rb', line 185

def analyze_blk
	pos = 0 
	loop{
		pos = @ldata.index(/(#{@@ST_BLOCK_PTN[@blkPtn].join('|')}|#{@@ED_BLOCK_PTN[@blkPtn].join('|')})(.*)/)
		pos = skip_space(pos)
		kwd = match_BlkReserve(@ldata,pos)
		if pos == nil then
			@outline << @ldata
			pos = @ldata.size
		else
			case kwd
			when "do","{"
				@stBLKCnt+=1
			when "end","}"
				@edBLKCnt+=1
			end
			if @stBLKCnt == @edBLKCnt then
				@outline << @ldata[0...pos]
				pos = pos+kwd.size
			else
				pos = pos+kwd.size
				@outline << @ldata[0...pos]
			end
		end
		@ldata = @ldata[pos..-1]
		break if @ldata.size == 0
		break if @stBLKCnt == @edBLKCnt 
	}
end

#analyze_blk_startObject

skip,add_stcnt,add_edcnt,chgoutF



148
149
150
151
152
153
# File 'lib/nysol/mrubyparse.rb', line 148

def analyze_blk_start
	sp = @ldata.match(/(.*)#{@kwd}(#{@@ST_BLOCK_PTN[0].join('|')}|#{@@ST_BLOCK_PTN[1].join('|')})(.*)/)
	sp = @ldata.match(/(.*)#{@kwd}\s*\(.*\)\s*(#{@@ST_BLOCK_PTN[0].join('|')}|#{@@ST_BLOCK_PTN[1].join('|')})(.*)/) if sp == nil
	@blkPtn = 1 if sp[2].include?("do")
	@ldata = sp[3]
end

#analyze_blk_subObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/nysol/mrubyparse.rb', line 104

def analyze_blk_sub
	if @ldata =~ /^\s*#/ then
		@ldata =""
		return 			
	end
	if @edCnt == @stCnt and @ldata =~ /^\s*require\s/ then
		@outdata << @ldata
		@ldata =""
		return
	end
	pos = 0
	pos = @ldata.index(/#{@@RESERVE_REG.join('|')}/,pos)
	pos = skip_space(pos)
	kwd = match_Reserve(@ldata,pos)
	if pos == nil then
		@outline << @ldata if @outFLG
		pos = @ldata.size
	elsif kwd == "end" then
		@edCnt +=1
		pos += kwd.size
		@outline << @ldata[0...pos] if @outFLG
		@outFLG =false if @edCnt == @stCnt
	elsif kwd == "if" || kwd == "unless" then
		@stCnt +=1 if @ldata[0...pos].index(/\S/) == nil
		pos += kwd.size
		@outline << @ldata[0...pos] if @outFLG
	else	
		@stCnt +=1
		pos += kwd.size
		@outFLG =true
		@outline << @ldata[0...pos] if @outFLG
	end
	@ldata = @ldata[pos..-1]
	# 行終了
	if @ldata.size==0 then
		if @outline.size != 0 then
			@outdata << @outline 
			@recCnt += 1
		end
		@recCnt = 0 if @edCnt == @stCnt
	end
end

#match_BlkReserve(ldata, pos) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/nysol/mrubyparse.rb', line 91

def match_BlkReserve(ldata,pos)
	return nil if pos ==nil
	lldata = ldata[pos..-1]
	@@ST_BLOCK_WORD[@blktype].each{|reserv|
		return reserv if lldata.index(/^#{reserv}/)
	}
	@@ED_BLOCK_WORD[@blktype].each{|reserv|
		return reserv if lldata.index(/^#{reserv}/)
	}
	return nil
end

#match_Reserve(ldata, pos) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/nysol/mrubyparse.rb', line 78

def match_Reserve(ldata,pos)
	return nil if pos ==nil
	lldata = ldata[pos..-1]
	
	@@ST_RESERVE_WORD.each{|reserv|
		return reserv if lldata.index(/^#{reserv}/)
	}
	@@ED_RESERVE_WORD.each{|reserv|
		return reserv if lldata.index(/^#{reserv}/)
	}
	return nil
end

#output(outfn) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/nysol/mrubyparse.rb', line 215

def output(outfn)
	File.open(outfn,"w"){|ofp|
	ofp.puts("#!/usr/bin/env ruby")
	ofp.puts("# -*- coding: utf-8 -*- ")
	File.open(@infn,"r"){|ifp|
		ifp.each_line do | ldata |
			@outline = "" 
			@ldata = ldata.chomp
			loop{
				break if @ldata.size==0
				case @state
				when 0 # kwd(m2each) block前
					if ifp.lineno == @limLin then 
						@state = 1 ; next ; 
					end
					analyze_blk_sub()
				when 1 # kwd(m2each) 行
					analyze_blk_start()
					@outFLG = false
					@stBLKCnt += 1
					@state = 2
					outrqCHK =[]
					@recCnt.times{ #requireは出力する
						chkword = @outdata.pop 
						outrqCHK << chkword if @ldata =~ /^\s*require\s/ 
					}
					@outdata.each{|ld| ofp.puts(ld) }
					outrqCHK.each{|ld| ofp.puts(ld) }
					@values.each {|ld| ofp.puts(ld) }
					@outdata=[]
				when 2 # kwd(m2each) 引数
					analyze_args()
					if @argED_FLG then
						@argsV.each_with_index{|v,i|
							if i==0 then
								ofp.puts("#{v} = ARGV[#{i}].split(',')")
								ofp.puts("#{v} = #{v}[0] if #{v}.size==1")
							else
								ofp.puts("#{v} = ARGV[#{i}].to_i")
							end
						}
						@state = 3
					end
				when 3 # kwd(m2each) ブロック
					analyze_blk()
					if @stBLKCnt == @edBLKCnt || @ldata.size == 0 then
						ofp.puts(@outline)
						@state = 4 if @stBLKCnt == @edBLKCnt
					end
				when 4 # end check
					analyze_blk_sub()
					if @edCnt == @stCnt then
						@outdata=[]
						@state = 5
					end
				when 5 # end check
					analyze_blk_sub()
				end
				break if @ldata.size==0
			}
		end
		@outdata.each{|ld| ofp.puts(ld) }
	}
	}
end

#skip_space(pos) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/nysol/mrubyparse.rb', line 65

def skip_space(pos)
	return nil if pos==nil
	rpos = @ldata.index(/\S/,pos)
	if rpos == nil then
		if @ldata.size == 0 then
			rpos = pos
		else
			rpos = @ldata.size-1
		end
	end
	return rpos
end