From 41723eb4caf581b7287f5d85944126f61b469bab Mon Sep 17 00:00:00 2001 From: Joscha Date: Fri, 18 Nov 2022 20:55:44 +0100 Subject: [PATCH] Parse comments in whitespace --- src/ast/basic.rs | 14 ++++++++++++-- src/parser/basic.rs | 35 +++++++++++++++++++++++++++++------ 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/src/ast/basic.rs b/src/ast/basic.rs index 628b459..e472e39 100644 --- a/src/ast/basic.rs +++ b/src/ast/basic.rs @@ -2,15 +2,25 @@ use std::fmt; use crate::span::{HasSpan, Span}; +#[derive(Clone)] +pub enum Line { + Empty, + Comment(String), +} + #[derive(Clone)] pub struct Space { - pub comment: Vec<(String, Span)>, + pub lines: Vec, pub span: Span, } impl fmt::Debug for Space { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("Space").finish() + if self.lines.iter().any(|l| matches!(l, Line::Comment(_))) { + write!(f, "space with comments") + } else { + write!(f, "space") + } } } diff --git a/src/parser/basic.rs b/src/parser/basic.rs index 971d558..d3cb2f1 100644 --- a/src/parser/basic.rs +++ b/src/parser/basic.rs @@ -1,20 +1,43 @@ //! Corresponds to `ast::basic`. use chumsky::prelude::*; +use chumsky::text::Character; -use crate::ast::{Ident, Space}; +use crate::ast::{Ident, Line, Space}; use crate::span::Span; pub type Error = Simple; // TODO https://github.com/rust-lang/rust/issues/63063 +pub fn inline() -> impl Parser { + filter(|c: &char| c.is_whitespace() && *c != '\n') + .repeated() + .to(()) +} + +pub fn newline() -> impl Parser { + just('\n').to(()) +} + +pub fn line() -> impl Parser { + let empty = newline().to(Line::Empty); + + let comment = just('#') + .ignore_then(take_until(newline())) + .map(|(s, _)| s) + .collect::() + .map(Line::Comment); + + empty.or(comment) +} + pub fn space() -> impl Parser { - // TODO Parse comments - text::whitespace().map_with_span(|(), span| Space { - comment: vec![], - span, - }) + inline() + .ignore_then(line()) + .repeated() + .then_ignore(inline()) + .map_with_span(|lines, span| Space { lines, span }) } pub fn ident() -> impl Parser {