-
Notifications
You must be signed in to change notification settings - Fork 12
/
zcl_op_value_pretty_printer.clas.abap
649 lines (550 loc) · 18.6 KB
/
zcl_op_value_pretty_printer.clas.abap
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
"!this class is responsible for pretty printing
"!VALUE # operator strings
CLASS zcl_op_value_pretty_printer DEFINITION
PUBLIC
CREATE PRIVATE
GLOBAL FRIENDS zcl_op_pretty_printer_factory. "needed for instantiation
PUBLIC SECTION.
INTERFACES: zif_op_value_pretty_printer.
ALIASES: format FOR zif_op_value_pretty_printer~format.
PROTECTED SECTION.
PRIVATE SECTION.
TYPES: BEGIN OF t_char_offset,
count TYPE i,
offset_no TYPE i,
char(1) TYPE c,
END OF t_char_offset,
tt_char_offset TYPE STANDARD TABLE OF t_char_offset WITH DEFAULT KEY,
BEGIN OF ENUM enum_terminal STRUCTURE c_terminal,
assign,
rhs,
empty,
struct,
struct2,
iteration,
table,
rhs2,
symbolname,
number,
non_terminal,
text_literal,
string_literal,
spaces,
END OF ENUM enum_terminal STRUCTURE c_terminal,
BEGIN OF ENUM enum_sub_terminal STRUCTURE c_sub_terminal,
na, " not applicable
assign_operator, " =
value_operator, " VALUE #(
parenthesis_open, " (
parenthesis_close, " )
empty_line, " ( )
end_of_statement, " .
END OF ENUM enum_sub_terminal STRUCTURE c_sub_terminal,
BEGIN OF ty_trace,
parent_sync_point TYPE i,
terminal TYPE enum_terminal,
sub_terminal TYPE enum_sub_terminal,
offset TYPE i,
length TYPE i,
sync_point TYPE i,
text TYPE string,
END OF ty_trace,
BEGIN OF ty_sync_point,
terminal TYPE enum_terminal,
sub_terminal TYPE enum_sub_terminal,
offset TYPE i,
trace_index TYPE i,
END OF ty_sync_point.
CONSTANTS: c_newline TYPE abap_char1 VALUE cl_abap_char_utilities=>cr_lf,
c_regex_lhs TYPE string VALUE `[A-Z][A-Z0-9_/\-\[\]\>]+` ##NO_TEXT,
c_regex_number TYPE string VALUE `[0-9]+` ##NO_TEXT,
c_regex_text_literal TYPE string VALUE `'(?:''|[^'])+'` ##NO_TEXT,
c_regex_string_literal TYPE string VALUE '`(?:``|[^`])+`' ##NO_TEXT.
DATA: steering TYPE tt_char_offset,
offset TYPE i,
text TYPE string,
trace_tab TYPE TABLE OF ty_trace
WITH NON-UNIQUE SORTED KEY k1 COMPONENTS parent_sync_point terminal offset,
sync_points TYPE TABLE OF ty_sync_point.
METHODS:
get_spaces RETURNING VALUE(r_spaces_string) TYPE string,
get_offset_in_current_line
IMPORTING
i_content TYPE string
RETURNING
VALUE(r_offset) TYPE i,
get_offset_for_apostroph
IMPORTING
i_content TYPE string
RETURNING
VALUE(r_offset) TYPE i,
get_offset_for_closing_bracket
IMPORTING
i_content TYPE string
RETURNING
VALUE(r_offset) TYPE i,
clean_up_steering_tab
IMPORTING
i_last_char TYPE c,
assign RAISING lcx_no_match,
find_spaces,
rhs RAISING lcx_no_match,
add_empty_value_operator RAISING lcx_no_match,
add_structure_rhs RAISING lcx_no_match,
add_structure_content RAISING lcx_no_match,
add_table RAISING lcx_no_match,
rhs2 RAISING lcx_no_match,
lhs RAISING lcx_no_match,
add_number RAISING lcx_no_match,
non_terminal
IMPORTING
i_sub_terminal TYPE enum_sub_terminal
i_regex TYPE csequence
RAISING lcx_no_match,
add_text_literal RAISING lcx_no_match,
add_string_literal RAISING lcx_no_match,
set_offset_for_regex
IMPORTING i_regex TYPE csequence
RAISING lcx_no_match,
add_to_trace,
set_sync_point
IMPORTING
i_terminal TYPE enum_terminal
i_sub_terminal TYPE enum_sub_terminal DEFAULT c_sub_terminal-na
RETURNING
VALUE(r_current_sync_point_index) TYPE i,
reset_to_sync_point
IMPORTING
i_sync_point_index TYPE i,
cancel_sync_point
IMPORTING
i_sync_point_index TYPE i,
add_value_open_operator RAISING lcx_no_match,
add_parenthesis_close RAISING lcx_no_match,
add_assign_operator RAISING lcx_no_match,
add_end_of_statement RAISING lcx_no_match,
add_empty_line RAISING lcx_no_match,
add_parenthesis_open RAISING lcx_no_match.
ENDCLASS.
CLASS zcl_op_value_pretty_printer IMPLEMENTATION.
METHOD get_spaces.
DATA(spaces_no) = me->steering[ lines( me->steering ) ]-offset_no.
r_spaces_string = repeat( val = ` ` occ = spaces_no ).
ENDMETHOD.
METHOD get_offset_in_current_line.
SPLIT i_content AT c_newline INTO TABLE DATA(lt_split).
DATA(last_line) = lt_split[ lines( lt_split ) ].
r_offset = strlen( last_line ) + 1.
ENDMETHOD.
METHOD get_offset_for_apostroph.
SPLIT i_content AT c_newline INTO TABLE DATA(lt_split).
DATA(last_line) = lt_split[ lines( lt_split ) ].
DATA(regex_matcher) = cl_abap_matcher=>create( pattern = |\\S|
text = last_line ).
regex_matcher->find_next( ).
r_offset = regex_matcher->get_offset( ).
ENDMETHOD.
METHOD get_offset_for_closing_bracket.
"find offset of corresponding opening bracket
"therefor we need to loop backwards in steering table
DATA(x_lines) = lines( me->steering ).
DO x_lines TIMES.
DATA(line) = me->steering[ x_lines ].
IF line-char EQ '('.
r_offset = line-offset_no.
EXIT.
ELSE.
IF x_lines = 1.
EXIT.
ELSE.
x_lines = x_lines - 1.
ENDIF.
ENDIF.
ENDDO.
ENDMETHOD.
METHOD clean_up_steering_tab.
CHECK i_last_char EQ |)|.
"a formating block is complete
"we need to clean up our steering data and remove infos about this complete formating block
DATA(x_lines) = lines( me->steering ).
DO x_lines TIMES.
DATA(line) = me->steering[ x_lines ].
DELETE me->steering INDEX x_lines.
IF line-char EQ |(|.
EXIT.
ENDIF.
IF x_lines = 1.
EXIT.
ELSE.
x_lines = x_lines - 1.
ENDIF.
ENDDO.
ENDMETHOD.
METHOD assign.
" assign tries to build this: LHS = RHS
TRY.
find_spaces( ).
DATA(current_sync_point_index) = set_sync_point( i_terminal = c_terminal-assign ).
lhs( ).
add_assign_operator( ).
rhs( ).
add_end_of_statement( ).
add_to_trace( ).
CLEANUP.
reset_to_sync_point( current_sync_point_index ).
ENDTRY.
ENDMETHOD.
METHOD rhs.
"RHS could be : number | text | string | empty | struct | table
find_spaces( ).
DATA(current_sync_point_index) = set_sync_point( c_terminal-rhs ).
TRY.
TRY.
add_number( ).
CATCH lcx_no_match.
TRY.
add_text_literal( ).
CATCH lcx_no_match.
TRY.
add_string_literal( ).
CATCH lcx_no_match.
TRY.
add_empty_value_operator( ).
CATCH lcx_no_match.
TRY.
add_structure_rhs( ).
CATCH lcx_no_match.
add_table( ).
ENDTRY.
ENDTRY.
ENDTRY.
ENDTRY.
ENDTRY.
CLEANUP.
reset_to_sync_point( current_sync_point_index ).
ENDTRY.
add_to_trace( ).
ENDMETHOD.
METHOD add_empty_value_operator.
"empty : 'VALUE #( )'
TRY.
find_spaces( ).
DATA(current_sync_point_index) = set_sync_point( c_terminal-empty ).
set_offset_for_regex( `VALUE #\( \)` ).
add_to_trace( ).
CLEANUP.
reset_to_sync_point( current_sync_point_index ).
ENDTRY.
ENDMETHOD.
METHOD add_structure_rhs.
"struct : 'VALUE #(' struct2 ')'
TRY.
find_spaces( ).
DATA(current_sync_point_index) = set_sync_point( c_terminal-struct ).
add_value_open_operator( ).
add_structure_content( ).
add_parenthesis_close( ).
add_to_trace( ).
CLEANUP.
reset_to_sync_point( current_sync_point_index ).
ENDTRY.
ENDMETHOD.
METHOD add_structure_content.
"struct2 : ( LHS '=' RHS )+
TRY.
find_spaces( ).
DATA(structure_sync_point_index) = set_sync_point( c_terminal-struct2 ).
set_sync_point( c_terminal-iteration ).
lhs( ).
add_assign_operator( ).
rhs( ).
add_to_trace( ).
DO.
TRY.
find_spaces( ).
DATA(point2) = set_sync_point( c_terminal-iteration ).
TRY.
lhs( ).
CATCH lcx_no_match.
cancel_sync_point( point2 ).
EXIT.
ENDTRY.
add_assign_operator( ).
rhs( ).
add_to_trace( ).
CLEANUP.
reset_to_sync_point( point2 ).
ENDTRY.
ENDDO.
add_to_trace( ).
CLEANUP.
reset_to_sync_point( structure_sync_point_index ).
ENDTRY.
ENDMETHOD.
METHOD add_table.
* table : 'VALUE #(' ( '( )' | ( '(' RHS2 ')' )+ ) ')'
TRY.
find_spaces( ).
DATA(point) = set_sync_point( c_terminal-table ).
add_value_open_operator( ).
TRY.
add_empty_line( ).
CATCH lcx_no_match.
DO.
TRY.
find_spaces( ).
DATA(point2) = set_sync_point( c_terminal-iteration ).
TRY.
add_parenthesis_open( ).
CATCH lcx_no_match.
cancel_sync_point( point2 ).
EXIT.
ENDTRY.
rhs2( ).
add_parenthesis_close( ).
add_to_trace( ).
CLEANUP.
reset_to_sync_point( point2 ).
ENDTRY.
ENDDO.
ENDTRY.
add_parenthesis_close( ).
add_to_trace( ).
CLEANUP.
reset_to_sync_point( point ).
ENDTRY.
ENDMETHOD.
METHOD rhs2.
* RHS2 : number | text | string | empty | struct2
TRY.
find_spaces( ).
DATA(current_sync_point_index) = set_sync_point( c_terminal-rhs2 ).
TRY.
add_number( ).
CATCH lcx_no_match.
TRY.
add_text_literal( ).
CATCH lcx_no_match.
TRY.
add_string_literal( ).
CATCH lcx_no_match.
TRY.
add_empty_value_operator( ).
CATCH lcx_no_match.
add_structure_content( ).
ENDTRY.
ENDTRY.
ENDTRY.
ENDTRY.
add_to_trace( ).
CLEANUP.
reset_to_sync_point( current_sync_point_index ).
ENDTRY.
ENDMETHOD.
METHOD lhs.
TRY.
find_spaces( ).
DATA(current_sync_point_index) = set_sync_point( c_terminal-symbolname ).
" SCALAR
" STRUCTURE
" TABLE
" STRUCTURE-COMPONENT
" TABLE[1]
" TABLE[1]-COMPONENT
" VAR_2
" /NAMESPACE/VARIABLE
"something like "CUT->ST03N_TRANSACTION_PROFILE" could also be a lhs
set_offset_for_regex( c_regex_lhs ).
add_to_trace( ).
CLEANUP.
reset_to_sync_point( current_sync_point_index ).
ENDTRY.
ENDMETHOD.
METHOD add_number.
TRY.
find_spaces( ).
DATA(current_sync_point_index) = set_sync_point( c_terminal-number ).
set_offset_for_regex( c_regex_number ).
add_to_trace( ).
CLEANUP.
reset_to_sync_point( current_sync_point_index ).
ENDTRY.
ENDMETHOD.
METHOD non_terminal.
TRY.
find_spaces( ).
DATA(current_sync_point_index) = set_sync_point( i_terminal = c_terminal-non_terminal i_sub_terminal = i_sub_terminal ).
set_offset_for_regex( i_regex ).
add_to_trace( ).
CLEANUP.
reset_to_sync_point( current_sync_point_index ).
ENDTRY.
ENDMETHOD.
METHOD add_text_literal.
TRY.
find_spaces( ).
DATA(current_sync_point_index) = set_sync_point( c_terminal-text_literal ).
set_offset_for_regex( c_regex_text_literal ).
add_to_trace( ).
CLEANUP.
reset_to_sync_point( current_sync_point_index ).
ENDTRY.
ENDMETHOD.
METHOD add_string_literal.
TRY.
find_spaces( ).
DATA(current_sync_point_index) = set_sync_point( c_terminal-string_literal ).
set_offset_for_regex( c_regex_string_literal ).
add_to_trace( ).
CLEANUP.
reset_to_sync_point( current_sync_point_index ).
ENDTRY.
ENDMETHOD.
METHOD find_spaces.
IF me->offset >= strlen( me->text ).
RETURN.
ENDIF.
FIND FIRST OCCURRENCE OF REGEX '^\s+' IN me->text+me->offset MATCH LENGTH DATA(length).
IF sy-subrc = 0.
APPEND VALUE ty_trace(
sync_point = 0
parent_sync_point = 0
terminal = c_terminal-spaces
sub_terminal = c_sub_terminal-na
offset = me->offset
length = length
) TO me->trace_tab.
me->offset = me->offset + length.
ENDIF.
ENDMETHOD.
METHOD set_offset_for_regex.
IF me->offset >= strlen( me->text ).
RAISE EXCEPTION TYPE lcx_no_match.
ENDIF.
DATA(regex2) = '^' && i_regex.
FIND FIRST OCCURRENCE OF REGEX regex2 IN me->text+me->offset MATCH LENGTH DATA(length).
IF sy-subrc = 0.
me->offset = me->offset + length.
ELSE.
RAISE EXCEPTION TYPE lcx_no_match.
ENDIF.
ENDMETHOD.
METHOD zif_op_value_pretty_printer~format.
* start : assign
*
* assign : symbolname = RHS
*
* RHS : number | text | string | empty | struct | table
*
* empty : 'VALUE #( )'
*
* add_structure_rhs : 'VALUE #(' struct2 ')'
*
* add_structure_content : ( LHS (left hand side) '=' RHS (right hand side) )+
*
* table : 'VALUE #(' ( '( )' | struct2 ( '(' RHS2 ')' )+ | ( '(' RHS ')' )+ ')'
*
* RHS2 : number | text | string | empty | struct2
*
* LHS : ^[A-Z][A-Z0-9_\-]+
*
* number : ^[0-9]+
*
* text : ^'(?:''|[^'])+'
me->text = i_unformated_value_content.
me->offset = 0.
me->trace_tab = VALUE #( ).
me->sync_points = VALUE #( ).
TRY.
assign( ).
CATCH:
lcx_parser_interrupt,
lcx_no_match INTO DATA(lx_error).
zcl_op_debugger_integration=>debug_debugger_if_needed( ).
ENDTRY.
DATA(indent) = 0.
DATA(indent_step) = 8.
DATA(indent_spaces) = repeat( val = ` ` occ = indent ).
LOOP AT me->trace_tab REFERENCE INTO DATA(trace_line).
CASE trace_line->terminal.
WHEN c_terminal-symbolname
OR c_terminal-non_terminal
OR c_terminal-number
OR c_terminal-text_literal
OR c_terminal-string_literal
OR c_terminal-empty
OR c_terminal-spaces.
r_formated_content = r_formated_content && substring( val = i_unformated_value_content off = trace_line->offset len = trace_line->length ).
ENDCASE.
IF trace_line->terminal = c_terminal-iteration.
r_formated_content = r_formated_content && |\n| && indent_spaces.
ELSEIF trace_line->sub_terminal = c_sub_terminal-value_operator
OR trace_line->sub_terminal = c_sub_terminal-parenthesis_open.
indent = indent + indent_step.
indent_spaces = indent_spaces && repeat( val = ` ` occ = indent_step ).
r_formated_content = r_formated_content && |\n| && indent_spaces.
ELSEIF trace_line->sub_terminal = c_sub_terminal-parenthesis_close.
indent = indent - indent_step.
SHIFT indent_spaces LEFT BY indent_step PLACES.
ENDIF.
ENDLOOP.
ENDMETHOD.
METHOD add_to_trace.
ASSIGN me->sync_points[ lines( me->sync_points ) ] TO FIELD-SYMBOL(<sync_point>).
APPEND VALUE ty_trace(
LET length = me->offset - <sync_point>-offset IN
sync_point = lines( me->sync_points )
parent_sync_point = lines( me->sync_points ) - 1
terminal = <sync_point>-terminal
sub_terminal = <sync_point>-sub_terminal
offset = <sync_point>-offset
length = length
) TO me->trace_tab.
DELETE me->sync_points INDEX lines( me->sync_points ).
IF 0 = 1.
RAISE EXCEPTION TYPE lcx_parser_interrupt.
ENDIF.
ENDMETHOD.
METHOD set_sync_point.
APPEND VALUE ty_sync_point(
terminal = i_terminal
sub_terminal = i_sub_terminal
offset = me->offset
trace_index = lines( me->trace_tab )
) TO me->sync_points.
r_current_sync_point_index = lines( me->sync_points ).
ENDMETHOD.
METHOD reset_to_sync_point.
DELETE me->trace_tab FROM me->sync_points[ i_sync_point_index ]-trace_index + 1.
me->offset = me->sync_points[ i_sync_point_index ]-offset.
DELETE me->sync_points FROM i_sync_point_index.
IF 0 = 1.
RAISE EXCEPTION TYPE lcx_parser_interrupt.
ENDIF.
ENDMETHOD.
METHOD cancel_sync_point.
DELETE me->trace_tab FROM me->sync_points[ i_sync_point_index ]-trace_index + 1.
DELETE me->sync_points FROM i_sync_point_index.
IF 0 = 1.
RAISE EXCEPTION TYPE lcx_parser_interrupt.
ENDIF.
ENDMETHOD.
METHOD add_value_open_operator.
non_terminal( i_sub_terminal = c_sub_terminal-value_operator i_regex = `VALUE #\(` ).
ENDMETHOD.
METHOD add_parenthesis_close.
non_terminal( i_sub_terminal = c_sub_terminal-parenthesis_close i_regex = `\)` ).
ENDMETHOD.
METHOD add_assign_operator.
non_terminal( i_sub_terminal = c_sub_terminal-assign_operator i_regex = '=' ).
ENDMETHOD.
METHOD add_end_of_statement.
non_terminal( i_sub_terminal = c_sub_terminal-end_of_statement i_regex = '\.' ).
ENDMETHOD.
METHOD add_empty_line.
non_terminal( i_sub_terminal = c_sub_terminal-empty_line i_regex = '\( \)' ).
ENDMETHOD.
METHOD add_parenthesis_open.
non_terminal( i_sub_terminal = c_sub_terminal-parenthesis_open i_regex = '\(' ).
ENDMETHOD.
ENDCLASS.