4.6.0
Freundlich's C++ toolkit
|
Library for parsers.
Link to fcppt_parse_interface
.
A parser gets a string as input and if it belongs to a certain language it transforms this string into a specific output. Parsers are built up from basic parsers and combinating parsers. The most basic parsers are:
From this, more complicated parsers can be built. The combinating parsers are:
Let us consider the following example. We want to parse numbers of the form {0, ..., 9}*
, where we allow multiple leading zeroes for simplicity. Each word that belongs to this language should produce a word over the alphabet {0,...,9}
. To encode this in C++, we start with an enum of the form
Next, we create a function that given char c
and digit d
, it produces a parser that returns d
if and only if it gets c
as input:
We can use this function to create a parser that recognizes any digit:
Here, the operator|
constructs a fcppt::parse::alternative out of its two arguments. The result type of p09
is still digit
because variant<digit, ..., digit>
is simplified to digit
. To recognize an arbitrary string of digits, we define the following parser:
The operator*
creates an fcppt::parse::repetition out of its argument. Its result type is vector<digit>
. The fcppt::parse::parse_string function takes a parser P
and a string and returns an fcppt::either::object of fcppt::parse::parse_string_error and P
's result type. We can then inspect this either by using fcppt::either::match.
A lot of languages are not whitespace-sensitive, which means that whitespaces can be added in a lot of places. For example, in JSON, we can write [1,2]
or [1, 2]
, which both result in an array with elements 1 and 2. Here, we use the notaiton of a Skipper to achieve this behaviour. A skipper is simpler than a parser in the sense that it never produces results, it only consumes input. Skippers are run in very specific places:
fcppt::parse::sequence{l,r}
runs the skipper after running l
and before running r
. fcppt::parse::repetition{p}
runs the skipper after each successful parse of p
. Whitespace skipping can be disabled using fcppt::parse::lexeme. Consider the following example: We declare a parser that parses two unsigned ints in succession.
Without whitespace skipping, this parser can never succeed, since the first uint parser will always consume as many digits as it can, leaving nothing for the second uint parser. In the following example, the first uint parser reads 10
, stops at the space character, and then the second parser fails.
However, if we use whitespace skipping, then the first uint parser reads 10
, stops at the space character, then the sequence (>>
) parser runs the skipper, which consumes the space character, and then the second uint parser reads 20
. The skipper we use here consumes as many space characters as possible.
Note that the implementation of fcppt::parse::parse runs fcppt::parse::phrase_parse internally, using the skipper fcppt::parse::skipper::epsilon, which does nothing.
Consider a language where we want to parse lists of the form {E_1, ..., E_n}
, where E_1, ..., E_n
are of the form name = L
(where we allow names to consist of the characters a, b and c) and L
is again a list. First, we need to create a C++ data type that represents such a list, which we declare as follows:
We would like to create a parser for lists and a parser for entries. The parser for lists needs the parser for entries in its definition and vice versa. To deal with (mutually) recursive definitions, we do the following:
In our example, we declare the class as follows:
First, fcppt::parse::grammar is a helper class that the actual grammars derive from. It selects the result type produced by the start rule, the character type of the input and the skipper type that is being used. Here, we use fcppt::parse::skipper::epsilon, which implies that no skipping is done. To actually implement the grammar, we have to define its constructor. Here, we have to pass a reference to one of our parsers to the base class, which defines the start nonterminal of the grammar. We also have to pass an implementation of the skipper type, which in this case simply is the epsilon skipper.
To use the grammar for parsing, we call fcppt::parse::grammar_parse_string. This internally uses fcppt::parse::phrase_parse_string, passing the start symbol and the skipper of the grammar.
The parser implementation makes use of so-called backtracking, which means that certain parsers are expected to fail, and if they do, the input stream is set to a previous position and another parser is tried instead. For example, the alternative parser a | b
first tries a
and then b
. Suppose that a
fails on the current input. Internally, the parser a | b
first saves the current position p
, runs a
and after it fails, resets the position to p
and runs b
. However, depending on why a
fails, we know that b
cannot succeed. Consider the following example:
The parser a
fails if the input is {]
, but still the parser b
is tried even though it cannot succeed. This is fine in most cases, but in some this may lead to excessive backtracking. In addition, the parser a | b
will produce an error message of the form
{ Line 1:3: Expected }, got ] OR Line 1:2: Expected [, got { }.
Since we know that b
cannot succeed if a
fails after reading {
, we can make the part after {
fatal.
Then the parser a | b
does not try b
on input {]
. Instead, it aborts immediately, and produces an error message of the form
Line 1:3: Expected }, got ].
a
fatal, then the parser b
would never be tried! Using too many fatal errors can make parsers really hard to understand.The following tables gives a quick overview over all available parsers. In the type column, we write that a parser P has type (T_1,...,T_n) -> U
, which means that if P is constructed from parsers that produce T_1, ..., T_n
, then P produces U.
Parser | Type | Description |
---|---|---|
fcppt::parse::alternative | (L,R) -> fcppt::parse::alternative_result<L,R> | Returns the result of the first matching parser. |
fcppt::parse::as_struct | fcppt::tuple::object<T_1,...,T_n> -> struct { T_1 t_1; ... ; T_n t_n } | Converts a tuple to a struct. |
fcppt::parse::basic_char, fcppt::parse::char_ | Char | Parses any character. |
fcppt::parse::basic_char_set, fcppt::parse::char_set | Char | Parses a set of characters. |
fcppt::parse::basic_literal, fcppt::parse::literal | fcppt::unit | Parses a specific character. |
fcppt::parse::basic_string, fcppt::parse::string | fcppt::unit | Parses a specific string. |
fcppt::parse::complement | fcppt::unit -> fcppt::unit | Complements a fcppt::parse::basic_char_set parser. |
fcppt::parse::construct | T -> U | Constructs |
fcppt::parse::convert | T -> U | Converts |
fcppt::parse::convert_const | fcppt::unit -> U | Converts to |
fcppt::parse::convert_if | T -> U | Converts |
fcppt::parse::epsilon | fcppt::unit | Parses the empty string. |
fcppt::parse::exclude | (T,fcppt::unit) -> T | Matches only if another parser fails. |
fcppt::parse::fail | fcppt::unit | Always fails. |
fcppt::parse::float_ | Float | Parses floats. |
fcppt::parse::ignore | T -> fcppt::unit | Ignores a result. |
fcppt::parse::int_ | Signed Int | Parses signed integers. |
fcppt::parse::lexeme | T -> T | Disables skipping. |
fcppt::parse::list | (fcppt::unit, T, fcppt::unit, fcppt::unit) -> std::vector<T> | Parses a list of delimited values with opening and closing parentheses. |
fcppt::parse::named | T -> T | Gives a parser a name, improving error messages. |
fcppt::parse::not_ | fcppt::unit -> fcppt::unit | Reverses a parser. |
fcppt::parse::optional | T -> fcppt::optional::object<T> | Creates a parser that may fail. |
fcppt::parse::recursive | T -> fcppt::recursive<T> | Wraps a type in fcppt::recursive. |
fcppt::parse::repetition | T -> fcppt::parse::repetition_result<T> | Repeats a parser as long as possible. |
fcppt::parse::repetition_plus | T -> fcppt::parse::repetition_result<T> | Repeats a parser as long as possible. Only accepts nonempty results. |
fcppt::parse::separator | (T,fcppt::unit) -> std::vector<T> | Parses a list of delimited values. |
fcppt::parse::sequence | (L,R) -> fcppt::parse::sequence_result<L,R> | Uses two parsers in succession. |
fcppt::parse::uint | Unsigned Int | Parses unsigned integers. |
The following table shows a list of available skippers.
Skipper | Description |
---|---|
fcppt::parse::skipper::basic_char_set, fcppt::parse::skipper::char_set | Skips a set of characters. |
fcppt::parse::skipper::basic_literal, fcppt::parse::skipper::literal | Skips a specific character. |
fcppt::parse::skipper::epsilon | Skips nothing. |
fcppt::parse::skipper::repetition | Repeats a skipper as long as possible. |
fcppt::parse::skipper::sequence | Runs two skippers in succession. |
A parser is an object with the following properties:
result_type
. A skipper is an object with the following properties:
Classes | |
class | fcppt::parse::alternative< Left, Right > |
Tries the left parser, and if that fails also the right parser. Uses the result of the first one that does not fail. More... | |
class | fcppt::parse::base< Result, Ch, Skipper > |
Base class for a parser. More... | |
class | fcppt::parse::basic_char< Ch > |
Parses a single character and returns it. More... | |
class | fcppt::parse::basic_char_set< Ch > |
Parses a set of characters and returns which one has matched. More... | |
class | fcppt::parse::basic_literal< Ch > |
Parses a specific character and returns nothing. More... | |
class | fcppt::parse::basic_stream< Ch > |
The stream class used while parsing. More... | |
class | fcppt::parse::basic_string< Ch > |
Parses a specific string and returns nothing. More... | |
class | fcppt::parse::complement< Parser > |
Builds the complement of a fcppt::parse::basic_char_set parser. More... | |
class | fcppt::parse::convert_const< Parser, Result > |
Replaces the success value of a parser with a constant. More... | |
class | fcppt::parse::convert< Parser, Result > |
Converts the success value of a parser into another success value. More... | |
class | fcppt::parse::convert_if< Ch, Parser, Result > |
Converts the success value of a parser into either another success value or an error. More... | |
class | fcppt::parse::epsilon |
Parses the empty string, returns nothing. More... | |
class | fcppt::parse::error< Ch > |
The error type. More... | |
class | fcppt::parse::exclude< Parser, Exclude > |
Matches only if another parser does not match. More... | |
class | fcppt::parse::fail< Result > |
Always fails. More... | |
class | fcppt::parse::fatal< Parser > |
Makes all error fatal. More... | |
class | fcppt::parse::float_< Type > |
Parses floating-point numbers. More... | |
class | fcppt::parse::grammar< Result, Ch, Skipper > |
Base class for grammars. More... | |
class | fcppt::parse::ignore< Parser > |
Ignores the result of a parser. More... | |
class | fcppt::parse::int_< Type > |
Parses signed integers. More... | |
class | fcppt::parse::lexeme< Parser > |
Disables the skipper. More... | |
class | fcppt::parse::list< Start, Inner, Sep, End > |
Parses lists, e.g., [a_1,...,a_n]. More... | |
class | fcppt::parse::named< Ch, Parser > |
Gives a parser a name, improving error messages. More... | |
class | fcppt::parse::not_< Parser > |
Negates the result of a parser, returns nothing. More... | |
class | fcppt::parse::optional< Parser > |
Makes a parser optional. More... | |
class | fcppt::parse::position< Ch > |
The position of an fcppt::parse::basic_stream. More... | |
class | fcppt::parse::recursive< Parser > |
Wraps a type in fcppt::recursive. More... | |
class | fcppt::parse::repetition< Parser > |
Tries a parser repeatedly, producing a vector of results. More... | |
class | fcppt::parse::repetition_plus< Parser > |
Tries a parser repeatedly, producing a vector of results. Must produce at least one element. More... | |
class | fcppt::parse::separator< Inner, Ch > |
Parses a list of elements, delimited by a separator, e.g., a_1,...,a_n. More... | |
class | fcppt::parse::sequence< Left, Right > |
Tries two parsers in succession. Produces a tuple of both results if both parsers do not fail. More... | |
class | fcppt::parse::skipper::alternative< Left, Right > |
Tries the left skipper, and if that fails also the right skipper. More... | |
class | fcppt::parse::skipper::any |
Skips a single character. More... | |
class | fcppt::parse::skipper::basic_char_set< Ch > |
Skips all characters from a set. More... | |
class | fcppt::parse::skipper::basic_literal< Ch > |
Skips a specific character. More... | |
class | fcppt::parse::skipper::epsilon |
Skips nothing. More... | |
class | fcppt::parse::skipper::not_< Skipper > |
Negates the result of a skipper. More... | |
class | fcppt::parse::skipper::repetition< Parser > |
Skips characters repeatedly. More... | |
class | fcppt::parse::skipper::sequence< Left, Right > |
Tries two skippers in succession. More... | |
struct | fcppt::parse::skipper::tag |
The tag skippers derive from. More... | |
struct | fcppt::parse::tag |
The tag parsers derive from. More... | |
class | fcppt::parse::uint< Type > |
Parses unsigned integers. More... | |
Typedefs | |
template<typename Left , typename Right > | |
using | fcppt::parse::alternative_result |
The result type of an alternative parser. | |
template<typename Result , typename Ch , typename Skipper > | |
using | fcppt::parse::base_unique_ptr = fcppt::unique_ptr<fcppt::parse::base<Result, Ch, Skipper>> |
unique_ptr to fcppt::parse::base | |
template<typename Ch > | |
using | fcppt::parse::basic_char_set_container = std::unordered_set<Ch> |
The character set type. | |
using | fcppt::parse::char_ = fcppt::parse::basic_char<char> |
using | fcppt::parse::char_set = fcppt::parse::basic_char_set<char> |
using | fcppt::parse::column = fcppt::strong_typedef< std::uint64_t ,_> |
The column type of a position. | |
template<typename Parser > | |
using | fcppt::parse::deref_type = std::remove_cvref_t<fcppt::deref_type<Parser const>> |
The dereferenced type of a parser. | |
template<typename Type > | |
using | fcppt::parse::is_char = std::disjunction<std::is_same<Type, char>, std::is_same<Type, wchar_t>> |
The allowed char types, currently char and wchar_t. | |
template<typename Type > | |
using | fcppt::parse::is_parser = std::is_base_of<fcppt::parse::tag, Type> |
Checks if a type is a parser.A type is a parser if and only if it derives from fcppt::parse::tag. | |
template<typename Type > | |
using | fcppt::parse::is_valid_argument |
Checks if a parameter is a valid parser type. | |
using | fcppt::parse::line = fcppt::strong_typedef< std::uint64_t ,_> |
The line type of a position. | |
using | fcppt::parse::literal = fcppt::parse::basic_literal<char> |
template<typename Type > | |
using | fcppt::parse::repetition_result |
The result type of a repetition parser. | |
template<typename Ch , typename Type > | |
using | fcppt::parse::result = fcppt::either::object<fcppt::parse::error<Ch>, Type> |
The result type when parsing streams of Ch, producing values of Type.The result is an fcppt::either::object of fcppt::parse::error<Ch> and Type . | |
template<typename Parser > | |
using | fcppt::parse::result_of = typename fcppt::parse::deref_type<Parser>::result_type |
The result type of a parser. | |
template<typename Left , typename Right > | |
using | fcppt::parse::sequence_result |
The result of a sequence parser. | |
using | fcppt::parse::skipper::char_set = fcppt::parse::skipper::basic_char_set<char> |
template<typename Type > | |
using | fcppt::parse::skipper::is_skipper = std::is_base_of<fcppt::parse::skipper::tag, Type> |
Checks if a type is a skipper.A type is a skipper if and only if it derives from fcppt::parse::skipper::tag. | |
template<typename Type > | |
using | fcppt::parse::skipper::is_valid_argument |
Checks if a parameter is a valid skipper type. | |
using | fcppt::parse::skipper::literal = fcppt::parse::skipper::basic_literal<char> |
template<typename Ch > | |
using | fcppt::parse::skipper::result = fcppt::either::error<fcppt::parse::error<Ch>> |
The result type when skipping streams of Ch.The result is an fcppt::either::error of fcppt::parse::error<Ch> . | |
using | fcppt::parse::skipper::string = fcppt::parse::skipper::basic_string<char> |
using | fcppt::parse::string = fcppt::parse::basic_string<char> |
Functions | |
template<typename Result , typename Parser > requires (fcppt::tuple::is_object<fcppt::parse::result_of<Parser>>::value) | |
fcppt::parse::convert< std::remove_cvref_t< Parser >, Result > | fcppt::parse::as_struct (Parser &&_parser) |
Creates a parser that converts a tuple into a struct. | |
fcppt::parse::char_set | fcppt::parse::blank () |
Parser that parses blank characters. | |
template<typename Ch > | |
fcppt::parse::basic_char_set_container< Ch > | fcppt::parse::blank_set () |
The set of blank characters. | |
template<typename Result , typename Parser > | |
fcppt::parse::convert< std::remove_cvref_t< Parser >, Result > | fcppt::parse::construct (Parser &&_parser) |
Applies a constructor to the result of a parser. | |
template<typename Parser > | |
fcppt::parse::deref_type< Parser > const & | fcppt::parse::deref (Parser const &_parser) |
Dereferences a parser. | |
template<typename Ch > | |
fcppt::parse::basic_char_set< Ch > | fcppt::parse::digits () |
The char set of digits. | |
template<typename Ch > | |
fcppt::optional::object< Ch > | fcppt::parse::get_char (fcppt::reference< fcppt::parse::basic_stream< Ch > > const _state) |
The next character in a stream. | |
template<typename Ch > | |
fcppt::parse::position< Ch > | fcppt::parse::get_position (fcppt::reference< fcppt::parse::basic_stream< Ch > > const _state) |
The current position of a stream. | |
template<typename Result , typename Ch , typename Skipper > | |
fcppt::either::object< fcppt::parse::parse_stream_error< Ch >, Result > | fcppt::parse::grammar_parse_stream (std::basic_istream< Ch > &_stream, fcppt::parse::grammar< Result, Ch, Skipper > const &_grammar) |
Parse a stream using a grammar. | |
template<typename Result , typename Ch , typename Skipper > | |
fcppt::either::object< fcppt::parse::parse_string_error< Ch >, Result > | fcppt::parse::grammar_parse_string (std::basic_string< Ch > &&_string, fcppt::parse::grammar< Result, Ch, Skipper > const &_grammar) |
Parse a string using a grammar. | |
template<typename Ch , typename Traits > | |
std::basic_ostream< Ch, Traits > & | fcppt::parse::operator<< (std::basic_ostream< Ch, Traits > &_stream, fcppt::parse::location const &_loc) |
Outputs a location to a stream. | |
template<typename Ch , typename Skipper , typename Parser > | |
fcppt::parse::base_unique_ptr< fcppt::parse::result_of< Parser >, Ch, Skipper > | fcppt::parse::make_base (Parser &&_parser) |
Hides the type of a parser, turning it into an fcppt::parse::base_unique_ptr. | |
template<typename Parser , typename Convert > | |
fcppt::parse::convert< std::remove_cvref_t< Parser >, std::invoke_result_t< Convert, fcppt::parse::result_of< Parser > && > > | fcppt::parse::make_convert (Parser &&_parser, Convert &&_convert) |
Creates an fcppt::parse::convert parser out of a lambda. | |
template<typename Ch , typename Parser , typename Convert > | |
fcppt::parse::convert_if< Ch, std::remove_cvref_t< Parser >, fcppt::either::success_type< std::invoke_result_t< Convert, fcppt::parse::position< Ch >, fcppt::parse::result_of< Parser > && > > > | fcppt::parse::make_convert_if (Parser &&_parser, Convert &&_convert) |
Creates an fcppt::parse::convert_if parser out of a lambda. | |
template<typename Parser > | |
fcppt::parse::fatal< std::remove_cvref_t< Parser > > | fcppt::parse::make_fatal (Parser &&_parser) |
Creates an fcppt::parse::fatal parser. | |
template<typename Parser > | |
fcppt::parse::ignore< std::remove_cvref_t< Parser > > | fcppt::parse::make_ignore (Parser &&_parser) |
Creates an fcppt::parse::ignore parser. | |
template<typename Parser > | |
fcppt::parse::lexeme< std::remove_cvref_t< Parser > > | fcppt::parse::make_lexeme (Parser &&_parser) |
Creates an fcppt::parse::lexeme parser. | |
template<typename Ch > | |
fcppt::parse::basic_literal< Ch > | fcppt::parse::make_literal (Ch const _ch) |
Creates an fcppt::parse::basic_literal parser. | |
template<typename Ch , typename Success > | |
fcppt::either::object< fcppt::parse::parse_stream_error< Ch >, std::remove_cvref_t< Success > > | fcppt::parse::make_parse_stream_success (Success &&_success) |
Creates a success value with a parse_stream failure type. | |
template<typename Ch , typename Success > | |
fcppt::either::object< fcppt::parse::parse_string_error< Ch >, std::remove_cvref_t< Success > > | fcppt::parse::make_parse_string_success (Success &&_success) |
Creates a success value with a parse_string failure type. | |
template<typename Parser > | |
fcppt::parse::recursive< std::remove_cvref_t< Parser > > | fcppt::parse::make_recursive (Parser &&_parser) |
Creates an fcppt::parse::recursive parser. | |
template<typename Ch , typename Success > | |
fcppt::parse::result< Ch, std::remove_cvref_t< Success > > | fcppt::parse::make_success (Success &&_success) |
Creates an fcppt::parse::result from a success value. | |
template<typename Parser > | |
fcppt::parse::with_location< std::remove_cvref_t< Parser > > | fcppt::parse::make_with_location (Parser &&_parser) |
Creates an fcppt::parse::with_location parser. | |
template<typename Left , typename Right > requires (std::conjunction_v< fcppt::parse::is_valid_argument<Left>, fcppt::parse::is_valid_argument<Right>>) | |
fcppt::parse::alternative< std::remove_cvref_t< Left >, std::remove_cvref_t< Right > > | fcppt::parse::operator| (Left &&_left, Right &&_right) |
Creates an fcppt::parse::alternative parser. | |
template<typename Parser > requires (fcppt::parse::detail::is_char_set<fcppt::parse::deref_type<Parser>>::value) | |
auto | fcppt::parse::operator~ (Parser &&_parser) |
Creates an fcppt::parse::complement parser. | |
template<typename Left , typename Right > requires (std::conjunction_v< fcppt::parse::is_valid_argument<Left>, fcppt::parse::is_valid_argument<Right>>) | |
fcppt::parse::exclude< std::remove_cvref_t< Left >, std::remove_cvref_t< Right > > | fcppt::parse::operator- (Left &&_left, Right &&_right) |
Creates an fcppt::parse::exclude parser. | |
template<typename Parser > requires (fcppt::parse::is_valid_argument<Parser>::value) | |
fcppt::parse::not_< std::remove_cvref_t< Parser > > | fcppt::parse::operator! (Parser &&_parser) |
Creates an fcppt::parse::not_ parser. | |
template<typename Parser > requires (fcppt::parse::is_valid_argument<Parser>::value) | |
fcppt::parse::optional< std::remove_cvref_t< Parser > > | fcppt::parse::operator- (Parser &&_parser) |
Creates an fcppt::parse::optional parser. | |
template<typename Parser > requires (fcppt::parse::is_valid_argument<Parser>::value) | |
fcppt::parse::repetition< std::remove_cvref_t< Parser > > | fcppt::parse::operator* (Parser &&_parser) |
Creates an fcppt::parse::repetition parser. | |
template<typename Parser > requires (fcppt::parse::is_valid_argument<Parser>::value) | |
fcppt::parse::repetition_plus< std::remove_cvref_t< Parser > > | fcppt::parse::operator+ (Parser &&_parser) |
Creates an fcppt::parse::repetition_plus parser. | |
template<typename Left , typename Right > requires (std::conjunction_v< fcppt::parse::is_valid_argument<Left>, fcppt::parse::is_valid_argument<Right>>) | |
fcppt::parse::sequence< std::remove_cvref_t< Left >, std::remove_cvref_t< Right > > | fcppt::parse::operator>> (Left &&_left, Right &&_right) |
Creates an fcppt::parse::sequence parser. | |
template<typename Ch , typename Parser > | |
fcppt::either::object< fcppt::parse::parse_stream_error< Ch >, fcppt::parse::result_of< Parser > > | fcppt::parse::parse (Parser const &_parser, fcppt::parse::basic_stream< Ch > &_input) |
Parses without whitespace skipping. | |
template<typename Ch , typename Parser > | |
fcppt::either::object< fcppt::parse::parse_stream_error< Ch >, fcppt::parse::result_of< Parser > > | fcppt::parse::parse_stream (Parser const &_parser, std::basic_istream< Ch > &_input) |
Parses a std::basic_istream without whitespace skipping. | |
template<typename Ch , typename Parser > | |
fcppt::either::object< fcppt::parse::parse_string_error< Ch >, fcppt::parse::result_of< Parser > > | fcppt::parse::parse_string (Parser const &_parser, std::basic_string< Ch > &&_string) |
Parses a std::basic_string without whitespace skipping. | |
template<typename Ch , typename Parser , typename Skipper > requires (std::conjunction_v< fcppt::parse::is_parser<Parser>, fcppt::parse::skipper::is_skipper<Skipper>>) | |
fcppt::either::object< fcppt::parse::parse_stream_error< Ch >, fcppt::parse::result_of< Parser > > | fcppt::parse::phrase_parse (Parser const &_parser, fcppt::parse::basic_stream< Ch > &_input, Skipper const &_skipper) |
The basic parse function. | |
template<typename Ch , typename Parser , typename Skipper > requires (std::conjunction_v< fcppt::parse::is_parser<Parser>, fcppt::parse::skipper::is_skipper<Skipper>>) | |
fcppt::either::object< fcppt::parse::parse_stream_error< Ch >, fcppt::parse::result_of< Parser > > | fcppt::parse::phrase_parse_stream (Parser const &_parser, std::basic_istream< Ch > &_input, Skipper const &_skipper) |
Parses a std::basic_istream , using whitespace skipping. | |
template<typename Ch , typename Parser , typename Skipper > requires (std::conjunction_v< fcppt::parse::is_parser<Parser>, fcppt::parse::skipper::is_skipper<Skipper>>) | |
fcppt::either::object< fcppt::parse::parse_string_error< Ch >, fcppt::parse::result_of< Parser > > | fcppt::parse::phrase_parse_string (Parser const &_parser, std::basic_string< Ch > &&_string, Skipper const &_skipper) |
Parses a std::basic_string , using whitespace skipping. | |
template<typename Ch > | |
bool | fcppt::parse::operator== (fcppt::parse::position< Ch > const &_left, fcppt::parse::position< Ch > const &_right) |
Compares two positions for equality. | |
template<typename Ch > | |
std::basic_ostream< Ch > & | fcppt::parse::operator<< (std::basic_ostream< Ch > &_stream, fcppt::parse::position< Ch > const &_pos) |
Outputs a position to a stream. | |
template<typename Ch > | |
void | fcppt::parse::set_position (fcppt::reference< fcppt::parse::basic_stream< Ch > > const _state, fcppt::parse::position< Ch > const _pos) |
Sets the current position of a stream. | |
template<typename Ch > | |
auto | fcppt::parse::skipper::basic_space () |
Skips all characters from fcppt::parse::space_set. | |
template<typename Ch > | |
fcppt::parse::skipper::result< Ch > | fcppt::parse::skipper::make_failure (fcppt::parse::error< Ch > &&_error) |
Creates a skipper result from an fcppt::parse::error. | |
template<typename Ch > | |
fcppt::parse::skipper::result< Ch > | fcppt::parse::skipper::make_success () |
Creates a skipper success result. | |
template<typename Left , typename Right > requires (std::conjunction_v< fcppt::parse::skipper::is_valid_argument<Left>, fcppt::parse::skipper::is_valid_argument<Right>>) | |
fcppt::parse::skipper::alternative< std::remove_cvref_t< Left >, std::remove_cvref_t< Right > > | fcppt::parse::skipper::operator| (Left &&_left, Right &&_right) |
Creates an fcppt::parse::skipper::alternative skipper. | |
template<typename Skipper > requires (fcppt::parse::skipper::is_valid_argument<Skipper>::value) | |
fcppt::parse::skipper::not_< std::remove_cvref_t< Skipper > > | fcppt::parse::skipper::operator! (Skipper &&_skipper) |
Creates an fcppt::parse::skipper::not_ skipper. | |
template<typename Parser > requires (fcppt::parse::skipper::is_valid_argument<Parser>::value) | |
fcppt::parse::skipper::repetition< std::remove_cvref_t< Parser > > | fcppt::parse::skipper::operator* (Parser &&_parser) |
Creates an fcppt::parse::skipper::repetition. | |
template<typename Left , typename Right > requires (std::conjunction_v< fcppt::parse::skipper::is_valid_argument<Left>, fcppt::parse::skipper::is_valid_argument<Right>>) | |
fcppt::parse::skipper::sequence< std::remove_cvref_t< Left >, std::remove_cvref_t< Right > > | fcppt::parse::skipper::operator>> (Left &&_left, Right &&_right) |
Creates an fcppt::parse::skipper::sequence. | |
template<typename Ch , typename Skipper > requires (fcppt::parse::skipper::is_skipper_v<Skipper>) | |
fcppt::parse::skipper::result< Ch > | fcppt::parse::skipper::run (Skipper const &_skipper, fcppt::reference< fcppt::parse::basic_stream< Ch > > const _state) |
Runs a skipper on a stream. | |
auto | fcppt::parse::skipper::space () |
The skipper basic_space<char> . | |
fcppt::parse::char_set | fcppt::parse::space () |
The char set parser using fcppt::parse::space_set<char> . | |
template<typename Ch > | |
fcppt::parse::basic_char_set_container< Ch > | fcppt::parse::space_set () |
The char set of spaces.Returns the set containing the whitespace, newline and tab characters. | |
Variables | |
template<typename Type > | |
constexpr bool | fcppt::parse::skipper::is_skipper_v = fcppt::parse::skipper::is_skipper<Type>::value |
Checks if a type is a skipper. | |
using fcppt::parse::alternative_result |
The result type of an alternative parser.
Let V<T> for a type T be the function that adds a variant type around T if T is not a variant already, or more formally:
V<T> = T
. V<T> = fcppt::variant::object<T>
. Let variant<L_1,...,L_n>
and variant<R_1,...,R_m>
be the results of applying V to Left and Right.
alternative_result<Left,Right>
is L_1. alternative_result<Left,Right>
is R_1. variant<L_1,...,L_n,R_1,...,R_m>
. using fcppt::parse::base_unique_ptr = fcppt::unique_ptr<fcppt::parse::base<Result, Ch, Skipper>> |
using fcppt::parse::basic_char_set_container = std::unordered_set<Ch> |
The character set type.
using fcppt::parse::char_ = fcppt::parse::basic_char<char> |
using fcppt::parse::char_set = fcppt::parse::basic_char_set<char> |
using fcppt::parse::column = fcppt::strong_typedef< std::uint64_t ,_> |
The column type of a position.
using fcppt::parse::deref_type = std::remove_cvref_t<fcppt::deref_type<Parser const>> |
The dereferenced type of a parser.
using fcppt::parse::is_char = std::disjunction<std::is_same<Type, char>, std::is_same<Type, wchar_t>> |
The allowed char types, currently char and wchar_t.
using fcppt::parse::is_parser = std::is_base_of<fcppt::parse::tag, Type> |
Checks if a type is a parser.A type is a parser if and only if it derives from fcppt::parse::tag.
using fcppt::parse::skipper::is_skipper = std::is_base_of<fcppt::parse::skipper::tag, Type> |
Checks if a type is a skipper.A type is a skipper if and only if it derives from fcppt::parse::skipper::tag.
using fcppt::parse::is_valid_argument |
Checks if a parameter is a valid parser type.
Parsers can be passed as-is, by fcppt::reference or by fcppt::unique_ptr.
using fcppt::parse::skipper::is_valid_argument |
Checks if a parameter is a valid skipper type.
Skippers can be passed as-is, by fcppt::reference or by fcppt::unique_ptr.
using fcppt::parse::line = fcppt::strong_typedef< std::uint64_t ,_> |
The line type of a position.
using fcppt::parse::literal = fcppt::parse::basic_literal<char> |
using fcppt::parse::repetition_result |
The result type of a repetition parser.
If Type is a character type (see fcppt::parse::is_char), then the result is std::basic_string<Type>
. Otherwise, it is std::vector<Type>
.
using fcppt::parse::result = fcppt::either::object<fcppt::parse::error<Ch>, Type> |
The result type when parsing streams of Ch, producing values of Type.The result is an fcppt::either::object of fcppt::parse::error<Ch>
and Type
.
using fcppt::parse::skipper::result = fcppt::either::error<fcppt::parse::error<Ch>> |
The result type when skipping streams of Ch.The result is an fcppt::either::error of fcppt::parse::error<Ch>
.
using fcppt::parse::result_of = typename fcppt::parse::deref_type<Parser>::result_type |
The result type of a parser.
using fcppt::parse::sequence_result |
The result of a sequence parser.
Let V<T> for a type T be the function that adds a tuple type around T if T is not a tuple already, or more formally:
V<T> = T
. V<T> = fcppt::tuple::object<T>
. The result type is computed as follows:
tuple<L_1,...,L_n>
and tuple<R_1,...,R_m>
be the results of applying V to Left and Right. Then the result is tuple<L_1,...,L_n,R_1,...,R_m>
. using fcppt::parse::string = fcppt::parse::basic_string<char> |
fcppt::parse::convert< std::remove_cvref_t< Parser >, Result > fcppt::parse::as_struct | ( | Parser && | _parser | ) |
Creates a parser that converts a tuple into a struct.
If Parser returns an fcppt::tuple::object<T_1,...,T_n>{t_1,...,t_n}
, then Result{t_1,...,t_n}
is returned. This implies that Result
must be constructible from T_1,...,T_n
.
Parser | A parser whose result is an fcppt::tuple::object . |
|
inline |
Skips all characters from fcppt::parse::space_set.
|
inline |
Parser that parses blank characters.
|
inline |
The set of blank characters.
Space and tab characters are considered to be blank characters.
fcppt::parse::convert< std::remove_cvref_t< Parser >, Result > fcppt::parse::construct | ( | Parser && | _parser | ) |
Applies a constructor to the result of a parser.
If the parser p returns s on success, then construct(p)
returns Result{s}
on success. Errors remain unchanged.
|
inline |
Dereferences a parser.
fcppt::parse::basic_char_set< Ch > fcppt::parse::digits | ( | ) |
The char set of digits.
Returns the char set of {0,...,9}.
fcppt::optional::object< Ch > fcppt::parse::get_char | ( | fcppt::reference< fcppt::parse::basic_stream< Ch > > const | _state | ) |
The next character in a stream.
fcppt::parse::position< Ch > fcppt::parse::get_position | ( | fcppt::reference< fcppt::parse::basic_stream< Ch > > const | _state | ) |
The current position of a stream.
|
nodiscard |
Parse a stream using a grammar.
fcppt::either::object< fcppt::parse::parse_string_error< Ch >, Result > fcppt::parse::grammar_parse_string | ( | std::basic_string< Ch > && | _string, |
fcppt::parse::grammar< Result, Ch, Skipper > const & | _grammar ) |
Parse a string using a grammar.
fcppt::parse::base_unique_ptr< fcppt::parse::result_of< Parser >, Ch, Skipper > fcppt::parse::make_base | ( | Parser && | _parser | ) |
Hides the type of a parser, turning it into an fcppt::parse::base_unique_ptr.
Note that a parser is usually polymorphic over character types and skipper types. However, when converting a parser to a base type, the character type Ch and the skipper type Skipper have to be committed, e.g. fcppt::parse::make_base<char,fcppt::parse::skipper::epsilon>(my_parser)
.
fcppt::parse::convert< std::remove_cvref_t< Parser >, std::invoke_result_t< Convert, fcppt::parse::result_of< Parser > && > > fcppt::parse::make_convert | ( | Parser && | _parser, |
Convert && | _convert ) |
Creates an fcppt::parse::convert parser out of a lambda.
_parser | The parser to convert from. |
_convert | The lambda function to use as conversion. |
fcppt::parse::convert_if< Ch, std::remove_cvref_t< Parser >, fcppt::either::success_type< std::invoke_result_t< Convert, fcppt::parse::position< Ch >, fcppt::parse::result_of< Parser > && > > > fcppt::parse::make_convert_if | ( | Parser && | _parser, |
Convert && | _convert ) |
Creates an fcppt::parse::convert_if parser out of a lambda.
_parser | The parser to convert from. |
_convert | The lambda function to use as conversion. TODO(philipp): Can we get rid of the Ch parameter? |
fcppt::parse::skipper::result< Ch > fcppt::parse::skipper::make_failure | ( | fcppt::parse::error< Ch > && | _error | ) |
Creates a skipper result from an fcppt::parse::error.
fcppt::parse::fatal< std::remove_cvref_t< Parser > > fcppt::parse::make_fatal | ( | Parser && | _parser | ) |
Creates an fcppt::parse::fatal parser.
fcppt::parse::ignore< std::remove_cvref_t< Parser > > fcppt::parse::make_ignore | ( | Parser && | _parser | ) |
Creates an fcppt::parse::ignore parser.
fcppt::parse::lexeme< std::remove_cvref_t< Parser > > fcppt::parse::make_lexeme | ( | Parser && | _parser | ) |
Creates an fcppt::parse::lexeme parser.
fcppt::parse::basic_literal< Ch > fcppt::parse::make_literal | ( | Ch const | _ch | ) |
Creates an fcppt::parse::basic_literal parser.
|
inline |
Creates a success value with a parse_stream failure type.
The character type Ch of the error value has to be specified explicitly.
|
inline |
Creates a success value with a parse_string failure type.
The character type Ch of the error value has to be specified explicitly.
fcppt::parse::recursive< std::remove_cvref_t< Parser > > fcppt::parse::make_recursive | ( | Parser && | _parser | ) |
Creates an fcppt::parse::recursive parser.
|
inline |
Creates an fcppt::parse::result from a success value.
The character type Ch of the error value has to be specified explicitly.
fcppt::parse::skipper::result< Ch > fcppt::parse::skipper::make_success | ( | ) |
Creates a skipper success result.
fcppt::parse::with_location< std::remove_cvref_t< Parser > > fcppt::parse::make_with_location | ( | Parser && | _parser | ) |
Creates an fcppt::parse::with_location parser.
|
inline |
Creates an fcppt::parse::not_ parser.
|
inlinenodiscard |
Creates an fcppt::parse::skipper::not_ skipper.
|
inline |
Creates an fcppt::parse::repetition parser.
fcppt::parse::skipper::repetition< std::remove_cvref_t< Parser > > fcppt::parse::skipper::operator* | ( | Parser && | _parser | ) |
Creates an fcppt::parse::skipper::repetition.
|
inline |
Creates an fcppt::parse::repetition_plus parser.
|
inline |
Creates an fcppt::parse::exclude parser.
|
inline |
Creates an fcppt::parse::optional parser.
std::basic_ostream< Ch > & fcppt::parse::operator<< | ( | std::basic_ostream< Ch > & | _stream, |
fcppt::parse::position< Ch > const & | _pos ) |
Outputs a position to a stream.
std::basic_ostream< Ch, Traits > & fcppt::parse::operator<< | ( | std::basic_ostream< Ch, Traits > & | _stream, |
fcppt::parse::location const & | _loc ) |
Outputs a location to a stream.
bool fcppt::parse::operator== | ( | fcppt::parse::position< Ch > const & | _left, |
fcppt::parse::position< Ch > const & | _right ) |
Compares two positions for equality.
|
inline |
Creates an fcppt::parse::sequence parser.
fcppt::parse::skipper::sequence< std::remove_cvref_t< Left >, std::remove_cvref_t< Right > > fcppt::parse::skipper::operator>> | ( | Left && | _left, |
Right && | _right ) |
Creates an fcppt::parse::skipper::sequence.
|
inline |
Creates an fcppt::parse::alternative parser.
|
inline |
Creates an fcppt::parse::skipper::alternative skipper.
|
inline |
Creates an fcppt::parse::complement parser.
Parser | Must be an fcppt::parse::basic_char_set |
|
inlinenodiscard |
Parses without whitespace skipping.
Calls fcppt::parse::phrase_parse with fcppt::parse::skipper::epsilon.
|
inlinenodiscard |
Parses a std::basic_istream
without whitespace skipping.
Calls fcppt::parse::phrase_parse_stream with fcppt::parse::skipper::epsilon.
|
inlinenodiscard |
Parses a std::basic_string
without whitespace skipping.
Calls fcppt::parse::phrase_parse_string with fcppt::parse::skipper::epsilon.
|
inlinenodiscard |
The basic parse function.
First, the skipper _skipper is called. If this succeeds, then the result of parsing _input with _parser and _skipper is returned. This function also catches all exceptions produced by _input and returns them as an error.
|
nodiscard |
Parses a std::basic_istream
, using whitespace skipping.
|
nodiscard |
Parses a std::basic_string
, using whitespace skipping.
|
nodiscard |
Runs a skipper on a stream.
void fcppt::parse::set_position | ( | fcppt::reference< fcppt::parse::basic_stream< Ch > > const | _state, |
fcppt::parse::position< Ch > const | _pos ) |
Sets the current position of a stream.
|
inline |
The skipper basic_space<char>
.
|
inline |
The char set parser using fcppt::parse::space_set<char>
.
|
inline |
The char set of spaces.Returns the set containing the whitespace, newline and tab characters.
|
inlineconstexpr |
Checks if a type is a skipper.