|
hoshi-lang dev
Yet another programming language
|
lexical analysis More...


Public Types | |
| using | token_type = typename lexer_base< BasicJsonType >::token_type |
Public Types inherited from lexer_base< BasicJsonType > | |
| enum class | token_type { uninitialized , literal_true , literal_false , literal_null , value_string , value_unsigned , value_integer , value_float , begin_array , begin_object , end_array , end_object , name_separator , value_separator , parse_error , end_of_input , literal_or_value } |
| token types for the parser More... | |
Public Member Functions | |
| lexer (InputAdapterType &&adapter, bool ignore_comments_=false) noexcept | |
| lexer (const lexer &)=delete | |
| lexer (lexer &&)=default | |
| lexer & | operator= (lexer &)=delete |
| lexer & | operator= (lexer &&)=default |
| ~lexer ()=default | |
| constexpr number_integer_t | get_number_integer () const noexcept |
| return integer value | |
| constexpr number_unsigned_t | get_number_unsigned () const noexcept |
| return unsigned integer value | |
| constexpr number_float_t | get_number_float () const noexcept |
| return floating-point value | |
| string_t & | get_string () |
| return current string value (implicitly resets the token; useful only once) | |
| constexpr position_t | get_position () const noexcept |
| return position of last read token | |
| std::string | get_token_string () const |
| JSON_HEDLEY_RETURNS_NON_NULL constexpr const char * | get_error_message () const noexcept |
| return syntax error message | |
| bool | skip_bom () |
| skip the UTF-8 byte order mark | |
| void | skip_whitespace () |
| token_type | scan () |
Private Types | |
| using | number_integer_t = typename BasicJsonType::number_integer_t |
| using | number_unsigned_t = typename BasicJsonType::number_unsigned_t |
| using | number_float_t = typename BasicJsonType::number_float_t |
| using | string_t = typename BasicJsonType::string_t |
| using | char_type = typename InputAdapterType::char_type |
| using | char_int_type = typename char_traits< char_type >::int_type |
Private Member Functions | |
| int | get_codepoint () |
get codepoint from 4 hex characters following \u | |
| bool | next_byte_in_range (std::initializer_list< char_int_type > ranges) |
| check if the next byte(s) are inside a given range | |
| token_type | scan_string () |
| scan a string literal | |
| bool | scan_comment () |
| scan a comment | |
| token_type | scan_number () |
| scan a number literal | |
| token_type | scan_literal (const char_type *literal_text, const std::size_t length, token_type return_type) |
| void | reset () noexcept |
| reset token_buffer; current character is beginning of token | |
| char_int_type | get () |
| void | unget () |
| unget current character (read it again on next get) | |
| void | add (char_int_type c) |
| add a character to token_buffer | |
Static Private Member Functions | |
| static JSON_HEDLEY_PURE char | get_decimal_point () noexcept |
| return the locale-dependent decimal point | |
| static void | strtof (float &f, const char *str, char **endptr) noexcept |
| static void | strtof (double &f, const char *str, char **endptr) noexcept |
| static void | strtof (long double &f, const char *str, char **endptr) noexcept |
Private Attributes | |
| InputAdapterType | ia |
| input adapter | |
| const bool | ignore_comments = false |
| whether comments should be ignored (true) or signaled as errors (false) | |
| char_int_type | current = char_traits<char_type>::eof() |
| the current character | |
| bool | next_unget = false |
| whether the next get() call should just return current | |
| position_t | position {} |
| the start position of the current token | |
| std::vector< char_type > | token_string {} |
| raw input token string (for error messages) | |
| string_t | token_buffer {} |
| buffer for variable-length tokens (numbers, strings) | |
| const char * | error_message = "" |
| a description of occurred lexer errors | |
| number_integer_t | value_integer = 0 |
| number_unsigned_t | value_unsigned = 0 |
| number_float_t | value_float = 0 |
| const char_int_type | decimal_point_char = '.' |
| the decimal point | |
| std::size_t | decimal_point_position = std::string::npos |
| the position of the decimal point in the input | |
Additional Inherited Members | |
Static Public Member Functions inherited from lexer_base< BasicJsonType > | |
| JSON_HEDLEY_RETURNS_NON_NULL static JSON_HEDLEY_CONST const char * | token_type_name (const token_type t) noexcept |
| return name of values of type token_type (only used for errors) | |
lexical analysis
This class organizes the lexical analysis during JSON deserialization.
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
| using token_type = typename lexer_base<BasicJsonType>::token_type |
|
inlineexplicitnoexcept |
|
delete |
|
default |
|
default |
|
inlineprivate |
add a character to token_buffer
Definition at line 8427 of file json.hpp.
References lexer< BasicJsonType, InputAdapterType >::token_buffer.
Referenced by lexer< BasicJsonType, InputAdapterType >::next_byte_in_range(), lexer< BasicJsonType, InputAdapterType >::scan_number(), and lexer< BasicJsonType, InputAdapterType >::scan_string().

