Skip to content

Commit

Permalink
_js: use pointer receivers in jsParser
Browse files Browse the repository at this point in the history
This simplifies having non-pointer fields later, like slices.

While at it, cover one more edge case with the interactive tests.
  • Loading branch information
mvdan committed Feb 25, 2019
1 parent 35ad032 commit 1426086
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
16 changes: 8 additions & 8 deletions _js/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func main() {
// Parser
stx.Set("NewParser", func(options ...func(interface{})) *js.Object {
p := syntax.NewParser()
jp := js.MakeFullWrapper(jsParser{p})
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 {
Expand All @@ -49,22 +49,22 @@ func main() {
})

stx.Set("KeepComments", func(v interface{}) {
syntax.KeepComments(v.(jsParser).Parser)
syntax.KeepComments(&v.(*jsParser).Parser)
})
stx.Set("Variant", func(l syntax.LangVariant) func(interface{}) {
if math.IsNaN(float64(l)) {
throw("Variant requires a LangVariant argument")
}
return func(v interface{}) {
syntax.Variant(l)(v.(jsParser).Parser)
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)
syntax.StopAt(word)(&v.(*jsParser).Parser)
}
})

Expand Down Expand Up @@ -115,7 +115,7 @@ func (r streamReader) Read(p []byte) (n int, err error) {
}

type jsParser struct {
*syntax.Parser
syntax.Parser
}

func adaptReader(src *js.Object) io.Reader {
Expand All @@ -125,19 +125,19 @@ func adaptReader(src *js.Object) io.Reader {
return strings.NewReader(src.String())
}

func (p jsParser) Parse(src *js.Object, name string) *js.Object {
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 {
func (p *jsParser) Incomplete() bool {
return p.Parser.Incomplete()
}

func (p jsParser) Interactive(src *js.Object, fn func([]*syntax.Stmt) bool) {
func (p *jsParser) Interactive(src *js.Object, fn func([]*syntax.Stmt) bool) {
err := p.Parser.Interactive(adaptReader(src), fn)
if err != nil {
throw(err)
Expand Down
2 changes: 2 additions & 0 deletions _js/testmain.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,15 @@ const printer = syntax.NewPrinter()
"bar; baz\n",
"\n",
"foo; 'incom\n",
" \n",
"plete'\n",
]
const wantCallbacks = [
{"count": 1, "incomplete": false},
{"count": 2, "incomplete": false},
{"count": 0, "incomplete": false},
{"count": 1, "incomplete": true},
{"count": 1, "incomplete": true},
{"count": 2, "incomplete": false},
]
var gotCallbacks = []
Expand Down

0 comments on commit 1426086

Please sign in to comment.