aboutsummaryrefslogtreecommitdiff
path: root/lexer
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2017-05-20 16:28:10 +0200
committerDimitri Sokolyuk <demon@dim13.org>2017-05-20 16:28:10 +0200
commit523f40968b9c1a23da1f4a1c2f125197d7611fef (patch)
tree36518bed0260cb3cede13b59fc7051a9e262d403 /lexer
parent69fc902f8f5fd8f36db0991f6ba4faeabb3090fa (diff)
04
Diffstat (limited to 'lexer')
-rw-r--r--lexer/lexer.go20
-rw-r--r--lexer/lexer_test.go17
2 files changed, 37 insertions, 0 deletions
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, ""},
}