Repository navigation
Expand file tree
/
Copy pathdoc.go
More file actions
185 lines (185 loc) · 5.16 KB
/
Copy pathdoc.go
File metadata and controls
185 lines (185 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Package zast provides conversion between Go AST and S-expression representation.
//
// zast enables programmatic manipulation of Go source code by converting Go's
// abstract syntax tree (AST) into a canonical S-expression format and back.
// This is particularly useful for:
// - Code generation and transformation
// - Static analysis tools
// - Educational tools for understanding Go's AST
// - Building Lisp-like macro systems for Go
//
// # Basic Usage
//
// Convert Go source to S-expression:
//
// fset := token.NewFileSet()
// file, err := parser.ParseFile(fset, "example.go", source, parser.ParseComments)
// if err != nil {
// log.Fatal(err)
// }
//
// writer := writer.New(fset)
// sexpText, err := writer.WriteFile(file)
// if err != nil {
// log.Fatal(err)
// }
// fmt.Println(sexpText)
//
// Convert S-expression back to Go AST:
//
// parser := sexp.NewParser(sexpText)
// sexpNode, err := parser.Parse()
// if err != nil {
// log.Fatal(err)
// }
//
// builder := builder.New()
// file, err := builder.BuildFile(sexpNode)
// if err != nil {
// log.Fatal(err)
// }
//
// # S-Expression Format
//
// The S-expression format mirrors Go's AST structure using keyword arguments:
//
// (Ident :namepos 10 :name "x" :obj nil)
// (BinaryExpr :x (Ident...) :oppos 15 :op ADD :y (Ident...))
// (FuncDecl :name (Ident...) :type (FuncType...) :body (BlockStmt...))
//
// See the documentation for individual node types for details on their
// S-expression representation.
//
// # Important Limitations
//
// # Position Information
//
// **zast does not preserve exact source positions through round-trip conversion.**
//
// When converting Go AST → S-expr → Go AST:
// - Position information (token.Pos) is lost
// - The reconstructed AST has all positions set to token.NoPos (0)
// - Comments are preserved and attached to correct AST nodes, but their positions are reset
// - Use go/printer to format the output - it will generate clean, valid formatting
//
// This is **by design**:
// - S-expression representation is for code transformation and analysis
// - Not for preserving exact source formatting or file positions
// - If you need to preserve original positions, keep the original AST and FileSet
//
// # What is Preserved
//
// ✅ Complete AST structure
// ✅ All comments (attached to correct nodes)
// ✅ All code semantics
// ✅ Ability to generate valid Go code
//
// # What is Lost
//
// ❌ Exact original formatting (spacing, line breaks)
// ❌ Source file positions
// ❌ Ability to point to original source locations
//
// # Comments
//
// When parsing with parser.ParseComments, all comments are preserved and
// attached to the correct AST nodes. However, their position information is
// reset to token.NoPos. The go/printer package will place comments appropriately
// when formatting the output.
//
// # Error Handling
//
// All conversion functions return descriptive errors when:
// - S-expression syntax is invalid
// - Required fields are missing
// - Field types don't match expectations
// - Go source code has syntax errors (when parsing)
//
// # Performance
//
// zast is designed to handle production codebases efficiently:
// - Small files (< 100 lines): < 10ms round-trip
// - Medium files (500-1000 lines): < 100ms round-trip
// - Large files (5000-10000 lines): < 1 second round-trip
// - Minimal memory allocation
// - No recursion limits for normal code
//
// # Additional Limitations
//
// The following features are not yet fully supported:
// - Generic type parameters (Go 1.18+)
// - Interface method types
//
// # Future Work
//
// Full position tracking through compilation stages will be handled by the
// zylisp/core source map architecture. This will provide proper source
// location tracking for error reporting in the Zylisp compiler pipeline
//
// # Example
//
// Here's a complete example of round-trip conversion:
//
// package main
//
// import (
// "fmt"
// "go/parser"
// "go/printer"
// "go/token"
// "log"
//
// "zylisp/zast/builder"
// "zylisp/zast/sexp"
// "zylisp/zast/writer"
// )
//
// func main() {
// source := `package main
//
// import "fmt"
//
// func main() {
// fmt.Println("Hello, world!")
// }
// `
//
// // Parse Go source
// fset := token.NewFileSet()
// file, err := parser.ParseFile(fset, "hello.go", source, 0)
// if err != nil {
// log.Fatal(err)
// }
//
// // Convert to S-expression
// w := writer.New(fset)
// sexpText, err := w.WriteFile(file)
// if err != nil {
// log.Fatal(err)
// }
//
// fmt.Println("S-expression:", sexpText[:80], "...")
//
// // Convert back to AST
// p := sexp.NewParser(sexpText)
// sexpNode, err := p.Parse()
// if err != nil {
// log.Fatal(err)
// }
//
// b := builder.New()
// file2, err := b.BuildFile(sexpNode)
// if err != nil {
// log.Fatal(err)
// }
//
// // Verify round-trip
// var buf1, buf2 bytes.Buffer
// printer.Fprint(&buf1, fset, file)
// printer.Fprint(&buf2, token.NewFileSet(), file2)
//
// if buf1.String() == buf2.String() {
// fmt.Println("Round-trip successful!")
// }
// }
package zast