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
|
|
|
|
|
|
|
|
import Flag "flag"
|
|
|
|
import Platform "platform"
|
|
|
|
import Scanner "scanner"
|
|
|
|
import AST "ast" // should not be needed
|
|
|
|
import Parser "parser"
|
|
|
|
import Printer "printer"
|
|
|
|
|
|
|
|
|
|
|
|
var (
|
2008-09-22 19:26:12 -06:00
|
|
|
silent = Flag.Bool("s", false, nil, "silent mode: no pretty print output");
|
|
|
|
verbose = Flag.Bool("v", false, nil, "verbose mode: trace parsing");
|
2008-09-18 17:58:37 -06:00
|
|
|
sixg = Flag.Bool("6g", false, nil, "6g compatibility mode");
|
|
|
|
tokenchan = Flag.Bool("token_chan", false, nil, "use token channel for scanner-parser connection");
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2008-09-19 13:12:28 -06:00
|
|
|
func Usage() {
|
|
|
|
print("usage: pretty { flags } { files }\n");
|
|
|
|
Flag.PrintDefaults();
|
|
|
|
sys.exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-18 17:58:37 -06:00
|
|
|
func main() {
|
|
|
|
Flag.Parse();
|
|
|
|
|
2008-09-19 13:12:28 -06:00
|
|
|
if Flag.NFlag() == 0 && Flag.NArg() == 0 {
|
|
|
|
Usage();
|
|
|
|
}
|
|
|
|
|
2008-09-18 17:58:37 -06:00
|
|
|
// process files
|
|
|
|
for i := 0; i < Flag.NArg(); i++ {
|
|
|
|
src_file := Flag.Arg(i);
|
|
|
|
|
|
|
|
src, ok := Platform.ReadSourceFile(src_file);
|
|
|
|
if !ok {
|
2008-09-25 16:14:26 -06:00
|
|
|
print("cannot open ", src_file, "\n");
|
|
|
|
sys.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if silent.BVal() {
|
|
|
|
print("- ", src_file, "\n");
|
|
|
|
}
|
2008-09-18 17:58:37 -06:00
|
|
|
|
|
|
|
scanner := new(Scanner.Scanner);
|
|
|
|
scanner.Open(src_file, src);
|
|
|
|
|
|
|
|
var tstream *<-chan *Scanner.Token;
|
|
|
|
if tokenchan.BVal() {
|
|
|
|
tstream = scanner.TokenStream();
|
|
|
|
}
|
|
|
|
|
|
|
|
parser := new(Parser.Parser);
|
2008-09-23 17:40:12 -06:00
|
|
|
parser.Open(verbose.BVal(), scanner, tstream);
|
2008-09-18 17:58:37 -06:00
|
|
|
|
2008-09-23 17:40:12 -06:00
|
|
|
prog := parser.ParseProgram();
|
2008-09-25 16:14:26 -06:00
|
|
|
|
|
|
|
if scanner.nerrors > 0 {
|
|
|
|
sys.exit(1);
|
|
|
|
}
|
|
|
|
|
2008-09-23 17:40:12 -06:00
|
|
|
if !silent.BVal() {
|
|
|
|
Printer.Print(prog);
|
|
|
|
}
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
}
|