Beispiel #1
0
func main() {
	fs := flag.NewFlagSet(os.Args[0], flag.ExitOnError)

	// define command-line flags
	var (
		cacheFlag     = fs.Bool("cache", false, "cache parsing results")
		dbgFlag       = fs.Bool("debug", false, "set debug mode")
		shortHelpFlag = fs.Bool("h", false, "show help page")
		longHelpFlag  = fs.Bool("help", false, "show help page")
		noRecoverFlag = fs.Bool("no-recover", false, "do not recover from panic")
		outputFlag    = fs.String("o", "", "output file, defaults to stdout")
		recvrNmFlag   = fs.String("receiver-name", "c", "receiver name for the generated methods")
		noBuildFlag   = fs.Bool("x", false, "do not build, only parse")
	)

	fs.Usage = usage
	fs.Parse(os.Args[1:])

	if *shortHelpFlag || *longHelpFlag {
		fs.Usage()
		exit(0)
	}

	if fs.NArg() > 1 {
		argError(1, "expected one argument, got %q", strings.Join(fs.Args(), " "))
	}

	// get input source
	infile := ""
	if fs.NArg() == 1 {
		infile = fs.Arg(0)
	}
	nm, rc := input(infile)
	defer rc.Close()

	// parse input
	g, err := ParseReader(nm, rc, Debug(*dbgFlag), Memoize(*cacheFlag), Recover(!*noRecoverFlag))
	if err != nil {
		fmt.Fprintln(os.Stderr, "parse error(s):\n", err)
		exit(3)
	}

	if !*noBuildFlag {
		// generate parser
		out := output(*outputFlag)
		defer out.Close()

		curNmOpt := builder.ReceiverName(*recvrNmFlag)
		if err := builder.BuildParser(out, g.(*ast.Grammar), curNmOpt); err != nil {
			fmt.Fprintln(os.Stderr, "build error: ", err)
			exit(5)
		}
	}
}
Beispiel #2
0
func main() {
	dbgFlag := flag.Bool("debug", false, "set debug mode")
	noBuildFlag := flag.Bool("x", false, "do not build, only parse")
	outputFlag := flag.String("o", "", "output file, defaults to stdout")
	flag.Parse()

	if flag.NArg() > 1 {
		fmt.Fprintf(os.Stderr, "USAGE: %s [options] [FILE]\n", os.Args[0])
		os.Exit(1)
	}

	nm := "stdin"
	inf := os.Stdin
	if flag.NArg() == 1 {
		f, err := os.Open(flag.Arg(0))
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
			os.Exit(2)
		}
		defer f.Close()
		inf = f
		nm = flag.Arg(0)
	}
	in := bufio.NewReader(inf)

	g, err := ParseReader(nm, in, Debug(*dbgFlag))
	if err != nil {
		fmt.Fprintln(os.Stderr, "parse error: ", err)
		os.Exit(3)
	}

	if !*noBuildFlag {
		outw := os.Stdout
		if *outputFlag != "" {
			f, err := os.Create(*outputFlag)
			if err != nil {
				fmt.Fprintln(os.Stderr, err)
				os.Exit(4)
			}
			defer f.Close()
			outw = f
		}

		if err := builder.BuildParser(outw, g.(*ast.Grammar)); err != nil {
			fmt.Fprintln(os.Stderr, "build error: ", err)
			os.Exit(5)
		}
	}
}