Class: Mdextab::Layer

Inherits:
Object
  • Object
show all
Defined in:
lib/mdextab/layer.rb

Overview

入れ子のTableにを管理するレイヤークラス

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mes, output) ⇒ Layer

初期化

Parameters:

  • mes (Messagex)

    Messagexクラスのインスタンス

  • output (IO)

    出力先



20
21
22
23
24
25
26
27
28
# File 'lib/mdextab/layer.rb', line 20

def initialize(mes, output)
  @mes = mes
  @output = output
  @return_from_nested_layer = false

  @layer_struct = Struct.new(:table, :star, :cur_state, :fname, :lineno)
  @cur_layer = nil
  @layers = []
end

Instance Attribute Details

#cur_layerLayer

現在のレイヤー

Returns:



10
11
12
# File 'lib/mdextab/layer.rb', line 10

def cur_layer
  @cur_layer
end

#return_from_nested_layerBoolean

入れ子のレイヤーからリターンしたかを示す

Returns:

  • (Boolean)


7
8
9
# File 'lib/mdextab/layer.rb', line 7

def return_from_nested_layer
  @return_from_nested_layer
end

#sizeInteger

レイヤーの階層数

Returns:

  • (Integer)


13
14
15
# File 'lib/mdextab/layer.rb', line 13

def size
  @size
end

Instance Method Details

#add_layer(fname, lineno, state = :START) ⇒ Symbol

新しいレイヤーの追加

Parameters:

  • fname (String)

    構文解析対象のMarkdownファイル名

  • lineno (String)

    TABLE_STARTトークン出現行の行番号

Returns:

  • (Symbol)

    テーブル拡張向け構文解析での状態



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/mdextab/layer.rb', line 80

def add_layer(fname, lineno, state=:START)
  new_layer = @layer_struct.new(nil, nil, nil, fname, lineno)
  @layers << new_layer
  @size = @layers.size
  # raise if state.class != Symbol
  new_layer.cur_state = state
  if @cur_layer
    new_layer.star = @cur_layer.star
  else
    new_layer.star = false
  end
  @cur_layer = new_layer
end

#check_layers(fname) ⇒ void

Note:

fnameはデバッグ出力、エラー出力に使う

This method returns an undefined value.

全レイヤーの状態検査

Parameters:

  • fname (String)

    構文解析対象のMarkdownファイル名



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/mdextab/layer.rb', line 196

def check_layers(fname)
  case @cur_layer.cur_state
  when :OUT_OF_TABLE
    if @layers.size > 1
      @mes.output_fatal("illeagal nested env after parsing|:OUT_OF_TABLE")
      @mes.output_fatal("@layers.size=#{@layers.size} :TABLE_START #{fname} #{table.lineno}")
      @layers.map {|x| @mes.output_debug("== @layers.cur_state=#{x.cur_state} :TABLE_START #{fname} #{x.table.lineno}") }
      @mes.output_debug("== table")
      @mes.output_info(table)
      exit(@mes.ec("EXIT_CODE_EXCEPTION"))
    end
  when :START
    if @layers.size > 1
      @mes.output_fatal("illeagal nested env after parsing|:START")
      @mes.output_fatal("@layers.size=#{@layers.size}")
      @layers.map {|x| @mes.output_error("== @layers.cur_state=#{x.cur_state} :TABLE_START #{fname} #{x.table.lineno}") }
      @mes.output_error("== table")
      @mes.output_error(table)
      exit(@mes.ec("EXIT_CODE_EXCEPTION"))
    end
  else
    @mes.output_fatal("illeagal state after parsing(@cur_layer.cur_state=#{@cur_layer.cur_state}|fname=#{fname}")
    @mes.output_fatal("@layers.size=#{@layers.size}")
    @mes.output_error("== cur_state=#{@cur_layer.cur_state}")
    @layers.map {|x| @mes.output_error("== @layers.cur_state=#{x.cur_state} #{fname}:#{x.table.lineno}") }
    @mes.output_error("")
    exit(@mes.ec("EXIT_CODE_ILLEAG<AL_STATE"))
  end
end

#cur_stateSymbol

カレントレイヤーの状態を設定

Returns:

  • (Symbol)

    現在の状態



41
42
43
44
# File 'lib/mdextab/layer.rb', line 41

def cur_state
  raise if @cur_layer.cur_state.class != Symbol
  @cur_layer.cur_state
end

#cur_state=(val) ⇒ Object

カレントレイヤーの状態を設定

Parameters:

  • val (Symbol)


33
34
35
36
# File 'lib/mdextab/layer.rb', line 33

def cur_state=(val)
  # raise if val.class != Symbol
  @cur_layer.cur_state = val
end

#peek_prev_layerLayer?

1つ前のレイヤーを返す

Returns:

  • (Layer, nil)

    1つ前のレイヤーまたはnil (1つ前のレイヤーが存在しない場合)



111
112
113
114
115
# File 'lib/mdextab/layer.rb', line 111

def peek_prev_layer
  return nil unless @layers.size > 1

  @layers[@layers.size - 2]
end

#pop_layerLayer

カレントレイヤーを取り出して返す

Returns:

  • (Layer)

    取り出されたカレントレイヤー



98
99
100
101
102
103
104
# File 'lib/mdextab/layer.rb', line 98

