Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added debug for cli parse #776

Merged
merged 1 commit into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 58 additions & 3 deletions cmd/kwil-cli/cmds/utils/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import (
)

func newParseCmd() *cobra.Command {
var debug bool
var out string

cmd := &cobra.Command{
Use: "parse <file_path>",
Short: "Parse a Kuneiform schema",
Expand All @@ -32,14 +35,52 @@ func newParseCmd() *cobra.Command {
return display.PrintErr(cmd, err)
}

if res.Err() != nil {
return display.PrintErr(cmd, res.Err())
// if not in debug mode, throw any errors and swallow the schema,
// since the schema is invalid.
if !debug {
if res.Err() != nil {
return display.PrintErr(cmd, res.Err())
}

if out == "" {
return display.PrintCmd(cmd, &schemaDisplay{Result: res.Schema})
}

bts, err := json.MarshalIndent(res.Schema, "", " ")
if err != nil {
return display.PrintErr(cmd, err)
}

err = os.WriteFile(out, bts, 0644)
if err != nil {
return display.PrintErr(cmd, err)
}

return display.PrintCmd(cmd, display.RespString(fmt.Sprintf("Schema written to %s", out)))
}

// if in debug mode, output the schema and the debug information.
if out == "" {
return display.PrintCmd(cmd, &debugDisplay{Result: res})
}

bts, err := json.MarshalIndent(res, "", " ")
if err != nil {
return display.PrintErr(cmd, err)
}

err = os.WriteFile(out, bts, 0644)
if err != nil {
return display.PrintErr(cmd, err)
}

return display.PrintCmd(cmd, &schemaDisplay{Result: res.Schema})
return display.PrintCmd(cmd, display.RespString(fmt.Sprintf("Debug information written to %s", out)))
},
}

cmd.Flags().BoolVarP(&debug, "debug", "d", false, "Display debug information")
cmd.Flags().StringVarP(&out, "out", "o", "", "Output file. If debug is true, errors will also be written to this file")

return cmd
}

Expand All @@ -55,3 +96,17 @@ func (s *schemaDisplay) MarshalJSON() ([]byte, error) {
func (s *schemaDisplay) MarshalText() (text []byte, err error) {
return json.MarshalIndent(s.Result, "", " ")
}

// debugDisplay is a struct that will be used to display the schema.
// It is used to display the debug information.
type debugDisplay struct {
Result *parse.SchemaParseResult
}

func (d *debugDisplay) MarshalJSON() ([]byte, error) {
return json.Marshal(d.Result)
}

func (d *debugDisplay) MarshalText() (text []byte, err error) {
return json.MarshalIndent(d.Result, "", " ")
}
2 changes: 1 addition & 1 deletion parse/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (p ParseError) Unwrap() error {
func (p *ParseError) Error() string {
// Add 1 to the column numbers to make them 1-indexed, since antlr-go is 0-indexed
// for columns.
return fmt.Sprintf("(%s) %s: %s\nstart %d:%d end %d:%d", p.ParserName, p.Err.Error(), p.Message,
return fmt.Sprintf("(%s) %s: %s\n start %d:%d end %d:%d", p.ParserName, p.Err.Error(), p.Message,
p.Position.StartLine, p.Position.StartCol+1,
p.Position.EndLine, p.Position.EndCol+1)
}
Expand Down
8 changes: 4 additions & 4 deletions parse/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ type Position struct {
// Set is true if the position of the Position has been set.
// This is useful for testing parsers.
IsSet bool `json:"-"`
StartLine int `json:"start_line"`
StartCol int `json:"start_col"`
EndLine int `json:"end_line"`
EndCol int `json:"end_col"`
StartLine int `json:"-"`
StartCol int `json:"-"`
EndLine int `json:"-"`
Comment on lines 13 to +16
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No opinion on this, but I'm curious why these needed to be hidden. Just too noisy in the document?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, so much noise that it makes the document useless. There's also never really a case where these are necessary in the debug AST, since it is only used for error positions.

EndCol int `json:"-"`
}

// Set sets the position of the Position based on the given parser rule context.
Expand Down
Loading