Commit f70e8ed0 authored by Robert Griesemer's avatar Robert Griesemer

ebnflint: better handling of stdin

- don't rely on /dev/stdin as the name for standard input
- employ EBNF extraction if the source contains tags

"cat source.html | ebnflint" works now

R=r
CC=golang-dev
https://golang.org/cl/4641075
parent d94e350f
...@@ -35,6 +35,12 @@ var ( ...@@ -35,6 +35,12 @@ var (
) )
func report(err os.Error) {
scanner.PrintError(os.Stderr, err)
os.Exit(1)
}
func extractEBNF(src []byte) []byte { func extractEBNF(src []byte) []byte {
var buf bytes.Buffer var buf bytes.Buffer
...@@ -75,34 +81,35 @@ func extractEBNF(src []byte) []byte { ...@@ -75,34 +81,35 @@ func extractEBNF(src []byte) []byte {
func main() { func main() {
flag.Parse() flag.Parse()
var filename string var (
filename string
src []byte
err os.Error
)
switch flag.NArg() { switch flag.NArg() {
case 0: case 0:
filename = "/dev/stdin" filename = "<stdin>"
src, err = ioutil.ReadAll(os.Stdin)
case 1: case 1:
filename = flag.Arg(0) filename = flag.Arg(0)
src, err = ioutil.ReadFile(filename)
default: default:
usage() usage()
} }
src, err := ioutil.ReadFile(filename)
if err != nil { if err != nil {
scanner.PrintError(os.Stderr, err) report(err)
os.Exit(1)
} }
if filepath.Ext(filename) == ".html" { if filepath.Ext(filename) == ".html" || bytes.Index(src, open) >= 0 {
src = extractEBNF(src) src = extractEBNF(src)
} }
grammar, err := ebnf.Parse(fset, filename, src) grammar, err := ebnf.Parse(fset, filename, src)
if err != nil { if err != nil {
scanner.PrintError(os.Stderr, err) report(err)
os.Exit(1)
} }
if err = ebnf.Verify(fset, grammar, *start); err != nil { if err = ebnf.Verify(fset, grammar, *start); err != nil {
scanner.PrintError(os.Stderr, err) report(err)
os.Exit(1)
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment