aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2024-12-21 19:18:45 -0300
committerEuAndreh <eu@euandre.org>2024-12-21 19:18:45 -0300
commit63c768bd4922a3014056803849d85447f04ecdc9 (patch)
tree77bac07836623aecb93a4b1003c0ed10588a7e85
parentchmod +x mkdeps.sh (diff)
downloadcotia-63c768bd4922a3014056803849d85447f04ecdc9.tar.gz
cotia-63c768bd4922a3014056803849d85447f04ecdc9.tar.xz
Combine "CompiledGrammar" and "Report" payloads
Instead of one JSON file for each output, a single file with both outputs combined, each under its own key. The beauty of maps. Also instead of using a flag to determine where to put the data, just print to stdout and be done with it.
-rw-r--r--src/urubu/cmd/vartan/compile.go73
-rw-r--r--src/urubu/cmd/vartan/parse.go8
-rw-r--r--src/urubu/cmd/vartan/show.go6
-rw-r--r--src/urubu/spec/grammar.go5
4 files changed, 26 insertions, 66 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
-}
diff --git a/src/urubu/cmd/vartan/parse.go b/src/urubu/cmd/vartan/parse.go
index 9c5fd9c..41b8723 100644
--- a/src/urubu/cmd/vartan/parse.go
+++ b/src/urubu/cmd/vartan/parse.go
@@ -78,12 +78,14 @@ func readCompiledGrammar(path string) (*spec.CompiledGrammar, error) {
if err != nil {
return nil, err
}
- cg := &spec.CompiledGrammar{}
- err = json.Unmarshal(data, cg)
+
+ output := &spec.Output{}
+ err = json.Unmarshal(data, output)
if err != nil {
return nil, err
}
- return cg, nil
+
+ return &output.Grammar, nil
}
func writeSyntaxErrorMessage(b *strings.Builder, cgram *spec.CompiledGrammar, synErr *driver.SyntaxError) {
diff --git a/src/urubu/cmd/vartan/show.go b/src/urubu/cmd/vartan/show.go
index cab63a0..a7a8be9 100644
--- a/src/urubu/cmd/vartan/show.go
+++ b/src/urubu/cmd/vartan/show.go
@@ -40,13 +40,13 @@ func readReport(path string) (*spec.Report, error) {
return nil, err
}
- report := &spec.Report{}
- err = json.Unmarshal(d, report)
+ output := &spec.Output{}
+ err = json.Unmarshal(d, output)
if err != nil {
return nil, err
}
- return report, nil
+ return &output.Report, nil
}
const reportTemplate = `# Conflicts
diff --git a/src/urubu/spec/grammar.go b/src/urubu/spec/grammar.go
index c2708d8..1c2bdf2 100644
--- a/src/urubu/spec/grammar.go
+++ b/src/urubu/spec/grammar.go
@@ -82,6 +82,11 @@ type CompiledGrammar struct {
ASTAction *ASTAction `json:"ast_action"`
}
+type Output struct{
+ Grammar CompiledGrammar `json:"grammar"`
+ Report Report `json:"report"`
+}
+
// StateID represents an ID of a state of a transition table.
type StateID int