forked from elixir-lang/elixir
/
elixir_interpolation.erl
149 lines (107 loc) · 5.29 KB
/
elixir_interpolation.erl
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
% Handle string and string-like interpolations.
-module(elixir_interpolation).
-export([extract/4, unescape_chars/1, unescape_chars/2,
unescape_tokens/1, unescape_tokens/2, unescape_map/1]).
-define(is_octal(S), S >= $0 andalso S =< $7).
-include("elixir.hrl").
%% Extract string interpolations
extract(Line, Interpol, String, Last) ->
extract(Line, Interpol, String, [], [], [], Last).
extract(Line, _Interpol, [], Buffer, [], Output, []) ->
finish_extraction(Line, Buffer, Output, []);
extract(Line, _Interpol, [], _Buffer, [], _Output, Last) ->
{ error, { Line, io_lib:format("missing string terminator, expected ~ts", [[Last]]), [] } };
extract(Line, _Interpol, [Last|Remaining], Buffer, [], Output, Last) ->
finish_extraction(Line, Buffer, Output, Remaining);
extract(Line, _Interpol, [Last], _Buffer, Search, _Output, Last) ->
{ error, { Line, io_lib:format("unexpected end of string, expected ~ts", [[hd(Search)]]), [Last] } };
extract(Line, Interpol, [$\n|Rest], Buffer, Search, Output, Last) ->
extract(Line+1, Interpol, Rest, [$\n|Buffer], Search, Output, Last);
extract(Line, Interpol, [$\\, $#, ${|Rest], Buffer, [], Output, Last) ->
extract(Line, Interpol, Rest, [${,$#|Buffer], [], Output, Last);
extract(Line, Interpol, [$\\,Char|Rest], Buffer, [], Output, Last) ->
extract(Line, Interpol, Rest, [Char,$\\|Buffer], [], Output, Last);
extract(Line, true, [$#, ${|Rest], Buffer, [], Output, Last) ->
NewOutput = build_interpol(s, Line, Buffer, Output),
extract(Line, true, Rest, [], [$}], NewOutput, Last);
extract(Line, true, [$}|Rest], Buffer, [$}], Output, Last) ->
NewOutput = build_interpol(i, Line, Buffer, Output),
extract(Line, true, Rest, [], [], NewOutput, Last);
%% Check for available separators "", {}, [] and () inside interpolation
extract(Line, Interpol, [C|Rest], Buffer, [C|Search], Output, Last) when C == $); C == $]; C == $}; C == $"; C == $' ->
extract(Line, Interpol, Rest, [C|Buffer], Search, Output, Last);
extract(Line, Interpol, [C|Rest], Buffer, [_|_] = Search, Output, Last) when C == $"; C == $' ->
extract(Line, Interpol, Rest, [C|Buffer], [C|Search], Output, Last);
extract(Line, Interpol, [${|Rest], Buffer, [_|_] = Search, Output, Last) ->
extract(Line, Interpol, Rest, [${|Buffer], [$}|Search], Output, Last);
extract(Line, Interpol, [$[|Rest], Buffer, [_|_] = Search, Output, Last) ->
extract(Line, Interpol, Rest, [$[|Buffer], [$]|Search], Output, Last);
extract(Line, Interpol, [$(|Rest], Buffer, [_|_] = Search, Output, Last) ->
extract(Line, Interpol, Rest, [$(|Buffer], [$)|Search], Output, Last);
%% Else
extract(Line, Interpol, [Char|Rest], Buffer, Search, Output, Last) ->
extract(Line, Interpol, Rest, [Char|Buffer], Search, Output, Last).
%% Unescape a series of tokens as returned by extract.
unescape_tokens(Tokens) ->
unescape_tokens(Tokens, fun unescape_map/1).
unescape_tokens(Tokens, Map) ->
[unescape_token(Token, Map) || Token <- Tokens].
unescape_token(Token, Map) when is_binary(Token) -> unescape_chars(Token, Map);
unescape_token(Other, _Map) -> Other.
% Unescape chars. For instance, "\" "n" (two chars) needs to be converted to "\n" (one char).
unescape_chars(String) -> unescape_chars(String, fun unescape_map/1).
-define(to_octal(List),
<<(list_to_integer(List, 8))/integer, (unescape_chars(Rest, Map))/binary>>
).
unescape_chars(<<$\\,A,B,C,Rest/binary>>, Map) when ?is_octal(A), ?is_octal(B), ?is_octal(C) ->
?to_octal([A,B,C]);
unescape_chars(<<$\\,A,B,Rest/binary>>, Map) when ?is_octal(A), ?is_octal(B) ->
?to_octal([A,B]);
unescape_chars(<<$\\,A,Rest/binary>>, Map) when ?is_octal(A) ->
?to_octal([A]);
unescape_chars(<<$\\,Escaped,Rest/binary>>, Map) ->
case Map(Escaped) of
false -> <<$\\,Escaped,(unescape_chars(Rest, Map))/binary>>;
Other -> <<Other,(unescape_chars(Rest, Map))/binary>>
end;
unescape_chars(<<Char, Rest/binary>>, Map) ->
<<Char, (unescape_chars(Rest, Map))/binary>>;
unescape_chars(<<>>, _Map) -> <<>>.
% Unescape Helpers
unescape_map($b) -> $\b;
unescape_map($d) -> $\d;
unescape_map($e) -> $\e;
unescape_map($f) -> $\f;
unescape_map($n) -> $\n;
unescape_map($r) -> $\r;
unescape_map($s) -> $\s;
unescape_map($t) -> $\t;
unescape_map($v) -> $\v;
unescape_map(E) -> E.
% Extract Helpers
finish_extraction(Line, Buffer, Output, Remaining) ->
case build_interpol(s, Line, Buffer, Output) of
[] -> Final = [<<>>];
Final -> []
end,
{ Line, lists:reverse(Final), Remaining }.
build_interpol(_Kind, _Line, [], Output) ->
Output;
build_interpol(s, _Line, Buffer, Output) ->
[unicode:characters_to_binary(lists:reverse(Buffer))|Output];
build_interpol(i, Line, Buffer, Output) ->
[wrap_interpol(Line, forms(lists:reverse(Buffer), Line))| Output].
wrap_interpol(_Line, Form) when is_binary(Form) ->
Form;
wrap_interpol(Line, Form) ->
{ '|', Line, [{ { '.', Line, ['__MAIN__.Binary.Chars', to_binary] }, Line, [Form]}, binary]}.
forms(String, StartLine) ->
case elixir_tokenizer:tokenize(String, StartLine) of
{ok, Tokens} ->
case elixir_parser:parse(Tokens) of
{ok, [Forms]} when not is_list(Forms) -> Forms;
{ok, Forms} -> { '__block__', StartLine, Forms };
{error, {Line, _, [Error, Token]}} -> throw({ interpolation_error, { Line, Error, Token } })
end;
{error, {Line, Error, Token}} -> throw({ interpolation_error, { Line, Error, Token } })
end.