diff options
Diffstat (limited to 'cmd/vartan/parse.go')
-rw-r--r-- | cmd/vartan/parse.go | 119 |
1 files changed, 21 insertions, 98 deletions
diff --git a/cmd/vartan/parse.go b/cmd/vartan/parse.go index 50d867e..c876c09 100644 --- a/cmd/vartan/parse.go +++ b/cmd/vartan/parse.go @@ -9,100 +9,33 @@ import ( driver "driver/parser" spec "spec/grammar" - "tester" ) -var parseFlags = struct { - source *string - onlyParse *bool - cst *bool - disableLAC *bool - format *string -}{} - -const ( - outputFormatText = "text" - outputFormatTree = "tree" - outputFormatJSON = "json" -) -/* -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, - } - parseFlags.source = cmd.Flags().StringP("source", "s", "", "source file path (default stdin)") - parseFlags.onlyParse = cmd.Flags().Bool("only-parse", false, "when this option is enabled, the parser performs only parse and doesn't semantic actions") - parseFlags.cst = cmd.Flags().Bool("cst", false, "when this option is enabled, the parser generates a CST") - parseFlags.disableLAC = cmd.Flags().Bool("disable-lac", false, "disable LAC (lookahead correction)") - parseFlags.format = cmd.Flags().StringP("format", "f", "text", "output format: one of text|tree|json") - rootCmd.AddCommand(cmd) -} -*/ func runParse(args []string) error { - if *parseFlags.onlyParse && *parseFlags.cst { - return fmt.Errorf("You cannot enable --only-parse and --cst at the same time") - } - if *parseFlags.format != outputFormatText && - *parseFlags.format != outputFormatTree && - *parseFlags.format != outputFormatJSON { - return fmt.Errorf("invalid output format: %v", *parseFlags.format) - } - cg, err := readCompiledGrammar(args[0]) if err != nil { return fmt.Errorf("Cannot read a compiled grammar: %w", err) } - var p *driver.Parser - var treeAct *driver.SyntaxTreeActionSet - var tb *driver.DefaultSyntaxTreeBuilder - { - src := os.Stdin - if *parseFlags.source != "" { - f, err := os.Open(*parseFlags.source) - if err != nil { - return fmt.Errorf("Cannot open the source file %s: %w", *parseFlags.source, err) - } - defer f.Close() - src = f - } + src := os.Stdin + gram := driver.NewGrammar(cg) - gram := driver.NewGrammar(cg) - - var opts []driver.ParserOption - { - switch { - case *parseFlags.cst: - tb = driver.NewDefaultSyntaxTreeBuilder() - treeAct = driver.NewCSTActionSet(gram, tb) - case !*parseFlags.onlyParse: - tb = driver.NewDefaultSyntaxTreeBuilder() - treeAct = driver.NewASTActionSet(gram, tb) - } - if treeAct != nil { - opts = append(opts, driver.SemanticAction(treeAct)) - } - - if *parseFlags.disableLAC { - opts = append(opts, driver.DisableLAC()) - } - } + tb := driver.NewDefaultSyntaxTreeBuilder() + treeAct := driver.NewCSTActionSet(gram, tb) - toks, err := driver.NewTokenStream(cg, src) - if err != nil { - return err - } + opts := []driver.ParserOption{} + opts = append(opts, driver.SemanticAction(treeAct)) - p, err = driver.NewParser(toks, gram, opts...) - if err != nil { - return err - } + toks, err := driver.NewTokenStream(cg, src) + if err != nil { + return err + } + + p, err := driver.NewParser(toks, gram, opts...) + if err != nil { + return err } err = p.Parse() @@ -110,24 +43,14 @@ func runParse(args []string) error { return err } - if !*parseFlags.onlyParse { - // A parser can construct a parse tree even if syntax errors occur. - // When therer is a parse tree, print it. - if tree := tb.Tree(); tree != nil { - switch *parseFlags.format { - case "tree": - b := tester.ConvertSyntaxTreeToTestableTree(tree).Format() - fmt.Fprintln(os.Stdout, string(b)) - case "json": - b, err := json.Marshal(tree) - if err != nil { - return err - } - fmt.Fprintln(os.Stdout, string(b)) - default: - driver.PrintTree(os.Stdout, tree) - } + // A parser can construct a parse tree even if syntax errors occur. + // When therer is a parse tree, print it. + if tree := tb.Tree(); tree != nil { + b, err := json.Marshal(tree) + if err != nil { + return err } + fmt.Fprintln(os.Stdout, string(b)) } if len(p.SyntaxErrors()) > 0 { |