go-toolsmith / astcopy Goto Github PK
View Code? Open in Web Editor NEWPackage astcopy implements Go AST deep copy operations.
License: MIT License
Package astcopy implements Go AST deep copy operations.
License: MIT License
The problem: astcopy functions do not copy ast.Object
fields because some projects may not care about them at all (go/types.Object
is better and deprecates ast.Object
). But if for whatever reason user must copy objects, there will be troubles.
I'm proposing this functions:
func IdentObjects(dst, src *ast.Ident) {/**/}
func ScopeObjects(dst, src *ast.Scope) {/**/}
func PackageObjects(dst, src *ast.Package) {/**/}
So, copy of *ast.Ident
with objects may be done this way:
y := astcopy.Ident(x)
astcopy.IdentObjects(y, x)
This can be wrapped by user into simple function:
func copyIdent(x) *ast.Ident {
y := astcopy.Ident(x)
astcopy.IdentObjects(y, x)
return y
}
This way, we avoid quite expensive copy that may be unnecessary in a first place and provide a way to copy objects if they are required.
Note that copying objects may be non-trivial.
It needs some investigation and problem overview beforehand.
I've come across a problem where I want to copy everything except the position.
To solve this problem I cloned astcopy.go
and made changes to all functions.
For example, this is how the UnaryExpr is now copied:
func UnaryExpr(x *ast.UnaryExpr) *ast.UnaryExpr {
if x == nil {
return nil
}
return &ast.UnaryExpr{
Op: x.Op,
X: copyExpr(x.X),
}
}
Is this something you would like to incorporate into this repo. Otherwise I will clone this repo and make an alternative version of it.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.