/
engine.ex
221 lines (172 loc) · 5.56 KB
/
engine.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
defmodule EEx.Engine do
@moduledoc ~S"""
Basic EEx engine that ships with Elixir.
An engine needs to implement all callbacks below.
This module also ships with a default engine implementation
you can delegate to. See `EEx.SmartEngine` as an example.
"""
@type state :: term
@doc """
Called at the beginning of every template.
It must return the initial state.
"""
@callback init(opts :: keyword) :: state
@doc """
Called at the end of every template.
It must return Elixir's quoted expressions for the template.
"""
@callback handle_body(state) :: Macro.t()
@doc """
Called for the text/static parts of a template.
It must return the updated state.
"""
@callback handle_text(state, [line: pos_integer, column: pos_integer], text :: String.t()) ::
state
@doc """
Called for the dynamic/code parts of a template.
The marker is what follows exactly after `<%`. For example,
`<% foo %>` has an empty marker, but `<%= foo %>` has `"="`
as marker. The allowed markers so far are:
* `""`
* `"="`
* `"/"`
* `"|"`
Markers `"/"` and `"|"` are only for use in custom EEx engines
and are not implemented by default. Using them without an
appropriate implementation raises `EEx.SyntaxError`.
It must return the updated state.
"""
@callback handle_expr(state, marker :: String.t(), expr :: Macro.t()) :: state
@doc """
Invoked at the beginning of every nesting.
It must return a new state that is used only inside the nesting.
Once the nesting terminates, the current `state` is resumed.
"""
@callback handle_begin(state) :: state
@doc """
Invokes at the end of a nesting.
It must return Elixir's quoted expressions for the nesting.
"""
@callback handle_end(state) :: Macro.t()
@doc false
@deprecated "Use explicit delegation to EEx.Engine instead"
defmacro __using__(_) do
quote do
@behaviour EEx.Engine
def init(opts) do
EEx.Engine.init(opts)
end
def handle_body(state) do
EEx.Engine.handle_body(state)
end
def handle_begin(state) do
EEx.Engine.handle_begin(state)
end
def handle_end(state) do
EEx.Engine.handle_end(state)
end
def handle_text(state, text) do
EEx.Engine.handle_text(state, [], text)
end
def handle_expr(state, marker, expr) do
EEx.Engine.handle_expr(state, marker, expr)
end
defoverridable EEx.Engine
end
end
@doc """
Handles assigns in quoted expressions.
A warning will be printed on missing assigns.
Future versions will raise.
This can be added to any custom engine by invoking
`handle_assign/1` with `Macro.prewalk/2`:
def handle_expr(state, token, expr) do
expr = Macro.prewalk(expr, &EEx.Engine.handle_assign/1)
super(state, token, expr)
end
"""
@spec handle_assign(Macro.t()) :: Macro.t()
def handle_assign({:@, meta, [{name, _, atom}]}) when is_atom(name) and is_atom(atom) do
line = meta[:line] || 0
quote(line: line, do: EEx.Engine.fetch_assign!(var!(assigns), unquote(name)))
end
def handle_assign(arg) do
arg
end
@doc false
# TODO: Raise on v2.0
@spec fetch_assign!(Access.t(), Access.key()) :: term | nil
def fetch_assign!(assigns, key) do
case Access.fetch(assigns, key) do
{:ok, val} ->
val
:error ->
keys = Enum.map(assigns, &elem(&1, 0))
IO.warn(
"assign @#{key} not available in EEx template. " <>
"Please ensure all assigns are given as options. " <>
"Available assigns: #{inspect(keys)}"
)
nil
end
end
@doc "Default implementation for `c:init/1`."
def init(_opts) do
%{
binary: [],
dynamic: [],
vars_count: 0
}
end
@doc "Default implementation for `c:handle_begin/1`."
def handle_begin(state) do
check_state!(state)
%{state | binary: [], dynamic: []}
end
@doc "Default implementation for `c:handle_end/1`."
def handle_end(quoted) do
handle_body(quoted)
end
@doc "Default implementation for `c:handle_body/1`."
def handle_body(state) do
check_state!(state)
%{binary: binary, dynamic: dynamic} = state
binary = {:<<>>, [], Enum.reverse(binary)}
dynamic = [binary | dynamic]
{:__block__, [], Enum.reverse(dynamic)}
end
@doc "Default implementation for `c:handle_text/3`."
def handle_text(state, _meta, text) do
check_state!(state)
%{binary: binary} = state
%{state | binary: [text | binary]}
end
@doc "Default implementation for `c:handle_expr/3`."
def handle_expr(state, "=", ast) do
check_state!(state)
%{binary: binary, dynamic: dynamic, vars_count: vars_count} = state
var = Macro.var(:"arg#{vars_count}", __MODULE__)
ast =
quote do
unquote(var) = String.Chars.to_string(unquote(ast))
end
segment =
quote do
unquote(var) :: binary
end
%{state | dynamic: [ast | dynamic], binary: [segment | binary], vars_count: vars_count + 1}
end
def handle_expr(state, "", ast) do
%{dynamic: dynamic} = state
%{state | dynamic: [ast | dynamic]}
end
def handle_expr(_state, marker, _ast) when marker in ["/", "|"] do
raise EEx.SyntaxError,
"unsupported EEx syntax <%#{marker} %> (the syntax is valid but not supported by the current EEx engine)"
end
defp check_state!(%{binary: _, dynamic: _, vars_count: _}), do: :ok
defp check_state!(state) do
raise "unexpected EEx.Engine state: #{inspect(state)}. " <>
"This typically means a bug or an outdated EEx.Engine or tool"
end
end