mirror of
https://github.com/go-sylixos/elvish.git
synced 2024-12-12 17:27:50 +08:00
pkg: Simplify some APIs by using parse.Tree.
This commit is contained in:
parent
4d54943ffb
commit
88122a019e
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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{}) {
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user