-
Notifications
You must be signed in to change notification settings - Fork 1.6k
ir: Add end locations in IR plan statements #9007
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ package ir | |
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/open-policy-agent/opa/v1/ast/location" | ||
| "github.com/open-policy-agent/opa/v1/types" | ||
| ) | ||
|
|
||
|
|
@@ -84,7 +85,7 @@ type ( | |
| } | ||
|
|
||
| locationStmt interface { | ||
| SetLocation(index, row, col int, file, text string) | ||
| SetLocation(index, row, col int, file string, text []byte) | ||
| GetLocation() *Location | ||
| } | ||
|
|
||
|
|
@@ -467,21 +468,45 @@ type ResultSetAddStmt struct { | |
| // Location records the filen index, and the row and column inside that file | ||
| // that a statement can be connected to. | ||
| type Location struct { | ||
| File int `json:"file"` // filename string constant index | ||
| Col int `json:"col"` | ||
| Row int `json:"row"` | ||
| file, text string // only used for debugging | ||
| File int `json:"file"` // filename string constant index | ||
| Col int `json:"col"` | ||
| Row int `json:"row"` | ||
| EndCol int `json:"end_col"` | ||
| EndRow int `json:"end_row"` | ||
|
|
||
| // Text is only used for location ranges and debug prints. | ||
| // A named type is used so that its String method is called during printing. | ||
| // String cannot be set on Location since it is embedded and impacts parent | ||
| // structs if registered here. | ||
| Text locationText `json:"-"` | ||
|
|
||
| file string // only used for debugging | ||
| } | ||
|
|
||
| type locationText []byte | ||
|
|
||
| func (d locationText) String() string { | ||
| return string(d) | ||
| } | ||
|
|
||
| // SetLocation sets the Location for a given Stmt. | ||
| func (l *Location) SetLocation(index, row, col int, file, text string) { | ||
| func (l *Location) SetLocation(index, row, col int, file string, text []byte) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a breaking change, but it seemed acceptable given:
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Switching text to []byte changes what Pretty prints, pretty.go dumps the struct with %+v, and Go renders a []byte as numbers rather than text. A String() method on ir.Location would fix it without giving up the []byte change, since %+v picks it up automatically.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, interesting... I have added 26ade847c to address. it's not ideal but adding String() on the location doesn't work since it's embedded into Stmt and so String set there risks only showing the Location string representation. |
||
| *l = Location{ | ||
| File: index, | ||
| Row: row, | ||
| Col: col, | ||
| Text: text, | ||
|
|
||
| file: file, | ||
| text: text, | ||
| } | ||
|
|
||
| l.EndRow, l.EndCol = location.EndOf(row, col, l.Text) | ||
| } | ||
|
|
||
| // End returns the end row and col of the location range, expected to be called | ||
| // after SetLocation or unmarshalling. | ||
| func (l *Location) End() (row, col int) { | ||
| return l.EndRow, l.EndCol | ||
| } | ||
|
|
||
| // GetLocation returns a Stmt's Location. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These just add the new fields.