libfly  6.2.2
C++20 utility library for Linux, macOS, and Windows
fly::BasicLexer< CharType > Class Template Reference

#include <lexer.hpp>

Public Member Functions

template<std::size_t N>
constexpr BasicLexer (const CharType(&literals)[N]) noexcept
 
constexpr BasicLexer (view_type view) noexcept
 
constexpr view_type view () const
 
constexpr std::size_t position () const
 
constexpr void set_position (std::size_t position)
 
constexpr std::optional< CharType > peek (std::size_t offset=0)
 
constexpr std::optional< CharType > consume ()
 
constexpr bool consume_if (CharType ch)
 
constexpr std::optional< std::uintmax_t > consume_number ()
 
constexpr std::optional< std::uintmax_t > consume_hex_number ()
 

Detailed Description

template<StandardCharacter CharType>
class fly::BasicLexer< CharType >

Helper class to perform lexical analysis of a C-string literal. All methods are constant expressions, allowing for string analysis at compile time.

Author
Timothy Flynn (trfly.nosp@m.nn89.nosp@m.@pm.m.nosp@m.e)
Version
January 3, 2021

Constructor & Destructor Documentation

◆ BasicLexer() [1/2]

template<StandardCharacter CharType>
template<std::size_t N>
constexpr fly::BasicLexer< CharType >::BasicLexer ( const CharType(&)  literals[N])
explicitconstexprnoexcept

Constructor. Stores a view into a C-string literal.

Template Parameters
NThe size of the C-string literal.
Parameters
literalsReference to a C-string literal of size N.

◆ BasicLexer() [2/2]

template<StandardCharacter CharType>
constexpr fly::BasicLexer< CharType >::BasicLexer ( view_type  view)
explicitconstexprnoexcept

Constructor. Stores an existing view into a string.

Parameters
viewThe existing view into the string.

Member Function Documentation

◆ consume()

template<StandardCharacter CharType>
constexpr std::optional< CharType > fly::BasicLexer< CharType >::consume
constexpr

If a character is available at the current position in the C-string literal, return that character and advance the current position to the next character.

Returns
If available, the current character. Otherwise, an uninitialized value.

◆ consume_hex_number()

template<StandardCharacter CharType>
constexpr std::optional< std::uintmax_t > fly::BasicLexer< CharType >::consume_hex_number
constexpr

Beginning with the current position, retrieve characters from the C-string literal and advance the current position to the next character until a character is either not available or not a hexadecimal digit. Convert the retrieved characters to an unsigned number.

Returns
If consumed, the parsed hexadecimal number. Otherwise, an uninitialized value.

◆ consume_if()

template<StandardCharacter CharType>
constexpr bool fly::BasicLexer< CharType >::consume_if ( CharType  ch)
constexpr

If a character is available at the current position in the C-string literal, and if that character is equivalent to the provided character, advance the current position to the next character.

Parameters
chThe character to test with.
Returns
Whether the current character was available and matched the provided character.

◆ consume_number()

template<StandardCharacter CharType>
constexpr std::optional< std::uintmax_t > fly::BasicLexer< CharType >::consume_number
constexpr

Beginning with the current position, retrieve characters from the C-string literal and advance the current position to the next character until a character is either not available or not a decimal digit. Convert the retrieved characters to an unsigned number.

Returns
If consumed, the parsed decimal number. Otherwise, an uninitialized value.

◆ peek()

template<StandardCharacter CharType>
constexpr std::optional< CharType > fly::BasicLexer< CharType >::peek ( std::size_t  offset = 0)
constexpr

If a character is available at the current position (or some offset from the current position) in the C-string literal, return that character.

Parameters
offsetThe offset from the current position to peek.
Returns
If available, the character at the provided offset. Otherwise, an uninitialized value.

◆ position()

template<StandardCharacter CharType>
constexpr std::size_t fly::BasicLexer< CharType >::position
constexpr
Returns
The lexer's current position into the C-string literal.

◆ set_position()

template<StandardCharacter CharType>
constexpr void fly::BasicLexer< CharType >::set_position ( std::size_t  position)
constexpr

Set the lexer's current position into the C-string literal.

The provided position is not checked for validity. If the position is past-the-end of the C-string literal, further operations will fail (i.e. |peek| and |consume| will return std::nullopt).

Parameters
positionThe lexer's new position.

◆ view()

template<StandardCharacter CharType>
constexpr auto fly::BasicLexer< CharType >::view
constexpr
Returns
A string view into the C-string literal.

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