feat: some parsing for comments

This commit is contained in:
rvcas
2022-11-16 11:16:45 -05:00
committed by Lucas
parent 00e5f99304
commit f7313ee61a
6 changed files with 40 additions and 31 deletions

View File

@@ -47,7 +47,8 @@ pub fn module(
false
}
Token::EmptyLine => {
extra.empty_lines.push(span.end);
println!("{:?}", span);
extra.empty_lines.push(span.start);
false
}

View File

@@ -5,7 +5,7 @@ use crate::ast::Span;
use super::{error::ParseError, token::Token};
pub fn lexer() -> impl Parser<char, Vec<(Token, Span)>, Error = ParseError> {
let int = text::int(10).map(|value| Token::Int { value });
let int = choice((text::int(10), text::int(16))).map(|value| Token::Int { value });
let op = choice((
just("==").to(Token::EqualEqual),
@@ -30,6 +30,7 @@ pub fn lexer() -> impl Parser<char, Vec<(Token, Span)>, Error = ParseError> {
just("||").to(Token::VbarVbar),
just('|').to(Token::Vbar),
just("&&").to(Token::AmperAmper),
just("\n\n").to(Token::EmptyLine),
));
let grouping = choice((
@@ -95,32 +96,39 @@ pub fn lexer() -> impl Parser<char, Vec<(Token, Span)>, Error = ParseError> {
}
});
let token = choice((keyword, int, op, grouping, string))
.or(any().map(Token::Error).validate(|t, span, emit| {
emit(ParseError::expected_input_found(
span,
None,
Some(t.clone()),
));
t
}))
.map_with_span(move |token, span| (token, span))
.padded()
.recover_with(skip_then_retry_until([]));
let module_comments =
just("////").ignore_then(take_until(text::newline()).to(Token::ModuleComment));
let doc_comments = just("///")
.ignore_then(take_until(text::newline()))
.to(Token::DocComment);
let comments = just("//")
.then_ignore(
just('(')
.ignore_then(take_until(just(")#")).ignored())
.or(none_of('\n').ignored().repeated().ignored()),
)
.padded()
.ignored()
.repeated();
.ignore_then(take_until(text::newline()))
.to(Token::Comment);
token
.padded_by(comments)
.repeated()
.padded()
.then_ignore(end())
choice((
module_comments,
doc_comments,
comments,
keyword,
int,
op,
grouping,
string,
))
.or(any().map(Token::Error).validate(|t, span, emit| {
emit(ParseError::expected_input_found(
span,
None,
Some(t.clone()),
));
t
}))
.map_with_span(move |token, span| (token, span))
.padded()
.recover_with(skip_then_retry_until([]))
.repeated()
.padded()
.then_ignore(end())
}