From 402a0dc87e7d51812a18fa76feeb46d66efda175 Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Sat, 6 Jan 2018 14:46:52 +0100 Subject: sys: support type aliases (aka typedefs) Complex types that are often repeated can be given short type aliases using the following syntax: ``` type identifier underlying_type ``` For example: ``` type signalno int32[0:65] type net_port proc[20000, 4, int16be] ``` Then, type alias can be used instead of the underlying type in any contexts. Underlying type needs to be described as if it's a struct field, that is, with the base type if it's required. However, type alias can be used as syscall arguments as well. Underlying types are currently restricted to integer types, `ptr`, `ptr64`, `const`, `flags` and `proc` types. --- pkg/ast/parser.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'pkg/ast/parser.go') diff --git a/pkg/ast/parser.go b/pkg/ast/parser.go index 9e1ab679e..db211ab2a 100644 --- a/pkg/ast/parser.go +++ b/pkg/ast/parser.go @@ -128,6 +128,9 @@ func (p *parser) parseTop() Node { return p.parseResource() case tokIdent: name := p.parseIdent() + if name.Name == "type" { + return p.parseTypeDef() + } switch p.tok { case tokLParen: return p.parseCall(name) @@ -245,6 +248,17 @@ func (p *parser) parseResource() *Resource { } } +func (p *parser) parseTypeDef() *TypeDef { + pos0 := p.pos + name := p.parseIdent() + typ := p.parseType() + return &TypeDef{ + Pos: pos0, + Name: name, + Type: typ, + } +} + func (p *parser) parseCall(name *Ident) *Call { c := &Call{ Pos: name.Pos, -- cgit mrf-deployment