From bfd33f8437be167d1947ee7c19d8979ba4c6d2d1 Mon Sep 17 00:00:00 2001 From: Gokcehan Date: Tue, 22 Nov 2016 22:12:47 +0300 Subject: [PATCH] add whitespace escaping for backslash --- eval_test.go | 24 ++++++++++++++++++++++++ scan.go | 14 ++++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/eval_test.go b/eval_test.go index 35257909..282562d1 100644 --- a/eval_test.go +++ b/eval_test.go @@ -41,6 +41,30 @@ var gTests = []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\tworld", "\n"}, + []Expr{&CallExpr{"echo", []string{"hello\tworld"}}}, + }, + + { + "echo hello\\\nworld", + []string{"echo", "hello\nworld", "\n"}, + []Expr{&CallExpr{"echo", []string{"hello\nworld"}}}, + }, + + { + `echo hello\aworld`, + []string{"echo", "helloworld", "\n"}, + []Expr{&CallExpr{"echo", []string{"helloworld"}}}, + }, + { "set hidden # trailing comments are allowed", []string{"set", "hidden", "\n"}, diff --git a/scan.go b/scan.go index 242432d9..514c2ed0 100644 --- a/scan.go +++ b/scan.go @@ -236,13 +236,23 @@ scan: s.cmd = true s.next() default: - beg := s.off + var buf []byte for !s.eof && !isSpace(s.chr) && s.chr != ';' && s.chr != '#' { + if s.chr == '\\' { + s.next() + if isSpace(s.chr) { + buf = append(buf, s.chr) + s.next() + } else { + s.next() + } + } + buf = append(buf, s.chr) s.next() } s.typ = TokenIdent - s.tok = string(s.buf[beg:s.off]) + s.tok = string(buf) s.sem = true if s.tok == "push" {