1
+ # IEx needs to treat TokenMissingError error in a special way, so this one is
2
+ # used only to distinguish from TokenMissingError in the rescue clause
3
+ defexception IExTokenMissingError , [ message: nil ]
4
+
1
5
defmodule IEx.Server do
2
6
@ moduledoc false
3
7
@@ -13,18 +17,39 @@ defmodule IEx.Server do
13
17
cache = config . cache
14
18
code = cache ++ io_get ( config )
15
19
20
+ file = nil # this needs to be config.scope.file
21
+
16
22
new_config =
17
23
try do
24
+ # Instead of doing just `eval`, we first parse the expression to see if
25
+ # it's well formed. If parsing succeeds, we evaluate the AST as usual.
26
+ #
27
+ # If parsing fails, this might be a TokenMissingError which we treat in
28
+ # a special way (to allow for continuation of an expression on the next
29
+ # line in IEx). In case of any other error, we let :elixir_translator
30
+ # to re-raise it.
18
31
{ result , new_binding , scope } =
19
- :elixir . eval ( code , config . binding , counter , config . scope )
32
+ case :elixir_translator . forms ( code , counter , file , [ ] ) do
33
+ { :ok , forms } ->
34
+ :elixir . eval_forms ( forms , config . binding , config . scope )
35
+
36
+ { :error , { line , error , token } } ->
37
+ if token == [ ] do
38
+ # Let the rescue clause catch this error to wait for more input
39
+ raise IExTokenMissingError [ ]
40
+ else
41
+ # Encountered malformed expression
42
+ :elixir_translator . parse_error ( line , file , error , token )
43
+ end
44
+ end
20
45
21
46
io_put result
22
47
23
48
config = config . result ( result )
24
49
update_history ( config . cache ( code ) . scope ( nil ) )
25
50
config . update_counter ( & 1 + 1 ) . cache ( '' ) . binding ( new_binding ) . scope ( scope )
26
51
rescue
27
- TokenMissingError ->
52
+ IExTokenMissingError ->
28
53
config . cache ( code )
29
54
exception ->
30
55
print_stacktrace System . stacktrace , fn ->
@@ -84,4 +109,4 @@ defmodule IEx.Server do
84
109
defp remote_prefix do
85
110
if node == node ( :erlang . group_leader ) , do: "iex" , else: "rem"
86
111
end
87
- end
112
+ end
0 commit comments