Make great documentation goal 1.2, and plan the README's reorganisation and the nested-template gaps #33
@@ -110,5 +110,5 @@ In [docs/decisions.md](docs/decisions.md):
|
||||
- The Swedish ids draw Skatteverket's test series
|
||||
- The US given names come from a mirror of the SSA file
|
||||
- `List` advertises direct descents only
|
||||
- Each entry point to `drawCheck.hasRead` says what its caller gets
|
||||
- Each entry point to `readFence.hasRead` says what its caller gets
|
||||
- The four path walks are four loops
|
||||
|
||||
@@ -886,7 +886,8 @@ format.go the {token} grammar: scanning, tokens, operands, validation, com
|
||||
hold.go the hold: one draw per held name, and reading a field through it
|
||||
holdfence.go the load fences over the hold: the routes and spellings that read one held name two ways
|
||||
holdset.go one reference draw per render and group: the hold set a render reads its paths through
|
||||
drawfence.go the load fences over reference draws: a drawGroup's, a render's, a record's columns and a table family's, and the walk over what a render reads
|
||||
drawfence.go the load fences over reference draws: a drawGroup's, a render's, a record's columns and a table family's
|
||||
readsurvey.go the load-time survey of what a render reads, and what a path reads of a table family, for the draw fences
|
||||
pins.go the pin set: the table rows a render fixes, which the draw fences replay
|
||||
reference.go reference sigils, and binding references across the tree
|
||||
graph.go the node graph: tree walks, containment and render edges, and the cycle and repeat fences
|
||||
|
||||
@@ -246,7 +246,7 @@ func checkRenders(s nodeScope) error {
|
||||
if err := s(heldCheck); err != nil {
|
||||
return err
|
||||
}
|
||||
fence := &drawCheck{}
|
||||
fence := &readFence{}
|
||||
refs := false
|
||||
if err := s(func(path string, n node) error {
|
||||
if t, ok := n.(*template); ok && len(t.link.refs) > 0 {
|
||||
|
||||
@@ -34,7 +34,7 @@ package fejkdata
|
||||
// `table.parentT`, named by the root that chain ends at: `table.familyRoot`.
|
||||
// - whole — a read of a table with no selector and no descent, {/city}, landing on
|
||||
// the `table` itself: it draws a row apart from every pin, so the fences keep the rows
|
||||
// its cells stand in as `drawAt.wholePins`, beside `drawAt.wholeTable`.
|
||||
// its cells stand in as `surveyAt.wholePins`, beside `surveyAt.wholeTable`.
|
||||
// - fence — a load-time check, so rendering a compiled tree cannot fail: `New`
|
||||
// runs them over the data set, `NewTemplate` and `FakeStruct` over what those
|
||||
// compile. The draw fences are `heldCheck`, `drawCheck` and `checkFamilies`.
|
||||
// compile. The draw fences are `heldCheck`, `readFence` and `checkFamilies`.
|
||||
|
||||
+1
-1
@@ -668,7 +668,7 @@ import drops them by name rather than by a rank a regeneration would move.
|
||||
the set of every descent through a chain of five tables is every subsequence of it, and
|
||||
the direct chain is the one a reader can predict from the tables' parents.
|
||||
|
||||
## Each entry point to `drawCheck.hasRead` says what its caller gets
|
||||
## Each entry point to `readFence.hasRead` says what its caller gets
|
||||
|
||||
2026-09-25, larv-review; approved 2026-09-25 by lilleman. Goal 2: a reader lands on
|
||||
`readsPath` or `splitsDraws` from its caller, so each doc states where that walk stops,
|
||||
|
||||
+13
-248
@@ -36,11 +36,11 @@ func checkNestedDrawGroup(fields map[string]node, group string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// drawCheck fences each template of a scope as a render of its own, remembering which nodes read a
|
||||
// readFence fences each template of a scope as a render of its own, remembering which nodes read a
|
||||
// reference path.
|
||||
// docs/decisions.md#a-render-shares-one-reference-draw-per-category-per-group
|
||||
// docs/decisions.md#the-expansion-hold-and-the-renders-draws-are-two-fences
|
||||
type drawCheck struct {
|
||||
type readFence struct {
|
||||
memo map[hasReadMemo]bool
|
||||
}
|
||||
|
||||
@@ -51,14 +51,14 @@ type hasReadMemo struct {
|
||||
|
||||
// checkDrawGroup refuses a draw group that splits nothing: one whose render reads every reference
|
||||
// path inside a repeat or a nested draw group, which draw apart from it whatever it names.
|
||||
func (c *drawCheck) checkDrawGroup(path string, n node) error {
|
||||
func (c *readFence) checkDrawGroup(path string, n node) error {
|
||||
if t, ok := n.(*template); ok && t.drawGroup != "" && !c.splitsDraws(t) {
|
||||
return fmt.Errorf("%s: drawGroup %q splits nothing, since nothing it renders reads a reference path outside a repeat or a nested drawGroup; drop it", path, t.drawGroup)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *drawCheck) checkDraws(path string, n node) error {
|
||||
func (c *readFence) checkDraws(path string, n node) error {
|
||||
t, ok := n.(*template)
|
||||
if !ok {
|
||||
return nil
|
||||
@@ -69,7 +69,7 @@ func (c *drawCheck) checkDraws(path string, n node) error {
|
||||
if !c.readsPath(t) {
|
||||
return nil
|
||||
}
|
||||
if err := renderDraws(t).check(); err != nil {
|
||||
if err := surveyRender(t).check(); err != nil {
|
||||
return fmt.Errorf("%s: %w", path, err)
|
||||
}
|
||||
return nil
|
||||
@@ -113,18 +113,10 @@ func checkOwnFamily(t *template) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func renderDraws(t *template) *drawWalk {
|
||||
w := newDrawWalk()
|
||||
for _, e := range renderEdges(t) {
|
||||
w.edge(t, e, drawAt{group: t.link.drawGroupKey, route: drawRoute{e.reached(), e.label}})
|
||||
}
|
||||
return w
|
||||
}
|
||||
|
||||
// checkRecordDraws fences the columns of a record — a template compiled at the top without a
|
||||
// repeat — so a load proves the record view of it as well as the string view.
|
||||
// docs/decisions.md#a-category-never-references-itself-and-a-records-fences-run-at-load
|
||||
func (c *drawCheck) checkRecordDraws(path string, n node) error {
|
||||
func (c *readFence) checkRecordDraws(path string, n node) error {
|
||||
t, ok := n.(*template)
|
||||
if !ok || !t.isRecord {
|
||||
return nil
|
||||
@@ -146,15 +138,15 @@ func (c *drawCheck) checkRecordDraws(path string, n node) error {
|
||||
|
||||
// readsPath reports whether rendering n reads a reference path, short of a repeat, which renders
|
||||
// over draws of its own.
|
||||
func (c *drawCheck) readsPath(n node) bool { return c.hasRead(n, false) }
|
||||
func (c *readFence) readsPath(n node) bool { return c.hasRead(n, false) }
|
||||
|
||||
// splitsDraws reports whether rendering n reads a reference path that n's own draw group answers
|
||||
// for: one outside a repeat and outside a nested draw group, which hold their own draws.
|
||||
func (c *drawCheck) splitsDraws(n node) bool { return c.hasRead(n, true) }
|
||||
func (c *readFence) splitsDraws(n node) bool { return c.hasRead(n, true) }
|
||||
|
||||
// hasRead walks what rendering n renders for a reference path, stopping at a repeat — and at a nested
|
||||
// draw group when stopAtGroup — since each holds draws of its own.
|
||||
func (c *drawCheck) hasRead(n node, stopAtGroup bool) bool {
|
||||
func (c *readFence) hasRead(n node, stopAtGroup bool) bool {
|
||||
k := hasReadMemo{n, stopAtGroup}
|
||||
if r, done := c.memo[k]; done {
|
||||
return r
|
||||
@@ -203,152 +195,12 @@ func readsTable(n node, a arm) bool {
|
||||
}
|
||||
|
||||
// checkColumnDraws fences a record's columns as one render.
|
||||
func checkColumnDraws(t *template, columns []string) error { return columnDraws(t, columns).check() }
|
||||
|
||||
func columnDraws(t *template, columns []string) *drawWalk {
|
||||
w := newDrawWalk()
|
||||
for _, name := range columns {
|
||||
w.walk(t.fields[name], drawAt{group: t.link.drawGroupKey, route: drawRoute{spelling: fmt.Sprintf("column %q", name)}})
|
||||
}
|
||||
return w
|
||||
}
|
||||
|
||||
// drawWalk gathers the references one render reads, by draw group, for check to compare.
|
||||
type drawWalk struct {
|
||||
reads []pathRead
|
||||
read map[readKey]bool
|
||||
seen map[nodeVisit]bool
|
||||
pinIDs map[pinsLink]pinsID
|
||||
}
|
||||
|
||||
// drawAt is where a walk stands: the draw group it draws in, how the render's root reached it, the
|
||||
// table rows it pinned, the table a whole read draws a row of, and the rows of whole draws whose
|
||||
// cells the walk is in.
|
||||
type drawAt struct {
|
||||
group string
|
||||
route drawRoute
|
||||
pins pinSet
|
||||
wholeTable *table
|
||||
wholePins pinSet
|
||||
}
|
||||
|
||||
// drawRoute is how a render reaches a draw: as its author spells it, and the root edge's label.
|
||||
type drawRoute struct{ spelling, label string }
|
||||
|
||||
// pathRead is one reference a render reads: a path, or a bare reference with no tail.
|
||||
type pathRead struct {
|
||||
at drawAt
|
||||
a arm
|
||||
tr *tableRead // set where the reference names a table
|
||||
}
|
||||
|
||||
type nodeVisit struct {
|
||||
n node
|
||||
group string
|
||||
wholeTable *table
|
||||
pins pinsID
|
||||
wholePins pinsID
|
||||
}
|
||||
|
||||
type readKey struct {
|
||||
group string
|
||||
path string
|
||||
pins pinsID
|
||||
wholePins pinsID
|
||||
}
|
||||
|
||||
// pinsID names a pin set within one walk: two sets pinning the same rows share one.
|
||||
type pinsID int
|
||||
|
||||
// pinsLink is a set's last pin, in path order, and the id of the set before it.
|
||||
type pinsLink struct {
|
||||
prev pinsID
|
||||
pin tablePin
|
||||
}
|
||||
|
||||
func newDrawWalk() *drawWalk {
|
||||
return &drawWalk{read: map[readKey]bool{}, seen: map[nodeVisit]bool{}, pinIDs: map[pinsLink]pinsID{}}
|
||||
}
|
||||
|
||||
func (w *drawWalk) pinsID(p *pinSet) pinsID {
|
||||
var pins []tablePin
|
||||
p.each(func(t *table, r int) { pins = append(pins, tablePin{t, r}) })
|
||||
sort.Slice(pins, func(i, j int) bool { return pins[i].t.path < pins[j].t.path })
|
||||
id := pinsID(0)
|
||||
for _, q := range pins {
|
||||
l := pinsLink{id, q}
|
||||
next, ok := w.pinIDs[l]
|
||||
if !ok {
|
||||
next = pinsID(len(w.pinIDs) + 1)
|
||||
w.pinIDs[l] = next
|
||||
}
|
||||
id = next
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
// walk follows what rendering n renders. A repeat renders over draws of its own, so the walk stops
|
||||
// there. A cell whose row the pins keep out has no case: a pinned table renders its pinned row
|
||||
// alone and an unpinned one draws a row inside its nearest pinned ancestor's, so that cell never
|
||||
// renders on this route.
|
||||
func (w *drawWalk) walk(n node, at drawAt) {
|
||||
v := nodeVisit{n, at.group, at.wholeTable, w.pinsID(&at.pins), w.pinsID(&at.wholePins)}
|
||||
if w.seen[v] {
|
||||
return
|
||||
}
|
||||
w.seen[v] = true
|
||||
if repeats(n) {
|
||||
return
|
||||
}
|
||||
if t, isTemplate := n.(*template); isTemplate && t.link.drawGroupKey != "" {
|
||||
at.group = t.link.drawGroupKey
|
||||
}
|
||||
if t, isTable := n.(*table); isTable {
|
||||
at.wholeTable = t
|
||||
}
|
||||
for _, e := range renderEdges(n) {
|
||||
to, isTemplate := e.to.(*template)
|
||||
cell := cellSite{}
|
||||
if isTemplate {
|
||||
cell = to.cell
|
||||
}
|
||||
switch {
|
||||
case cell.table == nil:
|
||||
w.edge(n, e, at)
|
||||
case cell.table == at.wholeTable:
|
||||
in := at
|
||||
in.wholePins = at.wholePins.clone()
|
||||
in.wholePins.add(cell.table, cell.row)
|
||||
w.edge(n, e, in)
|
||||
case at.pins.clash(cell.table, cell.row) == nil:
|
||||
in := at
|
||||
in.pins = at.pins.entered(cell.table, cell.row)
|
||||
w.edge(n, e, in)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// edge records the reference an edge reads, then walks on with every row the read
|
||||
// pins entered, so a selected row renders only its own cells.
|
||||
func (w *drawWalk) edge(from node, e renderEdge, at drawAt) {
|
||||
if e.readsRef() {
|
||||
a := e.read
|
||||
tr := tableReadOf(from.(*template).head(a.key), a, e.to)
|
||||
if k := (readKey{at.group, a.path, w.pinsID(&at.pins), w.pinsID(&at.wholePins)}); !w.read[k] {
|
||||
w.read[k] = true
|
||||
w.reads = append(w.reads, pathRead{at, a, tr})
|
||||
}
|
||||
if tr != nil {
|
||||
tr.pins.each(func(t *table, r int) { at.pins = at.pins.entered(t, r) })
|
||||
}
|
||||
}
|
||||
w.walk(e.to, at)
|
||||
}
|
||||
func checkColumnDraws(t *template, columns []string) error { return surveyColumns(t, columns).check() }
|
||||
|
||||
// check refuses what one draw per reference path cannot answer for: a read of a level beside a path
|
||||
// another read takes into it, and two reads of one table family that select different rows. Reads
|
||||
// are compared in path order, so which pair is reported does not vary.
|
||||
func (w *drawWalk) check() error {
|
||||
func (w *readSurvey) check() error {
|
||||
sort.SliceStable(w.reads, func(i, j int) bool {
|
||||
if w.reads[i].at.group != w.reads[j].at.group {
|
||||
return w.reads[i].at.group < w.reads[j].at.group
|
||||
@@ -447,97 +299,10 @@ func conflict(r pathRead, err error) error {
|
||||
// alternatives reports whether two reads never render together: one read renders one row of a
|
||||
// table, so reads under different rows the walks pinned, or under different rows of one whole
|
||||
// draw, never meet.
|
||||
func alternatives(a, b drawAt) bool {
|
||||
func alternatives(a, b surveyAt) bool {
|
||||
return a.pins.differs(&b.pins) || a.wholePins.differs(&b.wholePins)
|
||||
}
|
||||
|
||||
func overlapError(route drawRoute, ref string, into pathRead) error {
|
||||
func overlapError(route readRoute, ref string, into pathRead) error {
|
||||
return fmt.Errorf("%s renders a level that %s reads a path into; name the fields you want instead, or draw them apart with a drawGroup", route.spelled(ref), into.at.route.spelled(into.a.spelling))
|
||||
}
|
||||
|
||||
// spelled names the route, and the reference it reaches a draw by where its root edge is not that
|
||||
// reference.
|
||||
func (r drawRoute) spelled(ref string) string {
|
||||
if ref == "" || ref == r.label {
|
||||
return r.spelling
|
||||
}
|
||||
return fmt.Sprintf("%s with {%s}", r.spelling, ref)
|
||||
}
|
||||
|
||||
// tableRead is what a reference path reads of a table family: the table its head
|
||||
// names, the rows its selectors pin, the tables it draws — those it walks with no
|
||||
// row pinned, and their unpinned ancestors — each selector's spelling, and whether
|
||||
// it lands on a `tableRow`.
|
||||
type tableRead struct {
|
||||
headTable *table
|
||||
pins pinSet
|
||||
drawn map[*table]bool
|
||||
sels []tableSel
|
||||
landsRow bool
|
||||
}
|
||||
|
||||
// tableSel is one selector on the way: the table it selects a row of, and the path
|
||||
// as written up to and including it.
|
||||
type tableSel struct {
|
||||
t *table
|
||||
spelling string
|
||||
}
|
||||
|
||||
// tableReadOf replays a reference path through pathProbe, so two paths pinning one
|
||||
// row by different routes compare equal. checkPath proved
|
||||
// each selector names a row.
|
||||
func tableReadOf(head node, a arm, leaf node) *tableRead {
|
||||
t, isTable := head.(*table)
|
||||
if !isTable {
|
||||
return nil
|
||||
}
|
||||
probe := &pathProbe{drawn: map[*table]bool{}}
|
||||
_, _ = probe.walk(t, a.tail)
|
||||
tr := &tableRead{headTable: t, pins: probe.pins, drawn: probe.drawn}
|
||||
written := a.spelling[:len(a.spelling)-len(joinSegments(a.tail))]
|
||||
cur := t
|
||||
for i, seg := range a.tail {
|
||||
switch d := cur.descendant(seg); {
|
||||
case isSelector(seg):
|
||||
tr.sels = append(tr.sels, tableSel{cur, written + joinSegments(a.tail[:i+1])})
|
||||
case d != nil:
|
||||
cur = d
|
||||
}
|
||||
}
|
||||
_, tr.landsRow = leaf.(*tableRow)
|
||||
return tr
|
||||
}
|
||||
|
||||
// selected is the selector in r on t, or on the nearest ancestor of t it selects.
|
||||
func (r *tableRead) selected(t *table) (tableSel, bool) {
|
||||
for ; t != nil; t = t.parentT {
|
||||
for _, s := range r.sels {
|
||||
if s.t == t {
|
||||
return s, true
|
||||
}
|
||||
}
|
||||
}
|
||||
return tableSel{}, false
|
||||
}
|
||||
|
||||
// drawnOf is a table the read draws that pins holds a row of, if any.
|
||||
func (r *tableRead) drawnOf(pins *pinSet) *table {
|
||||
var found *table
|
||||
pins.each(func(t *table, _ int) {
|
||||
if found == nil && r.drawn[t] {
|
||||
found = t
|
||||
}
|
||||
})
|
||||
return found
|
||||
}
|
||||
|
||||
// replay pins the read's rows into d, where they agree with the rows pinned before.
|
||||
func (r *tableRead) replay(d *pinSet) error {
|
||||
var err error
|
||||
r.pins.each(func(t *table, row int) {
|
||||
if err == nil {
|
||||
err = d.pinRow(t, row)
|
||||
}
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
+8
-8
@@ -63,7 +63,7 @@ func TestEveryReadARenderMakesIsGathered(t *testing.T) {
|
||||
roots, tables := fenceRoots(t, f)
|
||||
traced := 0
|
||||
for _, root := range roots {
|
||||
traced += wantGathered(t, tables, root.label, renderDraws(root.t).reads, func(trace renderTrace) { renderRoot(f.rand, root.t, trace) })
|
||||
traced += wantGathered(t, tables, root.label, surveyRender(root.t).reads, func(trace renderTrace) { renderRoot(f.rand, root.t, trace) })
|
||||
if !root.t.isRecord || len(root.t.fields) == 0 {
|
||||
continue
|
||||
}
|
||||
@@ -71,7 +71,7 @@ func TestEveryReadARenderMakesIsGathered(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("%s: recordOf = %v", root.label, err)
|
||||
}
|
||||
traced += wantGathered(t, tables, root.label+" as a record", columnDraws(root.t, sortedNames(root.t.fields)).reads, func(trace renderTrace) { renderRecordRoot(f.rand, root.t, columns, trace) })
|
||||
traced += wantGathered(t, tables, root.label+" as a record", surveyColumns(root.t, sortedNames(root.t.fields)).reads, func(trace renderTrace) { renderRecordRoot(f.rand, root.t, columns, trace) })
|
||||
}
|
||||
if traced == 0 {
|
||||
t.Error("no render traced a reference read, so the comparison proved nothing")
|
||||
@@ -90,7 +90,7 @@ func wantGathered(t *testing.T, tables map[string]*table, label string, gathered
|
||||
if !isRef(a.key) {
|
||||
return
|
||||
}
|
||||
r := pathRead{at: drawAt{group: group}, a: a}
|
||||
r := pathRead{at: surveyAt{group: group}, a: a}
|
||||
if table != "" {
|
||||
r.at.pins.add(tables[table], row)
|
||||
}
|
||||
@@ -104,7 +104,7 @@ func wantGathered(t *testing.T, tables map[string]*table, label string, gathered
|
||||
}
|
||||
for _, r := range reads {
|
||||
if !gathers(gathered, r) {
|
||||
t.Errorf("%s: the render read {%s} in draw group %q from %s, and drawWalk gathered no such read", label, r.a.spelling, r.at.group, spellPins(&r.at.pins))
|
||||
t.Errorf("%s: the render read {%s} in draw group %q from %s, and readSurvey gathered no such read", label, r.a.spelling, r.at.group, spellPins(&r.at.pins))
|
||||
}
|
||||
}
|
||||
return len(reads)
|
||||
@@ -171,9 +171,9 @@ func TestReplayPairsFindsWhatAWholeReplayFinds(t *testing.T) {
|
||||
})
|
||||
compared := 0
|
||||
for _, root := range roots {
|
||||
compared += wantPairsFindAll(t, root.label, renderDraws(root.t).reads)
|
||||
compared += wantPairsFindAll(t, root.label, surveyRender(root.t).reads)
|
||||
if root.t.isRecord && len(root.t.fields) > 0 {
|
||||
compared += wantPairsFindAll(t, root.label+" as a record", columnDraws(root.t, sortedNames(root.t.fields)).reads)
|
||||
compared += wantPairsFindAll(t, root.label+" as a record", surveyColumns(root.t, sortedNames(root.t.fields)).reads)
|
||||
}
|
||||
}
|
||||
if compared == 0 {
|
||||
@@ -201,7 +201,7 @@ func TestReplayPairsFindsWhatAWholeReplayOfSelectedRowsFinds(t *testing.T) {
|
||||
if !isTemplate {
|
||||
t.Fatalf("NewTemplate(%s) compiled to %T", tb.selectorSpelling(r), inline.n)
|
||||
}
|
||||
reads = append(reads, renderDraws(root).reads...)
|
||||
reads = append(reads, surveyRender(root).reads...)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
@@ -300,7 +300,7 @@ func TestDrawWalkWalksACellByEveryRoute(t *testing.T) {
|
||||
t.Fatalf("r is a %T, want a template", g.children["r"])
|
||||
}
|
||||
var pinned, whole bool
|
||||
for _, read := range renderDraws(r).reads {
|
||||
for _, read := range surveyRender(r).reads {
|
||||
if read.a.path != "/z" {
|
||||
continue
|
||||
}
|
||||
|
||||
+1
-1
@@ -18,7 +18,7 @@ func heldCheck(path string, n node) error {
|
||||
readers := pathKeyReaders(t.compiled.ops, t.compiled.pathKeys)
|
||||
for _, name := range heldNames(t) {
|
||||
if _, isPath := t.compiled.pathKeys[name]; isPath && isRef(name) {
|
||||
continue // held for the render: drawCheck compares every read of it, by path, across the render and its groups
|
||||
continue // held for the render: readFence compares every read of it, by path, across the render and its groups
|
||||
}
|
||||
if err := checkNameHeld(t, name, readers); err != nil {
|
||||
return fmt.Errorf("%s: %w", path, err)
|
||||
|
||||
+241
@@ -0,0 +1,241 @@
|
||||
package fejkdata
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
)
|
||||
|
||||
// readSurvey gathers, at load, the references one render reads, by draw group, for check to compare.
|
||||
type readSurvey struct {
|
||||
reads []pathRead
|
||||
read map[readKey]bool
|
||||
seen map[nodeVisit]bool
|
||||
pinIDs map[pinsLink]pinsID
|
||||
}
|
||||
|
||||
// surveyAt is where a survey stands: the draw group it draws in, how the render's root reached it, the
|
||||
// table rows it pinned, the table a whole read draws a row of, and the rows of whole draws whose
|
||||
// cells the walk is in.
|
||||
type surveyAt struct {
|
||||
group string
|
||||
route readRoute
|
||||
pins pinSet
|
||||
wholeTable *table
|
||||
wholePins pinSet
|
||||
}
|
||||
|
||||
// readRoute is how a render reaches a read: as its author spells it, and the root edge's label.
|
||||
type readRoute struct{ spelling, label string }
|
||||
|
||||
// pathRead is one reference a render reads: a path, or a bare reference with no tail.
|
||||
type pathRead struct {
|
||||
at surveyAt
|
||||
a arm
|
||||
tr *tableRead // set where the reference names a table
|
||||
}
|
||||
|
||||
type nodeVisit struct {
|
||||
n node
|
||||
group string
|
||||
wholeTable *table
|
||||
pins pinsID
|
||||
wholePins pinsID
|
||||
}
|
||||
|
||||
type readKey struct {
|
||||
group string
|
||||
path string
|
||||
pins pinsID
|
||||
wholePins pinsID
|
||||
}
|
||||
|
||||
// pinsID names a pin set within one walk: two sets pinning the same rows share one.
|
||||
type pinsID int
|
||||
|
||||
// pinsLink is a set's last pin, in path order, and the id of the set before it.
|
||||
type pinsLink struct {
|
||||
prev pinsID
|
||||
pin tablePin
|
||||
}
|
||||
|
||||
func newReadSurvey() *readSurvey {
|
||||
return &readSurvey{read: map[readKey]bool{}, seen: map[nodeVisit]bool{}, pinIDs: map[pinsLink]pinsID{}}
|
||||
}
|
||||
|
||||
func surveyRender(t *template) *readSurvey {
|
||||
w := newReadSurvey()
|
||||
for _, e := range renderEdges(t) {
|
||||
w.edge(t, e, surveyAt{group: t.link.drawGroupKey, route: readRoute{e.reached(), e.label}})
|
||||
}
|
||||
return w
|
||||
}
|
||||
|
||||
func surveyColumns(t *template, columns []string) *readSurvey {
|
||||
w := newReadSurvey()
|
||||
for _, name := range columns {
|
||||
w.walk(t.fields[name], surveyAt{group: t.link.drawGroupKey, route: readRoute{spelling: fmt.Sprintf("column %q", name)}})
|
||||
}
|
||||
return w
|
||||
}
|
||||
|
||||
func (w *readSurvey) pinsID(p *pinSet) pinsID {
|
||||
var pins []tablePin
|
||||
p.each(func(t *table, r int) { pins = append(pins, tablePin{t, r}) })
|
||||
sort.Slice(pins, func(i, j int) bool { return pins[i].t.path < pins[j].t.path })
|
||||
id := pinsID(0)
|
||||
for _, q := range pins {
|
||||
l := pinsLink{id, q}
|
||||
next, ok := w.pinIDs[l]
|
||||
if !ok {
|
||||
next = pinsID(len(w.pinIDs) + 1)
|
||||
w.pinIDs[l] = next
|
||||
}
|
||||
id = next
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
// walk follows what rendering n renders. A repeat renders over draws of its own, so the walk stops
|
||||
// there. A cell whose row the pins keep out has no case: a pinned table renders its pinned row
|
||||
// alone and an unpinned one draws a row inside its nearest pinned ancestor's, so that cell never
|
||||
// renders on this route.
|
||||
func (w *readSurvey) walk(n node, at surveyAt) {
|
||||
v := nodeVisit{n, at.group, at.wholeTable, w.pinsID(&at.pins), w.pinsID(&at.wholePins)}
|
||||
if w.seen[v] {
|
||||
return
|
||||
}
|
||||
w.seen[v] = true
|
||||
if repeats(n) {
|
||||
return
|
||||
}
|
||||
if t, isTemplate := n.(*template); isTemplate && t.link.drawGroupKey != "" {
|
||||
at.group = t.link.drawGroupKey
|
||||
}
|
||||
if t, isTable := n.(*table); isTable {
|
||||
at.wholeTable = t
|
||||
}
|
||||
for _, e := range renderEdges(n) {
|
||||
to, isTemplate := e.to.(*template)
|
||||
cell := cellSite{}
|
||||
if isTemplate {
|
||||
cell = to.cell
|
||||
}
|
||||
switch {
|
||||
case cell.table == nil:
|
||||
w.edge(n, e, at)
|
||||
case cell.table == at.wholeTable:
|
||||
in := at
|
||||
in.wholePins = at.wholePins.clone()
|
||||
in.wholePins.add(cell.table, cell.row)
|
||||
w.edge(n, e, in)
|
||||
case at.pins.clash(cell.table, cell.row) == nil:
|
||||
in := at
|
||||
in.pins = at.pins.entered(cell.table, cell.row)
|
||||
w.edge(n, e, in)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// edge records the reference an edge reads, then walks on with every row the read
|
||||
// pins entered, so a selected row renders only its own cells.
|
||||
func (w *readSurvey) edge(from node, e renderEdge, at surveyAt) {
|
||||
if e.readsRef() {
|
||||
a := e.read
|
||||
tr := tableReadOf(from.(*template).head(a.key), a, e.to)
|
||||
if k := (readKey{at.group, a.path, w.pinsID(&at.pins), w.pinsID(&at.wholePins)}); !w.read[k] {
|
||||
w.read[k] = true
|
||||
w.reads = append(w.reads, pathRead{at, a, tr})
|
||||
}
|
||||
if tr != nil {
|
||||
tr.pins.each(func(t *table, r int) { at.pins = at.pins.entered(t, r) })
|
||||
}
|
||||
}
|
||||
w.walk(e.to, at)
|
||||
}
|
||||
|
||||
// spelled names the route, and the reference it reaches a draw by where its root edge is not that
|
||||
// reference.
|
||||
func (r readRoute) spelled(ref string) string {
|
||||
if ref == "" || ref == r.label {
|
||||
return r.spelling
|
||||
}
|
||||
return fmt.Sprintf("%s with {%s}", r.spelling, ref)
|
||||
}
|
||||
|
||||
// tableRead is what a reference path reads of a table family: the table its head
|
||||
// names, the rows its selectors pin, the tables it draws — those it walks with no
|
||||
// row pinned, and their unpinned ancestors — each selector's spelling, and whether
|
||||
// it lands on a `tableRow`.
|
||||
type tableRead struct {
|
||||
headTable *table
|
||||
pins pinSet
|
||||
drawn map[*table]bool
|
||||
sels []tableSel
|
||||
landsRow bool
|
||||
}
|
||||
|
||||
// tableSel is one selector on the way: the table it selects a row of, and the path
|
||||
// as written up to and including it.
|
||||
type tableSel struct {
|
||||
t *table
|
||||
spelling string
|
||||
}
|
||||
|
||||
// tableReadOf replays a reference path through pathProbe, so two paths pinning one
|
||||
// row by different routes compare equal. checkPath proved
|
||||
// each selector names a row.
|
||||
func tableReadOf(head node, a arm, leaf node) *tableRead {
|
||||
t, isTable := head.(*table)
|
||||
if !isTable {
|
||||
return nil
|
||||
}
|
||||
probe := &pathProbe{drawn: map[*table]bool{}}
|
||||
_, _ = probe.walk(t, a.tail)
|
||||
tr := &tableRead{headTable: t, pins: probe.pins, drawn: probe.drawn}
|
||||
written := a.spelling[:len(a.spelling)-len(joinSegments(a.tail))]
|
||||
cur := t
|
||||
for i, seg := range a.tail {
|
||||
switch d := cur.descendant(seg); {
|
||||
case isSelector(seg):
|
||||
tr.sels = append(tr.sels, tableSel{cur, written + joinSegments(a.tail[:i+1])})
|
||||
case d != nil:
|
||||
cur = d
|
||||
}
|
||||
}
|
||||
_, tr.landsRow = leaf.(*tableRow)
|
||||
return tr
|
||||
}
|
||||
|
||||
// selected is the selector in r on t, or on the nearest ancestor of t it selects.
|
||||
func (r *tableRead) selected(t *table) (tableSel, bool) {
|
||||
for ; t != nil; t = t.parentT {
|
||||
for _, s := range r.sels {
|
||||
if s.t == t {
|
||||
return s, true
|
||||
}
|
||||
}
|
||||
}
|
||||
return tableSel{}, false
|
||||
}
|
||||
|
||||
// drawnOf is a table the read draws that pins holds a row of, if any.
|
||||
func (r *tableRead) drawnOf(pins *pinSet) *table {
|
||||
var found *table
|
||||
pins.each(func(t *table, _ int) {
|
||||
if found == nil && r.drawn[t] {
|
||||
found = t
|
||||
}
|
||||
})
|
||||
return found
|
||||
}
|
||||
|
||||
// replay pins the read's rows into d, where they agree with the rows pinned before.
|
||||
func (r *tableRead) replay(d *pinSet) error {
|
||||
var err error
|
||||
r.pins.each(func(t *table, row int) {
|
||||
if err == nil {
|
||||
err = d.pinRow(t, row)
|
||||
}
|
||||
})
|
||||
return err
|
||||
}
|
||||
+4
-4
@@ -1000,7 +1000,7 @@ func TestEnteredRowsAgreeWithPinning(t *testing.T) {
|
||||
var none pinSet
|
||||
for _, entered := range tables {
|
||||
for row := 0; row < entered.rowCount(); row++ {
|
||||
whole := drawAt{wholeTable: entered}
|
||||
whole := surveyAt{wholeTable: entered}
|
||||
whole.wholePins.add(entered, row)
|
||||
s := none.entered(entered, row)
|
||||
for _, cand := range tables {
|
||||
@@ -1013,14 +1013,14 @@ func TestEnteredRowsAgreeWithPinning(t *testing.T) {
|
||||
for cr := 0; cr < cand.rowCount(); cr++ {
|
||||
beside := s.clone()
|
||||
refused := beside.pinRow(cand, cr) != nil
|
||||
candAt := drawAt{pins: none.entered(cand, cr)}
|
||||
if got := alternatives(drawAt{pins: s}, candAt); got != refused {
|
||||
candAt := surveyAt{pins: none.entered(cand, cr)}
|
||||
if got := alternatives(surveyAt{pins: s}, candAt); got != refused {
|
||||
t.Errorf("alternatives(%s, %s) = %v, but pinning both refuses = %v", entered.selectorSpelling(row), cand.selectorSpelling(cr), got, refused)
|
||||
}
|
||||
if alternatives(whole, candAt) || alternatives(candAt, whole) {
|
||||
t.Errorf("%s rendered whole is an alternative to %s pinned, though the whole draw ignores the pin", entered.selectorSpelling(row), cand.selectorSpelling(cr))
|
||||
}
|
||||
other := drawAt{wholeTable: cand}
|
||||
other := surveyAt{wholeTable: cand}
|
||||
other.wholePins.add(cand, cr)
|
||||
if want := cand == entered && cr != row; alternatives(whole, other) != want {
|
||||
t.Errorf("alternatives(%s, %s), both rendered whole, = %v, want %v", entered.selectorSpelling(row), cand.selectorSpelling(cr), !want, want)
|
||||
|
||||
@@ -26,10 +26,7 @@ least modify.
|
||||
|
||||
This round, in order:
|
||||
|
||||
- Rename the load-time `draw*` types (`drawWalk`, `drawAt`, `drawRoute`, `drawCheck`)
|
||||
to say they read and draw nothing, and move `drawWalk` and `tableRead` out of
|
||||
`drawfence.go`: a grep for "draw" lands in render and check code alike.
|
||||
- Spell `drawWalk.walk`'s dropped cell as a case of its switch: a clashing row's cell
|
||||
- Spell `readSurvey.walk`'s dropped cell as a case of its switch: a clashing row's cell
|
||||
falls out with no arm, which four seats named.
|
||||
- Define `descended` where `table.route` takes it: five seats reverse-engineered it from
|
||||
the `draw` expression and the four walks' loop condition.
|
||||
@@ -313,7 +310,7 @@ because pairing a street with its exact postnummer rewrites shipped rows.
|
||||
back to the clause it came from: the goal spells them out in prose while `todo.md`
|
||||
records `Navigation`, `Locality`, `Shape` and `Self-sufficiency`.
|
||||
- Delete `doc.go`'s "The draw fences are …" sentence: it names a function, a type and
|
||||
a helper reached only through `drawWalk.check`, and misses `checkDrawGroup`,
|
||||
a helper reached only through `readSurvey.check`, and misses `checkDrawGroup`,
|
||||
`checkOwnFamily` and `checkColumnDraws`; the README's Layout indexes the fences by file.
|
||||
- Cut the README's Layout block to the lines that say what a file name cannot:
|
||||
`calc.go`, `doc.go` and `cmd/fejkdata/` restate their names.
|
||||
|
||||
Reference in New Issue
Block a user