cmd/compile/internal/syntax: support for alias declarations

Permits parsing of alias declarations with -newparser

	const/type/var/func T => p.T

but the compiler will reject it with an error. For now this
also accepts

	type T = p.T

so we can experiment with a type-alias only scenario.

- renamed _Arrow token to _Larrow (<-)
- introduced _Rarrow token (=>)
- introduced AliasDecl node
- extended scanner to accept _Rarrow
- extended parser and printer to handle alias declarations

Change-Id: I0170d10a87df8255db9186d466b6fd405228c38e
Reviewed-on: https://go-review.googlesource.com/29355
Run-TryBot: Robert Griesemer <gri@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Robert Griesemer 2016-09-15 17:40:26 -07:00
parent 28ed2b0cd9
commit 32db3f2756
7 changed files with 147 additions and 42 deletions

View file

@ -25,14 +25,21 @@ func (n *node) Line() uint32 {
return n.line
}
// TODO(gri) clean up init/initFrom once we have a good file pos story
func (n *node) init(p *parser) {
n.pos = uint32(p.pos)
n.line = uint32(p.line)
}
func (n *node) initFrom(a *node) {
n.pos = a.pos
n.line = a.line
}
// ----------------------------------------------------------------------------
// Files
// package PkgName; DeclList[0], DeclList[1], ...
type File struct {
PkgName *Name
DeclList []Decl
@ -49,6 +56,8 @@ type (
aDecl()
}
// Path
// LocalPkgName Path
ImportDecl struct {
LocalPkgName *Name // including "."; nil means no rename present
Path *BasicLit
@ -56,6 +65,18 @@ type (
decl
}
// Name => Orig
AliasDecl struct {
Tok token // Const, Type, Var, or Func
Name *Name
Orig Expr
Group *Group // nil means not part of a group
decl
}
// NameList
// NameList = Values
// NameList Type = Values
ConstDecl struct {
NameList []*Name
Type Expr // nil means no type
@ -64,13 +85,18 @@ type (
decl
}
// Name Type
TypeDecl struct {
Name *Name
Type Expr
Alias bool
Group *Group // nil means not part of a group
decl
}
// NameList Type
// NameList Type = Values
// NameList = Values
VarDecl struct {
NameList []*Name
Type Expr // nil means no type
@ -79,6 +105,10 @@ type (
decl
}
// func Name Type { Body }
// func Name Type
// func Receiver Name Type { Body }
// func Receiver Name Type
FuncDecl struct {
Attr map[string]bool // go:attr map
Recv *Field // nil means regular function
@ -418,6 +448,8 @@ func (simpleStmt) aSimpleStmt() {}
// ----------------------------------------------------------------------------
// Comments
// TODO(gri) Consider renaming to CommentPos, CommentPlacement, etc.
// Kind = Above doesn't make much sense.
type CommentKind uint
const (