-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
171 lines (139 loc) · 3.1 KB
/
index.js
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define([], factory);
} else if (typeof module === 'object' && module.exports) {
module.exports = factory();
} else {
root.SGF = factory();
}
})(this, function () {
function isWhitespace(c) {
return /^\s$/.test(c);
}
function isEscape(c) {
return /^\\$/.test(c);
}
function isAlpha(c) {
return /^[a-zA-Z]$/.test(c);
}
function createSource(sgf) {
var data = sgf.split('');
var position = 0;
function peek() {
if (position >= data.length) {
throw new Error('Unexpected end of input.');
}
return data[position];
}
function next() {
position++;
return position < data.length;
}
function skipws() {
while (isWhitespace(peek()) && next());
}
return {
peek: peek,
next: next,
skipws: skipws
};
}
function parseIdent(source) {
source.skipws();
var ident = '';
while (isAlpha(source.peek())) {
ident += source.peek();
source.next();
}
return ident;
}
function parseSingleValue(source) {
source.skipws();
source.next();
var value = '';
while (source.peek() != ']') {
if (isEscape(source.peek())) {
source.next();
}
value += source.peek();
source.next();
}
source.next();
return value;
}
function parseValue(source) {
source.skipws();
var values = [];
while (source.peek() == '[') {
var value = parseSingleValue(source);
values.push(value);
source.skipws();
}
return (values.length === 1) ? values[0] : values;
}
function parseProperty(source) {
source.skipws();
var ident = parseIdent(source);
var value = parseValue(source);
return {
ident: ident,
value: value
};
}
function parseNode(source) {
source.next();
var node = {
props: {},
childs: []
};
while (true) {
source.skipws();
if (!isAlpha(source.peek())) {
break;
}
var property = parseProperty(source);
node.props[property.ident] = property.value;
}
return node;
}
function parseTree(source) {
source.skipws();
if (source.peek() !== '(') {
throw new Error('Expected "(", got "' + source.peek() + '"');
}
source.next();
var tree = null;
var cursor = null;
while (source.peek() !== ')') {
switch (source.peek()) {
case ';':
var node = parseNode(source);
if (cursor) {
cursor.childs.push(node);
} else {
tree = node;
}
cursor = node;
break;
case '(':
var node = parseTree(source);
cursor.childs.push(node);
break;
default:
if (!isWhitespace(source.peek())) {
throw new Error('Malformed input.');
}
source.next();
break;
}
}
source.next();
return tree;
}
function parse(sgf) {
return parseTree(createSource(sgf));
}
return {
parse: parse
};
});