Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 /* | 5 /* |
| 6 This file contains the main() for the Mojom parser binary. This tool parses | 6 This file contains parseCmd which parses a set of .mojom files and emits a |
| 7 a set of .mojom files and emits a serialized MojomFileGraph struct. See | 7 serialized MojomFileGraph struct. See mojom_files.mojom for the definition of |
| 8 mojom_files.mojom for the definition of MojomFileGraph. | 8 MojomFileGraph. |
| 9 | 9 |
| 10 The tool is invoked as follows: | 10 The parse command is invoked as follows: |
| 11 | 11 |
| 12 mojom_parser [-I <include_dirs>] [-out <out_file>] [-debug] <mojom_file>... | 12 mojom parse [-I <include_dirs>] [-out <out_file>] [-debug] <mojom_file>... |
| 13 | 13 |
| 14 <include_dirs> is comma-separated list of directory paths to search for mojom i mports. | 14 <include_dirs> is comma-separated list of directory paths to search for mojom i mports. |
| 15 <out_file> is the path to the output file. If not given the output will be writ ten to standard out. | 15 <out_file> is the path to the output file. If not given the output will be writ ten to standard out. |
| 16 <mojom_file>... is one or more paths to .mojom files to be parsed. | 16 <mojom_file>... is one or more paths to .mojom files to be parsed. |
| 17 | 17 |
| 18 If there are no errors then the program returns status zero and writes nothing | 18 If there are no errors then the program returns status zero and writes nothing |
| 19 to standard error and writes nothing to standard out except possibly the output | 19 to standard error and writes nothing to standard out except possibly the output |
| 20 if <out_file> is not specified. If there are any errors then the program return s | 20 if <out_file> is not specified. If there are any errors then the program return s |
| 21 status code 1 and writes error messages to standard error. | 21 status code 1 and writes error messages to standard error. |
| 22 | 22 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 57 return err | 57 return err |
| 58 } | 58 } |
| 59 if !info.IsDir() { | 59 if !info.IsDir() { |
| 60 return fmt.Errorf("%s is not a directory.", name) | 60 return fmt.Errorf("%s is not a directory.", name) |
| 61 } | 61 } |
| 62 *dl = append(*dl, name) | 62 *dl = append(*dl, name) |
| 63 } | 63 } |
| 64 return nil | 64 return nil |
| 65 } | 65 } |
| 66 | 66 |
| 67 var directoryListFlag DirectoryList | 67 // parseCmd implements the parse command for the mojom tool. |
| 68 // The slice of strings |args| is the list of arguments passed on the command | |
| 69 // line starting with the program name and followed by the invoked command. | |
| 70 func parseCmd(args []string) { | |
| 71 » flagSet := flag.NewFlagSet("parse", flag.ContinueOnError) | |
| 72 » var directoryListFlag DirectoryList | |
| 73 » flagSet.Var(&directoryListFlag, "I", "comma-separated list of directory paths to search for mojom imports") | |
| 74 » outFile := flagSet.String("out", "", "The path to the output file. If no t present the output will "+ | |
| 75 » » "be written to standard out.") | |
| 76 » debug := flagSet.Bool("debug", false, "Generate debug data including the parse tree and print it to standard out.") | |
| 77 » metaDataOnly := flagSet.Bool("meta-data-only", false, "Only parse file a ttributes and 'module' statement, "+ | |
| 78 » » "not mojom declarations or import statements.") | |
| 68 | 79 |
| 69 func init() { | 80 » printUsage := func() { |
| 70 » flag.Var(&directoryListFlag, "I", "comma-separated list of directory pat hs to search for mojom imports") | 81 » » fmt.Fprintf(os.Stderr, "Usage: %s parse [-I <include_dirs>] [-ou t <out_file>] [-debug] [-meta-data-only] <mojom_file>...\n\n", filepath.Base(arg s[0])) |
| 71 } | 82 » » flagSet.PrintDefaults() |
| 83 » } | |
| 72 | 84 |
| 73 func main() { | 85 » if err := flagSet.Parse(args[2:]); err != nil { |
| 74 » outFile := flag.String("out", "", "The path to the output file. If not p resent the output will "+ | 86 » » fmt.Fprintln(os.Stderr, err.Error()) |
|
rudominer
2016/02/19 20:42:42
This is still not working perfectly. When I type
.
azani
2016/02/19 23:23:24
Done.
| |
| 75 » » "be written to standard out.") | 87 » » os.Exit(1) |
| 76 » debug := flag.Bool("debug", false, "Generate debug data including the pa rse tree and print it to standard out.") | 88 » } |
| 77 » metaDataOnly := flag.Bool("meta-data-only", false, "Only parse file attr ibutes and 'module' statement, "+ | |
| 78 » » "not mojom declarations or import statements.") | |
| 79 » flag.Parse() | |
| 80 | 89 |
| 81 » fileNames := flag.Args() | 90 » fileNames := flagSet.Args() |
| 82 if len(fileNames) == 0 { | 91 if len(fileNames) == 0 { |
| 83 » » ErrorExit(fmt.Sprintf("No .mojom files given.\n"+ | 92 » » fmt.Fprintln(os.Stderr, "No .mojom files given.") |
| 84 » » » "Usage: %s [-I <include_dirs>] [-out <out_file>] [-debug ] [-meta-data-only] <mojom_file>...", | 93 » » printUsage() |
| 85 » » » filepath.Base(os.Args[0]))) | 94 » » os.Exit(1) |
| 86 } | 95 } |
| 87 | 96 |
| 88 parseDriver := parser.NewDriver(directoryListFlag, *debug, *metaDataOnly ) | 97 parseDriver := parser.NewDriver(directoryListFlag, *debug, *metaDataOnly ) |
| 89 | 98 |
| 90 // Do the parsing | 99 // Do the parsing |
| 91 descriptor, err := parseDriver.ParseFiles(fileNames) | 100 descriptor, err := parseDriver.ParseFiles(fileNames) |
| 92 if err != nil { | 101 if err != nil { |
| 93 ErrorExit(fmt.Sprintf("%s", err.Error())) | 102 ErrorExit(fmt.Sprintf("%s", err.Error())) |
| 94 } else if *debug { | 103 } else if *debug { |
| 95 fmt.Println("Parsing complete.") | 104 fmt.Println("Parsing complete.") |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 117 if err := ioutil.WriteFile(*outFile, bytes, os.ModePerm); err != nil { | 126 if err := ioutil.WriteFile(*outFile, bytes, os.ModePerm); err != nil { |
| 118 ErrorExit(fmt.Sprintf("Error writing output to %s: %s.", *outFile, err.Error())) | 127 ErrorExit(fmt.Sprintf("Error writing output to %s: %s.", *outFile, err.Error())) |
| 119 } else { | 128 } else { |
| 120 if *debug { | 129 if *debug { |
| 121 fmt.Printf("The output was written to %s.\n", *o utFile) | 130 fmt.Printf("The output was written to %s.\n", *o utFile) |
| 122 } | 131 } |
| 123 } | 132 } |
| 124 } | 133 } |
| 125 } | 134 } |
| 126 | 135 |
| 127 func ErrorExit(message string) { | |
| 128 fmt.Fprintf(os.Stderr, "%s\n", message) | |
| 129 os.Exit(1) | |
| 130 } | |
| 131 | |
| 132 func PrintDebugOutput(debugString string, descriptor *mojom.MojomDescriptor) { | 136 func PrintDebugOutput(debugString string, descriptor *mojom.MojomDescriptor) { |
| 133 fmt.Println("\n\n=============================================") | 137 fmt.Println("\n\n=============================================") |
| 134 fmt.Println("\n Pre-Serialized Go Object:") | 138 fmt.Println("\n Pre-Serialized Go Object:") |
| 135 fmt.Printf("\n%s\n", descriptor.String()) | 139 fmt.Printf("\n%s\n", descriptor.String()) |
| 136 fmt.Println("\n\n=============================================") | 140 fmt.Println("\n\n=============================================") |
| 137 fmt.Println("\n Debug Serialized Output:") | 141 fmt.Println("\n Debug Serialized Output:") |
| 138 fmt.Println(debugString) | 142 fmt.Println(debugString) |
| 139 } | 143 } |
| OLD | NEW |