-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_misc.go
46 lines (40 loc) · 1015 Bytes
/
parse_misc.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
package lunar
import (
"go/ast"
"strings"
)
func (p *Parser) parseCommentGroup(w *Writer, cg *ast.CommentGroup) {
if cg == nil {
return
}
for _, c := range cg.List {
// Handle long-style comments using Lua long-style comments
if c.Text[0:2] == "/*" {
if strings.Contains(c.Text, "]=]") {
p.error(cg, "Cannot handle comment containing ']='")
}
w.WriteLinef("--[=[%s]=]", c.Text[2:len(c.Text)-2])
} else {
w.WriteLinef("--%s", c.Text[2:])
}
}
}
func (p *Parser) parseFile(w *Writer, f *ast.File) {
pkg := p.nodePkg(f)
name := pkg.Pkg.Name()
path := pkg.Pkg.Path()
w.WriteLine("-- Package declaration")
w.WriteLinef(`local _%s = _G["%s"] or {}`, name, path)
w.WriteLinef(`_G["%s"] = _%s`, path, name)
w.WriteNewline()
w.WriteLine("local builtins = _G.lunar_go_builtins")
w.WriteNewline()
for _, decl := range f.Decls {
p.parseNode(w, decl, true)
}
}
func (p *Parser) parsePackage(w *Writer, pkg *ast.Package) {
for _, f := range pkg.Files {
p.parseFile(w, f)
}
}