forked from mvdan/sh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
195 lines (171 loc) · 4.41 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
package main
import (
"bytes"
"fmt"
"io"
"math"
"os"
"strings"
"github.com/gopherjs/gopherjs/js"
"mvdan.cc/sh/v3/syntax"
)
func main() {
exps := js.Module.Get("exports")
exps.Set("syntax", map[string]interface{}{})
stx := exps.Get("syntax")
// Type helpers just for JS
stx.Set("NodeType", func(v interface{}) string {
if v == nil {
return "nil"
}
node, ok := v.(syntax.Node)
if !ok {
panic("NodeType requires a Node argument")
}
typ := fmt.Sprintf("%T", node)
if i := strings.LastIndexAny(typ, "*.]"); i >= 0 {
typ = typ[i+1:]
}
return typ
})
// Parser
stx.Set("NewParser", func(options ...func(interface{})) *js.Object {
p := syntax.NewParser()
jp := js.MakeFullWrapper(&jsParser{Parser: *p})
// Apply the options after we've wrapped the parser, as
// otherwise we cannot internalise the value.
for _, opt := range options {
opt(jp)
}
return jp
})
stx.Set("IsIncomplete", syntax.IsIncomplete)
stx.Set("KeepComments", func(v interface{}) {
syntax.KeepComments(&v.(*jsParser).Parser)
})
stx.Set("Variant", func(l syntax.LangVariant) func(interface{}) {
if math.IsNaN(float64(l)) {
panic("Variant requires a LangVariant argument")
}
return func(v interface{}) {
syntax.Variant(l)(&v.(*jsParser).Parser)
}
})
stx.Set("LangBash", syntax.LangBash)
stx.Set("LangPOSIX", syntax.LangPOSIX)
stx.Set("LangMirBSDKorn", syntax.LangMirBSDKorn)
stx.Set("StopAt", func(word string) func(interface{}) {
return func(v interface{}) {
syntax.StopAt(word)(&v.(*jsParser).Parser)
}
})
// Printer
stx.Set("NewPrinter", func() *js.Object {
p := syntax.NewPrinter()
return js.MakeFullWrapper(jsPrinter{p})
})
// Syntax utilities
stx.Set("Walk", func(node syntax.Node, jsFn func(*js.Object) bool) {
fn := func(node syntax.Node) bool {
if node == nil {
return jsFn(nil)
}
return jsFn(js.MakeFullWrapper(node))
}
syntax.Walk(node, fn)
})
stx.Set("DebugPrint", func(node syntax.Node) {
syntax.DebugPrint(os.Stdout, node)
})
stx.Set("SplitBraces", func(w *syntax.Word) *js.Object {
w = syntax.SplitBraces(w)
return js.MakeFullWrapper(w)
})
}
func throw(err error) {
js.Global.Call("$throw", js.MakeFullWrapper(err))
}
// streamReader is an io.Reader wrapper for Node's stream.Readable. See
// https://nodejs.org/api/stream.html#stream_class_stream_readable
// TODO: support https://streams.spec.whatwg.org/#rs-class too?
type streamReader struct {
stream *js.Object
}
func (r streamReader) Read(p []byte) (n int, err error) {
obj := r.stream.Call("read", len(p))
if obj == nil {
return 0, io.EOF
}
bs := []byte(obj.String())
return copy(p, bs), nil
}
type jsParser struct {
syntax.Parser
accumulated []*syntax.Stmt
incomplete bytes.Buffer
}
func adaptReader(src *js.Object) io.Reader {
if src.Get("read") != js.Undefined {
return streamReader{stream: src}
}
return strings.NewReader(src.String())
}
func (p *jsParser) Parse(src *js.Object, name string) *js.Object {
f, err := p.Parser.Parse(adaptReader(src), name)
if err != nil {
throw(err)
}
return js.MakeFullWrapper(f)
}
func (p *jsParser) Incomplete() bool {
return p.Parser.Incomplete()
}
func (p *jsParser) Interactive(src *js.Object, jsFn func([]*js.Object) bool) {
fn := func(stmts []*syntax.Stmt) bool {
objs := make([]*js.Object, len(stmts))
for i, stmt := range stmts {
objs[i] = js.MakeFullWrapper(stmt)
}
return jsFn(objs)
}
err := p.Parser.Interactive(adaptReader(src), fn)
if err != nil {
throw(err)
}
}
func (p *jsParser) InteractiveStep(line string) []*syntax.Stmt {
// pick up previous chunks of the incomplete statement
r := strings.NewReader(p.incomplete.String() + line)
lastEnd := uint(0)
err := p.Parser.Interactive(r, func(stmts []*syntax.Stmt) bool {
if len(stmts) > 0 {
// don't re-parse finished statements
lastEnd = stmts[len(stmts)-1].End().Offset()
}
p.accumulated = append(p.accumulated, stmts...)
return false
})
if syntax.IsIncomplete(err) {
// starting or continuing an incomplete statement
p.incomplete.WriteString(line[lastEnd:])
return p.accumulated
}
// complete; empty both fields and return
p.incomplete.Reset()
if err != nil {
throw(err)
}
acc := p.accumulated
p.accumulated = p.accumulated[:0]
return acc
}
type jsPrinter struct {
*syntax.Printer
}
func (p jsPrinter) Print(file *syntax.File) string {
var buf bytes.Buffer
if err := p.Printer.Print(&buf, file); err != nil {
throw(err)
}
return buf.String()
}