-
I'm missing something basic here to do with https://lexy.foonathan.net/playground/?id=14zej9o4P&mode=tree #include <lexy/callback.hpp>
struct StepRange {
int first{};
int last{};
int step{};
};
struct simplestep { // "6"
static constexpr auto rule = dsl::integer<int>;
static constexpr auto value = lexy::construct<StepRange>;
};
struct simplerange { // "1..10"
static constexpr auto rule = dsl::integer<int> >> dsl::twice(dsl::period) + dsl::integer<int>;
static constexpr auto value = lexy::construct<StepRange>;
};
struct complexrange { // "1,3..9"
static constexpr auto rule = dsl::integer<int> >> dsl::comma + dsl::integer<int> >> dsl::twice(dsl::period) + dsl::integer<int>;
static constexpr auto value = lexy::construct<StepRange>;
};
struct compound_range {
static constexpr auto rule = dsl::p<complexrange>
| dsl::p<simplerange>
| dsl::p<simplestep>
;
static constexpr auto value = lexy::forward<StepRange>;
}; Thanks for any suggestions on the best way to do this...I think I'm very close... |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
With the above example, this parses correctly: This fails:
This also fails:
Obviously, it's not switching to try the other choice options... I thought if the first one failed, it would then try the next. In fact, I've done some of my own "command parsing" examples which use choice (also, the simple hex parser example in the docs), and they worked as I expected. |
Beta Was this translation helpful? Give feedback.
-
Okay, using https://lexy.foonathan.net/playground/?id=f5x5xY58T&mode=tree The modified lines are shown like this #include <lexy/callback.hpp>
struct StepRange {
int first{};
int last{};
int step{};
};
struct simplestep { // "6"
static constexpr auto rule = dsl::integer<int>;
static constexpr auto value = lexy::construct<StepRange>;
};
struct simplerange { // "1..10"
static constexpr auto rule = dsl::peek(dsl::while_one(dsl::digit<>) >> dsl::twice(dsl::period)) // <--- using peek
>> dsl::integer<int16_t>
>> dsl::twice(dsl::period)
+ dsl::integer<int16_t>;
static constexpr auto value = lexy::construct<StepRange>;
};
struct complexrange { // "1,3..9"
static constexpr auto rule = dsl::peek(dsl::while_one(dsl::digit<>) + dsl::comma) // <--- using peek
>> dsl::integer<int16_t>
>> dsl::comma
+ dsl::integer<int16_t>
+ dsl::twice(dsl::period)
+ dsl::integer<int16_t>;
static constexpr auto value = lexy::construct<StepRange>;
};
struct compound_range {
static constexpr auto rule = dsl::p<complexrange>
| dsl::p<simplerange>
| dsl::p<simplestep>
;
static constexpr auto value = lexy::forward<StepRange>;
}; |
Beta Was this translation helpful? Give feedback.
-
You can also avoid But note that when constructing a complex range, this (and your solution), will fill something like |
Beta Was this translation helpful? Give feedback.
Okay, using
dsl::peek
I got it working; let me know if you think there's a better way to do it.https://lexy.foonathan.net/playground/?id=f5x5xY58T&mode=tree
The modified lines are shown like this
// <--- using peek