Commit f8ff3b10 authored by Robert Griesemer's avatar Robert Griesemer

daily snapshot:

- more work on template-driven ast formatting
- added preliminary test suite
- added documentation

TBR=r
OCL=27858
CL=27858
parent eaba458e
//string = //string =
// "%s" ; // "%s";
pointer = pointer =
^ ; *;
array = array =
^ ; *;
//token.Token = //token.Token =
// "token<%d>" ; // this should be a Go-installed formatter // "token<%d>"; // this could be a Go-installed formatter
ast ast
; ;
Comments = Comments =
"comments\n" ; "comments\n";
Ident = Ident =
Value ; Value;
Program = Program =
"package " Name "\n\n" { Decls "\n\n" } ; "package " Name "\n\n" {Decls "\n\n"};
GenDecl = GenDecl =
Doc Doc
...@@ -28,13 +28,13 @@ GenDecl = ...@@ -28,13 +28,13 @@ GenDecl =
")\n"; ")\n";
FuncType = FuncType =
"(" { Params } ")" ; "(" ")";
BlockStmt = BlockStmt =
"{\n" "}\n" ; "{\n" "}\n";
FuncDecl = FuncDecl =
"func " Name Type [ " " Body ] ; "func " Name Type [" " Body];
Decl = Decl =
^ ; ^;
\ No newline at end of file \ No newline at end of file
This diff is collapsed.
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package format
import (
"format";
"testing";
)
func check(t *testing.T, form, expected string, args ...) {
result := format.Parse(form).Sprint(args);
if result != expected {
t.Errorf(
"format : %s\nresult : %s\nexpected: %s\n\n",
form, result, expected
)
}
}
// ----------------------------------------------------------------------------
// - formatting of basic type int
const F0 =
`int = "0x%x";`
func Test0(t *testing.T) {
check(t, F0, "0x2a", 42);
}
// ----------------------------------------------------------------------------
// - default formatting of basic type int
// - formatting of a struct
type T1 struct {
a int;
}
const F1 =
`format.T1 = "<" a ">";`
func Test1(t *testing.T) {
check(t, F1, "<42>", T1{42});
}
// ----------------------------------------------------------------------------
// - formatting of a struct with an optional field (pointer)
// - default formatting for pointers
type T2 struct {
s string;
p *T1;
}
const F2a =
F1 +
`pointer = *;`
`format.T2 = s ["-" p "-"];`;
const F2b =
F1 +
`format.T2 = s ("-" p "-" | "empty");`;
func Test2(t *testing.T) {
check(t, F2a, "foo", T2{"foo", nil});
check(t, F2a, "bar-<17>-", T2{"bar", &T1{17}});
check(t, F2b, "fooempty", T2{"foo", nil});
}
// ----------------------------------------------------------------------------
// - formatting of a struct with a repetitive field (slice)
type T3 struct {
s string;
a []int;
}
const F3a =
`format.T3 = s { " " a a "," };`;
const F3b =
`format.T3 = [a:""] s | "nothing";`; // use 'a' to select alternative w/o printing a
func Test3(t *testing.T) {
check(t, F3a, "foo", T3{"foo", nil});
check(t, F3a, "foo 00, 11, 22,", T3{"foo", []int{0, 1, 2}});
//check(t, F3b, "nothing", T3{"bar", nil}); // TODO fix this
check(t, F3b, "bar", T3{"bar", []int{0}});
}
...@@ -135,7 +135,7 @@ func main() { ...@@ -135,7 +135,7 @@ func main() {
if ok && !*silent { if ok && !*silent {
tw := makeTabwriter(os.Stdout); tw := makeTabwriter(os.Stdout);
if *formatter { if *formatter {
ast_format.Apply(tw, prog); ast_format.Fprint(tw, prog);
} 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);
......
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