|
inlineprivate |
Definition at line 8363 of file json.hpp.
References position_t::chars_read_current_line, position_t::chars_read_total, lexer< BasicJsonType, InputAdapterType >::current, lexer< BasicJsonType, InputAdapterType >::ia, JSON_HEDLEY_LIKELY, position_t::lines_read, lexer< BasicJsonType, InputAdapterType >::next_unget, lexer< BasicJsonType, InputAdapterType >::position, and lexer< BasicJsonType, InputAdapterType >::token_string.
Referenced by lexer< BasicJsonType, InputAdapterType >::get_codepoint(), lexer< BasicJsonType, InputAdapterType >::next_byte_in_range(), lexer< BasicJsonType, InputAdapterType >::scan_comment(), lexer< BasicJsonType, InputAdapterType >::scan_literal(), lexer< BasicJsonType, InputAdapterType >::scan_number(), lexer< BasicJsonType, InputAdapterType >::scan_string(), lexer< BasicJsonType, InputAdapterType >::skip_bom(), and lexer< BasicJsonType, InputAdapterType >::skip_whitespace().

|
inlineprivate |
get codepoint from 4 hex characters following \u
For input "\u c1 c2 c3 c4" the codepoint is: (c1 * 0x1000) + (c2 * 0x0100) + (c3 * 0x0010) + c4 = (c1 << 12) + (c2 << 8) + (c3 << 4) + (c4 << 0)
Furthermore, the possible characters '0'..'9', 'A'..'F', and 'a'..'f' must be converted to the integers 0x0..0x9, 0xA..0xF, 0xA..0xF, resp. The conversion is done by subtracting the offset (0x30, 0x37, and 0x57) between the ASCII value of the character and the desired integer value.
Definition at line 7192 of file json.hpp.
References lexer< BasicJsonType, InputAdapterType >::current, lexer< BasicJsonType, InputAdapterType >::get(), and JSON_ASSERT.
Referenced by lexer< BasicJsonType, InputAdapterType >::scan_string().


|
inlinestaticprivatenoexcept |
return the locale-dependent decimal point
Definition at line 7166 of file json.hpp.
References JSON_ASSERT.
|
inlineconstexprnoexcept |
return syntax error message
Definition at line 8504 of file json.hpp.
References lexer< BasicJsonType, InputAdapterType >::error_message.
|
inlineconstexprnoexcept |
return floating-point value
Definition at line 8450 of file json.hpp.
References lexer< BasicJsonType, InputAdapterType >::value_float.
|
inlineconstexprnoexcept |
return integer value
Definition at line 8438 of file json.hpp.
References lexer< BasicJsonType, InputAdapterType >::value_integer.
|
inlineconstexprnoexcept |
return unsigned integer value
Definition at line 8444 of file json.hpp.
References lexer< BasicJsonType, InputAdapterType >::value_unsigned.
|
inlineconstexprnoexcept |
return position of last read token
Definition at line 8471 of file json.hpp.
References lexer< BasicJsonType, InputAdapterType >::position.
|
inline |
return current string value (implicitly resets the token; useful only once)
Definition at line 8456 of file json.hpp.
References lexer< BasicJsonType, InputAdapterType >::decimal_point_char, lexer< BasicJsonType, InputAdapterType >::decimal_point_position, and lexer< BasicJsonType, InputAdapterType >::token_buffer.
|
inline |
return the last read token (for errors only). Will never contain EOF (an arbitrary value that is not a valid char value, often -1), because 255 may legitimately occur. May contain NUL, which should be escaped.
Definition at line 8479 of file json.hpp.
References lexer< BasicJsonType, InputAdapterType >::token_string.
|
inlineprivate |
check if the next byte(s) are inside a given range
Adds the current byte and, for each passed range, reads a new byte and checks if it is inside the range. If a violation was detected, set up an error message and return false. Otherwise, return true.
| [in] | ranges | list of integers; interpreted as list of pairs of inclusive lower and upper bound, respectively |
Definition at line 7240 of file json.hpp.
References lexer< BasicJsonType, InputAdapterType >::add(), lexer< BasicJsonType, InputAdapterType >::current, lexer< BasicJsonType, InputAdapterType >::error_message, lexer< BasicJsonType, InputAdapterType >::get(), JSON_ASSERT, and JSON_HEDLEY_LIKELY.
Referenced by lexer< BasicJsonType, InputAdapterType >::scan_string().


