From 53bc9aa56f92b366a0c00d3309e5bd135bbab7ca Mon Sep 17 00:00:00 2001 From: rvcas Date: Thu, 15 Dec 2022 10:44:41 -0500 Subject: [PATCH] fix: properly capture empty lines --- crates/lang/src/parser.rs | 1 + crates/lang/src/parser/lexer.rs | 14 ++++---------- crates/lang/src/parser/token.rs | 2 ++ examples/sample/validators/swap.ak | 7 +++++++ 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/crates/lang/src/parser.rs b/crates/lang/src/parser.rs index a85d4879..025e08ea 100644 --- a/crates/lang/src/parser.rs +++ b/crates/lang/src/parser.rs @@ -51,6 +51,7 @@ pub fn module( false } + Token::NewLine => false, _ => true, }); diff --git a/crates/lang/src/parser/lexer.rs b/crates/lang/src/parser/lexer.rs index 3c1d758b..7256fe36 100644 --- a/crates/lang/src/parser/lexer.rs +++ b/crates/lang/src/parser/lexer.rs @@ -42,6 +42,7 @@ pub fn lexer() -> impl Parser, Error = ParseError> { just("&&").to(Token::AmperAmper), just('#').to(Token::Hash), just("\n\n").to(Token::EmptyLine), + just("\n").to(Token::NewLine), )); let grouping = choice(( @@ -127,17 +128,10 @@ pub fn lexer() -> impl Parser, Error = ParseError> { .map_with_span(|token, span| (token, span)), ); - let comments_with_trailing_newline = just("//").ignore_then( - take_until(text::newline()) - .then_ignore(text::newline().rewind()) - .to(Token::Comment) - .map_with_span(|token, span| (token, span)), - ); - choice(( module_comments, doc_comments, - choice((comments_with_trailing_newline, comments)), + comments, choice((keyword, int, op, grouping, string)) .or(any().map(Token::Error).validate(|t, span, emit| { emit(ParseError::expected_input_found( @@ -149,9 +143,9 @@ pub fn lexer() -> impl Parser, Error = ParseError> { })) .map_with_span(|token, span| (token, span)), )) - .padded() + .padded_by(one_of(" \t").ignored().repeated()) .recover_with(skip_then_retry_until([])) .repeated() - .padded() + .padded_by(one_of(" \t").ignored().repeated()) .then_ignore(end()) } diff --git a/crates/lang/src/parser/token.rs b/crates/lang/src/parser/token.rs index 0811268a..0ed3efac 100644 --- a/crates/lang/src/parser/token.rs +++ b/crates/lang/src/parser/token.rs @@ -55,6 +55,7 @@ pub enum Token { DocComment, ModuleComment, EmptyLine, + NewLine, // Keywords (alphabetically): As, Assert, @@ -130,6 +131,7 @@ impl fmt::Display for Token { Token::DocComment => "///", Token::ModuleComment => "////", Token::EmptyLine => "EMPTYLINE", + Token::NewLine => "NEWLINE", Token::As => "as", Token::Assert => "assert", Token::Check => "check", diff --git a/examples/sample/validators/swap.ak b/examples/sample/validators/swap.ak index 370e2d6b..868965b3 100644 --- a/examples/sample/validators/swap.ak +++ b/examples/sample/validators/swap.ak @@ -1,10 +1,17 @@ use sample +// Stuff + +/// Spend validator pub fn spend(datum: sample.Datum, rdmr: sample.Redeemer, _ctx: Nil) -> Bool { let x = #(datum, rdmr, #[244]) + let y = [#(#[222], #[222]), #(#[233], #[52])] + let [z, f, ..g] = y + let #(a, b, _) = x + z == #(#[222], #[222]) }