-
Notifications
You must be signed in to change notification settings - Fork 20
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
Add helper functions for SQL(), Pos(), End() #120
Merged
makenowjust
merged 13 commits into
cloudspannerecosystem:main
from
apstndb:feature/add-sql-pos-helper-functions
Oct 15, 2024
Merged
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
4236da9
Bump Go version to 1.20, and add util.go
apstndb 3f8aca2
Add comments to ast/util.go
apstndb 19f717d
Add strOpt in ast/util.go
apstndb b0c6658
Refactor (*CallExpr).SQL()
apstndb 50c4204
Refactor (*Select).SQL()
apstndb 803f45e
Refactor (*Select).End()
apstndb 18d6085
Bump Go version in .github/workflows
apstndb 9decfc9
Refactor (*Path).Pos(), (*Path).End()
apstndb 94a198a
Refactor (*Path).SQL()
apstndb 42ae05e
Merge remote-tracking branch 'origin/main' into feature/add-sql-pos-h…
apstndb e1a3e81
Remove lastElem
apstndb aa8820b
Revert "Remove lastElem"
apstndb 47ea709
Re-organize helper functions
apstndb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package ast | ||
|
||
import ( | ||
"github.com/cloudspannerecosystem/memefish/token" | ||
"strings" | ||
) | ||
|
||
// Helper functions for SQL() | ||
|
||
// sqlOpt outputs: | ||
// | ||
// when node != nil: left + node.SQL() + right | ||
// else : empty string | ||
// | ||
// This function corresponds to sqlOpt in ast.go | ||
func sqlOpt[T interface { | ||
Node | ||
comparable | ||
}](left string, node T, right string) string { | ||
var zero T | ||
if node == zero { | ||
return "" | ||
} | ||
return left + node.SQL() + right | ||
} | ||
|
||
// strOpt outputs: | ||
// | ||
// when pred == true: s | ||
// else : empty string | ||
// | ||
// This function corresponds to {{if pred}}s{{end}} in ast.go | ||
func strOpt(pred bool, s string) string { | ||
if pred { | ||
return s | ||
} | ||
return "" | ||
} | ||
|
||
// sqlJoin outputs joined string of SQL() of all elems by sep. | ||
// This function corresponds to sqlJoin in ast.go | ||
func sqlJoin[T Node](elems []T, sep string) string { | ||
var b strings.Builder | ||
for i, r := range elems { | ||
if i > 0 { | ||
b.WriteString(sep) | ||
} | ||
b.WriteString(r.SQL()) | ||
} | ||
return b.String() | ||
} | ||
|
||
// Helper functions for Pos(), End() | ||
|
||
// lastElem returns last element of slice s. | ||
// This function corresponds to NodeSliceVar[$] in ast.go. | ||
func lastElem[T any](s []T) T { | ||
return s[len(s)-1] | ||
} | ||
|
||
// firstValidEnd returns the first valid Pos() in argument. | ||
// "valid" means the node is not nil and Pos().Invalid() is not true. | ||
// This function corresponds to "(n0 ?? n1 ?? ...).End()" | ||
func firstValidEnd(ns ...Node) token.Pos { | ||
for _, n := range ns { | ||
if n != nil && !n.End().Invalid() { | ||
return n.End() | ||
} | ||
} | ||
return token.InvalidPos | ||
} | ||
|
||
// firstPos returns the Pos() of the first node. | ||
// If argument is an empty slice, this function returns token.InvalidPos. | ||
// This function corresponds to NodeSliceVar[0].pos in ast.go. | ||
func firstPos[T Node](s []T) token.Pos { | ||
if len(s) == 0 { | ||
return token.InvalidPos | ||
} | ||
return s[0].Pos() | ||
} | ||
|
||
// lastEnd returns the End() of the last node. | ||
// If argument is an empty slice, this function returns token.InvalidPos. | ||
// This function corresponds to NodeSliceVar[$].end in ast.go. | ||
func lastEnd[T Node](s []T) token.Pos { | ||
if len(s) == 0 { | ||
return token.InvalidPos | ||
} | ||
return lastElem(s).End() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I understand your intention, but this function seems too generic. I would not want to add this as it could be used in places it is not supposed to be.
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.
OK,
lastElem
can be written as inline, so I will remove it.(It may be possible to be accepted if
T
is restricted toNode
)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.
Fixed e1a3e81
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.
func last
is acceptable.ElemNode[T Node](s []T) T {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.
Introduce
func lastNode[T Node](s []T) T
in 47ea709