-
Notifications
You must be signed in to change notification settings - Fork 3
/
m2lang.l
336 lines (287 loc) · 6.74 KB
/
m2lang.l
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
%{
/* This file is part of the software similarity tester SIM.
Written by Dick Grune, Vrije Universiteit, Amsterdam.
$Id: m2lang.l,v 2.18 2012-06-08 16:04:29 Gebruiker Exp $
*/
/*
Modula-2 language front end for the similarity tester.
Author: Dick Grune <dick@cs.vu.nl>
*/
#include "options.h"
#include "token.h"
#include "language.h"
#include "algollike.h"
#include "idf.h"
#include "lex.h"
#include "lang.h"
/* General language front end data */
Token lex_token;
size_t lex_nl_cnt;
size_t lex_tk_cnt;
size_t lex_non_ascii_cnt;
/* Language-dependent data */
/* Most Modula-2 programs start with a number of IMPORTs that look
very similar from program to program. These are skipped by ignoring
the reserved words IMPLEMENTATION, DEFINITION, MODULE, IMPORT
and FROM, having a flag skip_imports, and start reacting only
at the first non-ignored reserved word.
Also, the nesting comments require a state variable.
*/
/* Additional state variables, set in yystart() */
static int skip_imports;
static int comment_level;
/* Data for module idf */
static const struct idf reserved[] = {
{"AND", NORM('&')},
{"ARRAY", NORM('A')},
{"BEGIN", NORM('{')},
{"BY", NORM('B')},
{"CASE", NORM('c')},
{"CONST", NORM('C')},
{"DEFINITION", No_Token},
{"DIV", NORM('/')},
{"DO", NORM('D')},
{"ELSE", NORM('e')},
{"ELSIF", NORM('e')},
{"END", NORM('}')},
{"EXIT", NORM('E')},
{"EXPORT", CTRL('E')},
{"FOR", NORM('F')},
{"FROM", No_Token},
{"IF", NORM('i')},
{"IMPLEMENTATION", No_Token},
{"IMPORT", No_Token},
{"IN", NORM('I')},
{"LOOP", NORM('l')},
{"MOD", NORM('%')},
{"MODULE", No_Token},
{"NOT", NORM('~')},
{"OF", No_Token},
{"OR", NORM('O')},
{"POINTER", NORM('p')},
{"PROCEDURE", NORM('P')},
{"QUALIFIED", NORM('q')},
{"RECORD", NORM('r')},
{"REPEAT", NORM('R')},
{"RETURN", CTRL('r')},
{"SET", NORM('s')},
{"THEN", No_Token},
{"TO", NORM('t')},
{"TYPE", NORM('T')},
{"UNTIL", NORM('u')},
{"VAR", NORM('v')},
{"WHILE", NORM('w')},
{"WITH", NORM('W')},
};
static const struct idf standard[] = {
{"ABS", META('a')},
{"ADDRESS", META('A')},
{"ALLOCATE", MTCT('A')},
{"BITSET", META('b')},
{"BOOLEAN", META('B')},
{"CAP", META('c')},
{"CARDINAL", META('C')},
{"CHAR", MTCT('C')},
{"CHR", META('x')},
{"DEALLOCATE", META('d')},
{"DEC", META('D')},
{"EXCL", META('e')},
{"FALSE", META('f')},
{"FLOAT", META('F')},
{"HALT", META('h')},
{"HIGH", META('H')},
{"INC", META('i')},
{"INCL", META('I')},
{"INTEGER", MTCT('I')},
{"LONGCARD", META('L')},
{"LONGINT", META('L')},
{"LONGREAL", META('L')},
{"MAX", META('m')},
{"MIN", META('M')},
{"NEWPROCESS", META('n')},
{"NIL", META('N')},
{"ODD", META('o')},
{"ORD", META('O')},
{"PROC", META('p')},
{"REAL", META('r')},
{"SIZE", META('s')},
{"SYSTEM", META('S')},
{"TRANSFER", META('t')},
{"TRUE", META('T')},
{"TRUNC", MTCT('T')},
{"VAL", META('v')},
{"WORD", META('w')}
};
/* Special treatment of identifiers */
static Token
idf2token(int hashing) {
Token tk;
/* the token can be on two lists, reserved and standard */
tk = idf_in_list(yytext, reserved, sizeof reserved, IDF);
/* is it one of the keywords to be ignored? */
if (Token_EQ(tk, No_Token)) return tk;
/* The statement below is a significant comment
on the value of state variables.
*/
if (!Token_EQ(tk, IDF)) {
/* reserved word, stop the skipping */
skip_imports = 0;
}
else {
/* it is an identifier but not a reserved word */
if (skip_imports) {
/* skip it */
tk = 0;
}
else {
/* look further */
tk = idf_in_list(yytext, standard, sizeof standard, IDF);
if (Token_EQ(tk, IDF) && hashing) {
/* return a one-Token hash code */
tk = idf_hashed(yytext);
}
}
}
return tk;
}
/* Token sets for module algollike */
const Token Non_Finals[] = {
IDF, /* identifier */
NORM('{'), /* also BEGIN */
NORM('('),
NORM('['),
NORM('A'), /* ARRAY */
NORM('c'), /* CASE */
NORM('C'), /* CONST */
NORM('E'), /* EXIT */
NORM('F'), /* FOR */
NORM('i'), /* IF */
NORM('l'), /* LOOP */
NORM('p'), /* POINTER */
NORM('P'), /* PROCEDURE */
NORM('r'), /* RECORD */
NORM('R'), /* REPEAT */
CTRL('R'), /* RETURN */
NORM('s'), /* SET */
NORM('T'), /* TYPE */
NORM('v'), /* VAR */
NORM('w'), /* WHILE */
NORM('W'), /* WITH */
No_Token
};
const Token Non_Initials[] = {
NORM('}'),
NORM(')'),
NORM(']'),
NORM(';'),
No_Token
};
const Token Openers[] = {
NORM('{'),
NORM('('),
NORM('['),
No_Token
};
const Token Closers[] = {
NORM('}'),
NORM(')'),
NORM(']'),
No_Token
};
/* Language-dependent code */
void
Init_Language(void) {
Init_Algol_Language(Non_Finals, Non_Initials, Openers, Closers);
}
int
May_Be_Start_Of_Run(Token ch) {
return May_Be_Start_Of_Algol_Run(ch);
}
size_t
Best_Run_Size(const Token *str, size_t size) {
return Best_Algol_Run_Size(str, size);
}
%}
%option noyywrap
%Start Comment
Layout ([ \t\r\f])
ASCII95 ([- !"#$%&'()*+,./0-9:;<=>?@A-Z\[\\\]^_`a-z{|}~])
AnyQuoted (\\.)
QuStrChar ([^"\n\\]|{AnyQuoted})
ApoStrChar ([^'\n\\]|{AnyQuoted})
StartComment ("(*")
EndComment ("*)")
SafeComChar ([^*\n])
UnsafeComChar ("*")
Digit ([0-9a-fA-F])
Idf ([A-Za-z][A-Za-z0-9_]*)
%%
{StartComment} { /* See clang.l */
/* Lex itself is incapable of handling Modula-2's
nested comments. So let's help it a bit.
*/
if (comment_level == 0) {
BEGIN Comment;
}
comment_level++;
}
<Comment>{SafeComChar}+ { /* safe comment chunk */
}
<Comment>{UnsafeComChar} { /* unsafe char, read one by one */
}
<Comment>"\n" { /* to break up long comments */
return_eol();
}
<Comment>{EndComment} { /* end-of-comment */
comment_level--;
if (comment_level == 0) {
BEGIN INITIAL;
}
}
\"{QuStrChar}*\" { /* quoted strings */
return_ch('"');
}
\'{ApoStrChar}*\' { /* apostrophed strings */
return_ch('"');
}
{Digit}+("B"|"C"|"H")? { /* numeral, passed as an identifier */
return_tk(IDF);
}
"END"{Layout}*{Idf} { /* ignore identifier after END */
Token tk =
idf_in_list("END", reserved, sizeof reserved, No_Token);
if (!Token_EQ(tk, No_Token)) return_tk(tk);
}
{Idf}/"(" { /* identifier in front of ( */
Token tk = idf2token(is_set_option('F')/* hashing option */);
if (!Token_EQ(tk, No_Token)) return_tk(tk);
}
{Idf} { /* identifier */
Token tk = idf2token(0 /* no hashing */);
if (!Token_EQ(tk, No_Token)) return_tk(tk);
}
"<>" { /* <>, special equivalence */
return_ch('#');
}
\; { /* semicolon, conditionally ignored */
if (is_set_option('f')) return_ch(yytext[0]);
}
\n { /* count newlines */
return_eol();
}
{Layout} { /* ignore layout */
}
{ASCII95} { /* copy other text */
if (!skip_imports) return_ch(yytext[0]);
}
. { /* count non-ASCII chars */
lex_non_ascii_cnt++;
}
%%
/* More language-dependent code */
void
yystart(void) {
skip_imports = 1;
comment_level = 0;
BEGIN INITIAL;
}