HooverChessUtils_PgnReader 0.9.0
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Protected Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | Static Private Attributes | List of all members
hoover_chess_utils::pgn_reader::PgnScanner Class Reference

The PGN scanner (lexer) More...

#include <pgnscanner.h>

Inheritance diagram for hoover_chess_utils::pgn_reader::PgnScanner:
Inheritance graph
[legend]
Collaboration diagram for hoover_chess_utils::pgn_reader::PgnScanner:
Collaboration graph
[legend]

Public Member Functions

 PgnScanner (const char *inputData, std::size_t inputLen) noexcept
 Constructor.
 
 PgnScanner (const PgnScanner &)=delete
 
 PgnScanner (PgnScanner &&)=delete
 
PgnScanneroperator= (const PgnScanner &) &=delete
 
PgnScanneroperator= (PgnScanner &&) &=delete
 
PgnScannerToken nextTokenNoThrowOnErrorToken ()
 Scans input and returns the next token. On bad input, error token is returned instead of throwing an exception.
 
PgnScannerToken nextToken ()
 Scans input and returns the next token.
 
PgnScannerToken getCurrentToken () const noexcept
 Returns the previously scanned token.
 
const PgnScannerTokenInfogetTokenInfo () const noexcept
 Returns additional information on the token.
 

Static Public Member Functions

static constexpr const char * scannerTokenToString (PgnScannerToken token) noexcept
 Returns a string for a scanner token.
 

Protected Member Functions

int LexerInput (char *buf, int maxSize)
 Provides more input to the parent class (general flex version)
 
std::size_t LexerInput (char *buf, std::size_t maxSize)
 Provides more input to the parent class (Apple flex version)
 

Private Member Functions

PgnScannerToken yylexex ()
 The generated lexer.
 
template<typename RetType , bool digitsOnly>
RetType asciiToUnsigned (const char *str, const char *end, const char *tokenType)
 Ascii to unsigned integer conversion.
 
void setTokenInfo_MOVENUM (const char *str, const char *end)
 
void setTokenInfo_PAWN_MOVE (SquareSet srcMask, Square dstSq, Piece promoPiece)
 
void setTokenInfo_PIECE_MOVE (SquareSet srcMask, bool capture, Square dstSq)
 
void setTokenInfo_NAG (std::uint8_t nag)
 
void setTokenInfo_RESULT (PgnResult result)
 
void setTokenInfo_ERROR (const char *errorMessage)
 
PgnScannerToken tokenizePieceMove (std::string_view str)
 
PgnScannerToken tokenizeUnusualPawnMove (std::string_view str)
 

Static Private Member Functions

static constexpr Square charCoordToSq (char colChar, char rowChar) noexcept
 Translates character coordinates to a square.
 
static constexpr SquareSet colCharToMask (char colChar) noexcept
 Translates column coordinate (file) to a square set.
 
static constexpr SquareSet rowCharToMask (char rowChar) noexcept
 Translates row coordinate (rank) to a square set.
 
static constexpr Piece getPieceForChar (char c) noexcept
 
static constexpr PgnScannerToken getMovePieceScannerTokenForChar (char c) noexcept
 

Private Attributes

const char * m_inputData
 
std::size_t m_inputLeft
 
PgnScannerToken m_curToken { }
 
PgnScannerTokenInfo m_tokenInfo
 

Static Private Attributes

static constexpr std::array< Piece, 32 > ctCharToPieceTable
 
static constexpr std::array< PgnScannerToken, 32 > ctCharToMovePieceTable
 

Detailed Description

The PGN scanner (lexer)

Constructor & Destructor Documentation

◆ PgnScanner() [1/3]

hoover_chess_utils::pgn_reader::PgnScanner::PgnScanner ( const char *  inputData,
std::size_t  inputLen 
)
inlinenoexcept

Constructor.

Parameters
[in]inputDataInput data
[in]inputLenInput length

◆ PgnScanner() [2/3]

hoover_chess_utils::pgn_reader::PgnScanner::PgnScanner ( const PgnScanner )
delete

◆ PgnScanner() [3/3]

hoover_chess_utils::pgn_reader::PgnScanner::PgnScanner ( PgnScanner &&  )
delete

Member Function Documentation

