lib/jkf/parser/kifuable.rb in jkf-0.4.1 vs lib/jkf/parser/kifuable.rb in jkf-0.4.2

- old
+ new

@@ -1,7 +1,11 @@ module Jkf::Parser + # Intersection of KIF and KI2 module Kifuable + protected + + # initialboard : (" " nonls nl)? ("+" nonls nl)? ikkatsuline+ ("+" nonls nl)? def parse_initialboard s0 = s1 = @current_pos if match_space != :failed parse_nonls s2 = parse_nl @@ -40,10 +44,11 @@ @current_pos = s0 :failed end end + # ikkatsuline : "|" masu:masu+ "|" nonls! nl def parse_ikkatsuline s0 = @current_pos if match_str("|") != :failed s3 = parse_masu if s3 != :failed @@ -84,10 +89,11 @@ end s0 end + # masu : teban piece | " ・" def parse_masu s0 = @current_pos s1 = parse_teban if s1 != :failed s2 = parse_piece @@ -113,10 +119,11 @@ end s0 end + # teban : (" " | "+" | "^") | ("v" | "V") def parse_teban s0 = @current_pos s1 = match_space if s1 == :failed s1 = match_str("+") @@ -138,10 +145,11 @@ s0 = s1 end s0 end + # pointer : "&" nonls nl def parse_pointer s0 = @current_pos s1 = match_str("&") if s1 != :failed s2 = parse_nonls @@ -157,10 +165,33 @@ s0 = :failed end s0 end + # num : [123456789] + def parse_num + s0 = @current_pos + s1 = match_regexp(/^[123456789]/) + if s1 != :failed + @reported_pos = s0 + s1 = zen2n(s1) + end + s1 + end + + # numkan : [一二三四五六七八九] + def parse_numkan + s0 = @current_pos + s1 = match_regexp(/^[一二三四五六七八九]/) + if s1 != :failed + @reported_pos = s0 + s1 = kan2n(s1) + end + s1 + end + + # piece : "成"? [歩香桂銀金角飛王玉と杏圭全馬竜龍] def parse_piece s0 = @current_pos s1 = match_str("成") s1 = "" if s1 == :failed s2 = match_regexp(/^[歩香桂銀金角飛王玉と杏圭全馬竜龍]/) @@ -171,10 +202,15 @@ @current_pos = s0 :failed end end + # result : "まで" [0-9]+ "手" ( + # "で" (turn "手の" (result_toryo | result_illegal)) | + # result_timeup | result_chudan | result_jishogi | + # result_sennichite | result_tsumi | result_fuzumi + # ) nl def parse_result s0 = @current_pos if match_str("まで") != :failed s2 = match_digits! if s2 != :failed @@ -244,10 +280,11 @@ @current_pos = s0 :failed end end + # result_toryo : "勝ち" def parse_result_toryo s0 = @current_pos s1 = match_str("勝ち") if s1 != :failed @reported_pos = s0 @@ -256,10 +293,11 @@ @current_pos = s0 :failed end end + # result_illegal : "反則" ("勝ち" | "負け") def parse_result_illegal s0 = @current_pos if match_str("反則") != :failed s10 = @current_pos s11 = match_str("勝ち") @@ -288,10 +326,11 @@ @current_pos = s0 :failed end end + # result_timeup : "で時間切れにより" turn "手の勝ち" def parse_result_timeup s0 = @current_pos if match_str("で時間切れにより") != :failed if parse_turn != :failed if match_str("手の勝ち") != :failed @@ -309,10 +348,11 @@ @current_pos = s0 :failed end end + # result_chudan : "で中断" def parse_result_chudan s0 = @current_pos s1 = match_str("で中断") if s1 != :failed @reported_pos = s0 @@ -321,10 +361,11 @@ @current_pos = s0 :failed end end + # result_jishogi : "で持将棋" def parse_result_jishogi s0 = @current_pos s1 = match_str("で持将棋") if s1 != :failed @reported_pos = s0 @@ -333,10 +374,11 @@ @current_pos = s0 :failed end end + # result_sennichite : "で千日手" def parse_result_sennichite s0 = @current_pos s1 = match_str("で千日手") if s1 != :failed @reported_pos = s0 @@ -345,10 +387,11 @@ @current_pos = s0 :failed end end + # result_tsumi : "で"? "詰" "み"? def parse_result_tsumi s0 = @current_pos match_str("で") if match_str("詰") != :failed match_str("み") @@ -358,10 +401,11 @@ @current_pos = s0 :failed end end + # result_fuzumi : "で不詰" def parse_result_fuzumi s0 = @current_pos s1 = match_str("で不詰") if s1 != :failed @reported_pos = s0 @@ -370,10 +414,11 @@ @current_pos = s0 :failed end end + # skipline : "#" nonls newline def parse_skipline s0 = @current_pos s1 = match_str("#") if s1 != :failed s2 = parse_nonls @@ -389,34 +434,16 @@ s0 = :failed end s0 end - def parse_num - s0 = @current_pos - s1 = match_regexp(/^[123456789]/) - if s1 != :failed - @reported_pos = s0 - s1 = zen2n(s1) - end - s1 - end - - def parse_numkan - s0 = @current_pos - s1 = match_regexp(/^[一二三四五六七八九]/) - if s1 != :failed - @reported_pos = s0 - s1 = kan2n(s1) - end - s1 - end - + # whitespace : " " | "\t" def parse_whitespace match_regexp(/^[ \t]/) end + # newline : whitespace* ("\n" | "\r" "\n"?) def parse_newline s0 = @current_pos s1 = [] s2 = parse_whitespace while s2 != :failed @@ -442,10 +469,11 @@ @current_pos = s0 :failed end end + # nl : newline+ skipline* def parse_nl s0 = @current_pos s2 = parse_newline if s2 != :failed s1 = [] @@ -468,35 +496,37 @@ @current_pos = s0 :failed end end + # nonl : def parse_nonl match_regexp(/^[^\r\n]/) end + # nonls : nonl* def parse_nonls stack = [] matched = parse_nonl while matched != :failed stack << matched matched = parse_nonl end stack end + # nonls! : nonl+ def parse_nonls! matched = parse_nonls if matched.empty? :failed else matched end end - protected - + # transform header-data to jkf def transform_root_header_data(ret) if ret["header"]["手番"] ret["initial"]["data"]["color"] = "下先".include?(ret["header"]["手番"]) ? 0 : 1 ret["header"].delete("手番") else @@ -509,10 +539,11 @@ %w(先手の持駒 下手の持駒 後手の持駒 上手の持駒).each do |key| ret["header"].delete(key) end end + # transfrom forks to jkf def transform_root_forks(forks, moves) fork_stack = [{ "te" => 0, "moves" => moves }] forks.each do |f| now_fork = f _fork = fork_stack.pop @@ -523,10 +554,11 @@ fork_stack << _fork fork_stack << now_fork end end + # transform initialboard to jkf def transform_initialboard(lines) board = [] 9.times do |i| line = [] 9.times do |j| @@ -535,18 +567,21 @@ board << line end { "preset" => "OTHER", "data" => { "board" => board } } end + # zenkaku number to number def zen2n(s) "0123456789".index(s) end + # kanji number to number (1) def kan2n(s) "〇一二三四五六七八九".index(s) end + # kanji number to number (2) def kan2n2(s) case s.length when 1 "〇一二三四五六七八九十".index(s) when 2 @@ -554,10 +589,11 @@ else raise "21以上の数値に対応していません" end end + # kanji piece-type to csa def kind2csa(kind) if kind[0] == "成" { "香" => "NY", "桂" => "NK", @@ -583,9 +619,10 @@ "龍" => "RY" }[kind] end end + # preset string to jkf def preset2str(preset) { "平手" => "HIRATE", "香落ち" => "KY", "右香落ち" => "KY_R",