From acb34cc0edcff326654714ebb19907e51562b384 Mon Sep 17 00:00:00 2001 From: Mel Date: Sun, 18 Dec 2022 01:33:22 +0100 Subject: Skip comments in scanner --- pkg/lang/scanner/scanner.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'pkg/lang/scanner/scanner.go') diff --git a/pkg/lang/scanner/scanner.go b/pkg/lang/scanner/scanner.go index eb7da89..e1f8edf 100644 --- a/pkg/lang/scanner/scanner.go +++ b/pkg/lang/scanner/scanner.go @@ -48,7 +48,7 @@ func (s *Scanner) scanToken() (token.Token, error) { return token.Token{}, ErrScannerFinished } - hadNewline, firstNewline, err := s.skipWhitespace() + hadNewline, firstNewline, err := s.skipNonCode() if err != nil { return token.Token{}, err } @@ -314,7 +314,8 @@ func (s *Scanner) scanNumber() (token.Token, error) { return token.New(token.Int, loc, num), nil } -func (s *Scanner) skipWhitespace() (bool, source.Loc, error) { +func (s *Scanner) skipNonCode() (bool, source.Loc, error) { + isInComment := false hadNewline := false firstNewline := source.Loc{} @@ -329,7 +330,13 @@ func (s *Scanner) skipWhitespace() (bool, source.Loc, error) { hadNewline = true } - if eof || !unicode.IsSpace(c) { + if isInComment { + if c == '\n' { + isInComment = false + } + } else if c == '#' { + isInComment = true + } else if eof || !unicode.IsSpace(c) { break } -- cgit 1.4.1