Basic String escape
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
package lexer
|
||||
|
||||
import "monkey/token"
|
||||
import (
|
||||
"monkey/token"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Lexer struct {
|
||||
input string
|
||||
@@ -179,14 +182,20 @@ func (l *Lexer) skipWhitespace() {
|
||||
}
|
||||
|
||||
func (l *Lexer) readString() string {
|
||||
position := l.position + 1
|
||||
b := &strings.Builder{}
|
||||
for {
|
||||
l.readChar()
|
||||
if l.ch == '"' || l.ch == 0 {
|
||||
break
|
||||
if l.ch == '\\' && l.peekChar() == '"' {
|
||||
l.readChar()
|
||||
} else {
|
||||
if l.ch == '"' || l.ch == 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
b.WriteByte(l.ch)
|
||||
}
|
||||
return l.input[position:l.position]
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func (l *Lexer) readLine() string {
|
||||
|
||||
@@ -32,6 +32,7 @@ func TestNextToken(t *testing.T) {
|
||||
"foo bar"
|
||||
[1, 2];
|
||||
{"foo": "bar"}
|
||||
"foo \"bar\""
|
||||
`
|
||||
|
||||
tests := []struct {
|
||||
@@ -137,7 +138,7 @@ func TestNextToken(t *testing.T) {
|
||||
{token.COLON, ":"},
|
||||
{token.STRING, "bar"},
|
||||
{token.RBRACE, "}"},
|
||||
|
||||
{token.STRING, "foo \"bar\""},
|
||||
{token.EOF, ""},
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user