| 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 parseCmd which parses a set of .mojom files and emits a | 6 This file contains parseCmd which parses a set of .mojom files and emits a |
| 7 serialized MojomFileGraph struct. See mojom_files.mojom for the definition of | 7 serialized MojomFileGraph struct. See mojom_files.mojom for the definition of |
| 8 MojomFileGraph. | 8 MojomFileGraph. |
| 9 | 9 |
| 10 The parse command is invoked as follows: | 10 The parse command is invoked as follows: |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 data. | 26 data. |
| 27 */ | 27 */ |
| 28 | 28 |
| 29 package main | 29 package main |
| 30 | 30 |
| 31 import ( | 31 import ( |
| 32 "bufio" | 32 "bufio" |
| 33 "flag" | 33 "flag" |
| 34 "fmt" | 34 "fmt" |
| 35 "io/ioutil" | 35 "io/ioutil" |
| 36 "log" |
| 36 "mojom/mojom_parser/mojom" | 37 "mojom/mojom_parser/mojom" |
| 37 "mojom/mojom_parser/parser" | 38 "mojom/mojom_parser/parser" |
| 38 "mojom/mojom_parser/serialization" | 39 "mojom/mojom_parser/serialization" |
| 39 "os" | 40 "os" |
| 40 "path/filepath" | 41 "path/filepath" |
| 41 "strings" | 42 "strings" |
| 42 ) | 43 ) |
| 43 | 44 |
| 44 // DirectoryList holds the result of parsing a command-line flag | 45 // DirectoryList holds the result of parsing a command-line flag |
| 45 // that accepts a comma-separated list of directory paths. This | 46 // that accepts a comma-separated list of directory paths. This |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 fmt.Fprintln(os.Stderr, "No .mojom files given.") | 97 fmt.Fprintln(os.Stderr, "No .mojom files given.") |
| 97 printUsage() | 98 printUsage() |
| 98 os.Exit(1) | 99 os.Exit(1) |
| 99 } | 100 } |
| 100 | 101 |
| 101 parseDriver := parser.NewDriver(directoryListFlag, *debug, *metaDataOnly
) | 102 parseDriver := parser.NewDriver(directoryListFlag, *debug, *metaDataOnly
) |
| 102 | 103 |
| 103 // Do the parsing | 104 // Do the parsing |
| 104 descriptor, err := parseDriver.ParseFiles(fileNames) | 105 descriptor, err := parseDriver.ParseFiles(fileNames) |
| 105 if err != nil { | 106 if err != nil { |
| 106 » » ErrorExit(fmt.Sprintf("%s", err.Error())) | 107 » » log.Fatalln(err.Error()) |
| 107 } else if *debug { | 108 } else if *debug { |
| 108 fmt.Println("Parsing complete.") | 109 fmt.Println("Parsing complete.") |
| 109 } | 110 } |
| 110 | 111 |
| 111 // Serialize the result. | 112 // Serialize the result. |
| 112 bytes, debug_string, err := serialization.Serialize(descriptor, *debug) | 113 bytes, debug_string, err := serialization.Serialize(descriptor, *debug) |
| 113 if err != nil { | 114 if err != nil { |
| 114 » » ErrorExit(fmt.Sprintf("Serialization error: %s", err)) | 115 » » log.Fatalf("Serialization error: %s\n", err) |
| 115 } | 116 } |
| 116 | 117 |
| 117 // In debug mode print out the debug information. | 118 // In debug mode print out the debug information. |
| 118 if *debug { | 119 if *debug { |
| 119 PrintDebugOutput(debug_string, descriptor) | 120 PrintDebugOutput(debug_string, descriptor) |
| 120 } | 121 } |
| 121 | 122 |
| 122 // Emit the output to a file or standard out. | 123 // Emit the output to a file or standard out. |
| 123 if len(*outFile) == 0 { | 124 if len(*outFile) == 0 { |
| 124 w := bufio.NewWriter(os.Stdout) | 125 w := bufio.NewWriter(os.Stdout) |
| 125 if _, err := w.Write(bytes); err != nil { | 126 if _, err := w.Write(bytes); err != nil { |
| 126 » » » ErrorExit(fmt.Sprintf("Error writing output to standard
out: %s.", *outFile, err.Error())) | 127 » » » log.Fatalf("Error writing output to standard out: %s.\n"
, err.Error()) |
| 127 } | 128 } |
| 128 w.Flush() | 129 w.Flush() |
| 129 } else { | 130 } else { |
| 130 if err := ioutil.WriteFile(*outFile, bytes, os.ModePerm); err !=
nil { | 131 if err := ioutil.WriteFile(*outFile, bytes, os.ModePerm); err !=
nil { |
| 131 » » » ErrorExit(fmt.Sprintf("Error writing output to %s: %s.",
*outFile, err.Error())) | 132 » » » log.Fatalf("Error writing output to %s: %s.", *outFile,
err.Error()) |
| 132 } else { | 133 } else { |
| 133 if *debug { | 134 if *debug { |
| 134 fmt.Printf("The output was written to %s.\n", *o
utFile) | 135 fmt.Printf("The output was written to %s.\n", *o
utFile) |
| 135 } | 136 } |
| 136 } | 137 } |
| 137 } | 138 } |
| 138 } | 139 } |
| 139 | 140 |
| 140 func PrintDebugOutput(debugString string, descriptor *mojom.MojomDescriptor) { | 141 func PrintDebugOutput(debugString string, descriptor *mojom.MojomDescriptor) { |
| 141 fmt.Println("\n\n=============================================") | 142 fmt.Println("\n\n=============================================") |
| 142 fmt.Println("\n Pre-Serialized Go Object:") | 143 fmt.Println("\n Pre-Serialized Go Object:") |
| 143 fmt.Printf("\n%s\n", descriptor.String()) | 144 fmt.Printf("\n%s\n", descriptor.String()) |
| 144 fmt.Println("\n\n=============================================") | 145 fmt.Println("\n\n=============================================") |
| 145 fmt.Println("\n Debug Serialized Output:") | 146 fmt.Println("\n Debug Serialized Output:") |
| 146 fmt.Println(debugString) | 147 fmt.Println(debugString) |
| 147 } | 148 } |
| OLD | NEW |