lib/jkf/parser/ki2.rb in jkf-0.4.1 vs lib/jkf/parser/ki2.rb in jkf-0.4.2
- old
+ new
@@ -1,11 +1,15 @@
# coding: utf-8
module Jkf::Parser
+ # KI2 Parser
class Ki2 < Base
include Kifuable
+ protected
+
+ # kifu : header* initialboard? header* moves fork*
def parse_root
s0 = @current_pos
s1 = []
s2 = parse_header
while s2 != :failed
@@ -40,10 +44,11 @@
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
s1 = []
@@ -84,13 +89,14 @@
end
s0 = parse_header_teban if s0 == :failed
s0
end
+ # header_teban : [先後上下] "手番" nl
def parse_header_teban
s0 = @current_pos
- s1 = match_regexp(/^[先後上下]/)
+ s1 = parse_turn
if s1 != :failed
s2 = match_str("手番")
if s2 != :failed
s3 = parse_nl
if s3 != :failed
@@ -108,10 +114,11 @@
@current_pos = s0
:failed
end
end
+ # moves : firstboard : move* result?
def parse_moves
s0 = @current_pos
s1 = parse_firstboard
if s1 != :failed
s2 = []
@@ -133,10 +140,11 @@
s0 = :failed
end
s0
end
+ # firstboard : comment* pointer?
def parse_firstboard
s0 = @current_pos
s1 = []
s2 = parse_comment
while s2 != :failed
@@ -147,10 +155,11 @@
@reported_pos = s0
s0 = s1.empty? ? {} : { "comments" => s1 }
s0
end
+ # move : line comment* pointer? (nl | " ")*
def parse_move
s0 = @current_pos
s1 = parse_line
if s1 != :failed
s2 = []
@@ -180,10 +189,11 @@
end
s0
end
+ # line : [▲△] fugou (nl / " ")*
def parse_line
s0 = @current_pos
s1 = match_regexp(/^[▲△]/)
if s1 != :failed
s1 = if s1 == "▲"
@@ -212,10 +222,11 @@
s0 = :failed
end
s0
end
+ # fugou : place piece soutai? dousa? ("成" | "不成")? "打"?
def parse_fugou
s0 = @current_pos
s1 = parse_place
if s1 != :failed
s2 = parse_piece
@@ -239,10 +250,11 @@
@current_pos = s0
:failed
end
end
+ # place : num numkan
def parse_place
s0 = @current_pos
s1 = parse_num
if s1 != :failed
s2 = parse_numkan
@@ -269,19 +281,21 @@
end
end
s0
end
-
+ # soutai : [左直右]
def parse_soutai
match_regexp(/^[左直右]/)
end
+ # dousa : [上寄引]
def parse_dousa
match_regexp(/^[上寄引]/)
end
+ # "*" nonls nl
def parse_comment
s0 = @current_pos
if match_str("*") != :failed
s2 = parse_nonls
if parse_nl != :failed
@@ -296,10 +310,11 @@
s0 = :failed
end
s0
end
+ # fork : "変化:" " "* [0-9]+ "手" nl moves
def parse_fork
s0 = @current_pos
if match_str("変化:") != :failed
match_spaces
s3 = match_digits!
@@ -331,16 +346,16 @@
s0 = :failed
end
s0
end
+ # turn : [先後上下]
def parse_turn
match_regexp(/^[先後上下]/)
end
- protected
-
+ # transform to jkf
def transform_root(headers, ini, headers2, moves, forks)
ret = { "header" => {}, "moves" => moves }
headers.compact.each { |h| ret["header"][h["k"]] = h["v"] }
headers2.compact.each { |h| ret["header"][h["k"]] = h["v"] }
if ini
@@ -352,10 +367,11 @@
transform_root_header_data(ret) if ret["initial"] && ret["initial"]["data"]
transform_root_forks(forks, moves)
ret
end
+ # transfrom fugou to jkf
def transform_fugou(pl, pi, sou, dou, pro, da)
ret = { "piece" => pi }
if pl["same"]
ret["same"] = true
else
@@ -369,26 +385,29 @@
ret["relative"] = rel unless rel.empty?
end
ret
end
+ # relative string to jkf
def soutai2relative(str)
{
"左" => "L",
"直" => "C",
"右" => "R"
}[str] || ""
end
+ # movement string to jkf
def dousa2relative(str)
{
"上" => "U",
"寄" => "M",
"引" => "D"
}[str] || ""
end
+ # generate motigoma
def make_hand(str)
ret = { "FU" => 0, "KY" => 0, "KE" => 0, "GI" => 0, "KI" => 0, "KA" => 0, "HI" => 0 }
return ret if str.empty?
str.gsub(/ $/, "").split(" ").each do |kind|
@@ -397,9 +416,10 @@
end
ret
end
+ # check eos
def eos?
@input[@current_pos].nil?
end
end
end