diff options
| author | Dmitry Vyukov <dvyukov@google.com> | 2018-04-29 10:56:48 +0200 |
|---|---|---|
| committer | Dmitry Vyukov <dvyukov@google.com> | 2018-04-29 12:04:22 +0200 |
| commit | 1e85f7b9af8e29f06a22eb1ff325de2a40072738 (patch) | |
| tree | fbbe16d0eb26f84ad22b82e0925420b1177798e2 /pkg/ast/parser.go | |
| parent | c7f6891ca7dc623ed77840580cb9270c61d200ee (diff) | |
pkg/ast: support char constants
Frequently it's useful to do something like:
int8['a':'z']
punctuation = ',', '-', ':'
Diffstat (limited to 'pkg/ast/parser.go')
| -rw-r--r-- | pkg/ast/parser.go | 15 |
1 files changed, 9 insertions, 6 deletions
diff --git a/pkg/ast/parser.go b/pkg/ast/parser.go index fb3327223..e4c5d8742 100644 --- a/pkg/ast/parser.go +++ b/pkg/ast/parser.go @@ -413,7 +413,7 @@ func (p *parser) parseType() *Type { switch p.tok { case tokInt: allowColon = true - arg.Value, arg.ValueHex = p.parseIntValue() + arg.Value, arg.valueFmt = p.parseIntValue() case tokIdent: allowColon = true arg.Ident = p.lit @@ -429,7 +429,7 @@ func (p *parser) parseType() *Type { arg.Pos2 = p.pos switch p.tok { case tokInt: - arg.Value2, arg.Value2Hex = p.parseIntValue() + arg.Value2, arg.value2Fmt = p.parseIntValue() case tokIdent: arg.Ident2 = p.lit default: @@ -479,7 +479,7 @@ func (p *parser) parseInt() *Int { } switch p.tok { case tokInt: - i.Value, i.ValueHex = p.parseIntValue() + i.Value, i.valueFmt = p.parseIntValue() case tokIdent: i.Ident = p.lit default: @@ -489,13 +489,16 @@ func (p *parser) parseInt() *Int { return i } -func (p *parser) parseIntValue() (uint64, bool) { +func (p *parser) parseIntValue() (uint64, intFmt) { + if p.lit[0] == '\'' { + return uint64(p.lit[1]), intFmtChar + } if v, err := strconv.ParseUint(p.lit, 10, 64); err == nil { - return v, false + return v, intFmtDec } if len(p.lit) > 2 && p.lit[0] == '0' && p.lit[1] == 'x' { if v, err := strconv.ParseUint(p.lit[2:], 16, 64); err == nil { - return v, true + return v, intFmtHex } } panic(fmt.Sprintf("scanner returned bad integer %q", p.lit)) |
