Commit b7585a31 authored by Robert Griesemer's avatar Robert Griesemer

weekly snapshot:

format.go:
- better error handling, indentation, support for defaults,
  environments for custom formatters, cleanups (more functionality, less code)

pretty.go:
- better comment printing using format.go

made test script more robust

TBR=r
DELTA=622  (175 added, 305 deleted, 142 changed)
OCL=28956
CL=28956
parent a343e5ce
...@@ -49,7 +49,7 @@ ast.Decl = ...@@ -49,7 +49,7 @@ ast.Decl =
// Comments // Comments
ast.Comment = ast.Comment =
Text:string "\n"; Text:string [Text:isMultiLineComment "\n"];
ast.Comments = ast.Comments =
{*}; {*};
...@@ -305,9 +305,13 @@ ast.BadDecl = ...@@ -305,9 +305,13 @@ ast.BadDecl =
ast.GenDecl = ast.GenDecl =
Doc Doc
Tok " " Tok " "
[Lparen:isValidPos "(" >> "\t" "\n"] ( Lparen:isValidPos
>> "\t" "(\n"
{Specs / ";\n"} {Specs / ";\n"}
[Rparen:isValidPos << "\n" ")"]; <<
"\n)"
| {Specs / ";\n"}
);
ast.FuncDecl = ast.FuncDecl =
"func " ["(" Recv ") "] Name Type:funcSignature "func " ["(" Recv ") "] Name Type:funcSignature
......
This diff is collapsed.
...@@ -6,12 +6,17 @@ package format ...@@ -6,12 +6,17 @@ package format
import ( import (
"format"; "format";
"io";
"testing"; "testing";
) )
func check(t *testing.T, form, expected string, args ...) { func check(t *testing.T, form, expected string, args ...) {
result := format.ParseOrDie(form, nil).Sprint(args); f, err := format.Parse(io.StringBytes(form), nil);
if err != nil {
panic(err.String());
}
result := f.Sprint(args);
if result != expected { if result != expected {
t.Errorf( t.Errorf(
"format : %s\nresult : `%s`\nexpected: `%s`\n\n", "format : %s\nresult : `%s`\nexpected: `%s`\n\n",
...@@ -39,7 +44,6 @@ func Test0(t *testing.T) { ...@@ -39,7 +44,6 @@ func Test0(t *testing.T) {
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// - default formatting of basic type int
// - formatting of a struct // - formatting of a struct
type T1 struct { type T1 struct {
...@@ -47,6 +51,7 @@ type T1 struct { ...@@ -47,6 +51,7 @@ type T1 struct {
} }
const F1 = const F1 =
`int = "%d";`
`format.T1 = "<" a ">";` `format.T1 = "<" a ">";`
func Test1(t *testing.T) { func Test1(t *testing.T) {
...@@ -56,7 +61,6 @@ func Test1(t *testing.T) { ...@@ -56,7 +61,6 @@ func Test1(t *testing.T) {
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// - formatting of a struct with an optional field (pointer) // - formatting of a struct with an optional field (pointer)
// - default formatting for pointers
type T2 struct { type T2 struct {
s string; s string;
...@@ -65,11 +69,14 @@ type T2 struct { ...@@ -65,11 +69,14 @@ type T2 struct {
const F2a = const F2a =
F1 + F1 +
`string = "%s";`
`pointer = *;` `pointer = *;`
`format.T2 = s ["-" p "-"];` `format.T2 = s ["-" p "-"];`
const F2b = const F2b =
F1 + F1 +
`string = "%s";`
`pointer = *;`
`format.T2 = s ("-" p "-" | "empty");`; `format.T2 = s ("-" p "-" | "empty");`;
func Test2(t *testing.T) { func Test2(t *testing.T) {
...@@ -88,9 +95,14 @@ type T3 struct { ...@@ -88,9 +95,14 @@ type T3 struct {
} }
const F3a = const F3a =
`default = "%v";`
`array = *;`
`format.T3 = s {" " a a / ","};` `format.T3 = s {" " a a / ","};`
const F3b = const F3b =
`int = "%d";`
`string = "%s";`
`array = *;`
`nil = ;` `nil = ;`
`empty = *:nil;` `empty = *:nil;`
`format.T3 = s [a:empty ": " {a / "-"}]` `format.T3 = s [a:empty ": " {a / "-"}]`
...@@ -112,11 +124,17 @@ type T4 struct { ...@@ -112,11 +124,17 @@ type T4 struct {
} }
const F4a = const F4a =
`int = "%d";`
`pointer = *;`
`array = *;`
`nil = ;` `nil = ;`
`empty = *:nil;` `empty = *:nil;`
`format.T4 = "<" (x:empty x | "-") ">" ` `format.T4 = "<" (x:empty x | "-") ">" `
const F4b = const F4b =
`int = "%d";`
`pointer = *;`
`array = *;`
`nil = ;` `nil = ;`
`empty = *:nil;` `empty = *:nil;`
`format.T4 = "<" (a:empty {a / ", "} | "-") ">" ` `format.T4 = "<" (a:empty {a / ", "} | "-") ">" `
......
...@@ -94,20 +94,32 @@ func (h *ErrorHandler) Error(pos token.Position, msg string) { ...@@ -94,20 +94,32 @@ func (h *ErrorHandler) Error(pos token.Position, msg string) {
} }
func isValidPos(w io.Writer, value interface{}, name string) bool { func isValidPos(w io.Writer, env, value interface{}, name string) bool {
return value.(token.Position).Line > 0; return value.(token.Position).Line > 0;
} }
func isSend(w io.Writer, value interface{}, name string) bool { func isSend(w io.Writer, env, value interface{}, name string) bool {
return value.(ast.ChanDir) & ast.SEND != 0; return value.(ast.ChanDir) & ast.SEND != 0;
} }
func isRecv(w io.Writer, value interface{}, name string) bool { func isRecv(w io.Writer, env, value interface{}, name string) bool {
return value.(ast.ChanDir) & ast.RECV != 0; return value.(ast.ChanDir) & ast.RECV != 0;
} }
func isMultiLineComment(w io.Writer, env, value interface{}, name string) bool {
return value.([]byte)[1] == '*'
}
var fmap = format.FormatterMap{
"isValidPos": isValidPos,
"isSend": isSend,
"isRecv": isRecv,
"isMultiLineComment": isMultiLineComment,
}
func main() { func main() {
// handle flags // handle flags
...@@ -129,7 +141,7 @@ func main() { ...@@ -129,7 +141,7 @@ func main() {
fmt.Fprintf(os.Stderr, "%s: %v\n", ast_txt, err); fmt.Fprintf(os.Stderr, "%s: %v\n", ast_txt, err);
os.Exit(1); os.Exit(1);
} }
ast_format, err := format.Parse(src, format.FormatterMap{"isValidPos": isValidPos, "isSend": isSend, "isRecv": isRecv}); ast_format, err := format.Parse(src, fmap);
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "%s: format errors:\n%s", ast_txt, err); fmt.Fprintf(os.Stderr, "%s: format errors:\n%s", ast_txt, err);
os.Exit(1); os.Exit(1);
...@@ -156,7 +168,13 @@ func main() { ...@@ -156,7 +168,13 @@ func main() {
if !*silent { if !*silent {
tw := makeTabwriter(os.Stdout); tw := makeTabwriter(os.Stdout);
if *formatter { if *formatter {
ast_format.Fprint(tw, prog); var optSemi bool; // formatting environment
_, err := ast_format.Fprint(tw, &optSemi, prog);
if err != nil {
fmt.Fprintf(os.Stderr, "format error$$: %s", err);
exitcode = 1;
continue; // proceed with next file
}
} else { } else {
var p astPrinter.Printer; var p astPrinter.Printer;
p.Init(tw, nil, nil /*prog.Comments*/, false); p.Init(tw, nil, nil /*prog.Comments*/, false);
......
...@@ -69,7 +69,7 @@ type Object struct { ...@@ -69,7 +69,7 @@ type Object struct {
func (obj *Object) IsExported() bool { func (obj *Object) IsExported() bool {
switch obj.Kind { switch obj.Kind {
case NONE /* FUNC for now */, CONST, TYPE, VAR, FUNC: case NONE /* FUNC for now */, CONST, TYPE, VAR, FUNC:
ch, size := utf8.DecodeRuneInString(obj.Ident, 0); ch, size := utf8.DecodeRuneInString(obj.Ident);
return unicode.IsUpper(ch); return unicode.IsUpper(ch);
} }
return false; return false;
......
...@@ -36,8 +36,7 @@ apply1() { ...@@ -36,8 +36,7 @@ apply1() {
# apply to local files # apply to local files
applydot() { applydot() {
for F in *.go for F in `find . -name "*.go" | grep -v "OLD" | grep -v "._"`; do
do
apply1 $1 $F apply1 $1 $F
done done
} }
...@@ -45,7 +44,7 @@ applydot() { ...@@ -45,7 +44,7 @@ applydot() {
# apply to all .go files we can find # apply to all .go files we can find
apply() { apply() {
for F in `find $GOROOT -name "*.go" | grep -v "OLD"`; do for F in `find $GOROOT -name "*.go" | grep -v "OLD" | grep -v "._"`; do
apply1 $1 $F apply1 $1 $F
done done
} }
......
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