UNPKG

@echecs/pgn

Version:

PGN is a parser that is part of the ECHECS project, designed to interpret the PGN (Portable Game Notation) specification.

144 lines 9.55 kB
"use strict"; // Generated automatically by nearley, version 2.20.1 // http://github.com/Hardmath123/nearley (function () { function id(x) { return x[0]; } const lexer = require('./lexer.ts').default; var grammar = { Lexer: lexer, ParserRules: [ { "name": "DATABASE$ebnf$1$subexpression$1", "symbols": [(lexer.has("__") ? { type: "__" } : __), "DATABASE"] }, { "name": "DATABASE$ebnf$1", "symbols": ["DATABASE$ebnf$1$subexpression$1"], "postprocess": id }, { "name": "DATABASE$ebnf$1", "symbols": [], "postprocess": function (d) { return null; } }, { "name": "DATABASE", "symbols": ["GAME", "DATABASE$ebnf$1"], "postprocess": (d) => { const games = [d[0]]; if (d[1]) { games.push(...d[1][1]); } return games; } }, { "name": "GAME", "symbols": ["TAGS", (lexer.has("__") ? { type: "__" } : __), "MOVES", (lexer.has("__") ? { type: "__" } : __), (lexer.has("result") ? { type: "result" } : result)], "postprocess": (d) => { function pair(moves, start = 0) { return moves.reduce((acc, move, i) => { const color = (start + i) % 2 === 0 ? 'white' : 'black'; const index = Math.floor((start + i) / 2); if (acc[index] === undefined) { acc[index] = [index + 1, undefined]; } if (move.number !== undefined && move.number.value !== index + 1) { console.warn(`Warning: Move number mismatch - ${move.number.value} at line ${move.number.line} col ${move.number.col}`); } delete move.number; if (move.castling) { move.to = color === 'white' ? move.long ? 'c1' : 'g1' : move.long ? 'c8' : 'g8'; // Delete the temporary castling property delete move.long; } if (move.variants) { move.variants = move.variants.map((variant) => pair(variant, start + i)); } acc[index][color === 'white' ? 1 : 2] = move; return acc; }, []).slice(Math.floor(start / 2)); } //return ({ meta: d[0], moves, result: String(d[4]) }); const result = d[4].value; let mappedResult; switch (result) { case '1-0': mappedResult = 1; break; case '0-1': mappedResult = 0; break; case '1/2-1/2': mappedResult = 0.5; break; default: mappedResult = '?'; break; } return ({ meta: d[0], moves: pair(d[2]), result: mappedResult }); } }, { "name": "TAGS$ebnf$1$subexpression$1", "symbols": [(lexer.has("__") ? { type: "__" } : __), "TAGS"] }, { "name": "TAGS$ebnf$1", "symbols": ["TAGS$ebnf$1$subexpression$1"], "postprocess": id }, { "name": "TAGS$ebnf$1", "symbols": [], "postprocess": function (d) { return null; } }, { "name": "TAGS", "symbols": ["TAG", "TAGS$ebnf$1"], "postprocess": (d) => { const tag = d[0]; const rest = d[1] ? d[1][1] : {}; return { ...tag, ...rest }; } }, { "name": "TAG", "symbols": [{ "literal": "[" }, (lexer.has("identifier") ? { type: "identifier" } : identifier), (lexer.has("__") ? { type: "__" } : __), (lexer.has("value") ? { type: "value" } : value), { "literal": "]" }], "postprocess": (d) => ({ [d[1].value]: d[3].value }) }, { "name": "MOVES$ebnf$1", "symbols": [] }, { "name": "MOVES$ebnf$1$subexpression$1$ebnf$1", "symbols": [(lexer.has("__") ? { type: "__" } : __)], "postprocess": id }, { "name": "MOVES$ebnf$1$subexpression$1$ebnf$1", "symbols": [], "postprocess": function (d) { return null; } }, { "name": "MOVES$ebnf$1$subexpression$1", "symbols": ["MOVES$ebnf$1$subexpression$1$ebnf$1", "RAV"] }, { "name": "MOVES$ebnf$1", "symbols": ["MOVES$ebnf$1", "MOVES$ebnf$1$subexpression$1"], "postprocess": function arrpush(d) { return d[0].concat([d[1]]); } }, { "name": "MOVES$ebnf$2$subexpression$1", "symbols": [(lexer.has("__") ? { type: "__" } : __), "MOVES"] }, { "name": "MOVES$ebnf$2", "symbols": ["MOVES$ebnf$2$subexpression$1"], "postprocess": id }, { "name": "MOVES$ebnf$2", "symbols": [], "postprocess": function (d) { return null; } }, { "name": "MOVES", "symbols": ["MOVE", "MOVES$ebnf$1", "MOVES$ebnf$2"], "postprocess": (d) => { let moves = [d[0]]; if (d[1].length > 0) { moves[0].variants = d[1].map(d1 => d1[1]); } if (d[2]) { moves.push(...d[2][1]); } return moves; } }, { "name": "MOVE$ebnf$1", "symbols": [(lexer.has("number") ? { type: "number" } : number)], "postprocess": id }, { "name": "MOVE$ebnf$1", "symbols": [], "postprocess": function (d) { return null; } }, { "name": "MOVE$ebnf$2", "symbols": [(lexer.has("__") ? { type: "__" } : __)], "postprocess": id }, { "name": "MOVE$ebnf$2", "symbols": [], "postprocess": function (d) { return null; } }, { "name": "MOVE$ebnf$3", "symbols": [] }, { "name": "MOVE$ebnf$3$subexpression$1$ebnf$1", "symbols": [(lexer.has("__") ? { type: "__" } : __)], "postprocess": id }, { "name": "MOVE$ebnf$3$subexpression$1$ebnf$1", "symbols": [], "postprocess": function (d) { return null; } }, { "name": "MOVE$ebnf$3$subexpression$1", "symbols": ["MOVE$ebnf$3$subexpression$1$ebnf$1", "NAG"] }, { "name": "MOVE$ebnf$3", "symbols": ["MOVE$ebnf$3", "MOVE$ebnf$3$subexpression$1"], "postprocess": function arrpush(d) { return d[0].concat([d[1]]); } }, { "name": "MOVE$ebnf$4", "symbols": [] }, { "name": "MOVE$ebnf$4$subexpression$1$ebnf$1", "symbols": [(lexer.has("__") ? { type: "__" } : __)], "postprocess": id }, { "name": "MOVE$ebnf$4$subexpression$1$ebnf$1", "symbols": [], "postprocess": function (d) { return null; } }, { "name": "MOVE$ebnf$4$subexpression$1", "symbols": ["MOVE$ebnf$4$subexpression$1$ebnf$1", "COMMENT"] }, { "name": "MOVE$ebnf$4", "symbols": ["MOVE$ebnf$4", "MOVE$ebnf$4$subexpression$1"], "postprocess": function arrpush(d) { return d[0].concat([d[1]]); } }, { "name": "MOVE", "symbols": ["MOVE$ebnf$1", "MOVE$ebnf$2", (lexer.has("san") ? { type: "san" } : san), "MOVE$ebnf$3", "MOVE$ebnf$4"], "postprocess": (d) => { // We keep the token for number so we can warn about mismatches. Not really // useful as numbers could be out of order. const number = d[0] ?? undefined; const annotations = d[3].map(d3 => d3[1]); const comments = d[4].map(d4 => d4[1]).filter(Boolean); const san = d[2].value; return { number, ...(annotations.length > 0 && { annotations }), ...(comments.length > 0 && { comment: comments.join(' ').replace(/\n/g, '') }), ...san, }; } }, { "name": "RAV$ebnf$1", "symbols": [(lexer.has("__") ? { type: "__" } : __)], "postprocess": id }, { "name": "RAV$ebnf$1", "symbols": [], "postprocess": function (d) { return null; } }, { "name": "RAV$ebnf$2", "symbols": [(lexer.has("__") ? { type: "__" } : __)], "postprocess": id }, { "name": "RAV$ebnf$2", "symbols": [], "postprocess": function (d) { return null; } }, { "name": "RAV", "symbols": [{ "literal": "(" }, "RAV$ebnf$1", "MOVES", "RAV$ebnf$2", { "literal": ")" }], "postprocess": (d) => d[2] }, { "name": "NAG", "symbols": [(lexer.has("nag_import") ? { type: "nag_import" } : nag_import)], "postprocess": (d) => d[0].value }, { "name": "NAG", "symbols": [(lexer.has("nag_export") ? { type: "nag_export" } : nag_export)], "postprocess": id }, { "name": "COMMENT", "symbols": [(lexer.has("comment_line") ? { type: "comment_line" } : comment_line)] }, { "name": "COMMENT", "symbols": [(lexer.has("comment_multiline") ? { type: "comment_multiline" } : comment_multiline)] } ], ParserStart: "DATABASE" }; if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') { module.exports = grammar; } else { window.grammar = grammar; } })(); //# sourceMappingURL=grammar.cjs.map