aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cmd/vartan/compile.go82
-rw-r--r--cmd/vartan/main.go12
-rw-r--r--cmd/vartan/parse.go77
-rw-r--r--cmd/vartan/root.go28
-rw-r--r--go.mod5
-rw-r--r--go.sum3
6 files changed, 206 insertions, 1 deletions
diff --git a/cmd/vartan/compile.go b/cmd/vartan/compile.go
new file mode 100644
index 0000000..769f143
--- /dev/null
+++ b/cmd/vartan/compile.go
@@ -0,0 +1,82 @@
+package main
+
+import (
+ "encoding/json"
+ "fmt"
+ "os"
+
+ "github.com/nihei9/vartan/grammar"
+ "github.com/nihei9/vartan/spec"
+ "github.com/spf13/cobra"
+)
+
+var compileFlags = struct {
+ grammar *string
+ output *string
+}{}
+
+func init() {
+ cmd := &cobra.Command{
+ Use: "compile",
+ Short: "Compile a grammar into a parsing table",
+ Example: ` cat grammar | vartan compile -o grammar.json`,
+ RunE: runCompile,
+ }
+ compileFlags.grammar = cmd.Flags().StringP("grammar", "g", "", "grammar 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) {
+ gram, err := readGrammar(*compileFlags.grammar)
+ if err != nil {
+ return err
+ }
+
+ cgram, err := grammar.Compile(gram)
+ if err != nil {
+ return err
+ }
+
+ err = writeCompiledGrammar(cgram, *compileFlags.output)
+ if err != nil {
+ return fmt.Errorf("Cannot write a compiled grammar: %w", err)
+ }
+
+ return nil
+}
+
+func readGrammar(path string) (*grammar.Grammar, error) {
+ r := os.Stdin
+ if path != "" {
+ f, err := os.Open(path)
+ if err != nil {
+ return nil, fmt.Errorf("Cannot open the grammar file %s: %w", path, err)
+ }
+ defer f.Close()
+ r = f
+ }
+ ast, err := spec.Parse(r)
+ if err != nil {
+ return nil, err
+ }
+ return grammar.NewGrammar(ast)
+}
+
+func writeCompiledGrammar(cgram *spec.CompiledGrammar, path string) error {
+ out, err := json.Marshal(cgram)
+ if err != nil {
+ return err
+ }
+ 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
+}
diff --git a/cmd/vartan/main.go b/cmd/vartan/main.go
new file mode 100644
index 0000000..701f02f
--- /dev/null
+++ b/cmd/vartan/main.go
@@ -0,0 +1,12 @@
+package main
+
+import (
+ "os"
+)
+
+func main() {
+ err := Execute()
+ if err != nil {
+ os.Exit(1)
+ }
+}
diff --git a/cmd/vartan/parse.go b/cmd/vartan/parse.go
new file mode 100644
index 0000000..0f262b4
--- /dev/null
+++ b/cmd/vartan/parse.go
@@ -0,0 +1,77 @@
+package main
+
+import (
+ "encoding/json"
+ "fmt"
+ "io/ioutil"
+ "os"
+
+ "github.com/nihei9/vartan/driver"
+ "github.com/nihei9/vartan/spec"
+ "github.com/spf13/cobra"
+)
+
+var lexFlags = struct {
+ source *string
+}{}
+
+func init() {
+ cmd := &cobra.Command{
+ Use: "parse <grammar file path>",
+ Short: "Parse a text stream",
+ Example: ` cat src | vartan parse grammar.json`,
+ Args: cobra.ExactArgs(1),
+ RunE: runParse,
+ }
+ lexFlags.source = cmd.Flags().StringP("source", "s", "", "source file path (default stdin)")
+ rootCmd.AddCommand(cmd)
+}
+
+func runParse(cmd *cobra.Command, args []string) (retErr error) {
+ cgram, err := readCompiledGrammar(args[0])
+ if err != nil {
+ return fmt.Errorf("Cannot read a compiled grammar: %w", err)
+ }
+
+ var p *driver.Parser
+ {
+ src := os.Stdin
+ if *lexFlags.source != "" {
+ f, err := os.Open(*lexFlags.source)
+ if err != nil {
+ return fmt.Errorf("Cannot open the source file %s: %w", *lexFlags.source, err)
+ }
+ defer f.Close()
+ src = f
+ }
+ p, err = driver.NewParser(cgram, src)
+ if err != nil {
+ return err
+ }
+ }
+
+ err = p.Parse()
+ if err != nil {
+ return err
+ }
+ fmt.Printf("Accepted\n")
+
+ return nil
+}
+
+func readCompiledGrammar(path string) (*spec.CompiledGrammar, error) {
+ f, err := os.Open(path)
+ if err != nil {
+ return nil, err
+ }
+ data, err := ioutil.ReadAll(f)
+ if err != nil {
+ return nil, err
+ }
+ cgram := &spec.CompiledGrammar{}
+ err = json.Unmarshal(data, cgram)
+ if err != nil {
+ return nil, err
+ }
+ return cgram, nil
+}
diff --git a/cmd/vartan/root.go b/cmd/vartan/root.go
new file mode 100644
index 0000000..ad163a6
--- /dev/null
+++ b/cmd/vartan/root.go
@@ -0,0 +1,28 @@
+package main
+
+import (
+ "fmt"
+ "os"
+
+ "github.com/spf13/cobra"
+)
+
+var rootCmd = &cobra.Command{
+ Use: "vartan",
+ Short: "Generate a portable parsing table from a grammar",
+ Long: `vartan provides two features:
+- Generates a portable parsing table from a grammar.
+- Tokenizes a text stream according to the grammar.
+ This feature is primarily aimed at debugging the grammar.`,
+ SilenceErrors: true,
+ SilenceUsage: true,
+}
+
+func Execute() error {
+ err := rootCmd.Execute()
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "%v\n", err)
+ return err
+ }
+ return nil
+}
diff --git a/go.mod b/go.mod
index c8670d8..3e16f7d 100644
--- a/go.mod
+++ b/go.mod
@@ -2,4 +2,7 @@ module github.com/nihei9/vartan
go 1.16
-require github.com/nihei9/maleeni v0.1.0
+require (
+ github.com/nihei9/maleeni v0.1.0
+ github.com/spf13/cobra v1.1.3
+)
diff --git a/go.sum b/go.sum
index 111d665..050de01 100644
--- a/go.sum
+++ b/go.sum
@@ -88,6 +88,7 @@ github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO
github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ=
github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I=
github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc=
+github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
@@ -148,9 +149,11 @@ github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4k
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
+github.com/spf13/cobra v1.1.3 h1:xghbfqPkxzxP3C/f3n5DdpAbdKLj4ZE4BWQI362l53M=
github.com/spf13/cobra v1.1.3/go.mod h1:pGADOWyqRD/YMrPZigI/zbliZ2wVD/23d+is3pSWzOo=
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
+github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/viper v1.7.0/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5qpdg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=