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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
|
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 1997-2009. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%%
%% %CopyrightEnd%
%%
%%
-module(icscan).
-export([scan/2]).
-include("ic.hrl").
%%----------------------------------------------------------------------
%%----------------------------------------------------------------------
-import(lists, [reverse/1]).
scan(G, File) ->
PL = call_preproc(G, File),
call_scan(G, PL).
call_preproc(G, File) ->
case ic_options:get_opt(G, use_preproc) of
true ->
icpreproc:preproc(G, File);
false ->
case catch file:read_file(File) of
{ok, Bin} ->
binary_to_list(Bin);
Other ->
exit(Other)
end
end.
call_scan(G, PL) ->
BE = ic_options:get_opt(G, be),
RSL = scan(G, BE, PL, 1, []),
lists:reverse(RSL).
%% Guard macros used at top scan functions only
-define(is_number(X), X >= $0 , X =< $9).
-define(is_upper(X), X >= $A , X =< $Z).
-define(is_lower(X), X >= $a, X =< $z).
-define(is_hex_uc(X), X >= $A , X =< $F).
-define(is_hex_lc(X), X >= $a , X =< $f).
-define(is_octal(X), X >=$0, X =< $7).
%% Handle:
%% const wchar aWChar = L'X';
scan(G, BE, [$L, $'|Str], Line, Out) ->
scan_const(G, BE, wchar, Str, [], Line, Out);
scan(G, BE, [$L, $"|Str], Line, Out) ->
scan_const(G, BE, wstring, Str, [], Line, Out);
scan(G, BE, [$_, X|Str], Line, Out) when ?is_upper(X) ->
scan_name(G, BE, Str, [X], false, Line, Out);
scan(G, BE, [$_, X|Str], Line, Out) when ?is_lower(X) ->
scan_name(G, BE, Str, [X], false, Line, Out);
scan(G, BE, [X|Str], Line, Out) when ?is_upper(X) ->
scan_name(G, BE, Str, [X], true, Line, Out);
scan(G, BE, [X|Str], Line, Out) when ?is_lower(X) ->
scan_name(G, BE, Str, [X], true, Line, Out);
scan(G, BE, [X|Str], Line, Out) when ?is_number(X) ->
scan_number(G, BE, Str, [X], Line, Out);
scan(G, BE, [9| T], Line, Out) -> scan(G, BE, T, Line, Out);
scan(G, BE, [32| T], Line, Out) -> scan(G, BE, T, Line, Out);
scan(G, BE, [$\r|Str], Line, Out) ->
scan(G, BE, Str, Line, Out);
scan(G, BE, [$\n|Str], Line, Out) ->
scan(G, BE, Str, Line+1, Out);
scan(G, BE, [$:, $: | Str], Line, Out) ->
scan(G, BE, Str, Line, [{'::', Line} | Out]);
scan(G, BE, [$/, $/ | Str], Line, Out) ->
Rest = skip_to_nl(Str),
scan(G, BE, Rest, Line, Out);
scan(G, BE, [$/, $* | Str], Line, Out) ->
Rest = skip_comment(Str),
scan(G, BE, Rest, Line, Out);
scan(G, BE, [$", $\\|Str], Line, Out) ->
scan_const(G, BE, string, [$\\|Str], [], Line, Out);
scan(G, BE, [$"|Str], Line, Out) ->
scan_const(G, BE, string, Str, [], Line, Out);
scan(G, BE, [$', $\\|Str], Line, Out) ->
scan_const(G, BE, char, [$\\|Str], [], Line, Out);
scan(G, BE, [$'|Str], Line, Out) ->
scan_const(G, BE, char, Str, [], Line, Out);
scan(G, BE, [$\\|Str], Line, Out) ->
scan_const(G, BE, escaped, [$\\|Str], [], Line, Out);
scan(G, BE, [$. | Str], Line, Out) ->
scan_frac(G, BE, Str, [$.], Line, Out);
scan(G, BE, [$# | Str], Line, Out) ->
scan_preproc(G, BE, Str, Line, Out);
scan(G, BE, [$<, $< | Str], Line, Out) ->
scan(G, BE, Str, Line, [{'<<', Line} | Out]);
scan(G, BE, [$>, $> | Str], Line, Out) ->
scan(G, BE, Str, Line, [{'>>', Line} | Out]);
scan(G, BE, [C|Str], Line, Out) ->
scan(G, BE, Str, Line, [{list_to_atom([C]), Line} | Out]);
scan(_G, _BE, [], _Line, Out) ->
Out.
scan_number(G, BE, [X|Str], [$0], Line, Out) when X == $X ; X ==$x ->
case Str of
[D|_TmpStr] when ?is_number(D); ?is_hex_uc(D); ?is_hex_lc(D) ->
{Num,Rest} = scan_hex_number(Str,0),
scan(G, BE, Rest, Line, [{'<integer_literal>', Line,
integer_to_list(Num)} | Out]);
[D|TmpStr] ->
scan(G, BE, TmpStr, Line, [{list_to_atom([D]), Line} | Out])
end;
scan_number(G, BE, Str, [$0], Line, Out) ->
%% If an integer literal starts with a 0 it may indicate that
%% it is represented as an octal number. But, it can also be a fixed
%% type which must use padding to match a fixed typedef. For example:
%% typedef fixed<5,2> fixed52;
%% 123.45d, 123.00d and 023.00d is all valid fixed values.
%% Naturally, a float can be defined as 0.14 or 00.14.
case pre_scan_number(Str, [], octal) of
octal ->
{Num, Rest} = scan_octal_number(Str,0),
scan(G, BE, Rest, Line, [{'<integer_literal>', Line,
integer_to_list(Num)} | Out]);
{fixed, Fixed, Rest} ->
scan(G, BE, Rest, Line, [{'<fixed_pt_literal>', Line, Fixed} | Out]);
float ->
%% Not very likely that someone defines a constant as 00.14 but ...
NewStr = remove_leading_zeroes(Str),
scan(G, BE, NewStr, Line, Out)
end;
scan_number(G, BE, [X|Str], Accum, Line, Out) when ?is_number(X) ->
scan_number(G, BE, Str, [X|Accum], Line, Out);
scan_number(G, BE, [X|Str], Accum, Line, Out) when X==$. ->
scan_frac(G, BE, Str, [X|Accum], Line, Out);
scan_number(G, BE, [X|Str], Accum, Line, Out) when X==$e ; X==$e ->
scan_exp(G, BE, Str, [X|Accum], Line, Out);
scan_number(G, BE, [X|Str], Accum, Line, Out) when X==$D ; X==$d ->
scan(G, BE, Str, Line, [{'<fixed_pt_literal>', Line,
(lists:reverse(Accum))} | Out]);
scan_number(G, BE, Str, Accum, Line, Out) ->
scan(G, BE, Str, Line, [{'<integer_literal>', Line,
(lists:reverse(Accum))} | Out]).
remove_leading_zeroes([$0|Rest]) ->
remove_leading_zeroes(Rest);
remove_leading_zeroes(L) ->
L.
scan_hex_number([X|Rest],Acc) when X >=$a, X =< $f ->
scan_hex_number(Rest,(Acc bsl 4) + (X - $a + 10));
scan_hex_number([X|Rest],Acc) when X >=$A, X =< $F ->
scan_hex_number(Rest,(Acc bsl 4) + (X - $A + 10));
scan_hex_number([X|Rest],Acc) when X >=$0, X =< $9 ->
scan_hex_number(Rest,(Acc bsl 4) + (X-$0));
scan_hex_number(Rest,Acc) ->
{Acc,Rest}.
pre_scan_number([$d|Rest], Acc, _) ->
{fixed, [$0|lists:reverse(Acc)], Rest};
pre_scan_number([$D|Rest], Acc, _) ->
{fixed, [$0|lists:reverse(Acc)], Rest};
pre_scan_number([$.|Rest], Acc, _) ->
%% Actually, we don't know if it's a float since it can be a fixed.
pre_scan_number(Rest, [$.|Acc], float);
pre_scan_number([X|_], _Acc, _) when X == $E ; X ==$e ->
%% Now we now it's a float.
float;
pre_scan_number([X|Rest], Acc, Type) when ?is_number(X) ->
pre_scan_number(Rest, [X|Acc], Type);
pre_scan_number(_Rest, _Acc, Type) ->
%% At this point we know it's a octal or float.
Type.
scan_octal_number([X|Rest],Acc) when ?is_octal(X) ->
scan_octal_number(Rest,(Acc bsl 3) + (X-$0));
scan_octal_number(Rest,Acc) ->
{Acc, Rest}.
%% Floating point number scan.
%%
%% Non trivial scan. A float consists of an integral part, a
%% decimal point, a fraction part, an e or E and a signed integer
%% exponent. Either the integer part or the fraction part but not
%% both may be missing, and either the decimal point or the
%% exponent part but not both may be missing. The exponent part
%% must consist of an e or E and a possibly signed exponent.
%%
%% Analysis shows that "1." ".7" "1e2" ".5e-3" "1.7e2" "1.7e-2"
%% is allowed and "1" ".e9" is not. The sign is only allowed just
%% after an e or E. The scanner reads a number as an integer
%% until it encounters a "." so the integer part only error case
%% will not be caught in the scanner (but rather in expression
%% evaluation)
scan_frac(G, _BE, [$e | _Str], [$.], Line, _Out) ->
ic_error:fatal_error(G, {illegal_float, Line});
scan_frac(G, _BE, [$E | _Str], [$.], Line, _Out) ->
ic_error:fatal_error(G, {illegal_float, Line});
scan_frac(G, BE, Str, Accum, Line, Out) ->
scan_frac2(G, BE, Str, Accum, Line, Out).
scan_frac2(G, BE, [X|Str], Accum, Line, Out) when ?is_number(X) ->
scan_frac2(G, BE, Str, [X|Accum], Line, Out);
scan_frac2(G, BE, [X|Str], Accum, Line, Out) when X==$e ; X==$E ->
scan_exp(G, BE, Str, [X|Accum], Line, Out);
%% The following case is for fixed (e.g. 123.45d).
scan_frac2(G, BE, [X|Str], Accum, Line, Out) when X==$d ; X==$D ->
scan(G, BE, Str, Line, [{'<fixed_pt_literal>', Line,
(lists:reverse(Accum))} | Out]);
scan_frac2(G, BE, Str, Accum, Line, Out) ->
scan(G, BE, Str, Line, [{'<floating_pt_literal>', Line,
(lists:reverse(Accum))} | Out]).
scan_exp(G, BE, [X|Str], Accum, Line, Out) when X==$- ->
scan_exp2(G, BE, Str, [X|Accum], Line, Out);
scan_exp(G, BE, Str, Accum, Line, Out) ->
scan_exp2(G, BE, Str, Accum, Line, Out).
scan_exp2(G, BE, [X|Str], Accum, Line, Out) when ?is_number(X) ->
scan_exp2(G, BE, Str, [X|Accum], Line, Out);
scan_exp2(G, BE, Str, Accum, Line, Out) ->
scan(G, BE, Str, Line, [{'<floating_pt_literal>', Line,
(lists:reverse(Accum))} | Out]).
scan_name(G, BE, [X|Str], Accum, TypeCheck, Line, Out) when ?is_upper(X) ->
scan_name(G, BE, Str, [X|Accum], TypeCheck, Line, Out);
scan_name(G, BE, [X|Str], Accum, TypeCheck, Line, Out) when ?is_lower(X) ->
scan_name(G, BE, Str, [X|Accum], TypeCheck, Line, Out);
scan_name(G, BE, [X|Str], Accum, TypeCheck, Line, Out) when ?is_number(X) ->
scan_name(G, BE, Str, [X|Accum], TypeCheck, Line, Out);
scan_name(G, BE, [$_|Str], Accum, TypeCheck, Line, Out) ->
scan_name(G, BE, Str, [$_|Accum], TypeCheck, Line, Out);
scan_name(G, BE, S, Accum, false, Line, Out) ->
%% The CORBA 2.3 specification allows the user to override typechecking:
%% typedef string _native;
%% interface i {
%% void foo(in _native VT);
%% };
%% BUT, the IFR-id remains the same ("IDL:native:1.0") etc. The reason for
%% this is that one don't have to re-write a large chunk of IDL- and
%% application-code.
scan(G, BE, S, Line, [{'<identifier>', Line, lists:reverse(Accum)} | Out]);
scan_name(G, BE, S, Accum, _, Line, Out) ->
L = lists:reverse(Accum),
X = case is_reserved(L, BE) of
undefined ->
{'<identifier>', Line, L};
Yes ->
{Yes, Line}
end,
scan(G, BE, S, Line, [X | Out]).
%% Shall scan a constant
scan_const(G, BE, string, [$" | Rest], Accum, Line, [{'<string_literal>', _, Str}|Out]) ->
scan(G, BE, Rest, Line,
[{'<string_literal>', Line, Str ++ lists:reverse(Accum)} | Out]);
scan_const(G, BE, string, [$" | Rest], Accum, Line, Out) ->
scan(G, BE, Rest, Line,
[{'<string_literal>', Line, lists:reverse(Accum)} | Out]);
scan_const(G, BE, wstring, [$" | Rest], Accum, Line, [{'<wstring_literal>', _,Wstr}|Out]) -> %% WSTRING
scan(G, BE, Rest, Line,
[{'<wstring_literal>', Line, Wstr ++ lists:reverse(Accum)} | Out]);
scan_const(G, BE, wstring, [$" | Rest], Accum, Line, Out) -> %% WSTRING
scan(G, BE, Rest, Line,
[{'<wstring_literal>', Line, lists:reverse(Accum)} | Out]);
scan_const(G, _BE, string, [], _Accum, Line, Out) -> %% Bad string
ic_error:error(G, {bad_string, Line}),
Out;
scan_const(G, _BE, wstring, [], _Accum, Line, Out) -> %% Bad WSTRING
ic_error:error(G, {bad_string, Line}),
Out;
scan_const(G, BE, char, [$' | Rest], Accum, Line, Out) ->
scan(G, BE, Rest, Line,
[{'<character_literal>', Line, lists:reverse(Accum)} | Out]);
scan_const(G, BE, wchar, [$' | Rest], Accum, Line, Out) -> %% WCHAR
scan(G, BE, Rest, Line,
[{'<wcharacter_literal>', Line, lists:reverse(Accum)} | Out]);
scan_const(G, BE, Mode, [$\\, C | Rest], Accum, Line, Out) ->
case escaped_char(C) of
error ->
ic_error:error(G, {bad_escape_character, Line, C}), %% Bad escape character
scan_const(G, BE, Mode, Rest, [C | Accum], Line, Out);
octal ->
{Num,Rest2} = scan_octal_number([C|Rest], 0),
scan_const(G, BE, Mode, Rest2, [Num|Accum], Line, Out);
hexadecimal ->
{Num,Rest2} = scan_hex_number(Rest, 0),
if
Num > 255 -> %% 16#FF
ic_error:error(G, {bad_escape_character, Line, C}),
scan_const(G, BE, Mode, Rest, [C | Accum], Line, Out);
true ->
scan_const(G, BE, Mode, Rest2, [Num|Accum], Line, Out)
end;
unicode ->
{Num,Rest2} = scan_hex_number(Rest, 0),
if
Num > 65535 -> %% 16#FFFF
ic_error:error(G, {bad_escape_character, Line, C}),
scan_const(G, BE, Mode, Rest, [C | Accum], Line, Out);
true ->
scan_const(G, BE, Mode, Rest2, [Num|Accum], Line, Out)
end;
EC ->
scan_const(G, BE, Mode, Rest, [EC | Accum], Line, Out)
end;
scan_const(G, BE, Mode, [C | Rest], Accum, Line, Out) ->
scan_const(G, BE, Mode, Rest, [C | Accum], Line, Out).
%%
%% Preprocessor output handling
%%
%% gcc outputs a line with line number, file name (within \") and
%% one or more integer flags. The scanner scans the line number,
%% the id and all integers up to nl.
%%
%% NOTE: This will have to be enhanced in order to eat #pragma
%%
scan_preproc(G, BE, Str, Line, Out) ->
{List, Rest} = scan_to_nl(strip(Str), []),
NewLine = get_new_line_nr(strip(List), Line+1, []),
case scan_number(G, BE, List, [], Line, [{'#', Line} | Out]) of
L when is_list(L) ->
scan(G, BE, Rest, NewLine, [{'#', Line} | L])
end.
get_new_line_nr([C|R], Line, Acc) when C>=$0, C=<$9 ->
get_new_line_nr(R, Line, [C|Acc]);
get_new_line_nr(_, Line, []) -> Line; % No line nr found
get_new_line_nr(_, _, Acc) -> list_to_integer(reverse(Acc)).
scan_to_nl([], Acc) -> {reverse(Acc), []};
scan_to_nl([$\n|Str], Acc) -> {reverse(Acc), Str};
scan_to_nl([$\r|R], Acc) -> scan_to_nl(R, Acc);
scan_to_nl([C|R], Acc) -> scan_to_nl(R, [C|Acc]).
strip([$ |R]) -> strip(R);
strip(L) -> L.
%% Escaped character. Escaped chars are repr as two characters in the
%% input list of letters and this is translated into one char.
escaped_char($n) -> $\n;
escaped_char($t) -> $\t;
escaped_char($v) -> $\v;
escaped_char($b) -> $\b;
escaped_char($r) -> $ ;
escaped_char($f) -> $\f;
escaped_char($a) -> $\a;
escaped_char($\\) -> $\\;
escaped_char($?) -> $?;
escaped_char($') -> $';
escaped_char($") -> $";
escaped_char($x) -> hexadecimal;
escaped_char($u) -> unicode;
escaped_char(X) when ?is_octal(X) -> octal;
%% Error
escaped_char(_Other) -> error.
skip_to_nl([]) -> [];
skip_to_nl([$\n | Str]) ->[$\n | Str];
skip_to_nl([_|Str]) ->
skip_to_nl(Str).
skip_comment([$\\, _ | Str]) ->
skip_comment(Str);
skip_comment([$*, $/ | Str]) -> Str;
skip_comment([_|Str]) ->
skip_comment(Str).
%%----------------------------------------------------------------------
%% Shall separate keywords from identifiers and numbers
%% Fill in the ets of reserved words
is_reserved("Object", _) -> 'Object';
is_reserved("in", _) -> in;
is_reserved("interface", _) -> interface;
is_reserved("case", _) -> 'case';
is_reserved("union", _) -> union;
is_reserved("struct", _) -> struct;
is_reserved("any", _) -> any;
is_reserved("long", _) -> long;
is_reserved("float", _) -> float;
is_reserved("out", _) -> out;
is_reserved("enum", _) -> enum;
is_reserved("double", _) -> double;
is_reserved("context", _) -> context;
is_reserved("oneway", _) -> oneway;
is_reserved("sequence", _) -> sequence;
is_reserved("FALSE", _) -> 'FALSE';
is_reserved("readonly", _) -> readonly;
is_reserved("char", _) -> char;
is_reserved("wchar", _) -> wchar;
is_reserved("void", _) -> void;
is_reserved("inout", _) -> inout;
is_reserved("attribute", _) -> attribute;
is_reserved("octet", _) -> octet;
is_reserved("TRUE", _) -> 'TRUE';
is_reserved("switch", _) -> switch;
is_reserved("unsigned", _) -> unsigned;
is_reserved("typedef", _) -> typedef;
is_reserved("const", _) -> const;
is_reserved("raises", _) -> raises;
is_reserved("string", _) -> string;
is_reserved("wstring", _) -> wstring;
is_reserved("default", _) -> default;
is_reserved("short", _) -> short;
is_reserved("module", _) -> module;
is_reserved("exception", _) -> exception;
is_reserved("boolean", _) -> boolean;
%% --- New keywords Introduced in CORBA-2.3.1 ---
%% For now we cannot add these for all backends right now since it would cause
%% some problems for at least one customer.
is_reserved("fixed", BE) -> check_be(BE, fixed);
%is_reserved("abstract", BE) -> check_be(BE, abstract);
%is_reserved("custom", BE) -> check_be(BE, custom);
%is_reserved("factory", BE) -> check_be(BE, factory);
%is_reserved("local", BE) -> check_be(BE, local);
%is_reserved("native", BE) -> check_be(BE, native);
%is_reserved("private", BE) -> check_be(BE, private);
%is_reserved("public", BE) -> check_be(BE, public);
%is_reserved("supports", BE) -> check_be(BE, supports);
%is_reserved("truncatable", BE) -> check_be(BE, truncatable);
%is_reserved("ValueBase", BE) -> check_be(BE, 'ValueBase');
%is_reserved("valuetype", BE) -> check_be(BE, valuetype);
is_reserved(_, _) -> undefined.
check_be(erl_corba, KeyWord) ->
KeyWord;
check_be(_, _) ->
undefined.
|