-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcppParser.py
More file actions
831 lines (767 loc) · 28.1 KB
/
cppParser.py
File metadata and controls
831 lines (767 loc) · 28.1 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
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
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
# Complete Arduino Structure Comment Parser
# A two-phase parser that:
# 1) Formats Arduino code using clang-format (plus indents else-if blocks)
# 2) Adds structure comments with focus on functions and control structures, correctly handling if-else chains
# Usage: ./arduino_parser.py input.ino -o output.ino
# """
import subprocess
import sys
import os
import argparse
import tempfile
import re
import string
from typing import List, Tuple, Optional, Set
PRINTCODE = True
# PRINTCODE = False;
STRUCTURE_TAGS = {
"if": ("beginif", "endif"), # // //
"for": ("beginfor", "endfor"),
"while": ("beginwhile", "endwhile"),
"switch": ("beginswitch", "endswitch"),
"function": ("beginfunc", "endfunc"),
"class": ("beginclass", "endclass"),
}
config = """#// //
Language: Cpp
BasedOnStyle: Google
IndentWidth: 4
AccessModifierOffset: -4
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
BreakBeforeBraces: Allman
ColumnLimit: 100
PointerAlignment: Left
SortIncludes: false
ReflowComments: false
UseTab: Never
SpacesBeforeTrailingComments: 1
"""
def filter_ascii(text):
# """Keep only printable ASCII characters""
return "".join(char for char in text if ord(char) < 128)
def create_clang_config(): # beginfunc
"""Create a temporary clang-format configuration file for Arduino code."""
fd, path = tempfile.mkstemp(prefix=".clang-format-", text=True)
with os.fdopen(fd, "w") as f: # beginwith
f.write(config)
# endwith
return path
# endfunc
def indent_else_blocks(code: str) -> str: # beginfunc
"""
After formatting, indent 'else'/'else if' lines one level deeper
under the preceding block's closing brace.
"""
lines = code.split("\n")
new_lines = []
for line in lines: # beginfor
stripped = line.lstrip()
if stripped.startswith("else") and new_lines: # beginif
prev = new_lines[-1]
prev_indent = len(prev) - len(prev.lstrip())
new_indent = prev_indent + 4
new_lines.append(" " * new_indent + stripped)
else:
new_lines.append(line)
# endif
# endfor
return "\n".join(new_lines)
# endfunc
def format_code(code: str) -> str: # beginfunc
"""
Format Arduino code using clang-format, then indent else-if blocks.
Args:
code: The original code
Returns:
Formatted code with else-if indented
"""
try: # begintry
# config_path = create_clang_config()
# formatted = proc.stdout.decode('utf-8')
# os.unlink(config_path)
script_dir = os.path.dirname(os.path.abspath(__file__)) # ////
print(
f"Script is running from: {script_dir} - make sure clang-format is also installed on path"
) # // //////
clang_format_path = r"clang-format" # clang_format_path = r"C:\\Users\\lopezl10\\AppData\\Roaming\\Python\\Python312\\Scripts\\clang-format"
clang_yaml_path = os.path.join(
script_dir, "vfc.yaml"
) # clang_yaml_path = r"C:\\Users\\lopezl10\\AppData\\Local\\RedHorseVR\\C2VFC_parser\\vfc.yaml"
stream = os.popen(
f'"{clang_format_path}" -style=file:"{clang_yaml_path}" "{code_file}"'
) # stream = os.popen(f'"{clang_format_path}" "{code_file}"')
formatted = stream.read() # ////
# print( "--->" + code_file )
# if PRINTCODE : print( "Formatted Code: \n\n" , formatted + "\n\n")
return formatted # return indent_else_blocks(formatted)
except Exception as e:
print(f"Error during formatting: {e}", file=sys.stderr)
sys.exit(1)
# endtry
# endfunc
def add_structure_comments(code: str) -> str: # beginfunc
"""
Add structure comments focusing only on selected block types, handling if-else chains.
Args:
code: The formatted code
Returns:
Code with added structure comments
"""
lines = code.split("\n")
result = lines.copy()
function_regex = re.compile(
r"^\s*(?!(?:if|for|while|else|switch)\b)[\w\s\*\&\:\<\>\~]+\w+\s*\([^;{]*\)\s*$"
)
blocks: List[Tuple[str, int, Optional[int], Optional[int]]] = []
brace_stack: List[Tuple[int, int, Optional[int]]] = []
tagged_lines: Set[int] = set()
# First pass - detect blocks and track braces
for i, line in enumerate(lines): # beginfor
stripped = line.strip()
if not stripped or stripped.startswith("//"): # beginif
continue # // //
# endif
indent = len(line) - len(line.lstrip())
# More precise detection of control structures
if re.search(r"^\s*if\s*\(", line): # beginif
blocks.append(("if", i, None, None))
elif re.search(r"^\s*for\s*\(", line): # beginelif
blocks.append(("for", i, None, None))
elif re.search(r"^\s*while\s*\(", line): # beginelif
blocks.append(("while", i, None, None))
elif re.search(r"^\s*switch\s*\(", line): # beginelif
blocks.append(("switch", i, None, None))
elif re.search(r"^\s*class\s*\(", line): # beginelif
blocks.append(("class", i, None, None))
elif function_regex.match(line): # beginelif
blocks.append(("function", i, None, None))
# endlif
# endlif
# endlif
# endlif
# endif
# Track opening braces
if "{" in stripped: # beginif
# Check if this opening brace belongs to a control structure
# Look at recent blocks that don't have an opening brace assigned yet
potential_owners = [
(idx, block)
for idx, block in enumerate(blocks)
if block[2] is None and block[1] <= i and i - block[1] <= 2
]
if potential_owners: # beginif
# Associate this brace with the most recent matching block
bidx = potential_owners[-1][0]
btype, sline, _, eline = blocks[bidx]
blocks[bidx] = (btype, sline, i, eline)
else:
bidx = None
# endif
brace_stack.append((i, indent, bidx))
# endif
# Track closing braces
if "}" in stripped: # beginif
if brace_stack: # beginif
_, _, bidx = brace_stack.pop()
if bidx is not None: # beginif
btype, sline, ob, _ = blocks[bidx]
blocks[bidx] = (btype, sline, ob, i)
# endif
# endif
# endif
# endfor
# Process if-else chains
updated = []
for btype, sline, ob, eline in blocks: # beginfor
if btype == "if" and eline is not None: # beginif
chain_end = eline
j = eline + 1
while j < len(lines): # beginwhile
st = lines[j].strip()
if not st or st.startswith("//"): # beginif
j += 1
continue
# endif
if st.startswith("else"): # beginif
# locate '{'# locate 0
if "{" in st: # beginif
open_j = j
else:
k = j + 1
while k < len(lines) and "{" not in lines[k]: # beginwhile
k += 1
# endwhile
open_j = k
# endif
count = 0
for ch in lines[open_j]: # beginfor
if ch == "{": # beginif
count += 1
# endif
if ch == "}": # beginif
count -= 1
# endif
# endfor
m = open_j + 1
while m < len(lines) and count > 0: # beginwhile
for ch in lines[m]: # beginfor
if ch == "{": # beginif
count += 1
# endif
if ch == "}": # beginif
count -= 1
# endif
# endfor
m += 1
# endwhile
chain_end = m - 1
j = chain_end + 1
continue
# endif
break
# endwhile
updated.append((btype, sline, ob, chain_end))
else:
updated.append((btype, sline, ob, eline))
# endif
# endfor
blocks = updated
# Apply structure comments
for btype, sline, ob, eline in blocks: # beginfor
if (
btype in STRUCTURE_TAGS and sline is not None and eline is not None
): # beginif
start_tag, end_tag = STRUCTURE_TAGS[btype]
if sline not in tagged_lines and "//" not in result[sline]: # beginif
result[sline] = f"{result[sline]} //{start_tag}"
tagged_lines.add(sline)
# endif
if eline not in tagged_lines and "//" not in result[eline]: # beginif
result[eline] = f"{result[eline]} //{end_tag}"
tagged_lines.add(eline)
# endif
# endif
# endfor
return "\n".join(result)
# endfunc
def process_file(
input_file: str, output_file: str = None, skip_format: bool = False
) -> None: # beginfunc
global code_file # ////
code_file = input_file # ////
try: # begintry
code = open(input_file).read()
code = filter_ascii(code)
formatted = code if skip_format else format_code(code)
final = add_structure_comments(formatted)
if output_file: # beginif
open(output_file, "w").write(final)
print(f"Output written to {output_file}")
else:
pass # print(final)
# endif
except Exception as e:
print(f"Error: {e}", file=sys.stderr)
sys.exit(1)
# endtry
return final
# endfunc
############################################ HUMAN ONLY CAN MODIFY BELOW# ########################################### HUMAN ONLY CAN MODIFY BELOW
VFCSEPERATOR = ";//"
Begins = [ # // //
"beginfunc",
"beginmethod",
"beginclass",
"beginif",
"begintry",
"beginswitch",
"beginwith",
"beginwhile",
"beginfor",
]
Ends = [ # // //
"endfunc",
"endmethod",
"endclass",
"endif",
"endtry",
"endswitch",
"endwith",
"endfor",
"endwhile",
]
begin_type = { # // //
"beginfunc": "input",
"beginmethod": "input",
"beginclass": "input",
"beginif": "branch",
"begintry": "branch",
"beginswitch": "branch",
"beginwith": "branch",
"beginwhile": "loop",
"beginfor": "loop",
}
end_type = { # // //
"endfunc": "end",
"endmethod": "end",
"endclass": "end",
"endif": "bend",
"endtry": "bend",
"endswitch": "bend",
"endwith": "bend",
"endfor": "lend",
"endwhile": "lend",
}
paths = [ # // //
"else if",
"else",
"case",
"except",
"finally",
]
ends = [ # // //
"return",
"continue",
"break",
]
events = [ # // //
"#include",
"#define",
"using",
"delay",
]
outputs = [ # // //
"Serial",
"write",
"cout",
]
def is_path(line: str) -> bool: # beginfunc
""" """
parts = line.strip().split(None, 1)
if not parts: # beginif
return False
# endif
if parts[0].strip(" :") in paths: # beginif
return True
# endif
# endfunc
def replace_string_literals(input_string): # beginfunc
result = re.sub(r'(["\'])(.*?)(\1)', "0", input_string)
return result
# endfunc
def split_on_comment(input_string): # beginfunc
match = re.search(r'(?<!")#.*$', temp_str)
if match: # beginif
s1 = input_string.strip()
s2 = match.strip()
else:
s1, s2 = input_string.strip(), ""
# endif
return (s1, s2)
# endfunc
INLINECOMMENT = "//"
def split_string(input_string): # beginfunc
temp_str = replace_string_literals(input_string)
parts = temp_str.split(INLINECOMMENT, 1)
s1 = input_string.strip()
if len(parts) > 1: # beginif
s2 = parts[1]
s1 = s1.replace(INLINECOMMENT + s2, "")
else:
s2 = ""
# endif
return (s1, s2)
# endfunc
def get_marker(comment): # beginfunc
parts = comment.strip().split(None, 1)
if not parts: # beginif
return "none"
# endif
marker = parts[0]
return marker
# endfunc
def first_token(code): # beginfunc
tokens = re.split(r"[.;(]+", code.strip())
return tokens[0] if tokens else "none"
# endfunc
def get_VFC_type(code: str, line: str) -> Optional[str]: # beginfunc
# """
# If the first word of `line` (without any leading INLINECOMMENT ) is in Begins or Ends,
# returns its mapped type; otherwise returns None.
# """
token = code.strip().split(None, 1)[0] if len(code) > 1 else "none"
if first_token(code) in outputs:
# // beginif//
return "output" # // //
elif first_token(code) in ends: # beginif
return "end" # // //
elif token in events: # // beginif//
return "event" # // //
elif is_path(code): # // beginif//
return "path" # // //
# endif
parts = line.strip().split(None, 1)
if not parts: # beginif
return "set"
# endif
marker = parts[0]
if marker in Begins: # beginif
return begin_type[marker]
# endif
if marker in Ends: # beginif
return end_type[marker]
# endif
return "set"
# endfunc
def generate_VFC(input_string): # beginfunc
DEBUG = False # DEBUG = True
strings = input_string.split("\n")
VFC = ""
fix_stack = []
types = "(void|bool|char|wchar_t|char8_t|char16_t|char32_t|short|int|long|longlong|float|double|longdouble)" # ////
integers = "(unsigned +char|unsigned +short|unsigned +int|unsigned +long|unsigned +long)" # ////
CLASS_TYPE = r"^\s*(?:enum\s+|struct\s+|interface\s+|abstract\s+)?class\s+\w+\b(?!.*;\s*$)" # CLASS_TYPE = r'^\s*(\w\s)*class\b'
STRUCT_ENUM_TYPE = r"^\s*(typedef\s+)?(struct|enum(?!\s+class))\s+\w+\b(?!.*;\s*$)"
function_type = r"(?:void|int|float|double|char|long|short|bool|inline|static|extern|APIENTRY|\w|\*|&)*\s+\w+\s*\("
OUTPUT_types = ["cout", "read", "write", "print", "send", "echo"]
method_type = (
r"\b[\w\s&\*]+::" # method_type = r'\b[\w\s&\*]+::\w+\s*\([^)]*\)\s*\{'
)
prev_type = "set"
prev_code = ""
for string in strings: # beginfor
if not string.strip(): # beginif
# VFC += f"set(){VFCSEPERATOR}\n"
VFC += f"set(){VFCSEPERATOR}\n"
continue # pass
# endif
code, comment = split_string(string)
code = code.strip()
type = get_VFC_type(code, comment)
# --------------------------------------------------------------------------------------------------------- FIX##--------------------------------------------------------------------------------------------------------- FIX
# --------------------------------------------------------------------------------------------------------- FIX##--------------------------------------------------------------------------------------------------------- FIX
# --------------------------------------------------------------------------------------------------------- FIX##--------------------------------------------------------------------------------------------------------- FIX
if (
re.match(r"^if\b", code) or re.match(STRUCT_ENUM_TYPE, code)
) and type != "branch":
type = "branch"
if not re.match(r"^if\b.*;$", code):
fix_stack.append("bend")
# endif
if DEBUG:
comment = " + br " + comment
elif code == "{" and (
prev_type == "path" or "case" in prev_code
): # elif code == '{' :
fix_stack.append("end")
if DEBUG:
comment = " + p{ " + comment
elif "#pragma" in code:
type = "event"
if DEBUG:
comment = " +ev " + comment
elif type == "path" and "case" in prev_code: # collapse paths
type = "set" # collapse paths
if DEBUG:
comment = " +cp " + comment
elif re.match(r"^try\b", code) and type == "set":
type = "branch"
fix_stack.append("bend")
if DEBUG:
comment = " + try " + comment
elif re.match(
r".*\bcase\b", code
): # elif re.match( r'} catch (const exception& e) {', code ) and type == 'set' :
type = "path"
if DEBUG:
comment = " + case " + comment
elif re.match(
r".*\belse\b *if\b", code
): # elif re.match( r'} catch (const exception& e) {', code ) and type == 'set' :
type = "path"
if DEBUG:
comment = " + case " + comment
elif re.match(
r".*\bcatch\b", code
): # elif re.match( r'} catch (const exception& e) {', code ) and type == 'set' :
type = "path"
if DEBUG:
comment = " + cat " + comment
elif re.match(r"^#if", code) and type == "set":
type = "branch"
if DEBUG:
comment = " + #if " + comment
elif (
re.match(r"(typedef +)*(enum|struct|union|namespace) *{$", code)
and type == "set"
):
type = "branch"
if DEBUG:
comment = " + #enum" + comment
fix_stack.append("bend")
#''' --------------------------------------------
elif re.match(r"^(namespace)", code) and type == "set": # ////////
type = "branch"
if DEBUG:
comment = " + namespace " + comment
fix_stack.append("bend")
# -------------------------------------------- '''
elif re.match(r"^#end", code) and type == "set":
type = "bend"
if DEBUG:
comment = " + #eif " + comment
elif re.match(r"^return\b", code) and type == "set":
type = "end"
if DEBUG:
comment = " + end " + comment
elif re.match(r"} while\b", code) and type == "set":
type = "lend"
if DEBUG:
comment = " + dw " + comment
elif (
re.match(r"^default\b", code)
or re.match(r"^#else", code)
or re.match(r"^#elif", code)
): # elif re.match( r'^case\b.*\{$', code ) :
type = "path"
# fix_stack.append( 'end' )
if DEBUG:
comment = " + def " + comment
elif re.match(r"\} else\b", code) and type != "path":
type = "path"
if DEBUG:
comment = " + pa " + comment
elif (
re.match(r"^while\b", code)
or re.match(r"^for\b", code)
or re.match(r"^do\b", code)
) and type != "loop":
type = "loop"
fix_stack.append("lend")
if DEBUG:
comment = " + lo " + comment
elif re.match(r"^inline\b.+\(.*\) *\{.*\}$", code):
if DEBUG:
comment = (
" + inline function " + comment
) # comment= ' + template function ' + comment
type = "input"
# fix_stack.append( 'end' )
elif re.match(r"^inline\b.+\(.*\) *\{$", code):
if DEBUG:
comment = (
" + inline function " + comment
) # comment= ' + template function ' + comment
type = "input"
fix_stack.append("end")
elif re.match(rf"^\w.+<.*>.*::.+\(.*\)", code):
if DEBUG:
comment = (
" + template function " + comment
) # comment= ' + template function ' + comment
type = "input"
fix_stack.append("end")
elif re.match(r"^(template|typedef)\b", code):
if DEBUG:
comment = (
" + template " + comment
) # comment= ' + template ' + comment
type = "event"
# fix_stack.append( 'end' )
elif (
re.match(function_type, code)
or re.match(method_type, code)
or re.match(r"\w*\s+APIENTRY", code)
or re.match(CLASS_TYPE, code)
):
type = "input"
if not "}" in code:
if not r";" in code:
fix_stack.append("end")
if DEBUG:
comment = " + in " + comment # comment= ' + in ' + comment
else:
type = "process"
if DEBUG:
comment = " + pr in " + comment
else:
if re.match(r".*;$", code):
type = "process"
else:
pass
if DEBUG:
comment = " + sl in " + comment
elif type == "set" and any(word in code for word in OUTPUT_types): # // //
type = "output" # pass
elif (
re.match(r"^}", code) and type == "set"
): # elif re.match( r'^}$', code ) and type == 'set' :
# try-catch-exception
try:
type = fix_stack.pop()
if DEBUG:
comment = " + pop " + comment
except:
type = "set" # type = 'bend'
if DEBUG:
comment = " + def pop " + comment
prev_type = type
prev_code = code
# --------------------------------------------------------------------------------------------------- FIX##--------------------------------------------------------------------------------------------------- FIX
# --------------------------------------------------------------------------------------------------- FIX##--------------------------------------------------------------------------------------------------- FIX
# --------------------------------------------------------------------------------------------------- FIX##--------------------------------------------------------------------------------------------------- FIX
marker = get_marker(comment)
if marker == "endclass": # beginif
VFC += f"bend(){VFCSEPERATOR}\n"
# endif
if type == "input":
pass # //VFC += f"end(){VFCSEPERATOR}\n"//
# endif
if re.match(CLASS_TYPE, code): # // if re.match( r'\s*class\b' , code ):////
VFC += f"end(){VFCSEPERATOR}\n"
# endif
if re.match(r"\};", code):
VFC += f"bend(){VFCSEPERATOR}\n" # VFC += f"bend(){VFCSEPERATOR}<--- end class\n"
type = "end"
# endif
if re.match(r"^(public|protected|private):", code) and type == "set":
type = "path"
# endif
if DEBUG:
pass # VFC += f'{type}({code}){VFCSEPERATOR} {comment}\n'
else:
token_list = [
"beginfor",
"endfor", # ////
"beginwhile",
"endwhile", # ////
"beginswitch",
"endswitch", # ////
"beginfunc",
"endfunc", # ////
"beginclass",
"endclass", # ////
"beginmethod",
"endmethod", # ////
"begininput",
"endinput", # ////
"beginif",
"endif", # ////
"begintry",
"endtry", # ////
"beginwith",
"endwith", # ////
"beginbranch",
"endbranch", # ////
"beginloop",
"endloop", # ////
] # // //////
pattern = (
r"^(" + "|".join(re.escape(token) for token in token_list) + r")\s*"
) # ////
comment = re.sub(pattern, "", comment.strip()) # ////
if INLINECOMMENT in code: # if INLINECOMMENT in code and comment in code :
VFC += f"{type}({code}){VFCSEPERATOR}\n" # // //
else:
VFC += f"{type}({code}){VFCSEPERATOR} {comment}\n" # // //
if re.match(CLASS_TYPE, code): # // if re.match( r'\s*class\b' , code ):////
VFC += f"branch(){VFCSEPERATOR}\n" # VFC += f"branch(){VFCSEPERATOR}<---class\n"
VFC += f"path(){VFCSEPERATOR}\n"
# VFC += f"path(){VFCSEPERATOR} --- \n"
# endif
if type == "branch": # beginif
VFC += f"path(){VFCSEPERATOR}\n"
# endif
if type == "branch" and re.match(r"^if\b.*;$", code):
VFC += f"bend(){VFCSEPERATOR}\n"
# endif
if marker == "beginclass": # beginif
VFC += f"branch(){VFCSEPERATOR}\n"
VFC += f"path(){VFCSEPERATOR}\n"
VFC += f"path(){VFCSEPERATOR}\n"
# endif
# endfor
VFC += f"set(){VFCSEPERATOR}\n"
VFC += f"set(){VFCSEPERATOR}\n"
VFC += f"set(){VFCSEPERATOR}\n"
return VFC
# endfunc
def footer(exportname): # beginfunc
ENVTOK = "INSECTA"
foot = f";{ENVTOK} EMBEDDED SESSION INFORMATION\n"
foot += "; 255 16777215 65280 16777088 16711680 32896 8421504 0 255 255 16777215 4227327 2960640\n"
foot += f"; { os.path.basename(exportname) } // \n"
foot += "; notepad.exe\n"
foot += f";{ENVTOK} EMBEDDED ALTSESSION INFORMATION\n"
foot += "; 880 168 766 1516 0 110 392 31 C++.key 0\n"
return foot
# endfunc
def __fix_VFC_paths(input_string): # beginfunc
strings = input_string.split("\n")
VFC = ""
skip_next = 0
for i in range(len(strings)): # beginfor
code = strings[i]
if code.startswith("branch"): # beginif
code2 = strings[i + 1].strip()
code3 = strings[i + 2].strip()
if code2.startswith("path()") and code3.startswith("set({)"): # beginif
VFC += code + "\n"
VFC += "path({)" + VFCSEPERATOR + "\n"
skip_next = 3
# endif
# endif
if skip_next > 0: # beginif
skip_next -= 1
continue
# endif
VFC += code + "\n"
# endfor
return VFC
# endfunc
def fix_VFC_paths(input_string): # beginfunc
strings = input_string.split("\n")
VFC = ""
skip_next = 0
for i in range(len(strings)): # beginfor
code = strings[i]
if code.startswith("branch"): # beginif
code2 = strings[i + 1].strip()
code3 = strings[i + 2].strip()
if code2.startswith("path()") and code3.startswith("set({)"): # beginif
VFC += code + "\n"
VFC += "path({)" + VFCSEPERATOR + "\n"
skip_next = 3
# endif
# endif
if skip_next > 0: # beginif
skip_next -= 1
continue
# endif
VFC += code + "\n"
# endfor
return VFC
# endfunc
def main(): # beginfunc
global modified_code
p = argparse.ArgumentParser()
p.add_argument("input_file")
p.add_argument("-o", "--output")
p.add_argument("--skip-format", action="store_true")
args = p.parse_args()
print("INPUT: ", args.input_file) # // //
modified_code = process_file(args.input_file, args.output, args.skip_format)
VFC = generate_VFC(modified_code)
VFC = fix_VFC_paths(VFC)
# print( modified_code )
with open(args.input_file + ".vfc", "w") as VFC_output: # beginwith
VFC_output.write(VFC)
VFC_output.write(footer(args.input_file))
# endwith
# endfunc
if __name__ == "__main__": # beginif
main()
# endif
# Export Date: 12:09:11 PM - 23:Apr:2025.