-
Notifications
You must be signed in to change notification settings - Fork 1
/
SGFParser.cy
80 lines (70 loc) · 1.73 KB
/
SGFParser.cy
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
%class_name SGFParser
%token_type SGFToken
%nonterminal_type prop_value_list "[String]"
%nonterminal_type property "(String, [String])"
%nonterminal_type property_list "[(String, [String])]"
%nonterminal_type node SGFNode
%nonterminal_type sequence SGFNode
%nonterminal_type gametree SGFNode
%nonterminal_type gametree_list "[SGFNode]"
%nonterminal_type collection "[SGFNode]"
%nonterminal_type output "[SGFNode]"
output ::= collection(a). {
return a
}
collection ::= gametree(a). {
return [a]
}
collection ::= gametree(a) collection(b). {
return [a] + b
}
gametree ::= OPEN_PARENTHESIS sequence(a) CLOSE_PARENTHESIS. {
return a
}
gametree ::= OPEN_PARENTHESIS sequence(a) gametree_list(b) CLOSE_PARENTHESIS. {
var node = a
while node.children.count > 0 {
node = node.children[0]
}
node.children.append(contentsOf: b)
return a
}
gametree_list ::= gametree(a). {
return [a]
}
gametree_list ::= gametree(a) gametree_list(b). {
return [a] + b
}
sequence ::= node(a). {
return a
}
sequence ::= node(a) sequence(b). {
a.children.append(b)
return a
}
node ::= SEMICOLUMN. {
return SGFNode()
}
node ::= SEMICOLUMN property_list(a). {
let node = SGFNode()
for (k, v) in a {
node.properties[k] = v
}
return node
}
property_list ::= property(a). {
return [a]
}
property_list ::= property(a) property_list(b). {
return [a] + b
}
property ::= PROP_INDENT(a) prop_value_list(b). {
return (a.toIdentifierString()!, b)
}
prop_value_list ::= VALUE(a). {
return [a.toValueString()!]
}
prop_value_list ::= VALUE(a) prop_value_list(b). {
return [a.toValueString()!] + b
}
//value_type ::= NUMBER | REAL | DOUBLE | COLOR | SIMPLE_TEXT | TEXT | POINT | MOVE | STONE