-
Notifications
You must be signed in to change notification settings - Fork 148
/
ast.ex
654 lines (571 loc) · 21.5 KB
/
ast.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
defmodule Surface.AST.Container do
@moduledoc """
An AST node representing a container of other nodes. This does not
have content itself, just contains children which have content, and
directives that operate on the entirety of the children (i.e. for, if, debug)
## Properties
* `:children` - children AST nodes
* `:attributes` - the specified attributes
* `:directives` - directives associated with this container
* `:meta` - compile meta
* `:debug` - keyword list indicating when debug information should be printed during compilation
"""
defstruct [:children, :meta, debug: [], attributes: [], directives: []]
@type t :: %__MODULE__{
children: list(Surface.AST.t()),
debug: list(atom()),
meta: Surface.AST.Meta.t(),
attributes: list(Surface.AST.Attribute.t()),
directives: list(Surface.AST.Directive.t())
}
end
defmodule Surface.AST.Block do
@moduledoc """
An AST node representing a generic block.
## Properties
* `:name` - name of the block
* `:expression` - the expression passed to block
* `:sub_blocks` - a list containing each sub-block's {name, children_ast}
* `:meta` - compile meta
* `:debug` - keyword list indicating when debug information should be printed during compilation
"""
defstruct [:name, :expression, :sub_blocks, :meta, debug: []]
@type t :: %__MODULE__{
name: binary(),
expression: Surface.AST.AttributeExpr.t(),
sub_blocks: list(Surface.AST.SubBlock.t()),
debug: list(atom()),
meta: Surface.AST.Meta.t()
}
end
defmodule Surface.AST.SubBlock do
@moduledoc """
An AST node representing a generic sub-block.
## Properties
* `:name` - name of the block
* `:expression` - the expression passed to block
* `:children` - children AST nodes
* `:meta` - compile meta
* `:debug` - keyword list indicating when debug information should be printed during compilation
"""
defstruct [:name, :expression, :children, :meta, debug: []]
@type t :: %__MODULE__{
name: :default | binary(),
expression: Surface.AST.AttributeExpr.t(),
children: list(Surface.AST.t()),
debug: list(atom()),
meta: Surface.AST.Meta.t()
}
end
defmodule Surface.AST.Expr do
@moduledoc """
An AST node representing an expression which does not resolve to a value printed out to the final DOM.
## Properties
* `:value` - a quoted expression
* `:constant?` - true if the expression can be evaluated at compile time
* `:meta` - compile meta
* `:directives` - directives associated with this expression node
"""
defstruct [:value, :meta, constant?: false, directives: []]
@type t :: %__MODULE__{
# quoted expression
value: any(),
constant?: boolean(),
directives: list(Surface.AST.Directive.t()),
meta: Surface.AST.Meta.t()
}
end
defmodule Surface.AST.Meta do
@moduledoc """
A container for metadata about compilation.
## Properties
* `:line` - the line from the source code where the parent was extracted
* `:module` - the component module (e.g. `Surface.Components.LivePatch`)
* `:node_alias` - the alias used inside the source code (e.g. `LivePatch`)
* `:file` - the file from which the source was extracted
* `:caller` - a Macro.Env struct representing the caller
* `:function_component?` - indicates if it's a function component or not
* `:style` - the style info of the component, if any
"""
alias Surface.Compiler.Helpers
@derive {Inspect, only: [:line, :column, :module, :node_alias, :file, :checks, :function_component?]}
defstruct [:line, :column, :module, :node_alias, :file, :caller, :checks, :function_component?, :style]
@type t :: %__MODULE__{
line: non_neg_integer(),
column: non_neg_integer(),
module: atom(),
node_alias: binary() | nil,
caller: Macro.Env.t(),
file: binary(),
checks: Keyword.t(boolean()),
function_component?: boolean(),
style:
%{
scope_id: binary(),
css: binary(),
selectors: [binary()],
vars: %{(var :: binary()) => expr :: binary()}
}
| nil
}
@doc false
def quoted_caller_context(meta) do
quoted_cid =
if Helpers.is_stateful_component(meta.caller.module) and meta.caller.function == {:render, 1} do
quote do: @myself
else
nil
end
quote do
%{
cid: unquote(quoted_cid),
file: unquote(meta.file),
line: unquote(meta.line),
module: unquote(meta.caller.module)
}
end
end
end
defmodule Surface.AST.Directive do
@moduledoc """
An AST node representing a directive
## Properties
* `:module` - the module which implements logic for this directive (e.g. `Surface.Directive.Let`)
* `:name` - the name of the directive (e.g. `:let`)
* `:value` - the code/configuration for this directive. typically a quoted expression
* `:meta` - compilation meta data
"""
defstruct [:module, :name, :value, :meta]
@type t :: %__MODULE__{
module: atom(),
name: atom(),
# the value here is defined by the individual directive
value: Surface.AST.AttributeExpr.t() | Surface.AST.Literal.t() | nil,
meta: Surface.AST.Meta.t()
}
end
defmodule Surface.AST.For do
@moduledoc """
An AST node representing a for comprehension.
## Properties
* `:generator` - a quoted expression
* `:children` - the children to collect over the generator
* `:meta` - compilation meta data
* `:debug` - keyword list indicating when debug information should be printed during compilation
* `:directives` - directives associated with this node
"""
defstruct [:generator, :children, :meta, else: [], debug: [], directives: []]
@type t :: %__MODULE__{
generator: any(),
debug: list(atom()),
directives: list(Surface.AST.Directive.t()),
children: list(Surface.AST.t()),
meta: Surface.AST.Meta.t()
}
end
defmodule Surface.AST.If do
@moduledoc """
An AST node representing an if/else expression
## Properties
* `:condition` - a quoted expression
* `:children` - the children to insert into the dom if the condition evaluates truthy
* `:else` - the children to insert into the dom if the condition evaluates falsy
* `:meta` - compilation meta data
* `:debug` - keyword list indicating when debug information should be printed during compilation
* `:directives` - directives associated with this node
"""
defstruct [:condition, :children, :meta, else: [], debug: [], directives: []]
@type t :: %__MODULE__{
condition: Surface.AST.AttributeExpr.t(),
debug: list(atom()),
directives: list(Surface.AST.Directive.t()),
children: list(Surface.AST.t()),
else: list(Surface.AST.t()),
meta: Surface.AST.Meta.t()
}
end
defmodule Surface.AST.Attribute do
@moduledoc """
An AST node representing an attribute or property
## Properties
* `:type` - an atom representing the type of attribute. See Surface.API for the list of supported types
* `:type_opts` - a keyword list of options for the type
* `:name` - the name of the attribute (e.g. `:class`)
* `:root` - true if the attribute was defined using root notation `{ ... }`. Root attributes won't have `name`.
* `:value` - a list of nodes that can be concatenated to form the value for this attribute. Potentially contains dynamic data
* `:meta` - compilation meta data
"""
defstruct [:name, :root, :type, :type_opts, :value, :meta]
@type t :: %__MODULE__{
type: atom() | nil,
type_opts: keyword() | nil,
name: atom() | nil,
root: boolean() | nil,
value: Surface.AST.Literal.t() | Surface.AST.AttributeExpr.t(),
meta: Surface.AST.Meta.t()
}
end
defmodule Surface.AST.DynamicAttribute do
@moduledoc """
An AST node representing a dynamic attribute (or attributes).
## Properties
* `:expr` - an expression which will generate a keyword list of attributes and value tuples of the form {type, value}
* `:meta` - compilation meta data
"""
defstruct [:name, :expr, :meta]
@type t :: %__MODULE__{
expr: Surface.AST.AttributeExpr.t(),
meta: Surface.AST.Meta.t()
}
end
defmodule Surface.AST.AttributeExpr do
@moduledoc """
An AST node representing an attribute expression (i.e. a dynamic value for an attribute, directive, or property)
## Properties
* `:original` - the original text, useful for debugging and error messages
* `:value` - a quoted expression
* `:constant?` - true if the expression can be evaluated at compile time
* `:meta` - compilation meta data
"""
defstruct [:original, :value, :meta, constant?: false]
@type t :: %__MODULE__{
# quoted
value: any(),
original: binary(),
constant?: boolean(),
meta: Surface.AST.Meta.t()
}
def new(expr, original, meta) do
%__MODULE__{
value: expr,
original: original,
constant?: constant?(expr),
meta: meta
}
end
defp constant?(
{{:., _, [{:__aliases__, _, [:Surface, :TypeHandler]}, :expr_to_value!]}, _,
[_type, _name, clauses, opts, _module, _original, ctx]}
) do
Macro.quoted_literal?(clauses) and Macro.quoted_literal?(opts) and Macro.quoted_literal?(ctx)
end
defp constant?(expr) do
Macro.quoted_literal?(expr)
end
end
defmodule Surface.AST.Interpolation do
@moduledoc """
An AST node representing interpolation within a node
## Properties
* `:original` - the original text, useful for debugging and error messages
* `:value` - a quoted expression
* `:constant?` - true if the expression can be evaluated at compile time
* `:meta` - compilation meta data
* `:directives` - directives associated with this interpolation
"""
defstruct [:original, :value, :meta, constant?: false, directives: []]
@type t :: %__MODULE__{
original: binary(),
# quoted
value: any(),
constant?: boolean(),
directives: list(Surface.AST.Directive.t()),
meta: Surface.AST.Meta.t()
}
end
defmodule Surface.AST.Slot do
@moduledoc """
An AST node representing a <#slot /> tag
## Properties
* `:name` - the slot name
* `:index` - the index of the slotable entry assigned to this slot
* `:for` - the slotable entry assigned for this slot
* `:default` - a list of AST nodes representing the default content for this slot
* `:arg` - quoted expression representing the argument for this slot
* `:generator_value` - value from the `:generator_prop` property
* `:context_put` - value from the `:generator_prop` property
* `:meta` - compilation meta data
* `:directives` - directives associated with this slot
"""
defstruct [:name, :as, :for, :index, :arg, :generator_value, :context_put, :default, :meta, directives: []]
@type t :: %__MODULE__{
name: binary(),
as: atom(),
index: any(),
for: any(),
directives: list(Surface.AST.Directive.t()),
meta: Surface.AST.Meta.t(),
arg: Macro.t(),
generator_value: any(),
context_put: list(Surface.AST.AttributeExpr.t()),
default: list(Surface.AST.t())
}
end
# TODO differentiate between raw HTML and plain text ?
defmodule Surface.AST.Literal do
@moduledoc """
An AST node representing a literal value
## Properties
* `:value` - the value
* `:directives` - directives associated with this literal value
"""
defstruct [:value, directives: []]
@type t :: %__MODULE__{
directives: list(Surface.AST.Directive.t()),
value: binary | boolean | integer | atom
}
end
defmodule Surface.AST.Tag do
@moduledoc """
An AST node representing a standard HTML tag
## Properties
* `:element` - the element name
* `:attributes` - the attributes for this tag
* `:directives` - any directives to be applied to this tag
* `:children` - the tag children
* `:meta` - compilation meta data
* `:debug` - keyword list indicating when debug information should be printed during compilation
"""
defstruct [:element, :attributes, :children, :meta, debug: [], directives: []]
@type t :: %__MODULE__{
element: binary(),
debug: list(atom()),
attributes: list(Surface.AST.Attribute.t() | Surface.AST.DynamicAttribute.t()),
directives: list(Surface.AST.Directive.t()),
children: list(Surface.AST.t()),
meta: Surface.AST.Meta.t()
}
end
defmodule Surface.AST.VoidTag do
@moduledoc """
An AST node representing a void (empty) HTML tag
## Properties
* `:element` - the element name
* `:attributes` - the attributes for this tag
* `:directives` - any directives to be applied to this tag
* `:meta` - compilation meta data
* `:debug` - keyword list indicating when debug information should be printed during compilation
"""
defstruct [:element, :attributes, :meta, debug: [], directives: []]
@type t :: %__MODULE__{
element: binary(),
debug: list(atom()),
attributes: list(Surface.AST.Attribute.t() | Surface.AST.DynamicAttribute.t()),
directives: list(Surface.AST.Directive.t()),
meta: Surface.AST.Meta.t()
}
end
defmodule Surface.AST.SlotEntry do
@moduledoc """
An AST node representing a <:slot> entry. This is used to provide content for slots
## Properties
* `:name` - the slot entry name
* `:props` - the props for slot entry tag
* `:let` - the `:let` expression
* `:children` - the slot entry children
* `:meta` - compilation meta data
* `:debug` - keyword list indicating when debug information should be printed during compilation
* `:directives` - directives associated with this slot entry
"""
defstruct [:name, :props, :children, :let, :meta, directives: []]
@type t :: %__MODULE__{
name: atom(),
children: list(Surface.AST.t()),
directives: list(Surface.AST.Directive.t()),
props: list(Surface.AST.Attribute.t() | Surface.AST.DynamicAttribute.t()),
let: Surface.AST.AttributeExpr.t() | nil,
meta: Surface.AST.Meta.t()
}
end
defmodule Surface.AST.Error do
@moduledoc """
An AST node representing an error. This will be rendered as an html element.
## Properties
* `:message` - the error message
* `:meta` - compilation meta data
* `:attributes` - the specified attributes
* `:directives` - directives associated with this error node
"""
defstruct [:message, :meta, attributes: [], directives: []]
@type t :: %__MODULE__{
message: binary(),
attributes: list(Surface.AST.Attribute.t()),
directives: list(Surface.AST.Directive.t()),
meta: Surface.AST.Meta.t()
}
end
defmodule Surface.AST.Component do
@moduledoc """
An AST node representing a component
## Properties
* `:module` - the component module
* `:type` - the type of component (i.e. Surface.LiveComponent vs Surface.LiveView or :dynamic_live)
* `:props` - the props for this component
* `:directives` - any directives to be applied to this tag
* `:children` - the tag children
* `:meta` - compilation meta data
* `:debug` - keyword list indicating when debug information should be printed during compilation
"""
defstruct [:module, :type, :props, :dynamic_props, :slot_entries, :meta, debug: [], directives: []]
@type t :: %__MODULE__{
module: module() | Surface.AST.AttributeExpr.t(),
debug: list(atom()),
type: module() | :dynamic_live,
props: list(Surface.AST.Attribute.t()),
dynamic_props: Surface.AST.DynamicAttribute.t(),
directives: list(Surface.AST.Directive.t()),
slot_entries: %{
:default => list(Surface.AST.SlotEntry.t() | Surface.AST.SlotableComponent.t()),
optional(atom()) => list(Surface.AST.SlotEntry.t() | Surface.AST.SlotableComponent.t())
},
meta: Surface.AST.Meta.t()
}
end
defmodule Surface.AST.FunctionComponent do
@moduledoc """
An AST node representing a function component
## Properties
* `:module` - the component module
* `:fun` - the render function
* `:type` - the type of function (:local or :remote)
* `:props` - the props for this component
* `:directives` - any directives to be applied to this tag
* `:children` - the tag children
* `:meta` - compilation meta data
* `:debug` - keyword list indicating when debug information should be printed during compilation
"""
defstruct [:module, :fun, :type, :props, :dynamic_props, :slot_entries, :meta, debug: [], directives: []]
@type t :: %__MODULE__{
module: module() | Surface.AST.AttributeExpr.t(),
fun: atom() | Surface.AST.AttributeExpr.t() | nil,
debug: list(atom()),
type: :local | :remote | :dynamic,
props: list(Surface.AST.Attribute.t()),
dynamic_props: Surface.AST.DynamicAttribute.t(),
directives: list(Surface.AST.Directive.t()),
slot_entries: %{
:default => list(Surface.AST.SlotEntry.t() | Surface.AST.SlotableComponent.t()),
optional(atom()) => list(Surface.AST.SlotEntry.t() | Surface.AST.SlotableComponent.t())
},
meta: Surface.AST.Meta.t()
}
end
defmodule Surface.AST.MacroComponent do
@moduledoc """
An AST node representing a macro component
## Properties
* `:module` - the component module
* `:attributes` - the specified attributes
* `:directives` - any directives to be applied to this macro
* `:children` - the children after the macro is expanded
* `:meta` - compilation meta data
* `:debug` - keyword list indicating when debug information should be printed during compilation
"""
defstruct [:module, :name, :attributes, :children, :meta, debug: [], directives: []]
@type t :: %__MODULE__{
module: module(),
debug: list(atom()),
name: binary(),
attributes: list(Surface.AST.Attribute.t()),
directives: list(Surface.AST.Directive.t()),
children: list(Surface.AST.t()),
meta: Surface.AST.Meta.t()
}
end
defmodule Surface.AST.SlotableComponent do
@moduledoc """
An AST node representing a standard HTML tag
## Properties
* `:module` - the component module
* `:type` - the type of component (i.e. Surface.LiveComponent vs Surface.Component)
* `:slot` - the name of the slot that this component is for
* `:let` - the `:let` expression
* `:props` - the props for this component
* `:directives` - any directives to be applied to this tag
* `:children` - the tag children
* `:meta` - compilation meta data
* `:debug` - keyword list indicating when debug information should be printed during compilation
"""
defstruct [
:module,
:slot,
:type,
:let,
:props,
:dynamic_props,
:slot_entries,
:meta,
debug: [],
directives: []
]
@type t :: %__MODULE__{
module: module(),
debug: list(atom()),
type: module(),
slot: atom(),
let: Surface.AST.AttributeExpr.t() | nil,
props: list(Surface.AST.Attribute.t()),
dynamic_props: Surface.AST.DynamicAttribute.t(),
directives: list(Surface.AST.Directive.t()),
slot_entries: %{
:default => list(Surface.AST.SlotEntry.t()),
optional(atom()) => list(Surface.AST.SlotEntry.t())
},
meta: Surface.AST.Meta.t()
}
end
defmodule Surface.AST do
alias __MODULE__
@type t ::
AST.Literal.t()
| AST.Interpolation.t()
| AST.Expr.t()
| AST.Tag.t()
| AST.VoidTag.t()
| AST.SlotEntry.t()
| AST.Slot.t()
| AST.If.t()
| AST.For.t()
| AST.Container.t()
| AST.Component.t()
| AST.FunctionComponent.t()
| AST.MacroComponent.t()
| AST.SlotableComponent.t()
| AST.Error.t()
def find_attribute_value(attributes, name) do
Enum.find_value(attributes, fn
%AST.Attribute{name: ^name, value: value} -> value
_ -> nil
end)
end
def has_attribute?(attributes, name) do
Enum.any?(attributes, fn %{name: attr_name} -> attr_name == name end)
end
def pop_attributes_values_as_map(attributes, names) do
initial = {Map.new(names, &{&1, nil}), []}
{map, others} =
Enum.reduce(attributes, initial, fn %AST.Attribute{name: name, value: value} = attr, {map, others} ->
if name in names do
{Map.put(map, name, value), others}
else
{map, [attr | others]}
end
end)
{map, Enum.reverse(others)}
end
def pop_attributes_as_map(attributes, names) do
initial = {Map.new(names, &{&1, nil}), []}
{map, others} =
Enum.reduce(attributes, initial, fn
%AST.Attribute{name: name} = attr, {map, others} ->
if name in names do
{Map.put(map, name, attr), others}
else
{map, [attr | others]}
end
attr, {map, others} ->
{map, [attr | others]}
end)
{map, Enum.reverse(others)}
end
end