header_utils
Loading...
Searching...
No Matches
Parsing

Functions for parsing typical value and token types from strings. More...

Classes

struct  ghassanpl::parsing::parse_error
 An exception type that will be thrown when eating/parsing fails. More...

Functions

size_t ghassanpl::parsing::find_line_number (std::string_view of_string, std::string_view in_document) noexcept
 Given a multiline in_document, will return the line number (starting at 1) of the line which contains the start of of_string.
std::pair< size_t, size_t > ghassanpl::parsing::find_line_and_column (std::string_view of_string, std::string_view in_document) noexcept
 Given a multiline in_document, will return the line and column numbers (starting at 1) of the start of of_string.
std::string_view ghassanpl::parsing::consume_c_identifier (std::string_view &str)
 Will consume a C-style identifier: [A-Za-z_][A-Za-z0-9_]*.
std::string_view ghassanpl::parsing::consume_c_identifier_with (std::string_view &str, std::string_view additional_chars)
 Will consume a C-style identifier that may contain any additional characters from additional_chars: '[A-Za-z_'+additional_chars+'][A-Za-z0-9_'+additional_chars+']*'.
std::pair< std::string_view, double > ghassanpl::parsing::consume_c_float (std::string_view &str)
std::pair< std::string_view, int64_t > ghassanpl::parsing::consume_c_integer (std::string_view &str, int base)
std::pair< std::string_view, uint64_t > ghassanpl::parsing::consume_c_unsigned (std::string_view &str, int base)
template<char DELIMITER>
std::pair< std::string_view, std::string > ghassanpl::parsing::consume_c_string (std::string_view &strv)
bool ghassanpl::parsing::try_eat (std::string_view &str, std::string_view what)
bool ghassanpl::parsing::try_eat (std::string_view &str, char what)
void ghassanpl::parsing::eat (std::string_view &str, std::string_view what)
void ghassanpl::parsing::eat (std::string_view &str, char what)
std::string_view ghassanpl::parsing::try_eat_identifier (std::string_view &str)
std::string_view ghassanpl::parsing::eat_identifier (std::string_view &str)
std::string_view ghassanpl::parsing::try_eat_identifier_with (std::string_view &str, std::string_view additional_chars)
std::string_view ghassanpl::parsing::eat_identifier_with (std::string_view &str, std::string_view additional_chars)
std::string_view ghassanpl::parsing::eat_whitespace (std::string_view &str)
bool ghassanpl::parsing::try_eat_line_comment (std::string_view &str, std::string_view comment_start)
bool ghassanpl::parsing::try_eat_unsigned (std::string_view &str, uint64_t &result, int base)
std::optional< uint64_t > ghassanpl::parsing::try_eat_unsigned (std::string_view &str, int base)
bool ghassanpl::parsing::try_eat_integer (std::string_view &str, int64_t &result, int base)
std::optional< int64_t > ghassanpl::parsing::try_eat_integer (std::string_view &str, int base)
bool ghassanpl::parsing::try_eat_float (std::string_view &str, double &result)
std::optional< double > ghassanpl::parsing::try_eat_float (std::string_view &str)
uint64_t ghassanpl::parsing::eat_unsigned (std::string_view &str, int base)
int64_t ghassanpl::parsing::eat_integer (std::string_view &str, int base)
double ghassanpl::parsing::eat_float (std::string_view &str)
 Eats a C-style floating-point value.
char32_t ghassanpl::parsing::try_eat_utf8_codepoint (std::string_view &str)
char32_t ghassanpl::parsing::eat_utf8_codepoint (std::string_view &str)

Detailed Description

Functions for parsing typical value and token types from strings.

Warning
This code partially relies on the std::from_chars functionality, which is not available in the lastest Clang libc++ versions (for some reason).

A failure usually results in throwing a parse_error

Function Documentation

◆ consume_c_float()

std::pair< std::string_view, double > ghassanpl::parsing::consume_c_float ( std::string_view & str)
inlinenodiscard

Definition at line 145 of file parsing.h.

◆ consume_c_identifier()

std::string_view ghassanpl::parsing::consume_c_identifier ( std::string_view & str)
inlinenodiscard

Will consume a C-style identifier: [A-Za-z_][A-Za-z0-9_]*.

Definition at line 121 of file parsing.h.

◆ consume_c_identifier_with()

std::string_view ghassanpl::parsing::consume_c_identifier_with ( std::string_view & str,
std::string_view additional_chars )
inlinenodiscard

Will consume a C-style identifier that may contain any additional characters from additional_chars: '[A-Za-z_'+additional_chars+'][A-Za-z0-9_'+additional_chars+']*'.

Definition at line 132 of file parsing.h.

◆ consume_c_integer()

std::pair< std::string_view, int64_t > ghassanpl::parsing::consume_c_integer ( std::string_view & str,
int base = 10 )
inlinenodiscard

Definition at line 161 of file parsing.h.

◆ consume_c_string()

