Skip to content

How to change the parsing rules based on the parsed content? #157

Answered by foonathan
Life4gal asked this question in Q&A
Discussion options

You must be logged in to vote

This is context-sensitive parsing, which can't easily be done with pure lexy rules.

But your idea with the state is in the right-track. You can use dsl::scan to dispatch between rules based on members of the state. Something like:

struct value : lexy::scan_production<lexeme_type>
{
     template <typename Context, typename Reader>
     static constexpr scan_result scan(lexy::rule_scanner<Context, Reader>& scanner, const State& state)
    {
          if (state.allow_whitespace())
               return scanner.parse(value_with_whitespace_production{});
          else
               return scanner.parse(value_without_whitespace_production{});
    }
};

Likewise, you can use a different scan p…

Replies: 4 comments 13 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
3 replies
@Life4gal
Comment options

@Life4gal
Comment options

@foonathan
Comment options

Answer selected by Life4gal
Comment options

You must be logged in to vote
1 reply
@foonathan
Comment options

Comment options

You must be logged in to vote
9 replies
@Life4gal
Comment options

@foonathan
Comment options

@Life4gal
Comment options

@foonathan
Comment options

@Life4gal
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants