runningwater 3 лет назад
Родитель
Сommit
cf946f8a15
3 измененных файлов с 13 добавлено и 4 удалено
  1. 5 3
      lexer/lexer.go
  2. 7 1
      lexer/lexer_test.go
  3. 1 0
      token/token.go

+ 5 - 3
lexer/lexer.go

@@ -117,9 +117,6 @@ func (l *Lexer) NextToken() token.Token {
 		tok = newToken(token.LBRACE, l.ch)
 	case '}':
 		tok = newToken(token.RBRACE, l.ch)
-	case 0:
-		tok.Type = token.EOF
-		tok.Literal = ""
 	case '"':
 		tok.Type = token.STRING
 		tok.Literal = l.readString()
@@ -127,6 +124,11 @@ func (l *Lexer) NextToken() token.Token {
 		tok = newToken(token.LBRACKET, l.ch)
 	case ']':
 		tok = newToken(token.RBRACKET, l.ch)
+	case ':':
+		tok = newToken(token.COLON, l.ch)
+	case 0:
+		tok.Type = token.EOF
+		tok.Literal = ""
 	default:
 		if isLetter(l.ch) {
 			tok.Literal = l.readIdentifier()

+ 7 - 1
lexer/lexer_test.go

@@ -30,7 +30,8 @@ func TestNextToken(t *testing.T) {
         10 != 9;
         "foobar"
 		"foo bar"
-		[1, 2];`
+		[1, 2];
+		{"foo":"bar"}`
 	tests := []struct {
 		expectedType    token.TypeToken
 		expectedLiteral string
@@ -115,6 +116,11 @@ func TestNextToken(t *testing.T) {
 		{token.INT, "2"},
 		{token.RBRACKET, "]"},
 		{token.SEMICOLON, ";"},
+		{token.LBRACE, "{"},
+		{token.STRING, "foo"},
+		{token.COLON, ":"},
+		{token.STRING, "bar"},
+		{token.RBRACE, "}"},
 		{token.EOF, ""},
 	}
 

+ 1 - 0
token/token.go

@@ -32,6 +32,7 @@ const (
 	// Delimiters
 	COMMA     = ","
 	SEMICOLON = ";"
+	COLON     = ":"
 
 	LPAREN = "("
 	RPAREN = ")"