Limn  0.1
Public Member Functions | List of all members
lm::action_< Func > Class Template Referencefinal

Customizable parser. More...

#include <limn.h>

Inherits lm::impl::parser_base< Base >.

Public Member Functions

constexpr action_ (Func &&func) noexcept
 Construct an action_ parser. More...
 
constexpr auto operator* () const noexcept
 The Kleene star or "any number of times" parser combinator. More...
 
constexpr auto operator+ () const noexcept
 The plus parser or "one or more" parser combinator. More...
 
constexpr auto operator[] (std::string_view &output) const noexcept
 The match operator. More...
 
constexpr auto operator[] (std::function< void(const std::string_view &)> callback) const noexcept
 The match callback operator. More...
 

Detailed Description

template<typename Func>
class lm::action_< Func >

Customizable parser.

An object of this type uses a callback to determine a match. This enables recursive grammars and interaction with other parsers. For an example, look at tests.cpp:validParentheses (which can also be found in README.md).

Constructor & Destructor Documentation

◆ action_()

template<typename Func >
constexpr lm::action_< Func >::action_ ( Func &&  func)
inlineexplicitnoexcept

Construct an action_ parser.

This function receives a string_view by reference and returns a bool. The string_view argument starts at the current parse position and ends at the end of the input string. To indicate a match, return true and adjust the string_view's start position to the end of the matched section. If there is no match, the function must return false and not modify the input.

For example, suppose you have a callback that parses "ABC". You could have used lm::lit_("ABC"), but you like making things complicated I guess.

Your function could be called with "ABCDEFG". In that case, you would set the input to "DEFG" and return true.

If your function was invoked with "QWERTY", then you would return false without modifying the input.

Parameters
[in]funcThe function to call.

Member Function Documentation

◆ operator*()

template<typename Base >
constexpr auto lm::impl::parser_base< Base >::operator* ( ) const
inlinenoexceptinherited

The Kleene star or "any number of times" parser combinator.

This function returns a parser that matches its input any number of times (including 0 times). For example, `* lm::char_('a')matches "", "a", "aa", etc. This is a greedy match;* lm::char_('a') >> lm::lit_("aa")` will NEVER parse an input string because the Kleene star will eat up all the 'a's.

◆ operator+()

template<typename Base >
constexpr auto lm::impl::parser_base< Base >::operator+ ( ) const
inlinenoexceptinherited

The plus parser or "one or more" parser combinator.

This function returns a parser that matches its input one or more times (not including 0 times). For example, `+ lm::char_('a')matches "a", "aa", etc. This is a greedy match;+ lm::char_('a') >> lm::lit_("aa")` will NEVER parse an input string because the plus parser will eat up all the 'a's.

◆ operator[]() [1/2]

template<typename Base >
constexpr auto lm::impl::parser_base< Base >::operator[] ( std::string_view &  ouput) const
inlinenoexceptinherited

The match operator.

This side-effect-only function copies the matched part of input to its argument output. If there is no match, then output is unchanged. For example, `(* lm::char_('a'))[output]will match any number ofacharacters in sequence, and it will copy that sequence to thestd::string_viewoutput`.

This is useful for getting the matched string out of the parser. It's also sometimes useful for printf-debugging.

See tests.cpp or README.md for an example.

Parameters
[out]outputThe argument to receive the match portion.

◆ operator[]() [2/2]

template<typename Base >
constexpr auto lm::impl::parser_base< Base >::operator[] ( std::function< void(const std::string_view &)>  callback) const
inlinenoexceptinherited

The match callback operator.

The std::function will be called when an item get matched. The std::function should have the protytype std::function<void(const std::string_view&)> where the argument is the matched string_view. If there is no match, then this function will not be called. For example, `(* lm::char_('a'))[functor]will match any number ofa` characters in sequence, and it will call the functor.

This is useful for add the user defined action in this std::function For example, adding the AST of the matched item to the parsing tree

See tests.cpp or README.md for an example.

Parameters
[in]callbackThe std::function callback function which will be called when matched portion, the functor's parameter is the matched string_view.

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