earley-sgf
Version:
Early algorithm used to parse SGF file
54 lines (53 loc) • 1.42 kB
JavaScript
function classify(prefix) {
switch (prefix) {
case 'SZ': /* board size */
case 'KM': /* komi */
case 'DT': /* date */
case 'B': /* move */
case 'W':
case 'PB': /* player */
case 'PW':
case 'TB': /* territory */
case 'TW':
case 'TE':
case 'AB': /* setup */
case 'AW':
case 'AE':
case 'RE': /* result */
return prefix;
default:
return 'prefix';
}
}
/*
*
* The reserved characters are the brackets and the semicolon
*/
const reserved = /[\]\[\(\);]/;
/*
* The tokens are : the reserved characters, strings between reserved characters.
*/
export function* sgfTokenize(data) {
let previous = '';
while (data.length > 0) {
let regexp = (previous === '[') ? ']' : reserved;
const b = data.search(regexp);
if (b < 0) {
throw new Error('Invalid SGF ' + data);
}
if (0 < b) {
let text = data.slice(0, b).trim();
if (0 < text.length) {
if (previous === '[') {
yield { token: 'text', text };
}
else {
yield { token: classify(text), text };
}
}
}
previous = data.at(b);
yield { token: previous, text: previous };
data = data.slice(b + 1);
}
}