taocpp / PEGTL

Parsing Expression Grammar Template Library
Boost Software License 1.0
1.94k stars 228 forks source link

Correct way to remove children of a node #213

Closed Atmaks closed 4 years ago

Atmaks commented 4 years ago

I'm trying to use the example grammar for Lua to build a call graph. The approach I have in mind is building a parse tree and then traversing it to create a proper call graph. For now, suppose we only care about "straight" function definitions (no assignment shenanigans). This is the selector I came up with:

// A transform that leaves the first node (supposed to be an identifier)
// and removes any children that don't have their own children (other identifiers)
// Intended for use with function_call and function_definition nodes
struct trim_children : pegtl::parse_tree::apply< trim_children > {
    template< typename Node, typename... States >
    static void transform(std::unique_ptr< Node >& node, States&&... st) {
        node->children.erase(std::remove_if(node->children.begin() + 1, node->children.end(), [](std::unique_ptr< Node >& n) { return n->children.size() == 0; }));
    }
};

template< typename Rule > using func_calls_selector = pegtl::parse_tree::selector< Rule,
    pegtl::parse_tree::store_content::on<
        name
    >, trim_children::on<
        function_definition,
        function_call
    > >;

The line with "children.erase" causes print_dot to crash with a Windows SEH exception with code 0xc0000005 "read access violation", which means this is not a proper way to trim the parse tree. What should I do instead?

And thank you for making this awesome library open source! I believe this is the only modern lexer-parser C++ library publically available.

d-frey commented 4 years ago

I'm not too familiar with the Lua example (@ColinH wrote the grammar), so my first guess would be, that the children are empty and hence node->children.begin() + 1 is simply invalid. Could you add a check and see if that is causing the crash?

d-frey commented 4 years ago

Closing due to inactivity.