Пример #1
0
func main() {
	outFile := flag.String("out", "", "The path to the output file. If not present the output will "+
		"be written to standard out.")
	debug := flag.Bool("debug", false, "Generate debug data including the parse tree and print it to standard out.")
	flag.Parse()

	fileNames := flag.Args()
	if len(fileNames) == 0 {
		ErrorExit(fmt.Sprintf("No .mojom files given.\n"+
			"Usage: %s [-I <include_dirs>] [-out <out_file>] [-debug] <mojom_file>...",
			filepath.Base(os.Args[0])))
	}

	parseDriver := parser.NewDriver(directoryListFlag, *debug)

	// Do the parsing
	descriptor, err := parseDriver.ParseFiles(fileNames)
	if err != nil {
		ErrorExit(fmt.Sprintf("%s", err.Error()))
	} else if *debug {
		fmt.Println("Parsing complete.")
	}

	// Serialize the result.
	bytes, debug_string, err := serialization.Serialize(descriptor, *debug)
	if err != nil {
		ErrorExit(fmt.Sprintf("Serialization error: %s", err))
	}

	// In debug mode print out the debug information.
	if *debug {
		PrintDebugOutput(debug_string, descriptor)
	}

	// Emit the output to a file or standard out.
	if len(*outFile) == 0 {
		w := bufio.NewWriter(os.Stdout)
		if _, err := w.Write(bytes); err != nil {
			ErrorExit(fmt.Sprintf("Error writing output to standard out: %s.", *outFile, err.Error()))
		}
		w.Flush()
	} else {
		if err := ioutil.WriteFile(*outFile, bytes, os.ModePerm); err != nil {
			ErrorExit(fmt.Sprintf("Error writing output to %s: %s.", *outFile, err.Error()))
		} else {
			if *debug {
				fmt.Printf("The output was written to %s.\n", *outFile)
			}
		}
	}
}
Пример #2
0
func main() {
	debug := flag.Bool("debug", false, "Generate and print the parse tree.")

	flag.Parse()

	parseDriver := parser.NewDriver()
	parseDriver.SetDebugMode(*debug)
	parseDriver.SetImportDirectories(directoryListFlag)

	// Do the parsing
	result := parseDriver.ParseFiles(flag.Args())

	if result.Err != nil {
		fmt.Printf("%s", result.Err.Error())
	} else {
		fmt.Println("Parsing complete.")
	}

	// Serialize the output.
	bytes, err := result.Descriptor.Serialize()
	if err != nil {
		fmt.Println("Serialization error: %s", err)
		return
	}
	// For debug purposes at present we print out the hex representation
	// of the bytes to the console.
	// TODO(rudominer) Write the output bytes to a file.
	fmt.Println("\n\n=============================================")
	fmt.Println("\n Debug Serialized Output:")
	if bytes == nil {
		fmt.Println("bytes == nil")
	} else {
		fmt.Printf("len(bytes)=%d\n", len(bytes))
		for _, b := range bytes {
			fmt.Printf("%X ", b)
		}
	}
	fmt.Println("\n\n=============================================")
	fmt.Println("\n Pre-Serialized Go Object:")
	fmt.Printf("\n%s\n", result.Descriptor.String())
}