aboutsummaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
Diffstat (limited to 'cmd')
-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
4 files changed, 199 insertions, 0 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
+}