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.") |
| 79 » flagSet.SetOutput(ioutil.Discard) |
68 | 80 |
69 func init() { | 81 » printUsage := func() { |
70 » flag.Var(&directoryListFlag, "I", "comma-separated list of directory pat
hs to search for mojom imports") | 82 » » 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 } | 83 » » fmt.Fprintf(os.Stderr, UsageString(flagSet)) |
| 84 » } |
72 | 85 |
73 func main() { | 86 » 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 "+ | 87 » » if err != flag.ErrHelp { |
75 » » "be written to standard out.") | 88 » » » fmt.Fprintln(os.Stderr, err.Error()) |
76 » debug := flag.Bool("debug", false, "Generate debug data including the pa
rse tree and print it to standard out.") | 89 » » } |
77 » metaDataOnly := flag.Bool("meta-data-only", false, "Only parse file attr
ibutes and 'module' statement, "+ | 90 » » printUsage() |
78 » » "not mojom declarations or import statements.") | 91 » » os.Exit(1) |
79 » flag.Parse() | 92 » } |
80 | 93 |
81 » fileNames := flag.Args() | 94 » fileNames := flagSet.Args() |
82 if len(fileNames) == 0 { | 95 if len(fileNames) == 0 { |
83 » » ErrorExit(fmt.Sprintf("No .mojom files given.\n"+ | 96 » » fmt.Fprintln(os.Stderr, "No .mojom files given.") |
84 » » » "Usage: %s [-I <include_dirs>] [-out <out_file>] [-debug
] [-meta-data-only] <mojom_file>...", | 97 » » printUsage() |
85 » » » filepath.Base(os.Args[0]))) | 98 » » os.Exit(1) |
86 } | 99 } |
87 | 100 |
88 parseDriver := parser.NewDriver(directoryListFlag, *debug, *metaDataOnly
) | 101 parseDriver := parser.NewDriver(directoryListFlag, *debug, *metaDataOnly
) |
89 | 102 |
90 // Do the parsing | 103 // Do the parsing |
91 descriptor, err := parseDriver.ParseFiles(fileNames) | 104 descriptor, err := parseDriver.ParseFiles(fileNames) |
92 if err != nil { | 105 if err != nil { |
93 ErrorExit(fmt.Sprintf("%s", err.Error())) | 106 ErrorExit(fmt.Sprintf("%s", err.Error())) |
94 } else if *debug { | 107 } else if *debug { |
95 fmt.Println("Parsing complete.") | 108 fmt.Println("Parsing complete.") |
(...skipping 21 matching lines...) Expand all Loading... |
117 if err := ioutil.WriteFile(*outFile, bytes, os.ModePerm); err !=
nil { | 130 if err := ioutil.WriteFile(*outFile, bytes, os.ModePerm); err !=
nil { |
118 ErrorExit(fmt.Sprintf("Error writing output to %s: %s.",
*outFile, err.Error())) | 131 ErrorExit(fmt.Sprintf("Error writing output to %s: %s.",
*outFile, err.Error())) |
119 } else { | 132 } else { |
120 if *debug { | 133 if *debug { |
121 fmt.Printf("The output was written to %s.\n", *o
utFile) | 134 fmt.Printf("The output was written to %s.\n", *o
utFile) |
122 } | 135 } |
123 } | 136 } |
124 } | 137 } |
125 } | 138 } |
126 | 139 |
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) { | 140 func PrintDebugOutput(debugString string, descriptor *mojom.MojomDescriptor) { |
133 fmt.Println("\n\n=============================================") | 141 fmt.Println("\n\n=============================================") |
134 fmt.Println("\n Pre-Serialized Go Object:") | 142 fmt.Println("\n Pre-Serialized Go Object:") |
135 fmt.Printf("\n%s\n", descriptor.String()) | 143 fmt.Printf("\n%s\n", descriptor.String()) |
136 fmt.Println("\n\n=============================================") | 144 fmt.Println("\n\n=============================================") |
137 fmt.Println("\n Debug Serialized Output:") | 145 fmt.Println("\n Debug Serialized Output:") |
138 fmt.Println(debugString) | 146 fmt.Println(debugString) |
139 } | 147 } |
OLD | NEW |