-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecode_src_utils.dbl
More file actions
389 lines (335 loc) · 9.6 KB
/
decode_src_utils.dbl
File metadata and controls
389 lines (335 loc) · 9.6 KB
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
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;;; <summary>
;;; Have we reached the end of the parameters
;;; </summary>
;;; <param name="a_buff">
;;; Current source code buffer
;;; </param>
;;; <param name="a_in_proc">
;;; Returned - are we now in the procedure division?
;;; </param>
;;; <returns>
;;; true/false
;;; </returns>
function end_of_params ,boolean
req in a_buff ,a ;buffer to check
req out a_in_proc ,n ;in procedure division
endparams
static record
comment_eop_len ,i4
comment_eop ,a80 ;comment signalling end of parameters
hdl ,D_HANDLE
endrecord
record
start ,i4
pos ,i4
endrecord
structure buff
b ,a1
endstructure
proc
using a_buff select
('endparams '),
freturn true
('proc ', '.proc '),
begin
a_in_proc = 1
freturn true
end
('record ', 'common ', 'global ' 'external ', 'literal '),
freturn true
('stack record', 'static record', 'local record'),
freturn true
('.define ', '.align '),
freturn true
('.include '),
begin
if(%instr(1, a_buff, 'record=') || %instr(1, a_buff, 'record ='))
freturn true
if(%instr(1, a_buff, 'common=') || %instr(1, a_buff, 'common ='))
freturn true
;if(!%isGroupInclude())
; freturn true
end
endusing
;; check for Synergy provided includes
if(%instr(1, a_buff, 'dbldir:dbl.def'))
freturn true
if(%instr(1, a_buff, 'dbldir:windows.def'))
freturn true
if(%instr(1, a_buff, 'dbldir:synxml.def'))
freturn true
if(%instr(1, a_buff, 'wnd:tools.def'))
freturn true
if(%instr(1, a_buff, 'wnd:tkctl.def'))
freturn true
if(%instr(1, a_buff, 'wnd:windows.def')) ;;should be in DBLDIR:
freturn true
if(%instr(1, a_buff, 'rpslib:ddinfo.def'))
freturn true
if(%instr(1, a_buff, 'connectdir:ssql.def'))
freturn true
;end of parameters comment
if(!comment_eop_len)
begin
getlog('COMMENT_EOP', comment_eop, pos)
if(!pos)
begin
comment_eop = ';end of '
pos = %trim(comment_eop)
end
comment_eop_len = pos
end
if(comment_eop && a_buff == comment_eop(1:comment_eop_len))
freturn true
;application specific includes
if(!hdl)
begin
;static handles are > 0
hdl = %mem_proc(DM_ALLOC.bor.DM_STATIC.bor.DM_BLANK, 65000)
getlog('APP_INCLUDES', ^m(hdl), pos)
if(pos) then
hdl = %mem_proc(DM_RESIZ, pos, hdl)
else
begin
hdl = %mem_proc(DM_FREE, hdl)
hdl = -1
end
end
if(hdl > 0)
begin
start = 1
while(pos=%instr(start, ^m(hdl), '|'))
begin
if(%instr(1, a_buff, ^m(buff.b(start, pos-1), hdl)))
freturn true
start = pos+1
end
end
freturn false
endfunction
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
.define TOKEN_NONE 0
.define TOKEN_IDENTIFIER 1
.define TOKEN_PAREN 2
.define TOKEN_BRACKET 3
.define TOKEN_OPERATOR 4
.define TOKEN_INVALID -1 ;Invalid tokens are <= this token
.define TOKEN_NEXT_INVALID -1
.define TOKEN_CURR_INVALID -2
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;;; <summary>
;;; What type of token do we have?
;;; </summary>
;;; <param name="a_token">Current token</param>
;;; <param name="a_source">Next token</param>
;;; <returns>
;;; 0=none, 1=identifier, 2=parenthesis, 3=bracket, 4=operator
;;; -1=next token is invalid,
;;; -2=current token is invalid
;;; </returns>
function nextToken ,i
a_token ,a ;current token in line
a_source ,a ;next token on line
endparams
record
pos ,i4
len ,i4
endrecord
proc
pos = %trim(a_token)+1
len = %trim(a_source)
if(pos > len)
freturn TOKEN_NONE
using a_source(pos:1) select
(' '),
begin
while(pos < len)
begin
pos += 1
using a_source(pos:1) select
(' '),
nop
(';'),
freturn TOKEN_NONE
('A' thru 'Z', 'a' thru 'z', '$', '_'),
freturn TOKEN_IDENTIFIER
('('),
freturn TOKEN_PAREN
('['),
freturn TOKEN_BRACKET
('=', '>', '<', '!', '.'),
freturn TOKEN_OPERATOR
(),
freturn TOKEN_NEXT_INVALID
endusing
end
end
(';'),
freturn TOKEN_NONE
('('),
freturn TOKEN_PAREN
('['),
freturn TOKEN_BRACKET
('=', '>', '<', '!', '.'),
freturn TOKEN_OPERATOR
(),
freturn TOKEN_CURR_INVALID
endusing
freturn TOKEN_NONE
endfunction
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;;; <summary>
;;; Read source file
;;; </summary>
;;; <param name="in_ch">Input channel</param>
;;; <param name="buff">
;;; Source line buffer
;;; </param>
;;; <param name="buff_lc">
;;; Lowercase version of BUFF
;;; </param>
;;; <param name="buff_len">
;;; Length of BUFF
;;; </param>
;;; <param name="next_buff">
;;; Next source line
;;; </param>
;;; <param name="next_buff_lc">
;;; Lowercase version of next source line
;;; </param>
;;; <param name="next_len">
;;; Length of next source line
;;; </param>
subroutine read_buff
req in in_ch ,i4 ;; in channel
req inout buff ,a512 ;; current read buffer
req inout buff_lc ,a512 ;; lowercase copy of current read buffer
req inout buff_len ,i4 ;; length of buff
req inout next_buff ,a512 ;; next read buffer
req inout next_buff_lc ,a512 ;; lowercase copy of next read buffer
req inout next_len ,i4 ;; length of next_buff
endparams
;; logical end of file
.define EOF_SRC_FILE '~~~~'
proc
if(next_buff == EOF_SRC_FILE)
begin
clear buff, buff_lc, next_buff, next_buff_lc
buff_len = -1
xreturn
end
buff = next_buff
buff_lc = next_buff_lc
buff_len = next_len
reads(in_ch, next_buff, read_buff_eof)
gen_buff_lc(next_buff, next_buff_lc, next_len)
xreturn
read_buff_eof,
if(!%rsize) then
begin
next_buff = EOF_SRC_FILE
next_buff_lc = EOF_SRC_FILE
next_len = %trim(next_buff)
end
else
gen_buff_lc(next_buff, next_buff_lc, next_len)
xreturn
endsubroutine
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;;; <summary>
;;; Lowercase source, excluding string contents
;;; </summary>
;;; <param name="a_buff">Source code line</param>
;;; <param name="a_buff_lc">Returned lowercase version of source code line</param>
;;; <param name="a_len">Returned length of source code line</param>
subroutine gen_buff_lc
req in a_buff ,a ;buffer
req out a_buff_lc ,a ;lowercase buffer
req out a_len ,n ;length of buff_lc
endparams
record
pos ,i4
len ,i4
in_string ,i4
endrecord
record buff_lc
buff_lc1 ,200a1
endrecord
proc
clear a_buff_lc, a_len
buff_lc = a_buff
locase buff_lc
len = %trimz(buff_lc)
if(!len)
xreturn
;replace control chars with spaces
for pos from 1 thru len
begin
if(buff_lc1(pos) < ' ' || buff_lc1(pos) > '~')
clear buff_lc1(pos)
end
len = %trimz(buff_lc)
if(!len)
xreturn
;remove leading whitespace
pos = 1
while(!buff_lc1(pos))
pos += 1
if(pos > 1 && pos <= len)
buff_lc = buff_lc(pos, len)
;remove multiple spaces that are not in strings and/or comments
pos = 1
while(pos <= len)
begin
using buff_lc1(pos) select
('"'),
begin
using in_string select
(0),
in_string = 2
(2),
in_string = 0
endusing
end
("'"),
begin
using in_string select
(0),
in_string = 1
(1),
in_string = 0
endusing
end
(';'),
begin
if(!in_string)
pos = len
end
(' '),
begin
if(!in_string && pos < len)
begin
using buff_lc1(pos+1) select
(' '),
begin
buff_lc(pos, len) = buff_lc(pos+1, len)
len -= 1
pos -= 1
end
(','),
begin
buff_lc(pos, len) = buff_lc(pos+1, len)
len -= 1
end
endusing
end
end
endusing
pos += 1
end
a_buff_lc = buff_lc
a_len = %trimz(buff_lc)
return
endsubroutine
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++