parent:
20dc26e627d8341795ca628664e44a83b66d316a
nmyk <nick@nmyk.io>
2026-11-14T20:51:21-05:00
refactor govlisp repl
diff --git a/DESIGN.md b/DESIGN.md
index 5678bfa07dc7353ec71dac4260c3c87f220ea340..e523e6027e1415b7261daf57c85ddc18d40284c1 100644
--- a/DESIGN.md
+++ b/DESIGN.md
@@ -340,7 +340,7 @@ Shared flags: `--at <RFC 3339 time>`, `--anchor <cert or public key PEM>`, `--crl <file>` (repeatable), `--max-nodes <n>`.
The CLI is built on [cobra](https://github.com/spf13/cobra), so flags follow GNU style: long flags take two dashes, `-o` is short for `--output`, flags may come before or after the file name, and `government <command> --help` lists a command's flags.
-GOVERNMENT's Lisp source files use the extension `.gov.lisp`. A second command, `govlisp`, runs Lisp source directly, with no certificates: `govlisp` alone is a REPL (multi-line forms, errors reported without ending the session), and `govlisp prog.gov.lisp` runs a file, printing each non-`define` value and reporting the first error at its source line and column.
+GOVERNMENT's Lisp source files use the extension `.gov.lisp`. A second command, `govlisp`, runs Lisp source directly, with no certificates: `govlisp` alone is a REPL (multi-line forms, errors reported without ending the session; on a terminal, line editing and history via [ergochat/readline](https://github.com/ergochat/readline), kept in `~/.govlisp_history`, Tab completion of names in scope, and Ctrl-C to discard the form being typed), and `govlisp prog.gov.lisp` runs a file, printing each non-`define` value and reporting the first error at its source line and column.
## Transpiler and test suite
diff --git a/PLAN.md b/PLAN.md
index a0ad2dd00f0fd73dc0c16e3c334d1e79ca20b44d..4b78842a379855cda5ef673497786365937c7523 100644
--- a/PLAN.md
+++ b/PLAN.md
@@ -91,7 +91,7 @@ - Build: typed errors with codes (`E-UNBOUND`, `E-ARITY`, …) and an optional source location; a test runner that reads `testdata/*.gov.lisp`, parses `;=>` and `;=!` annotations, and runs each top-level form through reader + evaluator.
- Learn: designing errors as data; golden-style tests that read like documentation.
- Done when: the design doc's example test files all pass. **Milestone: the language works.**
-**Extra: the `govlisp` REPL** (`cmd/govlisp`). Added between Phases 1 and 2. `govlisp` with no arguments is an interactive REPL; `govlisp prog.gov.lisp` runs a file. A form may span lines: the reader marks syntax errors caused only by input ending early as incomplete, and the REPL reads another line instead of reporting them. Errors name the failing subexpression and keep the session going.
+**Extra: the `govlisp` REPL** (`cmd/govlisp`). Added between Phases 1 and 2. `govlisp` with no arguments is an interactive REPL; `govlisp prog.gov.lisp` runs a file. A form may span lines: the reader marks syntax errors caused only by input ending early as incomplete, and the REPL reads another line instead of reporting them. Errors name the failing subexpression and keep the session going. Later moved onto cobra, with readline on a terminal: line editing, history, Tab completion of names in scope (Tab indents when there is nothing to complete), and Ctrl-C to discard the current entry. Piped input still gets the plain line reader and no prompts.
## Phase 2: The AST and OID encoding (steps 14–17)
diff --git a/cmd/govlisp/lines.go b/cmd/govlisp/lines.go
index 856923c765037bf19bf28f9509b29af033772449..ac3dd23f2ca4349931625831ac9fe1b9210b5dd7 100644
--- a/cmd/govlisp/lines.go
+++ b/cmd/govlisp/lines.go
@@ -2,23 +2,53 @@ package main
import (
"bufio"
+ "errors"
"io"
+ "os"
+ "path/filepath"
+ "sort"
+ "strings"
+
+ "github.com/ergochat/readline"
+
+ "government/builtin"
+ "government/eval"
+ "government/sexpr"
)
-// lineReader yields input one line at a time, without the line ending. It
-// has no limit on line length, unlike bufio.Scanner's default.
-type lineReader struct {
+// newSource picks how the REPL reads: with readline on a terminal, and
+// line by line with no prompts otherwise, so piped input gives clean
+// output.
+func newSource(stdin *os.File, top *eval.TopLevel) (lineSource, error) {
+ if !isTerminal(stdin) {
+ return newPlainSource(stdin), nil
+ }
+ history := ""
+ if home, err := os.UserHomeDir(); err == nil {
+ history = filepath.Join(home, ".govlisp_history")
+ }
+ return newReadlineSource(&readline.Config{HistoryFile: history}, top)
+}
+
+func isTerminal(f *os.File) bool {
+ info, err := f.Stat()
+ return err == nil && info.Mode()&os.ModeCharDevice != 0
+}
+
+// plainSource yields input one line at a time, without the line ending.
+// It has no limit on line length, unlike bufio.Scanner's default.
+type plainSource struct {
r *bufio.Reader
}
-func newLineReader(in io.Reader) *lineReader {
- return &lineReader{r: bufio.NewReader(in)}
+func newPlainSource(in io.Reader) *plainSource {
+ return &plainSource{r: bufio.NewReader(in)}
}
-func (l *lineReader) next() (string, bool) {
- line, err := l.r.ReadString('\n')
+func (s *plainSource) readLine(string) (string, error) {
+ line, err := s.r.ReadString('\n')
if err != nil && line == "" {
- return "", false
+ return "", io.EOF
}
if n := len(line); n > 0 && line[n-1] == '\n' {
line = line[:n-1]
@@ -26,5 +56,84 @@ if n := len(line); n > 0 && line[n-1] == '\r' {
line = line[:n-1]
}
}
- return line, true
+ return line, nil
+}
+
+func (s *plainSource) Close() error { return nil }
+
+// readlineSource reads from a terminal with line editing and history.
+// History is kept one line at a time, as typed: joining a multi-line form
+// into one entry would change its meaning when a line ends in a comment
+// or a string spans lines.
+type readlineSource struct {
+ rl *readline.Instance
+}
+
+func newReadlineSource(cfg *readline.Config, top *eval.TopLevel) (*readlineSource, error) {
+ cfg.Prompt = prompt
+ cfg.AutoComplete = completer{top}
+ cfg.InterruptPrompt = "^C"
+ cfg.HistorySearchFold = true
+ rl, err := readline.NewFromConfig(cfg)
+ if err != nil {
+ return nil, err
+ }
+ return &readlineSource{rl}, nil
+}
+
+func (s *readlineSource) readLine(p string) (string, error) {
+ s.rl.SetPrompt(p)
+ line, err := s.rl.Readline()
+ if errors.Is(err, readline.ErrInterrupt) {
+ return "", errInterrupt
+ }
+ return line, err
+}
+
+func (s *readlineSource) Close() error { return s.rl.Close() }
+
+// completer completes the name before the cursor from everything in scope:
+// the special forms, the builtins and the user's own definitions. With
+// nothing to complete, Tab indents, which is what it is for on the
+// continuation lines of a long form.
+type completer struct {
+ top *eval.TopLevel
+}
+
+const indent = " "
+
+func (c completer) Do(line []rune, pos int) ([][]rune, int) {
+ start := pos
+ for start > 0 && !sexpr.IsDelimiter(line[start-1]) {
+ start--
+ }
+ prefix := string(line[start:pos])
+ if prefix == "" {
+ return [][]rune{[]rune(indent)}, 0
+ }
+ var out [][]rune
+ for _, name := range c.names() {
+ if strings.HasPrefix(name, prefix) && name != prefix {
+ out = append(out, []rune(name[len(prefix):]))
+ }
+ }
+ return out, len([]rune(prefix))
+}
+
+func (c completer) names() []string {
+ seen := map[string]bool{}
+ for _, spec := range builtin.All {
+ if spec.Kind == builtin.SpecialForm {
+ seen[spec.Name] = true
+ }
+ }
+ for _, name := range c.top.Names() {
+ seen[string(name)] = true
+ }
+ names := make([]string, 0, len(seen))
+ for name := range seen {
+ names = append(names, name)
+ }
+ sort.Strings(names)
+ return names
}
diff --git a/cmd/govlisp/main.go b/cmd/govlisp/main.go
index 395007c10921b253eda00c2ff0c1c3db7b294965..674861883a99b7a821b412ff4395ccaebfc8a21b 100644
--- a/cmd/govlisp/main.go
+++ b/cmd/govlisp/main.go
@@ -1,18 +1,17 @@
-// Command govlisp runs GOVERNMENT programs straight from Lisp source, with
-// no certificates involved.
+// Command govlisp runs GOVERNMENT programs straight from GovLisp source,
+// with no certificates involved.
//
-// govlisp interactive REPL
+// govlisp interactive REPL
// govlisp prog.gov.lisp run a file, printing the value of each non-define form
-//
-// The REPL has no line editing; wrap it with rlwrap for history and arrow
-// keys.
package main
import (
+ "errors"
"fmt"
"io"
"os"
- "strings"
+
+ "github.com/spf13/cobra"
"government/eval"
"government/sexpr"
@@ -25,20 +24,59 @@ exitSyntax = 2
)
func main() {
- switch len(os.Args) {
- case 1:
- os.Exit(repl(os.Stdin, os.Stdout, isTerminal(os.Stdin)))
- case 2:
- os.Exit(runFile(os.Args[1], os.Stdout, os.Stderr))
- default:
- fmt.Fprintln(os.Stderr, "usage: govlisp [file.gov.lisp]")
- os.Exit(exitSyntax)
- }
+ os.Exit(run(os.Args[1:], os.Stdin, os.Stdout, os.Stderr))
}
-func isTerminal(f *os.File) bool {
- info, err := f.Stat()
- return err == nil && info.Mode()&os.ModeCharDevice != 0
+// exitCode carries a file run's exit code out through cobra; the file run
+// has already reported the error itself.
+type exitCode int
+
+func (c exitCode) Error() string { return fmt.Sprintf("exit %d", int(c)) }
+
+func run(args []string, stdin *os.File, stdout, stderr io.Writer) int {
+ root := &cobra.Command{
+ Use: "govlisp [prog.gov.lisp]",
+ Short: "Run GovLisp source directly, with no certificates",
+ Long: "govlisp runs GovLisp source directly, with no certificates.\n\n" +
+ "With a file, it evaluates each top-level form in order, prints the value of each\n" +
+ "one that is not a define, and stops at the first error.\n\n" +
+ "With no file, it starts a REPL. On a terminal the REPL has line editing, history\n" +
+ "(kept in ~/.govlisp_history), tab completion of names in scope, and Ctrl-C to\n" +
+ "discard the form being typed; Ctrl-D ends the session.",
+ Args: cobra.MaximumNArgs(1),
+ SilenceUsage: true,
+ SilenceErrors: true,
+ CompletionOptions: cobra.CompletionOptions{DisableDefaultCmd: true},
+ RunE: func(cmd *cobra.Command, args []string) error {
+ if len(args) == 1 {
+ if code := runFile(args[0], cmd.OutOrStdout(), cmd.ErrOrStderr()); code != exitOK {
+ return exitCode(code)
+ }
+ return nil
+ }
+ top := eval.NewTopLevel()
+ src, err := newSource(stdin, top)
+ if err != nil {
+ return err
+ }
+ defer src.Close()
+ repl(top, src, cmd.OutOrStdout())
+ return nil
+ },
+ }
+ root.SetArgs(args)
+ root.SetOut(stdout)
+ root.SetErr(stderr)
+ err := root.Execute()
+ var code exitCode
+ switch {
+ case err == nil:
+ return exitOK
+ case errors.As(err, &code):
+ return int(code)
+ }
+ fmt.Fprintf(stderr, "govlisp: %v\nRun 'govlisp --help' for usage.\n", err)
+ return exitSyntax
}
// runFile evaluates every top-level form in path, printing each non-define
@@ -84,73 +122,6 @@ fmt.Fprintln(stdout, sexpr.Print(v))
}
}
return exitOK
-}
-
-// repl reads forms from in until end of input. A form may span several
-// lines: while the input so far is an unfinished form, it keeps reading.
-// An error is reported and the session carries on; definitions made before
-// it are kept.
-func repl(in io.Reader, out io.Writer, prompts bool) int {
- const (
- prompt = "gov> "
- continuation = "...> "
- )
- top := eval.NewTopLevel()
- lines := newLineReader(in)
- var entry strings.Builder
-
- for {
- if prompts {
- if entry.Len() == 0 {
- fmt.Fprint(out, prompt)
- } else {
- fmt.Fprint(out, continuation)
- }
- }
- line, ok := lines.next()
- if !ok {
- if entry.Len() > 0 {
- if _, err := sexpr.ReadAll(entry.String()); err != nil {
- fmt.Fprintf(out, "syntax error: %v\n", err)
- }
- }
- if prompts {
- fmt.Fprintln(out)
- }
- return exitOK
- }
- entry.WriteString(line)
- entry.WriteByte('\n')
-
- forms, err := sexpr.ReadAll(entry.String())
- if se, ok := err.(*sexpr.SyntaxError); ok && se.Incomplete {
- continue
- }
- entry.Reset()
- if err != nil {
- fmt.Fprintf(out, "syntax error: %v\n", err)
- continue
- }
- for _, form := range forms {
- v, err := top.Eval(form)
- if err != nil {
- fmt.Fprintln(out, describe(err))
- break
- }
- if !isDefine(form) {
- fmt.Fprintln(out, sexpr.Print(v))
- }
- }
- }
-}
-
-// describe adds the subexpression that failed to an evaluation error.
-func describe(err error) string {
- e, ok := err.(*eval.Error)
- if !ok || e.Expr == nil {
- return err.Error()
- }
- return fmt.Sprintf("%v\n in %s", err, sexpr.Print(e.Expr))
}
func isDefine(form sexpr.Value) bool {
diff --git a/cmd/govlisp/main_test.go b/cmd/govlisp/main_test.go
index 08b0e287031d1d43ebd94c017eacf9eb3fbf6d3c..b1fad469b95a1a88b96f355be9d1741a0749c3dc 100644
--- a/cmd/govlisp/main_test.go
+++ b/cmd/govlisp/main_test.go
@@ -6,14 +6,14 @@ "os"
"path/filepath"
"strings"
"testing"
+
+ "government/eval"
)
-func runREPL(t *testing.T, input string, prompts bool) string {
+func runREPL(t *testing.T, input string) string {
t.Helper()
var out bytes.Buffer
- if code := repl(strings.NewReader(input), &out, prompts); code != exitOK {
- t.Fatalf("repl exited with %d", code)
- }
+ repl(eval.NewTopLevel(), newPlainSource(strings.NewReader(input)), &out)
return out.String()
}
@@ -57,21 +57,13 @@ in (car 5)
syntax error: 1:1: unexpected )
16
`
- if got := runREPL(t, input, false); got != want {
+ if got := runREPL(t, input); got != want {
t.Errorf("transcript:\n%s\nwant:\n%s", got, want)
}
}
-func TestREPLPrompts(t *testing.T) {
- got := runREPL(t, "(+ 1\n2)\n", true)
- want := "gov> ...> 3\ngov> \n"
- if got != want {
- t.Errorf("got %q, want %q", got, want)
- }
-}
-
func TestREPLUnfinishedInputAtEOF(t *testing.T) {
- got := runREPL(t, "(+ 1\n", false)
+ got := runREPL(t, "(+ 1\n")
if want := "syntax error: 1:1: unterminated list\n"; got != want {
t.Errorf("got %q, want %q", got, want)
}
diff --git a/cmd/govlisp/readline_test.go b/cmd/govlisp/readline_test.go
new file mode 100644
index 0000000000000000000000000000000000000000..928951aa45705be854b4b0f6d921733139042895
--- /dev/null
+++ b/cmd/govlisp/readline_test.go
@@ -0,0 +1,110 @@
+package main
+
+import (
+ "bytes"
+ "io"
+ "os"
+ "path/filepath"
+ "reflect"
+ "strings"
+ "testing"
+
+ "github.com/ergochat/readline"
+
+ "government/eval"
+)
+
+// fakeTerminal runs readline over in as if it were a terminal, so a test
+// can type keys at it: "\r" is Enter, "\t" is Tab, "\x03" is Ctrl-C.
+func fakeTerminal(t *testing.T, in string) (*readlineSource, *eval.TopLevel, *bytes.Buffer, string) {
+ t.Helper()
+ var out bytes.Buffer
+ history := filepath.Join(t.TempDir(), "history")
+ top := eval.NewTopLevel()
+ src, err := newReadlineSource(&readline.Config{
+ Stdin: strings.NewReader(in),
+ Stdout: &out,
+ HistoryFile: history,
+ FuncIsTerminal: func() bool { return true },
+ FuncMakeRaw: func() error { return nil },
+ FuncExitRaw: func() error { return nil },
+ FuncGetSize: func() (int, int) { return 80, 24 },
+ FuncOnWidthChanged: func(func()) {},
+ }, top)
+ if err != nil {
+ t.Fatal(err)
+ }
+ return src, top, &out, history
+}
+
+func TestReadlineREPL(t *testing.T) {
+ keys := "(define fact (lambda (n)\r" +
+ "\t(if (== n 0) 1 (* n (fact (- n 1))))))\r" +
+ "(fa\t 10)\r" +
+ "(car\x03" +
+ "(+ 1\r2)\r"
+ src, top, out, history := fakeTerminal(t, keys)
+ repl(top, src, out)
+ src.Close()
+
+ got := out.String()
+ for _, want := range []string{"gov> ", "...> ", "3628800\n", "^C", "3\n"} {
+ if !strings.Contains(got, want) {
+ t.Errorf("output lacks %q:\n%q", want, got)
+ }
+ }
+ if strings.Contains(got, "E-") || strings.Contains(got, "syntax error") {
+ t.Errorf("an error in the session (Ctrl-C should have dropped \"(car\"):\n%q", got)
+ }
+ data, err := os.ReadFile(history)
+ if err != nil {
+ t.Fatal(err)
+ }
+ lines := strings.Split(strings.TrimSpace(string(data)), "\n")
+ want := []string{
+ "(define fact (lambda (n)",
+ " (if (== n 0) 1 (* n (fact (- n 1))))))",
+ "(fact 10)",
+ "(+ 1",
+ "2)",
+ }
+ if !reflect.DeepEqual(lines, want) {
+ t.Errorf("history %q\nwant %q", lines, want)
+ }
+}
+
+func TestCompleter(t *testing.T) {
+ top := eval.NewTopLevel()
+ forms := "(define square (lambda (n) (* n n)))"
+ runREPLWith(t, top, forms)
+ c := completer{top}
+ tests := []struct {
+ line string
+ want []string
+ length int
+ }{
+ {"(squ", []string{"are"}, 3},
+ {"(la", []string{"mbda"}, 2},
+ {"(ca", []string{"r"}, 2},
+ {"(car", nil, 3},
+ {"'(cd", []string{"r"}, 2},
+ {"", []string{indent}, 0},
+ {"(+ 1 ", []string{indent}, 0},
+ {"(zzz", nil, 3},
+ }
+ for _, tt := range tests {
+ got, length := c.Do([]rune(tt.line), len([]rune(tt.line)))
+ var words []string
+ for _, r := range got {
+ words = append(words, string(r))
+ }
+ if !reflect.DeepEqual(words, tt.want) || length != tt.length {
+ t.Errorf("%q: %q, %d; want %q, %d", tt.line, words, length, tt.want, tt.length)
+ }
+ }
+}
+
+func runREPLWith(t *testing.T, top *eval.TopLevel, input string) {
+ t.Helper()
+ repl(top, newPlainSource(strings.NewReader(input)), io.Discard)
+}
diff --git a/cmd/govlisp/repl.go b/cmd/govlisp/repl.go
new file mode 100644
index 0000000000000000000000000000000000000000..8d05cd2391b51f38d521d7b76d442c302364c729
--- /dev/null
+++ b/cmd/govlisp/repl.go
@@ -0,0 +1,85 @@
+package main
+
+import (
+ "errors"
+ "fmt"
+ "io"
+ "strings"
+
+ "government/eval"
+ "government/sexpr"
+)
+
+const (
+ prompt = "gov> "
+ continuation = "...> "
+)
+
+// errInterrupt is Ctrl-C at a prompt: throw away the form being typed.
+var errInterrupt = errors.New("interrupt")
+
+// A lineSource hands the REPL one line of input at a time. At the end of
+// input it returns io.EOF, and errInterrupt when the user asks to discard
+// the current entry.
+type lineSource interface {
+ readLine(prompt string) (string, error)
+ Close() error
+}
+
+// repl reads forms until end of input. A form may span several lines:
+// while the input so far is an unfinished form, it keeps reading, with the
+// continuation prompt. An error is reported and the session carries on;
+// definitions made before it are kept.
+func repl(top *eval.TopLevel, src lineSource, out io.Writer) {
+ var entry strings.Builder
+ for {
+ p := prompt
+ if entry.Len() > 0 {
+ p = continuation
+ }
+ line, err := src.readLine(p)
+ if errors.Is(err, errInterrupt) {
+ entry.Reset()
+ continue
+ }
+ if err != nil {
+ if entry.Len() > 0 {
+ if _, err := sexpr.ReadAll(entry.String()); err != nil {
+ fmt.Fprintf(out, "syntax error: %v\n", err)
+ }
+ }
+ return
+ }
+ entry.WriteString(line)
+ entry.WriteByte('\n')
+
+ forms, err := sexpr.ReadAll(entry.String())
+ if se, ok := err.(*sexpr.SyntaxError); ok && se.Incomplete {
+ continue
+ }
+ entry.Reset()
+ if err != nil {
+ fmt.Fprintf(out, "syntax error: %v\n", err)
+ continue
+ }
+ for _, form := range forms {
+ v, err := top.Eval(form)
+ if err != nil {
+ fmt.Fprintln(out, describe(err))
+ break
+ }
+ if !isDefine(form) {
+ fmt.Fprintln(out, sexpr.Print(v))
+ }
+ }
+ }
+}
+
+// describe adds the subexpression that failed to an evaluation error.
+func describe(err error) string {
+ e, ok := err.(*eval.Error)
+ if !ok || e.Expr == nil {
+ return err.Error()
+ }
+ return fmt.Sprintf("%v\n in %s", err, sexpr.Print(e.Expr))
+}
diff --git a/eval/define.go b/eval/define.go
index 9bda6fcf026d40e8973f5c993a62068ad207c87f..821d7323e194e6712c6125f3c4c923e29da63797 100644
--- a/eval/define.go
+++ b/eval/define.go
@@ -63,6 +63,20 @@ func NewTopLevelIn(env *Env) *TopLevel {
return &TopLevel{scope: newScope(env)}
}
+// Names lists every name bound at the top level so far, builtins
+// included, innermost first, for tools such as a REPL's completion.
+func (t *TopLevel) Names() []sexpr.Symbol {
+ var names []sexpr.Symbol
+ seen := map[sexpr.Symbol]bool{}
+ for b := t.scope.env; b != nil; b = b.parent {
+ if !b.pending && !seen[b.name] {
+ seen[b.name] = true
+ names = append(names, b.name)
+ }
+ }
+ return names
+}
+
func (t *TopLevel) Eval(form sexpr.Value) (sexpr.Value, error) {
if items, ok := defineForm(form); ok {
v, err := t.scope.define(items)
diff --git a/eval/define_test.go b/eval/define_test.go
index 3e7b302e156af7ee8c74b913b1b368d5dd80ef36..27686566600afc8a9483e9004cd4edfb8344285d 100644
--- a/eval/define_test.go
+++ b/eval/define_test.go
@@ -1,6 +1,10 @@
package eval
-import "testing"
+import (
+ "testing"
+
+ "government/sexpr"
+)
func TestDefine(t *testing.T) {
runEvalCases(t, []evalCase{
@@ -94,3 +98,25 @@ {"(define sq (lambda (n) (* n n))) (define square sq) (square 1 2)", ErrArity, "sq takes 1 argument, got 2"},
{"((lambda (n) n))", ErrArity, "lambda takes 1 argument, got 0"},
})
}
+
+func TestTopLevelNames(t *testing.T) {
+ top := NewTopLevel()
+ before := len(top.Names())
+ for _, src := range []string{"(define sq (lambda (n) (* n n)))", "(define x 1)"} {
+ forms, _ := sexpr.ReadAll(src)
+ if _, err := top.Eval(forms[0]); err != nil {
+ t.Fatal(err)
+ }
+ }
+ names := top.Names()
+ if len(names) != before+2 || names[0] != "x" || names[1] != "sq" {
+ t.Errorf("names %v; want x, sq, then %d builtins", names, before)
+ }
+ has := map[sexpr.Symbol]bool{}
+ for _, n := range names {
+ has[n] = true
+ }
+ if !has["car"] || !has["+"] {
+ t.Errorf("builtins missing from %v", names)
+ }
+}
diff --git a/go.mod b/go.mod
index 0c97266440a3efc936f94533140772c6ba00a769..8c4783225acfe3eb89a920eda73569cdf985a760 100644
--- a/go.mod
+++ b/go.mod
@@ -3,8 +3,13 @@
go 1.24
require (
+ github.com/ergochat/readline v0.1.3
github.com/spf13/cobra v1.10.2
github.com/spf13/pflag v1.0.9
)
-require github.com/inconshreveable/mousetrap v1.1.0 // indirect
+require (
+ github.com/inconshreveable/mousetrap v1.1.0 // indirect
+ golang.org/x/sys v0.15.0 // indirect
+ golang.org/x/text v0.9.0 // indirect
+)
diff --git a/go.sum b/go.sum
index a6ee3e0fb39674f5ff47f1f0480a532bde4abf20..b0966f692b2656c8f54dd7fc673234773339612c 100644
--- a/go.sum
+++ b/go.sum
@@ -1,4 +1,6 @@
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
+github.com/ergochat/readline v0.1.3 h1:/DytGTmwdUJcLAe3k3VJgowh5vNnsdifYT6uVaf4pSo=
+github.com/ergochat/readline v0.1.3/go.mod h1:o3ux9QLHLm77bq7hDB21UTm6HlV2++IPDMfIfKDuOgY=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
@@ -7,4 +9,8 @@ github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiTUUS4=
github.com/spf13/pflag v1.0.9 h1:9exaQaMOCwffKiiiYk6/BndUBv+iRViNW+4lEMi0PvY=
github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
+golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
+golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
+golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
+golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
diff --git a/sexpr/token.go b/sexpr/token.go
index 88fb8640f4f8b4b205e36ac8b9b4052cbb8ca806..6657193fa6af975ac586f1c4644edad118d4119d 100644
--- a/sexpr/token.go
+++ b/sexpr/token.go
@@ -153,7 +153,7 @@ return Token{Kind: TokComment, Text: l.src[begin:l.off], Pos: start}, nil
}
begin := l.off
- for l.off < len(l.src) && !isDelimiter(l.peek()) {
+ for l.off < len(l.src) && !IsDelimiter(l.peek()) {
l.advance()
}
text := l.src[begin:l.off]
@@ -164,7 +164,9 @@ }
return Token{Kind: TokAtom, Text: text, Pos: start}, nil
}
-func isDelimiter(r rune) bool {
+// IsDelimiter reports whether r ends an atom: whitespace, a paren, a
+// quote, a string's opening quote or a comment's semicolon.
+func IsDelimiter(r rune) bool {
switch r {
case '(', ')', '\'', '"', ';':
return true