aboutsummaryrefslogtreecommitdiff
path: root/cli/cmd/compile.go
diff options
context:
space:
mode:
authorRyo Nihei <nihei.dev@gmail.com>2021-02-16 01:24:27 +0900
committerRyo Nihei <nihei.dev@gmail.com>2021-02-16 01:43:13 +0900
commit01f1d86b8237755565a1e7fd3e555f557af2114e (patch)
treee962f58692efd7d4388f37db982a021930d094e8 /cli/cmd/compile.go
parentAdd types of lexical specifications (diff)
downloadtre-01f1d86b8237755565a1e7fd3e555f557af2114e.tar.gz
tre-01f1d86b8237755565a1e7fd3e555f557af2114e.tar.xz
Add CLI
Diffstat (limited to 'cli/cmd/compile.go')
-rw-r--r--cli/cmd/compile.go49
1 files changed, 49 insertions, 0 deletions
diff --git a/cli/cmd/compile.go b/cli/cmd/compile.go
new file mode 100644
index 0000000..23aa120
--- /dev/null
+++ b/cli/cmd/compile.go
@@ -0,0 +1,49 @@
+package cmd
+
+import (
+ "encoding/json"
+ "fmt"
+ "io/ioutil"
+ "os"
+
+ "github.com/nihei9/maleeni/compiler"
+ "github.com/nihei9/maleeni/spec"
+ "github.com/spf13/cobra"
+)
+
+func init() {
+ cmd := &cobra.Command{
+ Use: "compile",
+ Short: "Compile a lexical specification into a DFA",
+ Long: `compile takes a lexical specification and generates a DFA accepting the tokens described in the specification.`,
+ Example: ` cat lexspec.json | maleeni compile > clexspec.json`,
+ RunE: runCompile,
+ }
+ rootCmd.AddCommand(cmd)
+}
+
+func runCompile(cmd *cobra.Command, args []string) error {
+ var lspec *spec.LexSpec
+ {
+ data, err := ioutil.ReadAll(os.Stdin)
+ if err != nil {
+ return err
+ }
+ lspec = &spec.LexSpec{}
+ err = json.Unmarshal(data, lspec)
+ if err != nil {
+ return err
+ }
+ }
+ clspec, err := compiler.Compile(lspec)
+ if err != nil {
+ return err
+ }
+ out, err := json.Marshal(clspec)
+ if err != nil {
+ return err
+ }
+ fmt.Fprintf(os.Stdout, "%v\n", string(out))
+
+ return nil
+}