From f288ae7f12747bf5da011ca3c6023f530baab363 Mon Sep 17 00:00:00 2001 From: imaqtkatt Date: Mon, 8 Jul 2024 13:30:02 -0300 Subject: [PATCH] Add multi line comment syntax --- src/fun/parser.rs | 54 +++++++++++++++--- .../parse_file/multi_line_comment.bend | 55 +++++++++++++++++++ .../parse_file__multi_line_comment.bend.snap | 23 ++++++++ 3 files changed, 125 insertions(+), 7 deletions(-) create mode 100644 tests/golden_tests/parse_file/multi_line_comment.bend create mode 100644 tests/snapshots/parse_file__multi_line_comment.bend.snap diff --git a/src/fun/parser.rs b/src/fun/parser.rs index 0792704e..72f4aca9 100644 --- a/src/fun/parser.rs +++ b/src/fun/parser.rs @@ -1093,14 +1093,31 @@ impl<'a> Parser<'a> for TermParser<'a> { continue; } if c == '#' { - while let Some(c) = self.peek_one() { - if c != '\n' { + self.advance_one(); + if let Some(c) = self.peek_one() { + if c == '{' { self.advance_one(); + while let Some(c) = self.peek_one() { + self.advance_one(); + if c == '}' { + if let Some('#') = self.peek_one() { + self.advance_one(); + break; + } else { + self.advance_one(); + } + } + } } else { - break; + while let Some(c) = self.peek_one() { + if c != '\n' { + self.advance_one(); + } else { + break; + } + } } } - self.advance_one(); // Skip the newline character as well continue; } break; @@ -1243,12 +1260,35 @@ pub trait ParserCommons<'a>: Parser<'a> { continue; } if c == '#' { - while let Some(c) = self.peek_one() { - if c != '\n' { + self.advance_one(); + char_count += 1; + if let Some(c) = self.peek_one() { + if c == '{' { self.advance_one(); char_count += 1; + while let Some(c) = self.peek_one() { + self.advance_one(); + char_count += 1; + if c == '}' { + if let Some('#') = self.peek_one() { + self.advance_one(); + char_count += 1; + break; + } else { + self.advance_one(); + char_count += 1; + } + } + } } else { - break; + while let Some(c) = self.peek_one() { + if c != '\n' { + self.advance_one(); + char_count += 1; + } else { + break; + } + } } } continue; diff --git a/tests/golden_tests/parse_file/multi_line_comment.bend b/tests/golden_tests/parse_file/multi_line_comment.bend new file mode 100644 index 00000000..7def751d --- /dev/null +++ b/tests/golden_tests/parse_file/multi_line_comment.bend @@ -0,0 +1,55 @@ +#{ + + + + +}# + +def main: + #{ + ok + #}# + # + return 0 + +#{foo type}# +type Foo: + #{foo constructor}# + Foo { foo } + +#{bar type}# +object Bar { bar } +#{ +# ignore +# }# + +#{X x x x}# +(X x x x) = #{ x }# x #{ x }# + +#{V}# +type V + = #{V constructor}# V + +(String/is_empty s) = + #{test if string is nil}# + match s #{ + + }#{ + String/Nil: 1 + String/Cons: 0 + } + +def String/not_empty(s): + #{test if string if not nil}# + match s: + #{ + its + not + }# + case String/Nil: + return 0 + case String/Cons: + #{ + it + is}# + return 1 diff --git a/tests/snapshots/parse_file__multi_line_comment.bend.snap b/tests/snapshots/parse_file__multi_line_comment.bend.snap new file mode 100644 index 00000000..0782b826 --- /dev/null +++ b/tests/snapshots/parse_file__multi_line_comment.bend.snap @@ -0,0 +1,23 @@ +--- +source: tests/golden_tests.rs +input_file: tests/golden_tests/parse_file/multi_line_comment.bend +--- +(X) = λ%arg0 λ%arg1 λ%arg2 use x = %arg2; x + +(String/is_empty) = λ%arg0 use s = %arg0; match s = s { String/Nil: 1; String/Cons: 0; } + +(main) = 0 + +(String/not_empty) = λ%arg0 use s = %arg0; match s = s { String/Nil: 0; String/Cons: 1; } + +(Foo/Foo) = λfoo λ%x (%x Foo/Foo/tag foo) + +(Bar) = λbar λ%x (%x Bar/tag bar) + +(V/V) = λ%x (%x V/V/tag) + +(Foo/Foo/tag) = 0 + +(Bar/tag) = 0 + +(V/V/tag) = 0