-
Hi, I'm trying to parse comments using: use chumsky::error::Rich;
use chumsky::prelude::*;
// Token is an enum
use crate::parsing::lexer::token::Token;
// type Span = SimpleSpan<usize>;
use crate::parsing::span::Span;
pub fn comment<'src>() -> impl Parser<
'src, &'src str, Token, extra::Err<Rich<'src, char, Span>>
> {
let line_comment = just("//").ignore_then(none_of("\r\n").repeated());
let block_comment = just("/*").ignore_then(none_of("*/").repeated());
line_comment.or(block_comment)
.to_slice()
.map(|val: &str| Token::Comment(String::from(val)))
} But I recently found out that the code for block comment let line_comment = just("//").ignore_then(take_until(text::newline()));
let block_comment = just("/*").ignore_then(take_until(just("*/"))); but I tried implementing a custom parser, but couldn't figure out the proper syntax: pub fn comment<'src>() -> impl Parser<
'src, &'src str, Token, extra::Err<Rich<'src, char, Span>>
> {
let line_comment = just("//").ignore_then(none_of("\r\n").repeated());
let block_comment = just("/*").ignore_then(custom::<_, &str, _, _>(|inp| {
let mut fslash_is_end = false;
loop {
match inp.next() {
Some('/') if fslash_is_end => { return Ok(()); }
Some('*') => { fslash_is_end = true; }
Some(___) => { fslash_is_end = false; }
None => { Err(Rich::custom(/* how to determine span? */, "Unexpected End of Input")) }
}
}
}))
line_comment.or(block_comment)
.to_slice()
.map(|val: &str| Token::Comment(String::from(val)))
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You're probably looking for any().and_is(just("*/").not()) This tells chumsky to accept a character that is not the start of the sequence Note that I think you might misunderstand what |
Beta Was this translation helpful? Give feedback.
You're probably looking for
and_is
andnot
. You can use them together like:This tells chumsky to accept a character that is not the start of the sequence
*/
.Note that I think you might misunderstand what
none_of
does: likeone_of
, it accepts a set of characters to reject, not a sequence. Sonone_of("*/")
will accept any character except*
and/
.