-
Notifications
You must be signed in to change notification settings - Fork 2
/
CodeGen.roc
214 lines (181 loc) · 6.53 KB
/
CodeGen.roc
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
module [generate]
import Parser exposing [Node]
generate : List { name : Str, nodes : List Node } -> Str
generate = \templates ->
functions =
List.map templates renderTemplate
|> Str.joinWith "\n\n"
names =
templates
|> List.map \template -> " $(template.name),"
|> Str.joinWith "\n"
imports =
templates
|> List.walk (Set.empty {}) \acc, template -> Set.union acc (moduleImports template)
|> Set.toList
|> List.map \moduleImport -> "import $(moduleImport)"
|> Str.joinWith "\n"
# If we included escapeHtml in every module, templates without interpolations would
# trigger a warning when running roc check for escapeHtml not being used.
requiresEscapeHtml = List.any templates \{ nodes } ->
containsInterpolation nodes
"""
## Generated by RTL https://github.com/isaacvando/rtl
module [
$(names)
]
$(imports)
$(functions)
$(if requiresEscapeHtml then escapeHtml else "")
"""
moduleImports = \template ->
template.nodes
|> List.walk (Set.empty {}) \acc, n ->
when n is
ModuleImport m -> Set.insert acc m
_ -> acc
containsInterpolation : List Node -> Bool
containsInterpolation = \nodes ->
List.any nodes \node ->
when node is
Interpolation _ -> Bool.true
Conditional { trueBranch, falseBranch } ->
containsInterpolation trueBranch || containsInterpolation falseBranch
Sequence { body } -> containsInterpolation body
WhenIs { cases } ->
List.any cases \case ->
containsInterpolation case.branch
Text _ | RawInterpolation _ | ModuleImport _ -> Bool.false
escapeHtml =
"""
escapeHtml : Str -> Str
escapeHtml = \\input ->
input
|> Str.replaceEach "&" "&"
|> Str.replaceEach "<" "<"
|> Str.replaceEach ">" ">"
|> Str.replaceEach "\\"" """
|> Str.replaceEach "'" "'"
"""
RenderNode : [
Text Str,
Conditional { condition : Str, trueBranch : List RenderNode, falseBranch : List RenderNode },
Sequence { item : Str, list : Str, body : List RenderNode },
WhenIs { expression : Str, cases : List { pattern : Str, branch : List RenderNode } },
]
renderTemplate : { name : Str, nodes : List Node } -> Str
renderTemplate = \{ name, nodes } ->
body =
condense nodes
|> renderNodes
# We check if the model was used in the template so that we can ignore the parameter
# if it was not used to prevent an unused field warning from showing up.
"""
$(name) = \\$(if isModelUsedInList nodes then "" else "_")model ->
$(body)
"""
renderNodes : List RenderNode -> Str
renderNodes = \nodes ->
when List.map nodes toStr is
[] -> "\"\"" |> indent
[elem] -> elem
blocks ->
list = blocks |> Str.joinWith ",\n"
"""
[
$(list)
]
|> Str.joinWith ""
"""
|> indent
toStr = \node ->
block =
when node is
Text t ->
"""
\"""
$(t)
\"""
"""
Conditional { condition, trueBranch, falseBranch } ->
"""
if $(condition) then
$(renderNodes trueBranch)
else
$(renderNodes falseBranch)
"""
Sequence { item, list, body } ->
"""
List.map $(list) \\$(item) ->
$(renderNodes body)
|> Str.joinWith ""
"""
WhenIs { expression, cases } ->
branches =
List.map cases \{ pattern, branch } ->
"""
$(pattern) ->
$(renderNodes branch)
"""
|> Str.joinWith "\n"
|> indent
"""
when $(expression) is
$(branches)
"""
indent block
condense : List Node -> List RenderNode
condense = \nodes ->
List.map nodes \node ->
when node is
RawInterpolation i -> Text "\$($(i))"
Interpolation i -> Text "\$($(i) |> escapeHtml)"
Text t ->
# Escape Roc string interpolations from the template
escaped = Str.replaceEach t "$" "\\$"
Text escaped
Sequence { item, list, body } -> Sequence { item, list, body: condense body }
ModuleImport _ -> Text ""
Conditional { condition, trueBranch, falseBranch } ->
Conditional {
condition,
trueBranch: condense trueBranch,
falseBranch: condense falseBranch,
}
WhenIs { expression, cases } ->
WhenIs {
expression,
cases: List.map cases \{ pattern, branch } ->
{ pattern, branch: condense branch },
}
|> List.walk [] \state, elem ->
when (state, elem) is
([.. as rest, Text x], Text y) ->
combined = Str.concat x y |> Text
rest |> List.append combined
_ -> List.append state elem
isModelUsedInList = \nodes ->
List.any nodes isModelUsedInNode
# We can't determine with full certainty if the model was used without parsing the Roc code that
# is used on the template, which we don't do. This is a heuristic that just checks if any of the spots
# that could reference the model contain "model". So a string literal that contains "model" could create
# a false positive, but this isn't a big deal.
isModelUsedInNode = \node ->
containsModel = \str ->
Str.contains str "model"
when node is
Interpolation i | RawInterpolation i -> containsModel i
Conditional { condition, trueBranch, falseBranch } ->
containsModel condition || isModelUsedInList trueBranch || isModelUsedInList falseBranch
Sequence { list, body } -> containsModel list || isModelUsedInList body
WhenIs { expression, cases } ->
containsModel expression
|| List.any cases \case ->
isModelUsedInList case.branch
Text _ | ModuleImport _ -> Bool.false
indent : Str -> Str
indent = \input ->
Str.split input "\n"
|> List.map \str ->
Str.concat " " str
|> Str.joinWith "\n"