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:
+21
-54
@@ -13,9 +13,10 @@ import (
|
||||
// read. It reports what is wrong with the text and with values the format
|
||||
// describes, and never fails on an opcode it doesn't know.
|
||||
func (d *Document) Validate() []Problem {
|
||||
v := &validator{defines: map[string]string{}, known: map[string]known{}}
|
||||
defs := defines{}
|
||||
v := &validator{defs: defs, known: map[string]known{}}
|
||||
v.problems = append(v.problems, d.problems...)
|
||||
v.file(d.Root)
|
||||
walk(d.Root, defs, v)
|
||||
v.endHeader()
|
||||
return v.problems
|
||||
}
|
||||
@@ -34,7 +35,8 @@ type known struct {
|
||||
}
|
||||
|
||||
type validator struct {
|
||||
defines map[string]string
|
||||
// defs are the variables defined so far, which [walk] sets.
|
||||
defs defines
|
||||
// known holds the lookups made, as a document names the same few opcodes
|
||||
// many times.
|
||||
known map[string]known
|
||||
@@ -50,58 +52,25 @@ func (v *validator) add(file string, n *Node, sev Severity, code, msg string) {
|
||||
v.problems = append(v.problems, Problem{File: file, Line: n.Line, Col: n.Col, Severity: sev, Code: code, Msg: msg})
|
||||
}
|
||||
|
||||
func (v *validator) file(f *File) {
|
||||
for _, n := range f.Nodes {
|
||||
switch n.Kind {
|
||||
case KindDefine:
|
||||
v.undefined(f, n, n.Value)
|
||||
v.defines[n.Name] = strings.TrimSpace(substitute(v.defines, n.Value))
|
||||
case KindInclude:
|
||||
v.undefined(f, n, n.Value)
|
||||
if n.Resolved != "" && !fs.ValidPath(n.Resolved) {
|
||||
v.add(f.Path, n, SeverityWarning, "include.outside", fmt.Sprintf("%s is outside the folder of the main file", n.Resolved))
|
||||
}
|
||||
if n.File != nil {
|
||||
v.file(n.File)
|
||||
}
|
||||
case KindHeader:
|
||||
v.header(f, n, n.Name)
|
||||
case KindOpcode, KindText:
|
||||
if n.Kind == KindOpcode && !strings.Contains(n.Raw, "$") {
|
||||
v.opcode(f, n, n.Name, n.Value)
|
||||
continue
|
||||
}
|
||||
v.undefined(f, n, n.Raw)
|
||||
text := n.Raw
|
||||
if strings.Contains(text, "$") {
|
||||
// A variable's value can hold opcodes and headers, so the
|
||||
// text it makes is read again.
|
||||
text = substitute(v.defines, text)
|
||||
}
|
||||
v.read(f, n, text)
|
||||
}
|
||||
func (v *validator) define(f *File, n *Node) { v.undefined(f, n, n.Value) }
|
||||
|
||||
func (v *validator) include(f *File, n *Node) {
|
||||
v.undefined(f, n, n.Value)
|
||||
if n.Resolved != "" && !fs.ValidPath(n.Resolved) {
|
||||
v.add(f.Path, n, SeverityWarning, "include.outside", fmt.Sprintf("%s is outside the folder of the main file", n.Resolved))
|
||||
}
|
||||
}
|
||||
|
||||
// read checks the headers and opcodes that text holds, which stands where n
|
||||
// does.
|
||||
func (v *validator) read(f *File, n *Node, text string) {
|
||||
for _, m := range lex(text) {
|
||||
switch m.Kind {
|
||||
case KindHeader:
|
||||
v.header(f, n, m.Name)
|
||||
case KindOpcode:
|
||||
v.opcode(f, n, m.Name, m.Value)
|
||||
case KindText:
|
||||
if strings.Contains(m.Raw, "$") {
|
||||
continue
|
||||
}
|
||||
v.add(f.Path, n, SeverityWarning, "text.unreadable", fmt.Sprintf("can't read %q", strings.TrimSpace(m.Raw)))
|
||||
}
|
||||
func (v *validator) variables(f *File, n *Node) { v.undefined(f, n, n.Raw) }
|
||||
|
||||
func (v *validator) text(f *File, n *Node, raw string) {
|
||||
if strings.Contains(raw, "$") {
|
||||
return
|
||||
}
|
||||
v.add(f.Path, n, SeverityWarning, "text.unreadable", fmt.Sprintf("can't read %q", strings.TrimSpace(raw)))
|
||||
}
|
||||
|
||||
func (v *validator) header(f *File, n *Node, name string) {
|
||||
func (v *validator) header(f *File, n *Node, name string, _ bool) {
|
||||
v.endHeader()
|
||||
v.inHeader = true
|
||||
v.numbers = map[string]opcodeAt{}
|
||||
@@ -131,7 +100,7 @@ func (v *validator) endHeader() {
|
||||
v.numbers = nil
|
||||
}
|
||||
|
||||
func (v *validator) opcode(f *File, n *Node, name, value string) {
|
||||
func (v *validator) opcode(f *File, n *Node, name, value string, _ bool) {
|
||||
if !v.inHeader {
|
||||
v.add(f.Path, n, SeverityInfo, "opcode.no-header", fmt.Sprintf("%s is before any header", name))
|
||||
v.inHeader = true
|
||||
@@ -216,10 +185,8 @@ func bounds(info OpcodeInfo) string {
|
||||
|
||||
// undefined reports the variables in text that haven't been defined.
|
||||
func (v *validator) undefined(f *File, n *Node, text string) {
|
||||
for _, run := range variableRE.FindAllString(text, -1) {
|
||||
if substitute(v.defines, run) == run {
|
||||
v.add(f.Path, n, SeverityWarning, "define.undefined", fmt.Sprintf("%s isn't defined", run))
|
||||
}
|
||||
for _, run := range v.defs.undefined(text) {
|
||||
v.add(f.Path, n, SeverityWarning, "define.undefined", fmt.Sprintf("%s isn't defined", run))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user