aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/ast/parser.go
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2020-02-10 14:45:20 +0100
committerDmitry Vyukov <dvyukov@google.com>2020-02-10 14:45:20 +0100
commit18847f55bb3fe9db41e46a2e9e49a9f7c28143af (patch)
treee181c33c75f5851a5042290ff59dd44cce353c66 /pkg/ast/parser.go
parentd0da558cb16f3fb7cf26f20d24ee89eeb49a6f30 (diff)
pkg/ast: introduce hex-encoded string literals
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.
Diffstat (limited to 'pkg/ast/parser.go')
-rw-r--r--pkg/ast/parser.go19
1 files changed, 16 insertions, 3 deletions
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,