2008-09-18 17:58:37 -06:00
|
|
|
// 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 main
|
|
|
|
|
2009-01-07 17:54:03 -07:00
|
|
|
import (
|
2009-05-18 15:59:16 -06:00
|
|
|
"astprinter";
|
2009-02-06 16:26:30 -07:00
|
|
|
"flag";
|
2009-04-21 19:37:48 -06:00
|
|
|
"fmt";
|
2009-05-18 15:59:16 -06:00
|
|
|
"format";
|
2009-04-16 21:52:37 -06:00
|
|
|
"go/ast";
|
2009-04-21 19:37:48 -06:00
|
|
|
"go/parser";
|
|
|
|
"go/token";
|
|
|
|
"io";
|
2009-04-16 21:52:37 -06:00
|
|
|
"os";
|
2009-05-18 15:59:16 -06:00
|
|
|
"sort";
|
2009-03-31 17:53:58 -06:00
|
|
|
"tabwriter";
|
2009-01-07 17:54:03 -07:00
|
|
|
)
|
2008-09-18 17:58:37 -06:00
|
|
|
|
|
|
|
|
|
|
|
var (
|
2009-04-21 19:37:48 -06:00
|
|
|
// operation modes
|
2009-04-23 22:53:01 -06:00
|
|
|
columns bool;
|
2009-02-06 16:26:30 -07:00
|
|
|
silent = flag.Bool("s", false, "silent mode: no pretty print output");
|
2009-04-21 19:37:48 -06:00
|
|
|
verbose = flag.Bool("v", false, "verbose mode: trace parsing");
|
2009-03-31 17:53:58 -06:00
|
|
|
|
|
|
|
// layout control
|
2009-04-21 19:37:48 -06:00
|
|
|
tabwidth = flag.Int("tabwidth", 4, "tab width");
|
2009-04-23 22:53:01 -06:00
|
|
|
usetabs = flag.Bool("tabs", false, "align with tabs instead of blanks");
|
|
|
|
formatter = flag.Bool("formatter", false, "use formatter"); // TODO remove eventually
|
2008-09-18 17:58:37 -06:00
|
|
|
)
|
|
|
|
|
2009-03-31 17:53:58 -06:00
|
|
|
|
2009-01-09 14:42:46 -07:00
|
|
|
func init() {
|
2009-04-21 19:37:48 -06:00
|
|
|
user, err := os.Getenv("USER");
|
2009-04-23 22:53:01 -06:00
|
|
|
flag.BoolVar(&columns, "columns", user == "gri", "print column no. in error messages");
|
2009-01-09 14:42:46 -07:00
|
|
|
}
|
|
|
|
|
2008-09-18 17:58:37 -06:00
|
|
|
|
2009-01-15 18:16:41 -07:00
|
|
|
func usage() {
|
2009-04-28 20:11:37 -06:00
|
|
|
fmt.Fprintf(os.Stderr, "usage: pretty { flags } { files }\n");
|
2009-02-06 16:26:30 -07:00
|
|
|
flag.PrintDefaults();
|
2009-05-08 16:21:41 -06:00
|
|
|
os.Exit(1);
|
2008-09-19 13:12:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-21 19:37:48 -06:00
|
|
|
// TODO(gri) use library function for this once it exists
|
|
|
|
func readFile(filename string) ([]byte, os.Error) {
|
|
|
|
f, err := os.Open(filename, os.O_RDONLY, 0);
|
|
|
|
if err != nil {
|
|
|
|
return nil, err;
|
|
|
|
}
|
|
|
|
defer f.Close();
|
|
|
|
var b io.ByteBuffer;
|
|
|
|
if n, err := io.Copy(f, &b); err != nil {
|
|
|
|
return nil, err;
|
|
|
|
}
|
|
|
|
return b.Data(), nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TODO(gri) move this function into tabwriter.go? (also used in godoc)
|
2009-05-08 12:22:57 -06:00
|
|
|
func makeTabwriter(writer io.Writer) *tabwriter.Writer {
|
2009-03-31 17:53:58 -06:00
|
|
|
padchar := byte(' ');
|
|
|
|
if *usetabs {
|
|
|
|
padchar = '\t';
|
|
|
|
}
|
2009-05-01 20:32:41 -06:00
|
|
|
return tabwriter.NewWriter(writer, *tabwidth, 1, padchar, 0);
|
2009-04-21 19:37:48 -06:00
|
|
|
}
|
|
|
|
|
2009-03-31 17:53:58 -06:00
|
|
|
|
2009-06-01 20:13:44 -06:00
|
|
|
func isValidPos(state *format.State, value interface{}, rule_name string) bool {
|
2009-05-18 15:59:16 -06:00
|
|
|
pos := value.(token.Position);
|
|
|
|
return pos.IsValid();
|
2009-05-06 17:28:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-01 20:13:44 -06:00
|
|
|
func isSend(state *format.State, value interface{}, rule_name string) bool {
|
2009-05-06 17:28:18 -06:00
|
|
|
return value.(ast.ChanDir) & ast.SEND != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-01 20:13:44 -06:00
|
|
|
func isRecv(state *format.State, value interface{}, rule_name string) bool {
|
2009-05-06 17:28:18 -06:00
|
|
|
return value.(ast.ChanDir) & ast.RECV != 0;
|
|
|
|
}
|
|
|
|
|
2009-06-01 20:13:44 -06:00
|
|
|
|
|
|
|
func isMultiLineComment(state *format.State, value interface{}, rule_name string) bool {
|
|
|
|
return value.([]byte)[1] == '*';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type environment struct {
|
|
|
|
optSemi *bool;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (e environment) Copy() format.Environment {
|
|
|
|
optSemi := *e.optSemi;
|
|
|
|
return environment{&optSemi};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func clearOptSemi(state *format.State, value interface{}, rule_name string) bool {
|
|
|
|
*state.Env().(environment).optSemi = false;
|
|
|
|
return true;
|
2009-05-15 19:52:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-01 20:13:44 -06:00
|
|
|
func setOptSemi(state *format.State, value interface{}, rule_name string) bool {
|
|
|
|
*state.Env().(environment).optSemi = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func optSemi(state *format.State, value interface{}, rule_name string) bool {
|
|
|
|
if !*state.Env().(environment).optSemi {
|
|
|
|
state.Write([]byte{';'});
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var fmap = format.FormatterMap {
|
2009-05-15 19:52:59 -06:00
|
|
|
"isValidPos": isValidPos,
|
|
|
|
"isSend": isSend,
|
|
|
|
"isRecv": isRecv,
|
|
|
|
"isMultiLineComment": isMultiLineComment,
|
2009-06-01 20:13:44 -06:00
|
|
|
"/": clearOptSemi,
|
|
|
|
"clearOptSemi": clearOptSemi,
|
|
|
|
"setOptSemi": setOptSemi,
|
|
|
|
"optSemi": optSemi,
|
2009-05-15 19:52:59 -06:00
|
|
|
}
|
|
|
|
|
2009-05-06 17:28:18 -06:00
|
|
|
|
2008-09-18 17:58:37 -06:00
|
|
|
func main() {
|
2009-04-21 19:37:48 -06:00
|
|
|
// handle flags
|
2009-02-06 16:26:30 -07:00
|
|
|
flag.Parse();
|
|
|
|
if flag.NFlag() == 0 && flag.NArg() == 0 {
|
2009-01-15 18:16:41 -07:00
|
|
|
usage();
|
2008-09-19 13:12:28 -06:00
|
|
|
}
|
|
|
|
|
2009-04-21 19:37:48 -06:00
|
|
|
// determine parsing mode
|
|
|
|
mode := parser.ParseComments;
|
|
|
|
if *verbose {
|
|
|
|
mode |= parser.Trace;
|
|
|
|
}
|
|
|
|
|
2009-04-23 22:53:01 -06:00
|
|
|
// get ast format
|
|
|
|
const ast_txt = "ast.txt";
|
|
|
|
src, err := readFile(ast_txt);
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "%s: %v\n", ast_txt, err);
|
2009-05-08 16:21:41 -06:00
|
|
|
os.Exit(1);
|
2009-04-23 22:53:01 -06:00
|
|
|
}
|
2009-05-15 19:52:59 -06:00
|
|
|
ast_format, err := format.Parse(src, fmap);
|
2009-05-06 17:28:18 -06:00
|
|
|
if err != nil {
|
2009-06-01 20:13:44 -06:00
|
|
|
fmt.Fprintf(os.Stderr, "%s: %v\n", ast_txt, err);
|
2009-05-08 16:21:41 -06:00
|
|
|
os.Exit(1);
|
2009-04-23 22:53:01 -06:00
|
|
|
}
|
|
|
|
|
2008-09-18 17:58:37 -06:00
|
|
|
// process files
|
2009-05-06 17:28:18 -06:00
|
|
|
exitcode := 0;
|
2009-02-06 16:26:30 -07:00
|
|
|
for i := 0; i < flag.NArg(); i++ {
|
2009-04-21 19:37:48 -06:00
|
|
|
filename := flag.Arg(i);
|
|
|
|
|
|
|
|
src, err := readFile(filename);
|
|
|
|
if err != nil {
|
2009-04-23 22:53:01 -06:00
|
|
|
fmt.Fprintf(os.Stderr, "%s: %v\n", filename, err);
|
2009-05-06 17:28:18 -06:00
|
|
|
exitcode = 1;
|
|
|
|
continue; // proceed with next file
|
2009-04-21 19:37:48 -06:00
|
|
|
}
|
|
|
|
|
2009-05-18 15:59:16 -06:00
|
|
|
prog, err := parser.Parse(src, mode);
|
|
|
|
if err != nil {
|
|
|
|
if errors, ok := err.(parser.ErrorList); ok {
|
|
|
|
sort.Sort(errors);
|
|
|
|
for _, e := range errors {
|
|
|
|
fmt.Fprintf(os.Stderr, "%s:%v\n", filename, e);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(os.Stderr, "%s: %v\n", filename, err);
|
|
|
|
}
|
2009-05-06 17:28:18 -06:00
|
|
|
exitcode = 1;
|
2009-05-08 16:21:41 -06:00
|
|
|
continue; // proceed with next file
|
2009-05-06 17:28:18 -06:00
|
|
|
}
|
2009-04-23 22:53:01 -06:00
|
|
|
|
2009-05-06 17:28:18 -06:00
|
|
|
if !*silent {
|
2009-04-23 22:53:01 -06:00
|
|
|
tw := makeTabwriter(os.Stdout);
|
|
|
|
if *formatter {
|
2009-06-01 20:13:44 -06:00
|
|
|
env := environment{new(bool)};
|
|
|
|
_, err := ast_format.Fprint(tw, env, prog);
|
2009-05-15 19:52:59 -06:00
|
|
|
if err != nil {
|
2009-06-01 20:13:44 -06:00
|
|
|
fmt.Fprintf(os.Stderr, "format error: %v\n", err);
|
2009-05-15 19:52:59 -06:00
|
|
|
exitcode = 1;
|
|
|
|
continue; // proceed with next file
|
|
|
|
}
|
2009-04-23 22:53:01 -06:00
|
|
|
} else {
|
|
|
|
var p astPrinter.Printer;
|
|
|
|
p.Init(tw, nil, nil /*prog.Comments*/, false);
|
|
|
|
p.DoProgram(prog);
|
|
|
|
}
|
|
|
|
tw.Flush();
|
2008-09-23 17:40:12 -06:00
|
|
|
}
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2009-05-08 16:21:41 -06:00
|
|
|
|
|
|
|
os.Exit(exitcode);
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|