From eea28fee309680462752ef7709f1a94178dce44d Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Fri, 10 May 2019 18:02:54 +0200 Subject: pkg/compiler: support complex len targets This change adds compiler support for complex path expressions in len targets. E.g. it allows to refer to a sibling field as len[parent_struct:field:another_field]. See the docs change for details. This is just a compiler change. The feature is not yet supported by the prog package. --- pkg/ast/parser.go | 28 +++++++++++++++------------- pkg/ast/testdata/all.txt | 3 ++- 2 files changed, 17 insertions(+), 14 deletions(-) (limited to 'pkg/ast') diff --git a/pkg/ast/parser.go b/pkg/ast/parser.go index b68a1a8a2..b8d22fd88 100644 --- a/pkg/ast/parser.go +++ b/pkg/ast/parser.go @@ -424,20 +424,22 @@ func (p *parser) parseType() *Type { p.expect(tokInt, tokIdent, tokString) } p.next() - if allowColon && p.tryConsume(tokColon) { - col := &Type{ - Pos: p.pos, - } - switch p.tok { - case tokInt: - col.Value, col.ValueFmt = p.parseIntValue() - case tokIdent: - col.Ident = p.lit - default: - p.expect(tokInt, tokIdent) + if allowColon { + for p.tryConsume(tokColon) { + col := &Type{ + Pos: p.pos, + } + switch p.tok { + case tokInt: + col.Value, col.ValueFmt = p.parseIntValue() + case tokIdent: + col.Ident = p.lit + default: + p.expect(tokInt, tokIdent) + } + arg.Colon = append(arg.Colon, col) + p.next() } - arg.Colon = append(arg.Colon, col) - p.next() } arg.Args = p.parseTypeList() return arg diff --git a/pkg/ast/testdata/all.txt b/pkg/ast/testdata/all.txt index e5a1839dd..392796254 100644 --- a/pkg/ast/testdata/all.txt +++ b/pkg/ast/testdata/all.txt @@ -22,11 +22,12 @@ str_flags4 = "string", 42 ### unexpected int, expecting string call(foo ,int32 , bar int32) ### unexpected ',', expecting int, identifier, string call(foo int32:"bar") ### unexpected string, expecting int, identifier +call(a int32, b len[a:"bar"]) ### unexpected string, expecting int, identifier define FOO `bar` define FOO `bar ### C expression is not terminated -foo(x int32[1:2:3, opt]) ### unexpected ':', expecting ']' +foo(x int32[1:2:3, opt]) foo2(x int32[1[2]:2]) ### unexpected ':', expecting ']' s0 { -- cgit mrf-deployment