|
| 1 | +package explain |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/sqlc-dev/doubleclick/ast" |
| 8 | +) |
| 9 | + |
| 10 | +// explainDictionaryAttributeDeclaration outputs a dictionary attribute declaration. |
| 11 | +func explainDictionaryAttributeDeclaration(sb *strings.Builder, n *ast.DictionaryAttributeDeclaration, indent string, depth int) { |
| 12 | + children := 0 |
| 13 | + if n.Type != nil { |
| 14 | + children++ |
| 15 | + } |
| 16 | + if n.Default != nil { |
| 17 | + children++ |
| 18 | + } |
| 19 | + if n.Expression != nil { |
| 20 | + children++ |
| 21 | + } |
| 22 | + if children > 0 { |
| 23 | + fmt.Fprintf(sb, "%sDictionaryAttributeDeclaration %s (children %d)\n", indent, n.Name, children) |
| 24 | + } else { |
| 25 | + fmt.Fprintf(sb, "%sDictionaryAttributeDeclaration %s\n", indent, n.Name) |
| 26 | + } |
| 27 | + if n.Type != nil { |
| 28 | + Node(sb, n.Type, depth+1) |
| 29 | + } |
| 30 | + if n.Default != nil { |
| 31 | + Node(sb, n.Default, depth+1) |
| 32 | + } |
| 33 | + if n.Expression != nil { |
| 34 | + Node(sb, n.Expression, depth+1) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +// explainDictionaryDefinition outputs a dictionary definition section. |
| 39 | +func explainDictionaryDefinition(sb *strings.Builder, n *ast.DictionaryDefinition, indent string, depth int) { |
| 40 | + children := 0 |
| 41 | + if len(n.PrimaryKey) > 0 { |
| 42 | + children++ |
| 43 | + } |
| 44 | + if n.Source != nil { |
| 45 | + children++ |
| 46 | + } |
| 47 | + if n.Lifetime != nil { |
| 48 | + children++ |
| 49 | + } |
| 50 | + if n.Layout != nil { |
| 51 | + children++ |
| 52 | + } |
| 53 | + if n.Range != nil { |
| 54 | + children++ |
| 55 | + } |
| 56 | + if len(n.Settings) > 0 { |
| 57 | + children++ |
| 58 | + } |
| 59 | + if children > 0 { |
| 60 | + fmt.Fprintf(sb, "%sDictionary definition (children %d)\n", indent, children) |
| 61 | + } else { |
| 62 | + fmt.Fprintf(sb, "%sDictionary definition\n", indent) |
| 63 | + } |
| 64 | + |
| 65 | + // PRIMARY KEY |
| 66 | + if len(n.PrimaryKey) > 0 { |
| 67 | + fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(n.PrimaryKey)) |
| 68 | + for _, pk := range n.PrimaryKey { |
| 69 | + Node(sb, pk, depth+2) |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + // SOURCE |
| 74 | + if n.Source != nil { |
| 75 | + explainDictionarySource(sb, n.Source, indent+" ", depth+1) |
| 76 | + } |
| 77 | + |
| 78 | + // LIFETIME |
| 79 | + if n.Lifetime != nil { |
| 80 | + explainDictionaryLifetime(sb, n.Lifetime, indent+" ", depth+1) |
| 81 | + } |
| 82 | + |
| 83 | + // RANGE (if present, comes before LAYOUT) |
| 84 | + if n.Range != nil { |
| 85 | + explainDictionaryRange(sb, n.Range, indent+" ", depth+1) |
| 86 | + } |
| 87 | + |
| 88 | + // LAYOUT |
| 89 | + if n.Layout != nil { |
| 90 | + explainDictionaryLayout(sb, n.Layout, indent+" ", depth+1) |
| 91 | + } |
| 92 | + |
| 93 | + // SETTINGS |
| 94 | + if len(n.Settings) > 0 { |
| 95 | + fmt.Fprintf(sb, "%s Set\n", indent) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +// explainDictionarySource outputs a dictionary SOURCE clause. |
| 100 | +func explainDictionarySource(sb *strings.Builder, n *ast.DictionarySource, indent string, depth int) { |
| 101 | + // FunctionWithKeyValueArguments has extra space before name |
| 102 | + children := 0 |
| 103 | + if len(n.Args) > 0 { |
| 104 | + children = 1 |
| 105 | + } |
| 106 | + if children > 0 { |
| 107 | + fmt.Fprintf(sb, "%sFunctionWithKeyValueArguments %s (children %d)\n", indent, strings.ToLower(n.Type), children) |
| 108 | + fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(n.Args)) |
| 109 | + for _, arg := range n.Args { |
| 110 | + explainKeyValuePair(sb, arg, indent+" ", depth+2) |
| 111 | + } |
| 112 | + } else { |
| 113 | + fmt.Fprintf(sb, "%sFunctionWithKeyValueArguments %s\n", indent, strings.ToLower(n.Type)) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +// explainKeyValuePair outputs a key-value pair (lowercase "pair"). |
| 118 | +func explainKeyValuePair(sb *strings.Builder, n *ast.KeyValuePair, indent string, depth int) { |
| 119 | + children := 0 |
| 120 | + if n.Value != nil { |
| 121 | + children = 1 |
| 122 | + } |
| 123 | + if children > 0 { |
| 124 | + fmt.Fprintf(sb, "%spair (children %d)\n", indent, children) |
| 125 | + Node(sb, n.Value, depth+1) |
| 126 | + } else { |
| 127 | + fmt.Fprintf(sb, "%spair\n", indent) |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +// explainDictionaryLifetime outputs a dictionary LIFETIME clause. |
| 132 | +func explainDictionaryLifetime(sb *strings.Builder, n *ast.DictionaryLifetime, indent string, depth int) { |
| 133 | + // LIFETIME is output as "Dictionary lifetime" without children count typically |
| 134 | + fmt.Fprintf(sb, "%sDictionary lifetime\n", indent) |
| 135 | +} |
| 136 | + |
| 137 | +// explainDictionaryLayout outputs a dictionary LAYOUT clause. |
| 138 | +func explainDictionaryLayout(sb *strings.Builder, n *ast.DictionaryLayout, indent string, depth int) { |
| 139 | + children := 0 |
| 140 | + if len(n.Args) > 0 { |
| 141 | + children = 1 |
| 142 | + } |
| 143 | + if children > 0 { |
| 144 | + fmt.Fprintf(sb, "%sDictionary layout (children %d)\n", indent, children) |
| 145 | + fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(n.Args)) |
| 146 | + for _, arg := range n.Args { |
| 147 | + explainKeyValuePair(sb, arg, indent+" ", depth+2) |
| 148 | + } |
| 149 | + } else { |
| 150 | + fmt.Fprintf(sb, "%sDictionary layout (children 1)\n", indent) |
| 151 | + fmt.Fprintf(sb, "%s ExpressionList\n", indent) |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +// explainDictionaryRange outputs a dictionary RANGE clause. |
| 156 | +func explainDictionaryRange(sb *strings.Builder, n *ast.DictionaryRange, indent string, depth int) { |
| 157 | + children := 0 |
| 158 | + if n.Min != nil { |
| 159 | + children++ |
| 160 | + } |
| 161 | + if n.Max != nil { |
| 162 | + children++ |
| 163 | + } |
| 164 | + if children > 0 { |
| 165 | + fmt.Fprintf(sb, "%sDictionary range (children %d)\n", indent, children) |
| 166 | + if n.Min != nil { |
| 167 | + Node(sb, n.Min, depth+1) |
| 168 | + } |
| 169 | + if n.Max != nil { |
| 170 | + Node(sb, n.Max, depth+1) |
| 171 | + } |
| 172 | + } else { |
| 173 | + fmt.Fprintf(sb, "%sDictionary range\n", indent) |
| 174 | + } |
| 175 | +} |
0 commit comments