forma-dsl 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,226 @@
1
+ /** @import { SourceLocation } from '../index.js' */
2
+
3
+ /**
4
+ * The shapes the parser produces, as types only.
5
+ *
6
+ * This module has no runtime content. It exists because the AST was previously implicit:
7
+ * the set of `kind` values, and what each one carries, could only be recovered by reading
8
+ * the parser and the evaluator side by side and hoping they agreed.
9
+ *
10
+ * @module
11
+ */
12
+
13
+ export {};
14
+
15
+ /**
16
+ * A number, string, boolean or null written directly in the source.
17
+ *
18
+ * @typedef {object} LiteralExpression
19
+ * @property {'literal'} kind Discriminant.
20
+ * @property {number | string | boolean | null} value The value.
21
+ * @property {SourceLocation} [loc] Where it was written.
22
+ */
23
+
24
+ /**
25
+ * A `"..."` string with at least one `${...}` splice in it.
26
+ *
27
+ * @typedef {object} TemplateExpression
28
+ * @property {'template'} kind Discriminant.
29
+ * @property {Array<{ kind: 'text', value: string } | { kind: 'expr', expression: Expression }>} parts
30
+ * The literal runs and the spliced expressions, in source order.
31
+ * @property {SourceLocation} loc Where the string began.
32
+ */
33
+
34
+ /**
35
+ * A `[a, b, c]` list.
36
+ *
37
+ * @typedef {object} ArrayExpression
38
+ * @property {'array'} kind Discriminant.
39
+ * @property {Expression[]} items The elements.
40
+ * @property {SourceLocation} loc Where the bracket was.
41
+ */
42
+
43
+ /**
44
+ * A `{ key = value }` object.
45
+ *
46
+ * @typedef {object} ObjectExpression
47
+ * @property {'object'} kind Discriminant.
48
+ * @property {Array<{ key: string, value: Expression }>} entries The entries, in source order.
49
+ * @property {SourceLocation} loc Where the brace was.
50
+ */
51
+
52
+ /**
53
+ * A bare name, resolved against the enclosing scopes.
54
+ *
55
+ * @typedef {object} IdentifierExpression
56
+ * @property {'identifier'} kind Discriminant.
57
+ * @property {string} name The name.
58
+ * @property {SourceLocation} loc Where the name was.
59
+ */
60
+
61
+ /**
62
+ * A `target.property` access. Also covers `.x`/`.y`/`.z` on a list.
63
+ *
64
+ * @typedef {object} MemberExpression
65
+ * @property {'member'} kind Discriminant.
66
+ * @property {Expression} object What is being read from.
67
+ * @property {string} property The property name.
68
+ * @property {SourceLocation} [loc] Where the dot was.
69
+ */
70
+
71
+ /**
72
+ * A `target[index]` access.
73
+ *
74
+ * @typedef {object} IndexExpression
75
+ * @property {'index'} kind Discriminant.
76
+ * @property {Expression} object What is being indexed.
77
+ * @property {Expression} index The index.
78
+ * @property {SourceLocation} [loc] Where the bracket was.
79
+ */
80
+
81
+ /**
82
+ * A prefix `-` or `!`.
83
+ *
84
+ * @typedef {object} UnaryExpression
85
+ * @property {'unary'} kind Discriminant.
86
+ * @property {'-' | '!'} op The operator.
87
+ * @property {Expression} operand What it applies to.
88
+ * @property {SourceLocation} loc Where the operator was.
89
+ */
90
+
91
+ /**
92
+ * An infix operator. `&&` and `||` short-circuit; the rest evaluate both sides.
93
+ *
94
+ * @typedef {object} BinaryExpression
95
+ * @property {'binary'} kind Discriminant.
96
+ * @property {string} op The operator.
97
+ * @property {Expression} left The left operand.
98
+ * @property {Expression} right The right operand.
99
+ * @property {SourceLocation} loc Where the operator was.
100
+ */
101
+
102
+ /**
103
+ * A `condition ? then : otherwise`.
104
+ *
105
+ * @typedef {object} ConditionalExpression
106
+ * @property {'conditional'} kind Discriminant.
107
+ * @property {Expression} condition What decides.
108
+ * @property {Expression} then The value when truthy.
109
+ * @property {Expression} otherwise The value when not.
110
+ * @property {SourceLocation} [loc] Where it was written.
111
+ */
112
+
113
+ /**
114
+ * A call to one of the builtin functions. There are no user-defined functions.
115
+ *
116
+ * @typedef {object} CallExpression
117
+ * @property {'call'} kind Discriminant.
118
+ * @property {Expression} callee Must resolve to an identifier naming a builtin.
119
+ * @property {Array<{ name: string | null, value: Expression }>} args The arguments.
120
+ * @property {SourceLocation} loc Where the parenthesis was.
121
+ */
122
+
123
+ /**
124
+ * Anything that evaluates to a value.
125
+ *
126
+ * @typedef {LiteralExpression | TemplateExpression | ArrayExpression | ObjectExpression
127
+ * | IdentifierExpression | MemberExpression | IndexExpression | UnaryExpression
128
+ * | BinaryExpression | ConditionalExpression | CallExpression} Expression
129
+ */
130
+
131
+ /**
132
+ * A `name = expression` inside a block body.
133
+ *
134
+ * @typedef {object} Attribute
135
+ * @property {string} name The attribute name.
136
+ * @property {Expression} value The value.
137
+ * @property {SourceLocation} [loc] Where the name was.
138
+ */
139
+
140
+ /**
141
+ * The contents of a `{ ... }`, sorted by what each entry is.
142
+ *
143
+ * @typedef {object} Body
144
+ * @property {Attribute[]} attributes The `name = value` entries.
145
+ * @property {Array<Block | ForBlock | IfBlock>} blocks The nested blocks, in source order.
146
+ * @property {ParamDeclaration[]} params Params declared in this body.
147
+ * @property {LocalDeclaration[]} locals Locals declared in this body.
148
+ */
149
+
150
+ /**
151
+ * A nested block: a shape, an operation, a part, or a call to a component.
152
+ *
153
+ * @typedef {object} Block
154
+ * @property {'block'} kind Discriminant.
155
+ * @property {string} type Which block, such as `box` or `translate`.
156
+ * @property {Expression[]} labels The labels before the brace, as expressions, so a part
157
+ * built in a loop can name itself from the loop variable.
158
+ * @property {Body} body Its attributes and children.
159
+ * @property {SourceLocation} loc Where the block began.
160
+ */
161
+
162
+ /**
163
+ * A `for name in sequence { ... }`, optionally with an index variable.
164
+ *
165
+ * @typedef {object} ForBlock
166
+ * @property {'for'} kind Discriminant.
167
+ * @property {string[]} names One name binds the value; two bind the index and the value.
168
+ * @property {Expression} sequence What to iterate.
169
+ * @property {Body} body What to emit per iteration.
170
+ * @property {SourceLocation} loc Where the `for` was.
171
+ */
172
+
173
+ /**
174
+ * An `if condition { ... } else { ... }`.
175
+ *
176
+ * @typedef {object} IfBlock
177
+ * @property {'if'} kind Discriminant.
178
+ * @property {Expression} condition What decides.
179
+ * @property {Body} then The branch taken when truthy.
180
+ * @property {Body | null} otherwise The branch taken otherwise, if there is one.
181
+ * @property {SourceLocation} loc Where the `if` was.
182
+ */
183
+
184
+ /**
185
+ * A `param name { ... }` or `param name = expression`.
186
+ *
187
+ * @typedef {object} ParamDeclaration
188
+ * @property {'param'} kind Discriminant.
189
+ * @property {string} name The param name.
190
+ * @property {Attribute[]} attributes Its metadata; only `default` affects geometry.
191
+ * @property {SourceLocation} loc Where the declaration began.
192
+ */
193
+
194
+ /**
195
+ * A `local name = expression`.
196
+ *
197
+ * @typedef {object} LocalDeclaration
198
+ * @property {'local'} kind Discriminant.
199
+ * @property {string} name The local's name.
200
+ * @property {Expression} value Its value, computed once.
201
+ * @property {SourceLocation} loc Where the declaration began.
202
+ */
203
+
204
+ /**
205
+ * A `component "name" { ... }` or `model "name" { ... }`.
206
+ *
207
+ * @typedef {object} NamedDeclaration
208
+ * @property {'component' | 'model'} kind Which of the two.
209
+ * @property {string} name The declared name, always static.
210
+ * @property {Body} body Its contents.
211
+ * @property {SourceLocation} loc Where the declaration began.
212
+ */
213
+
214
+ /**
215
+ * Anything that can appear at the top level of a document.
216
+ *
217
+ * @typedef {ParamDeclaration | LocalDeclaration | NamedDeclaration} Declaration
218
+ */
219
+
220
+ /**
221
+ * A whole parsed document.
222
+ *
223
+ * @typedef {object} Document
224
+ * @property {'document'} kind Discriminant.
225
+ * @property {Declaration[]} declarations The top-level blocks, in source order.
226
+ */