Parse and check a format in one call, keeping compileTemplate under the complexity gate

This commit is contained in:
2026-09-24 23:27:25 +02:00
committed by Lillem4n
parent 467cb6fb18
commit 2a7499e93c
2 changed files with 10 additions and 8 deletions
+2 -8
View File
@@ -204,13 +204,10 @@ func jsonKind(v any) string {
}
func compileString(s string) (node, error) {
toks, err := parseFormat(s)
toks, err := parseChecked(s, nil)
if err != nil {
return nil, err
}
if err := checkTokens(toks, nil); err != nil {
return nil, err
}
t := &template{format: s, tokens: toks, repeat: 1, fromString: true}
if err := t.compileRefFree(); err != nil {
return nil, err
@@ -336,13 +333,10 @@ func compileTemplate(m map[string]any, pos position) (node, error) {
if len(fields) == 0 && o.repeat == 1 && !o.weighted && o.datatype == DataTypeString && o.group == "" {
return nil, fmt.Errorf("an object holding only a format is a string; write %q", o.format)
}
toks, err := parseFormat(o.format)
toks, err := parseChecked(o.format, fields)
if err != nil {
return nil, err
}
if err := checkTokens(toks, fields); err != nil {
return nil, err
}
if err := checkNestedDrawGroup(fields, o.group); err != nil {
return nil, err
}
+8
View File
@@ -185,6 +185,14 @@ func checkFunc(tok formatToken, fields map[string]node) error {
return nil
}
func parseChecked(format string, fields map[string]node) ([]formatToken, error) {
toks, err := parseFormat(format)
if err != nil {
return nil, err
}
return toks, checkTokens(toks, fields)
}
// checkTokens proves every parsed token names an existing field or a known function,
// so a typo'd or dangling reference is a New-time error.
func checkTokens(toks []formatToken, fields map[string]node) error {