diff options
author | Ryo Nihei <nihei.dev@gmail.com> | 2021-08-05 00:31:50 +0900 |
---|---|---|
committer | Ryo Nihei <nihei.dev@gmail.com> | 2021-08-05 00:31:50 +0900 |
commit | b8ef7961255ed1d9ef5c51a92f1832c99c6d89cd (patch) | |
tree | 0530f34f3af55b0948583a09a727133218c867fe | |
parent | Generate an AST and a CST only when they are necessary (diff) | |
download | cotia-b8ef7961255ed1d9ef5c51a92f1832c99c6d89cd.tar.gz cotia-b8ef7961255ed1d9ef5c51a92f1832c99c6d89cd.tar.xz |
Add --cst option to the parse command
When this option is enabled, the parser generates a CST.
-rw-r--r-- | cmd/vartan/parse.go | 30 |
1 files changed, 23 insertions, 7 deletions
diff --git a/cmd/vartan/parse.go b/cmd/vartan/parse.go index 95b61b2..1311ce6 100644 --- a/cmd/vartan/parse.go +++ b/cmd/vartan/parse.go @@ -11,8 +11,9 @@ import ( "github.com/spf13/cobra" ) -var lexFlags = struct { +var parseFlags = struct { source *string + cst *bool }{} func init() { @@ -23,7 +24,8 @@ func init() { Args: cobra.ExactArgs(1), RunE: runParse, } - lexFlags.source = cmd.Flags().StringP("source", "s", "", "source file path (default stdin)") + parseFlags.source = cmd.Flags().StringP("source", "s", "", "source file path (default stdin)") + parseFlags.cst = cmd.Flags().Bool("cst", false, "when this option is enabled, the parser generates a CST") rootCmd.AddCommand(cmd) } @@ -54,15 +56,22 @@ func runParse(cmd *cobra.Command, args []string) (retErr error) { var p *driver.Parser { src := os.Stdin - if *lexFlags.source != "" { - f, err := os.Open(*lexFlags.source) + if *parseFlags.source != "" { + f, err := os.Open(*parseFlags.source) if err != nil { - return fmt.Errorf("Cannot open the source file %s: %w", *lexFlags.source, err) + return fmt.Errorf("Cannot open the source file %s: %w", *parseFlags.source, err) } defer f.Close() src = f } - p, err = driver.NewParser(cgram, src, driver.MakeAST()) + + var opts []driver.ParserOption + if *parseFlags.cst { + opts = append(opts, driver.MakeCST()) + } else { + opts = append(opts, driver.MakeAST()) + } + p, err = driver.NewParser(cgram, src, opts...) if err != nil { return err } @@ -72,8 +81,15 @@ func runParse(cmd *cobra.Command, args []string) (retErr error) { if err != nil { return err } + + var tree *driver.Node + if *parseFlags.cst { + tree = p.CST() + } else { + tree = p.AST() + } fmt.Printf("Accepted\n") - driver.PrintTree(os.Stdout, p.AST()) + driver.PrintTree(os.Stdout, tree) return nil } |