feat: pull comment tokens out of the Iter before parsing

This commit is contained in:
rvcas 2022-11-15 18:45:18 -05:00 committed by Lucas
parent f10c78d800
commit 00e5f99304
1 changed files with 30 additions and 33 deletions

View File

@ -7,7 +7,7 @@ pub mod lexer;
pub mod token; pub mod token;
use crate::{ use crate::{
ast::{self, BinOp, Span, TodoKind, CAPTURE_VARIABLE}, ast::{self, BinOp, Span, TodoKind, UntypedDefinition, CAPTURE_VARIABLE},
expr, expr,
}; };
@ -15,15 +15,6 @@ use error::ParseError;
use extra::ModuleExtra; use extra::ModuleExtra;
use token::Token; use token::Token;
#[allow(dead_code)]
enum DefinitionOrExtra {
Definition(Box<ast::UntypedDefinition>),
ModuleComment(Span),
DocComment(Span),
Comment(Span),
EmptyLine(usize),
}
pub fn module( pub fn module(
src: &str, src: &str,
kind: ast::ModuleKind, kind: ast::ModuleKind,
@ -37,21 +28,33 @@ pub fn module(
src.chars().enumerate().map(|(i, c)| (c, span(i))), src.chars().enumerate().map(|(i, c)| (c, span(i))),
))?; ))?;
let module_data =
module_parser().parse(chumsky::Stream::from_iter(span(len), tokens.into_iter()))?;
let mut definitions = Vec::new();
let mut extra = ModuleExtra::new(); let mut extra = ModuleExtra::new();
for data in module_data { let tokens = tokens.into_iter().filter(|(token, span)| match token {
match data { Token::ModuleComment => {
DefinitionOrExtra::Definition(def) => definitions.push(*def), extra.module_comments.push(*span);
DefinitionOrExtra::ModuleComment(c) => extra.module_comments.push(c),
DefinitionOrExtra::DocComment(c) => extra.doc_comments.push(c), false
DefinitionOrExtra::Comment(c) => extra.comments.push(c),
DefinitionOrExtra::EmptyLine(e) => extra.empty_lines.push(e),
} }
Token::DocComment => {
extra.doc_comments.push(*span);
false
} }
Token::Comment => {
extra.comments.push(*span);
false
}
Token::EmptyLine => {
extra.empty_lines.push(span.end);
false
}
_ => true,
});
let definitions = module_parser().parse(chumsky::Stream::from_iter(span(len), tokens))?;
let module = ast::UntypedModule { let module = ast::UntypedModule {
kind, kind,
@ -64,18 +67,12 @@ pub fn module(
Ok((module, extra)) Ok((module, extra))
} }
fn module_parser() -> impl Parser<Token, Vec<DefinitionOrExtra>, Error = ParseError> { fn module_parser() -> impl Parser<Token, Vec<UntypedDefinition>, Error = ParseError> {
choice(( choice((
import_parser() import_parser(),
.map(Box::new) data_parser(),
.map(DefinitionOrExtra::Definition), type_alias_parser(),
data_parser() fn_parser(),
.map(Box::new)
.map(DefinitionOrExtra::Definition),
type_alias_parser()
.map(Box::new)
.map(DefinitionOrExtra::Definition),
fn_parser().map(Box::new).map(DefinitionOrExtra::Definition),
)) ))
.repeated() .repeated()
.then_ignore(end()) .then_ignore(end())