From 88122a019ebaf5d41eaf39f436f1d45b7e85de27 Mon Sep 17 00:00:00 2001 From: Qi Xiao Date: Sat, 25 Apr 2020 19:36:35 +0100 Subject: [PATCH] pkg: Simplify some APIs by using parse.Tree. --- pkg/edit/editor.go | 2 +- pkg/edit/highlight.go | 7 +++---- pkg/edit/highlight/highlight.go | 6 +++--- pkg/edit/highlight/highlighter_test.go | 2 +- pkg/edit/highlight_test.go | 4 ++-- pkg/eval/builtin_fn_misc.go | 2 +- pkg/eval/builtin_special.go | 2 +- pkg/eval/compiler.go | 6 +++--- pkg/eval/eval.go | 10 +++++----- pkg/eval/testutils.go | 2 +- 10 files changed, 21 insertions(+), 22 deletions(-) diff --git a/pkg/edit/editor.go b/pkg/edit/editor.go index 923426a1..9fcacf8a 100644 --- a/pkg/edit/editor.go +++ b/pkg/edit/editor.go @@ -77,7 +77,7 @@ func evalDefaultBinding(ev *eval.Evaler, ns eval.Ns) { if err != nil { panic(err) } - op, err := ev.CompileWithGlobal(tree.Root, src, ns) + op, err := ev.CompileWithGlobal(tree, ns) if err != nil { panic(err) } diff --git a/pkg/edit/highlight.go b/pkg/edit/highlight.go index 48f4b8fc..a3047a6e 100644 --- a/pkg/edit/highlight.go +++ b/pkg/edit/highlight.go @@ -13,14 +13,13 @@ import ( func initHighlighter(appSpec *cli.AppSpec, ev *eval.Evaler) { appSpec.Highlighter = highlight.NewHighlighter(highlight.Config{ - Check: func(n *parse.Chunk) error { return check(ev, n) }, + Check: func(tree parse.Tree) error { return check(ev, tree) }, HasCommand: func(cmd string) bool { return hasCommand(ev, cmd) }, }) } -func check(ev *eval.Evaler, n *parse.Chunk) error { - src := parse.Source{Name: "[tty]", Code: parse.SourceText(n)} - _, err := ev.Compile(n, src) +func check(ev *eval.Evaler, tree parse.Tree) error { + _, err := ev.Compile(tree) return err } diff --git a/pkg/edit/highlight/highlight.go b/pkg/edit/highlight/highlight.go index e7753a21..1239e86e 100644 --- a/pkg/edit/highlight/highlight.go +++ b/pkg/edit/highlight/highlight.go @@ -11,7 +11,7 @@ import ( // Config keeps configuration for highlighting code. type Config struct { - Check func(n *parse.Chunk) error + Check func(n parse.Tree) error HasCommand func(name string) bool } @@ -31,7 +31,7 @@ func highlight(code string, cfg Config, lateCb func(ui.Text)) (ui.Text, []error) var errors []error var errorRegions []region - tree, errParse := parse.Parse(parse.Source{Name: "[interactive]", Code: code}) + tree, errParse := parse.Parse(parse.Source{Name: "[tty]", Code: code}) if errParse != nil { for _, err := range errParse.(*parse.MultiError).Entries { if err.Context.From != len(code) { @@ -45,7 +45,7 @@ func highlight(code string, cfg Config, lateCb func(ui.Text)) (ui.Text, []error) } if cfg.Check != nil { - err := cfg.Check(tree.Root) + err := cfg.Check(tree) if r, ok := err.(diag.Ranger); ok && r.Range().From != len(code) { errors = append(errors, err) errorRegions = append(errorRegions, diff --git a/pkg/edit/highlight/highlighter_test.go b/pkg/edit/highlight/highlighter_test.go index 89f3f777..fbb27755 100644 --- a/pkg/edit/highlight/highlighter_test.go +++ b/pkg/edit/highlight/highlighter_test.go @@ -76,7 +76,7 @@ func TestHighlighter_CheckErrors(t *testing.T) { var checkError error // Make a highlighter whose Check callback returns checkError. hl := NewHighlighter(Config{ - Check: func(*parse.Chunk) error { return checkError }}) + Check: func(parse.Tree) error { return checkError }}) getWithCheckError := func(code string, err error) (ui.Text, []error) { checkError = err return hl.Get(code) diff --git a/pkg/edit/highlight_test.go b/pkg/edit/highlight_test.go index 1dbc62e0..f23df520 100644 --- a/pkg/edit/highlight_test.go +++ b/pkg/edit/highlight_test.go @@ -130,12 +130,12 @@ func TestMakeHasCommand(t *testing.T) { }) } -func mustParse(src string) *parse.Chunk { +func mustParse(src string) parse.Tree { tree, err := parse.Parse(parse.SourceForTest(src)) if err != nil { panic(err) } - return tree.Root + return tree } func mustMkdirAll(path string) { diff --git a/pkg/eval/builtin_fn_misc.go b/pkg/eval/builtin_fn_misc.go index 8948e3c6..d5324c1e 100644 --- a/pkg/eval/builtin_fn_misc.go +++ b/pkg/eval/builtin_fn_misc.go @@ -276,7 +276,7 @@ func source(fm *Frame, fname string) error { for name := range fm.up.static() { scriptGlobal.set(name) } - op, err := compile(fm.Builtin.static(), scriptGlobal, tree.Root, src) + op, err := compile(fm.Builtin.static(), scriptGlobal, tree) if err != nil { return err } diff --git a/pkg/eval/builtin_special.go b/pkg/eval/builtin_special.go index df59918e..15a08130 100644 --- a/pkg/eval/builtin_special.go +++ b/pkg/eval/builtin_special.go @@ -314,7 +314,7 @@ func evalModule(fm *Frame, r diag.Ranger, key string, src parse.Source) (Ns, err fm.addTraceback(r), false, } - op, err := compile(newFm.Builtin.static(), modGlobal.static(), tree.Root, src) + op, err := compile(newFm.Builtin.static(), modGlobal.static(), tree) if err != nil { return nil, err } diff --git a/pkg/eval/compiler.go b/pkg/eval/compiler.go index 6a3c4c79..a274f8bc 100644 --- a/pkg/eval/compiler.go +++ b/pkg/eval/compiler.go @@ -23,8 +23,8 @@ type compiler struct { srcMeta parse.Source } -func compile(b, g staticNs, n *parse.Chunk, src parse.Source) (op Op, err error) { - cp := &compiler{b, []staticNs{g}, make(staticNs), nil, src} +func compile(b, g staticNs, tree parse.Tree) (op Op, err error) { + cp := &compiler{b, []staticNs{g}, make(staticNs), nil, tree.Source} defer func() { r := recover() if r == nil { @@ -37,7 +37,7 @@ func compile(b, g staticNs, n *parse.Chunk, src parse.Source) (op Op, err error) panic(r) } }() - return Op{cp.chunkOp(n), src}, nil + return Op{cp.chunkOp(tree.Root), tree.Source}, nil } func (cp *compiler) errorpf(r diag.Ranger, format string, args ...interface{}) { diff --git a/pkg/eval/eval.go b/pkg/eval/eval.go index ed2d334a..9f86adbb 100644 --- a/pkg/eval/eval.go +++ b/pkg/eval/eval.go @@ -286,13 +286,13 @@ func (ev *Evaler) ParseAndCompile(src parse.Source) (Op, error) { if err != nil { return Op{}, err } - return ev.Compile(tree.Root, src) + return ev.Compile(tree) } // Compile compiles Elvish code in the global scope. If the error is not nil, it // can be passed to GetCompilationError to retrieve more details. -func (ev *Evaler) Compile(n *parse.Chunk, src parse.Source) (Op, error) { - return ev.CompileWithGlobal(n, src, ev.Global) +func (ev *Evaler) Compile(tree parse.Tree) (Op, error) { + return ev.CompileWithGlobal(tree, ev.Global) } // CompileWithGlobal compiles Elvish code in an alternative global scope. If the @@ -302,6 +302,6 @@ func (ev *Evaler) Compile(n *parse.Chunk, src parse.Source) (Op, error) { // TODO(xiaq): To use the Op created, the caller must create a Frame and mutate // its local scope manually. Consider restructuring the API to make that // unnecessary. -func (ev *Evaler) CompileWithGlobal(n *parse.Chunk, src parse.Source, g Ns) (Op, error) { - return compile(ev.Builtin.static(), g.static(), n, src) +func (ev *Evaler) CompileWithGlobal(tree parse.Tree, g Ns) (Op, error) { + return compile(ev.Builtin.static(), g.static(), tree) } diff --git a/pkg/eval/testutils.go b/pkg/eval/testutils.go index 264a1181..4eb1fbf3 100644 --- a/pkg/eval/testutils.go +++ b/pkg/eval/testutils.go @@ -261,7 +261,7 @@ func evalAndCollect(t *testing.T, ev *Evaler, texts []string) result { if err != nil { t.Fatalf("Parse(%q) error: %s", src.Code, err) } - op, err := ev.Compile(tree.Root, src) + op, err := ev.Compile(tree) if err != nil { // NOTE: Only the compilation error of the last code is saved. r.compilationError = err