forked from ErlyORM/boss_db
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboss_compiler.erl
247 lines (231 loc) · 10.5 KB
/
boss_compiler.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
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
-module(boss_compiler).
-export([compile/1, compile/2, parse/3]).
%% @spec compile( File::string() ) -> {ok, Module} | {error, Reason}
compile(File) ->
compile(File, []).
compile(File, Options) ->
IncludeDirs = proplists:get_value(include_dirs, Options, []),
TokenTransform = proplists:get_value(token_transform, Options),
case parse(File, TokenTransform, IncludeDirs) of
{ok, Forms, TokenInfo} ->
CompilerOptions = proplists:get_value(compiler_options, Options,
[verbose, return_errors]),
NewForms = case proplists:get_value(pre_revert_transform, Options) of
undefined ->
Forms;
TransformFun when is_function(TransformFun) ->
case erlang:fun_info(TransformFun, arity) of
{arity, 1} ->
TransformFun(Forms);
{arity, 2} ->
TransformFun(Forms, TokenInfo)
end
end,
OTPVersion = erlang:system_info(otp_release),
{Version, _Rest} = string:to_integer(string:sub_string(OTPVersion, 2, 3)),
{NewNewForms, BossDBParseTransforms} = case Version of
Version when Version >= 16 ->
% OTP Version starting with R16A needs boss_db_pmod_pt
% boss_db_pmod_pt needs the form to end with {eof, 0} tagged tupple
NewForms1 = NewForms ++ [{eof,0}],
% boss_db_pmod_pt needs the form to be in "new" format
{erl_syntax:revert_forms(erl_syntax:revert(NewForms1)), [boss_db_pmod_pt, boss_db_pt]};
_ ->
{erl_syntax:revert(NewForms), [boss_db_pt]}
end,
ParseTransforms = BossDBParseTransforms ++ proplists:get_value(parse_transforms, Options, []),
RevertedForms = lists:foldl(fun(Mod, Acc) ->
Mod:parse_transform(Acc, CompilerOptions)
end, NewNewForms, ParseTransforms),
case compile_forms(RevertedForms, File, CompilerOptions) of
{ok, Module, Bin} ->
ok = case proplists:get_value(out_dir, Options) of
undefined -> ok;
OutDir ->
BeamFile = filename:join([OutDir, lists:concat([Module, ".beam"])]),
file:write_file(BeamFile, Bin)
end,
{ok, Module};
Error ->
Error
end;
Error ->
Error
end.
compile_forms(Forms, File, Options) ->
case compile:forms(Forms, Options) of
{ok, Module1, Bin} ->
code:purge(Module1),
case code:load_binary(Module1, File, Bin) of
{module, _} -> {ok, Module1, Bin};
_ -> {error, lists:concat(["code reload failed: ", Module1])}
end;
OtherError ->
OtherError
end.
parse(File, TokenTransform, IncludeDirs) when is_list(File) ->
case file:read_file(File) of
{ok, FileContents} ->
parse_text(File, FileContents, TokenTransform, IncludeDirs);
Error ->
Error
end;
parse(File, TokenTransform, IncludeDirs) when is_binary(File) ->
parse_text(undefined, File, TokenTransform, IncludeDirs).
parse_text(FileName, FileContents, TokenTransform, IncludeDirs) ->
case scan_transform(FileContents) of
{ok, Tokens} ->
{NewTokens, TokenInfo} = case TokenTransform of
undefined ->
{Tokens, undefined};
TransformFun when is_function(TransformFun) ->
TransformFun(Tokens)
end,
case aleppo:process_tokens(NewTokens, [{file, FileName}, {include, IncludeDirs}]) of
{ok, ProcessedTokens} ->
% We have to flatten the token locations because the Erlang parser
% has a bug that chokes on {Line, Col} locations in typed record
% definitions
TokensWithOnlyLineNumbers = flatten_token_locations(ProcessedTokens),
{Forms, Errors} = parse_tokens(TokensWithOnlyLineNumbers, FileName),
case length(Errors) of
0 ->
{ok, Forms, TokenInfo};
_ ->
Errors1 = lists:map(fun(File) ->
{File, proplists:get_all_values(File, Errors)}
end, proplists:get_keys(Errors)),
{error, Errors1, []}
end;
{error, ErrorInfo} ->
{error, {FileName, [ErrorInfo]}}
end;
{error, ErrorInfo} ->
{error, {FileName, [ErrorInfo]}}
end.
parse_tokens(Tokens, FileName) ->
parse_tokens(Tokens, [], [], [], FileName).
parse_tokens([], _, FormAcc, ErrorAcc, _) ->
{lists:reverse(FormAcc), lists:reverse(ErrorAcc)};
parse_tokens([{dot, _}=Token|Rest], TokenAcc, FormAcc, ErrorAcc, FileName) ->
case erl_parse:parse_form(lists:reverse([Token|TokenAcc])) of
{ok, {attribute, _, file, {NewFileName, _Line}} = AbsForm} ->
parse_tokens(Rest, [], [AbsForm|FormAcc], ErrorAcc, NewFileName);
{ok, {attribute, La, record, {Record, Fields}} = AbsForm} ->
case epp:normalize_typed_record_fields(Fields) of
{typed, NewFields} ->
parse_tokens(Rest, [], lists:reverse([
{attribute, La, record, {Record, NewFields}},
{attribute, La, type, {{record, Record}, Fields, []}}],
FormAcc), ErrorAcc, FileName);
not_typed ->
parse_tokens(Rest, [], [AbsForm|FormAcc], ErrorAcc, FileName)
end;
{ok, AbsForm} ->
parse_tokens(Rest, [], [AbsForm|FormAcc], ErrorAcc, FileName);
{error, ErrorInfo} ->
parse_tokens(Rest, [], FormAcc, [{FileName, ErrorInfo}|ErrorAcc], FileName)
end;
parse_tokens([{eof, Location}], TokenAcc, FormAcc, ErrorAcc, FileName) ->
parse_tokens([], TokenAcc, [{eof, Location}|FormAcc], ErrorAcc, FileName);
parse_tokens([Token|Rest], TokenAcc, FormAcc, ErrorAcc, FileName) ->
parse_tokens(Rest, [Token|TokenAcc], FormAcc, ErrorAcc, FileName).
scan_transform(FileContents) ->
scan_transform(FileContents, {1, 1}).
scan_transform([], StartLocation) ->
{ok, [{eof, StartLocation}]};
scan_transform(FileContents, StartLocation) when is_binary(FileContents) ->
scan_transform(unicode:characters_to_list(FileContents), StartLocation);
scan_transform(FileContents, StartLocation) ->
case erl_scan:tokens([], FileContents, StartLocation) of
{done, Return, Rest} ->
case Return of
{ok, Tokens, EndLocation} ->
case scan_transform(Rest, EndLocation) of
{ok, NewTokens} ->
{ok, Tokens ++ NewTokens};
Err -> Err
end;
{eof, EndLocation} ->
{ok, [{eof, EndLocation}]};
{error, ErrorInfo, _EndLocation} ->
case ErrorInfo of
{ErrorLocation, erl_scan, {illegal,character}} ->
{Truncated, IllegalChar, Rest1} = cut_at_location(ErrorLocation, FileContents, StartLocation),
case transform_char(IllegalChar) of
{ok, String} ->
Transformed = Truncated ++ String ++ Rest1,
scan_transform(Transformed, StartLocation);
error ->
{error, ErrorInfo}
end;
ErrorInfo ->
{error, ErrorInfo}
end
end;
{more, Continuation1} ->
{done, Return, eof} = erl_scan:tokens(Continuation1, eof, eof),
case Return of
{ok, Tokens, _EndLocation} ->
{ok, Tokens};
{eof, EndLocation} ->
{ok, [{eof, EndLocation}]};
{error, ErrorInfo, _EndLocation} ->
{error, ErrorInfo}
end
end.
transform_char(8800) -> % ≠
{ok, ",'not_equals',"};
transform_char(8804) -> % ≤
{ok, ",'le',"};
transform_char(8805) -> % ≥
{ok, ",'ge',"};
transform_char(8712) -> % ∈
{ok, ",'in',"};
transform_char(8713) -> % ∉
{ok, ",'not_in',"};
transform_char(8715) -> % ∋
{ok, ",'contains',"};
transform_char(8716) -> % ∌
{ok, ",'not_contains',"};
transform_char(8764) -> % ∼
{ok, ",'matches',"};
transform_char(8769) -> % ≁
{ok, ",'not_matches',"};
transform_char(8839) -> % ⊇
{ok, ",'contains_all',"};
transform_char(8841) -> % ⊉
{ok, ",'not_contains_all',"};
transform_char(8745) -> % ∩
{ok, ",'contains_any',"};
transform_char(8869) -> % ⊥
{ok, ",'contains_none',"};
transform_char(10178) -> % ⊥ look-alike
{ok, ",'contains_none',"};
transform_char(Char) when Char > 127 ->
Bytes = binary_to_list(unicode:characters_to_binary([Char], unicode, utf8)),
{ok, lists:flatten(lists:map(fun(Byte) ->
io_lib:format("\\x{~.16B}", [Byte])
end, Bytes))};
transform_char(_) ->
error.
cut_at_location({CutLine, CutCol}, FileContents, {StartLine, StartCol}) ->
cut_at_location1({CutLine, CutCol}, FileContents, {StartLine, StartCol}, []).
cut_at_location1(_, [], _, Acc) ->
{lists:reverse(Acc), 0, ""};
cut_at_location1({Line, Col}, [C|Rest], {Line, Col}, Acc) ->
{lists:reverse(Acc), C, Rest};
cut_at_location1({Line, Col}, [C|Rest], {ThisLine, _}, Acc) when C =:= $\n ->
cut_at_location1({Line, Col}, Rest, {ThisLine + 1, 1}, [C|Acc]);
cut_at_location1({Line, Col}, [C|Rest], {ThisLine, ThisCol}, Acc) ->
cut_at_location1({Line, Col}, Rest, {ThisLine, ThisCol + 1}, [C|Acc]).
flatten_token_locations(Tokens) ->
flatten_token_locations1(Tokens, []).
flatten_token_locations1([], Acc) ->
lists:reverse(Acc);
flatten_token_locations1([{Type, {Line, _Col}}|Rest], Acc) ->
flatten_token_locations1(Rest, [{Type, Line}|Acc]);
flatten_token_locations1([{Type, {Line, _Col}, Extra}|Rest], Acc) ->
flatten_token_locations1(Rest, [{Type, Line, Extra}|Acc]);
flatten_token_locations1([Other|Rest], Acc) ->
flatten_token_locations1(Rest, [Other|Acc]).