-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
579 lines (511 loc) · 16.4 KB
/
main.go
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
// go generate super charged on steroids
package main
import (
"bytes"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"reflect"
"strings"
"text/template"
genericinterperter "github.com/mh-cbon/gigo/interpreter/generic"
glanginterpreter "github.com/mh-cbon/gigo/interpreter/glang"
genericlexer "github.com/mh-cbon/gigo/lexer/generic"
gigolexer "github.com/mh-cbon/gigo/lexer/gigo"
glanglexer "github.com/mh-cbon/gigo/lexer/glang"
glang "github.com/mh-cbon/gigo/struct/glang"
"github.com/mh-cbon/state-lexer"
)
func main() {
var symbol string
flag.StringVar(&symbol, "symbol", "", "Find specified symbol name")
flag.Parse()
if flag.NArg() < 2 {
fmt.Println("Wrong usage, should be")
fmt.Println("go run main.go <cmd> <file>")
fmt.Println("")
fmt.Println("Available commands:")
fmt.Println("dump: pretty print the interpretation result of a file")
fmt.Println("gen: mutate a source file")
panic("not enough arguments")
}
cmd := flag.Arg(0)
path := flag.Arg(1)
// f := must open os.Open("demo.gigo")
fileDef := MustInterpretFile(path)
if cmd == "str" || cmd == "s" {
if symbol != "" {
symbols := fileDef.FindSymbols(symbol)
if len(symbols) > 0 {
fmt.Println(symbols[0])
} else {
fmt.Println("No symbol found for ", symbol)
}
} else {
fmt.Println(fileDef.String())
}
} else if cmd == "dump" || cmd == "d" {
if symbol != "" {
symbols := fileDef.FindSymbols(symbol)
if len(symbols) > 0 {
glanginterpreter.Dump(symbols[0])
} else {
fmt.Println("No symbol found for ", symbol)
}
} else {
glanginterpreter.Dump(fileDef)
}
} else if cmd == "gen" || cmd == "g" {
newDecl, err := mutate(fileDef)
if err != nil {
fmt.Printf("%#v\n", err)
panic(err)
}
if symbol != "" {
symbols := newDecl.FindSymbols(symbol)
if len(symbols) > 0 {
fmt.Println(symbols[0])
} else {
fmt.Println("No symbol found for ", symbol)
}
} else {
fmt.Println(newDecl.String())
}
}
}
func mutate(fileDef *glang.FileDecl) (glang.ScopeReceiver, error) {
allTplsFuncs := map[string]interface{}{
"joinexpr": func(glue string, tokens interface{}) string {
t := []genericinterperter.Tokener{}
switch yy := tokens.(type) {
case []genericinterperter.Tokener:
t = append(t, yy...)
case []*glang.PropDecl:
for _, xx := range yy {
t = append(t, xx)
}
case []*glang.IdentifierDecl:
for _, xx := range yy {
t = append(t, xx)
}
}
ret := []string{}
for _, xx := range t {
ret = append(ret, xx.String())
}
return strings.Join(ret, glue)
},
}
tplTypesFuncs := map[string]interface{}{}
outData := &Tomate{
implTplData: map[string]interface{}{},
}
/* At that moment the file is processed,
all the template/type/struct/interface/func/ect declarations
are well known.
*/
// prepare the source for its rendering
var defineFunc []glang.FuncDeclarer
structTypes := fileDef.FindStructsTypes()
implTypes := fileDef.FindImplementsTypes()
tplTypes := fileDef.FindTemplatesTypes()
funcs := fileDef.FindFuncs()
tplFuncs := fileDef.FindTemplateFuncs()
defFuncs := fileDef.FindDefineFuncs()
var attachMethod = func(m glang.FuncDeclarer) {
for _, t := range tplTypes {
if x, ok := m.GetReceiverType().First().(*glang.IdentifierDecl); ok {
if t.GetSlugName() == x.GetSlugName() {
t.AddMethod(m)
return
}
}
}
panic("not found")
}
var attachImplMethod = func(m glang.FuncDeclarer) bool {
if m.IsMethod() {
for _, t := range implTypes {
if x, ok := m.GetReceiverType().First().(*glang.IdentifierDecl); ok {
if t.Name.GetSlugName() == x.GetSlugName() {
t.AddMethod(m)
return true
}
}
}
}
// panic("not found")
return false
}
// type XXX implements{}, needs to be replaced by a placeholder,
// its template tokens values are changed to avoid further problems
for _, i := range implTypes {
name := fmt.Sprintf("placeholder%v", len(outData.placeholders))
m := NewPlaceholderTypeMutation(name, i)
outData.placeholders = append(outData.placeholders, m)
fileDef.MustInsertAfter(i, m.PlaceholderDecl)
fileDef.MustRemove(i)
i.SetTokenValue(glanglexer.TplOpenToken, "<:")
i.SetTokenValue(glanglexer.TplCloseToken, ":>")
}
// template XXX<Modifier> struct {}
// are to be removed, they really just template expressions.
for _, i := range tplTypes {
fileDef.MustRemove(i)
i.SetTokenValue(glanglexer.TplOpenToken, "<:")
i.SetTokenValue(glanglexer.TplCloseToken, ":>")
}
// <Modifier> func ()
// and
// func(receiver<...>)...
// are to be removed, they really just template expressions.
// it also attaches the method to their type.
for _, i := range tplFuncs {
fileDef.MustRemove(i)
i.SetTokenValue(glanglexer.TplOpenToken, "<:")
i.SetTokenValue(glanglexer.TplCloseToken, ":>")
// i.GetBody().SetTokenValue(glanglexer.GreaterToken, ":>") // trick unti fix.
attachMethod(i)
}
// <define> func XXX ()
// are to be removed because those funcs are injected into the template instances
for _, i := range defFuncs {
fileDef.MustRemove(i)
i.SetTokenValue(glanglexer.TplOpenToken, "<:")
i.SetTokenValue(glanglexer.TplCloseToken, ":>")
defineFunc = append(defineFunc, i)
//- define a template func
// that func (tbd later) will be available in type declarations expressions like
// - implement<>
// - template<>
name := i.GetName()
tplTypesFuncs[name] = stubFunc(i.String())
// the key difficulty in this feature is that the func string can not be
// evaluated at runtime, so this whole template transforms step,
// needs to be delayed to a new sub go program where the func body string can be written.
// just refactroring of the current mess!
}
// regular go fund method are attached to ehir type.
for _, i := range funcs {
attachImplMethod(i)
}
for _, i := range structTypes {
// declare regular structs as data protperties
outData.implTplData[i.GetName()] = i
}
// for every declarations
// - template XXXX struct{}
// - func(of the template)...
// do
//- create a template.Template of its string
//- create a template.Func of its mutation
funcsForTypesMutators := map[string]interface{}{}
for k, v := range tplTypesFuncs {
funcsForTypesMutators[k] = v
}
for k, v := range allTplsFuncs {
funcsForTypesMutators[k] = v
}
for _, i := range tplTypes {
outData.tplTypesMutators = append(outData.tplTypesMutators, &TypeMutator{
Decl: i,
funcs: funcsForTypesMutators,
})
}
// need to remove comments, they are not understood by template.Template,
// and if they contain the template syntax, it breaks becasue template evaluate them.
// on the other hand, GigoInterpreter does not interpret comments, so it can t see and manage those
// problematic strings. :/
// finally the idea is to lacehold the comments, its kind of noop, works well.
x := placeholdComments(genericlexer.CommentBlockToken, fileDef, "blockcomments")
outData.placeholders = append(outData.placeholders, x...)
y := placeholdComments(genericlexer.CommentLineToken, fileDef, "linecomments")
outData.placeholders = append(outData.placeholders, y...)
tplContent := fileDef.String()
// execute the modified file tree with a taylor made template context.
tpl := makeTplOfSource("gigo", tplContent, allTplsFuncs)
var out bytes.Buffer
if err := tpl.Execute(&out, outData); err != nil {
return nil, genericinterperter.NewStringTplSyntaxError(err, "gigo", tplContent)
}
return InterpretString(fileDef.GetName(), out.String())
}
type Tomate struct {
placeholders []mutationExecuter
tplTypesMutators []*TypeMutator
implTplData map[string]interface{}
}
func (t *Tomate) getPlaceholder(name string) mutationExecuter {
for _, p := range t.placeholders {
if p.getName() == name {
return p
}
}
return nil
}
func (t *Tomate) GetResult(name string) string {
pl := t.getPlaceholder(name)
if pl != nil {
res, err := pl.execute(t.tplTypesMutators, t.implTplData)
if err != nil {
panic(err)
}
return res
}
return "not found"
}
type TemplateTplDot struct {
*glang.StructDecl
Args []interface{}
}
func (t *TemplateTplDot) ArgType(s interface{}) string {
return reflect.TypeOf(s).Name()
}
func makeLexerReader(r io.Reader) genericinterperter.TokenerReader {
l := lexer.New(r, (gigolexer.New()).StartHere)
l.ErrorHandler = func(e string) {}
return genericinterperter.NewReadTokenWithPos(l)
}
func prettyPrinterLexer(reader genericinterperter.TokenerReader) genericinterperter.TokenerReader {
namer := genericinterperter.TokenerName(gigolexer.TokenName)
reader = genericinterperter.NewReadNPrettyPrint(reader, namer, os.Stdout)
return reader
}
func InterpretFile(fileName string) (*glang.FileDecl, error) {
f, err := os.Open(fileName)
if err != nil {
return nil, err
}
defer f.Close()
reader := makeLexerReader(f)
// reader = prettyPrinterLexer(reader)
interpret := glanginterpreter.NewGigoInterpreter(reader)
return interpret.ProcessFile(fileName)
}
func InterpretString(pkgName, content string) (*glang.StrDecl, error) {
var buf bytes.Buffer
buf.WriteString(content)
reader := makeLexerReader(&buf)
//reader = prettyPrinterLexer(reader)
interpret := glanginterpreter.NewGigoInterpreter(reader)
return interpret.ProcessStr(content)
}
func MustInterpretFile(fileName string) *glang.FileDecl {
ret, err := InterpretFile(fileName)
if err != nil {
fmt.Printf("%#v\n", err)
fmt.Printf("%+v\n", err)
panic(err)
}
return ret
}
func MustInterpretString(name, content string) *glang.StrDecl {
ret, err := InterpretString(name, content)
if err != nil {
fmt.Printf("%#v\n", err)
panic(err)
}
return ret
}
var plToken lexer.TokenType = -200
func placeholderToken(name string, pos genericinterperter.TokenPos) *genericinterperter.TokenWithPos {
tok := lexer.Token{
Type: plToken,
Value: fmt.Sprintf("<:.GetResult \"%v\":>", name),
}
return genericinterperter.NewTokenWithPos(tok, pos.Line, pos.Pos)
}
func placeholdComments(T lexer.TokenType, src *glang.FileDecl, prefix string) []mutationExecuter {
ret := []mutationExecuter{}
for _, c := range src.FindAll(T) {
name := fmt.Sprintf("placeholder%v%v", prefix, len(ret))
m := NewPlaceholderMutation(name, c.GetTokens()[0])
ret = append(ret, m)
src.InsertAfter(c, m.PlaceholderDecl)
src.Remove(c)
}
return ret
}
type mutationExecuter interface {
execute(mutators []*TypeMutator, data interface{}) (string, error)
getName() string
}
type placeholderMutation struct {
OriginDecl genericinterperter.Tokener
PlaceholderDecl *genericinterperter.TokenWithPos
Name string
}
func (p *placeholderMutation) getName() string {
return p.Name
}
func (p *placeholderMutation) execute(mutators []*TypeMutator, data interface{}) (string, error) {
return p.OriginDecl.String(), nil
}
func NewPlaceholderMutation(name string, of genericinterperter.Tokener) *placeholderMutation {
return &placeholderMutation{
OriginDecl: of,
PlaceholderDecl: placeholderToken(name, of.GetPos()),
Name: name,
}
}
type placeholderTypeMutation struct {
mutation *ImplTypeMutation
PlaceholderDecl *genericinterperter.TokenWithPos
Name string
}
func (p *placeholderTypeMutation) getName() string {
return p.Name
}
func (p *placeholderTypeMutation) execute(mutators []*TypeMutator, data interface{}) (string, error) {
expr, err := p.mutation.mutate(mutators, data)
res := ""
if expr != nil {
res = expr.String()
}
return res, err
}
func NewPlaceholderTypeMutation(name string, of *glang.ImplementDecl) *placeholderTypeMutation {
return &placeholderTypeMutation{
mutation: &ImplTypeMutation{Decl: of},
PlaceholderDecl: placeholderToken(name, of.GetPos()),
Name: name,
}
}
func makeTplOfSource(name, src string, funcs map[string]interface{}) *template.Template {
t, err := template.New(name).Funcs(funcs).Delims("<:", ":>").Parse(src)
if err != nil {
// fmt.Println(src)
fErr := genericinterperter.NewStringTplSyntaxError(err, name, src)
fmt.Printf("%#v\n", fErr)
panic(fErr)
}
return t
}
func stubFunc(content string) func() error {
return func() error {
fmt.Println("template func content is")
fmt.Println(content)
return nil
}
}
type TypeMutator struct {
Decl *glang.TemplateDecl
funcs map[string]interface{}
}
func (t *TypeMutator) getTemplateStr() string {
tplContent := ""
// the template declares a type like this
// template XXXX struct{}
// it is needed to replace the template keyword by a type.
// => type XXXX struct{}
if y := t.Decl.GetToken(glanglexer.TemplateToken); y != nil {
// y.SetType(glanglexer.TypeToken) // not needed to update
y.SetValue("type")
}
tplContent += t.Decl.String()
for _, m := range t.Decl.Methods {
tplContent += m.String()
if m.GetModifier() != nil { // test if there is a front modifier like <range $m :=...>
tplContent += "<:end:>" // close the template expression, quick and dirty, but just works :)
}
}
return tplContent
}
func (t *TypeMutator) execute(data interface{}) (string, error) {
name := t.Decl.GetName()
src := t.getTemplateStr()
tpl := makeTplOfSource(name, src, t.funcs)
var buf bytes.Buffer
err := tpl.Execute(&buf, data)
return buf.String(), err
}
func (t *TypeMutator) mutate(origin *glang.StructDecl, args ...interface{}) (*glang.StructDecl, error) {
// the provided argument becomes the template root dot {{.}}
arg := &TemplateTplDot{StructDecl: origin, Args: args}
content, err := t.execute(arg)
if err == nil {
// we shall parse it
newFileDef := MustInterpretString("random", content)
newStruct := newFileDef.FindStructsTypes()[0] // a type for a type
// should it be added to the current template data ?
// structTypes = append(structTypes, newStruct)
// note, it is expected the type gets added to the package repository.
// dont forget to attach its method.
for _, f := range newFileDef.FindFuncs() {
newStruct.AddMethod(f)
}
return newStruct, nil
}
return origin, err
}
type ImplTypeMutation struct {
scope genericinterperter.Expression
Decl *glang.ImplementDecl
Res []*glang.StructDecl
}
func (t *ImplTypeMutation) getMutationFunc(m *TypeMutator) func(origin *glang.StructDecl, args ...interface{}) (*glang.StructDecl, error) {
return func(origin *glang.StructDecl, args ...interface{}) (*glang.StructDecl, error) {
res, err := m.mutate(origin, args...)
if err == nil {
t.Res = append(t.Res, res)
}
return res, err
}
}
func (t *ImplTypeMutation) mutate(mutators []*TypeMutator, data interface{}) (*glang.StrDecl, error) {
// in a decl like implement<X Y Z>
// X Y Z are func template of a template string "X Y Z"
funcs := map[string]interface{}{}
for _, m := range mutators {
name := m.Decl.GetSlugName()
funcs[name] = t.getMutationFunc(m)
}
tplContent := t.Decl.String()
tpl := makeTplOfSource("gigo", tplContent, funcs)
if err := tpl.Execute(ioutil.Discard, data); err != nil {
fmt.Println(tplContent)
return nil, err
}
// once the template "X Y Z" invoked => new struct type is added to t.Res
// finalize the implements instruction into a regular struct
// it becomes regular go code.
// from => type xxx impements<y u i>{}
// to => type xxx struct{}
i := t.Decl
i.SetTokenValue(glanglexer.ImplementsToken, "struct")
i.RemoveT(glanglexer.TplOpenToken) // get ride of the template mutations
strDecl := &glang.StrDecl{}
if len(t.Res) > 0 {
// if any mutations is found, get the last one,
// and apply it to the original type
last := t.Res[len(t.Res)-1]
tok := genericinterperter.NewTokenWithPos(lexer.Token{Type: genericlexer.WordToken, Value: last.GetName()}, 0, 0)
nl := genericinterperter.NewTokenWithPos(lexer.Token{Type: glanglexer.NlToken, Value: "\n"}, 0, 0)
ws := genericinterperter.NewTokenWithPos(lexer.Token{Type: genericlexer.WsToken, Value: "\t"}, 0, 0)
// define the last genrated type as an underlying type of i
ID := glang.NewExpressionDecl()
name := glang.NewIdentifierDecl()
name.AddExpr(tok)
ID.AddExpr(name)
i.GetBlock().Underlying = append(i.GetBlock().Underlying, ID)
i.GetBlock().InsertAt(1, nl)
i.GetBlock().InsertAt(2, ws)
i.GetBlock().InsertAt(3, ID)
// add every generated types and all of their methods to the string decl
for _, r := range t.Res {
strDecl.AddExprs(r.Tokens)
nl := genericinterperter.NewTokenWithPos(lexer.Token{Type: glanglexer.NlToken, Value: "\n"}, 0, 0)
strDecl.AddExpr(nl)
for _, m := range r.Methods {
strDecl.AddExprs(m.GetTokens())
nl := genericinterperter.NewTokenWithPos(lexer.Token{Type: glanglexer.NlToken, Value: "\n"}, 0, 0)
strDecl.AddExpr(nl)
}
}
}
// finally add the original modified I struct to the string decl
strDecl.AddExpr(i)
return strDecl, nil
}