◆ asciiToUnsigned()

template<typename RetType , bool digitsOnly>
RetType hoover_chess_utils::pgn_reader::PgnScanner::asciiToUnsigned ( const char *  str,
const char *  end,
const char *  tokenType 
)
inlineprivate

Ascii to unsigned integer conversion.

Template Parameters
RetTypeUnsigned integer type
digitsOnlyWhether the input is guaranteed to include only digits
Parameters
[in]strStart of string to convert (inclusive)
[in]endEnd of string to convert (exclusive)
[in]tokenTypeName of the token type, e.g., "NAG". Used for error reporting
Returns
Unsigned integer
Exceptions
PgnError(PgnErrorCode::BAD_CHARACTER)Integer overflow
Precondition
When digitsOnly = true, this function assumes that the string consists only of numbers.

In case digitsOnly is:

  • true: This function assumes that the string consists only of numbers. If this is not the case, this function results in undefined behavior.
  • false: This function returns early when the first non-digit character is encountered.

◆ charCoordToSq()

static constexpr Square hoover_chess_utils::pgn_reader::PgnScanner::charCoordToSq ( char  colChar,
char  rowChar 
)
inlinestaticconstexprprivatenoexcept

Translates character coordinates to a square.

Parameters
[in]colCharColumn character (file), range: ['a', 'h']
[in]rowCharRow character (rank), range: ['1', '8']
Returns
Corresponding square

◆ colCharToMask()

static constexpr SquareSet hoover_chess_utils::pgn_reader::PgnScanner::colCharToMask ( char  colChar)
inlinestaticconstexprprivatenoexcept

Translates column coordinate (file) to a square set.

Parameters
[in]colCharColumn character (file), range: ['a', 'h']
Returns
Corresponding set of squares

◆ getCurrentToken()

PgnScannerToken hoover_chess_utils::pgn_reader::PgnScanner::getCurrentToken ( ) const
inlinenoexcept

Returns the previously scanned token.

Returns
Token

The token in textual format can be obtained with calls to YYText() and YYLeng() provided by the parent class.

Certain tokens are decoded by the scanner. The decoded information is obtained with a call to getTokenInfo().

See also
https://westes.github.io/flex/manual/Cxx.html

◆ getMovePieceScannerTokenForChar()

static constexpr PgnScannerToken hoover_chess_utils::pgn_reader::PgnScanner::getMovePieceScannerTokenForChar ( char  c)
inlinestaticconstexprprivatenoexcept

◆ getPieceForChar()

static constexpr Piece hoover_chess_utils::pgn_reader::PgnScanner::getPieceForChar ( char  c)
inlinestaticconstexprprivatenoexcept

◆ getTokenInfo()

const PgnScannerTokenInfo & hoover_chess_utils::pgn_reader::PgnScanner::getTokenInfo ( ) const
inlinenoexcept

Returns additional information on the token.

Returns
Additional inforation on the current token

◆ LexerInput() [1/2]

int hoover_chess_utils::pgn_reader::PgnScanner::LexerInput ( char *  buf,
int  maxSize 
)
inlineprotected

Provides more input to the parent class (general flex version)

Parameters
[in]bufBuffer for input data
[in]maxSizeMaximum size of data to be provided
Returns
Number of bytes provided
See also
https://westes.github.io/flex/manual/Cxx.html

◆ LexerInput() [2/2]

std::size_t hoover_chess_utils::pgn_reader::PgnScanner::LexerInput ( char *  buf,
std::size_t  maxSize 
)
inlineprotected

Provides more input to the parent class (Apple flex version)

Parameters
[in]bufBuffer for input data
[in]maxSizeMaximum size of data to be provided
Returns
Number of bytes provided
See also
https://westes.github.io/flex/manual/Cxx.html

◆ nextToken()

PgnScannerToken hoover_chess_utils::pgn_reader::PgnScanner::nextToken ( )
inline

Scans input and returns the next token.

Returns
Token
Exceptions
PgnError(PgnErrorCode::BAD_CHARACTER)Erroneous scanner input

The token in textual format can be obtained with calls to YYText() and YYLeng() provided by the parent class.

Certain tokens are decoded by the scanner. The decoded information is obtained with a call to getTokenInfo().

