aboutsummaryrefslogtreecommitdiff
path: root/src/urubu/cmd/vartan/compile.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/urubu/cmd/vartan/compile.go')
-rw-r--r--src/urubu/cmd/vartan/compile.go73
1 files changed, 13 insertions, 60 deletions
diff --git a/src/urubu/cmd/vartan/compile.go b/src/urubu/cmd/vartan/compile.go
index 79fa9ef..7b67a97 100644
--- a/src/urubu/cmd/vartan/compile.go
+++ b/src/urubu/cmd/vartan/compile.go
@@ -69,7 +69,7 @@ func runCompile(args []string) (retErr error) {
return err
}
- err = writeCompiledGrammarAndReport(gram, report, "")
+ err = writeCompiledGrammarAndReport(gram, report, os.Stdout)
if err != nil {
return fmt.Errorf("Cannot write an output files: %w", err)
}
@@ -123,68 +123,21 @@ func readGrammar(path string) (*spec.CompiledGrammar, *spec.Report, error) {
// The report file is named <grammar-name>.json.
// 3. When the path is an empty string, this function writes the compiled grammar to the stdout and writes
// the report to a file named <current-directory>/<grammar-name>-report.json.
-func writeCompiledGrammarAndReport(cgram *spec.CompiledGrammar, report *spec.Report, path string) error {
- cgramPath, reportPath, err := makeOutputFilePaths(cgram.Name, path)
- if err != nil {
- return err
+func writeCompiledGrammarAndReport(
+ cgram *spec.CompiledGrammar,
+ report *spec.Report,
+ w io.Writer,
+) error {
+ out := map[string]interface{}{
+ "grammar": cgram,
+ "report": report,
}
- {
- var cgramW io.Writer
- if cgramPath != "" {
- cgramFile, err := os.OpenFile(cgramPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
- if err != nil {
- return err
- }
- defer cgramFile.Close()
- cgramW = cgramFile
- } else {
- cgramW = os.Stdout
- }
-
- b, err := json.Marshal(cgram)
- if err != nil {
- return err
- }
- fmt.Fprintf(cgramW, "%v\n", string(b))
- }
-
- {
- reportFile, err := os.OpenFile(reportPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
- if err != nil {
- return err
- }
- defer reportFile.Close()
-
- b, err := json.Marshal(report)
- if err != nil {
- return err
- }
- fmt.Fprintf(reportFile, "%v\n", string(b))
+ b, err := json.Marshal(out)
+ if err != nil {
+ return err
}
+ fmt.Fprintf(w, "%v\n", string(b))
return nil
}
-
-func makeOutputFilePaths(gramName string, path string) (string, string, error) {
- reportFileName := gramName + "-report.json"
-
- if path == "" {
- wd, err := os.Getwd()
- if err != nil {
- return "", "", err
- }
- return "", filepath.Join(wd, reportFileName), nil
- }
-
- fi, err := os.Stat(path)
- if err != nil && !os.IsNotExist(err) {
- return "", "", err
- }
- if os.IsNotExist(err) || !fi.IsDir() {
- dir, _ := filepath.Split(path)
- return path, filepath.Join(dir, reportFileName), nil
- }
-
- return filepath.Join(path, gramName+".json"), filepath.Join(path, reportFileName), nil
-}