From 523f40968b9c1a23da1f4a1c2f125197d7611fef Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sat, 20 May 2017 16:28:10 +0200 Subject: 04 --- lexer/lexer.go | 20 ++++++++++++++++++++ lexer/lexer_test.go | 17 +++++++++++++++++ 2 files changed, 37 insertions(+) (limited to 'lexer') diff --git a/lexer/lexer.go b/lexer/lexer.go index cffd925..8deda73 100644 --- a/lexer/lexer.go +++ b/lexer/lexer.go @@ -51,6 +51,8 @@ func (l *Lexer) NextToken() token.Token { tok = newToken(token.GT, l.ch) case ';': tok = newToken(token.SEMICOLON, l.ch) + case ':': + tok = newToken(token.COLON, l.ch) case ',': tok = newToken(token.COMMA, l.ch) case '{': @@ -61,6 +63,13 @@ func (l *Lexer) NextToken() token.Token { tok = newToken(token.LPAREN, l.ch) case ')': tok = newToken(token.RPAREN, l.ch) + case '"': + tok.Type = token.STRING + tok.Literal = l.readString() + case '[': + tok = newToken(token.LBRACKET, l.ch) + case ']': + tok = newToken(token.RBRACKET, l.ch) case 0: tok.Literal = "" tok.Type = token.EOF @@ -122,6 +131,17 @@ func (l *Lexer) readNumber() string { return l.input[position:l.position] } +func (l *Lexer) readString() string { + position := l.position + 1 + for { + l.readChar() + if l.ch == '"' { + break + } + } + return l.input[position:l.position] +} + func isLetter(ch byte) bool { return 'a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z' || ch == '_' } diff --git a/lexer/lexer_test.go b/lexer/lexer_test.go index 0a7f248..e4e64a4 100644 --- a/lexer/lexer_test.go +++ b/lexer/lexer_test.go @@ -26,6 +26,10 @@ if (5 < 10) { 10 == 10; 10 != 9; +"foobar" +"foo bar" +[1, 2]; +{"foo": "bar"} ` tests := []struct { @@ -105,6 +109,19 @@ if (5 < 10) { {token.NOT_EQ, "!="}, {token.INT, "9"}, {token.SEMICOLON, ";"}, + {token.STRING, "foobar"}, + {token.STRING, "foo bar"}, + {token.LBRACKET, "["}, + {token.INT, "1"}, + {token.COMMA, ","}, + {token.INT, "2"}, + {token.RBRACKET, "]"}, + {token.SEMICOLON, ";"}, + {token.LBRACE, "{"}, + {token.STRING, "foo"}, + {token.COLON, ":"}, + {token.STRING, "bar"}, + {token.RBRACE, "}"}, {token.EOF, ""}, } -- cgit v1.2.3