Browse Source

fix: lexer to properly support forward slashes in comments

Matthias Ladkau 3 years ago
parent
commit
155035339d
3 changed files with 24 additions and 1 deletions
  1. 2 0
      examples/game_of_life/game_of_life.ecal
  2. 1 1
      parser/lexer.go
  3. 21 0
      parser/lexer_test.go

+ 2 - 0
examples/game_of_life/game_of_life.ecal

@@ -3,6 +3,8 @@ Conway's Game of Life
 
 A zero-player game that evolves based on its initial state.
 
+https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
+
 Some examples:
 
 grid := [              # Normally evolving

+ 1 - 1
parser/lexer.go

@@ -746,7 +746,7 @@ func lexComment(l *lexer) lexFunc {
 
 		r = l.next(0)
 
-		for r != '*' && l.next(1) != '/' {
+		for r != '*' || l.next(1) != '/' {
 
 			if r == '\n' {
 				lLine++

+ 21 - 0
parser/lexer_test.go

@@ -298,6 +298,27 @@ bar`
 		t.Error("Unexpected lexer result:", res)
 		return
 	}
+
+	input = `
+/*
+Conway's Game of Life
+
+A zero-player game that evolves based on its initial state.
+
+https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
+*/
+
+1+ 2 # Some comment`
+	if res := LexToList("mytest", input); fmt.Sprint(res) != `[/* 
+Conway's Game of Life
+
+A zero-player game that evolves based on its initial state.
+
+https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
+ */ v:"1" + v:"2" #  Some comment EOF]` {
+		t.Error("Unexpected lexer result:", res)
+		return
+	}
 }
 
 func TestSinkLexing(t *testing.T) {