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

- old
+ new

@@ -1,16 +1,21 @@ # coding: utf-8 module Jkf::Parser + # CSA Parser class Csa < Base + protected + + # kifu : csa2 | csa1 def parse_root @input += "\n" unless @input[-1] =~ /\n|\r|,/ # FIXME s0 = parse_csa2 s0 = parse_csa1 if s0 == :failed s0 end + # csa2 : version22 information? initialboard moves? def parse_csa2 s0 = @current_pos if parse_version22 != :failed s1 = parse_information s1 = nil if s1 == :failed @@ -36,10 +41,11 @@ s0 = :failed end s0 end + # version22 : comment* "V2.2" nl def parse_version22 s0 = @current_pos s1 = parse_comments s2 = match_str("V2.2") if s2 != :failed @@ -55,10 +61,11 @@ s0 = :failed end s0 end + # information : players? headers def parse_information s0 = @current_pos s1 = parse_players s1 = nil if s1 == :failed s2 = parse_headers @@ -70,10 +77,11 @@ s0 = :failed end s0 end + # headers : header* def parse_headers s0 = @current_pos s1 = [] s2 = parse_header while s2 != :failed @@ -89,10 +97,11 @@ ret end.call(s1) s0 end + # header : comment* "$" [^:]+ ":" nonls nl def parse_header s0 = @current_pos parse_comments if match_str("$") != :failed s4 = match_regexp(/^[^:]/) @@ -128,10 +137,11 @@ s0 = :failed end s0 end + # csa1 : players? initialboard? moves def parse_csa1 s0 = @current_pos s1 = parse_players s1 = nil if s1 == :failed s2 = parse_initial_board @@ -152,10 +162,11 @@ s0 = :failed end s0 end + # players : comment* ("N+" nonls nl)? comment* ("N-" nonls nl)? def parse_players s0 = @current_pos parse_comments s2 = @current_pos if match_str("N+") != :failed @@ -191,10 +202,11 @@ @reported_pos = s0 s0 = [(s2 ? s2.join : nil), (s4 ? s4.join : nil)] s0 end + # initialboard : comment* (hirate | ikkatsu | "") komabetsu comment* teban nl def parse_initial_board s0 = @current_pos parse_comments s2 = parse_hirate if s2 == :failed @@ -242,10 +254,11 @@ @current_pos = s0 :failed end end + # hirate : "PI" xypiece* nl def parse_hirate s0 = @current_pos if match_str("PI") != :failed s2 = [] s3 = parse_xy_piece @@ -271,10 +284,11 @@ s0 = :failed end s0 end + # ikkatsu : ikkatsuline+ def parse_ikkatsu s0 = @current_pos s2 = parse_ikkatsu_line if s2 != :failed s1 = [] @@ -301,14 +315,15 @@ end s0 = s1 s0 end + # ikkatsuline : "P" [1-9] masu+ nl def parse_ikkatsu_line s0 = @current_pos if match_str("P") != :failed - if match_regexp(/^[1-9]/) != :failed + if match_digit != :failed s4 = parse_masu if s4 != :failed s3 = [] while s4 != :failed s3 << s4 @@ -339,10 +354,11 @@ s0 = :failed end s0 end + # masu : teban piece | " * " def parse_masu s0 = @current_pos s1 = parse_teban if s1 != :failed s2 = parse_piece @@ -366,10 +382,11 @@ s0 = s1 end s0 end + # komabetsu : komabetsuline* def parse_komabetsu s0 = @current_pos s1 = [] s2 = parse_komabetsu_line while s2 != :failed @@ -378,10 +395,11 @@ end @reported_pos = s0 transform_komabetsu_lines(s1) end + # komabetsuline : "P" teban xypiece+ nl def parse_komabetsu_line s0 = @current_pos if match_str("P") != :failed s2 = parse_teban if s2 != :failed @@ -416,10 +434,11 @@ s0 = :failed end s0 end + # moves : firstboard move* comment* def parse_moves s0 = @current_pos s1 = parse_firstboard if s1 != :failed s2 = [] @@ -436,17 +455,19 @@ s0 = :failed end s0 end + # firstboard : comment* def parse_firstboard s0 = @current_pos s1 = parse_comments @reported_pos = s0 s1.empty? ? {} : { "comments" => s1 } end + # move : (normalmove | specialmove) time? comment* def parse_move s0 = @current_pos s1 = parse_normal_move s1 = parse_special_move if s1 == :failed if s1 != :failed @@ -470,10 +491,11 @@ s0 = :failed end s0 end + # normalmove : teban xy xy piece nl def parse_normal_move s0 = @current_pos s1 = parse_teban if s1 != :failed s2 = parse_xy @@ -510,10 +532,11 @@ s0 = :failed end s0 end + # specialmove : "%" [-+_A-Z]+ nl def parse_special_move s0 = @current_pos s1 = match_str("%") if s1 != :failed s3 = match_regexp(/^[\-+_A-Z]/) @@ -543,10 +566,11 @@ s0 = :failed end s0 end + # teban : "+" | "-" def parse_teban s0 = @current_pos s1 = match_str("+") if s1 != :failed @reported_pos = s0 @@ -563,10 +587,11 @@ s0 = s1 end s0 end + # comment : "'" nonls nl def parse_comment s0 = @current_pos if match_str("'") != :failed s2 = parse_nonls if parse_nl != :failed @@ -580,20 +605,22 @@ @current_pos = s0 :failed end end + # comments : comment* def parse_comments stack = [] matched = parse_comment while matched != :failed stack << matched matched = parse_comment end stack end + # time : "T" [0-9]* nl def parse_time s0 = @current_pos if match_str("T") != :failed s2 = match_digits if parse_nl != :failed @@ -608,10 +635,11 @@ s0 = :failed end s0 end + # xy : [0-9] [0-9] def parse_xy s0 = @current_pos s1 = match_digit if s1 != :failed s2 = match_digit @@ -627,10 +655,11 @@ s0 = :failed end s0 end + # piece : [A-Z] [A-Z] def parse_piece s0 = @current_pos s1 = match_regexp(/^[A-Z]/) if s1 != :failed s2 = match_regexp(/^[A-Z]/) @@ -646,10 +675,11 @@ s0 = :failed end s0 end + # xypiece : xy piece def parse_xy_piece s0 = @current_pos s1 = parse_xy if s1 != :failed s2 = parse_piece @@ -665,10 +695,11 @@ s0 = :failed end s0 end + # nl : ("\r"? "\n") | " "* "," def parse_nl s0 = @current_pos s1 = match_str("\r") s1 = nil if s1 == :failed s2 = match_str("\n") @@ -690,26 +721,27 @@ end end s0 end + # nonl : [^\r\n] 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 - protected - + # lines to jkf def transform_komabetsu_lines(lines) board = generate_empty_board hands = [ { "FU" => 0, "KY" => 0, "KE" => 0, "GI" => 0, "KI" => 0, "KA" => 0, "HI" => 0 }, { "FU" => 0, "KY" => 0, "KE" => 0, "GI" => 0, "KI" => 0, "KA" => 0, "HI" => 0 } @@ -735,10 +767,11 @@ end { "preset" => "OTHER", "data" => { "board" => board, "hands" => hands } } end + # return empty board jkf def generate_empty_board board = [] 9.times do |_i| line = [] 9.times do |_j| @@ -747,16 +780,18 @@ board << line end board end + # sec to time(m, s) def sec2time(sec) s = sec % 60 m = (sec - s) / 60 { "m" => m, "s" => s } end + # return hirate board jkf def get_hirate [ [{ "color" => 1, "kind" => "KY" }, {}, { "color" => 1, "kind" => "FU" }, {}, {}, {}, { "color" => 0, "kind" => "FU" }, {}, { "color" => 0, "kind" => "KY" }], [{ "color" => 1, "kind" => "KE" }, { "color" => 1, "kind" => "KA" }, @@ -778,9 +813,10 @@ [{ "color" => 1, "kind" => "KY" }, {}, { "color" => 1, "kind" => "FU" }, {}, {}, {}, { "color" => 0, "kind" => "FU" }, {}, { "color" => 0, "kind" => "KY" }] ] end + # normalize header key def normalize_header_key(key) { "EVENT" => "棋戦", "SITE" => "場所", "START_TIME" => "開始日時",