diff options
| author | Dmitry Vyukov <dvyukov@google.com> | 2019-05-10 17:15:29 +0200 |
|---|---|---|
| committer | Dmitry Vyukov <dvyukov@google.com> | 2019-05-14 19:28:01 +0200 |
| commit | 1886b2a4811a4d9adbfc509505a095848cc655eb (patch) | |
| tree | 8ab666c6f9ea59912d911abd5639480eae534eda /pkg/ast | |
| parent | 354b388e08761dabb99236aa73a24e9b861b6e5f (diff) | |
pkg/ast: refactor COLON handling
This prepared for handling of bytesize[parent:foo:bar] expressions
by allowing multiple identifiers after colon.
No functional changes for now, just preparation for storing more
than one identifier after colon.
Diffstat (limited to 'pkg/ast')
| -rw-r--r-- | pkg/ast/ast.go | 11 | ||||
| -rw-r--r-- | pkg/ast/clone.go | 6 | ||||
| -rw-r--r-- | pkg/ast/format.go | 9 | ||||
| -rw-r--r-- | pkg/ast/parser.go | 10 | ||||
| -rw-r--r-- | pkg/ast/testdata/all.txt | 1 |
5 files changed, 14 insertions, 23 deletions
diff --git a/pkg/ast/ast.go b/pkg/ast/ast.go index 1e1e97221..b692f8633 100644 --- a/pkg/ast/ast.go +++ b/pkg/ast/ast.go @@ -199,13 +199,10 @@ type Type struct { Ident string String string HasString bool - // Part after COLON (for ranges and bitfields). - HasColon bool - Pos2 Pos - Value2 uint64 - Value2Fmt IntFmt - Ident2 string - Args []*Type + // Parts after COLON (for ranges and bitfields). + Colon []*Type + // Sub-types in []. + Args []*Type } func (n *Type) Info() (Pos, string, string) { diff --git a/pkg/ast/clone.go b/pkg/ast/clone.go index 13a603ba8..a594e41f6 100644 --- a/pkg/ast/clone.go +++ b/pkg/ast/clone.go @@ -155,11 +155,7 @@ func (n *Type) Clone() Node { Ident: n.Ident, String: n.String, HasString: n.HasString, - HasColon: n.HasColon, - Pos2: n.Pos2, - Value2: n.Value2, - Value2Fmt: n.Value2Fmt, - Ident2: n.Ident2, + Colon: cloneTypes(n.Colon), Args: cloneTypes(n.Args), } } diff --git a/pkg/ast/format.go b/pkg/ast/format.go index 13cd26e2c..c3d931706 100644 --- a/pkg/ast/format.go +++ b/pkg/ast/format.go @@ -176,13 +176,8 @@ func fmtType(t *Type) string { default: v = FormatInt(t.Value, t.ValueFmt) } - if t.HasColon { - switch { - case t.Ident2 != "": - v += fmt.Sprintf(":%v", t.Ident2) - default: - v += fmt.Sprintf(":%v", FormatInt(t.Value2, t.Value2Fmt)) - } + for _, c := range t.Colon { + v += ":" + fmtType(c) } v += fmtTypeList(t.Args) return v diff --git a/pkg/ast/parser.go b/pkg/ast/parser.go index bd9d33657..b68a1a8a2 100644 --- a/pkg/ast/parser.go +++ b/pkg/ast/parser.go @@ -425,16 +425,18 @@ func (p *parser) parseType() *Type { } p.next() if allowColon && p.tryConsume(tokColon) { - arg.HasColon = true - arg.Pos2 = p.pos + col := &Type{ + Pos: p.pos, + } switch p.tok { case tokInt: - arg.Value2, arg.Value2Fmt = p.parseIntValue() + col.Value, col.ValueFmt = p.parseIntValue() case tokIdent: - arg.Ident2 = p.lit + col.Ident = p.lit default: p.expect(tokInt, tokIdent) } + arg.Colon = append(arg.Colon, col) p.next() } arg.Args = p.parseTypeList() diff --git a/pkg/ast/testdata/all.txt b/pkg/ast/testdata/all.txt index 6e4e72d2c..e5a1839dd 100644 --- a/pkg/ast/testdata/all.txt +++ b/pkg/ast/testdata/all.txt @@ -27,6 +27,7 @@ define FOO `bar` define FOO `bar ### C expression is not terminated foo(x int32[1:2:3, opt]) ### unexpected ':', expecting ']' +foo2(x int32[1[2]:2]) ### unexpected ':', expecting ']' s0 { f0 string[""] |
