mirror of
https://github.com/golang/go
synced 2024-10-04 13:11:22 -06:00
121 lines
2.2 KiB
Go
121 lines
2.2 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 (
|
||
|
"bytes";
|
||
|
"ebnf";
|
||
|
"flag";
|
||
|
"fmt";
|
||
|
"io";
|
||
|
"os";
|
||
|
"path";
|
||
|
"sort";
|
||
|
"strings";
|
||
|
)
|
||
|
|
||
|
|
||
|
var start = flag.String("start", "Start", "name of start production");
|
||
|
|
||
|
|
||
|
func usage() {
|
||
|
fmt.Fprintf(os.Stderr, "usage: ebnflint [flags] [filename]\n");
|
||
|
flag.PrintDefaults();
|
||
|
os.Exit(1);
|
||
|
}
|
||
|
|
||
|
|
||
|
// Markers around EBNF sections in .html files
|
||
|
var (
|
||
|
open = strings.Bytes(`<pre class="ebnf">`);
|
||
|
close = strings.Bytes(`</pre>`);
|
||
|
)
|
||
|
|
||
|
|
||
|
func extractEBNF(src []byte) []byte {
|
||
|
var buf bytes.Buffer;
|
||
|
|
||
|
for i, j, n := 0, 0, len(src); ; {
|
||
|
// i = beginning of EBNF section
|
||
|
i = bytes.Index(src[j : n], open);
|
||
|
if i < 0 {
|
||
|
break;
|
||
|
}
|
||
|
i += j+len(open);
|
||
|
|
||
|
// write as many newlines as found in the excluded text
|
||
|
// to maintain correct line numbers in error messages
|
||
|
for _, ch := range src[j : i] {
|
||
|
if ch == '\n' {
|
||
|
buf.WriteByte('\n');
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// j = end of EBNF section
|
||
|
j = bytes.Index(src[i : n], close);
|
||
|
if j < 0 {
|
||
|
// missing closing
|
||
|
// TODO(gri) should this be an error?
|
||
|
j = n-i;
|
||
|
}
|
||
|
j += i;
|
||
|
|
||
|
// copy EBNF section
|
||
|
buf.Write(src[i : j]);
|
||
|
}
|
||
|
|
||
|
return buf.Data();
|
||
|
}
|
||
|
|
||
|
|
||
|
// TODO(gri) This is the same code for reportError as in gofmt.
|
||
|
// Should factor this out as part of some parsing framework
|
||
|
// that could also deal with reading various input sources.
|
||
|
|
||
|
func reportError(filename string, err os.Error) {
|
||
|
if errors, ok := err.(ebnf.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);
|
||
|
}
|
||
|
os.Exit(1);
|
||
|
}
|
||
|
|
||
|
|
||
|
func main() {
|
||
|
flag.Parse();
|
||
|
|
||
|
var filename string;
|
||
|
switch flag.NArg() {
|
||
|
case 0:
|
||
|
filename = "/dev/stdin";
|
||
|
case 1:
|
||
|
filename = flag.Arg(0);
|
||
|
default:
|
||
|
usage();
|
||
|
}
|
||
|
|
||
|
src, err := io.ReadFile(filename);
|
||
|
if err != nil {
|
||
|
reportError(filename, err);
|
||
|
}
|
||
|
|
||
|
if path.Ext(filename) == ".html" {
|
||
|
src = extractEBNF(src);
|
||
|
}
|
||
|
|
||
|
grammar, err := ebnf.Parse(src);
|
||
|
if err != nil {
|
||
|
reportError(filename, err);
|
||
|
}
|
||
|
|
||
|
if err = ebnf.Verify(grammar, *start); err != nil {
|
||
|
reportError(filename, err);
|
||
|
}
|
||
|
}
|