1
0
mirror of https://github.com/golang/go synced 2024-10-03 20:21:22 -06:00
go/usr/gri/pretty/pretty.go
Robert Griesemer e45eb60657 Added mechanism for very precise self-testing:
- in selftest mode (-t) interpret comments of the form /* ERROR */ and /* SYNC */
  and validate reported errors with the error markings in a file
- added initial selftest.go file

Also:
- fixed an issue with empty blocks
- generally report better error messages
- added many more tests to the test script (essentially all .go programs which
  have no syntax errors)

R=r
OCL=17426
CL=17426
2008-10-18 16:42:25 -07:00

70 lines
1.5 KiB
Go

// 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 Parser "parser"
import Printer "printer"
var (
silent = Flag.Bool("s", false, nil, "silent mode: no pretty print output");
verbose = Flag.Bool("v", false, nil, "verbose mode: trace parsing");
sixg = Flag.Bool("6g", true, nil, "6g compatibility mode");
testmode = Flag.Bool("t", false, nil, "test mode: interprets /* ERROR */ and /* SYNC */ comments");
tokenchan = Flag.Bool("token_chan", false, nil, "use token channel for scanner-parser connection");
)
func Usage() {
print("usage: pretty { flags } { files }\n");
Flag.PrintDefaults();
sys.exit(0);
}
func main() {
Flag.Parse();
if Flag.NFlag() == 0 && Flag.NArg() == 0 {
Usage();
}
// process files
for i := 0; i < Flag.NArg(); i++ {
src_file := Flag.Arg(i);
src, ok := Platform.ReadSourceFile(src_file);
if !ok {
print("cannot open ", src_file, "\n");
sys.exit(1);
}
scanner := new(Scanner.Scanner);
scanner.Open(src_file, src, testmode.BVal());
var tstream *<-chan *Scanner.Token;
if tokenchan.BVal() {
tstream = scanner.TokenStream();
}
parser := new(Parser.Parser);
parser.Open(verbose.BVal(), sixg.BVal(), scanner, tstream);
prog := parser.ParseProgram();
if scanner.nerrors > 0 {
sys.exit(1);
}
if !silent.BVal() && !testmode.BVal() {
var P Printer.Printer;
(&P).Program(prog);
}
}
}