|
default |
|
delete |
|
inlineprivatenoexcept |
reset token_buffer; current character is beginning of token
Definition at line 8345 of file json.hpp.
References lexer< BasicJsonType, InputAdapterType >::current, lexer< BasicJsonType, InputAdapterType >::decimal_point_position, lexer< BasicJsonType, InputAdapterType >::token_buffer, and lexer< BasicJsonType, InputAdapterType >::token_string.
Referenced by lexer< BasicJsonType, InputAdapterType >::scan_number(), and lexer< BasicJsonType, InputAdapterType >::scan_string().

|
inline |
Definition at line 8540 of file json.hpp.
References position_t::chars_read_total, lexer< BasicJsonType, InputAdapterType >::current, lexer< BasicJsonType, InputAdapterType >::error_message, lexer< BasicJsonType, InputAdapterType >::ignore_comments, lexer< BasicJsonType, InputAdapterType >::position, lexer< BasicJsonType, InputAdapterType >::scan_comment(), lexer< BasicJsonType, InputAdapterType >::scan_literal(), lexer< BasicJsonType, InputAdapterType >::scan_number(), lexer< BasicJsonType, InputAdapterType >::scan_string(), lexer< BasicJsonType, InputAdapterType >::skip_bom(), and lexer< BasicJsonType, InputAdapterType >::skip_whitespace().

|
inlineprivate |
scan a comment
Definition at line 7867 of file json.hpp.
References lexer< BasicJsonType, InputAdapterType >::error_message, lexer< BasicJsonType, InputAdapterType >::get(), and lexer< BasicJsonType, InputAdapterType >::unget().
Referenced by lexer< BasicJsonType, InputAdapterType >::scan().


|
inlineprivate |
| [in] | literal_text | the literal text to expect |
| [in] | length | the length of the passed literal text |
| [in] | return_type | the token type to return on success |
Definition at line 8325 of file json.hpp.
References lexer< BasicJsonType, InputAdapterType >::current, lexer< BasicJsonType, InputAdapterType >::error_message, lexer< BasicJsonType, InputAdapterType >::get(), JSON_ASSERT, and JSON_HEDLEY_UNLIKELY.
Referenced by lexer< BasicJsonType, InputAdapterType >::scan().