See also
https://westes.github.io/flex/manual/Cxx.html

◆ nextTokenNoThrowOnErrorToken()

PgnScannerToken hoover_chess_utils::pgn_reader::PgnScanner::nextTokenNoThrowOnErrorToken ( )
inline

Scans input and returns the next token. On bad input, error token is returned instead of throwing an exception.

Returns
Token

This function is mainly intended for error recovery.

◆ operator=() [1/2]

PgnScanner & hoover_chess_utils::pgn_reader::PgnScanner::operator= ( const PgnScanner ) &
delete

◆ operator=() [2/2]

PgnScanner & hoover_chess_utils::pgn_reader::PgnScanner::operator= ( PgnScanner &&  ) &
delete

◆ rowCharToMask()

static constexpr SquareSet hoover_chess_utils::pgn_reader::PgnScanner::rowCharToMask ( char  rowChar)
inlinestaticconstexprprivatenoexcept

Translates row coordinate (rank) to a square set.

Parameters
[in]rowCharRow character (rank), range: ['1', '8']
Returns
Corresponding set of squares

◆ scannerTokenToString()

static constexpr const char * hoover_chess_utils::pgn_reader::PgnScanner::scannerTokenToString ( PgnScannerToken  token)
inlinestaticconstexprnoexcept

Returns a string for a scanner token.

Parameters
[in]tokenPGN scanner token
Returns
Corresponding string

◆ setTokenInfo_ERROR()

void hoover_chess_utils::pgn_reader::PgnScanner::setTokenInfo_ERROR ( const char *  errorMessage)
inlineprivate

◆ setTokenInfo_MOVENUM()

void hoover_chess_utils::pgn_reader::PgnScanner::setTokenInfo_MOVENUM ( const char *  str,
const char *  end 
)
inlineprivate

◆ setTokenInfo_NAG()

void hoover_chess_utils::pgn_reader::PgnScanner::setTokenInfo_NAG ( std::uint8_t  nag)
inlineprivate

◆ setTokenInfo_PAWN_MOVE()

void hoover_chess_utils::pgn_reader::PgnScanner::setTokenInfo_PAWN_MOVE ( SquareSet  srcMask,
Square  dstSq,
Piece  promoPiece 
)
inlineprivate

◆ setTokenInfo_PIECE_MOVE()

void hoover_chess_utils::pgn_reader::PgnScanner::setTokenInfo_PIECE_MOVE ( SquareSet  srcMask,
bool  capture,
Square  dstSq 
)
inlineprivate

◆ setTokenInfo_RESULT()

void hoover_chess_utils::pgn_reader::PgnScanner::setTokenInfo_RESULT ( PgnResult  result)
inlineprivate

◆ tokenizePieceMove()

PgnScannerToken hoover_chess_utils::pgn_reader::PgnScanner::tokenizePieceMove ( std::string_view  str)
inlineprivate

◆ tokenizeUnusualPawnMove()

PgnScannerToken hoover_chess_utils::pgn_reader::PgnScanner::tokenizeUnusualPawnMove ( std::string_view  str)
inlineprivate

◆ yylexex()

PgnScannerToken hoover_chess_utils::pgn_reader::PgnScanner::yylexex ( )
private

The generated lexer.

Returns
Token

Member Data Documentation

◆ ctCharToMovePieceTable

constexpr std::array<PgnScannerToken, 32> hoover_chess_utils::pgn_reader::PgnScanner::ctCharToMovePieceTable
staticconstexprprivate

◆ ctCharToPieceTable

constexpr std::array<Piece, 32> hoover_chess_utils::pgn_reader::PgnScanner::ctCharToPieceTable
staticconstexprprivate

◆ m_curToken

PgnScannerToken hoover_chess_utils::pgn_reader::PgnScanner::m_curToken { }
private

◆ m_inputData

const char* hoover_chess_utils::pgn_reader::PgnScanner::m_inputData
private

◆ m_inputLeft

std::size_t hoover_chess_utils::pgn_reader::PgnScanner::m_inputLeft
private

◆ m_tokenInfo

PgnScannerTokenInfo hoover_chess_utils::pgn_reader::PgnScanner::m_tokenInfo
private

The documentation for this class was generated from the following file: