Share variable scope and the walk over a document's nodes

Flatten and Validate read nodes through one walk, and the parser, flattener,
validator and writer set variables through one type.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-01 18:20:35 +00:00
co-authored by Claude Sonnet 5.5
parent cc484ac3ef
commit 1bb842589e
5 changed files with 129 additions and 104 deletions
+17 -43
View File
@@ -73,8 +73,7 @@ type target struct {
// flattener reads a document's nodes in order.
type flattener struct {
flat *Flat
defines map[string]string
flat *Flat
// levels are the opcodes set at each level.
levels [levelRegion + 1]Opcodes
into *target
@@ -86,55 +85,30 @@ type flattener struct {
// new header clears the ones below it, so a <group> starts with no group
// opcodes. Comments are dropped. An include that wasn't read is skipped.
func (d *Document) Flatten() *Flat {
f := &flattener{
flat: &Flat{Control: Opcodes{}, Headers: map[string]int{}},
defines: map[string]string{},
}
f := &flattener{flat: &Flat{Control: Opcodes{}, Headers: map[string]int{}}}
for i := range f.levels {
f.levels[i] = Opcodes{}
}
f.file(d.Root)
walk(d.Root, defines{}, f)
f.closeRegion()
return f.flat
}
func (f *flattener) file(file *File) {
for _, n := range file.Nodes {
switch n.Kind {
case KindDefine:
f.defines[n.Name] = strings.TrimSpace(substitute(f.defines, n.Value))
case KindInclude:
if n.File != nil {
f.file(n.File)
}
case KindHeader:
f.header(n, file.Path)
case KindOpcode:
if !strings.Contains(n.Name+n.Value, "$") {
f.opcode(n.Name, n.Value)
continue
}
f.expand(n, file.Path, n.Name+"="+n.Value)
case KindText:
if strings.Contains(n.Raw, "$") {
f.expand(n, file.Path, n.Raw)
}
}
func (f *flattener) define(*File, *Node) {}
func (f *flattener) include(*File, *Node) {}
func (f *flattener) variables(*File, *Node) {}
func (f *flattener) text(*File, *Node, string) {}
func (f *flattener) header(file *File, n *Node, name string, variable bool) {
if variable {
// The header has no node of its own, so it takes the line of the node that
// made it.
n = &Node{Name: name, Line: n.Line}
}
f.startHeader(n, file.Path)
}
// expand reads again the text that a node makes once its variables are
// replaced, as a variable's value can hold opcodes or headers.
func (f *flattener) expand(n *Node, file, text string) {
for _, m := range lex(substitute(f.defines, text)) {
switch m.Kind {
case KindHeader:
f.header(&Node{Name: m.Name, Line: n.Line}, file)
case KindOpcode:
f.opcode(m.Name, m.Value)
}
}
}
func (f *flattener) opcode(_ *File, _ *Node, name, value string, _ bool) { f.setOpcode(name, value) }
// closeRegion adds the region being read, if any. A region takes the <control>
// settings made before it, which later <control> headers don't change.
@@ -152,7 +126,7 @@ func (f *flattener) closeRegion() {
f.flat.Regions = append(f.flat.Regions, FlatRegion{Opcodes: ops, File: f.into.file, Line: f.into.region.Line})
}
func (f *flattener) header(n *Node, file string) {
func (f *flattener) startHeader(n *Node, file string) {
f.closeRegion()
name := strings.ToLower(n.Name)
switch name {
@@ -182,7 +156,7 @@ func (f *flattener) header(n *Node, file string) {
}
}
func (f *flattener) opcode(name, value string) {
func (f *flattener) setOpcode(name, value string) {
if f.into == nil {
// Opcodes before any header apply to every region.
f.into = &target{ops: f.levels[levelGlobal]}