jkf 0.4.3 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -16,11 +16,17 @@ module Jkf::Parser
16
16
  # csa2 : version22 information? initialboard moves?
17
17
  def parse_csa2
18
18
  s0 = @current_pos
19
- if parse_version22 != :failed
19
+ if parse_version22 == :failed
20
+ @current_pos = s0
21
+ s0 = :failed
22
+ else
20
23
  s1 = parse_information
21
24
  s1 = nil if s1 == :failed
22
25
  s2 = parse_initial_board
23
- if s2 != :failed
26
+ if s2 == :failed
27
+ @current_pos = s0
28
+ s0 = :failed
29
+ else
24
30
  s3 = parse_moves
25
31
  s3 = nil if s3 == :failed
26
32
  @reported_pos = s0
@@ -32,13 +38,7 @@ module Jkf::Parser
32
38
  end
33
39
  ret
34
40
  end.call(s1, s2, s3)
35
- else
36
- @current_pos = s0
37
- s0 = :failed
38
41
  end
39
- else
40
- @current_pos = s0
41
- s0 = :failed
42
42
  end
43
43
  s0
44
44
  end
@@ -48,17 +48,17 @@ module Jkf::Parser
48
48
  s0 = @current_pos
49
49
  s1 = parse_comments
50
50
  s2 = match_str("V2.2")
51
- if s2 != :failed
51
+ if s2 == :failed
52
+ @current_pos = s0
53
+ s0 = :failed
54
+ else
52
55
  s3 = parse_nl
53
- if s3 != :failed
54
- s0 = [s1, s2, s3]
55
- else
56
+ if s3 == :failed
56
57
  @current_pos = s0
57
58
  s0 = :failed
59
+ else
60
+ s0 = [s1, s2, s3]
58
61
  end
59
- else
60
- @current_pos = s0
61
- s0 = :failed
62
62
  end
63
63
  s0
64
64
  end
@@ -69,12 +69,12 @@ module Jkf::Parser
69
69
  s1 = parse_players
70
70
  s1 = nil if s1 == :failed
71
71
  s2 = parse_headers
72
- if s2 != :failed
73
- @reported_pos = s0
74
- s0 = { "players" => s1, "header" => s2 }
75
- else
72
+ if s2 == :failed
76
73
  @current_pos = s0
77
74
  s0 = :failed
75
+ else
76
+ @reported_pos = s0
77
+ s0 = { "players" => s1, "header" => s2 }
78
78
  end
79
79
  s0
80
80
  end
@@ -103,38 +103,36 @@ module Jkf::Parser
103
103
  def parse_header
104
104
  s0 = @current_pos
105
105
  parse_comments
106
- if match_str("$") != :failed
106
+ if match_str("$") == :failed
107
+ @current_pos = s0
108
+ s0 = :failed
109
+ else
107
110
  s4 = match_regexp(/^[^:]/)
108
- if s4 != :failed
111
+ if s4 == :failed
112
+ s3 = :failed
113
+ else
109
114
  s3 = []
110
115
  while s4 != :failed
111
116
  s3 << s4
112
117
  s4 = match_regexp(/^[^:]/)
113
118
  end
114
- else
115
- s3 = :failed
116
119
  end
117
- if s3 != :failed
118
- if match_str(":") != :failed
119
- s4 = parse_nonls
120
- if parse_nl != :failed
121
- @reported_pos = s0
122
- s0 = { "k" => s3.join, "v" => s4.join }
123
- else
124
- @current_pos = s0
125
- s0 = :failed
126
- end
127
- else
120
+ if s3 == :failed
121
+ @current_pos = s0
122
+ s0 = :failed
123
+ elsif match_str(":") != :failed
124
+ s4 = parse_nonls
125
+ if parse_nl == :failed
128
126
  @current_pos = s0
129
127
  s0 = :failed
128
+ else
129
+ @reported_pos = s0
130
+ s0 = { "k" => s3.join, "v" => s4.join }
130
131
  end
131
132
  else
132
133
  @current_pos = s0
133
134
  s0 = :failed
