Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(78)

Side by Side Diff: mojom/mojom_parser/parse_cmd.go

Issue 1702873002: Mojom formatter incorporated in the mojom binary. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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_cmd", flag.ContinueOnError)
rudominer 2016/02/19 05:49:16 The string "parse_cmd" will show up in user-facing
azani 2016/02/19 19:28:30 Done.
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())
75 » » "be written to standard out.") 87 » » printUsage()
rudominer 2016/02/19 05:49:16 I patched in your cl and played around with this.
azani 2016/02/19 19:28:30 Done.
76 » debug := flag.Bool("debug", false, "Generate debug data including the pa rse tree and print it to standard out.") 88 » » os.Exit(1)
77 » metaDataOnly := flag.Bool("meta-data-only", false, "Only parse file attr ibutes and 'module' statement, "+ 89 » }
78 » » "not mojom declarations or import statements.")
79 » flag.Parse()
80 90
81 » fileNames := flag.Args() 91 » fileNames := flagSet.Args()
82 if len(fileNames) == 0 { 92 if len(fileNames) == 0 {
83 » » ErrorExit(fmt.Sprintf("No .mojom files given.\n"+ 93 » » fmt.Fprintln(os.Stderr, "No .mojom files given.")
84 » » » "Usage: %s [-I <include_dirs>] [-out <out_file>] [-debug ] [-meta-data-only] <mojom_file>...", 94 » » printUsage()
85 » » » filepath.Base(os.Args[0]))) 95 » » os.Exit(1)
86 } 96 }
87 97
88 parseDriver := parser.NewDriver(directoryListFlag, *debug, *metaDataOnly ) 98 parseDriver := parser.NewDriver(directoryListFlag, *debug, *metaDataOnly )
89 99
90 // Do the parsing 100 // Do the parsing
91 descriptor, err := parseDriver.ParseFiles(fileNames) 101 descriptor, err := parseDriver.ParseFiles(fileNames)
92 if err != nil { 102 if err != nil {
93 ErrorExit(fmt.Sprintf("%s", err.Error())) 103 ErrorExit(fmt.Sprintf("%s", err.Error()))
94 } else if *debug { 104 } else if *debug {
95 fmt.Println("Parsing complete.") 105 fmt.Println("Parsing complete.")
(...skipping 21 matching lines...) Expand all
117 if err := ioutil.WriteFile(*outFile, bytes, os.ModePerm); err != nil { 127 if err := ioutil.WriteFile(*outFile, bytes, os.ModePerm); err != nil {
118 ErrorExit(fmt.Sprintf("Error writing output to %s: %s.", *outFile, err.Error())) 128 ErrorExit(fmt.Sprintf("Error writing output to %s: %s.", *outFile, err.Error()))
119 } else { 129 } else {
120 if *debug { 130 if *debug {
121 fmt.Printf("The output was written to %s.\n", *o utFile) 131 fmt.Printf("The output was written to %s.\n", *o utFile)
122 } 132 }
123 } 133 }
124 } 134 }
125 } 135 }
126 136
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) { 137 func PrintDebugOutput(debugString string, descriptor *mojom.MojomDescriptor) {
133 fmt.Println("\n\n=============================================") 138 fmt.Println("\n\n=============================================")
134 fmt.Println("\n Pre-Serialized Go Object:") 139 fmt.Println("\n Pre-Serialized Go Object:")
135 fmt.Printf("\n%s\n", descriptor.String()) 140 fmt.Printf("\n%s\n", descriptor.String())
136 fmt.Println("\n\n=============================================") 141 fmt.Println("\n\n=============================================")
137 fmt.Println("\n Debug Serialized Output:") 142 fmt.Println("\n Debug Serialized Output:")
138 fmt.Println(debugString) 143 fmt.Println(debugString)
139 } 144 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698