From 6b52293f4defa6b45b564d037fd641be5d6d0e0e Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Wed, 10 Jan 2018 16:13:34 +0100 Subject: pkg/compiler: support type templates Netlink descriptions contain tons of code duplication, and need much more for proper descriptions. Introduce type templates to simplify writing such descriptions and remove code duplication. Note: type templates are experimental, have poor error handling and are subject to change. Type templates can be declared as follows: ``` type buffer[DIR] ptr[DIR, array[int8]] type fileoff[BASE] BASE type nlattr[TYPE, PAYLOAD] { nla_len len[parent, int16] nla_type const[TYPE, int16] payload PAYLOAD } [align_4] ``` and later used as follows: ``` syscall(a buffer[in], b fileoff[int64], c ptr[in, nlattr[FOO, int32]]) ``` --- pkg/ast/parser.go | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) (limited to 'pkg/ast/parser.go') diff --git a/pkg/ast/parser.go b/pkg/ast/parser.go index db211ab2a..bd5650ad5 100644 --- a/pkg/ast/parser.go +++ b/pkg/ast/parser.go @@ -251,11 +251,34 @@ func (p *parser) parseResource() *Resource { func (p *parser) parseTypeDef() *TypeDef { pos0 := p.pos name := p.parseIdent() - typ := p.parseType() + var typ *Type + var str *Struct + var args []*Ident + p.expect(tokLBrack, tokIdent) + if p.tryConsume(tokLBrack) { + args = append(args, p.parseIdent()) + for p.tryConsume(tokComma) { + args = append(args, p.parseIdent()) + } + p.consume(tokRBrack) + if p.tok == tokLBrace || p.tok == tokLBrack { + name := &Ident{ + Pos: pos0, + Name: "", + } + str = p.parseStruct(name) + } else { + typ = p.parseType() + } + } else { + typ = p.parseType() + } return &TypeDef{ - Pos: pos0, - Name: name, - Type: typ, + Pos: pos0, + Name: name, + Args: args, + Type: typ, + Struct: str, } } -- cgit mrf-deployment