Documentation
¶
Overview ¶
Package analysis finds what each code cell of a notebook defines and uses, and orders the cells by those dependencies.
A cell is Go with statements allowed at the top level, as in a REPL:
import "math"
func sq(x float64) float64 { return x * x }
y := sq(math.Pi)
y
Its top-level func, type and import declarations are declarations of the notebook; everything else is a statement of the cell, run in order. The names a cell defines at its top level (by :=, var, const, func or type) are the notebook's variables: every cell can read them, and exactly one cell may define each — the rule that makes the execution order a function of the code, not of the order cells were run in. Names starting with an underscore, and err, stay local to their cell (see Private). A cell may not assign to a variable another cell defines, for the same reason: the change would be invisible to the dependency graph. (Mutation through a method or a slice element cannot be seen statically and is not refused, as in marimo.)
The analysis is syntactic: it needs no type information and works on code that does not compile yet. A cell's final expression statement is its display value.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Cell ¶
type Cell struct {
Index int // position among the notebook's code cells
Source string // the cell's Go source
Chunks []Chunk
// Defines lists the notebook variables the cell defines: those of its
// declarations, then those of its statements, each in order of
// appearance. Funcs and Types are the subsets declared with func and
// type. Imports maps a package name to its import path.
Defines []string
Funcs []string
Types []string
Imports map[string]string
// Uses lists the free identifiers the cell reads, sorted: notebook
// variables, package names other cells import, builtins, and names
// that are not defined anywhere (the compiler reports those).
Uses []string
// OwnPackages lists, sorted, the names of the packages the cell imports
// itself and refers to.
OwnPackages []string
// Display is the cell's final expression statement, if any, and its
// byte range in Source.
Display ast.Expr
DisplayStart, DisplayEnd int
// Assigns lists the free identifiers the cell assigns to (=, op=, ++,
// --), with the offset of each assignment.
Assigns []Use
// Errors are syntax errors, with offsets in Source.
Errors []Error
}
Cell is the analysis of one code cell.
func AnalyzeCell ¶
AnalyzeCell analyses the source of code cell number index.
type Chunk ¶
Chunk is a run of a cell's text that is either one top-level declaration (func, type or import) or a sequence of statements.
type Graph ¶
type Graph struct {
Cells []*Cell
// Owner maps each notebook variable to the cell that defines it.
Owner map[string]int
// Deps[i] lists the cells cell i reads from, sorted; Dependents is the
// reverse relation.
Deps, Dependents [][]int
// Order is an execution order: every cell after the cells it reads, and
// otherwise in document order. It holds every cell when Errors is empty.
Order []int
// Errors lists syntax errors, names defined twice, assignments to
// another cell's variable, and dependency cycles.
Errors []Error
}
Graph is the dependency graph of a notebook's code cells.
func Analyze ¶
Analyze analyses the sources of a notebook's code cells, in document order, and builds their dependency graph.
func (*Graph) Downstream ¶
Downstream returns the cells that must re-run when the given cells change: those cells and everything that depends on them, in execution order.