134
135
  end
135
- else
136
- @current_pos = s0
137
- s0 = :failed
138
136
  end
139
137
  s0
140
138
  end
@@ -147,7 +145,10 @@ module Jkf::Parser
147
145
  s2 = parse_initial_board
148
146
  s2 = nil if s2 == :failed
149
147
  s3 = parse_moves
150
- if s3 != :failed
148
+ if s3 == :failed
149
+ @current_pos = s0
150
+ s0 = :failed
151
+ else
151
152
  @reported_pos = s0
152
153
  s0 = -> (ply, ini, ms) do
153
154
  ret = { "header" => {}, "initial" => ini, "moves" => ms }
@@ -157,9 +158,6 @@ module Jkf::Parser
157
158
  end
158
159
  ret
159
160
  end.call(s1, s2, s3)
160
- else
161
- @current_pos = s0
162
- s0 = :failed
163
161
  end
164
162
  s0
165
163
  end
@@ -169,34 +167,34 @@ module Jkf::Parser
169
167
  s0 = @current_pos
170
168
  parse_comments
171
169
  s2 = @current_pos
172
- if match_str("N+") != :failed
170
+ if match_str("N+") == :failed
171
+ @current_pos = s2
172
+ s2 = :failed
173
+ else
173
174
  s4 = parse_nonls
174
- if parse_nl != :failed
175
- @reported_pos = s2
176
- s2 = s4
177
- else
175
+ if parse_nl == :failed
178
176
  @current_pos = s2
179
177
  s2 = :failed
178
+ else
179
+ @reported_pos = s2
180
+ s2 = s4
180
181
  end
181
- else
182
- @current_pos = s2
183
- s2 = :failed
184
182
  end
185
183
  s2 = nil if s2 == :failed
186
184
  parse_comments
187
185
  s4 = @current_pos
188
- if match_str("N-") != :failed
186
+ if match_str("N-") == :failed
187
+ @current_pos = s4
188
+ s4 = :failed
189
+ else
189
190
  s6 = parse_nonls
190
- if parse_nl != :failed
191
- @reported_pos = s4
192
- s4 = s6
193
- else
191
+ if parse_nl == :failed
194
192
  @current_pos = s4
195
193
  s4 = :failed
194
+ else
195
+ @reported_pos = s4
196
+ s4 = s6
196
197
  end
197
- else
198
- @current_pos = s4
199
- s4 = :failed
200
198
  end
201
199
  s4 = nil if s4 == :failed
202
200
  @reported_pos = s0
@@ -221,52 +219,56 @@ module Jkf::Parser
221
219
  s2 = s3
222
220
  end
223
221
  end
224
- if s2 != :failed
222
+ if s2 == :failed
223
+ @current_pos = s0
224
+ :failed
225
+ else
225
226
  s3 = parse_komabetsu
226
- if s3 != :failed
227
+ if s3 == :failed
228
+ @current_pos = s0
229
+ :failed
230
+ else
227
231
  parse_comments
228
232
  s5 = parse_teban
229
- if s5 != :failed
230
- if parse_nl != :failed
231
- @reported_pos = s0
232
- -> (data, koma, teban) do
233
- if data == "NO"
234
- data = koma
235
- else
236
- data["data"]["hands"] = koma["data"]["hands"]
237
- end
238
- data["data"]["color"] = teban
239
- data
240
- end.call(s2, s3, s5)
241
- else
242
- @current_pos = s0
243
- :failed
244
- end
233
+ if s5 == :failed
234
+ @current_pos = s0
235
+ :failed
236
+ elsif parse_nl != :failed
237
+ @reported_pos = s0
238
+ -> (data, koma, teban) do
239
+ if data == "NO"
240
+ data = koma
241
+ else
242
+ data["data"]["hands"] = koma["data"]["hands"]
243
+ end
244
+ data["data"]["color"] = teban
245
+ data
246
+ end.call(s2, s3, s5)
245
247
  else
246
248
  @current_pos = s0
247
249
  :failed
248
250
  end
249
- else
250
- @current_pos = s0
251
- :failed
252
251
  end
253
- else
254
- @current_pos = s0
255
- :failed
256
252
  end
257
253
  end
258
254
 
259
255
  # hirate : "PI" xypiece* nl
260
256
  def parse_hirate
261
257
  s0 = @current_pos
262
- if match_str("PI") != :failed
258
+ if match_str("PI") == :failed
259
+ @current_pos = s0
260
+ s0 = :failed
261
+ else
263
262
  s2 = []
264
263
  s3 = parse_xy_piece
265
264
  while s3 != :failed
266
265
  s2 << s3
267
266
  s3 = parse_xy_piece
268
267
  end
269
- if parse_nl != :failed
268
+ if parse_nl == :failed
269
+ @current_pos = s0
270
+ s0 = :failed
271
+ else
270
272
  @reported_pos = s0
271
273
  s0 = -> (ps) do
272
274
  ret = { "preset" => "OTHER", "data" => { "board" => get_hirate } }
@@ -275,13 +277,7 @@ module Jkf::Parser
275
277
  end
276
278
  ret
277
279
  end.call(s2)
278
- else
279
- @current_pos = s0
280
- s0 = :failed
281
280
  end
282
- else
283
- @current_pos = s0
284
- s0 = :failed
285
281
  end
286
282
  s0
287
283
  end
@@ -290,14 +286,14 @@ module Jkf::Parser
290
286
  def parse_ikkatsu
291
287
  s0 = @current_pos
292
288
  s2 = parse_ikkatsu_line
293
- if s2 != :failed
289
+ if s2 == :failed
290
+ s1 = :failed
291
+ else
294
292
  s1 = []
295
293
  while s2 != :failed
296
294
  s1 << s2
297
295
  s2 = parse_ikkatsu_line
298
296
  end
299
- else
300
- s1 = :failed
301
297
  end
302
298
  if s1 != :failed
303
299
  @reported_pos = s0
@@ -320,34 +316,32 @@ module Jkf::Parser
320
316
  # ikkatsuline : "P" [1-9] masu+ nl
321
317
  def parse_ikkatsu_line
322
318
  s0 = @current_pos
323
- if match_str("P") != :failed
324
- if match_digit != :failed
325
- s4 = parse_masu
326
- if s4 != :failed
327
- s3 = []
328
- while s4 != :failed
329
- s3 << s4
330
- s4 = parse_masu
331
- end
332
- else
333
- s3 = :failed
319
+ if match_str("P") == :failed
320
+ @current_pos = s0
321
+ s0 = :failed
322
+ elsif match_digit != :failed
323
+ s4 = parse_masu
324
+ if s4 == :failed
325
+ s3 = :failed
326
+ else
327
+ s3 = []
328
+ while s4 != :failed
329
+ s3 << s4
330
+ s4 = parse_masu
334
331
  end
335
- if s3 != :failed
336
- s4 = parse_nl
337
- if s4 != :failed
338
- @reported_pos = s0
339
- s0 = s3
340
- else
341
- @current_pos = s0
342
- s0 = :failed
343
- end
344
- else
332
+ end
333
+ if s3 == :failed
334
+ @current_pos = s0
335
+ s0 = :failed
336
+ else
337
+ s4 = parse_nl
338
+ if s4 == :failed
345
339
  @current_pos = s0
346
340
  s0 = :failed
341
+ else
342
+ @reported_pos = s0
343
+ s0 = s3
347
344
  end
348
- else
349
- @current_pos = s0
350
- s0 = :failed
351
345
  end
352
346
  else
353
347
  @current_pos = s0
@@ -360,18 +354,18 @@ module Jkf::Parser
360
354
  def parse_masu
361
355
  s0 = @current_pos
362
356
  s1 = parse_teban
363
- if s1 != :failed
357
+ if s1 == :failed
358
+ @current_pos = s0
359
+ s0 = :failed
360
+ else
364
361
  s2 = parse_piece
365
- if s2 != :failed
366
- @reported_pos = s0
367
- s0 = { "color" => s1, "kind" => s2 }
368
- else
362
+ if s2 == :failed
369
363
  @current_pos = s0
