joemalle / limn

A tiny parser designed to compile quickly
Boost Software License 1.0
2 stars 1 forks source link

how to mention the sub matched parser rule item #7

Open asmwarrior opened 1 year ago

asmwarrior commented 1 year ago

As we have discussed in the pull request add the callback function to the metched items, so that the function will be called when matched

I see in this project: https://github.com/TheLartians/PEGParser

It use e[0] and e[1] like words to mention those items.

Not sure how we can do this in limn.

Thanks.

asmwarrior commented 1 year ago

I have created a simple test code:

struct abc
{
    void set_a(const std::string_view& out) {a = out[0];};
    void set_b(const std::string_view& out) {b = out[0];};
    void set_c(const std::string_view& out) {c = out[0];};
    void build_abc(const std::string_view& out)
    {
        std::cout << a << b << c << std::endl;
    }
private:
    char a;
    char b;
    char c;
};

abc g;

std::function<void(const std::string_view&)> fa = std::bind(&abc::set_a, &g, std::placeholders::_1);
std::function<void(const std::string_view&)> fb = std::bind(&abc::set_b, &g, std::placeholders::_1);
std::function<void(const std::string_view&)> fc = std::bind(&abc::set_c, &g, std::placeholders::_1);
std::function<void(const std::string_view&)> fabc = std::bind(&abc::build_abc, &g, std::placeholders::_1);

    parse("aec abc",  *(   ( (char_('a')[fa] >> char_('b')[fb])
                            | (char_('a')[fa] >> char_('e')[fb]) )  >> char_('c')[fc] >> *space_)[fabc]);

The task is simple, but the code looks complex.

joemalle commented 1 year ago

i don't know of a nicer way. I suppose you can use lambdas instead of std::bind, but you're pushing the limits of the original design (which is cool!)

I've invited you to be a collaborator. Feel free to modify this library and make it your own. I'm happy to do not-too-frequent code reviews if it helps

asmwarrior commented 1 year ago

i don't know of a nicer way. I suppose you can use lambdas instead of std::bind, but you're pushing the limits of the original design (which is cool!)

I've invited you to be a collaborator. Feel free to modify this library and make it your own. I'm happy to do not-too-frequent code reviews if it helps

Thanks. The long term goal I would like to do is using a more general TokenStream like class as the input of the parser. Which means the user can define the Token instead of the current string_view class.

A more long term goal is to use this library to parse more things, such as I have done in my branch to parse C++ function declaration, see:

https://github.com/asmwarrior/limn/blob/af852c808a9fdf3a3d7a156fa694c7b55d801a96/tests/test_parse_cxx.cpp

asmwarrior commented 1 year ago

FYI: I have added some test files in the tests folder. Especially in file test_parse_cxx_function_declaration.cpp, I use the parser to fill many member variables of a class.