/
Lexer.mo
468 lines (428 loc) · 13.8 KB
/
Lexer.mo
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
encapsulated package Lexer "Implements the DFA of OMCC"
import Types;
import LexerCode;
import LexTable;
import OMCCTypes;
import System;
import List;
import Util;
import arrayGet = MetaModelica.Dangerous.arrayGetNoBoundsChecking; // Bounds checked with debug=true
import stringGet = MetaModelica.Dangerous.stringGetNoBoundsChecking;
import MetaModelica.Dangerous.listReverseInPlace;
uniontype LexerTable
record LEXER_TABLE
array<Integer> accept;
array<Integer> ec;
array<Integer> meta;
array<Integer> base;
array<Integer> def;
array<Integer> nxt;
array<Integer> chk;
array<Integer> acclist;
end LEXER_TABLE;
end LexerTable;
constant Boolean debug = false;
function scan "Scan starts the lexical analysis, load the tables and consume the program to output the tokens"
input String fileName "input source code file";
output list<OMCCTypes.Token> tokens "return list of tokens";
protected
String contents;
algorithm
contents := loadSourceCode(fileName);
tokens := lex(fileName,contents);
end scan;
function scanString "Scan starts the lexical analysis, load the tables and consume the program to output the tokens"
input String fileSource "input source code file";
output list<OMCCTypes.Token> tokens "return list of tokens";
algorithm
tokens := lex("<StringSource>",fileSource);
end scanString;
function loadSourceCode
input String fileName "input source code file";
output String contents;
algorithm
System.realtimeTick(2);
contents := System.readFile(fileName);
print("loadSourceCode finished in: " + realString(System.realtimeTock(2)*1000) + "ms\n");
end loadSourceCode;
function lex "Scan starts the lexical analysis, load the tables and consume the program to output the tokens"
input String fileName "input source code file";
input String contents;
output list<OMCCTypes.Token> tokens "return list of tokens";
protected
Integer startSt,numStates,i,r,cTok,cTok2,currSt,pos,sPos,ePos,linenr,contentLen,numBacktrack,buffer,lineNrStart;
list<Integer> cProg,cProg2;
list<String> chars;
array<Integer> mm_accept,mm_ec,mm_meta,mm_base,mm_def,mm_nxt,mm_chk,mm_acclist,states;
LexerTable lexTables;
String s1,s2;
algorithm
// load arrays
mm_accept := listArray(LexTable.yy_accept);
mm_ec := listArray(LexTable.yy_ec);
mm_meta := listArray(LexTable.yy_meta);
mm_base := listArray(LexTable.yy_base);
mm_def := listArray(LexTable.yy_def);
mm_nxt := listArray(LexTable.yy_nxt);
mm_chk := listArray(LexTable.yy_chk);
mm_acclist := listArray(LexTable.yy_acclist);
lexTables := LEXER_TABLE(mm_accept,mm_ec,mm_meta,mm_base,mm_def,mm_nxt,mm_chk,mm_acclist);
// Initialize the Env Variables
startSt := 1;
currSt := 1;
pos := 1;
sPos := 0;
ePos := 0;
linenr := 1;
lineNrStart := 1;
buffer := 0;
// TODO: All
states := arrayCreate(128,1);
numStates := 1;
if (debug==true) then
print("\nLexer analyzer LexerCode..." + fileName + "\n");
//printAny("\nLexer analyzer LexerCode..." + fileName + "\n");
end if;
tokens := {};
if (debug) then
print("\n TOTAL Chars:");
print(intString(stringLength(contents)));
end if;
contentLen := stringLength(contents);
i := 1;
while i <= contentLen loop
cTok := stringGet(contents,i);
(tokens,numBacktrack,startSt,currSt,pos,sPos,ePos,linenr,lineNrStart,buffer,states,numStates) := consume(cTok,lexTables,tokens,contents,startSt,currSt,pos,sPos,ePos,linenr,lineNrStart,buffer,states,numStates,fileName);
i := i - numBacktrack + 1;
end while;
tokens := listReverseInPlace(tokens);
end lex;
function consume
input Integer cp;
input LexerTable lexTables;
input list<OMCCTypes.Token> tokens;
input String fileContents;
input Integer startSt;
input Integer currSt,pos,sPos,ePos,linenr,inLineNrStart;
input Integer inBuffer;
input array<Integer> inStates;
input Integer inNumStates;
input String fileName;
output list<OMCCTypes.Token> resToken;
output Integer bkBuffer = 0;
output Integer mm_startSt;
output Integer mm_currSt,mm_pos,mm_sPos,mm_ePos,mm_linenr,lineNrStart;
output Integer buffer;
output array<Integer> states;
output Integer numStates;
protected
OMCCTypes.Token tok;
Integer act,buffer2;
array<Integer> mm_accept,mm_ec,mm_meta,mm_base,mm_def,mm_nxt,mm_chk,mm_acclist;
Integer c,mm_finish,baseCond;
algorithm
LEXER_TABLE(accept=mm_accept,ec=mm_ec,meta=mm_meta,base=mm_base,
def=mm_def,nxt=mm_nxt,chk=mm_chk,acclist=mm_acclist) := lexTables;
mm_startSt := startSt;
mm_currSt := currSt;
mm_pos := pos;
mm_sPos := sPos;
mm_ePos := ePos;
mm_linenr := linenr;
lineNrStart := inLineNrStart;
buffer := inBuffer;
states := inStates;
numStates := inNumStates;
mm_finish := LexTable.yy_finish;
baseCond := arrayGet(mm_base,mm_currSt);
if (debug==true) then
print("\nPROGRAM:{" + intString(cp) + "} ");
print("\nBUFFER:{" + intString(buffer) + "} ");
print("base:" + intString(baseCond) + " st:" + intString(mm_currSt)+" ");
end if;
buffer := buffer+1;
mm_pos := mm_pos+1;
if (cp==10) then
mm_linenr := mm_linenr+1;
mm_sPos := 0;
else
mm_sPos := mm_sPos+1;
end if;
if (debug==true) then
print("\n[Reading:'" + intStringChar(cp) +"' at p:" + intString(mm_pos-1) + " line:"+ intString(mm_linenr) + " rPos:" + intString(mm_sPos) +"]");
end if;
c := arrayGet(mm_ec,cp);
if (debug==true) then
print(" evalState Before[c" + intString(c) + ",s"+ intString(mm_currSt)+"]");
end if;
(mm_currSt,c) := evalState(lexTables,mm_currSt,c);
if (debug==true) then
print(" After[c" + intString(c) + ",s"+ intString(mm_currSt)+"]");
end if;
if (mm_currSt>0) then
mm_currSt := arrayGet(mm_base,mm_currSt);
// print("BASE:"+ intString(mm_currSt)+"]");
mm_currSt := arrayGet(mm_nxt,mm_currSt + c);
// print("NEXT:"+ intString(mm_currSt)+"]");
else
mm_currSt := arrayGet(mm_nxt,c);
end if;
numStates := numStates+1; // TODO: BAD BAD BAD. At least arrayUpdate should be a safe operation... We need to grow the number of states on demand though.
arrayUpdate(states,numStates,mm_currSt);
// printAny(states);
// print("[c" + intString(c) + ",s"+ intString(mm_currSt)+"]");
// print("[B:" + intString(arrayGet(mm_base,mm_currSt))+"]");
baseCond := arrayGet(mm_base,mm_currSt);
if (baseCond==mm_finish) then
if (debug==true) then
print("\n[RESTORE=" + intString(arrayGet(mm_accept,mm_currSt)) + "]");
end if;
(act, mm_currSt, mm_pos, mm_sPos, mm_linenr, buffer, bkBuffer, states, numStates) := findRule(lexTables, fileContents, mm_currSt, mm_pos, mm_sPos, mm_ePos, mm_linenr, buffer, bkBuffer, states, numStates);
if (debug==true) then
print("\nFound rule: " + String(act));
end if;
(tok,mm_startSt,buffer2) := LexerCode.action(act,mm_startSt,mm_currSt,mm_pos,mm_sPos,mm_ePos,mm_linenr,lineNrStart,buffer,debug,fileName,fileContents);
if (debug==true) then
print("\nDid action");
end if;
mm_currSt := mm_startSt;
arrayUpdate(states,1,mm_startSt);
numStates := 1;
/* Either a token was output (get new positions for next token). Or a whitespace was emitted. */
if buffer <> buffer2 then
mm_ePos := mm_sPos;
lineNrStart := linenr;
end if;
buffer := buffer2;
/*
_ := match otok
local
OMCCTypes.Token tok;
case SOME(tok) equation print("Output token: " + OMCCTypes.printToken(tok) + "\n"); then ();
else ();
end match;
*/
resToken := match tok
case OMCCTypes.TOKEN(id=-1) then tokens;
else tok::tokens;
end match;
if(debug) then
print("\n CountTokens:" + intString(listLength(resToken)));
end if;
else
bkBuffer := 0; // consume the character
resToken := tokens;
end if;
end consume;
function findRule
input LexerTable lexTables;
input String fileContents;
input Integer currSt;
input Integer pos;
input Integer sPos;
input Integer mm_ePos;
input Integer linenr;
input Integer inBuffer;
input Integer inBkBuffer;
input array<Integer> inStates;
input Integer inNumStates;
output Integer action;
output Integer mm_currSt;
output Integer mm_pos;
output Integer mm_sPos;
output Integer mm_linenr;
output Integer buffer;
output Integer bkBuffer;
output array<Integer> states;
output Integer numStates;
protected
array<Integer> mm_accept,mm_ec,mm_meta,mm_base,mm_def,mm_nxt,mm_chk,mm_acclist;
Integer lp,lp1,stCmp;
Boolean st;
algorithm
LEXER_TABLE(accept=mm_accept,ec=mm_ec,meta=mm_meta,base=mm_base,
def=mm_def,nxt=mm_nxt,chk=mm_chk,acclist=mm_acclist) := lexTables;
mm_currSt := currSt;
mm_pos := pos;
mm_sPos := sPos;
mm_linenr := linenr;
buffer := inBuffer;
bkBuffer := inBkBuffer;
states := inStates;
numStates := inNumStates;
stCmp := arrayGet(states,numStates);
lp := arrayGet(mm_accept,stCmp);
lp1 := arrayGet(mm_accept,stCmp+1);
st := intGt(lp,0) and intLt(lp,lp1);
(action, mm_currSt, mm_pos, mm_sPos, mm_linenr, buffer, bkBuffer, states, numStates) := match(numStates,st)
local
Integer act,cp;
list<Integer> restBuff;
case (_,true)
equation
if debug then
checkArray(mm_accept,stCmp,sourceInfo());
checkArray(mm_acclist,lp,sourceInfo());
end if;
lp = arrayGet(mm_accept,stCmp);
act = arrayGet(mm_acclist,lp);
then (act, mm_currSt, mm_pos, mm_sPos, mm_linenr, buffer, bkBuffer, states, numStates);
case (_,false)
equation
cp = stringGet(fileContents,mm_pos-1);
buffer = buffer-1;
bkBuffer = bkBuffer+1;
mm_pos = mm_pos - 1;
mm_sPos = mm_sPos -1;
if (cp==10) then
mm_sPos = mm_ePos;
mm_linenr = mm_linenr-1;
end if;
if debug then
checkArray(states,numStates,sourceInfo());
end if;
mm_currSt = arrayGet(states,numStates);
numStates = numStates - 1;
(act, mm_currSt, mm_pos, mm_sPos, mm_linenr, buffer, bkBuffer, states, numStates) = findRule(lexTables, fileContents, mm_currSt, mm_pos, mm_sPos, mm_ePos, mm_linenr, buffer, bkBuffer, states, numStates);
then (act, mm_currSt, mm_pos, mm_sPos, mm_linenr, buffer, bkBuffer, states, numStates);
end match;
end findRule;
function evalState
input LexerTable lexTables;
input Integer cState;
input Integer c;
output Integer new_state;
output Integer new_c;
protected
Integer cState1=cState;
Integer c1=c;
array<Integer> mm_accept,mm_ec,mm_meta,mm_base,mm_def,mm_nxt,mm_chk,mm_acclist;
Integer val,val2,chk;
algorithm
LEXER_TABLE(accept=mm_accept,ec=mm_ec,meta=mm_meta,base=mm_base,
def=mm_def,nxt=mm_nxt,chk=mm_chk,acclist=mm_acclist) := lexTables;
chk := arrayGet(mm_base,cState1);
chk := chk + c1;
val := arrayGet(mm_chk,chk);
val2 := arrayGet(mm_base,cState1) + c1;
// print("{val2=" + intString(val2) + "}\n");
(new_state,new_c) := match (cState1==val)
local
Integer s,c2;
case (true)
then (cState1,c1);
case (false)
equation
cState1 = arrayGet(mm_def,cState1);
//print("[newS:" + intString(cState)+"]");
//c2 = c;
if ( cState1 >= LexTable.yy_limit ) then
c1 = arrayGet(mm_meta,c1);
// print("META[c:" + intString(c)+"]");
end if;
if (cState1>0) then
(cState1,c1) = evalState(lexTables,cState1,c1);
end if;
then (cState1,c1);
end match;
end evalState;
function getInfo
input Integer len;
input Integer sPos;
input Integer ePos;
input Integer lineNrStart;
input Integer mm_linenr;
input String programName;
output OMCCTypes.Info info;
protected
Integer mm_sPos;
Integer c;
algorithm
/* for i in (List.isEmpty(buff1)==false) loop
c::buff1 := buff1;
if (c==10) then
mm_linenr := mm_linenr - 1;
mm_sPos := 0;
else
mm_sPos := mm_sPos - 1;
end if;
end while; */
info := OMCCTypes.INFO(programName,false,lineNrStart,ePos+1,mm_linenr /* flineNr */, sPos+1 /* frPos+1 */,Absyn.dummyTimeStamp);
/*if (true) then
print("\nTOKEN file:" +programName + " p(" + intString(mm_sPos) + ":" + intString(mm_linenr) + ")-(" + intString(frPos) + ":" + intString(flineNr) + ")");
end if; */
end getInfo;
function printBuffer2
input list<Integer> inList;
input String cBuff;
output String outList;
protected
list<Integer> inList1=inList;
String cBuff1=cBuff;
algorithm
(outList) := match(inList,cBuff)
local
Integer c;
String new,tout;
list<Integer> rest;
case ({},_)
then (cBuff1);
else
equation
c::rest = inList1;
new = cBuff1 + intStringChar(c);
(tout) = printBuffer2(rest,new);
then (tout);
end match;
end printBuffer2;
function printBuffer
input list<Integer> inList;
output String outList;
protected
list<Integer> inList1=inList;
Integer c;
algorithm
outList := "";
while (List.isEmpty(inList1)==false) loop
c::inList1 := inList1;
outList := outList + intStringChar(c);
end while;
end printBuffer;
function printStack
input list<Integer> inList;
input String cBuff;
output String outList;
protected
list<Integer> inList1=inList;
String cBuff1=cBuff;
algorithm
(outList) := match(inList,cBuff)
local
Integer c;
String new,tout;
list<Integer> rest;
case ({},_)
then (cBuff1);
else
equation
c::rest = inList1;
new = cBuff1 + "|" + intString(c);
(tout) = printStack(rest,new);
then (tout);
end match;
end printStack;
function checkArray<T>
input array<T> arr;
input Integer index;
input SourceInfo info;
protected
String filename;
Integer lineStart;
algorithm
if index<1 or index>arrayLength(arr) then
SOURCEINFO(fileName=filename, lineNumberStart=lineStart) := info;
print("\n[" + filename + ":" + String(lineStart) + "]: checkArray failed: arrayLength="+String(arrayLength(arr))+" index=" + String(index) + "\n");
fail();
end if;
end checkArray;
end Lexer;