repl

package
v0.0.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 18, 2026 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ColorReset   = "\033[0m"
	ColorBold    = "\033[1m"
	ColorDim     = "\033[2m"
	ColorRed     = "\033[31m"
	ColorGreen   = "\033[32m"
	ColorYellow  = "\033[33m"
	ColorBlue    = "\033[34m"
	ColorMagenta = "\033[35m"
	ColorCyan    = "\033[36m"
	ColorGray    = "\033[90m"
)

Variables

View Source
var (
	TOKEN_EOF      int
	TOKEN_LPAREN   int
	TOKEN_RPAREN   int
	TOKEN_LBRACE   int
	TOKEN_RBRACE   int
	TOKEN_LBRACKET int
	TOKEN_RBRACKET int
	TOKEN_ASSIGN   int
	TOKEN_PLUS     int
	TOKEN_MINUS    int
	TOKEN_MULTIPLY int
	TOKEN_DIVIDE   int
	TOKEN_MODULO   int
	TOKEN_EQ       int
	TOKEN_NEQ      int
	TOKEN_LT       int
	TOKEN_GT       int
	TOKEN_LTE      int
	TOKEN_GTE      int
	TOKEN_AND      int
	TOKEN_OR       int
	TOKEN_BITAND   int
	TOKEN_BITOR    int
	TOKEN_BITXOR   int
	TOKEN_COMMA    int
	TOKEN_COLON    int
	TOKEN_DOT      int
	TOKEN_LET      int
	TOKEN_CONST    int
	TOKEN_RETURN   int
	TOKEN_IF       int
	TOKEN_ELSE     int
	TOKEN_FOR      int
	TOKEN_WHILE    int
	TOKEN_FUNCTION int
	TOKEN_TRY      int
	TOKEN_CATCH    int
	TOKEN_SWITCH   int
	TOKEN_CASE     int
	TOKEN_THROW    int
	TOKEN_IMPORT   int
	TOKEN_EXPORT   int
)
View Source
var BuiltinHelpTextFn func(name string) string

BuiltinHelpTextFn returns help text for a builtin.

View Source
var BuiltinNames func() map[string]struct{}

BuiltinNames returns all registered builtin names.

View Source
var DispatchHotReloadHooksFn func(path string)

DispatchHotReloadHooksFn fires hot-reload hooks for a path.

View Source
var EvalFn func(node any, env *object.Environment) object.Object

EvalFn evaluates an AST node in the given environment.

View Source
var FormatCallStackFn func(stack []object.CallFrame) string

FormatCallStackFn formats call-stack frames.

View Source
var HasBuiltinFn func(name string) bool

HasBuiltinFn returns true if a builtin with the given name exists.

View Source
var IsErrorFn func(obj object.Object) bool

IsErrorFn returns true if the object is an Error.

View Source
var LexerNextTokenFn func(lexer any) (tokenType int, literal string)

LexerNextTokenFn returns the next token from a lexer. Token is returned as (tokenType int, literal string).

View Source
var LineContextFn func(source string, line, column int) string

LineContextFn extracts a source-line context snippet.

View Source
var LoadConfigObjectFromPathFn func(path, format string) (object.Object, error)

LoadConfigObjectFromPathFn loads a config file.

View Source
var NewLexerFn func(input string) any

NewLexerFn creates a new lexer from source input.

View Source
var NewParserFn func(lexer any) any

NewParserFn creates a new parser from a lexer.

View Source
var ObjectErrorStringFn func(obj object.Object) string

ObjectErrorStringFn formats an error object as a string.

View Source
var ParseProgramFn func(parser any) (program any, errors []string)

ParseProgramFn parses and returns (program, errors).

View Source
var ResolveImportPathFn func(path string, env *object.Environment) (string, error)

ResolveImportPathFn resolves an import path to a file system path.

View Source
var RunNativeCommandLineFn func(env *object.Environment, command string, args []string) NativeCommandResult
View Source
var RunProgramSandboxedFn func(program any, env *object.Environment, policy *object.SecurityPolicy) object.Object

RunProgramSandboxedFn evaluates a program in the sandbox.

Functions

func AppendHistoryEntries

func AppendHistoryEntries(path string, entries []string) error

func ApplyReplConfig