370
364
  s0 = :failed
365
+ else
366
+ @reported_pos = s0
367
+ s0 = { "color" => s1, "kind" => s2 }
371
368
  end
372
- else
373
- @current_pos = s0
374
- s0 = :failed
375
369
  end
376
370
  if s0 == :failed
377
371
  s0 = @current_pos
@@ -400,38 +394,36 @@ module Jkf::Parser
400
394
  # komabetsuline : "P" teban xypiece+ nl
401
395
  def parse_komabetsu_line
402
396
  s0 = @current_pos
403
- if match_str("P") != :failed
397
+ if match_str("P") == :failed
398
+ @current_pos = s0
399
+ s0 = :failed
400
+ else
404
401
  s2 = parse_teban
405
- if s2 != :failed
402
+ if s2 == :failed
403
+ @current_pos = s0
404
+ s0 = :failed
405
+ else
406
406
  s4 = parse_xy_piece
407
- if s4 != :failed
407
+ if s4 == :failed
408
+ s3 = :failed
409
+ else
408
410
  s3 = []
409
411
  while s4 != :failed
410
412
  s3 << s4
411
413
  s4 = parse_xy_piece
412
414
  end
413
- else
414
- s3 = :failed
415
415
  end
416
- if s3 != :failed
417
- if parse_nl != :failed
418
- @reported_pos = s0
419
- s0 = { "teban" => s2, "pieces" => s3 }
420
- else
421
- @current_pos = s0
422
- s0 = :failed
423
- end
416
+ if s3 == :failed
417
+ @current_pos = s0
418
+ s0 = :failed
419
+ elsif parse_nl != :failed
420
+ @reported_pos = s0
421
+ s0 = { "teban" => s2, "pieces" => s3 }
424
422
  else
425
423
  @current_pos = s0
426
424
  s0 = :failed
427
425
  end
428
- else
429
- @current_pos = s0
430
- s0 = :failed
431
426
  end
432
- else
433
- @current_pos = s0
434
- s0 = :failed
435
427
  end
436
428
  s0
437
429
  end
@@ -440,7 +432,10 @@ module Jkf::Parser
440
432
  def parse_moves
441
433
  s0 = @current_pos
442
434
  s1 = parse_firstboard
443
- if s1 != :failed
435
+ if s1 == :failed
436
+ @current_pos = s0
437
+ s0 = :failed
438
+ else
444
439
  s2 = []
445
440
  s3 = parse_move
446
441
  while s3 != :failed
@@ -450,9 +445,6 @@ module Jkf::Parser
450
445
  parse_comments
451
446
  @reported_pos = s0
452
447
  s0 = s2.unshift(s1)
453
- else
454
- @current_pos = s0
455
- s0 = :failed
456
448
  end
457
449
  s0
458
450
  end
@@ -470,7 +462,10 @@ module Jkf::Parser
470
462
  s0 = @current_pos
471
463
  s1 = parse_normal_move
472
464
  s1 = parse_special_move if s1 == :failed
473
- if s1 != :failed
465
+ if s1 == :failed
466
+ @current_pos = s0
467
+ s0 = :failed
468
+ else
474
469
  s2 = parse_time
475
470
  s2 = nil if s2 == :failed
476
471
  s3 = parse_comments
@@ -486,9 +481,6 @@ module Jkf::Parser
486
481
  end
487
482
  ret
488
483
  end.call(s1, s2, s3)
489
- else
490
- @current_pos = s0
491
- s0 = :failed
492
484
  end
493
485
  s0
494
486
  end
@@ -497,39 +489,37 @@ module Jkf::Parser
497
489
  def parse_normal_move
498
490
  s0 = @current_pos
499
491
  s1 = parse_teban
500
- if s1 != :failed
492
+ if s1 == :failed
493
+ @current_pos = s0
494
+ s0 = :failed
495
+ else
501
496
  s2 = parse_xy
