Branch rule for parsing chr
once but not twice
#50
-
Hi there, struct bitwise_or {
static constexpr auto whitespace = ws;
static constexpr auto rule = []{
// <-- Here. Match not || and then match |, otherwise return nullopt
auto rhs = dsl::peek_not(LEXY_LIT("||")) >> (dsl::peek(LEXY_LIT("|")) >> LEXY_LIT("|") + dsl::recurse<bitwise_or> | dsl::else_ >> dsl::nullopt);
return dsl::p<bitwise_and> + dsl::opt(rhs);
}();
static constexpr auto value = binary_expression_eval<ast::expressions::bitwise_or>;
}; Solutionconstexpr auto operator_ = dsl::identifier(dsl::ascii::punct);
constexpr auto kw_bitwise_or = LEXY_KEYWORD("|", operator_);
struct bitwise_or {
static constexpr auto whitespace = ws;
static constexpr auto rule = dsl::p<bitwise_and> + dsl::opt(kw_bitwise_or >> dsl::recurse<bitwise_or>);
static constexpr auto value = binary_expression_eval<ast::expressions::bitwise_or>;
}; |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 10 replies
-
LEXY_LIT("|") >> dsl::peek_not(LEXY_LIT("|")) Then it can just be wrapped in Once a branch condition matches, the branch is taken without backtracking, so you would want to avoid starting with something like E.g. https://lexy.foonathan.net/playground/?id=hnxeM9P41&mode=tree |
Beta Was this translation helpful? Give feedback.
-
Instead of using Also note that if you can use the newly added But as I've already said in a comment, built-in support for operator precedence parsing is coming. |
Beta Was this translation helpful? Give feedback.
-
I've just published two additions that can help you here:
|
Beta Was this translation helpful? Give feedback.
Instead of using
dsl::peek_not()
, there is also an alternative approach: using identifiers and keywords. They are designed to prevent partial matches. In your example, you want to match an operator consisting of e.g. ASCII punctuation, so that's your "identifier", and the operators in question are keywords: https://lexy.foonathan.net/playground/?id=a7berP9Ks&mode=tree Essentially, the keyword rule does something like this: https://lexy.foonathan.net/playground/?id=r4Ye6bxrM&mode=treeAlso note that if you can use the newly added
dsl::literal_set()
(or previouslyoperator/
) to achieve similar maximal munch: https://lexy.foonathan.net/playground/?id=7WG9vnb3e&mode=tree It matches the longes…