mdextab 0.1.0 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/lib/mdextab.rb CHANGED
@@ -1,615 +1,500 @@
1
- require "mdextab/version"
2
-
3
- require 'mdextab/loggerx'
4
- require 'mdextab/token'
5
- require 'mdextab/table'
6
- require 'mdextab/tbody'
7
- require 'mdextab/td'
8
- require 'mdextab/th'
9
- require 'mdextab/token'
10
- require 'mdextab/tr'
11
- require 'mdextab/yamlx'
12
-
13
- require 'byebug'
14
-
1
+ # coding: utf-8
2
+ #
3
+ # MarkDownテーブル拡張モジュール
4
+ #
15
5
  module Mdextab
6
+ #
7
+ # エラークラス
8
+ #
16
9
  class Error < StandardError; end
17
10
 
11
+ #
12
+ # MarkDownテーブル拡張モジュールクラス
13
+ #
18
14
  class Mdextab
19
- def initialize(opt, fname, o_fname, yamlfname, auxiliaryYamlFname=nil)
15
+ require "mdextab/version"
16
+ require "mdextab/token"
17
+ require "mdextab/table"
18
+ require "mdextab/tbody"
19
+ require "mdextab/td"
20
+ require "mdextab/th"
21
+ require "mdextab/token"
22
+ require "mdextab/tr"
23
+ require "mdextab/makemdtab"
24
+ require "mdextab/layer"
25
+ require "messagex/loggerx"
26
+ require "filex"
27
+
28
+ require "byebug"
29
+
30
+ #
31
+ # 初期化
32
+ # @param [Hash] オプション
33
+ # @param [String] 入力Markdownファイル名
34
+ # @param [String] 出力Markdownファイル名
35
+ # @param [Messagex] mes Messagexクラスのインスタンス
36
+ def initialize(opt, fname, o_fname, mes=nil)
20
37
  @fname = fname
21
- @yamlfname = yamlfname
22
- @auxiliaryYamlFname = auxiliaryYamlFname
23
-
24
- @envStruct = Struct.new("Env" , :table, :star, :curState)
25
- @env = nil
26
- @envs = []
27
-
28
- @exit_nil=1
29
- @exit_exception=2
30
- @exit_next_state=3
31
- @exit_unknown=4
32
- @exit_table_end=5
33
- @exit_cannot_find_file=6
34
- @exit_cannot_write_file=7
35
- @exit_else=8
36
- @exit_illeagal_state=90
37
-
38
- @logger = Loggerx.new("log.txt")
39
- # @logger.level = Logger::WARN
40
- # @logger.level = Logger::INFO
41
- @logger.level = Logger::DEBUG if opt["debug"]
42
-
43
- # UNKNOWN > FATAL > ERROR > WARN > INFO > DEBUG
44
- # @logger.datetime_format = '%Y-%m-%d %H:%M:%S'
45
- @logger.datetime_format = ''
46
- #logger.formatter = proc do |severity, datetime, progname, msg|
47
- # ">>>>>> #{msg}\n"
48
- #end
38
+ @o_fname = o_fname
39
+
40
+ @mes = mes
41
+ unless @mes
42
+ @mes = Messagex::Messagex.new("EXIT_CODE_NORMAL_EXIT", 0, opt["debug"])
43
+ @mes.register_ecx
44
+ end
45
+
46
+ @mes.add_exitcode("EXIT_CODE_NEXT_STATE")
47
+ @mes.add_exitcode("EXIT_CODE_NIL")
48
+ @mes.add_exitcode("EXIT_CODE_EXCEPTION")
49
+ @mes.add_exitcode("EXIT_CODE_TABLE_END")
50
+ @mes.add_exitcode("EXIT_CODE_UNKNOWN")
51
+ @mes.add_exitcode("EXIT_CODE_ILLEAGAL_STATE")
52
+
53
+ Filex::Filex.setup(@mes)
54
+
49
55
  unless File.exist?(fname)
50
- mes="Can't find #{fname}"
51
- if @logger
52
- @logger.error(mes)
53
- else
54
- STDERR.puts(mes)
55
- end
56
- exit(@exit_cannot_find_file)
56
+ @mes.output_fatal("Can't find #{fname}")
57
+ exit(@mes.ec("EXIT_CODE_CANNOT_FIND_FILE"))
57
58
  end
58
59
 
59
- begin
60
- @output = File.open(o_fname, 'w')
61
- rescue => ex
62
- mes2="Can't write #{o_fname}"
63
- if @logger
64
- @logger.error(mes2)
65
- else
66
- STDERR.puts(mes2)
67
- end
68
- exit(@exit_cannot_write_file)
60
+ dir = File.dirname(o_fname)
61
+ if dir != "."
62
+ @mes.exc_make_directory(dir) { FileUtils.mkdir_p(dir) }
69
63
  end
64
+ @mes.exc_file_write(o_fname) { @output = File.open(o_fname, "w") }
70
65
 
71
- @fname = fname
72
- @state = {
73
- START: {TABLE_START: :IN_TABLE , ELSE: :OUT_OF_TABLE, STAR_START: :START, STAR_END: :START},
74
- OUT_OF_TABLE: {TABLE_START: :IN_TABLE , ELSE: :OUT_OF_TABLE, STAR_START: :OUT_OF_TABLE, STAR_END: :OUT_OF_TABLE, TD: :OUT_OF_TABLE },
75
- IN_TABLE: {TBODY_START: :IN_TABLE_BODY, TABLE_END: :OUT_OF_TABLE, ELSE: :IN_TABLE, TD: :IN_TD_NO_TBODY, TH: :IN_TH_NO_TBODY, TABLE_START: :IN_TABLE, STAR_START: :IN_TABLE, STAR_END: :IN_TABLE},
76
- IN_TABLE_BODY: { TH: :IN_TH , TD: :IN_TD , ELSE: :IN_TABLE_BODY, TABLE_START: :IN_TABLE_BODY, TBODY_END: :IN_TABLE, TABLE_END: :OUT_OF_TABLE, STAR_START: :IN_TABLE_BODY, STAR_END: :IN_TABLE_BODY},
77
- IN_TH: {ELSE: :IN_TH, TH: :IN_TH, TD: :IN_TD, TABLE_START: :IN_TH, STAR_START: :IN_TH, STAR_END: :IN_TH},
78
- IN_TH_NO_TBODY: {ELSE: :IN_TH_NO_TBODY, TH: :IN_TH_NO_TBODY, TD: :IN_TD_NO_TBODY, TABLE_START: :IN_TH_NO_TBODY, STAR_START: :IN_TH_NO_TBODY, STAR_END: :IN_TH_NO_TBODY},
79
- IN_TD: {ELSE: :IN_TD, TH: :IN_TH, TD: :IN_TD, TBODY_END: :IN_TABLE, TABLE_START: :IN_TD, STAR_START: :IN_TD, START_END: :IN_TD},
80
- IN_TD_NO_TBODY: {ELSE: :IN_TD_NO_TBODY, TH: :IN_TH_NO_TBODY, TD: :IN_TD_NO_TBODY, TABLE_START: :IN_TD_NO_TBODY, TABLE_END: :OUT_OF_TABLE, TBODY_END: :IN_TABLE, STAR_START: :IN_TD_NO_TBODY, STAR_END: :IN_TD_NO_TBODY},
81
- }
66
+ @token_op = Token.new(@mes)
67
+ @layer = Layer.new(@mes, @output)
82
68
 
