aboutsummaryrefslogtreecommitdiff
path: root/cli/cmd/compile.go
diff options
context:
space:
mode:
authorRyo Nihei <nihei.dev@gmail.com>2021-05-08 16:34:13 +0900
committerRyo Nihei <nihei.dev@gmail.com>2021-05-08 16:34:13 +0900
commitaa73b03f626189789224c3cc9a0d18e288a5a7b4 (patch)
treebda7c0e2238418d86b9d2a8bb000908513c1639d /cli/cmd/compile.go
parentChange type of acceping_states to slice (diff)
downloadtre-aa73b03f626189789224c3cc9a0d18e288a5a7b4.tar.gz
tre-aa73b03f626189789224c3cc9a0d18e288a5a7b4.tar.xz
Add CLI options
Diffstat (limited to 'cli/cmd/compile.go')
-rw-r--r--cli/cmd/compile.go71
1 files changed, 50 insertions, 21 deletions
diff --git a/cli/cmd/compile.go b/cli/cmd/compile.go
index 6ad64b6..f2d56fa 100644
--- a/cli/cmd/compile.go
+++ b/cli/cmd/compile.go
@@ -3,7 +3,6 @@ package cmd
import (
"encoding/json"
"fmt"
- "io"
"io/ioutil"
"os"
"time"
@@ -13,6 +12,12 @@ import (
"github.com/spf13/cobra"
)
+var compileFlags = struct {
+ debug *bool
+ lexSpec *string
+ output *string
+}{}
+
func init() {
cmd := &cobra.Command{
Use: "compile",
@@ -21,41 +26,47 @@ func init() {
Example: ` cat lexspec.json | maleeni compile > clexspec.json`,
RunE: runCompile,
}
+ compileFlags.debug = cmd.Flags().BoolP("debug", "d", false, "enable logging")
+ compileFlags.lexSpec = cmd.Flags().StringP("lex-spec", "l", "", "lexical specification file path (default: stdin)")
+ compileFlags.output = cmd.Flags().StringP("output", "o", "", "output file path (default: stdout)")
rootCmd.AddCommand(cmd)
}
func runCompile(cmd *cobra.Command, args []string) (retErr error) {
- lspec, err := readLexSpec()
+ lspec, err := readLexSpec(*compileFlags.lexSpec)
if err != nil {
return fmt.Errorf("Cannot read a lexical specification: %w", err)
}
- var w io.Writer
- {
+
+ var opts []compiler.CompilerOption
+ if *compileFlags.debug {
fileName := "maleeni-compile.log"
f, err := os.OpenFile(fileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return fmt.Errorf("Cannot open the log file %s: %w", fileName, err)
}
defer f.Close()
- w = f
- }
- fmt.Fprintf(w, `maleeni compile starts.
+ fmt.Fprintf(f, `maleeni compile starts.
Date time: %v
---
`, time.Now().Format(time.RFC3339))
- defer func() {
- fmt.Fprintf(w, "---\n")
- if retErr != nil {
- fmt.Fprintf(w, "maleeni compile failed: %v\n", retErr)
- } else {
- fmt.Fprintf(w, "maleeni compile succeeded.\n")
- }
- }()
- clspec, err := compiler.Compile(lspec, compiler.EnableLogging(w))
+ defer func() {
+ fmt.Fprintf(f, "---\n")
+ if retErr != nil {
+ fmt.Fprintf(f, "maleeni compile failed: %v\n", retErr)
+ } else {
+ fmt.Fprintf(f, "maleeni compile succeeded.\n")
+ }
+ }()
+
+ opts = append(opts, compiler.EnableLogging(f))
+ }
+
+ clspec, err := compiler.Compile(lspec, opts...)
if err != nil {
return err
}
- err = writeCompiledLexSpec(clspec)
+ err = writeCompiledLexSpec(clspec, *compileFlags.output)
if err != nil {
return fmt.Errorf("Cannot write a compiled lexical specification: %w", err)
}
@@ -63,8 +74,17 @@ Date time: %v
return nil
}
-func readLexSpec() (*spec.LexSpec, error) {
- data, err := ioutil.ReadAll(os.Stdin)
+func readLexSpec(path string) (*spec.LexSpec, error) {
+ r := os.Stdin
+ if path != "" {
+ f, err := os.Open(path)
+ if err != nil {
+ return nil, fmt.Errorf("Cannot open the lexical specification file %s: %w", path, err)
+ }
+ defer f.Close()
+ r = f
+ }
+ data, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
@@ -76,11 +96,20 @@ func readLexSpec() (*spec.LexSpec, error) {
return lspec, nil
}
-func writeCompiledLexSpec(clspec *spec.CompiledLexSpec) error {
+func writeCompiledLexSpec(clspec *spec.CompiledLexSpec, path string) error {
out, err := json.Marshal(clspec)
if err != nil {
return err
}
- fmt.Fprintf(os.Stdout, "%v\n", string(out))
+ w := os.Stdout
+ if path != "" {
+ f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
+ if err != nil {
+ return fmt.Errorf("Cannot open the output file %s: %w", path, err)
+ }
+ defer f.Close()
+ w = f
+ }
+ fmt.Fprintf(w, "%v\n", string(out))
return nil
}