diff options
| author | Jason A. Donenfeld <Jason@zx2c4.com> | 2020-02-07 22:26:45 +0100 |
|---|---|---|
| committer | Dmitry Vyukov <dvyukov@google.com> | 2020-02-10 10:51:16 +0100 |
| commit | b97dee873b419c5386b2742530c98a18dae36e2b (patch) | |
| tree | 67afbc63c3acf5196495baa9b62315ad5eef09b4 /pkg | |
| parent | 4f86d327735efede368648e63570189966cb30c0 (diff) | |
pkg/compiler: allow for escaped strings
This adds stringnozescapes to allow parsing of escape sequences in
strings.
Diffstat (limited to 'pkg')
| -rw-r--r-- | pkg/compiler/types.go | 21 |
1 files changed, 16 insertions, 5 deletions
diff --git a/pkg/compiler/types.go b/pkg/compiler/types.go index 43efee202..251e0fcaa 100644 --- a/pkg/compiler/types.go +++ b/pkg/compiler/types.go @@ -514,11 +514,12 @@ func genTextType(t *ast.Type) prog.TextKind { } const ( - stringnoz = "stringnoz" + stringnoz = "stringnoz" + stringnozescapes = "stringnozescapes" ) var typeString = &typeDesc{ - Names: []string{"string", stringnoz}, + Names: []string{"string", stringnoz, stringnozescapes}, CanBeTypedef: true, OptArgs: 2, Args: []namedArg{ @@ -526,7 +527,7 @@ var typeString = &typeDesc{ {Name: "size", Type: typeArgInt}, }, Check: func(comp *compiler, t *ast.Type, args []*ast.Type, base prog.IntTypeCommon) { - if t.Ident == stringnoz && len(args) > 1 { + if (t.Ident == stringnoz || t.Ident == stringnozescapes) && len(args) > 1 { comp.error(args[0].Pos, "fixed-size string can't be non-zero-terminated") } }, @@ -558,7 +559,7 @@ var typeString = &typeDesc{ return &prog.BufferType{ TypeCommon: base.TypeCommon, Kind: prog.BufferFilename, - NoZ: t.Ident == stringnoz, + NoZ: t.Ident == stringnoz || t.Ident == stringnozescapes, } } subkind := "" @@ -575,7 +576,7 @@ var typeString = &typeDesc{ Kind: prog.BufferString, SubKind: subkind, Values: vals, - NoZ: t.Ident == stringnoz, + NoZ: t.Ident == stringnoz || t.Ident == stringnozescapes, } }, } @@ -591,6 +592,16 @@ func (comp *compiler) genStrings(t *ast.Type, args []*ast.Type) []string { } if t.Ident == stringnoz { return vals + } else if t.Ident == stringnozescapes { + for i := range vals { + unquote, err := strconv.Unquote(`"` + vals[i] + `"`) + if err != nil { + comp.error(args[0].Pos, fmt.Sprintf("unable to unquote stringnozescapes %q: %v", vals[i], err)) + } else { + vals[i] = unquote + } + } + return vals } var size uint64 if len(args) > 1 { |