func ApplyReplConfig(env *object.Environment, cfg *ReplConfig)

func ApplyReplProfile

func ApplyReplProfile(cfg *ReplConfig, profile string) error

func ColorEnabled

func ColorEnabled() bool

ColorEnabled checks whether the terminal supports colours.

func ColorizeInputLine

func ColorizeInputLine(line string) string

ColorizeInputLine applies syntax highlighting to a single input line.

func CurrentToken

func CurrentToken(buf []rune, cursor int) (prefix string, start int, end int, ok bool)

func EvalReplInput

func EvalReplInput(input string, env *object.Environment)

EvalReplInput evaluates a complete REPL input string.

func FindCompletions

func FindCompletions(prefix string, candidates []string) []string

func FormatObjectForDisplay

func FormatObjectForDisplay(obj object.Object) string

FormatObjectForDisplay returns a colourised representation of an object for interactive REPL display.

func FormatObjectForDisplayWithEnv

func FormatObjectForDisplayWithEnv(obj object.Object, env *object.Environment) string

func FormatObjectPlain

func FormatObjectPlain(obj object.Object) string

FormatObjectPlain produces a plain-text representation of an object, suitable for REPL output.

func FormatRuntimeErrorForDisplay

func FormatRuntimeErrorForDisplay(obj object.Object, source string) string

FormatRuntimeErrorForDisplay formats an error object with context for REPL display.

func HandleReplMetaCommand

func HandleReplMetaCommand(line string, editor *ReplEditor, env *object.Environment) bool

HandleReplMetaCommand processes REPL meta-commands (lines starting with ':' or '!'). Returns true if the line was handled.

func HistoryEntriesToPersist

func HistoryEntriesToPersist(history []string, from int) []string

func IsTerminal

func IsTerminal(f *os.File) bool

IsTerminal returns true if the given file is connected to a terminal.

func LoadHistoryEntries

func LoadHistoryEntries(path string) ([]string, error)

func LongestCommonPrefix

func LongestCommonPrefix(items []string) string

func LooksLikeDateString

func LooksLikeDateString(s string) bool

LooksLikeDateString checks if a string looks like a date literal.

func Paint

func Paint(s, code string) string

Paint wraps s in ANSI colour codes when colours are enabled.

func ParseHistoryData

func ParseHistoryData(data []byte) []string

func PrintWelcome

func PrintWelcome(env *object.Environment)

func ReplActiveCall

func ReplActiveCall(line string, cursor int) (name string, argIndex int, ok bool)

ReplActiveCall returns the function call enclosing cursor and the current zero-based argument index. It is intentionally lightweight so it can run on every keypress in the REPL.

func ReplBuiltinArgHint

func ReplBuiltinArgHint(detail string, argIndex int) string

func ReplBuiltinCallTip

func ReplBuiltinCallTip(name string, argIndex int) string

func ReplBuiltinDetail

func ReplBuiltinDetail(name string) string

func ReplCallTip

func ReplCallTip(line string, cursor int, env *object.Environment) string

func ReplCandidates

func ReplCandidates() []string

ReplCandidates returns completion candidates without an environment.

func ReplCandidatesForEnv

func ReplCandidatesForEnv(env *object.Environment) []string

ReplCandidatesForEnv returns completion candidates from builtins, keywords, and environment variables.

func ReplCommandDetail

func ReplCommandDetail(name string) string

func ReplCommandNames

func ReplCommandNames() []string

func ReplCommandsTable

func ReplCommandsTable(query string) string

func ReplCompactHint

func ReplCompactHint(text string, maxRunes int) string

func ReplConfigTable

func ReplConfigTable(env *object.Environment) string

func ReplDocText

func ReplDocText(name string, env *object.Environment) string

func ReplEvalSource

func ReplEvalSource(input string, env *object.Environment, sourcePath string, printResult bool)

ReplEvalSource evaluates source code and optionally prints the result.

func ReplExamplesText

func ReplExamplesText() string

func ReplFunctionCallTip

func ReplFunctionCallTip(name string, fn *object.Function, argIndex int) string

func ReplFunctionParamTip

func ReplFunctionParamTip(fn *object.Function, argIndex int) string

func ReplFunctionSignature

