2009-07-13 11:26:58 -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 (
|
|
|
|
"bytes";
|
|
|
|
"ebnf";
|
|
|
|
"flag";
|
|
|
|
"fmt";
|
2009-07-14 11:46:18 -06:00
|
|
|
"go/scanner";
|
2009-07-13 11:26:58 -06:00
|
|
|
"io";
|
|
|
|
"os";
|
|
|
|
"path";
|
|
|
|
"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;
|
|
|
|
|
2009-09-03 11:53:27 -06:00
|
|
|
for {
|
|
|
|
// i = beginning of EBNF text
|
|
|
|
i := bytes.Index(src, open);
|
2009-07-13 11:26:58 -06:00
|
|
|
if i < 0 {
|
2009-09-03 11:53:27 -06:00
|
|
|
break; // no EBNF found - we are done
|
2009-07-13 11:26:58 -06:00
|
|
|
}
|
2009-09-03 11:53:27 -06:00
|
|
|
i += len(open);
|
2009-07-13 11:26:58 -06:00
|
|
|
|
|
|
|
// write as many newlines as found in the excluded text
|
2009-09-17 11:27:04 -06:00
|
|
|
// to maintain correct line numbers in error messages
|
2009-09-03 11:53:27 -06:00
|
|
|
for _, ch := range src[0 : i] {
|
2009-07-13 11:26:58 -06:00
|
|
|
if ch == '\n' {
|
|
|
|
buf.WriteByte('\n');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-03 11:53:27 -06:00
|
|
|
// j = end of EBNF text (or end of source)
|
|
|
|
j := bytes.Index(src[i : len(src)], close); // close marker
|
2009-07-13 11:26:58 -06:00
|
|
|
if j < 0 {
|
2009-09-03 11:53:27 -06:00
|
|
|
j = len(src)-i;
|
2009-07-13 11:26:58 -06:00
|
|
|
}
|
|
|
|
j += i;
|
|
|
|
|
2009-09-03 11:53:27 -06:00
|
|
|
// copy EBNF text
|
2009-07-13 11:26:58 -06:00
|
|
|
buf.Write(src[i : j]);
|
2009-09-03 11:53:27 -06:00
|
|
|
|
|
|
|
// advance
|
|
|
|
src = src[j : len(src)];
|
2009-07-13 11:26:58 -06:00
|
|
|
}
|
|
|
|
|
2009-09-16 16:15:00 -06:00
|
|
|
return buf.Bytes();
|
2009-07-13 11:26:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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 {
|
2009-07-14 11:46:18 -06:00
|
|
|
scanner.PrintError(os.Stderr, err);
|
2009-07-13 11:26:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if path.Ext(filename) == ".html" {
|
|
|
|
src = extractEBNF(src);
|
|
|
|
}
|
|
|
|
|
2009-07-14 11:46:18 -06:00
|
|
|
grammar, err := ebnf.Parse(filename, src);
|
2009-07-13 11:26:58 -06:00
|
|
|
if err != nil {
|
2009-07-14 11:46:18 -06:00
|
|
|
scanner.PrintError(os.Stderr, err);
|
2009-07-13 11:26:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = ebnf.Verify(grammar, *start); err != nil {
|
2009-07-14 11:46:18 -06:00
|
|
|
scanner.PrintError(os.Stderr, err);
|
2009-07-13 11:26:58 -06:00
|
|
|
}
|
|
|
|
}
|