kokopu
Version:
A JavaScript/TypeScript library implementing the chess game rules and providing tools to read/write the standard chess file formats.
155 lines (154 loc) • 6.1 kB
TypeScript
/*!
* -------------------------------------------------------------------------- *
* *
* Kokopu - A JavaScript/TypeScript chess library. *
* <https://www.npmjs.com/package/kokopu> *
* Copyright (C) 2018-2025 Yoann Le Montagner <yo35 -at- melix.net> *
* *
* Kokopu is free software: you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public License *
* as published by the Free Software Foundation, either version 3 of *
* the License, or (at your option) any later version. *
* *
* Kokopu is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General *
* Public License along with this program. If not, see *
* <http://www.gnu.org/licenses/>. *
* *
* -------------------------------------------------------------------------- */
/**
* Types of tokens that could be encountered in a PGN.
*/
export declare const enum TokenType {
INVALID = 0,
BEGIN_HEADER = 1,// [
END_HEADER = 2,// ]
HEADER_ID = 3,// Identifier of a header (e.g. `White` in header `[White "Kasparov, G."]`)
HEADER_VALUE = 4,// Value of a header (e.g. `Kasparov, G.` in header `[White "Kasparov, G."]`)
MOVE_NUMBER = 5,// 42. or 23...
MOVE = 6,// SAN notation
NAG = 7,// $[1-9][0-9]* or a key from table SPECIAL_NAGS_LOOKUP (!!, +-, etc..)
COMMENT = 8,// {some text}
BEGIN_VARIATION = 9,// (
END_VARIATION = 10,// )
END_OF_GAME = 11
}
/**
* Location within a PGN text.
*/
export interface StreamPosition {
pos: number;
lineIndex: number;
}
/**
* Stream of PGN tokens.
*/
export declare class TokenStream {
/** What is being parsed. */
private _text;
/** Current position in the string. */
private _pos;
/** Current line index in the string. */
private _lineIndex;
/** Current token. */
private _token;
/** Current token value (if any). */
private _tokenValue;
/** Position of the current token in the string. */
private _tokenCharacterIndex;
/** Line index of the current token in the string. */
private _tokenLineIndex;
/** Whether an empty line has been encountered before the current token. */
private _emptyLineBeforeToken;
/** Whether an empty line will be encountered after the current token. */
private _emptyLineAfterToken;
private _matchSpaces;
private _matchLineBreak;
private _matchFastAdvance;
private _matchBeginHeader;
private _matchEndHeader;
private _matchHeaderId;
private _matchEnterHeaderValue;
private _matchMoveNumber;
private _matchMove;
private _matchNag;
private _matchEnterComment;
private _matchBeginVariation;
private _matchEndVariation;
private _matchEndOfGame;
private _headerValueMode;
private _headerValueDegradedMode;
private _commentMode;
constructor(text: string, initialLocation?: StreamPosition);
/**
* PGN string being parsed.
*/
text(): string;
/**
* Current location within the stream.
*/
currentLocation(): {
pos: number;
lineIndex: number;
};
/**
* Whether there is an empty line just before the current token. WARNING: valid only after a call to `consumeToken()`.
*/
emptyLineBeforeToken(): boolean;
/**
* Whether there is an empty line just after the current token. WARNING: valid only after a call to `consumeToken()`.
*/
emptyLineAfterToken(): boolean;
/**
* Current token type. WARNING: valid only after a call to `consumeToken()`.
*/
token(): TokenType;
/**
* Value associated to the current token, if any. WARNING: valid only after a call to `consumeToken()`.
*/
tokenValue<T>(): T;
/**
* Character index of the current token. WARNING: valid only after a call to `consumeToken()`.
*/
tokenCharacterIndex(): number;
/**
* Line index of the current token. WARNING: valid only after a call to `consumeToken()`.
*/
tokenLineIndex(): number;
/**
* Wether the current token is a token of the move-text section. WARNING: valid only after a call to `consumeToken()`.
*/
isMoveTextSection(): boolean;
/**
* Try to consume 1 token.
*
* @returns `true` if a token could have been read, `false` if the end of the text has been reached.
* @throws {@link exception.InvalidPGN} if the text cannot be interpreted as a valid token.
*/
consumeToken(): boolean;
/**
* Try to skip all the tokens until a END_OF_GAME token or the end of the file is encountered.
*
* @returns `true` if any token have been found, `false` if the end of the file has been reached without finding any token.
*/
skipGame(): boolean;
/**
* Advance until the first non-blank character.
*
* @returns `true` if an empty line has been encountered.
*/
private skipBlanks;
/**
* Try to match the given regular expression at the current position, and increment the stream cursor `this._pos`
* and the line counter `this._lineIndex` in case of a match.
*/
private testAtPos;
}
export interface TokenCommentData {
comment: string | undefined;
tags: Map<string, string>;
}