From 8dbc476375f28a783591a57a162fb8d8c84c5c14 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Thu, 4 May 2017 12:44:06 +1000 Subject: [PATCH] Parse a "ptr T" type. --- lang/ast/ast.go | 25 +++++++++++++++++++++---- lang/parse/parse.go | 31 ++++++++++++++++++++++++++++--- lang/token/list.go | 2 ++ 3 files changed, 51 insertions(+), 7 deletions(-) diff --git a/lang/ast/ast.go b/lang/ast/ast.go index 8539a6b1..aecf1ddd 100644 --- a/lang/ast/ast.go +++ b/lang/ast/ast.go @@ -28,14 +28,30 @@ const ( // - RHS: Right KAssign - // KParam is a "name package.type" parameter: + // KParam is a "name type" parameter: // - ID0: name - // - ID1: package (optional) - // - ID2: type + // - RHS: KParam + // KType is a type, such as "u32", "pkg.foo", "ptr T" or "[8] T". + // - ID0: <0|IDPtr> + // - ID1: <0|package name> + // - ID2: <0|type name> + // - LHS: TODO. + // - RHS: + // + // A zero ID0 means an undecorated type like "u32" or "pkg.foo". ID1 is + // zero or "pkg". ID2 is "u32" or "foo". RHS is nil. + // + // A non-zero ID0 means a decorated type like "ptr T" or "[8] T". ID0 is + // the decoration. LHS is nil or "8". RHS is the T. ID1 and ID2 are zero. + // + // TODO: LHS for arrays (and slices?), such as "[8] T". + // TODO: refinements, such as "u32~[:4096]". + KType + // KFunc is "func ID0.ID1 (List0) (List1) { List2 }": - // - ID0: receiver (optional) + // - ID0: <0|receiver> // - ID1: name // - List0: in-parameters // - List1: out-parameters @@ -63,6 +79,7 @@ var kindStrings = [...]string{ KInvalid: "KInvalid", KLiteral: "KLiteral", KParam: "KParam", + KType: "KType", } type Flags uint32 diff --git a/lang/parse/parse.go b/lang/parse/parse.go index 79cc040e..92845b87 100644 --- a/lang/parse/parse.go +++ b/lang/parse/parse.go @@ -167,17 +167,42 @@ func (p *parser) parseParamList() ([]*a.Node, error) { } func (p *parser) parseParam() (*a.Node, error) { - id0, err := p.parseIdent() + id, err := p.parseIdent() if err != nil { return nil, err } - id1, id2, err := p.parseQualifiedIdent() + rhs, err := p.parseType() if err != nil { return nil, err } return &a.Node{ Kind: a.KParam, - ID0: id0, + ID0: id, + RHS: rhs, + }, nil +} + +func (p *parser) parseType() (*a.Node, error) { + switch p.peekID() { + case t.IDPtr: + p.src = p.src[1:] + rhs, err := p.parseType() + if err != nil { + return nil, err + } + return &a.Node{ + Kind: a.KType, + ID0: t.IDPtr, + RHS: rhs, + }, nil + } + + id1, id2, err := p.parseQualifiedIdent() + if err != nil { + return nil, err + } + return &a.Node{ + Kind: a.KType, ID1: id1, ID2: id2, }, nil diff --git a/lang/token/list.go b/lang/token/list.go index 8eb0512c..28f04b7f 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -132,6 +132,7 @@ const ( // TODO: sort these by name, when the list has stabilized. IDFunc = ID(0xA0<> idShift: {"as", IDAs}, IDFunc >> idShift: {"func", IDFunc}, + IDPtr >> idShift: {"ptr", IDPtr}, IDFalse >> idShift: {"false", IDFalse}, IDTrue >> idShift: {"true", IDTrue},