-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpgn-abnf-impl.scm
138 lines (115 loc) · 2.88 KB
/
pgn-abnf-impl.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
(import
scheme
(chicken base)
(chicken io)
(prefix abnf abnf:)
(prefix abnf-consumers abnf:)
test
(only utf8-srfi-14 char-set char-set-difference char-set-union
char-set:graphic char-set:printing char-set:ascii char-set:full))
(include-relative "matchers.scm")
(define tagkey
(abnf:bind-consumed->string
(abnf:concatenation (:+ abnf:alpha))))
(define tagvalue
(abnf:concatenation
(:! abnf:dquote)
(abnf:bind-consumed->string (:* ttext))
(:! abnf:dquote)))
(define pgn-tag
(abnf:bind-consumed-strings->list
'tag
(abnf:concatenation
begin-tag
tagkey
(:! abnf:wsp)
tagvalue
end-tag)))
(define piece (abnf:set-from-string "KNRBQknrbq" ))
(define rank (abnf:set-from-string "12345678" ))
(define file (abnf:set-from-string "abcdefgh" ))
(define capturechar (abnf:char #\x ))
(define dotchar (:! (abnf:char #\.)))
(define lwsp (:! abnf:lwsp))
(define annotation (abnf:set-from-string "=?!+#"))
(define castling
(abnf:concatenation
(abnf:lit "O-O")
(:?
(abnf:lit "-O"))
(:* annotation)))
(define result-variations
(abnf:alternatives
(abnf:lit "1-0")
(abnf:lit "0-1")
(abnf:lit "1/2-1/2")
(abnf:lit "*")))
(define result (between-fws result-variations ))
(define comment-text
(:!
(abnf:concatenation
(:! (abnf:char #\{) )
(:*
(abnf:concatenation
(:? fws)
ctext
(:? fws)))
(:? fws)
(:! (abnf:char #\})))))
(define comment (between-fws comment-text ))
(define move-number
(:!
(abnf:concatenation
(:+ abnf:decimal)
dotchar
lwsp)))
(define ply-text
(abnf:alternatives
castling
(abnf:concatenation
(abnf:alternatives file piece)
(abnf:alternatives file capturechar piece rank)
(:* (abnf:alternatives file piece capturechar rank annotation)))))
(define ply (between-fws ply-text ) )
;; This adds more match definitions for splitting files.
(include-relative "matcher-splitter.scm")
(define pgn-move
(abnf:bind-consumed-strings->list
'move
(abnf:concatenation
move-number
(:* (abnf:alternatives
comment
(abnf:bind-consumed->string ply)
(abnf:bind-consumed->string result))))))
(define moves-without-result
(abnf:bind-consumed-strings->list
(abnf:concatenation
move-number
(abnf:bind-consumed->string ply)
(:* (abnf:alternatives
comment
(abnf:bind-consumed->string ply))))))
;;matches a standalone line with result at the end.
(define pgn-all-moves-with-result
(abnf:concatenation
(:+ moves-without-result)
(abnf:bind-consumed->string result)))
(define pgn-tag-list
(:*
(abnf:concatenation
pgn-tag
newlines)))
(define pgn-move-list
(abnf:concatenation
(:*
(abnf:alternatives
comment
pgn-move))))
(define pgn-game
(abnf:bind-consumed-strings->list
'game
(abnf:concatenation
pgn-tag-list
pgn-move-list)))
(define pgn-db (:+ pgn-game))