From e626352e879c0243d6059c91fe39f398a0118354 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Wed, 11 Jan 2023 18:41:01 +0100 Subject: [PATCH] Implement support for multiline comments MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- src/lib.rs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index b758579..88c0513 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,7 +14,7 @@ use nom::{ branch::alt, bytes::complete::{tag, take_until, take_until1, take_while, take_while1}, character::complete::{multispace0, space0}, - combinator::recognize, + combinator::{opt, recognize}, error::{ErrorKind, ParseError}, multi::{many0, many1}, sequence::{delimited, tuple}, @@ -47,11 +47,23 @@ pub struct Spec { } fn allspaces(i: &str) -> IResult<&str, &str> { - alt((delimited(multispace0, comment, multispace0), multispace0))(i) + let (i, (_, comments)) = tuple((multispace0, opt(comment)))(i)?; + + let mut i = i; + if let Some(_) = comments { + let (j, _) = multispace0(i)?; + i = j; + } + + Ok((i, "")) } fn comment(i: &str) -> IResult<&str, &str> { - let (i, _) = delimited(alt((tag("#"), tag("//"))), take_until("\n"), tag("\n"))(i)?; + let (i, _) = many1(delimited( + alt((tag("#"), tag("//"))), + take_until("\n"), + tag("\n"), + ))(i)?; Ok((i, "")) }