func verify(name, start string, r io.Reader) error {
	if r == nil {
		f, err := os.Open(name)
		if err != nil {
			return err
		}
		defer f.Close()
		r = f
	}

	src, err := ioutil.ReadAll(r)
	if err != nil {
		return err
	}

	if filepath.Ext(name) == ".html" || bytes.Index(src, open) >= 0 {
		src = extractEBNF(src)
	}

	grammar, err := ebnf.Parse(name, bytes.NewBuffer(src))
	if err != nil {
		return err
	}

	return ebnf.Verify(grammar, start)
}
Example #2
0
func main() {
	flag.Parse()

	var (
		filename string
		src      []byte
		err      error
	)
	switch flag.NArg() {
	case 0:
		filename = "<stdin>"
		src, err = ioutil.ReadAll(os.Stdin)
	case 1:
		filename = flag.Arg(0)
		src, err = ioutil.ReadFile(filename)
	default:
		usage()
	}
	if err != nil {
		report(err)
	}

	if filepath.Ext(filename) == ".html" || bytes.Index(src, open) >= 0 {
		src = extractEBNF(src)
	}

	grammar, err := ebnf.Parse(filename, bytes.NewBuffer(src))
	if err != nil {
		report(err)
	}

	if err = ebnf.Verify(grammar, *start); err != nil {
		report(err)
	}
}