1
0
mirror of https://github.com/golang/go synced 2024-11-22 17:04:40 -07:00

snapshot of today

(little progress with interface printing, but now shows a
list of exported function names)

R=r
OCL=26082
CL=26082
This commit is contained in:
Robert Griesemer 2009-03-10 18:20:08 -07:00
parent 915f176f7f
commit 61815b8316
4 changed files with 41 additions and 35 deletions

View File

@ -469,7 +469,7 @@ type (
}; };
FuncDecl struct { FuncDecl struct {
Pos_ int; // position of "func" Pos int; // position of "func"
Recv *Field; Recv *Field;
Ident *Ident; Ident *Ident;
Sig *Signature; Sig *Signature;

View File

@ -8,8 +8,7 @@
// //
// A client may parse the entire program (ParseProgram), only the package // A client may parse the entire program (ParseProgram), only the package
// clause (ParsePackageClause), or the package clause and the import // clause (ParsePackageClause), or the package clause and the import
// declarations (ParseImportDecls). The resulting AST represents the part // declarations (ParseImportDecls).
// of the program that is parsed.
// //
package Parser package Parser
@ -70,23 +69,11 @@ type Parser struct {
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Helper functions // Helper functions
func unimplemented() {
panic("unimplemented");
}
func unreachable() { func unreachable() {
panic("unreachable"); panic("unreachable");
} }
func assert(pred bool) {
if !pred {
panic("assertion failed");
}
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Parsing support // Parsing support
@ -178,13 +165,6 @@ func (P *Parser) expect(tok int) {
} }
func (P *Parser) OptSemicolon() {
if P.tok == token.SEMICOLON {
P.next();
}
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Common productions // Common productions
@ -194,7 +174,6 @@ func (P *Parser) parseStatement() ast.Stat;
func (P *Parser) parseDeclaration() ast.Decl; func (P *Parser) parseDeclaration() ast.Decl;
// If scope != nil, lookup identifier in scope. Otherwise create one.
func (P *Parser) parseIdent() *ast.Ident { func (P *Parser) parseIdent() *ast.Ident {
if P.trace { if P.trace {
defer un(trace(P, "Ident")); defer un(trace(P, "Ident"));
@ -662,7 +641,9 @@ func (P *Parser) parseStructType() ast.Expr {
break; break;
} }
} }
P.OptSemicolon(); if P.tok == token.SEMICOLON {
P.next();
}
end = P.pos; end = P.pos;
P.expect(token.RBRACE); P.expect(token.RBRACE);
@ -812,9 +793,8 @@ func (P *Parser) parseStringLit() ast.Expr {
defer un(trace(P, "StringLit")); defer un(trace(P, "StringLit"));
} }
assert(P.tok == token.STRING);
var x ast.Expr = &ast.BasicLit{P.pos, P.tok, P.val}; var x ast.Expr = &ast.BasicLit{P.pos, P.tok, P.val};
P.next(); P.expect(token.STRING); // always satisfied
for P.tok == token.STRING { for P.tok == token.STRING {
y := &ast.BasicLit{P.pos, P.tok, P.val}; y := &ast.BasicLit{P.pos, P.tok, P.val};
@ -1605,7 +1585,9 @@ func (P *Parser) parseImportDecls() *vector.Vector {
list := vector.New(0); list := vector.New(0);
for P.tok == token.IMPORT { for P.tok == token.IMPORT {
list.Push(P.parseDecl(token.IMPORT)); list.Push(P.parseDecl(token.IMPORT));
P.OptSemicolon(); if P.tok == token.SEMICOLON {
P.next();
}
} }
return list; return list;
@ -1651,7 +1633,9 @@ func (P *Parser) ParseProgram() *ast.Program {
list := P.parseImportDecls(); list := P.parseImportDecls();
for P.tok != token.EOF { for P.tok != token.EOF {
list.Push(P.parseDeclaration()); list.Push(P.parseDeclaration());
P.OptSemicolon(); if P.tok == token.SEMICOLON {
P.next();
}
} }
// convert list // convert list

View File

@ -16,6 +16,8 @@ import (
"token"; "token";
"ast"; "ast";
"template"; "template";
"utf8";
"unicode";
SymbolTable "symboltable"; SymbolTable "symboltable";
) )
@ -1017,7 +1019,7 @@ func (P *Printer) DoVarDecl(d *ast.VarDecl) {
func (P *Printer) funcDecl(d *ast.FuncDecl, with_body bool) { func (P *Printer) funcDecl(d *ast.FuncDecl, with_body bool) {
P.Token(d.Pos_, token.FUNC); P.Token(d.Pos, token.FUNC);
P.separator = blank; P.separator = blank;
if recv := d.Recv; recv != nil { if recv := d.Recv; recv != nil {
// method: print receiver // method: print receiver
@ -1079,14 +1081,28 @@ func (P *Printer) Decl(d ast.Decl) {
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Interface // Package interface
// TODO this should be an AST method
func isExported(name *ast.Ident) bool {
ch, len := utf8.DecodeRuneInString(name.Str, 0);
return unicode.IsUpper(ch);
}
func (P *Printer) Interface(p *ast.Program) { func (P *Printer) Interface(p *ast.Program) {
for i := 0; i < len(p.Decls); i++ { for i := 0; i < len(p.Decls); i++ {
decl := p.Decls[i]; switch d := p.Decls[i].(type) {
// TODO use type switch case *ast.FuncDecl:
if fun, is_fun := decl.(*ast.FuncDecl); is_fun { if isExported(d.Ident) {
P.funcDecl(fun, false); P.Printf("<h2>%s</h2>\n", d.Ident.Str);
/*
P.Printf("<p><code>");
P.funcDecl(d, false);
P.String(0, "");
P.Printf("</code></p>");
*/
}
} }
} }
} }

View File

@ -1,5 +1,11 @@
<h1><!--PACKAGE--></h1> <h1>package <!--PACKAGE--></h1>
<!--INTERFACE-->
<hr />
<h1>package <!--PACKAGE--></h1>
<pre> <pre>
<!--BODY--> <!--BODY-->