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 =
// Comments
ast.Comment =
Text:string "\n";
Text:string [Text:isMultiLineComment "\n"];
ast.Comments =
{*};
......@@ -305,9 +305,13 @@ ast.BadDecl =
ast.GenDecl =
Doc
Tok " "
[Lparen:isValidPos "(" >> "\t" "\n"]
{Specs / ";\n"}
[Rparen:isValidPos << "\n" ")"];
( Lparen:isValidPos
>> "\t" "(\n"
{Specs / ";\n"}
<<
"\n)"
| {Specs / ";\n"}
);
ast.FuncDecl =
"func " ["(" Recv ") "] Name Type:funcSignature
......
This diff is collapsed.
......@@ -6,12 +6,17 @@ package format
import (
"format";
"io";
"testing";
)
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 {
t.Errorf(
"format : %s\nresult : `%s`\nexpected: `%s`\n\n",
......@@ -39,7 +44,6 @@ func Test0(t *testing.T) {
// ----------------------------------------------------------------------------
// - default formatting of basic type int
// - formatting of a struct
type T1 struct {
......@@ -47,6 +51,7 @@ type T1 struct {
}
const F1 =
`int = "%d";`
`format.T1 = "<" a ">";`
func Test1(t *testing.T) {
......@@ -56,7 +61,6 @@ func Test1(t *testing.T) {
// ----------------------------------------------------------------------------
// - formatting of a struct with an optional field (pointer)
// - default formatting for pointers
type T2 struct {
s string;
......@@ -65,11 +69,14 @@ type T2 struct {
const F2a =
F1 +
`string = "%s";`
`pointer = *;`
`format.T2 = s ["-" p "-"];`
const F2b =
F1 +
`string = "%s";`
`pointer = *;`
`format.T2 = s ("-" p "-" | "empty");`;
func Test2(t *testing.T) {
......@@ -88,9 +95,14 @@ type T3 struct {
}
const F3a =
`default = "%v";`
`array = *;`
`format.T3 = s {" " a a / ","};`
const F3b =
`int = "%d";`
`string = "%s";`
`array = *;`
`nil = ;`
`empty = *:nil;`
`format.T3 = s [a:empty ": " {a / "-"}]`
......@@ -112,11 +124,17 @@ type T4 struct {
}
const F4a =
`int = "%d";`
`pointer = *;`
`array = *;`
`nil = ;`
`empty = *:nil;`
`format.T4 = "<" (x:empty x | "-") ">" `
const F4b =
`int = "%d";`
`pointer = *;`
`array = *;`
`nil = ;`
`empty = *:nil;`
`format.T4 = "<" (a:empty {a / ", "} | "-") ">" `
......
......@@ -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;
}
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;
}
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;
}
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() {
// handle flags
......@@ -129,7 +141,7 @@ func main() {
fmt.Fprintf(os.Stderr, "%s: %v\n", ast_txt, err);
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 {
fmt.Fprintf(os.Stderr, "%s: format errors:\n%s", ast_txt, err);
os.Exit(1);
......@@ -156,7 +168,13 @@ func main() {
if !*silent {
tw := makeTabwriter(os.Stdout);
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 {
var p astPrinter.Printer;
p.Init(tw, nil, nil /*prog.Comments*/, false);
......
......@@ -69,7 +69,7 @@ type Object struct {
func (obj *Object) IsExported() bool {
switch obj.Kind {
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 false;
......
......@@ -36,8 +36,7 @@ apply1() {
# apply to local files
applydot() {
for F in *.go
do
for F in `find . -name "*.go" | grep -v "OLD" | grep -v "._"`; do
apply1 $1 $F
done
}
......@@ -45,7 +44,7 @@ applydot() {
# apply to all .go files we can find
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
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