502
- if s2 != :failed
497
+ if s2 == :failed
498
+ @current_pos = s0
499
+ s0 = :failed
500
+ else
503
501
  s3 = parse_xy
504
- if s3 != :failed
502
+ if s3 == :failed
503
+ @current_pos = s0
504
+ s0 = :failed
505
+ else
505
506
  s4 = parse_piece
506
- if s4 != :failed
507
- if parse_nl != :failed
508
- @reported_pos = s0
509
- s0 = -> (color, from, to, piece) do
510
- ret = { "color" => color, "to" => to, "piece" => piece }
511
- ret["from"] = from if from["x"] != 0
512
- ret
513
- end.call(s1, s2, s3, s4)
514
- else
515
- @current_pos = s0
516
- s0 = :failed
517
- end
507
+ if s4 == :failed
508
+ @current_pos = s0
509
+ s0 = :failed
510
+ elsif parse_nl != :failed
511
+ @reported_pos = s0
512
+ s0 = -> (color, from, to, piece) do
513
+ ret = { "color" => color, "to" => to, "piece" => piece }
514
+ ret["from"] = from if from["x"] != 0
515
+ ret
516
+ end.call(s1, s2, s3, s4)
518
517
  else
519
518
  @current_pos = s0
520
519
  s0 = :failed
521
520
  end
522
- else
523
- @current_pos = s0
524
- s0 = :failed
525
521
  end
526
- else
527
- @current_pos = s0
528
- s0 = :failed
529
522
  end
530
- else
531
- @current_pos = s0
532
- s0 = :failed
533
523
  end
534
524
  s0
535
525
  end
@@ -538,32 +528,30 @@ module Jkf::Parser
538
528
  def parse_special_move
539
529
  s0 = @current_pos
540
530
  s1 = match_str("%")
541
- if s1 != :failed
531
+ if s1 == :failed
532
+ @current_pos = s0
533
+ s0 = :failed
534
+ else
542
535
  s3 = match_regexp(/^[\-+_A-Z]/)
543
- if s3 != :failed
536
+ if s3 == :failed
537
+ s2 = :failed
538
+ else
544
539
  s2 = []
545
540
  while s3 != :failed
546
541
  s2 << s3
547
542
  s3 = match_regexp(/^[\-+_A-Z]/)
548
543
  end
549
- else
550
- s2 = :failed
551
544
  end
552
- if s2 != :failed
553
- if parse_nl != :failed
554
- @reported_pos = s0
555
- s0 = { "special" => s2.join }
556
- else
557
- @current_pos = s0
558
- s0 = :failed
559
- end
545
+ if s2 == :failed
546
+ @current_pos = s0
547
+ s0 = :failed
548
+ elsif parse_nl != :failed
549
+ @reported_pos = s0
550
+ s0 = { "special" => s2.join }
560
551
  else
561
552
  @current_pos = s0
562
553
  s0 = :failed
563
554
  end
564
- else
565
- @current_pos = s0
566
- s0 = :failed
567
555
  end
568
556
  s0
569
557
  end
@@ -592,18 +580,18 @@ module Jkf::Parser
592
580
  # comment : "'" nonls nl
593
581
  def parse_comment
594
582
  s0 = @current_pos
595
- if match_str("'") != :failed
583
+ if match_str("'") == :failed
584
+ @current_pos = s0
585
+ :failed
586
+ else
596
587
  s2 = parse_nonls
597
- if parse_nl != :failed
598
- @reported_pos = s0
599
- s2.join
600
- else
588
+ if parse_nl == :failed
601
589
  @current_pos = s0
602
590
  :failed
591
+ else
592
+ @reported_pos = s0
593
+ s2.join
603
594
  end
604
- else
605
- @current_pos = s0
606
- :failed
607
595
  end
608
596
  end
609
597
 
@@ -621,18 +609,18 @@ module Jkf::Parser
621
609
  # time : "T" [0-9]* nl
622
610
  def parse_time
623
611
  s0 = @current_pos
624
- if match_str("T") != :failed
612
+ if match_str("T") == :failed
613
+ @current_pos = s0
614
+ s0 = :failed
615
+ else
625
616
  s2 = match_digits