|
inlineprivate |
scan a number literal
This function scans a string according to Sect. 6 of RFC 8259.
The function is realized with a deterministic finite state machine derived from the grammar described in RFC 8259. Starting in state "init", the input is read and used to determined the next state. Only state "done" accepts the number. State "error" is a trap state to model errors. In the table below, "anything" means any character but the ones listed before.
| state | 0 | 1-9 | e E | + | - | . | anything |
|---|---|---|---|---|---|---|---|
| init | zero | any1 | [error] | [error] | minus | [error] | [error] |
| minus | zero | any1 | [error] | [error] | [error] | [error] | [error] |
| zero | done | done | exponent | done | done | decimal1 | done |
| any1 | any1 | any1 | exponent | done | done | decimal1 | done |
| decimal1 | decimal2 | decimal2 | [error] | [error] | [error] | [error] | [error] |
| decimal2 | decimal2 | decimal2 | exponent | done | done | done | done |
| exponent | any2 | any2 | [error] | sign | sign | [error] | [error] |
| sign | any2 | any2 | [error] | [error] | [error] | [error] | [error] |
| any2 | any2 | any2 | done | done | done | done | done |
The state machine is realized with one label per state (prefixed with "scan_number_") and goto statements between them. The state machine contains cycles, but any cycle can be left when EOF is read. Therefore, the function is guaranteed to terminate.
During scanning, the read bytes are stored in token_buffer. This string is then converted to a signed integer, an unsigned integer, or a floating-point number.
. to work with the locale-dependent converters. Definition at line 7992 of file json.hpp.
References lexer< BasicJsonType, InputAdapterType >::add(), lexer< BasicJsonType, InputAdapterType >::current, lexer< BasicJsonType, InputAdapterType >::decimal_point_char, lexer< BasicJsonType, InputAdapterType >::decimal_point_position, lexer< BasicJsonType, InputAdapterType >::error_message, lexer< BasicJsonType, InputAdapterType >::get(), JSON_ASSERT, lexer< BasicJsonType, InputAdapterType >::reset(), lexer< BasicJsonType, InputAdapterType >::strtof(), lexer< BasicJsonType, InputAdapterType >::token_buffer, lexer< BasicJsonType, InputAdapterType >::unget(), lexer< BasicJsonType, InputAdapterType >::value_float, lexer< BasicJsonType, InputAdapterType >::value_integer, and lexer< BasicJsonType, InputAdapterType >::value_unsigned.
Referenced by lexer< BasicJsonType, InputAdapterType >::scan().


|
inlineprivate |
scan a string literal
This function scans a string according to Sect. 7 of RFC 8259. While scanning, bytes are escaped and copied into buffer token_buffer. Then the function returns successfully, token_buffer is not null-terminated (as it may contain \0 bytes), and token_buffer.size() is the number of bytes in the string.
Definition at line 7277 of file json.hpp.
References lexer< BasicJsonType, InputAdapterType >::add(), lexer< BasicJsonType, InputAdapterType >::current, lexer< BasicJsonType, InputAdapterType >::error_message, lexer< BasicJsonType, InputAdapterType >::get(), lexer< BasicJsonType, InputAdapterType >::get_codepoint(), JSON_ASSERT, JSON_HEDLEY_LIKELY, JSON_HEDLEY_UNLIKELY, lexer< BasicJsonType, InputAdapterType >::next_byte_in_range(), and lexer< BasicJsonType, InputAdapterType >::reset().
Referenced by lexer< BasicJsonType, InputAdapterType >::scan().


|
inline |
skip the UTF-8 byte order mark
Definition at line 8517 of file json.hpp.
References lexer< BasicJsonType, InputAdapterType >::get(), and lexer< BasicJsonType, InputAdapterType >::unget().
Referenced by lexer< BasicJsonType, InputAdapterType >::scan().


|
inline |
Definition at line 8531 of file json.hpp.
References lexer< BasicJsonType, InputAdapterType >::current, and lexer< BasicJsonType, InputAdapterType >::get().
Referenced by lexer< BasicJsonType, InputAdapterType >::scan().


|
inlinestaticprivatenoexcept |
|
inlinestaticprivatenoexcept |
Definition at line 7935 of file json.hpp.
Referenced by lexer< BasicJsonType, InputAdapterType >::scan_number().

|
inlinestaticprivatenoexcept |
|
inlineprivate |
unget current character (read it again on next get)
We implement unget by setting variable next_unget to true. The input is not changed - we just simulate ungetting by modifying chars_read_total, chars_read_current_line, and token_string. The next call to get() will behave as if the unget character is read again.
Definition at line 8400 of file json.hpp.
References position_t::chars_read_current_line, position_t::chars_read_total, lexer< BasicJsonType, InputAdapterType >::current, JSON_ASSERT, JSON_HEDLEY_LIKELY, position_t::lines_read, lexer< BasicJsonType, InputAdapterType >::next_unget, lexer< BasicJsonType, InputAdapterType >::position, and lexer< BasicJsonType, InputAdapterType >::token_string.
Referenced by lexer< BasicJsonType, InputAdapterType >::scan_comment(), lexer< BasicJsonType, InputAdapterType >::scan_number(), and lexer< BasicJsonType, InputAdapterType >::skip_bom().

