| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 /* | |
| 6 This file contains the main() for the Mojom parser binary. This tool parses | |
| 7 a set of .mojom files and emits a serialized MojomFileGraph struct. See | |
| 8 mojom_files.mojom for the definition of MojomFileGraph. | |
| 9 | |
| 10 The tool is invoked as follows: | |
| 11 | |
| 12 mojom_parser [-I <include_dirs>] [-out <out_file>] [-debug] <mojom_file>... | |
| 13 | |
| 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. | |
| 16 <mojom_file>... is one or more paths to .mojom files to be parsed. | |
| 17 | |
| 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 | |
| 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. | |
| 22 | |
| 23 If -debug is specified then the program emits lots of debugging data to | |
| 24 standard out, including a depiction of the parse trees. If also <out_file> is | |
| 25 not specified then the actual output is written at the end after the debugging | |
| 26 data. | |
| 27 */ | |
| 28 | |
| 29 package main | |
| 30 | |
| 31 import ( | |
| 32 "bufio" | |
| 33 "flag" | |
| 34 "fmt" | |
| 35 "io/ioutil" | |
| 36 "mojom/mojom_parser/mojom" | |
| 37 "mojom/mojom_parser/parser" | |
| 38 "mojom/mojom_parser/serialization" | |
| 39 "os" | |
| 40 "path/filepath" | |
| 41 "strings" | |
| 42 ) | |
| 43 | |
| 44 // DirectoryList holds the result of parsing a command-line flag | |
| 45 // that accepts a comma-separated list of directory paths. This | |
| 46 // type satisfies the flag.Value interface. | |
| 47 type DirectoryList []string | |
| 48 | |
| 49 func (dl *DirectoryList) String() string { | |
| 50 return fmt.Sprintf("%v", *dl) | |
| 51 } | |
| 52 | |
| 53 func (dl *DirectoryList) Set(args string) error { | |
| 54 for _, name := range strings.Split(args, ",") { | |
| 55 info, err := os.Stat(name) | |
| 56 if err != nil { | |
| 57 return err | |
| 58 } | |
| 59 if !info.IsDir() { | |
| 60 return fmt.Errorf("%s is not a directory.", name) | |
| 61 } | |
| 62 *dl = append(*dl, name) | |
| 63 } | |
| 64 return nil | |
| 65 } | |
| 66 | |
| 67 var directoryListFlag DirectoryList | |
| 68 | |
| 69 func init() { | |
| 70 flag.Var(&directoryListFlag, "I", "comma-separated list of directory pat
hs to search for mojom imports") | |
| 71 } | |
| 72 | |
| 73 func main() { | |
| 74 outFile := flag.String("out", "", "The path to the output file. If not p
resent the output will "+ | |
| 75 "be written to standard out.") | |
| 76 debug := flag.Bool("debug", false, "Generate debug data including the pa
rse tree and print it to standard out.") | |
| 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 | |
| 81 fileNames := flag.Args() | |
| 82 if len(fileNames) == 0 { | |
| 83 ErrorExit(fmt.Sprintf("No .mojom files given.\n"+ | |
| 84 "Usage: %s [-I <include_dirs>] [-out <out_file>] [-debug
] [-meta-data-only] <mojom_file>...", | |
| 85 filepath.Base(os.Args[0]))) | |
| 86 } | |
| 87 | |
| 88 parseDriver := parser.NewDriver(directoryListFlag, *debug, *metaDataOnly
) | |
| 89 | |
| 90 // Do the parsing | |
| 91 descriptor, err := parseDriver.ParseFiles(fileNames) | |
| 92 if err != nil { | |
| 93 ErrorExit(fmt.Sprintf("%s", err.Error())) | |
| 94 } else if *debug { | |
| 95 fmt.Println("Parsing complete.") | |
| 96 } | |
| 97 | |
| 98 // Serialize the result. | |
| 99 bytes, debug_string, err := serialization.Serialize(descriptor, *debug) | |
| 100 if err != nil { | |
| 101 ErrorExit(fmt.Sprintf("Serialization error: %s", err)) | |
| 102 } | |
| 103 | |
| 104 // In debug mode print out the debug information. | |
| 105 if *debug { | |
| 106 PrintDebugOutput(debug_string, descriptor) | |
| 107 } | |
| 108 | |
| 109 // Emit the output to a file or standard out. | |
| 110 if len(*outFile) == 0 { | |
| 111 w := bufio.NewWriter(os.Stdout) | |
| 112 if _, err := w.Write(bytes); err != nil { | |
| 113 ErrorExit(fmt.Sprintf("Error writing output to standard
out: %s.", *outFile, err.Error())) | |
| 114 } | |
| 115 w.Flush() | |
| 116 } else { | |
| 117 if err := ioutil.WriteFile(*outFile, bytes, os.ModePerm); err !=
nil { | |
| 118 ErrorExit(fmt.Sprintf("Error writing output to %s: %s.",
*outFile, err.Error())) | |
| 119 } else { | |
| 120 if *debug { | |
| 121 fmt.Printf("The output was written to %s.\n", *o
utFile) | |
| 122 } | |
| 123 } | |
| 124 } | |
| 125 } | |
| 126 | |
| 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) { | |
| 133 fmt.Println("\n\n=============================================") | |
| 134 fmt.Println("\n Pre-Serialized Go Object:") | |
| 135 fmt.Printf("\n%s\n", descriptor.String()) | |
| 136 fmt.Println("\n\n=============================================") | |
| 137 fmt.Println("\n Debug Serialized Output:") | |
| 138 fmt.Println(debugString) | |
| 139 } | |
| OLD | NEW |