diff --git a/eval_test.go b/eval_test.go index 2baf0173..49ede3a5 100644 --- a/eval_test.go +++ b/eval_test.go @@ -41,6 +41,36 @@ var gEvalTests = []struct { []expr{&callExpr{"echo", []string{"hello world"}}}, }, + { + `echo "hello world"`, + []string{"echo", "hello world", "\n"}, + []expr{&callExpr{"echo", []string{"hello world"}}}, + }, + + { + `echo "hello\"world"`, + []string{"echo", `hello"world`, "\n"}, + []expr{&callExpr{"echo", []string{`hello"world`}}}, + }, + + { + `echo "hello\tworld"`, + []string{"echo", "hello\tworld", "\n"}, + []expr{&callExpr{"echo", []string{"hello\tworld"}}}, + }, + + { + `echo "hello\nworld"`, + []string{"echo", "hello\nworld", "\n"}, + []expr{&callExpr{"echo", []string{"hello\nworld"}}}, + }, + + { + `echo "hello\zworld"`, + []string{"echo", "helloworld", "\n"}, + []expr{&callExpr{"echo", []string{"helloworld"}}}, + }, + { `echo hello\ world`, []string{"echo", "hello world", "\n"}, @@ -60,7 +90,7 @@ var gEvalTests = []struct { }, { - `echo hello\aworld`, + `echo hello\zworld`, []string{"echo", "helloworld", "\n"}, []expr{&callExpr{"echo", []string{"helloworld"}}}, }, diff --git a/scan.go b/scan.go index 4f5f3f39..68cf5c73 100644 --- a/scan.go +++ b/scan.go @@ -203,6 +203,40 @@ scan: s.next() } goto scan + case s.chr == '"': + s.next() + var buf []byte + for !s.eof && s.chr != '"' { + if s.chr == '\\' { + s.next() + // TODO: octal escape + switch s.chr { + case '"', '\\': + buf = append(buf, s.chr) + case 'a': + buf = append(buf, '\a') + case 'b': + buf = append(buf, '\b') + case 'f': + buf = append(buf, '\f') + case 'n': + buf = append(buf, '\n') + case 'r': + buf = append(buf, '\r') + case 't': + buf = append(buf, '\t') + case 'v': + buf = append(buf, '\v') + } + s.next() + } else { + buf = append(buf, s.chr) + s.next() + } + } + s.typ = tokenIdent + s.tok = string(buf) + s.next() case s.chr == '\'': s.next() beg := s.off