| 1 |
// Package repl provides a read/eval/print loop for Starlark. |
| 2 |
// |
| 3 |
// It supports readline-style command editing, |
| 4 |
// and interrupts through Control-C. |
| 5 |
// |
| 6 |
// If an input line can be parsed as an expression, |
| 7 |
// the REPL parses and evaluates it and prints its result. |
| 8 |
// Otherwise the REPL reads lines until a blank line, |
| 9 |
// then tries again to parse the multi-line input as an |
| 10 |
// expression. If the input still cannot be parsed as an expression, |
| 11 |
// the REPL parses and executes it as a file (a list of statements), |
| 12 |
// for side effects. |
| 13 |
package repl // import "go.starlark.net/repl" |
| 14 |
|
| 15 |
import ( |
| 16 |
"context" |
| 17 |
"fmt" |
| 18 |
"io" |
| 19 |
"os" |
| 20 |
"os/signal" |
| 21 |
|
| 22 |
"github.com/chzyer/readline" |
| 23 |
"go.starlark.net/starlark" |
| 24 |
"go.starlark.net/syntax" |
| 25 |
) |
| 26 |
|
| 27 |
var interrupted = make(chan os.Signal, 1) |
| 28 |
|
| 29 |
// REPL calls [REPLOptions] using [syntax.LegacyFileOptions]. |
| 30 |
// |
| 31 |
// Deprecated: use [REPLOptions] with [syntax.FileOptions] instead, |
| 32 |
// because this function relies on legacy global variables. |
| 33 |
func REPL(thread *starlark.Thread, globals starlark.StringDict) { |
| 34 |
REPLOptions(syntax.LegacyFileOptions(), thread, globals) |
| 35 |
} |
| 36 |
|
| 37 |
// REPLOptions executes a read, eval, print loop. |
| 38 |
// |
| 39 |
// Before evaluating each expression, it sets the Starlark thread local |
| 40 |
// variable named "context" to a context.Context that is cancelled by a |
| 41 |
// SIGINT (Control-C). Client-supplied global functions may use this |
| 42 |
// context to make long-running operations interruptable. |
| 43 |
func REPLOptions(opts *syntax.FileOptions, thread *starlark.Thread, globals starlark.StringDict) { |
| 44 |
signal.Notify(interrupted, os.Interrupt) |
| 45 |
defer signal.Stop(interrupted) |
| 46 |
|
| 47 |
rl, err := readline.New(">>> ") |
| 48 |
if err != nil { |
| 49 |
PrintError(err) |
| 50 |
return |
| 51 |
} |
| 52 |
defer rl.Close() |
| 53 |
for { |
| 54 |
if err := rep(opts, rl, thread, globals); err != nil { |
| 55 |
if err == readline.ErrInterrupt { |
| 56 |
fmt.Println(err) |
| 57 |
continue |
| 58 |
} |
| 59 |
break |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
// rep reads, evaluates, and prints one item. |
| 65 |
// |
| 66 |
// It returns an error (possibly readline.ErrInterrupt) |
| 67 |
// only if readline failed. Starlark errors are printed. |
| 68 |
func rep(opts *syntax.FileOptions, rl *readline.Instance, thread *starlark.Thread, globals starlark.StringDict) error { |
| 69 |
// Each item gets its own context, |
| 70 |
// which is cancelled by a SIGINT. |
| 71 |
// |
| 72 |
// Note: during Readline calls, Control-C causes Readline to return |
| 73 |
// ErrInterrupt but does not generate a SIGINT. |
| 74 |
ctx, cancel := context.WithCancel(context.Background()) |
| 75 |
defer cancel() |
| 76 |
go func() { |
| 77 |
select { |
| 78 |
case <-interrupted: |
| 79 |
cancel() |
| 80 |
case <-ctx.Done(): |
| 81 |
} |
| 82 |
}() |
| 83 |
|
| 84 |
thread.SetLocal("context", ctx) |
| 85 |
|
| 86 |
eof := false |
| 87 |
|
| 88 |
// readline returns EOF, ErrInterrupted, or a line including "\n". |
| 89 |
rl.SetPrompt(">>> ") |
| 90 |
readline := func() ([]byte, error) { |
| 91 |
line, err := rl.Readline() |
| 92 |
rl.SetPrompt("... ") |
| 93 |
if err != nil { |
| 94 |
if err == io.EOF { |
| 95 |
eof = true |
| 96 |
} |
| 97 |
return nil, err |
| 98 |
} |
| 99 |
return []byte(line + "\n"), nil |
| 100 |
} |
| 101 |
|
| 102 |
// Treat load bindings as global (like they used to be) in the REPL. |
| 103 |
// Fixes github.com/google/starlark-go/issues/224. |
| 104 |
opts2 := *opts |
| 105 |
opts2.LoadBindsGlobally = true |
| 106 |
opts = &opts2 |
| 107 |
|
| 108 |
// parse |
| 109 |
f, err := opts.ParseCompoundStmt("<stdin>", readline) |
| 110 |
if err != nil { |
| 111 |
if eof { |
| 112 |
return io.EOF |
| 113 |
} |
| 114 |
PrintError(err) |
| 115 |
return nil |
| 116 |
} |
| 117 |
|
| 118 |
if expr := soleExpr(f); expr != nil { |
| 119 |
// eval |
| 120 |
v, err := starlark.EvalExprOptions(f.Options, thread, expr, globals) |
| 121 |
if err != nil { |
| 122 |
PrintError(err) |
| 123 |
return nil |
| 124 |
} |
| 125 |
|
| 126 |
// store the result in "_" variable to hold the value of last expression, similar to Python REPL |
| 127 |
globals["_"] = v |
| 128 |
|
| 129 |
// print |
| 130 |
if v != starlark.None { |
| 131 |
fmt.Println(v) |
| 132 |
} |
| 133 |
} else if err := starlark.ExecREPLChunk(f, thread, globals); err != nil { |
| 134 |
PrintError(err) |
| 135 |
return nil |
| 136 |
} |
| 137 |
|
| 138 |
return nil |
| 139 |
} |
| 140 |
|
| 141 |
func soleExpr(f *syntax.File) syntax.Expr { |
| 142 |
if len(f.Stmts) == 1 { |
| 143 |
if stmt, ok := f.Stmts[0].(*syntax.ExprStmt); ok { |
| 144 |
return stmt.X |
| 145 |
} |
| 146 |
} |
| 147 |
return nil |
| 148 |
} |
| 149 |
|
| 150 |
// PrintError prints the error to stderr, |
| 151 |
// or its backtrace if it is a Starlark evaluation error. |
| 152 |
func PrintError(err error) { |
| 153 |
if evalErr, ok := err.(*starlark.EvalError); ok { |
| 154 |
fmt.Fprintln(os.Stderr, evalErr.Backtrace()) |
| 155 |
} else { |
| 156 |
fmt.Fprintln(os.Stderr, err) |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
// MakeLoad calls [MakeLoadOptions] using [syntax.LegacyFileOptions]. |
| 161 |
// |
| 162 |
// Deprecated: use [MakeLoadOptions] with [syntax.FileOptions] instead, |
| 163 |
// because this function relies on legacy global variables. |
| 164 |
func MakeLoad() func(thread *starlark.Thread, module string) (starlark.StringDict, error) { |
| 165 |
return MakeLoadOptions(syntax.LegacyFileOptions()) |
| 166 |
} |
| 167 |
|
| 168 |
// MakeLoadOptions returns a simple sequential implementation of module loading |
| 169 |
// suitable for use in the REPL. |
| 170 |
// Each function returned by MakeLoadOptions accesses a distinct private cache. |
| 171 |
func MakeLoadOptions(opts *syntax.FileOptions) func(thread *starlark.Thread, module string) (starlark.StringDict, error) { |
| 172 |
type entry struct { |
| 173 |
globals starlark.StringDict |
| 174 |
err error |
| 175 |
} |
| 176 |
|
| 177 |
var cache = make(map[string]*entry) |
| 178 |
|
| 179 |
return func(thread *starlark.Thread, module string) (starlark.StringDict, error) { |
| 180 |
e, ok := cache[module] |
| 181 |
if e == nil { |
| 182 |
if ok { |
| 183 |
// request for package whose loading is in progress |
| 184 |
return nil, fmt.Errorf("cycle in load graph") |
| 185 |
} |
| 186 |
|
| 187 |
// Add a placeholder to indicate "load in progress". |
| 188 |
cache[module] = nil |
| 189 |
|
| 190 |
// Load it. |
| 191 |
thread := &starlark.Thread{Name: "exec " + module, Load: thread.Load} |
| 192 |
globals, err := starlark.ExecFileOptions(opts, thread, module, nil, nil) |
| 193 |
e = &entry{globals, err} |
| 194 |
|
| 195 |
// Update the cache. |
| 196 |
cache[module] = e |
| 197 |
} |
| 198 |
return e.globals, e.err |
| 199 |
} |
| 200 |
} |