template<char DELIMITER>
std::pair< std::string_view, std::string > ghassanpl::parsing::consume_c_string ( std::string_view & strv)
inlinenodiscard

Definition at line 228 of file parsing.h.

◆ consume_c_unsigned()

std::pair< std::string_view, uint64_t > ghassanpl::parsing::consume_c_unsigned ( std::string_view & str,
int base = 10 )
inlinenodiscard

Definition at line 177 of file parsing.h.

◆ eat() [1/2]

void ghassanpl::parsing::eat ( std::string_view & str,
char what )
inline

Definition at line 350 of file parsing.h.

◆ eat() [2/2]

void ghassanpl::parsing::eat ( std::string_view & str,
std::string_view what )
inline

Definition at line 344 of file parsing.h.

◆ eat_float()

double ghassanpl::parsing::eat_float ( std::string_view & str)
inline

Eats a C-style floating-point value.

Definition at line 462 of file parsing.h.

◆ eat_identifier()

std::string_view ghassanpl::parsing::eat_identifier ( std::string_view & str)
inline

Definition at line 362 of file parsing.h.

◆ eat_identifier_with()

std::string_view ghassanpl::parsing::eat_identifier_with ( std::string_view & str,
std::string_view additional_chars )
inline

Definition at line 376 of file parsing.h.

◆ eat_integer()

int64_t ghassanpl::parsing::eat_integer ( std::string_view & str,
int base = 10 )
inline

Definition at line 454 of file parsing.h.

◆ eat_unsigned()

uint64_t ghassanpl::parsing::eat_unsigned ( std::string_view & str,
int base = 10 )
inline

Definition at line 446 of file parsing.h.

◆ eat_utf8_codepoint()

char32_t ghassanpl::parsing::eat_utf8_codepoint ( std::string_view & str)
inline

Definition at line 475 of file parsing.h.

◆ eat_whitespace()

std::string_view ghassanpl::parsing::eat_whitespace ( std::string_view & str)
inline

Definition at line 384 of file parsing.h.

◆ find_line_and_column()

std::pair< size_t, size_t > ghassanpl::parsing::find_line_and_column ( std::string_view of_string,
std::string_view in_document )
inlinenodiscardnoexcept

Given a multiline in_document, will return the line and column numbers (starting at 1) of the start of of_string.

Precondition
of_string must be somewhere inside in_document

Definition at line 99 of file parsing.h.

◆ find_line_number()

size_t ghassanpl::parsing::find_line_number ( std::string_view of_string,
std::string_view in_document )
inlinenodiscardnoexcept

Given a multiline in_document, will return the line number (starting at 1) of the line which contains the start of of_string.

Precondition
of_string must be somewhere inside in_document

Definition at line 88 of file parsing.h.

◆ try_eat() [1/2]

bool ghassanpl::parsing::try_eat ( std::string_view & str,
char what )
inline

Definition at line 335 of file parsing.h.

◆ try_eat() [2/2]

bool ghassanpl::parsing::try_eat ( std::string_view & str,
std::string_view what )
inline

Definition at line 326 of file parsing.h.

◆ try_eat_float() [1/2]

std::optional< double > ghassanpl::parsing::try_eat_float ( std::string_view & str)
inline

Definition at line 439 of file parsing.h.

◆ try_eat_float() [2/2]

bool ghassanpl::parsing::try_eat_float ( std::string_view & str,
double & result )
inline

Definition at line 430 of file parsing.h.

◆ try_eat_identifier()

std::string_view ghassanpl::parsing::try_eat_identifier ( std::string_view & str)
inline

Definition at line 356 of file parsing.h.

◆ try_eat_identifier_with()

std::string_view ghassanpl::parsing::try_eat_identifier_with ( std::string_view & str,
std::string_view additional_chars )
inline

Definition at line 370 of file parsing.h.

◆ try_eat_integer() [1/2]

std::optional< int64_t > ghassanpl::parsing::try_eat_integer ( std::string_view & str,
int base = 10 )
inline

Definition at line 423 of file parsing.h.

◆ try_eat_integer() [2/2]

bool ghassanpl::parsing::try_eat_integer ( std::string_view & str,
int64_t & result,
int base = 10 )
inline

Definition at line 414 of file parsing.h.

◆ try_eat_line_comment()

bool ghassanpl::parsing::try_eat_line_comment ( std::string_view & str,
std::string_view comment_start = "//" )
inline

Definition at line 389 of file parsing.h.

◆ try_eat_unsigned() [1/2]

std::optional< uint64_t > ghassanpl::parsing::try_eat_unsigned ( std::string_view & str,
int base = 10 )
inline

Definition at line 407 of file parsing.h.

◆ try_eat_unsigned() [2/2]

bool ghassanpl::parsing::try_eat_unsigned ( std::string_view & str,
uint64_t & result,
int base = 10 )
inline

Definition at line 398 of file parsing.h.

◆ try_eat_utf8_codepoint()

char32_t ghassanpl::parsing::try_eat_utf8_codepoint ( std::string_view & str)
inline

Definition at line 470 of file parsing.h.