]> Cypherpunks repositories - gostls13.git/commitdiff
snapshot of today
authorRobert Griesemer <gri@golang.org>
Wed, 11 Mar 2009 01:20:08 +0000 (18:20 -0700)
committerRobert Griesemer <gri@golang.org>
Wed, 11 Mar 2009 01:20:08 +0000 (18:20 -0700)
(little progress with interface printing, but now shows a
list of exported function names)

R=r
OCL=26082
CL=26082

usr/gri/pretty/ast.go
usr/gri/pretty/parser.go
usr/gri/pretty/printer.go
usr/gri/pretty/template.html

index e8540548874e4f367ebf82de56b4e04e4802dd2e..ec5af4b8c5d91bd973f1cc4adee5f0acfda5e355 100644 (file)
@@ -469,7 +469,7 @@ type (
        };
 
        FuncDecl struct {
-               Pos_ int;  // position of "func"
+               Pos int;  // position of "func"
                Recv *Field;
                Ident *Ident;
                Sig *Signature;
index 4712996d971de71c39e60424444273f523471d37..4d37c87adf437e49e34b1b787811e3917ab84bc9 100644 (file)
@@ -8,8 +8,7 @@
 //
 // A client may parse the entire program (ParseProgram), only the package
 // clause (ParsePackageClause), or the package clause and the import
-// declarations (ParseImportDecls). The resulting AST represents the part
-// of the program that is parsed.
+// declarations (ParseImportDecls).
 //
 package Parser
 
@@ -70,23 +69,11 @@ type Parser struct {
 // ----------------------------------------------------------------------------
 // Helper functions
 
-func unimplemented() {
-       panic("unimplemented");
-}
-
-
 func unreachable() {
        panic("unreachable");
 }
 
 
-func assert(pred bool) {
-       if !pred {
-               panic("assertion failed");
-       }
-}
-
-
 // ----------------------------------------------------------------------------
 // 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
 
@@ -194,7 +174,6 @@ func (P *Parser) parseStatement() ast.Stat;
 func (P *Parser) parseDeclaration() ast.Decl;
 
 
-// If scope != nil, lookup identifier in scope. Otherwise create one.
 func (P *Parser) parseIdent() *ast.Ident {
        if P.trace {
                defer un(trace(P, "Ident"));
@@ -662,7 +641,9 @@ func (P *Parser) parseStructType() ast.Expr {
                                break;
                        }
                }
-               P.OptSemicolon();
+               if P.tok == token.SEMICOLON {
+                       P.next();
+               }
 
                end = P.pos;
                P.expect(token.RBRACE);
@@ -812,9 +793,8 @@ func (P *Parser) parseStringLit() ast.Expr {
                defer un(trace(P, "StringLit"));
        }
 
-       assert(P.tok == token.STRING);
        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 {
                y := &ast.BasicLit{P.pos, P.tok, P.val};
@@ -1605,7 +1585,9 @@ func (P *Parser) parseImportDecls() *vector.Vector {
        list := vector.New(0);
        for P.tok == token.IMPORT {
                list.Push(P.parseDecl(token.IMPORT));
-               P.OptSemicolon();
+               if P.tok == token.SEMICOLON {
+                       P.next();
+               }
        }
 
        return list;
@@ -1651,7 +1633,9 @@ func (P *Parser) ParseProgram() *ast.Program {
        list := P.parseImportDecls();
        for P.tok != token.EOF {
                list.Push(P.parseDeclaration());
-               P.OptSemicolon();
+               if P.tok == token.SEMICOLON {
+                       P.next();
+               }
        }
 
        // convert list
index 7bb0d1c6cc39fea5a1fe701be6bac0d257b9ca43..54580f90ab6c57610fe6ccc3ce4af2c8eb655433 100644 (file)
@@ -16,6 +16,8 @@ import (
        "token";
        "ast";
        "template";
+       "utf8";
+       "unicode";
        SymbolTable "symboltable";
 )
 
@@ -1017,7 +1019,7 @@ func (P *Printer) DoVarDecl(d *ast.VarDecl) {
 
 
 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;
        if recv := d.Recv; recv != nil {
                // 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) {
        for i := 0; i < len(p.Decls); i++ {
-               decl := p.Decls[i];
-               // TODO use type switch
-               if fun, is_fun := decl.(*ast.FuncDecl); is_fun {
-                       P.funcDecl(fun, false);
+               switch d := p.Decls[i].(type) {
+               case *ast.FuncDecl:
+                       if isExported(d.Ident) {
+                               P.Printf("<h2>%s</h2>\n", d.Ident.Str);
+                               /*
+                               P.Printf("<p><code>");
+                               P.funcDecl(d, false);
+                               P.String(0, "");
+                               P.Printf("</code></p>");
+                               */
+                       }
                }
        }
 }
index 71126499b653510b31f430cba1d41dff57884b5f..05adcd1e2feb3981f461de3fe141170c540cc20d 100644 (file)
@@ -1,5 +1,11 @@
 
-<h1><!--PACKAGE--></h1>
+<h1>package <!--PACKAGE--></h1>
+
+<!--INTERFACE-->
+
+<hr />
+
+<h1>package <!--PACKAGE--></h1>
 
 <pre>
 <!--BODY-->