-
Notifications
You must be signed in to change notification settings - Fork 0
/
cr_scan.rb
486 lines (412 loc) · 12.8 KB
/
cr_scan.rb
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
class AbsToken
attr_accessor :len, :pos, :col, :line, :sym
alias :Len :len
alias :Len= :len=
alias :Pos :pos
alias :Pos= :pos=
public
# virtual ~AbsToken() { }
# int Sym; // Token Number
# int Line, Col; // line and column of current Token
# int Len; // length of current Token
# long Pos; // file position of current Token
def GetSym()
return @sym
end
def SetSym( sym)
return @sym = sym
end
def GetPos()
return @pos
end
def init( sym = 0, line = 0, col = 0, pos = 0, len = 0)
@sym = sym
@line = line
@col = col
@pos = pos
@len = len
end
def clone
ret = AbsToken.new
ret.init(sym, line, col, pos, len)
return ret
end
end
class AbsScanner
attr_accessor :nextSym, :currSym, :buffer, :buffPos, :ch
def initialize
# p "init absscanner"
@nextSym = AbsToken.new
@nextSym.init
# p "nextSym=#{nextSym}"
@currSym = AbsToken.new
@currSym.init
end
=begin
public:
# virtual ~AbsScanner() {}
AbsToken CurrSym; // current (most recently parsed) token
AbsToken @nextSym; // next (look ahead) token
virtual int Get() = 0
virtual void Reset() = 0
virtual unsigned char CurrentCh(long pos) = 0
virtual void GetString(AbsToken *Sym, char *Buffer, int Max) = 0
virtual void GetString(long Pos, char *Buffer, int Max) = 0
virtual void GetName(AbsToken *Sym, char *Buffer, int Max) = 0
virtual long GetLine(long Pos, char *Line, int Max) = 0
=end
end
class AbsError
=begin
public:
# virtual ~AbsError() { }
# virtual void Store(int nr, int line, int col, long pos) = 0
#
# virtual void StoreErr(int nr, AbsToken &Token) = 0
#
# virtual void StoreWarn(int nr, AbsToken &Token) = 0
#
# virtual void SummarizeErrors() = 0
=end
end
LF_CHAR = 10
CR_CHAR = 13
EOF_CHAR = 0
TAB_CHAR = 9
TAB_SIZE = 8
def Upcase(c)
return (c >= 'a' && c <= 'z') ? c-32 : c;
end
# from CR_SCAN.hpp
class CRScanner < AbsScanner
public
def initialize()
super
@buffer = nil
Reset()
end
def initialize(s, ing)
# p "init CRScanner", 10
super()
@buffer = s
@ignoreCase = ing
# p "next sym = #{nextSym}"
Reset()
# p "next sym = #{nextSym}"
#p "init CRScanner OK"
end
# def CRScanner(ignoreCase)
# @buffer = NULL
# @ignoreCase = ignoreCase
# end
#
#
# def initialize( srcFile, ignoreCase)
# @buffer = nil
# ReadFile(srcFile)
# Reset()
# @ignoreCase = ignoreCase
# end
# ~CRScanner()
def Reset(buffPos=nil, currLine=nil, lineStart=nil, currCol=nil)
if !currLine
@currLine = 1
else
@currLine = currLine
end
if !lineStart
@lineStart = 0
else
@lineStart = lineStart
end
if !buffPos
@buffPos = -1
else
@buffPos = buffPos
end
if !currCol
@currCol = 0
else
@currCol = currCol
end
@comEols = 0
@nextSym = AbsToken.new
@nextSym.init()
NextCh()
end
def EqualStr(s)
# p "EqualStr: #{s}, #{@buffer[nextSym.pos..@buffer.size-1]}"
# raise ("EqualStr")
# long pos; char c;
if (nextSym.len != s.size)
return false
end
pos = nextSym.pos
s.each_char{|cc|
c = CurrentCh(pos)
#p("-->c:#{c}")
pos+=1
if (@ignoreCase)
c = Upcase(c)
cc = Upcase(cc)
# p "equalstr:#{cc}, #{c}"
end
if (c != cc)
return false
end
}
#if (s== "const")
# p("return true", 10)
# end
return true
end
def SetIgnoreCase()
@ignoreCase = 1
end
def Get()
raise("not implemented")
end
def GetString(sym, buffer, max)
raise("not implemented")
end
def GetString( pos, buffer, max)
raise("not implemented")
end
def GetSymValue(sym)
ret = ""
# p "sym #{sym.sym} pos #{sym.pos} "
len = sym.len
pos = sym.pos
while (true)
_cch = CurrentCh(pos)
break if _cch == nil
ret += _cch
pos +=1
len -=1
if len <=0
break
end
end
return ret
end
def GetSymString(sym)
ret = ""
# p "sym len #{sym.len} "
len = sym.len
pos = sym.pos
while (len > 0)
c = CurrentCh(pos)
break if c == nil
ret += c
pos +=1
len -=1
# if len <=0
# break
# end
end
return ret
end
# def GetName(sym)
# ret = ""
# p "sym len #{sym.len} "
# len = sym.len
# pos = sym.pos
#
# while (len > 0)
# c = CurrentCh(pos)
# break if c == nil
# ret += c
# pos +=1
# len -=1
# # if len <=0
# # break
# # end
# end
# return ret
# end
def GetLine( pos, line, max)
raise("not implemented")
end
def cur_line()
@buffer.lines[@currLine-1]
end
def _get()
#@buffPos+=1
#@ch = CurrentCh(@buffPos)
#return @ch
NextCh()
end
# include_current_char: returned string includes current char, NOTICE, current char means start of nextsym, not current sym
# e.g. int a = 1, @sym=C_intSym and curString="int", then "current char" is 'a'
def NextLine(include_current_char = false, from = nil)
#p "==>nextline1:line #{currLine}"
# p "from:#{from}, ch #{@buffer[from]}" if from
ret = ""
if include_current_char
if from
ret_start = from
# ret = "#{@buffer[from]}"
@buffPos = from
else
ret_start = @buffPos
# ret = "#{@buffer[@buffPos]}"
end
else
if from
ret_start = from +1
@buffPos = from +1
else
ret_start = @buffPos+1
end
end
@ch = @buffer[buffPos]
# p "nextline ret_start:#{ret_start}, ch #{@ch}, @buffPos:#{@buffPos}, pos #{@buffPos}", 20
if cch() == "\n" # if already in end of line, do nothing then _get()
else
begin # while (@ch != "\n")
# p "NextLine() @ch=#{@ch}"
# @buffPos+=1
# # p "@buffPos=#{@buffPos}"
# @ch = CurrentCh(@buffPos)
# _get()
# return @buffer[ret_start..@buffer.size-1] if @ch == nil || @ch.to_byte == EOF_CHAR
return ret if @ch == nil || @ch.to_byte == EOF_CHAR
#if (@ch == "\\")
# while (_get() =~ /\s/)
# end
# if @ch == "\n"
# _get()
# end
#end
# if line end with "\", move to \n, then will ignore this \n
if @ch == "\\" && @buffer[@buffPos+1] =~ /\s/
p "==>nl:1111"
# p "--->00000:#{@ch}, #{@buffPos}, #{@buffer[@buffPos]}@#{@buffer[@buffPos].to_byte} #{@buffer[@buffPos+1]}@#{@buffer[@buffPos+1].to_byte} #{@buffer[@buffPos+2]}@#{@buffer[@buffPos+2].to_byte}"
# skip white space after \
while (_get() =~ /\s/ && @ch != "\n")
end
p "==>nl:1112:#{@ch}"
# p "--->00001:#{@ch}, #{@buffPos}, #{@buffer[@buffPos+1]} #{@buffer[@buffPos+1].to_byte}"
# skip first LF
#if @ch == "\n"
# p "--->00003:#{@ch}, #{@buffPos}, #{@buffer[@buffPos+1]} #{@buffer[@buffPos+1].to_byte}"
#
# _get()
#end
# p "--->00002:#{@ch}, #{@buffPos}, #{@buffer[@buffPos+1]} #{@buffer[@buffPos+1].to_byte}"
end
# p "@ch011=#{@ch.to_byte}, #{@buffer[ret_start..@buffPos]}, pos #{@buffPos}, ret=#{ret}"
if (@ch == '"')
ret += "\""
# p "@ch222:#{@ch}"
_get()
while @ch != '"'
# p "@ch333:#{@ch.to_byte}, pos #{@buffPos}"
if @ch == "\\"
ret += "\\"
_get()
ret += @ch
_get()
p "@ch = #{@ch.to_byte}, #{@buffer[ret_start..@buffPos]}"
elsif (@ch >= ' ' && @ch <= '!' ||
@ch >= '#' && @ch.to_byte <= 255)
ret += @ch
_get()
elsif @ch.to_byte == 13 || @ch.to_byte == 10 || @ch == nil || @ch.to_byte == EOF_CHAR
break
else
ret += @ch
_get()
end
p "ret:#{ret}"
end
ret += '"'
elsif @ch == '/'
_pos = @buffPos
line = @currLine
Comment()
ret += @ch
# p "pos=#{@buffPos}, @ch1111 = #{@ch.to_byte}, #{@buffer[ret_start..@buffPos]}"
if @currLine > line # if multi line comments
break
# return @buffer[ret_start.._pos]
end
else
ret += @ch
end
_get()
# p "@ch2 = #{@ch.to_byte}, #{@buffer[ret_start..@buffPos]}, ret=#{ret}"
end while (@ch != "\n" && @ch != "\r")
end
# p "nextline2:#{ret}"
@ch = '\0' if @ch == nil
# p "@ch3 = #{@ch.to_byte}, #{@buffer[ret_start..@buffPos]}"
# bufferpos point to "\n"
ret_end = @buffPos-1 #return value not include \n
# @currLine += 1
# @currCol = 1
# @lineStart = @buffPos + 1
_get()
@ch = '\0' if @ch == nil
# p "@ch4 = #{@ch.to_byte}, #{@buffer[ret_start..@buffPos]}"
return "" if ret_end < ret_start
#p "nextline:#{@buffer[ret_start..ret_end]}", 30
#p "nextline1:#{ret}"
#p "==>nextline0:line #{currLine}"
# return @buffer[ret_start..ret_end]
return ret
end
# return skipped content
# include_current_char: returned string includes current char
def skip_curline(include_current_char = false, from=nil)
ret = NextLine(include_current_char, from)
# p "after skip current line, pos #{@buffPos}, ch = #{@buffer[@buffPos].to_byte}"
# pp "after skip current line:@buffPos=#{@buffPos}, buffer=#{@buffer}, ret=#{ret}", 20
# p "after skip current line2:"
return ret
end
def cch()
CurrentCh(@buffPos)
end
# def NextSym
# @nextSym
# end
private
# unsigned char *Buffer
protected
# int @comEols; // number of EOLs in a comment
# long @buffPos; // current position in buf
# int CurrCol; // current Column Number
# long InputLen; // source file size
# int @currLine; // current input line (may be higher than line)
# long @lineStart; // start position of current line
# unsigned char Ch
# int IgnoreCase
def ReadFile( srcFile)
raise("not implemented")
end
def CurrentCh( pos)
# p "buffer:#{@buffer}"
# p "pos:#{pos}"
return @buffer[pos]
end
def NextCh()
# p "NextCh() @ch=#{@ch}"
@buffPos+=1
# p "@buffPos=#{@buffPos}"
@ch = CurrentCh(@buffPos)
return if @ch == nil
if (@ignoreCase)
@ch = Upcase(@ch)
end
# p "@ch=#{@ch}"
if (@ch == TAB_CHAR)
@currCol += TAB_SIZE - (@currCol % TAB_SIZE)
elsif (@ch.to_byte == LF_CHAR)
@currLine+=1
@currCol = 0
@lineStart = @buffPos + 1
end
@currCol+=1
end
end