69
+ set_state
83
70
  end
84
71
 
85
- def getToken(l, lineno)
86
- case l
87
- when /^\*S(.+)$/
88
- content = $1
89
- ret = Token.new(:STAR_START, {content: content, lineno: lineno})
90
- when /^\*E(.+)$/
91
- content = $1
92
- ret = Token.new(:STAR_END, {content: content, lineno: lineno})
93
- when /^\s*<table/
94
- if /^\s*<table>\s*$/.match?(l)
95
- @logger.debug(%Q!T1 :TABLE_START attr: nil!)
96
- ret = Token.new(:TABLE_START, {lineno: lineno})
97
- elsif (m=/^\s*<table\s+(.+)>\s*$/.match(l))
98
- @logger.debug(%Q!T2 :TABLE_START attr: #{m[1]}!)
99
- ret = Token.new(:TABLE_START, {attr: m[1], lineno: lineno})
100
- else
101
- @logger.debug("E002 l=#{l}")
102
- ret = nil
103
- end
104
- when /^\s*<tbody/
105
- if /^\s*<tbody>\s*$/.match?(l)
106
- ret = Token.new(:TBODY_START, {lineno: lineno})
107
- else
108
- @logger.debug("E003 l=#{l}")
109
- ret = nil
110
- end
111
-
112
- when /^\s*(\:+)(.*)$/
113
- nth = $1.size
114
- cont = $2
115
- if (m=/^th(.*)/.match(cont))
116
- cont2 = m[1]
117
- @logger.debug( %Q!cont2=#{cont2}! )
118
- if (m2=/^\s(.*)/.match(cont2))
119
- cont3 = m2[1]
120
- if (m3=/^([^<]*)>(.*)$/.match(cont3))
121
- attr = m3[1]
122
- cont4 = m3[2]
123
- @logger.debug( %Q!1 :TH , { nth: #{nth} , attr: #{attr} , content: #{cont4}}! )
124
- ret = Token.new(:TH , { nth: nth , attr: attr , content: cont4, lineno: lineno})
125
- else
126
- # error
127
- #ret = nil
128
- @logger.debug( %Q!2 :ELSE , { nth: #{nth} , attr: nil , content: #{cont}}! )
129
- ret = Token.new(:ELSE , { nth: nth , attr: nil , content: cont, lineno: lineno})
130
- end
131
- elsif (m=/^>(.*)$/.match(cont2))
132
- cont3 = m[1]
133
- @logger.debug( %Q!3 :TH , { nth: #{nth} , attr: nil , content: #{cont3}}! )
134
- ret = Token.new(:TH , { nth: nth , attr: nil , content: cont3, lineno: lineno})
135
- else
136
- @logger.debug( %Q!4 :ELSE , { nth: #{nth} , attr: nil , content: #{cont}}! )
137
- ret = Token.new(:ELSE , { nth: nth , attr: nil , content: cont, lineno: lineno})
138
- end
139
- elsif (m=/^([^<]*)>(.*)$/.match(cont))
140
- attr = m[1]
141
- cont2 = m[2]
142
- @logger.debug( %Q!5 :TD , { nth: #{nth} , attr: #{attr} , content: #{cont2}}! )
143
- ret = Token.new(:TD , {nth: nth , attr: attr , content: cont2, lineno: lineno})
144
- else
145
- @logger.debug( %Q!6 :TD , { nth: #{nth} , attr: #{attr} , content: #{cont}}! )
146
- ret = Token.new(:TD , { nth: nth , attr: attr , content: cont , lineno: lineno})
147
- end
148
- when /^\s*<\/table/
149
- if /^\s*<\/table>\s*$/.match?(l)
150
- ret = Token.new(:TABLE_END, {lineno: lineno})
151
- else
152
- @logger.debug("E000 l=#{l}")
153
- ret = nil
154
- end
155
- when /^\s*<\/tbody/
156
- if /^\s*<\/tbody>\s*$/.match?(l)
157
- ret = Token.new(:TBODY_END, {lineno: lineno})
158
- else
159
- @logger.debug("E001 l=#{l}")
160
- ret = nil
161
- end
162
- else
163
- ret = Token.new(:ELSE, {content: l, lineno: lineno})
164
- end
165
-
166
- ret
72
+ #
73
+ # テーブル拡張向け構文解析用状態遷移テーブルの設定
74
+ #
75
+ def set_state
76
+ @states = {
77
+ START: { TABLE_START: :IN_TABLE, ELSE: :OUT_OF_TABLE, STAR_START: :START, STAR_END: :START },
78
+ OUT_OF_TABLE: { TABLE_START: :IN_TABLE, ELSE: :OUT_OF_TABLE, STAR_START: :OUT_OF_TABLE, STAR_END: :OUT_OF_TABLE, TD: :OUT_OF_TABLE },
79
+ IN_TABLE: { TBODY_START: :IN_TABLE_BODY, TABLE_END: :OUT_OF_TABLE, ELSE: :IN_TABLE, TD: :IN_TD_NO_TBODY, TH: :IN_TH_NO_TBODY, TABLE_START: :IN_TABLE, STAR_START: :IN_TABLE, STAR_END: :IN_TABLE },
80
+ IN_TABLE_BODY: { TH: :IN_TH, TD: :IN_TD, ELSE: :IN_TABLE_BODY, TABLE_START: :IN_TABLE_BODY, TBODY_END: :IN_TABLE, TABLE_END: :OUT_OF_TABLE, STAR_START: :IN_TABLE_BODY, STAR_END: :IN_TABLE_BODY },
81
+ IN_TH: { ELSE: :IN_TH, TH: :IN_TH, TD: :IN_TD, TABLE_START: :IN_TH, STAR_START: :IN_TH, STAR_END: :IN_TH },
82
+ IN_TH_NO_TBODY: { ELSE: :IN_TH_NO_TBODY, TH: :IN_TH_NO_TBODY, TD: :IN_TD_NO_TBODY, TABLE_START: :IN_TH_NO_TBODY, STAR_START: :IN_TH_NO_TBODY, STAR_END: :IN_TH_NO_TBODY },
83
+ IN_TD: { ELSE: :IN_TD, TH: :IN_TH, TD: :IN_TD, TBODY_END: :IN_TABLE, TABLE_START: :IN_TD, STAR_START: :IN_TD, START_END: :IN_TD },
84
+ IN_TD_NO_TBODY: { ELSE: :IN_TD_NO_TBODY, TH: :IN_TH_NO_TBODY, TD: :IN_TD_NO_TBODY, TABLE_START: :IN_TD_NO_TBODY, TABLE_END: :OUT_OF_TABLE, TBODY_END: :IN_TABLE, STAR_START: :IN_TD_NO_TBODY, STAR_END: :IN_TD_NO_TBODY },
85
+ }
167
86
  end
168
87
 
169
- def parse
170
- @env = getNewEnv()
171
- lineno=0
172
- Yamlx.loadSetting(@yamlfname , @auxiliaryYamlFname, @fname).each{ |l|
88
+ #
89
+ # テーブル拡張向け構文解析
90
+ # @param [Hash] eRubyスクリプト向け置換用ハッシュ
91
+ def parse(hash)
92
+ lineno = 0
93
+ @layer.add_layer(@fname, lineno)
94
+ Filex::Filex.check_and_expand_file_lines(@fname, hash, @mes).each do |line|
173
95
  lineno += 1
174
- token = getToken(l, lineno)
96
+ token = @token_op.get_token(line, lineno)
175
97
  kind = token.kind
176
98
 
177
- @logger.debug("kind=#{kind}")
178
- @logger.debug(%Q!(source)#{lineno}:#{l}!)
179
- if @env.curState == nil
180
- @logger.error("(script)#{__LINE__}| @env.curState=nil")
99
+ @mes.output_debug("layer.size=#{@layer.size}")
100
+ @mes.output_debug("token.kind=#{kind}")
101
+ @mes.output_debug(%Q!(source)#{lineno}:#{line}!)
102
+ if @layer.cur_state.nil?
103
+ @mes.output_error("(script)#{__LINE__}| @layer.cur_state=nil")
181
104
  else
182
- @logger.debug("(script)#{__LINE__}| @env.curState=#{@env.curState}")
105
+ @mes.output_debug("(script)#{__LINE__}| @layer.cur_state=#{@layer.cur_state}")
183
106
  end
184
107
  # debug_envs(5, token)
185
108
 
186
- ret = processOneLine(@env.curState, token, l, lineno)
187
- unless ret
188
- @logger.error("processOneLine returns nil")
189
- exit(@exit_next_state)
109
+ @layer.cur_state = process_one_line(@layer.cur_state, token, line, lineno)
110
+ unless @layer.cur_state
111
+ @mes.output_fatal("process_one_line returns nil")
112
+ exit(@mes.ec("EXIT_CODE_NEXT_STATE"))
190
113
  end
191
- @env.curState = ret
192
114
 
193
- v=@env.curState
194
- v="nil" unless v
195
- @logger.debug("NEXT kind=#{kind} @env.curState=#{v}")
196
- @logger.debug("-----")
197
- }
198
- checkEnvs
115
+ @mes.output_debug("NEXT kind=#{kind} @layer.cur_state=#{@layer.cur_state}")
116
+ @mes.output_debug("-----")
117
+ end
118
+ @layer.check_layers(@fname)
199
119
  end
200
120
 
121
+ #
122
+ # テーブル拡張向け構文解析
123
+ # @param [String] eRubyスクリプト向け置換用データファイル名(YAML形式)
124
+ def parse2(yamlfname)
125
+ hs = Filex::Filex.check_and_load_yamlfile(yamlfname, @mes)
126
+ parse(hs)
127
+ end
201
128
 
202
- def getNextState(token, line)
129
+ #
130
+ # テーブル拡張向け構文解析での次の状態を得る
131
+ # @param [String] token 読み込んだトークン
132
+ # @param [String] line 現在行
133
+ # @param [String] lineno 現在行の行番号
134
+ def get_next_state(token, line, lineno)
203
135
  kind = token.kind
204
- @logger.debug("#{__LINE__}|@env.curState=#{@env.curState} #{@env.curState.class}")
205
- tmp = @state[@env.curState]
206
- if tmp == nil
207
- @logger.error(%Q!kind=#{kind}!)
208
- @logger.error("=== tmp == nil")
209
- exit(@exit_nil)
136
+ @mes.output_debug("#{__LINE__}|@layer.cur_state=#{@layer.cur_state} #{@layer.cur_state.class}")
137
+ state_level1 = @states[@layer.cur_state]
138
+ if state_level1.nil?
139
+ @mes.output_error(%Q(token.kind=#{kind} | cur_state=#{@layer.cur_state}))
140
+ @mes.output_error("=== state_level1 == nil")
141
+ @mes.output_fatal("Next State is nil")
142
+ @mes.output_fatal("@fname=#{@fname} | lineno=#{lineno}")
143
+ exit(@mes.ec("EXIT_CODE_NIL"))
210
144
  else
211
- @logger.debug("tmp=#{tmp}")
212
- end
213
- @logger.debug("#{__LINE__}|kind=#{kind}")
214
-
215
- begin
216
- nextState = tmp[kind]
217
- @logger.debug("#{__LINE__}|nextState=#{nextState}")
218
- rescue
219
- @logger.fatal(@env.curState)
220
- @logger.fatal(kind)
221
- @logger.fatal(nextState)
222
- @logger.fatal("+++")
223
- exit(@exit_exception)
145
+ @mes.output_debug("state_level1=#{state_level1}")
224
146
  end
225
- @logger.debug("#{__LINE__}|nextState=#{nextState}")
226
- nextState
227
- end
147
+ @mes.output_debug("#{__LINE__}|kind=#{kind}")
228
148
 
229
- def debug_envs(n, token)
230
- @logger.debug( "***#{n}")
231
- @envs.each_with_index{|x,ind|
232
- @logger.debug( "@envs[#{ind}]=#{@envs[ind]}")
233
- }
234
- @logger.debug( "******#{n}")
235
- @logger.debug( "getNewEnv 1 token.kind=#{token.kind} @env.curState=#{@env.curState}" )
236
- end
237
-
238
- def processNestedTableStart(token, lineno)
239
- if @env.table.tbody == nil
240
- @env.table.add_tbody(lineno)
149
+ begin
150
+ next_state = state_level1[kind]
151
+ @mes.output_debug("#{__LINE__}|next_state=#{next_state}")
152
+ rescue StandardError
153
+ @mes.output_fatal("@layer.cur_state=#{@layer.cur_state}")
154
+ @mes.output_fatal("kind=#{kind}")
155
+ @mes.output_fatal("next_state=#{next_state}")
156
+ exit(@mes.ec("EXIT_CODE_EXCEPTION"))
241
157
  end
242
- @logger.debug( "B getNewEnv 1 token.kind=#{token.kind} token.opt[:lineno]=#{token.opt[:lineno]} @env.curState=#{@env.curState}" )
243
- @env = getNewEnv(:OUT_OF_TABLE)
244
- @env.table = Table.new(token.opt[:lineno], @logger, token.opt[:attr])
245
- @logger.debug( "getNewEnv 3 token.kind=#{token.kind} @env.curState=#{@env.curState}" )
158
+ @mes.output_debug("#{__LINE__}|next_state=#{next_state}")
159
+ next_state
246
160
  end
247
161
 
248
- def processTableEnd(token)
249
- #byebug
250
- prevEnv = peekPrevEnv()
251
- if prevEnv
252
- tmp_table = @env.table
253
-
254
- @logger.debug( "B getPrevEnv 1 token.kind=#{token.kind} token.opt[:lineno]=#{token.opt[:lineno]} @env.curState=#{@env.curState}" )
255
- @logger.debug( "@envs.size=#{@envs.size}")
256
- @env = getPrevEnv()
257
- @return_from_nested_env = true
258
- @logger.debug( "getPrevEnv 1 token.kind=#{token.kind} token.opt[:lineno]=#{token.opt[:lineno]} @env.curState=#{@env.curState}" )
259
-
260
- @logger.debug( "0 - processTableEnd @env.curState=#{@env.curState} @return_from_nested_env=#{@return_from_nested_env}")
261
- @logger.debug( tmp_table )
262
- case @env.curState
263
- when :IN_TD
264
- @env.table.tdAppend(tmp_table, @env.star)
265
- when :IN_TD_NO_TBODY
266
- @env.table.tdAppend(tmp_table, @env.star)
267
- when :IN_TH
268
- @env.table.thAppend(tmp_table, @env.star)
269
- when :IN_TH_NO_TBODY
270
- @env.table.thAppend(tmp_table,@env.star)
271
- when :IN_TABLE
272
- if @env.table == nil
273
- @logger.fatal( "In processNestedTableEnv: @env.table=nil token.kind=#{token.kind} token.opt[:lineno]=#{token.opt[:lineno]} @env.curState=#{@env.curState}" )
274
- raise
275
- end
276
- @env.table.add(tmp_table)
277
- when :IN_TABLE_BODY
278
- @env.table.add(tmp_table)
279
- when :START
280
- # do nothing?
162
+ #
163
+ # トークンELSEに対応する行の出力
164
+ # @param [String] str トークンELSEに対応する行
165
+ def output_in_else(str)
166
+ if @layer.star
167
+ if str.match?(/^\s*$/)
168
+ @mes.output_debug("InElse do nothing")
281
169
  else
282
- v = @env.curState
283
- v = "nil" unless v
284
- @logger.error("E100 @env.curState=#{v}")
285
- @logger.error("@env.table=#{@env.table}")
286
- exit(@exit_table_end)
170
+ @mes.exc_file_write(@o_fname) { @output.puts(str) }
287
171
  end
288
172
  else
289
- @logger.debug( "1 - processTableEnd @env.curState=#{@env.curState} @return_from_nested_env~#{@return_from_nested_env}")
290
- @output.puts(@env.table.end)
173
+ @mes.exc_file_write(@o_fname) { @output.puts(str) }
291
174
  end
292
175
  end
293
176
 
294
- def outputInElse(str)
295
- if @env.star
177
+ #
178
+ # テーブルのTHタグの一部として、トークンELSEに対応する行を追加
179
+ # @param [String] str トークンELSEに対応する行
180
+ def table_th_append_in_else(str)
181
+ if @layer.star
296
182
  if str.match?(/^\s*$/)
297
- @logger.debug("InElse do nothing")
183
+ @mes.output_debug("ThAppend InElse")
298
184
  else
299
- @output.puts(str)
185
+ @layer.table.th_append(str, @layer.star)
300
186
  end
301
187
  else
302
- @output.puts(str)
303
- end
188
+ @layer.table.th_append(str, @layer.star)
189
+ end
304
190
  end
305
191
 
306
- def tableThAppendInElse(str)
307
- if @env.star
192
+ #
193
+ # テーブルのTDタグの一部として、トークンELSEに対応する行を追加
194
+ # @param [String] str トークンELSEに対応する行
195
+ def table_td_append_in_else(str)
196
+ if @layer.star
308
197
  if str.match?(/^\s*$/)
309
- @logger.debug("ThAppend InElse")
198
+ @mes.output_debug("TdAppend InElse")
310
199
  else
311
- @env.table.thAppend(str,@env.star)
200
+ @layer.table.td_append(str, @layer.star)
312
201
  end
313
202
  else
314
- @env.table.thAppend(str,@env.star)
315
- end
203
+ @layer.table.td_append(str, @layer.star)
204
+ end
316
205
  end
317
206
 
318
- def tableTdAppendInElse(str)
319
- if @env.star
320
- if str.match?(/^\s*$/)
321
- @logger.debug("TdAppend InElse")
322
- else
323
- @env.table.tdAppend(str,@env.star)
324
- end
207
+ #
208
+ # START状態でのトークンと現在行の処理
209
+ # @param [String] token 読み込んだトークン
210
+ # @param [String] line 現在行
211
+ # @param [String] lineno 現在行の行番号
212
+ def process_one_line_for_start(token, line, lineno)
213
+ case token.kind
214
+ when :TABLE_START
215
+ @layer.table = Table.new(lineno, @mes, token.opt[:attr])
216
+ when :ELSE
217
+ # threw
218
+ output_in_else(token.opt[:content])
219
+ when :STAR_START
220
+ @layer.star = true
221
+ output_in_else("*" + token.opt[:content])
222
+ when :STAR_END
223
+ @layer.star = false
224
+ output_in_else("*" + token.opt[:content])
225
+ output_in_else(token.opt[:content])
325
226
  else
326
- @env.table.tdAppend(str,@env.star)
327
- end
227
+ @mes.output_fatal("In :START unknown tag=(#{token.kind}) in process_one_line_for_start")
228
+ @mes.output_fatal("@fname=#{@fname} | lineno=#{lineno}")
229
+ exit(@mes.ec("EXIT_CODE_UNKNOWN"))
230
+ end
328
231
  end
329
232
 
330
- def processOneLine(curState, token, line, lineno)
331
- @return_from_nested_env = false
332
- case @env.curState
333
- when :START
334
- case token.kind
335
- when :TABLE_START
336
- @env.table = Table.new(lineno, @logger,token.opt[:attr])
337
- when :ELSE
338
- # threw
339
- outputInElse(token.opt[:content])
340
- when :STAR_START
341
- @env.star = true
342
- outputInElse('*'+token.opt[:content])
343
- when :STAR_END
344
- @env.star = false
345
- outputInElse('*'+token.opt[:content])
346
- outputInElse(token.opt[:content])
347
- else
348
- @logger.error( ":START [unknown]")
349
- exit(@exit_unknown)
350
- end
351
- when :OUT_OF_TABLE
352
- case token.kind
353
- when :TABLE_START
354
- @env.table = Table.new(lineno, @logger,token.opt[:attr])
355
- when :ELSE
356
- outputInElse(token.opt[:content])
357
- when :STAR_START
358
- @env.star = true
359
- outputInElse('*'+token.opt[:content])
360
- when :STAR_END
361
- @env.star = false
362
- outputInElse('*'+token.opt[:content])
363
- outputInElse(token.opt[:content])
364
- when :TD
365
- # treat as :ELSE
366
- outputInElse(":" + token.opt[:content])
367
- else
368
- @logger.error( ":OUT_OF_TABLE [unknown]")
369
- exit(@exit_unknown)
370
- end
371
- when :IN_TABLE
372
- case token.kind
373
- when :TBODY_START
374
- @env.table.add_tbody(lineno)
375
- when :TABLE_END
376
- processTableEnd(token)
377
- when :ELSE
378
- outputInElse(token.opt[:content])
379
- when :TD
380
- @logger.debug(token)
381
- @env.table.add_tbody(lineno)
382
- @env.table.add_td(lineno, token.opt[:content], token.opt[:nth], token.opt[:attr],@env.star)
383
- when :TH
384
- @env.table.add_tbody(lineno)
385
- @env.table.add_th(lineno, token.opt[:content], token.opt[:nth], token.opt[:attr],@env.star)
386
- when :TABLE_START
387
- processNestedTableStart(token, lineno)
388
- when :STAR_START
389
- @env.star = true
390
- outputInElse('*'+token.opt[:content])
391
- when :STAR_END
392
- @env.star = false
393
- outputInElse('*'+token.opt[:content])
394
- else
395
- @logger.error( ":IN_TABLE [unknown]")
396
- exit(@exit_unknown)
397
- end
398
- when :IN_TABLE_BODY
399
- case token.kind
400
- when :TH
401
- @env.table.add_th(lineno, token.opt[:content], token.opt[:nth], token.opt[:attr],@env.star)
402
- when :TD
403
- @logger.debug(token)
404
- @env.table.add_td(lineno, token.opt[:content], token.opt[:nth], token.opt[:attr],@env.star)
405
- when :ELSE
406
- outputInElse(token.opt[:content])
407
- when :TABLE_START
408
- processNestedTableStart(token, lineno)
409
- when :TBODY_END
410
- # processTableEnd(token)
411
- when :TABLE_END
412
- processTableEnd(token)
413
- when :STAR_START
414
- @env.star = true
415
- outputInElse('*'+token.opt[:content])
416
- when :STAR_END
417
- @env.star = false
418
- outputInElse('*'+token.opt[:content])
419
- else
420
- @logger.error( ":IN_TABLE_BODY [unknown]")
421
- exit(@exit_unknown)
422
- #
423
- end
424
- when :IN_TH
425
- case token.kind
426
- when :ELSE
427
- tableThAppendInElse(token.opt[:content])
428
- when :TH
429
- @env.table.add_th(lineno, token.opt[:content], token.opt[:nth], token.opt[:attr],@env.star)
430
- when :TD
431
- @env.table.add_td(lineno, token.opt[:content], token.opt[:nth], token.opt[:attr],@env.star)
432
- when :TABLE_START
433
- processNestedTableStart(token, lineno)
434
- =begin
435
- # debug_envs(3, token)
436
- @env = getNewEnv(:OUT_OF_TABLE)
437
- @env.table = Table.new(lineno, @logger,token.opt[:attr])
438
- @logger.debug( "getNewEnv 2 token.kind=#{token.kind} @env.curState=#{@env.curState}" )
439
- # debug_envs(4, token)
440
- =end
441
- when :STAR_START
442
- @env.star = true
443
- tableThAppendInElse('*'+token.opt[:content])
444
- when :STAR_END
445
- @env.star = false
446
- tableThAppendInElse('*'+token.opt[:content])
447
- else
448
- @logger.error( ":IN_TH [unknown]")
449
- exit(@exit_unknown)
450
- #
451
- end
452
- when :IN_TH_NO_TBODY
453
- case token.kind
454
- when :ELSE
455
- tableThAppendInElse(token.opt[:content])
456
- when :TH
457
- @env.table.add_th(lineno, token.opt[:content], token.opt[:nth], token.opt[:attr],@env.star)
458
- when :TD
459
- @env.table.add_td(lineno, token.opt[:content], token.opt[:nth], token.opt[:attr],@env.star)
460
- when :TABLE_START
461
- processNestedTableStart(token, lineno)
462
- =begin
463
- # debug_envs(3, token)
464
- @env = getNewEnv(:OUT_OF_TABLE)
465
- @env.table = Table.new(lineno, @logger,token.opt[:attr])
466
- @logger.debug( "getNewEnv 2 token.kind=#{token.kind} @env.curState=#{@env.curState}" )
467
- # debug_envs(4, token)
468
- =end
469
- when :STAR_START
470
- @env.star = true
471
- tableThAppendInElse('*'+token.opt[:content])
472
- when :STAR_END
473
- @env.star = false
474
- tableThAppendInElse('*'+token.opt[:content])
475
- else
476
- @logger.error( ":IN_TH_NO_TBODY [unknown]")
477
- exit(@exit_unknown)
478
- #
479
- end
480
- when :IN_TD
481
- case token.kind
482
- when :ELSE
483
- tableTdAppendInElse(token.opt[:content])
484
- when :TH
485
- @env.table.add_th(lineno, token.opt[:content], token.opt[:nth], token.opt[:attr],@env.star)
486
- when :TD
487
- @env.table.add_td(lineno, token.opt[:content], token.opt[:nth], token.opt[:attr],@env.star)
488
- when :TBODY_END
489
- @env.table.tbody_end()
490
- when :TABLE_START
491
- processNestedTableStart(token, lineno)
492
- when :STAR_START
493
- @env.star = true
494
- tableTdAppendInElse('*'+token.opt[:content])
495
- when :STAR_END
496
- @env.star = false
497
- tableTdAppendInElse('*'+token.opt[:content])
498
- else
499
- @logger.error( ":IN_TD [unknown]")
500
- exit(@exit_unknown)
501
- #
502
- end
503
- when :IN_TD_NO_TBODY
504
- case token.kind
505
- when :ELSE
506
- tableTdAppendInElse(token.opt[:content])
507
- when :TH
508
- @env.table.add_th(lineno, token.opt[:content], token.opt[:nth], token.opt[:attr],@env.star)
509
- when :TD
510
- @env.table.add_td(lineno, token.opt[:content], token.opt[:nth], token.opt[:attr],@env.star)
511
- when :TABLE_START
512
- processNestedTableStart(token, lineno)
513
- when :TABLE_END
514
- processTableEnd(token)
515
- when :TBODY_END
516
- @env.table.tbody_end()
517
- when :STAR_START
518
- @env.star = true
519
- tableTdAppendInElse('*'+token.opt[:content])
520
- when :STAR_END
521
- @env.star = false
522
- tableTdAppendInElse('*'+token.opt[:content])
523
- else
524
- @logger.error( ":IN_TD_NO_TBODY [unknown]")
525
- exit(@exit_unknown)
526
- #
527
- end
233
+ #
234
+ # OUT_OF_TABLE状態でのトークンと現在行の処理
235
+ # @param [String] token 読み込んだトークン
236
+ # @param [String] line 現在行
237
+ # @param [String] lineno 現在行の行番号
238
+ def process_one_line_for_out_of_table(token, line, lineno)
239
+ case token.kind
240
+ when :TABLE_START
241
+ @layer.table = Table.new(lineno, @mes, token.opt[:attr])
242
+ when :ELSE
243
+ output_in_else(token.opt[:content])
244
+ when :STAR_START
245
+ @layer.star = true
246
+ output_in_else("*" + token.opt[:content])
247
+ when :STAR_END
248
+ @layer.star = false
249
+ output_in_else("*" + token.opt[:content])
250
+ output_in_else(token.opt[:content])
251
+ when :TD
252
+ # treat as :ELSE
253
+ output_in_else(":" + token.opt[:content])
528
254
  else
529
- @logger.error( "unknown state")
530
- exit(@exit_unknown)
531
- #
255
+ @mes.output_fatal("In :OUT_OF_TABLE unknown tag=(#{token.kind}) in process_one_line_for_out_of_table")
256
+ @mes.output_fatal("@fname=#{@fname} | lineno=#{lineno}")
257
+ @layer.check_layers(@fname)
532
258
  end
259
+ end
260
+
261
+ #
262
+ # TABLE_END状態でのトークン処理
263
+ # @param [String] token 読み込んだトークン
264
+ def process_one_line_for_table_end(token)
265
+ @layer.process_table_end(token)
266
+ return if @layer.return_from_nested_env
533
267
 
534
- unless @return_from_nested_env
535
- nextState = getNextState(token, line)
536
- @logger.debug("#{__LINE__}|nextState=#{nextState}")
268
+ @mes.output_debug("1 - process_one_line_table_end cur_state=#{@layer.cur_state} @return_from_nested_env~#{@layer.return_from_nested_env}")
269
+ @mes.exc_file_write(@o_fname) { @output.puts(@layer.table.end) }
270
+ end
271
+
272
+ #
273
+ # IN_TABLE状態でのトークンと現在行の処理
274
+ # @param [String] token 読み込んだトークン
275
+ # @param [String] line 現在行
276
+ # @param [String] lineno 現在行の行番号
277
+ def process_one_line_for_in_table(token, line, lineno)
278
+ case token.kind
279
+ when :TBODY_START
280
+ @layer.table.add_tbody(lineno)
281
+ when :TABLE_END
282
+ process_one_line_for_table_end(token)
283
+ when :ELSE
284
+ output_in_else(token.opt[:content])
285
+ when :TD
286
+ @mes.output_debug(token)
287
+ @layer.table.add_tbody(lineno)
288
+ @layer.table.add_td(lineno, token.opt[:content], token.opt[:nth], token.opt[:attr], @layer.star)
289
+ when :TH
290
+ @layer.table.add_tbody(lineno)
291
+ @layer.table.add_th(lineno, token.opt[:content], token.opt[:nth], token.opt[:attr], @layer.star)
292
+ when :TABLE_START
293
+ @layer.process_nested_table_start(token, lineno)
294
+ when :STAR_START
295
+ @layer.star = true
296
+ output_in_else("*" + token.opt[:content])
297
+ when :STAR_END
298
+ @layer.star = false
299
+ output_in_else("*" + token.opt[:content])
537
300
  else
538
- nextState = @env.curState
301
+ @mes.output_fatal("In :IN_TABLE unknown tag=(#{token.kind}) in process_one_line_for_in_table")
302
+ @mes.output_fatal("@fname=#{@fname} | lineno=#{lineno}")
303
+ exit(@mes.ec("EXIT_CODE_UNKNOWN"))
539
304
  end
540
- nextState
541
305
  end
542
306
 
543
- def end
544
- @output.close
307
+ #
308
+ # IN_TABLE_BODY状態でのトークンと現在行の処理
309
+ # @param [String] token 読み込んだトークン
310
+ # @param [String] line 現在行
311
+ # @param [String] lineno 現在行の行番号
312
+ def process_one_line_for_in_table_body(token, line, lineno)
313
+ case token.kind
314
+ when :TH
315
+ @layer.table.add_th(lineno, token.opt[:content], token.opt[:nth], token.opt[:attr], @layer.star)
316
+ when :TD
317
+ @mes.output_debug(token)
318
+ @layer.table.add_td(lineno, token.opt[:content], token.opt[:nth], token.opt[:attr], @layer.star)
319
+ when :ELSE
320
+ output_in_else(token.opt[:content])
321
+ when :TABLE_START
322
+ @layer.process_nested_table_start(token, lineno)
323
+ when :TBODY_END
324
+ true # don't call process_table_end(token)
325
+ when :TABLE_END
326
+ process_one_line_for_table_end(token)
327
+ when :STAR_START
328
+ @layer.star = true
329
+ output_in_else("*" + token.opt[:content])
330
+ when :STAR_END
331
+ @layer.star = false
332
+ output_in_else("*" + token.opt[:content])
333
+ else
334
+ @mes.output_fatal("In :IN_TABLE_BODY unknown tag=(#{token.kind}) in process_one_line_for_in_table_body")
335
+ @mes.output_fatal("@fname=#{@fname} | lineno=#{lineno}")
336
+ exit(@mes.ec("EXIT_CODE_UNKNOWN"))
337
+ end
545
338
  end
546
339
 
547
- def getNewEnv(state=:START)
548
- new_env = @envStruct.new
549
- @envs << new_env
550
- new_env.curState = state
551
- if @env
552
- new_env.star = @env.star
340
+ #
341
+ # IN_TH状態でのトークンと現在行の処理
342
+ # @param [String] token 読み込んだトークン
343
+ # @param [String] line 現在行
344
+ # @param [String] lineno 現在行の行番号
345
+ def process_one_line_for_in_th(token, line, lineno)
346
+ case token.kind
347
+ when :ELSE
348
+ table_th_append_in_else(token.opt[:content])
349
+ when :TH
350
+ @layer.table.add_th(lineno, token.opt[:content], token.opt[:nth], token.opt[:attr], @layer.star)
351
+ when :TD
352
+ @layer.table.add_td(lineno, token.opt[:content], token.opt[:nth], token.opt[:attr], @layer.star)
353
+ when :TABLE_START
354
+ @layer.process_nested_table_start(token, lineno)
355
+ when :STAR_START
356
+ @layer.star = true
357
+ table_th_append_in_else("*" + token.opt[:content])
358
+ when :STAR_END
359
+ @layer.star = false
360
+ table_th_append_in_else("*" + token.opt[:content])
553
361
  else
554
- new_env.star = false
362
+ @mes.output_fatal("In :IN_TH unknown tag=(#{token.kind}) in process_one_line_for_in_th")
363
+ @mes.output_fatal("@fname=#{@fname} | lineno=#{lineno}")
364
+ exit(@mes.ec("EXIT_CODE_UNKNOWN"))
555
365
  end
556
- new_env
557
366
  end
558
367
 
559
- def getCurState
560
- ret = @env.curState
368
+ #
369
+ # IN_TH_NO_TBODY状態でのトークンと現在行の処理
370
+ # @param [String] token 読み込んだトークン
371
+ # @param [String] line 現在行
372
+ # @param [String] lineno 現在行の行番号
373
+ def process_one_line_for_in_th_no_tbody(token, line, lineno)
374
+ case token.kind
375
+ when :ELSE
376
+ table_th_append_in_else(token.opt[:content])
377
+ when :TH
378
+ @layer.table.add_th(lineno, token.opt[:content], token.opt[:nth], token.opt[:attr], @layer.star)
379
+ when :TD
380
+ @layer.table.add_td(lineno, token.opt[:content], token.opt[:nth], token.opt[:attr], @layer.star)
381
+ when :TABLE_START
382
+ @layer.process_nested_table_start(token, lineno)
383
+ when :STAR_START
384
+ @layer.star = true
385
+ table_th_append_in_else("*" + token.opt[:content])
386
+ when :STAR_END
387
+ @layer.star = false
388
+ table_th_append_in_else("*" + token.opt[:content])
389
+ else
390
+ @mes.output_fatal("In :IN_TH_NO_TBODY unknown tag=(#{token.kind}) in process_one_line_for_in_th_no_tbody")
391
+ @mes.output_fatal("@fname=#{@fname} | lineno=#{lineno}")
392
+ exit(@mes.ec("EXIT_CODE_UNKNOWN"))
393
+ end
561
394
  end
562
395
 
563
- def getPrevEnv()
564
- @envs.pop
565
- @envs.last
396
+ #
397
+ # IN_TD状態でのトークンと現在行の処理
398
+ # @param [String] token 読み込んだトークン
399
+ # @param [String] line 現在行
400
+ # @param [String] lineno 現在行の行番号
401
+ def process_one_line_for_in_td(token, line, lineno)
402
+ case token.kind
403
+ when :ELSE
404
+ table_td_append_in_else(token.opt[:content])
405
+ when :TH
406
+ @layer.table.add_th(lineno, token.opt[:content], token.opt[:nth], token.opt[:attr], @layer.star)
407
+ when :TD
408
+ @layer.table.add_td(lineno, token.opt[:content], token.opt[:nth], token.opt[:attr], @layer.star)
409
+ when :TBODY_END
410
+ @layer.table.tbody_end
411
+ when :TABLE_START
412
+ @layer.process_nested_table_start(token, lineno)
413
+ when :STAR_START
414
+ @layer.star = true
415
+ table_td_append_in_else("*" + token.opt[:content])
416
+ when :STAR_END
417
+ @layer.star = false
418
+ table_td_append_in_else("*" + token.opt[:content])
419
+ else
420
+ @mes.output_fatal("In :IN_TD unknown tag=(#{token.kind}) in process_one_line_for_in_td")
421
+ @mes.output_fatal("@fname=#{@fname} | lineno=#{lineno}")
422
+ exit(@mes.ec("EXIT_CODE_UNKNOWN"))
423
+ end
566
424
  end
567
425
 
568
- def peekPrevEnv()
569
- if @envs.size > 1
570
- @envs[@envs.size-2]
426
+ #
427
+ # IN_TD_NO_TBODY状態でのトークンと現在行の処理
428
+ # @param [String] token 読み込んだトークン
429
+ # @param [String] line 現在行
430
+ # @param [String] lineno 現在行の行番号
431
+ def process_one_line_for_in_td_no_tbody(token, line, lineno)
432
+ case token.kind
433
+ when :ELSE
434
+ table_td_append_in_else(token.opt[:content])
435
+ when :TH
436
+ @layer.table.add_th(lineno, token.opt[:content], token.opt[:nth], token.opt[:attr], @layer.star)
437
+ when :TD
438
+ @layer.table.add_td(lineno, token.opt[:content], token.opt[:nth], token.opt[:attr], @layer.star)
439
+ when :TABLE_START
440
+ @layer.process_nested_table_start(token, lineno)
441
+ when :TABLE_END
442
+ process_one_line_for_table_end(token)
443
+ when :TBODY_END
444
+ @layer.table.tbody_end
445
+ when :STAR_START
446
+ @layer.star = true
447
+ table_td_append_in_else("*" + token.opt[:content])
448
+ when :STAR_END
449
+ @layer.star = false
450
+ table_td_append_in_else("*" + token.opt[:content])
571
451
  else
572
- nil
452
+ @mes.output_fatal("In :IN_TD_NO_TBODY unknown tag=(#{token.kind}) in process_one_line_for_in_td_no_tbody")
453
+ @mes.output_fatal("@fname=#{@fname} | lineno=#{lineno}")
454
+ exit(@mes.ec("EXIT_CODE_UNKNOWN"))
573
455
  end
574
456
  end
575
457
 
576
- def checkEnvs
577
- case @env.curState
578
- when :OUT_OF_TABLE
579
- if @envs.size > 1
580
- @logger.info("illeagal nested env after parsing|:OUT_OF_TABLE")
581
- @logger.fatal("@envs.size=#{@envs.size} :TABLE_START #{@env.table.lineno}" )
582
- @envs.map{ |x|
583
- @logger.fatal("== @envs.curState=#{x.curState} :TABLE_START #{x.table.lineno}")
584
- }
585
- @logger.fatal("== @env.table")
586
- @logger.info(@env.table)
587
- raise
588
- end
458
+ #
459
+ # 現在の状態に対するトークンと現在行の処理
460
+ # @param [Symbol] current_state 現在の状態
461
+ # @param [String] token 読み込んだトークン
462
+ # @param [String] line 現在行
463
+ # @param [String] lineno 現在行の行番号
464
+ def process_one_line(current_state, token, line, lineno)
465
+ @layer.return_from_nested_env = false
466
+
467
+ case current_state
589
468
  when :START
590
- if @envs.size > 1
591
- @logger.fatal("illeagal nested env after parsing|:START")
592
- @logger.fatal("@envs.size=#{@envs.size}")
593
- @envs.map{ |x|
594
- @logger.fatal("== @envs.curState=#{x.curState} :TABLE_START #{x.table.lineno}")
595
- }
596
- @logger.fatal("== @env.table")
597
- @logger.fatal(@env.table)
598
- raise
599
- end
469
+ process_one_line_for_start(token, line, lineno)
470
+ when :OUT_OF_TABLE
471
+ process_one_line_for_out_of_table(token, line, lineno)
472
+ when :IN_TABLE
473
+ process_one_line_for_in_table(token, line, lineno)
474
+ when :IN_TABLE_BODY
475
+ process_one_line_for_in_table_body(token, line, lineno)
476
+ when :IN_TH
477
+ process_one_line_for_in_th(token, line, lineno)
478
+ when :IN_TH_NO_TBODY
479
+ process_one_line_for_in_th_no_tbody(token, line, lineno)
480
+ when :IN_TD
481
+ process_one_line_for_in_td(token, line, lineno)
482
+ when :IN_TD_NO_TBODY
483
+ process_one_line_for_in_td_no_tbody(token, line, lineno)
600
484
  else
601
- @logger.fatal("illeagal state after parsing(#{@env.curState}|#{@env.curState.class})")
602
- @logger.fatal("@envs.size=#{@envs.size}")
603
- @logger.fatal("== @env.curState=#{@env.curState}")
604
- @envs.map{ |x|
605
- @logger.fatal("== @envs.curState=#{x.curState} #{@fname}:#{x.table.lineno}")
606
- }
607
- # @logger.fatal("== @env.table")
608
- # @logger.fatal(@env.table)
609
- # raise
610
- @logger.fatal("")
611
- exit(@exit_illeagal_state)
485
+ @mes.output_fatal("In Unknown state(#{current_state}) in process_one_line")
486
+ @mes.output_fatal("@fname=#{@fname} | lineno=#{lineno}")
487
+ exit(@mes.ec("EXIT_CODE_UNKNOWN"))
488
+ end
489
+
490
+ if @layer.return_from_nested_env
491
+ next_state = @layer.cur_state
492
+ else
493
+ next_state = get_next_state(token, line, lineno, @fname)
494
+
495
+ @mes.output_debug("#{__LINE__}|next_state=#{next_state}")
612
496
  end
497
+ next_state
613
498
  end
614
499
  end
615
500
  end