def pop_layer
  tmp_ = @layers.pop
  @size = @layers.size
  @cur_layer = @layers.last

  tmp_
end

#process_nested_table_start(token, lineno, fname) ⇒ void

This method returns an undefined value.

入れ子のTABLE_STARTトークンとトークン出現行の処理

Parameters:

  • token (Token)

    読み込んだトークン

  • lineno (Integer)

    トークン出現行の行番号

  • fname (String)

    構文解析対象のMarkdownファイル名



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/mdextab/layer.rb', line 124

def process_nested_table_start(token, lineno, fname)
  # TBODYトークンが出現する前にTABLE_STARTトークンが出現した場合、仮想的なTBODYトークンが出現したとみなす
  if table.tbody.nil?
    table.add_tbody(lineno)
  end
  @mes.output_debug("B process_nested_table_start 1 @cur_layer.table=#{@cur_layer.table.object_id} token.kind=#{token.kind} token.opt[:lineno]=#{token.opt[:lineno]} cur_state=#{@cur_layer.cur_state}")
  # 新しいレイヤーを追加して、それをカレントレイヤーとし、カレントレイヤーにTableを追加する
  add_layer(fname, lineno, :OUT_OF_TABLE)
  @cur_layer.table = Table.new(token.opt[:lineno], @mes, token.opt[:attr])
  @mes.output_debug("process_nested_table_start 3 token.kind=#{token.kind} cur_state=#{@cur_layer.cur_state}")
end

#process_table_end(token) ⇒ void

This method returns an undefined value.

TABLE_ENDトークンの処理

Parameters:

  • token (Token)

    読み込んだトークン



141
142
143
144
145
146
147
# File 'lib/mdextab/layer.rb', line 141

def process_table_end(token)
  prev_layer = peek_prev_layer
  retrun unless prev_layer

  # 一つ前のレイヤーが存在すれば、入れ子のTABLE_ENDトークンとして処理する
  process_table_end_for_prev_env(token)
end

#process_table_end_for_prev_env(token) ⇒ void

Note:

tokenはデバッグ出力、エラー出力に使う

This method returns an undefined value.

入れ子のTABLE_ENDトークンの処理

Parameters:

  • token (Token)

    読み込んだトークン



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
184
185
186
187
188
# File 'lib/mdextab/layer.rb', line 155

def process_table_end_for_prev_env(token)
  tmp_table = @cur_layer.table
  pop_layer
  @return_from_nested_layer = true

  # pop_layerを呼んだ後なので、カレントレイヤーはメソッド呼び出し時より一つ前のレイヤー
  case @cur_layer.cur_state
  when :IN_TD
    @cur_layer.table.td_append(tmp_table, @cur_layer.star)
  when :IN_TD_NO_TBODY
    @cur_layer.table.td_append(tmp_table, @cur_layer.star)
  when :IN_TH
    @cur_layer.table.th_append(tmp_table, @cur_layer.star)
  when :IN_TH_NO_TBODY
    @cur_layer.table.th_append(tmp_table, @cur_layer.star)
  when :IN_TABLE
    if @cur_layer.table.nil?
      @mes.output_debug("In process_nested_table_env_for_prev_env: table=nil token.kind=#{token.kind} token.opt[:lineno]=#{token.opt[:lineno]} cur_state=#{@cur_layer.cur_state}")
      raise
    end
    @cur_layer.table.add(tmp_table)
  when :IN_TABLE_BODY
    @cur_layer.table.add(tmp_table)
  when :START
    @mes.output_debug("In process_nested_table_env_for_prev_env: table=nil token.kind=#{token.kind} token.opt[:lineno]=#{token.opt[:lineno]} cur_state=#{@cur_layer.cur_state}")
    raise
  else
    v = @cur_layer.cur_state || "nil"
    @mes.output_fatal("E100 cur_state=#{v}")
    @mes.output_fatal("table=#{@cur_layer.table}")
    @mes.output_fatal("IllegalState(#{@cur_layer.cur_state} in process_table_end(#{token})")
    exit(@mes.ec("EXIT_CODE_TABLE_END"))
  end
end

#star=(val) ⇒ Boolean

カレントレイヤーのstarの存在の有無を取得

Returns:

  • (Boolean)

    カレントレイヤーのstaの存在の有無 true:starが存在 false:startが存在しない



70
71
72
# File 'lib/mdextab/layer.rb', line 70

def star
  @cur_layer.star
end

#star=(val) ⇒ Object

カレントレイヤーのstarの存在の有無を設定

Parameters:

  • val (Boolean)

    カレントレイヤーのstarの存在の有無 true:starが存在 false:startが存在しない



63
64
65
# File 'lib/mdextab/layer.rb', line 63

def star=(val)
  @cur_layer.star = val
end

#tablee=(val) ⇒ Object

カレントレイヤーのtableを取得

Parameters:

  • val (Table)

    カレントレイヤーのtable



56
57
58
# File 'lib/mdextab/layer.rb', line 56

def table
  @cur_layer.table
end

#table=(val) ⇒ Object

カレントレイヤーのtableを設定

Parameters:

  • val (Table)

    カレントレイヤーのtable



49
50
51
# File 'lib/mdextab/layer.rb', line 49

def table=(val)
  @cur_layer.table = val
end