lib/jkf/parser/ki2.rb in jkf-0.4.3 vs lib/jkf/parser/ki2.rb in jkf-0.5.0
- old
+ new
@@ -14,76 +14,74 @@
s2 = parse_header
while s2 != :failed
s1 << s2
s2 = parse_header
end
- if s1 != :failed
+ if s1 == :failed
+ @current_pos = s0
+ s0 = :failed
+ else
s2 = parse_initialboard
s2 = nil if s2 == :failed
s3 = []
s4 = parse_header
while s4 != :failed
s3 << s4
s4 = parse_header
end
s4 = parse_moves
- if s4 != :failed
+ if s4 == :failed
+ @current_pos = s0
+ s0 = :failed
+ else
s5 = []
s6 = parse_fork
while s6 != :failed
s5 << s6
s6 = parse_fork
end
@reported_pos = s0
s0 = transform_root(s1, s2, s3, s4, s5)
- else
- @current_pos = s0
- s0 = :failed
end
- else
- @current_pos = s0
- s0 = :failed
end
s0
end
# header : [^:\r\n]+ ":" nonls nl+ | header_teban
def parse_header
s0 = @current_pos
s2 = match_regexp(/^[^*:\r\n]/)
- if s2 != :failed
+ if s2 == :failed
+ s1 = :failed
+ else
s1 = []
while s2 != :failed
s1 << s2
s2 = match_regexp(/^[^:\r\n]/)
end
- else
- s1 = :failed
end
- if s1 != :failed
- if match_str(":") != :failed
- s3 = parse_nonls
- s5 = parse_nl
- if s5 != :failed
- s4 = []
- while s5 != :failed
- s4 << s5
- s5 = parse_nl
- end
- else
- s4 = :failed
- end
- if s4 != :failed
- @reported_pos = s0
- s0 = { "k" => s1.join, "v" => s3.join }
- else
- @current_pos = s0
- s0 = :failed
- end
+ if s1 == :failed
+ @current_pos = s0
+ s0 = :failed
+ elsif match_str(":") != :failed
+ s3 = parse_nonls
+ s5 = parse_nl
+ if s5 == :failed
+ s4 = :failed
else
+ s4 = []
+ while s5 != :failed
+ s4 << s5
+ s5 = parse_nl
+ end
+ end
+ if s4 == :failed
@current_pos = s0
s0 = :failed
+ else
+ @reported_pos = s0
+ s0 = { "k" => s1.join, "v" => s3.join }
end
else
@current_pos = s0
s0 = :failed
end
@@ -93,36 +91,39 @@
# header_teban : [先後上下] "手番" nl
def parse_header_teban
s0 = @current_pos
s1 = parse_turn
- if s1 != :failed
+ if s1 == :failed
+ @current_pos = s0
+ :failed
+ else
s2 = match_str("手番")
- if s2 != :failed
+ if s2 == :failed
+ @current_pos = s0
+ :failed
+ else
s3 = parse_nl
- if s3 != :failed
- @reported_pos = s0
- { "k" => "手番", "v" => s1 }
- else
+ if s3 == :failed
@current_pos = s0
:failed
+ else
+ @reported_pos = s0
+ { "k" => "手番", "v" => s1 }
end
- else
- @current_pos = s0
- :failed
end
- else
- @current_pos = s0
- :failed
end
end
# moves : firstboard : move* result?
def parse_moves
s0 = @current_pos
s1 = parse_firstboard
- if s1 != :failed
+ if s1 == :failed
+ @current_pos = s0
+ s0 = :failed
+ else
s2 = []
s3 = parse_move
while s3 != :failed
s2 << s3
s3 = parse_move
@@ -133,13 +134,10 @@
s0 = -> (hd, tl, res) do
tl.unshift(hd)
tl << { "special" => res } if res && !tl[tl.length - 1]["special"]
tl
end.call(s1, s2, s3)
- else
- @current_pos = s0
- s0 = :failed
end
s0
end
# firstboard : comment* pointer?
@@ -159,11 +157,14 @@
# move : line comment* pointer? (nl | " ")*
def parse_move
s0 = @current_pos
s1 = parse_line
- if s1 != :failed
+ if s1 == :failed
+ @current_pos = s0
+ s0 = :failed
+ else
s2 = []
s3 = parse_comment
while s3 != :failed
s2 << s3
s3 = parse_comment
@@ -181,58 +182,61 @@
s0 = -> (line, c) do
ret = { "move" => line }
ret["comments"] = c if !c.empty?
ret
end.call(s1, s2)
- else
- @current_pos = s0
- s0 = :failed
end
s0
end
# line : [▲△] fugou (nl / " ")*
def parse_line
s0 = @current_pos
s1 = match_regexp(/^[▲△]/)
- if s1 != :failed
+ if s1 == :failed
+ @current_pos = s0
+ s0 = :failed
+ else
s1 = if s1 == "▲"
{ "color" => 0 }
else
{ "color" => 1 }
end
s2 = parse_fugou
- if s2 != :failed
+ if s2 == :failed
+ @current_pos = s0
+ s0 = :failed
+ else
s3 = []
s4 = parse_nl
s4 = match_space if s4 == :failed
while s4 != :failed
s3 << s4
s4 = parse_nl
s4 = match_space if s4 == :failed
end
@reported_pos = s0
s0 = s2.merge(s1)
- else
- @current_pos = s0
- s0 = :failed
end
- else
- @current_pos = s0
- s0 = :failed
end
s0
end
# fugou : place piece soutai? dousa? ("成" | "不成")? "打"?
def parse_fugou
s0 = @current_pos
s1 = parse_place
- if s1 != :failed
+ if s1 == :failed
+ @current_pos = s0
+ :failed
+ else
s2 = parse_piece
- if s2 != :failed
+ if s2 == :failed
+ @current_pos = s0
+ :failed
+ else
s3 = parse_soutai
s3 = nil if s3 == :failed
s4 = parse_dousa
s4 = nil if s4 == :failed
s5 = match_str("成")
@@ -240,46 +244,40 @@
s5 = nil if s5 == :failed
s6 = match_str("打")
s6 = nil if s6 == :failed
@reported_pos = s0
transform_fugou(s1, s2, s3, s4, s5, s6)
- else
- @current_pos = s0
- :failed
end
- else
- @current_pos = s0
- :failed
end
end
# place : num numkan
def parse_place
s0 = @current_pos
s1 = parse_num
- if s1 != :failed
+ if s1 == :failed
+ @current_pos = s0
+ s0 = :failed
+ else
s2 = parse_numkan
- if s2 != :failed
- @reported_pos = s0
- s0 = { "x" => s1, "y" => s2 }
- else
+ if s2 == :failed
@current_pos = s0
s0 = :failed
+ else
+ @reported_pos = s0
+ s0 = { "x" => s1, "y" => s2 }
end
- else
- @current_pos = s0
- s0 = :failed
end
if s0 == :failed
s0 = @current_pos
- if match_regexp("同") != :failed
+ if match_regexp("同") == :failed
+ @current_pos = s0
+ s0 = :failed
+ else
match_str(" ")
@reported_pos = s0
s0 = { "same" => true }
- else
- @current_pos = s0
- s0 = :failed
end
end
s0
end
@@ -294,57 +292,55 @@
end
# "*" nonls nl
def parse_comment
s0 = @current_pos
- if match_str("*") != :failed
+ if match_str("*") == :failed
+ @current_pos = s0
+ s0 = :failed
+ else
s2 = parse_nonls
- if parse_nl != :failed
- @reported_pos = s0
- s0 = s2.join
- else
+ if parse_nl == :failed
@current_pos = s0
s0 = :failed
+ else
+ @reported_pos = s0
+ s0 = s2.join
end
- else
- @current_pos = s0
- s0 = :failed
end
s0
end
# fork : "変化:" " "* [0-9]+ "手" nl moves
def parse_fork
s0 = @current_pos
- if match_str("変化:") != :failed
+ if match_str("変化:") == :failed
+ @current_pos = s0
+ s0 = :failed
+ else
match_spaces
s3 = match_digits!
- if s3 != :failed
- if match_str("手") != :failed
- if parse_nl != :failed
- s6 = parse_moves
- if s6 != :failed
- @reported_pos = s0
- s0 = { "te" => s3.join.to_i, "moves" => s6[1..-1] }
- else
- @current_pos = s0
- s0 = :failed
- end
- else
+ if s3 == :failed
+ @current_pos = s0
+ s0 = :failed
+ elsif match_str("手") != :failed
+ if parse_nl == :failed
+ @current_pos = s0
+ s0 = :failed
+ else
+ s6 = parse_moves
+ if s6 == :failed
@current_pos = s0
s0 = :failed
+ else
+ @reported_pos = s0
+ s0 = { "te" => s3.join.to_i, "moves" => s6[1..-1] }
end
- else
- @current_pos = s0
- s0 = :failed
end
else
@current_pos = s0
s0 = :failed
end
- else
- @current_pos = s0
- s0 = :failed
end
s0
end
# turn : [先後上下]