626
- if parse_nl != :failed
627
- @reported_pos = s0
628
- s0 = { "now" => sec2time(s2.join.to_i) }
629
- else
617
+ if parse_nl == :failed
630
618
  @current_pos = s0
631
619
  s0 = :failed
620
+ else
621
+ @reported_pos = s0
622
+ s0 = { "now" => sec2time(s2.join.to_i) }
632
623
  end
633
- else
634
- @current_pos = s0
635
- s0 = :failed
636
624
  end
637
625
  s0
638
626
  end
@@ -641,18 +629,18 @@ module Jkf::Parser
641
629
  def parse_xy
642
630
  s0 = @current_pos
643
631
  s1 = match_digit
644
- if s1 != :failed
632
+ if s1 == :failed
633
+ @current_pos = s0
634
+ s0 = :failed
635
+ else
645
636
  s2 = match_digit
646
- if s2 != :failed
647
- @reported_pos = s0
648
- s0 = { "x" => s1.to_i, "y" => s2.to_i }
649
- else
637
+ if s2 == :failed
650
638
  @current_pos = s0
651
639
  s0 = :failed
640
+ else
641
+ @reported_pos = s0
642
+ s0 = { "x" => s1.to_i, "y" => s2.to_i }
652
643
  end
653
- else
654
- @current_pos = s0
655
- s0 = :failed
656
644
  end
657
645
  s0
658
646
  end
@@ -661,18 +649,18 @@ module Jkf::Parser
661
649
  def parse_piece
662
650
  s0 = @current_pos
663
651
  s1 = match_regexp(/^[A-Z]/)
664
- if s1 != :failed
652
+ if s1 == :failed
653
+ @current_pos = s0
654
+ s0 = :failed
655
+ else
665
656
  s2 = match_regexp(/^[A-Z]/)
666
- if s2 != :failed
667
- @reported_pos = s0
668
- s0 = s1 + s2
669
- else
657
+ if s2 == :failed
670
658
  @current_pos = s0
671
659
  s0 = :failed
660
+ else
661
+ @reported_pos = s0
662
+ s0 = s1 + s2
672
663
  end
673
- else
674
- @current_pos = s0
675
- s0 = :failed
676
664
  end
677
665
  s0
678
666
  end
@@ -681,18 +669,18 @@ module Jkf::Parser
681
669
  def parse_xy_piece
682
670
  s0 = @current_pos
683
671
  s1 = parse_xy
684
- if s1 != :failed
672
+ if s1 == :failed
673
+ @current_pos = s0
674
+ s0 = :failed
675
+ else
685
676
  s2 = parse_piece
686
- if s2 != :failed
687
- @reported_pos = s0
688
- s0 = { "xy" => s1, "piece" => s2 }
689
- else
677
+ if s2 == :failed
690
678
  @current_pos = s0
691
679
  s0 = :failed
680
+ else
681
+ @reported_pos = s0
682
+ s0 = { "xy" => s1, "piece" => s2 }
692
683
  end
693
- else
694
- @current_pos = s0
695
- s0 = :failed
696
684
  end
697
685
  s0
698
686
  end
@@ -703,21 +691,21 @@ module Jkf::Parser
703
691
  s1 = match_str("\r")
704
692
  s1 = nil if s1 == :failed
705
693
  s2 = match_str("\n")
706
- if s2 != :failed
707
- s0 = [s1, s2]
708
- else
694
+ if s2 == :failed
709
695
  @current_pos = s0
710
696
  s0 = :failed
697
+ else
698
+ s0 = [s1, s2]
711
699
  end
712
700
  if s0 == :failed
713
701
  s0 = @current_pos
714
702
  s1 = match_spaces
715
703
  s2 = match_str(",")
716
- if s2 != :failed
717
- s0 = [s1, s2]
718
- else
704
+ if s2 == :failed
719
705
  @current_pos = s0
720
706
  s0 = :failed
707
+ else
708
+ s0 = [s1, s2]
721
709
  end
722
710
  end
723
711
  s0