|
private |
the current character
Definition at line 8636 of file json.hpp.
Referenced by lexer< BasicJsonType, InputAdapterType >::get(), lexer< BasicJsonType, InputAdapterType >::get_codepoint(), lexer< BasicJsonType, InputAdapterType >::next_byte_in_range(), lexer< BasicJsonType, InputAdapterType >::reset(), lexer< BasicJsonType, InputAdapterType >::scan(), lexer< BasicJsonType, InputAdapterType >::scan_literal(), lexer< BasicJsonType, InputAdapterType >::scan_number(), lexer< BasicJsonType, InputAdapterType >::scan_string(), lexer< BasicJsonType, InputAdapterType >::skip_whitespace(), and lexer< BasicJsonType, InputAdapterType >::unget().
|
private |
the decimal point
Definition at line 8659 of file json.hpp.
Referenced by lexer< BasicJsonType, InputAdapterType >::get_string(), and lexer< BasicJsonType, InputAdapterType >::scan_number().
|
private |
the position of the decimal point in the input
Definition at line 8661 of file json.hpp.
Referenced by lexer< BasicJsonType, InputAdapterType >::get_string(), lexer< BasicJsonType, InputAdapterType >::reset(), and lexer< BasicJsonType, InputAdapterType >::scan_number().
|
private |
a description of occurred lexer errors
Definition at line 8651 of file json.hpp.
Referenced by lexer< BasicJsonType, InputAdapterType >::get_error_message(), lexer< BasicJsonType, InputAdapterType >::next_byte_in_range(), lexer< BasicJsonType, InputAdapterType >::scan(), lexer< BasicJsonType, InputAdapterType >::scan_comment(), lexer< BasicJsonType, InputAdapterType >::scan_literal(), lexer< BasicJsonType, InputAdapterType >::scan_number(), and lexer< BasicJsonType, InputAdapterType >::scan_string().
|
private |
input adapter
Definition at line 8630 of file json.hpp.
Referenced by lexer< BasicJsonType, InputAdapterType >::get().
|
private |
whether comments should be ignored (true) or signaled as errors (false)
Definition at line 8633 of file json.hpp.
Referenced by lexer< BasicJsonType, InputAdapterType >::scan().
|
private |
whether the next get() call should just return current
Definition at line 8639 of file json.hpp.
Referenced by lexer< BasicJsonType, InputAdapterType >::get(), and lexer< BasicJsonType, InputAdapterType >::unget().
|
private |
the start position of the current token
Definition at line 8642 of file json.hpp.
Referenced by lexer< BasicJsonType, InputAdapterType >::get(), lexer< BasicJsonType, InputAdapterType >::get_position(), lexer< BasicJsonType, InputAdapterType >::scan(), and lexer< BasicJsonType, InputAdapterType >::unget().
|
private |
buffer for variable-length tokens (numbers, strings)
Definition at line 8648 of file json.hpp.
Referenced by lexer< BasicJsonType, InputAdapterType >::add(), lexer< BasicJsonType, InputAdapterType >::get_string(), lexer< BasicJsonType, InputAdapterType >::reset(), and lexer< BasicJsonType, InputAdapterType >::scan_number().
|
private |
raw input token string (for error messages)
Definition at line 8645 of file json.hpp.
Referenced by lexer< BasicJsonType, InputAdapterType >::get(), lexer< BasicJsonType, InputAdapterType >::get_token_string(), lexer< BasicJsonType, InputAdapterType >::reset(), and lexer< BasicJsonType, InputAdapterType >::unget().
|
private |
Definition at line 8656 of file json.hpp.
Referenced by lexer< BasicJsonType, InputAdapterType >::get_number_float(), and lexer< BasicJsonType, InputAdapterType >::scan_number().
|
private |
Definition at line 8654 of file json.hpp.
Referenced by lexer< BasicJsonType, InputAdapterType >::get_number_integer(), and lexer< BasicJsonType, InputAdapterType >::scan_number().
|
private |
Definition at line 8655 of file json.hpp.
Referenced by lexer< BasicJsonType, InputAdapterType >::get_number_unsigned(), and lexer< BasicJsonType, InputAdapterType >::scan_number().