From ec36cfdb546df44204a81f8423f799b39efab218 Mon Sep 17 00:00:00 2001 From: mlokr Date: Tue, 25 Jun 2024 19:46:48 +0200 Subject: [PATCH] slight improvement --- hblang/README.md | 15 +++++++++++++++ hblang/src/lexer.rs | 32 ++++++++++++++++++++++++++------ 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/hblang/README.md b/hblang/README.md index 338150f5..09315f87 100644 --- a/hblang/README.md +++ b/hblang/README.md @@ -66,6 +66,21 @@ add_one := fn(x: int): int { } ``` +#### comments +```hb +// commant is an item +main := fn(): int { + // comment is a statement + foo(/* comment is an exprression /* if you are crazy */ */); + return 0; +} + +foo := fn(comment: void): void + return /* comment evaluates to void */; + +// comments might be formatted in the future +``` + #### if_statements ```hb main := fn(): int { diff --git a/hblang/src/lexer.rs b/hblang/src/lexer.rs index 65dcc408..7ce0ebc3 100644 --- a/hblang/src/lexer.rs +++ b/hblang/src/lexer.rs @@ -109,6 +109,8 @@ pub enum TokenKind { Que = b'?', Directive = b'@', + Comment, + Ident, Number, Eof, @@ -296,15 +298,33 @@ impl<'a> Lexer<'a> { let ident = &self.bytes[start as usize..self.pos as usize]; T::from_ident(ident) } - b'"' | b'\'' => { - while let Some(nc) = self.advance() { - match nc { - b'\\' => _ = self.advance(), - nc if nc == c => break, + b'"' | b'\'' => loop { + match self.advance() { + Some(b'\\') => _ = self.advance(), + Some(nc) if nc == c => break identity(c), + Some(_) => {} + None => break T::Eof, + } + }, + b'/' if self.advance_if(b'/') => { + while let Some(l) = self.advance() + && l != b'\n' + {} + T::Comment + } + b'/' if self.advance_if(b'*') => { + let mut depth = 1; + while let Some(l) = self.advance() { + match l { + b'/' if self.advance_if(b'*') => depth += 1, + b'*' if self.advance_if(b'/') => match depth { + 1 => break, + _ => depth -= 1, + }, _ => {} } } - identity(c) + T::Comment } b'.' if self.advance_if(b'{') => T::Ctor, b'.' if self.advance_if(b'(') => T::Tupl,