1// SPDX-License-Identifier: MIT
2
3package template
4
5import (
6 "strconv"
7 "strings"
8
9 "bnema.dev/ermite-actions/expr"
10)
11
12// DefinitionInfo is a definition together with the contexts and functions
13// expressions at that place may use: those of the definition and of every
14// definition it was reached through.
15type DefinitionInfo struct {
16 schema *Schema
17 // Definition is the schema definition.
18 Definition *Definition
19
20 allowed []string
21 named []string
22 funcs []expr.FuncInfo
23}
24
25func newDefinitionInfo(s *Schema, parent *DefinitionInfo, d *Definition) *DefinitionInfo {
26 di := &DefinitionInfo{schema: s, Definition: d}
27 if len(d.Context) > 0 {
28 seen := map[string]bool{}
29 if parent != nil {
30 for _, c := range parent.allowed {
31 di.allowed = append(di.allowed, c)
32 seen[strings.ToUpper(c)] = true
33 }
34 }
35 for _, c := range d.Context {
36 if u := strings.ToUpper(c); !seen[u] {
37 di.allowed = append(di.allowed, c)
38 seen[u] = true
39 }
40 }
41 } else if parent != nil {
42 di.allowed, di.named, di.funcs = parent.allowed, parent.named, parent.funcs
43 return di
44 }
45 di.named, di.funcs = splitAllowedContext(di.allowed)
46 return di
47}
48
49// child returns the info of the definition called name, reached through di.
50func (di *DefinitionInfo) child(name string) *DefinitionInfo {
51 return newDefinitionInfo(di.schema, di, di.schema.get(name))
52}
53
54// AllowedContext returns the raw entries: context names and function
55// signatures such as "hashFiles(1,255)".
56func (di *DefinitionInfo) AllowedContext() []string { return di.allowed }
57
58// NamedContexts returns the names of the contexts expressions may use.
59func (di *DefinitionInfo) NamedContexts() []string { return di.named }
60
61// Functions returns the extra functions expressions may call, with their
62// arity.
63func (di *DefinitionInfo) Functions() []expr.FuncInfo { return di.funcs }
64
65// splitAllowedContext separates "name(min,max)" entries, with max a number or
66// MAX, from plain context names.
67func splitAllowedContext(allowed []string) ([]string, []expr.FuncInfo) {
68 var named []string
69 var funcs []expr.FuncInfo
70 for _, item := range allowed {
71 if f, ok := parseFuncEntry(item); ok {
72 funcs = append(funcs, f)
73 } else {
74 named = append(named, item)
75 }
76 }
77 return named, funcs
78}
79
80// parseFuncEntry matches ^([a-zA-Z0-9_]+)\(([0-9]+),([0-9]+|MAX)\)$.
81func parseFuncEntry(s string) (expr.FuncInfo, bool) {
82 open := strings.IndexByte(s, '(')
83 if open <= 0 || !strings.HasSuffix(s, ")") {
84 return expr.FuncInfo{}, false
85 }
86 name := s[:open]
87 for _, r := range name {
88 if r != '_' && (r < '0' || r > '9') && (r < 'a' || r > 'z') && (r < 'A' || r > 'Z') {
89 return expr.FuncInfo{}, false
90 }
91 }
92 minS, maxS, ok := strings.Cut(s[open+1:len(s)-1], ",")
93 if !ok || !isDigits(minS) || (!isDigits(maxS) && maxS != "MAX") {
94 return expr.FuncInfo{}, false
95 }
96 lo, err := strconv.Atoi(minS)
97 if err != nil {
98 return expr.FuncInfo{}, false
99 }
100 hi := -1
101 if maxS != "MAX" {
102 if hi, err = strconv.Atoi(maxS); err != nil {
103 return expr.FuncInfo{}, false
104 }
105 }
106 return expr.FuncInfo{Name: name, MinArgs: lo, MaxArgs: hi}, true
107}
108
109func isDigits(s string) bool {
110 if s == "" {
111 return false
112 }
113 for i := 0; i < len(s); i++ {
114 if s[i] < '0' || s[i] > '9' {
115 return false
116 }
117 }
118 return true
119}
120
121// Info returns the info of the schema definition called name, with the
122// contexts and functions that definition allows, or nil when there is none.
123func (s *Schema) Info(name string) *DefinitionInfo {
124 d := s.definitions[name]
125 if d == nil {
126 return nil
127 }
128 return newDefinitionInfo(s, nil, d)
129}