From 18847f55bb3fe9db41e46a2e9e49a9f7c28143af Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Mon, 10 Feb 2020 14:45:20 +0100 Subject: pkg/ast: introduce hex-encoded string literals MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The stringnozescapes does not make sense with filename, also we may need similar escaping for string flags. Handle escaped strings on ast level instead. This avoids introducing new type and works seamleassly with flags. As alternative I've also tried using strconv.Quote/Unquote but it leads to ugly half-escaped strings: "\xb0\x80s\xe8\xd4N\x91\xe3ڒ,\"C\x82D\xbb\x88\\i\xe2i\xc8\xe9\xd85\xb1\x14):M\xdcn" Make hex-encoded strings a separate string format instead. --- pkg/ast/parser.go | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'pkg/ast/parser.go') diff --git a/pkg/ast/parser.go b/pkg/ast/parser.go index b8d22fd88..7b46f6611 100644 --- a/pkg/ast/parser.go +++ b/pkg/ast/parser.go @@ -314,7 +314,7 @@ func (p *parser) parseFlags(name *Ident) Node { switch p.tok { case tokInt, tokIdent: return p.parseIntFlags(name) - case tokString: + case tokString, tokStringHex: return p.parseStrFlags(name) default: p.expect(tokInt, tokIdent, tokString) @@ -417,9 +417,10 @@ func (p *parser) parseType() *Type { case tokIdent: allowColon = true arg.Ident = p.lit - case tokString: + case tokString, tokStringHex: arg.String = p.lit arg.HasString = true + arg.StringFmt = strTokToFmt(p.tok) default: p.expect(tokInt, tokIdent, tokString) } @@ -468,15 +469,27 @@ func (p *parser) parseIdent() *Ident { } func (p *parser) parseString() *String { - p.expect(tokString) + p.expect(tokString, tokStringHex) str := &String{ Pos: p.pos, Value: p.lit, + Fmt: strTokToFmt(p.tok), } p.next() return str } +func strTokToFmt(tok token) StrFmt { + switch tok { + case tokString: + return StrFmtRaw + case tokStringHex: + return StrFmtHex + default: + panic("bad string token") + } +} + func (p *parser) parseInt() *Int { i := &Int{ Pos: p.pos, -- cgit mrf-deployment