p.expect(token.LPAREN);
var typ ast.Expr;
if p.tok == token.TYPE {
- // special case for type switch
- typ = &ast.Ident{p.pos, "type"};
+ // type switch: typ == nil
p.next();
} else {
typ = p.parseType();
case *ast.SelectorExpr:
case *ast.IndexExpr:
case *ast.TypeAssertExpr:
+ if t.Type == nil {
+ // the form X.(type) is only allowed in type switch expressions
+ p.errorExpected(x.Pos(), "expression");
+ x = &ast.BadExpr{x.Pos()};
+ }
case *ast.CallExpr:
case *ast.StarExpr:
case *ast.UnaryExpr:
}
-func (p *parser) isExpr(s ast.Stmt) bool {
- if s == nil {
- return true;
- }
- dummy, isExpr := s.(*ast.ExprStmt);
- return isExpr;
-}
-
-
func (p *parser) makeExpr(s ast.Stmt) ast.Expr {
if s == nil {
return nil;
}
pos := p.expect(token.IF);
- s1, s2, dummy := p.parseControlClause(false);
+ s1, s2, _ := p.parseControlClause(false);
body := p.parseBlockStmt();
var else_ ast.Stmt;
if p.tok == token.ELSE {
}
+func (p *parser) parseTypeList() []ast.Expr {
+ if p.trace {
+ defer un(trace(p, "TypeList"));
+ }
+
+ list := vector.New(0);
+ list.Push(p.parseType());
+ for p.tok == token.COMMA {
+ p.next();
+ list.Push(p.parseType());
+ }
+
+ // convert list
+ exprs := make([]ast.Expr, list.Len());
+ for i := 0; i < list.Len(); i++ {
+ exprs[i] = list.At(i).(ast.Expr);
+ }
+
+ return exprs;
+}
+
+
func (p *parser) parseTypeCaseClause() *ast.TypeCaseClause {
if p.trace {
defer un(trace(p, "TypeCaseClause"));
// TypeSwitchCase
pos := p.pos;
- var typ ast.Expr;
+ var types []ast.Expr;
if p.tok == token.CASE {
p.next();
- typ = p.parseType();
+ types = p.parseTypeList();
} else {
p.expect(token.DEFAULT);
}
colon := p.expect(token.COLON);
body := p.parseStmtList();
- return &ast.TypeCaseClause{pos, typ, colon, body};
+ return &ast.TypeCaseClause{pos, types, colon, body};
+}
+
+
+func isExprSwitch(s ast.Stmt) bool {
+ if s == nil {
+ return true;
+ }
+ if e, ok := s.(*ast.ExprStmt); ok {
+ if a, ok := e.X.(*ast.TypeAssertExpr); ok {
+ return a.Type != nil; // regular type assertion
+ }
+ return true;
+ }
+ return false;
}
}
pos := p.expect(token.SWITCH);
- s1, s2, dummy := p.parseControlClause(false);
+ s1, s2, _ := p.parseControlClause(false);
- if p.isExpr(s2) {
- // expression switch
+ if isExprSwitch(s2) {
lbrace := p.expect(token.LBRACE);
cases := vector.New(0);
for p.tok == token.CASE || p.tok == token.DEFAULT {
UseSpaces; // use spaces instead of tabs for indentation and alignment
OptCommas; // print optional commas
OptSemis; // print optional semicolons
+ Reverse; // print top-level declarations in reverse order without forward-declarations
)
case *ast.TypeAssertExpr:
p.expr1(x.X, token.HighestPrec);
p.print(token.PERIOD, token.LPAREN);
- p.expr(x.Type);
+ if x.Type != nil {
+ p.expr(x.Type);
+ } else {
+ p.print(token.TYPE);
+ }
p.print(token.RPAREN);
case *ast.IndexExpr:
p.expr(x.Elt);
case *ast.StructType:
+ if x.Fields == nil && p.mode & Reverse != 0 && p.level == 0 {
+ // omit top-level forward declarations in reverse mode
+ return true;
+ }
p.print(token.STRUCT);
optSemi = p.fieldList(x.Lbrace, x.Fields, x.Rbrace, false);
p.signature(x.Params, x.Results);
case *ast.InterfaceType:
+ if x.Methods == nil && p.mode & Reverse != 0 && p.level == 0 {
+ // omit top-level forward declarations in reverse mode
+ return true;
+ }
p.print(token.INTERFACE);
optSemi = p.fieldList(x.Lbrace, x.Methods, x.Rbrace, true);
optSemi = true;
case *ast.TypeCaseClause:
- if s.Type != nil {
+ if s.Types != nil {
p.print(token.CASE, blank);
- p.expr(s.Type);
+ p.exprList(s.Types);
} else {
p.print(token.DEFAULT);
}
p.print(d.Lparen, token.LPAREN);
if len(d.Specs) > 0 {
p.print(+1, newline);
- for i, s := range d.Specs {
- if i > 0 {
- p.print(token.SEMICOLON);
- p.lineComment(comment);
- p.print(newline);
+ if p.mode & Reverse != 0 && p.level == 0 {
+ for i := len(d.Specs)-1; i >= 0; i-- {
+ s := d.Specs[i];
+ if i < len(d.Specs)-1 {
+ p.print(token.SEMICOLON);
+ p.lineComment(comment);
+ p.print(newline);
+ }
+ comment, optSemi = p.spec(s);
+ }
+ } else {
+ for i, s := range d.Specs {
+ if i > 0 {
+ p.print(token.SEMICOLON);
+ p.lineComment(comment);
+ p.print(newline);
+ }
+ comment, optSemi = p.spec(s);
}
- comment, optSemi = p.spec(s);
}
if p.optSemis() {
p.print(token.SEMICOLON);
}
case *ast.FuncDecl:
+ if d.Body == nil && p.mode & Reverse != 0 {
+ // omit forward declarations in reverse mode
+ break;
+ }
p.leadComment(d.Doc);
p.print(lineTag(d.Pos()), token.FUNC, blank);
if recv := d.Recv; recv != nil {
p.print(src.Pos(), token.PACKAGE, blank);
p.expr(src.Name);
- for _, d := range src.Decls {
- p.print(newline, newline);
- comment, _ := p.decl(d);
- if p.optSemis() {
- p.print(token.SEMICOLON);
+ if p.mode & Reverse != 0 {
+ for i := len(src.Decls)-1; i >= 0; i-- {
+ d := src.Decls[i];
+ p.print(newline, newline);
+ comment, _ := p.decl(d);
+ if p.optSemis() {
+ p.print(token.SEMICOLON);
+ }
+ p.lineComment(comment);
+ }
+ } else {
+ for _, d := range src.Decls {
+ p.print(newline, newline);
+ comment, _ := p.decl(d);
+ if p.optSemis() {
+ p.print(token.SEMICOLON);
+ }
+ p.lineComment(comment);
}
- p.lineComment(comment);
}
p.print(newline);
comment, _ := p.decl(n);
p.lineComment(comment); // no newline at end
case *ast.File:
- p.comment = n.Comments;
+ if mode & Reverse == 0 {
+ // don't print comments in reverse mode
+ p.comment = n.Comments;
+ }
p.file(n);
default:
p.errors <- os.NewError("unsupported node type");