Skip to content

Commit

Permalink
Re-organize helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
apstndb committed Oct 15, 2024
1 parent aa8820b commit 47ea709
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 92 deletions.
47 changes: 46 additions & 1 deletion ast/pos.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,51 @@ import (
"github.com/cloudspannerecosystem/memefish/token"
)

// ================================================================================
//
// Helper functions for Pos(), End()
// These functions are intended for use within this file only.
//
// ================================================================================

// lastNode returns last element of Node slice.
// This function corresponds to NodeSliceVar[$] in ast.go.
func lastNode[T Node](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 lastNode(s).End()
}

// ================================================================================
//
// SELECT
Expand Down Expand Up @@ -39,7 +84,7 @@ func (c *CTE) End() token.Pos { return c.Rparen + 1 }
func (s *Select) Pos() token.Pos { return s.Select }

func (s *Select) End() token.Pos {
return firstValidEnd(s.Limit, s.OrderBy, s.Having, s.GroupBy, s.Where, s.From, lastElem(s.Results))
return firstValidEnd(s.Limit, s.OrderBy, s.Having, s.GroupBy, s.Where, s.From, lastNode(s.Results))
}

func (c *CompoundQuery) Pos() token.Pos {
Expand Down
51 changes: 51 additions & 0 deletions ast/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,59 @@ package ast

import (
"github.com/cloudspannerecosystem/memefish/token"
"strings"
)

// ================================================================================
//
// Helper functions for SQL()
// These functions are intended for use within this file only.
//
// ================================================================================

// 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()
}

type prec int

const (
Expand Down
91 changes: 0 additions & 91 deletions ast/util.go

This file was deleted.

0 comments on commit 47ea709

Please sign in to comment.