Skip to content

Commit e48d8fe

Browse files
committed
update remaining files to match new scanner/parser interface
and use shared error handling infrastructure R=rsc DELTA=109 (3 added, 86 deleted, 20 changed) OCL=31600 CL=31605
1 parent 5945b25 commit e48d8fe

File tree

7 files changed

+22
-105
lines changed

7 files changed

+22
-105
lines changed

src/cmd/ebnflint/ebnflint.go

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"ebnf";
1010
"flag";
1111
"fmt";
12+
"go/scanner";
1213
"io";
1314
"os";
1415
"path";
@@ -70,23 +71,6 @@ func extractEBNF(src []byte) []byte {
7071
}
7172

7273

73-
// TODO(gri) This is the same code for reportError as in gofmt.
74-
// Should factor this out as part of some parsing framework
75-
// that could also deal with reading various input sources.
76-
77-
func reportError(filename string, err os.Error) {
78-
if errors, ok := err.(ebnf.ErrorList); ok {
79-
sort.Sort(errors);
80-
for _, e := range errors {
81-
fmt.Fprintf(os.Stderr, "%s:%v\n", filename, e);
82-
}
83-
} else {
84-
fmt.Fprintf(os.Stderr, "%s: %v\n", filename, err);
85-
}
86-
os.Exit(1);
87-
}
88-
89-
9074
func main() {
9175
flag.Parse();
9276

@@ -102,19 +86,19 @@ func main() {
10286

10387
src, err := io.ReadFile(filename);
10488
if err != nil {
105-
reportError(filename, err);
89+
scanner.PrintError(os.Stderr, err);
10690
}
10791

10892
if path.Ext(filename) == ".html" {
10993
src = extractEBNF(src);
11094
}
11195

112-
grammar, err := ebnf.Parse(src);
96+
grammar, err := ebnf.Parse(filename, src);
11397
if err != nil {
114-
reportError(filename, err);
98+
scanner.PrintError(os.Stderr, err);
11599
}
116100

117101
if err = ebnf.Verify(grammar, *start); err != nil {
118-
reportError(filename, err);
102+
scanner.PrintError(os.Stderr, err);
119103
}
120104
}

src/cmd/gobuild/util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ func PackageImports(file string) (pkg string, imports []string, err1 os.Error) {
225225
return "", nil, err
226226
}
227227

228-
prog, err := parser.Parse(f, parser.ImportsOnly);
228+
prog, err := parser.Parse(file, f, parser.ImportsOnly);
229229
if err != nil {
230230
return "", nil, err;
231231
}

src/cmd/godoc/godoc.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"go/doc";
3636
"go/parser";
3737
"go/printer";
38+
"go/scanner";
3839
"go/token";
3940
"http";
4041
"io";
@@ -166,10 +167,10 @@ func parse(path string, mode uint) (*ast.Program, *parseErrors) {
166167
return nil, &parseErrors{path, errs, nil};
167168
}
168169

169-
prog, err := parser.Parse(src, mode);
170+
prog, err := parser.Parse(path, src, mode);
170171
if err != nil {
171172
// sort and convert error list
172-
if errors, ok := err.(parser.ErrorList); ok {
173+
if errors, ok := err.(scanner.ErrorList); ok {
173174
sort.Sort(errors);
174175
errs := make([]parseError, len(errors) + 1); // +1 for final fragment of source
175176
offs := 0;

src/cmd/gofmt/gofmt.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"go/ast";
1111
"go/parser";
1212
"go/printer";
13+
"go/scanner";
1314
"io";
1415
"os";
1516
"sort";
@@ -84,16 +85,9 @@ func main() {
8485
os.Exit(1);
8586
}
8687

87-
prog, err := parser.Parse(src, parserMode());
88+
prog, err := parser.Parse(filename, src, parserMode());
8889
if err != nil {
89-
if errors, ok := err.(parser.ErrorList); ok {
90-
sort.Sort(errors);
91-
for _, e := range errors {
92-
fmt.Fprintf(os.Stderr, "%s:%v\n", filename, e);
93-
}
94-
} else {
95-
fmt.Fprintf(os.Stderr, "%s: %v\n", filename, err);
96-
}
90+
scanner.PrintError(os.Stderr, err);
9791
os.Exit(1);
9892
}
9993

@@ -102,7 +96,7 @@ func main() {
10296
ast.FilterExports(prog); // ignore result
10397
}
10498
w := makeTabwriter(os.Stdout);
105-
printer.Fprint(w, prog, printerMode());
99+
printer.Fprint(w, prog, printerMode()); // ignore errors
106100
w.Flush();
107101
}
108102
}

src/pkg/Make.deps

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ go/ast.install: go/token.install unicode.install utf8.install
2222
go/doc.install: container/vector.install fmt.install go/ast.install go/token.install io.install once.install regexp.install sort.install strings.install template.install
2323
go/parser.install: bytes.install container/vector.install fmt.install go/ast.install go/scanner.install go/token.install io.install os.install strings.install
2424
go/printer.install: fmt.install go/ast.install go/token.install io.install os.install reflect.install strings.install
25-
go/scanner.install: go/token.install strconv.install unicode.install utf8.install
25+
go/scanner.install: bytes.install container/vector.install fmt.install go/token.install io.install os.install sort.install strconv.install unicode.install utf8.install
2626
go/token.install: strconv.install
2727
gob.install: fmt.install io.install math.install os.install reflect.install strings.install sync.install unicode.install
2828
hash.install: io.install

src/pkg/datafmt/datafmt_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414

1515

1616
func parse(t *testing.T, form string, fmap FormatterMap) Format {
17-
f, err := Parse(strings.Bytes(form), fmap);
17+
f, err := Parse("", strings.Bytes(form), fmap);
1818
if err != nil {
1919
t.Errorf("Parse(%s): %v", form, err);
2020
return nil;

src/pkg/datafmt/parser.go

Lines changed: 7 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -15,53 +15,11 @@ import (
1515
"strings";
1616
)
1717

18-
// ----------------------------------------------------------------------------
19-
// Error handling
20-
21-
// Error describes an individual error. The position Pos, if valid,
22-
// indicates the format source position the error relates to. The
23-
// error is specified with the Msg string.
24-
//
25-
type Error struct {
26-
Pos token.Position;
27-
Msg string;
28-
}
29-
30-
31-
func (e *Error) String() string {
32-
pos := "";
33-
if e.Pos.IsValid() {
34-
pos = fmt.Sprintf("%d:%d: ", e.Pos.Line, e.Pos.Column);
35-
}
36-
return pos + e.Msg;
37-
}
38-
39-
40-
// An ErrorList is a list of errors encountered during parsing.
41-
type ErrorList []*Error
42-
43-
44-
// ErrorList implements SortInterface and the os.Error interface.
45-
46-
func (p ErrorList) Len() int { return len(p); }
47-
func (p ErrorList) Swap(i, j int) { p[i], p[j] = p[j], p[i]; }
48-
func (p ErrorList) Less(i, j int) bool { return p[i].Pos.Offset < p[j].Pos.Offset; }
49-
50-
51-
func (p ErrorList) String() string {
52-
switch len(p) {
53-
case 0: return "unspecified error";
54-
case 1: return p[0].String();
55-
}
56-
return fmt.Sprintf("%s (and %d more errors)", p[0].String(), len(p) - 1);
57-
}
58-
59-
6018
// ----------------------------------------------------------------------------
6119
// Parsing
6220

6321
type parser struct {
64-
errors vector.Vector;
22+
scanner.ErrorVector;
6523
scanner scanner.Scanner;
6624
pos token.Position; // token position
6725
tok token.Token; // one token look-ahead
@@ -83,26 +41,15 @@ func (p *parser) next() {
8341
}
8442

8543

86-
func (p *parser) init(src []byte) {
87-
p.errors.Init(0);
88-
p.scanner.Init(src, p, scanner.AllowIllegalChars); // return '@' as token.ILLEGAL w/o error message
44+
func (p *parser) init(filename string, src []byte) {
45+
p.ErrorVector.Init();
46+
p.scanner.Init(filename, src, p, scanner.AllowIllegalChars); // return '@' as token.ILLEGAL w/o error message
8947
p.next(); // initializes pos, tok, lit
9048
p.packs = make(map [string] string);
9149
p.rules = make(map [string] expr);
9250
}
9351

9452

95-
// The parser implements scanner.Error.
96-
func (p *parser) Error(pos token.Position, msg string) {
97-
// Don't collect errors that are on the same line as the previous error
98-
// in the hope to reduce the number of spurious errors due to incorrect
99-
// parser synchronization.
100-
if p.errors.Len() == 0 || p.errors.Last().(*Error).Pos.Line != pos.Line {
101-
p.errors.Push(&Error{pos, msg});
102-
}
103-
}
104-
105-
10653
func (p *parser) errorExpected(pos token.Position, msg string) {
10754
msg = "expected " + msg;
10855
if pos.Offset == p.pos.Offset {
@@ -416,10 +363,10 @@ func remap(p *parser, name string) string {
416363
// there are no errors, the result is a Format and the error is nil.
417364
// Otherwise the format is nil and a non-empty ErrorList is returned.
418365
//
419-
func Parse(src []byte, fmap FormatterMap) (Format, os.Error) {
366+
func Parse(filename string, src []byte, fmap FormatterMap) (Format, os.Error) {
420367
// parse source
421368
var p parser;
422-
p.init(src);
369+
p.init(filename, src);
423370
p.parseFormat();
424371

425372
// add custom formatters, if any
@@ -433,14 +380,5 @@ func Parse(src []byte, fmap FormatterMap) (Format, os.Error) {
433380
}
434381
}
435382

436-
// convert errors list, if any
437-
if p.errors.Len() > 0 {
438-
errors := make(ErrorList, p.errors.Len());
439-
for i := 0; i < p.errors.Len(); i++ {
440-
errors[i] = p.errors.At(i).(*Error);
441-
}
442-
return nil, errors;
443-
}
444-
445-
return p.rules, nil;
383+
return p.rules, p.GetError(scanner.NoMultiples);
446384
}

0 commit comments

Comments
 (0)