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

gop/ast/gopq: one, positions, codeOf #2128

Merged
merged 1 commit into from
Feb 25, 2025
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
12 changes: 11 additions & 1 deletion ast/gopq/gopq.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,20 @@ func (p NodeSet) One() NodeSet {
}

// One creates a node set that only contains a signle node.
func One(node Node) NodeSet {
func One__0(node Node) NodeSet {
return NodeSet{Data: &oneNode{node}}
}

// One creates a node set that only contains a signle node.
func One__1(f *ast.File) NodeSet {
return NodeSet{Data: &oneNode{astFile{f}}}
}

// One creates a node set that only contains a signle node.
func One__2(pkg *ast.Package) NodeSet {
return NodeSet{Data: &oneNode{astPackage{pkg}}}
}

// -----------------------------------------------------------------------------

type fixNodes struct {
Expand Down
28 changes: 28 additions & 0 deletions ast/gopq/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,26 @@ func (p NodeSet) UnquotedStringElts__0() (ret []string, err error) {

// -----------------------------------------------------------------------------

func (p NodeSet) Positions__1(exactly bool) (ret []token.Pos, err error) {
item, err := p.CollectOne__1(exactly)
if err != nil {
return
}
switch o := item.Obj().(type) {
case *ast.CompositeLit:
return []token.Pos{o.Pos(), o.End(), o.Lbrace, o.Rbrace}, nil
case ast.Node:
return []token.Pos{o.Pos(), o.End()}, nil
}
return nil, ErrUnexpectedNode
}

func (p NodeSet) Positions__0() (ret []token.Pos, err error) {
return p.Positions__1(false)
}

// -----------------------------------------------------------------------------

func (p NodeSet) EltLen__1(exactly bool) (ret int, err error) {
item, err := p.CollectOne__1(exactly)
if err != nil {
Expand Down Expand Up @@ -150,3 +170,11 @@ func getName(v interface{}, useEmpty bool) string {
}

// -----------------------------------------------------------------------------

func CodeOf(fset *token.FileSet, f *ast.File, start, end token.Pos) string {
pos := fset.Position(start)
n := int(end - start)
return string(f.Code[pos.Offset : pos.Offset+n])
}

// -----------------------------------------------------------------------------
32 changes: 19 additions & 13 deletions cmd/chore/gopbuiltingen/builtingen.gox
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import (
"bytes"
"go/ast"
"go/format"
"go/parser"
"go/token"
gopast "gop/ast"
"gop/ast/gopq"
"gop/parser"
"os"
Expand Down Expand Up @@ -49,7 +51,7 @@ func genDecls(f *ast.File) {
}
}

func initBuiltinTIs(fn gopq.NodeSet) (tistr *builtinTI) {
func initBuiltinTIs(fn gopq.NodeSet, f *gopast.File) (tistr *builtinTI) {
ti := fn.body.any.assignStmt.rhs(0).x.compositeLit("BuiltinTI")
methods := ti.elt("methods").cache
for method <- methods {
Expand All @@ -60,14 +62,16 @@ func initBuiltinTIs(fn gopq.NodeSet) (tistr *builtinTI) {
if ref := fn.callExpr.one; ref.ok {
pkg := ref.fun.x.ident!
name := ref.arg(0).unquotedString!
exargs := 0
if ex := item.elt(2).compositeLit("bmExargs").one; ex.ok {
exargs = ex.eltLen!
var ex *exargs
if e := item.elt(2).compositeLit("bmExargs").one; e.ok {
pos := e.positions!
code := gopq.codeOf(fset, f, pos[2]+1, pos[3])
ex = &exargs{e.eltLen!, code}
}
aTI.Methods <- builtin{mthd, {pkg, name, exargs}}
aTI.Methods <- builtin{mthd, {pkg, name, ex}}
} else {
name := fn.ident!
aTI.Methods <- builtin{mthd, {"", name, 0}}
aTI.Methods <- builtin{mthd, {"", name, nil}}
}
}
Types <- aTI
Expand All @@ -88,9 +92,9 @@ func newBuiltinDefault(fn gopq.NodeSet, tistr *builtinTI) {
pkg := ref.fun.x.ident!
name := ref.arg(0).unquotedString!
if pkg == "strx" {
tistr.Methods <- builtin{mthd, {pkg, name, 0}}
tistr.Methods <- builtin{mthd, {pkg, name, nil}}
} else {
aTI.Methods <- builtin{mthd, {pkg, name, 0}}
aTI.Methods <- builtin{mthd, {pkg, name, nil}}
}
}
if len(aTI.Methods) > 0 {
Expand All @@ -108,25 +112,27 @@ func initBuiltin(fn gopq.NodeSet) {
ref := call.arg(3).callExpr.one
pkg := ref.fun.x.ident!
name := ref.arg(0).unquotedString!
Builtins <- builtin{built, {pkg, name, 0}}
Builtins <- builtin{built, {pkg, name, nil}}
}
}
for call <- stmt.callExpr("initBuiltinFns") {
pkg := call.arg(2).ident!
builtins := call.arg(3).unquotedStringElts!
for built <- builtins {
Builtins <- builtin{built, {pkg, built.capitalize, 0}}
Builtins <- builtin{built, {pkg, built.capitalize, nil}}
}
}
}

fns := gopq.fromFile(fset, "${root}/../gogen/builtin.go", nil, parser.ParseComments)!.funcs
tistr := initBuiltinTIs(fns.funcDecl("initBuiltinTIs").one)
f := parser.parseFile(fset, "${root}/../gogen/builtin.go", nil, parser.ParseComments)!
fns := gopq.one(f).funcs
tistr := initBuiltinTIs(fns.funcDecl("initBuiltinTIs").one, f)

fns = gopq.fromFile(fset, "${root}/cl/builtin.go", nil, parser.ParseComments)!.funcs
initBuiltin fns.funcDecl("initBuiltin").one
newBuiltinDefault fns.funcDecl("newBuiltinDefault").one, tistr

b := gen()
os.Stdout.write b
os.writeFile "${root}/builtin/doc/builtin.gop", b, 0777

// os.Stdout.write b
7 changes: 4 additions & 3 deletions cmd/chore/gopbuiltingen/helper.gop
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ func importSpec(path string) *ast.ImportSpec {
}
}

func rmExargs(list []*ast.Field, exargs int) []*ast.Field {
if exargs == 0 {
func rmExargs(list []*ast.Field, ex *exargs) []*ast.Field {
if ex == nil {
return list
}
n := len(list)
ret := make([]*ast.Field, n)
for i, f <- list {
ret[i] = f
}
for n > 0 {
exargs := ex.N
for n > 0 && exargs > 0 {
f := ret[n-1]
if c := len(f.Names); c > exargs {
f.Names = f.Names[:c-exargs]
Expand Down
11 changes: 8 additions & 3 deletions cmd/chore/gopbuiltingen/reference.gox
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ import (
"runtime"
)

type exargs struct {
N int
Code string
}

var (
Pkg string
Name string
Exargs int
Exargs *exargs
)

func .toType(t *ast.FuncType, at string) *ast.FuncType {
Expand All @@ -19,7 +24,7 @@ func .toType(t *ast.FuncType, at string) *ast.FuncType {
}
}

func .toMethodType(t *ast.FuncType, exargs int, at string) (mt *ast.FuncType, recvType ast.Expr) {
func .toMethodType(t *ast.FuncType, ex *exargs, at string) (mt *ast.FuncType, recvType ast.Expr) {
list := t.Params.List
first := list[0]
recvType = first.Type
Expand All @@ -34,7 +39,7 @@ func .toMethodType(t *ast.FuncType, exargs int, at string) (mt *ast.FuncType, re
}
}
mt = {
Params: {List: rmExargs(list, exargs)},
Params: {List: rmExargs(list, ex)},
Results: toParams(t.Results, at),
}
return
Expand Down