func ReplFunctionSignature(name string, fn *object.Function) string

func ReplHintLines

func ReplHintLines(text string, width int) []string

func ReplInstallDependency

func ReplInstallDependency(alias, source string, env *object.Environment) error

func ReplMemoryUsage

func ReplMemoryUsage() string

func ReplNeedsContinuation

func ReplNeedsContinuation(input string) bool

ReplNeedsContinuation returns true when the input looks incomplete and requires additional lines.

func ReplObjectFields

func ReplObjectFields(obj object.Object) []string

ReplObjectFields returns known fields for common object types.

func ReplObjectMethods

func ReplObjectMethods(obj object.Object) []string

ReplObjectMethods returns known methods for common object types.

func ReplPalette

func ReplPalette(query string, editor *ReplEditor, env *object.Environment) string

func ReplParseProgram

func ReplParseProgram(input string) (any, []string)

ReplParseProgram parses input and returns (program, errors).

func ReplPrintLine

func ReplPrintLine(s string)

func ReplResolvedPath

func ReplResolvedPath(path string, env *object.Environment) (string, error)

func ReplTipsText

func ReplTipsText() string

func ReplToolsText

func ReplToolsText() string

func RunReplBasic

func RunReplBasic(env *object.Environment)

RunReplBasic starts a simple line-based REPL without raw terminal input.

func RunReplInteractive

func RunReplInteractive(env *object.Environment) error

RunReplInteractive starts the interactive REPL with line editing.

func StyleContinuationPrompt

func StyleContinuationPrompt(p string) string

StyleContinuationPrompt styles the continuation prompt.

func StylePrompt

func StylePrompt(p string) string

StylePrompt styles the primary REPL prompt.

Types

type NativeCommandResult

type NativeCommandResult struct {
	Stdout    string
	Stderr    string
	ExitCode  int
	OK        bool
	TimedOut  bool
	Cancelled bool
	Truncated bool
	Error     string
}

type ReplCompletionContext

type ReplCompletionContext struct {
	Prefix   string
	BaseExpr string
	Start    int
	End      int
	Ok       bool
}

func CompletionContext

func CompletionContext(buf []rune, cursor int) ReplCompletionContext

type ReplConfig

type ReplConfig struct {
	ExecutionProfile       string
	ModuleDir              string
	StrictMode             bool
	ProtectHost            bool
	AllowEnvWrite          bool
	AllowedCapabilities    []string
	DeniedCapabilities     []string
	AllowedExecCommands    []string
	AllowedNativeModules   []string
	DeniedNativeModules    []string
	AllowedNetworkHosts    []string
	AllowedDBDrivers       []string
	AllowedDBDSNPatterns   []string
	AllowedFileReadPaths   []string
	AllowedFileWritePaths  []string
	MaxDepth               int
	MaxSteps               int64
	MaxHeapMB              int64
	TimeoutMS              int64
	MaxOutputBytes         int64
	MaxHTTPBodyBytes       int64
	MaxExecOutputBytes     int64
	RenderMode             string
	RenderTerminalProtocol string
	RenderMaxBytes         int64
	RenderAllowURLs        bool
	RenderAllowURLHosts    []string
}

func ReplConfigForEnv

func ReplConfigForEnv(env *object.Environment) *ReplConfig

type ReplEditor

type ReplEditor struct {
	In          *os.File
	Out         *os.File
	Fd          int
	Env         *object.Environment
	History     []string
	HistoryPos  int
	HistoryFile string
	HistoryBase int
	Candidates  []string
	Index       *tooling.WorkspaceIndex
	SourcePath  string
	Source      string
}

func (*ReplEditor) CompletionsForContext

func (e *ReplEditor) CompletionsForContext(ctx ReplCompletionContext) []string

func (*ReplEditor) InlineHint

func (e *ReplEditor) InlineHint(line string, cursor int) string

func (*ReplEditor) RecordInput

func (e *ReplEditor) RecordInput(input string)

func (*ReplEditor) ReverseHistorySearch

func (e *ReplEditor) ReverseHistorySearch(current []rune) ([]rune, int)

func (*ReplEditor) SuggestionLines

func (e *ReplEditor) SuggestionLines(ctx ReplCompletionContext, candidates []string, width int) []string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL