Commit 2d543d0c authored by Robert Griesemer's avatar Robert Griesemer

Adjustements related to AST changes.

R=r
OCL=27026
CL=27028
parent 3ba69bf0
...@@ -1112,11 +1112,7 @@ func (P *Printer) DoBadDecl(d *ast.BadDecl) { ...@@ -1112,11 +1112,7 @@ func (P *Printer) DoBadDecl(d *ast.BadDecl) {
} }
func (P *Printer) DoImportDecl(d *ast.ImportDecl) { func (P *Printer) importSpec(d *ast.ImportSpec) {
if d.Pos().Offset > 0 {
P.Token(d.Pos(), token.IMPORT);
P.separator = blank;
}
if d.Name != nil { if d.Name != nil {
P.Expr(d.Name); P.Expr(d.Name);
} else { } else {
...@@ -1124,19 +1120,12 @@ func (P *Printer) DoImportDecl(d *ast.ImportDecl) { ...@@ -1124,19 +1120,12 @@ func (P *Printer) DoImportDecl(d *ast.ImportDecl) {
} }
P.separator = tab; P.separator = tab;
// TODO fix for longer package names // TODO fix for longer package names
if len(d.Path) > 1 {
panic();
}
P.HtmlPackageName(d.Path[0].Pos(), string(d.Path[0].Lit)); P.HtmlPackageName(d.Path[0].Pos(), string(d.Path[0].Lit));
P.newlines = 2; P.newlines = 2;
} }
func (P *Printer) DoConstDecl(d *ast.ConstDecl) { func (P *Printer) valueSpec(d *ast.ValueSpec) {
if d.Pos().Offset > 0 {
P.Token(d.Pos(), token.CONST);
P.separator = blank;
}
P.Idents(d.Names, P.full); P.Idents(d.Names, P.full);
if d.Type != nil { if d.Type != nil {
P.separator = blank; // TODO switch to tab? (indentation problem with structs) P.separator = blank; // TODO switch to tab? (indentation problem with structs)
...@@ -1152,11 +1141,7 @@ func (P *Printer) DoConstDecl(d *ast.ConstDecl) { ...@@ -1152,11 +1141,7 @@ func (P *Printer) DoConstDecl(d *ast.ConstDecl) {
} }
func (P *Printer) DoTypeDecl(d *ast.TypeDecl) { func (P *Printer) typeSpec(d *ast.TypeSpec) {
if d.Pos().Offset > 0 {
P.Token(d.Pos(), token.TYPE);
P.separator = blank;
}
P.Expr(d.Name); P.Expr(d.Name);
P.separator = blank; // TODO switch to tab? (but indentation problem with structs) P.separator = blank; // TODO switch to tab? (but indentation problem with structs)
P.Expr(d.Type); P.Expr(d.Type);
...@@ -1164,24 +1149,43 @@ func (P *Printer) DoTypeDecl(d *ast.TypeDecl) { ...@@ -1164,24 +1149,43 @@ func (P *Printer) DoTypeDecl(d *ast.TypeDecl) {
} }
func (P *Printer) DoVarDecl(d *ast.VarDecl) { func (P *Printer) spec(d ast.Spec) {
if d.Pos().Offset > 0 { switch s := d.(type) {
P.Token(d.Pos(), token.VAR); case *ast.ImportSpec: P.importSpec(s);
P.separator = blank; case *ast.ValueSpec: P.valueSpec(s);
} case *ast.TypeSpec: P.typeSpec(s);
P.Idents(d.Names, P.full); default: panic("unreachable");
if d.Type != nil {
P.separator = blank; // TODO switch to tab? (indentation problem with structs)
P.Expr(d.Type);
//P.separator = P.Type(d.Type);
} }
if d.Values != nil { }
P.separator = tab;
P.Token(noPos, token.ASSIGN);
P.separator = blank; func (P *Printer) DoGenDecl(d *ast.GenDecl) {
P.Exprs(d.Values); P.Token(d.Pos(), d.Tok);
P.separator = blank;
if d.Lparen.Line > 0 {
// group of parenthesized declarations
P.state = opening_scope;
P.Token(d.Lparen, token.LPAREN);
if len(d.Specs) > 0 {
P.newlines = 1;
for i := 0; i < len(d.Specs); i++ {
if i > 0 {
P.separator = semicolon;
}
P.spec(d.Specs[i]);
P.newlines = 1;
}
}
P.state = closing_scope;
P.Token(d.Rparen, token.RPAREN);
P.opt_semi = true;
P.newlines = 2;
} else {
// single declaration
P.spec(d.Specs[0]);
} }
P.newlines = 2;
} }
...@@ -1209,30 +1213,6 @@ func (P *Printer) DoFuncDecl(d *ast.FuncDecl) { ...@@ -1209,30 +1213,6 @@ func (P *Printer) DoFuncDecl(d *ast.FuncDecl) {
} }
func (P *Printer) DoDeclList(d *ast.DeclList) {
P.Token(d.Pos(), d.Tok);
P.separator = blank;
// group of parenthesized declarations
P.state = opening_scope;
P.Token(noPos, token.LPAREN);
if len(d.List) > 0 {
P.newlines = 1;
for i := 0; i < len(d.List); i++ {
if i > 0 {
P.separator = semicolon;
}
P.Decl(d.List[i]);
P.newlines = 1;
}
}
P.state = closing_scope;
P.Token(d.Rparen, token.RPAREN);
P.opt_semi = true;
P.newlines = 2;
}
func (P *Printer) Decl(d ast.Decl) { func (P *Printer) Decl(d ast.Decl) {
d.Visit(P); d.Visit(P);
} }
......
...@@ -38,12 +38,10 @@ func hasExportedNames(names []*ast.Ident) bool { ...@@ -38,12 +38,10 @@ func hasExportedNames(names []*ast.Ident) bool {
} }
func hasExportedDecls(decl []ast.Decl) bool { func hasExportedSpecs(specs []ast.Spec) bool {
for i, d := range decl { for i, s := range specs {
switch t := d.(type) { // only called for []astSpec lists of *ast.ValueSpec
case *ast.ConstDecl: return hasExportedNames(s.(*ast.ValueSpec).Names);
return hasExportedNames(t.Names);
}
} }
return false; return false;
} }
...@@ -51,13 +49,8 @@ func hasExportedDecls(decl []ast.Decl) bool { ...@@ -51,13 +49,8 @@ func hasExportedDecls(decl []ast.Decl) bool {
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
type constDoc struct { type valueDoc struct {
decl *ast.DeclList; decl *ast.GenDecl; // len(decl.Specs) >= 1, and the element type is *ast.ValueSpec
}
type varDoc struct {
decl *ast.DeclList;
} }
...@@ -67,7 +60,7 @@ type funcDoc struct { ...@@ -67,7 +60,7 @@ type funcDoc struct {
type typeDoc struct { type typeDoc struct {
decl *ast.TypeDecl; decl *ast.GenDecl; // len(decl.Specs) == 1, and the element type is *ast.TypeSpec
factories map[string] *funcDoc; factories map[string] *funcDoc;
methods map[string] *funcDoc; methods map[string] *funcDoc;
} }
...@@ -76,9 +69,9 @@ type typeDoc struct { ...@@ -76,9 +69,9 @@ type typeDoc struct {
type PackageDoc struct { type PackageDoc struct {
name string; // package name name string; // package name
doc ast.Comments; // package documentation, if any doc ast.Comments; // package documentation, if any
consts *vector.Vector; // list of *ast.DeclList with Tok == token.CONST consts *vector.Vector; // list of *valueDoc
vars *vector.Vector; // list of *ast.DeclList with Tok == token.CONST
types map[string] *typeDoc; types map[string] *typeDoc;
vars *vector.Vector; // list of *valueDoc
funcs map[string] *funcDoc; funcs map[string] *funcDoc;
} }
...@@ -116,9 +109,10 @@ func (doc *PackageDoc) lookupTypeDoc(typ ast.Expr) *typeDoc { ...@@ -116,9 +109,10 @@ func (doc *PackageDoc) lookupTypeDoc(typ ast.Expr) *typeDoc {
} }
func (doc *PackageDoc) addType(typ *ast.TypeDecl) { func (doc *PackageDoc) addType(decl *ast.GenDecl) {
typ := decl.Specs[0].(*ast.TypeSpec);
name := string(typ.Name.Lit); name := string(typ.Name.Lit);
tdoc := &typeDoc{typ, make(map[string] *funcDoc), make(map[string] *funcDoc)}; tdoc := &typeDoc{decl, make(map[string] *funcDoc), make(map[string] *funcDoc)};
doc.types[name] = tdoc; doc.types[name] = tdoc;
} }
...@@ -160,41 +154,40 @@ func (doc *PackageDoc) addFunc(fun *ast.FuncDecl) { ...@@ -160,41 +154,40 @@ func (doc *PackageDoc) addFunc(fun *ast.FuncDecl) {
func (doc *PackageDoc) addDecl(decl ast.Decl) { func (doc *PackageDoc) addDecl(decl ast.Decl) {
switch d := decl.(type) { switch d := decl.(type) {
case *ast.ConstDecl: case *ast.GenDecl:
if hasExportedNames(d.Names) { if len(d.Specs) > 0 {
// TODO switch d.Tok {
} case token.IMPORT:
// ignore
case *ast.TypeDecl: case token.CONST:
if isExported(d.Name) { // constants are always handled as a group
doc.addType(d); if hasExportedSpecs(d.Specs) {
} doc.consts.Push(&valueDoc{d});
}
case *ast.VarDecl: case token.TYPE:
if hasExportedNames(d.Names) { // types are handled individually
// TODO for i, spec := range d.Specs {
s := spec.(*ast.TypeSpec);
if isExported(s.Name) {
// make a (fake) GenDecl node for this TypeSpec
// (we need to do this here - as opposed to just
// for printing - so we don't loose the GenDecl
// documentation)
var noPos token.Position;
doc.addType(&ast.GenDecl{d.Doc, d.Pos(), token.TYPE, noPos, []ast.Spec{s}, noPos});
}
}
case token.VAR:
// variables are always handled as a group
if hasExportedSpecs(d.Specs) {
doc.vars.Push(&valueDoc{d});
}
}
} }
case *ast.FuncDecl: case *ast.FuncDecl:
if isExported(d.Name) { if isExported(d.Name) {
doc.addFunc(d); doc.addFunc(d);
} }
case *ast.DeclList:
switch d.Tok {
case token.IMPORT, token.TYPE:
for i, decl := range d.List {
doc.addDecl(decl);
}
case token.CONST:
if hasExportedDecls(d.List) {
doc.consts.Push(&constDoc{d});
}
case token.VAR:
if hasExportedDecls(d.List) {
doc.consts.Push(&varDoc{d});
}
}
} }
} }
...@@ -214,7 +207,7 @@ func (doc *PackageDoc) AddProgram(prog *ast.Program) { ...@@ -214,7 +207,7 @@ func (doc *PackageDoc) AddProgram(prog *ast.Program) {
doc.doc = prog.Doc doc.doc = prog.Doc
} }
// add all declarations // add all exported declarations
for i, decl := range prog.Decls { for i, decl := range prog.Decls {
doc.addDecl(decl); doc.addDecl(decl);
} }
...@@ -381,18 +374,10 @@ func printComments(p *astPrinter.Printer, comment ast.Comments) { ...@@ -381,18 +374,10 @@ func printComments(p *astPrinter.Printer, comment ast.Comments) {
} }
func (c *constDoc) print(p *astPrinter.Printer) { func (c *valueDoc) print(p *astPrinter.Printer) {
printComments(p, c.decl.Doc); printComments(p, c.decl.Doc);
p.Printf("<pre>"); p.Printf("<pre>");
p.DoDeclList(c.decl); p.DoGenDecl(c.decl);
p.Printf("</pre>\n");
}
func (c *varDoc) print(p *astPrinter.Printer) {
printComments(p, c.decl.Doc);
p.Printf("<pre>");
p.DoDeclList(c.decl);
p.Printf("</pre>\n"); p.Printf("</pre>\n");
} }
...@@ -415,11 +400,12 @@ func (f *funcDoc) print(p *astPrinter.Printer, hsize int) { ...@@ -415,11 +400,12 @@ func (f *funcDoc) print(p *astPrinter.Printer, hsize int) {
func (t *typeDoc) print(p *astPrinter.Printer) { func (t *typeDoc) print(p *astPrinter.Printer) {
d := t.decl; d := t.decl;
p.Printf("<h2>type %s</h2>\n", string(d.Name.Lit)); s := d.Specs[0].(*ast.TypeSpec);
p.Printf("<h2>type %s</h2>\n", string(s.Name.Lit));
p.Printf("<p><pre>"); p.Printf("<p><pre>");
p.DoTypeDecl(d); p.DoGenDecl(d);
p.Printf("</pre></p>\n"); p.Printf("</pre></p>\n");
printComments(p, d.Doc); printComments(p, s.Doc);
// print associated methods, if any // print associated methods, if any
for name, m := range t.factories { for name, m := range t.factories {
...@@ -458,7 +444,7 @@ func (doc *PackageDoc) Print(writer io.Write) { ...@@ -458,7 +444,7 @@ func (doc *PackageDoc) Print(writer io.Write) {
fmt.Fprintln(writer, "<hr />"); fmt.Fprintln(writer, "<hr />");
fmt.Fprintln(writer, "<h2>Constants</h2>"); fmt.Fprintln(writer, "<h2>Constants</h2>");
for i := 0; i < doc.consts.Len(); i++ { for i := 0; i < doc.consts.Len(); i++ {
doc.consts.At(i).(*constDoc).print(&p); doc.consts.At(i).(*valueDoc).print(&p);
} }
} }
}, },
...@@ -477,7 +463,7 @@ func (doc *PackageDoc) Print(writer io.Write) { ...@@ -477,7 +463,7 @@ func (doc *PackageDoc) Print(writer io.Write) {
fmt.Fprintln(writer, "<hr />"); fmt.Fprintln(writer, "<hr />");
fmt.Fprintln(writer, "<h2>Variables</h2>"); fmt.Fprintln(writer, "<h2>Variables</h2>");
for i := 0; i < doc.vars.Len(); i++ { for i := 0; i < doc.vars.Len(); i++ {
doc.vars.At(i).(*varDoc).print(&p); doc.vars.At(i).(*valueDoc).print(&p);
} }
} }
}, },
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment