hare

The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit 88cb6c68e9cb248aac3bd54bf543b9f8a08ac607
parent 33d56cc248af22b3ce6bc2dc6b12d51c16929140
Author: Drew DeVault <sir@cmpwn.com>
Date:   Fri, 19 Feb 2021 14:32:24 -0500

hare::lex: implement comments

Diffstat:
Mhare/lex/+test.ha | 10++++++++++
Mhare/lex/lex.ha | 18++++++++++++++----
2 files changed, 24 insertions(+), 4 deletions(-)

diff --git a/hare/lex/+test.ha b/hare/lex/+test.ha @@ -140,3 +140,13 @@ fn lextest(in: str, expected: [](uint, uint, token)) void = { ]; lextest(in, expected); }; + +@test fn comments() void = { + const in = "hello world // foo\nbar"; + const expected: [_](uint, uint, token) = [ + (1, 1, "hello": name), + (1, 7, "world": name), + (2, 1, "bar": name), + ]; + lextest(in, expected); +}; diff --git a/hare/lex/lex.ha b/hare/lex/lex.ha @@ -129,15 +129,14 @@ fn lex_name(lex: *lexer, loc: location) ((token, location) | io::EOF | error) = (tok: btoken: token, loc); }, }; - }; fn lex2( - lex: *lexer, + lexr: *lexer, loc: location, r: rune, ) ((token, location) | io::EOF | error) = { - let n = match (next(lex)) { + let n = match (next(lexr)) { err: io::error => return err, io::EOF => io::EOF, r: rune => r, @@ -161,6 +160,17 @@ fn lex2( '/' => match (n) { r: rune => switch (r) { '=' => return (btoken::DIVEQ: token, loc), + '/' => { + // Comment + for (true) match (next(lexr)) { + err: io::error => return err, + io::EOF => break, + r: rune => if (r == '\n') { + break; + }, + }; + return lex(lexr); + }, * => btoken::DIV, }, io::EOF => btoken::DIV, @@ -218,7 +228,7 @@ fn lex2( }, * => return syntaxerr(loc), }; - unget(lex, n); + unget(lexr, n); return (tok, loc); };