-
Hi, I am trying to parse union and "primitive" types but couldn't find a way to differentiate both types during parsing.
I tried the following using #[derive(Debug, Clone)]
enum Primary {
String,
Number,
Boolean,
}
#[derive(Debug, Clone)]
enum Ty {
Union(Vec<Primary>),
Primary(Primary),
}
let typ = choice((
just("string").to(Primary::String),
just("number").to(Primary::Number),
just("boolean").to(Primary::Boolean),
));
choice((
typ.separated_by(just('|').padded()).map(Ty::Union),
typ.map(Ty::Primary),
)); I can't figure out how to fix this(?). I also tried something different but got same results. I would appreciate If you could help me with this :) Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Hello! I think you can fix it by calling |
Beta Was this translation helpful? Give feedback.
-
Edit: #206 (comment) This is also correct. After doing some more brute force, I found a solution and the solution was jokingly obvious. choice((
typ.then_ignore(just('|')).chain(typ.separated_by(just('|'))).map(Ty::Union),
typ.map(Ty::Primary),
)); Now the parser is checking for the next token |
Beta Was this translation helpful? Give feedback.
Hello! I think you can fix it by calling
at_least(2)
on theSeperatedBy
structure:Ref: https://docs.rs/chumsky/0.8.0/chumsky/combinator/struct.SeparatedBy.html#method.at_least