-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParsingImpl.cpp
1334 lines (1285 loc) · 65 KB
/
ParsingImpl.cpp
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
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//===- ParsingImpl.cpp - metadata type implement define -------------------*- C++ -*-===//
//
// This file implements Lexer class and parser class code.
//
//===--------------------------------------------------------------------------------===//
#include "stdafx.h"
#include "ParsingImpl.h"
#include "HipsCfgObject.h"
#include "HookImplementObject.h"
namespace cchips {
const std::map<std::string, CLexer::_token> CLexer::_token_str_def = {
{"if", token_if}, {"else", token_else}, {"while", token_while}, {"define", token_define}, {"extern", token_extern}, {"(", token_parentheses_b}, {")", token_parentheses_e},
{"{", token_curlybraces_b}, {"}", token_curlybraces_e}, {"[", token_brackets_b}, {"]", token_brackets_e},{"*", token_asterisk}, {"->", token_arrow},
{".", token_point}, {"=", token_assign}, {"'", token_s_quotes}, {"\"", token_d_quotes},
};
const std::map<std::string, CLexerTy::_token_ty> CLexerTy::_token_ty_str_def = {
{"LP", token_reference_b}, {"*", token_reference_e}, {"[", token_brackets_b}, {"]", token_brackets_e},
};
const std::string CLexerTy::_re2_pattern_ty_def = {
"^([A-Za-z_]\\w*)\\[([0-9]+)\\]$|^([A-Za-z_]\\w*)[*]$|^LP([A-Za-z_]\\w*)$|^([A-Za-z_]\\w*)$"
};
std::unique_ptr<RE2> CLexerTy::_pattern_ty_ptr = std::make_unique<RE2>(_re2_pattern_ty_def, RE2::Quiet);
const std::map<std::string, CLexerVr::_token_vr> CLexerVr::_token_vr_str_def = {
{"(", token_parentheses_b}, {")", token_parentheses_e}, {"*", token_asterisk}, {"->", token_arrow}, {".", token_point},
};
const std::string CLexerVr::_re2_pattern_vr_def = {
"^[*]([A-Za-z_(][A-Za-z_0-9)\\[\\].>-]*)$|^[(]([A-Za-z_*][A-Za-z0-9_\\[\\].>-]*)[)]([A-Za-z_0-9\\[\\].>-]*)$|^([A-Za-z_][A-Za-z_0-9\\[\\]]*)->([A-Za-z_][A-Za-z_0-9\\[\\].>-]*)$|^([A-Za-z_][A-Za-z_0-9\\[\\]]*)[.]([A-Za-z_][A-Za-z_0-9\\[\\].>-]*)$|^([A-Za-z_]\\w*)\\[([0-9]+)\\]$|^([A-Za-z_]\\w*)$"
};
std::unique_ptr<RE2> CLexerVr::_pattern_vr_ptr = std::make_unique<RE2>(_re2_pattern_vr_def, RE2::Quiet);
const std::map<CExpParsing::_token, std::string> CExpParsing::_token_str_def = {
{token_invalid, "invalid"}, {token_string, "string"}, {token_integral, "integral"}, {token_float_pointer, "float_pointer"},
};
const std::map<std::string, int> CExpParsing::_op_precedence = {
{"(", -10}, {")", -10}, {"=", -7}, {"||", -5}, {"&&", -4}, {"|", -3}, {"&", -2}, {">", -1}, {">=", -1}, {"<", -1}, {"<=", -1}, {"==", -1}, {"!=", -1}, {"=~", -1}, {"!~", -1},
{"<<", 1}, {">>", 1}, {"+", 2}, {"-", 2}, {"*", 3}, {"/", 3}, {"^", 4}, {"!", 5},
};
const std::map<std::string, int> CExpParsing::_op_type_def = {
{"=", binary_type}, {"||", binary_type}, {"&&", binary_type}, {"|", binary_type}, {"&", binary_type}, {">", binary_type}, {">=", binary_type}, {"<", binary_type}, {"<=", binary_type}, {"==", binary_type}, {"!=", binary_type},
{"=~", binary_type}, {"!~", binary_type}, {"<<", binary_type}, {">>", binary_type}, {"+", binary_type}, {"-", binary_type}, {"*", binary_type}, {"/", binary_type}, {"^", binary_type}, {"!", unary_type},
};
const std::string CExpParsing::_token_var_def = "%var";
const std::string CExpParsing::_re2_pattern_exp_def = { "^(%var[0-9]+)$|^'([\\s\\S]+)'$|^\"([\\s\\S]+)\"$|^([1-9]\\d*)$|^([1-9]\\d*\\.\\d*)$|^(0\\.\\d*[1-9]\\d*)$"};
std::unique_ptr<RE2> CExpParsing::_pattern_exp_ptr = std::make_unique<RE2>(_re2_pattern_exp_def, RE2::Quiet);
const std::map<std::string, CPrototype::_class_type> CPrototype::_class_predefine = {
{"Normal", CPrototype::class_normal}, {"WMI", CPrototype::class_wmi}, {"VBE", CPrototype::class_vbs}, {"VBS", CPrototype::class_vbs}, {"PS", CPrototype::class_ps}, {"KVM", CPrototype::class_kvm}, {"CShape", CPrototype::class_cshape}, {"INLINE", CPrototype::class_inline},
};
std::string CLexerTy::GetWordRoot(const std::string_view& iden_str)
{
ASSERT(iden_str.length());
if (!iden_str.length()) return {};
std::string root_str(iden_str);
std::string iden_arr_str, sub_str, iden_poi_str, iden_lp_str, res_str;
auto func = make_y_combinator(
[](auto&& func, std::string& search_str, std::unique_ptr<RE2>& pattern, std::string& iden_arr_str, std::string& sub_str, std::string& iden_poi_str, std::string& iden_lp_str, std::string& res_str) ->bool{
if (RE2::FullMatch(search_str, *pattern, &iden_arr_str, &sub_str, &iden_poi_str, &iden_lp_str, &res_str))
{
if (res_str.length())
{
search_str = res_str;
return true;
}
else if (iden_poi_str.length())
{
search_str = iden_poi_str;
return func(search_str, pattern, iden_arr_str, sub_str, iden_poi_str, iden_lp_str, res_str);
}
else if (iden_lp_str.length())
{
search_str = iden_lp_str;
return func(search_str, pattern, iden_arr_str, sub_str, iden_poi_str, iden_lp_str, res_str);
}
else if (iden_arr_str.length() && sub_str.length())
{
search_str = iden_arr_str;
return func(search_str, pattern, iden_arr_str, sub_str, iden_poi_str, iden_lp_str, res_str);
}
}
return false;
}
);
if(func(root_str, _pattern_ty_ptr, iden_arr_str, sub_str, iden_poi_str, iden_lp_str, res_str))
return res_str;
return root_str;
}
std::shared_ptr<CObObject> CLexerTy::GetIdentifier(const std::string_view& iden_str) const
{
ASSERT(iden_str.length());
if (!iden_str.length()) return nullptr;
std::string root_str(iden_str);
std::string iden_arr_str, sub_str, iden_poi_str, iden_lp_str, res_str;
std::shared_ptr<CObObject> ob_ptr;
auto func = make_y_combinator(
[](auto&& func, std::string& search_str, std::unique_ptr<RE2>& pattern, std::string& iden_arr_str, std::string& sub_str, std::string& iden_poi_str, std::string& iden_lp_str, std::string& res_str) ->std::shared_ptr<CObObject> {
if (RE2::FullMatch(search_str, *pattern, &iden_arr_str, &sub_str, &iden_poi_str, &iden_lp_str, &res_str))
{
if (res_str.length())
{
return std::move(CTypeSymbolTableObject::GetInstance().GetTypeSymbolReference(res_str));
}
else if (iden_poi_str.length())
{
auto ref_ptr = std::make_shared<CReferenceObject>(search_str);
ASSERT(ref_ptr != nullptr);
if (!ref_ptr) return nullptr;
search_str = iden_poi_str;
std::shared_ptr<CObObject> ob_ptr = func(search_str, pattern, iden_arr_str, sub_str, iden_poi_str, iden_lp_str, res_str);
if (!ob_ptr) return nullptr;
ref_ptr->AddReference(std::move(ob_ptr));
return ref_ptr;
}
else if (iden_lp_str.length())
{
auto ref_ptr = std::make_shared<CReferenceObject>(search_str);
ASSERT(ref_ptr != nullptr);
if (!ref_ptr) return nullptr;
search_str = iden_lp_str;
std::shared_ptr<CObObject> ob_ptr = func(search_str, pattern, iden_arr_str, sub_str, iden_poi_str, iden_lp_str, res_str);
if (!ob_ptr) return nullptr;
ref_ptr->AddReference(std::move(ob_ptr));
return ref_ptr;
}
else if (iden_arr_str.length() && sub_str.length())
{
int dim = atoi(sub_str.c_str());
auto array_ptr = std::make_shared<CArrayObject>(search_str);
ASSERT(array_ptr != nullptr);
if (!array_ptr) return nullptr;
search_str = iden_arr_str;
std::shared_ptr<CObObject> ob_ptr = func(search_str, pattern, iden_arr_str, sub_str, iden_poi_str, iden_lp_str, res_str);
if (!ob_ptr) return nullptr;
array_ptr->AddArray(std::move(ob_ptr), dim);
return array_ptr;
}
}
return nullptr;
}
);
ob_ptr = func(root_str, _pattern_ty_ptr, iden_arr_str, sub_str, iden_poi_str, iden_lp_str, res_str);
return ob_ptr;
}
std::string CLexerVr::GetWordRoot(const std::string_view& iden_str)
{
ASSERT(iden_str.length());
if (!iden_str.length()) return {};
std::string root_str(iden_str);
std::string iden_ast_str, iden_par_b_str, iden_par_e_str, iden_arrow_b_str, iden_arrow_e_str, iden_poi_b_str, iden_poi_e_str, iden_arr_str, sub_str, res_str;
auto func = make_y_combinator(
[](auto&& func, std::string& search_str, std::unique_ptr<RE2>& pattern, std::string& iden_ast_str, std::string& iden_par_b_str, std::string& iden_par_e_str, std::string& iden_arrow_b_str, std::string& iden_arrow_e_str,
std::string& iden_poi_b_str, std::string& iden_poi_e_str, std::string& iden_arr_str, std::string& sub_str, std::string& res_str) ->bool {
if (RE2::FullMatch(search_str, *pattern, &iden_ast_str, &iden_par_b_str, &iden_par_e_str, &iden_arrow_b_str, &iden_arrow_e_str, &iden_poi_b_str, &iden_poi_e_str, &iden_arr_str, &sub_str, &res_str))
{
if (res_str.length())
{
search_str = res_str;
return true;
}
else if (iden_poi_b_str.length())
{
search_str = iden_poi_b_str;
return func(search_str, pattern, iden_ast_str, iden_par_b_str, iden_par_e_str, iden_arrow_b_str, iden_arrow_e_str, iden_poi_b_str, iden_poi_e_str, iden_arr_str, sub_str, res_str);
}
else if (iden_arrow_b_str.length())
{
search_str = iden_arrow_b_str;
return func(search_str, pattern, iden_ast_str, iden_par_b_str, iden_par_e_str, iden_arrow_b_str, iden_arrow_e_str, iden_poi_b_str, iden_poi_e_str, iden_arr_str, sub_str, res_str);
}
else if (iden_ast_str.length())
{
search_str = iden_ast_str;
return func(search_str, pattern, iden_ast_str, iden_par_b_str, iden_par_e_str, iden_arrow_b_str, iden_arrow_e_str, iden_poi_b_str, iden_poi_e_str, iden_arr_str, sub_str, res_str);
}
else if (iden_par_b_str.length())
{
search_str = iden_par_b_str;
return func(search_str, pattern, iden_ast_str, iden_par_b_str, iden_par_e_str, iden_arrow_b_str, iden_arrow_e_str, iden_poi_b_str, iden_poi_e_str, iden_arr_str, sub_str, res_str);
}
else if (iden_arr_str.length())
{
search_str = iden_arr_str;
return func(search_str, pattern, iden_ast_str, iden_par_b_str, iden_par_e_str, iden_arrow_b_str, iden_arrow_e_str, iden_poi_b_str, iden_poi_e_str, iden_arr_str, sub_str, res_str);
}
}
return false;
}
);
if (func(root_str, _pattern_vr_ptr, iden_ast_str, iden_par_b_str, iden_par_e_str, iden_arrow_b_str, iden_arrow_e_str, iden_poi_b_str, iden_poi_e_str, iden_arr_str, sub_str, res_str))
return res_str;
return {};
}
std::shared_ptr<CObObject> CLexerVr::GetIdentifier(const std::string_view& iden_str) const
{
if (!GetVariableSymbolTable()) return nullptr;
ASSERT(iden_str.length());
if (!iden_str.length()) return nullptr;
std::string root_str(iden_str);
std::string iden_ast_str, iden_par_b_str, iden_par_e_str, iden_arrow_b_str, iden_arrow_e_str, iden_poi_b_str, iden_poi_e_str, iden_arr_str, sub_str, res_str;
auto func = make_y_combinator(
[this](auto&& func, std::string& search_str, std::unique_ptr<RE2>& pattern, std::string& iden_ast_str, std::string& iden_par_b_str, std::string& iden_par_e_str, std::string& iden_arrow_b_str, std::string& iden_arrow_e_str,
std::string& iden_poi_b_str, std::string& iden_poi_e_str, std::string& iden_arr_str, std::string& sub_str, std::string& res_str, std::shared_ptr<CTupleObject> final_ptr, bool bast = false) ->bool {
auto init_func = [](std::shared_ptr<CObObject>& ptr, std::string_view elem_name, std::shared_ptr<CTupleObject>& final_ptr) ->bool {
if (!ptr) return false;
if (!elem_name.length()) return false;
CTupleObject::_tuple_elem elem{};
elem.ptr = ptr;
elem.elem_name = elem_name;
if (elem.ptr->IsCommReference()) {
elem.ref.tuple_ref = CTupleObject::_tuple_ref::tuple_ref_addr;
}
else if (elem.ptr->IsArray()) {
elem.arr.dim = static_cast<unsigned int>(-1);
}
else if (elem.ptr->IsStruct()) {
elem.struc.idx = static_cast<unsigned int>(-1);
}
else if (elem.ptr->IsFlag()) {
elem.flag.tuple_flg = CTupleObject::_tuple_flg::tuple_flg_str;
}
else {
elem.arr.dim = 0;
}
return final_ptr->AddElement(elem);
};
if (!final_ptr) return false;
if (RE2::FullMatch(search_str, *pattern, &iden_ast_str, &iden_par_b_str, &iden_par_e_str, &iden_arrow_b_str, &iden_arrow_e_str, &iden_poi_b_str, &iden_poi_e_str, &iden_arr_str, &sub_str, &res_str))
{
if (res_str.length())
{
if (final_ptr->GetElemCount())
{
std::any anyvalue = final_ptr->GetElement(static_cast<unsigned int>(-1));
if (!anyvalue.has_value() || anyvalue.type() != typeid(CTupleObject::_tuple_elem*)) return false;
auto elem = std::any_cast<CTupleObject::_tuple_elem*>(anyvalue);
if (!elem->ptr || !elem->ptr->IsStruct()) return false;
std::shared_ptr<CStructObject> struct_ptr = std::static_pointer_cast<CStructObject>(elem->ptr);
elem->struc.idx = struct_ptr->GetElementIdx(res_str);
if (elem->struc.idx == static_cast<unsigned int>(-1)) return false;
return init_func(struct_ptr->GetElement(res_str), res_str, final_ptr);
}
else {
std::shared_ptr<CObObject> ob_ptr = GetSymbolReference(res_str);
if (!ob_ptr) return false;
bool bret = init_func(ob_ptr, res_str, final_ptr);
if (!bast)
return bret;
else {
if (!bret) return bret;
std::any anyvalue = final_ptr->GetElement(static_cast<unsigned int>(-1));
if (!anyvalue.has_value() || anyvalue.type() != typeid(CTupleObject::_tuple_elem*)) return false;
auto elem = std::any_cast<CTupleObject::_tuple_elem*>(anyvalue);
if (!elem->ptr || !elem->ptr->IsCommReference()) return false;
elem->ref.tuple_ref = CTupleObject::_tuple_ref::tuple_ref_val;
std::shared_ptr<CObObject> ptr = std::static_pointer_cast<CReferenceObject>(elem->ptr)->GetData();
if (!init_func(ptr, "*", final_ptr)) return false;
return true;
}
}
}
else if (iden_poi_b_str.length() && iden_poi_e_str.length())
{
search_str = iden_poi_b_str;
std::string bak_search_str = iden_poi_e_str;
bool bret = func(search_str, pattern, iden_ast_str, iden_par_b_str, iden_par_e_str, iden_arrow_b_str, iden_arrow_e_str, iden_poi_b_str, iden_poi_e_str, iden_arr_str, sub_str, res_str, final_ptr, bast);
if (bret) {
if (!final_ptr) return false;
std::any anyvalue = final_ptr->GetElement(static_cast<unsigned int>(-1));
if (!anyvalue.has_value() || anyvalue.type() != typeid(CTupleObject::_tuple_elem*)) return false;
auto elem = std::any_cast<CTupleObject::_tuple_elem*>(anyvalue);
if (!elem->ptr || !elem->ptr->IsStruct()) return false;
search_str = bak_search_str;
return func(search_str, pattern, iden_ast_str, iden_par_b_str, iden_par_e_str, iden_arrow_b_str, iden_arrow_e_str, iden_poi_b_str, iden_poi_e_str, iden_arr_str, sub_str, res_str, final_ptr);
}
return false;
}
else if (iden_arrow_b_str.length() && iden_arrow_e_str.length())
{
search_str = iden_arrow_b_str;
std::string bak_search_str = iden_arrow_e_str;
bool bret = func(search_str, pattern, iden_ast_str, iden_par_b_str, iden_par_e_str, iden_arrow_b_str, iden_arrow_e_str, iden_poi_b_str, iden_poi_e_str, iden_arr_str, sub_str, res_str, final_ptr, bast);
if (bret) {
if (!final_ptr) return false;
std::any anyvalue = final_ptr->GetElement(static_cast<unsigned int>(-1));
if (!anyvalue.has_value() || anyvalue.type() != typeid(CTupleObject::_tuple_elem*)) return false;
auto elem = std::any_cast<CTupleObject::_tuple_elem*>(anyvalue);
if (!elem->ptr || !elem->ptr->IsCommReference()) return false;
elem->ref.tuple_ref = CTupleObject::_tuple_ref::tuple_ref_val;
std::shared_ptr<CObObject> ob_ptr = std::static_pointer_cast<CReferenceObject>(elem->ptr)->GetData();
if (init_func(ob_ptr, "->", final_ptr)) {
search_str = bak_search_str;
return func(search_str, pattern, iden_ast_str, iden_par_b_str, iden_par_e_str, iden_arrow_b_str, iden_arrow_e_str, iden_poi_b_str, iden_poi_e_str, iden_arr_str, sub_str, res_str, final_ptr);
}
}
return false;
}
else if (iden_ast_str.length())
{
search_str = iden_ast_str;
return func(search_str, pattern, iden_ast_str, iden_par_b_str, iden_par_e_str, iden_arrow_b_str, iden_arrow_e_str, iden_poi_b_str, iden_poi_e_str, iden_arr_str, sub_str, res_str, final_ptr, true);
}
else if (iden_par_b_str.length())
{
search_str = iden_par_b_str;
std::string bak_search_str = iden_par_e_str;
bool bret = func(search_str, pattern, iden_ast_str, iden_par_b_str, iden_par_e_str, iden_arrow_b_str, iden_arrow_e_str, iden_poi_b_str, iden_poi_e_str, iden_arr_str, sub_str, res_str, final_ptr);
if(bret) {
if (!final_ptr) return false;
if (bast) {
std::any anyvalue = final_ptr->GetElement(static_cast<unsigned int>(-1));
if (!anyvalue.has_value() || anyvalue.type() != typeid(CTupleObject::_tuple_elem*)) return false;
auto elem = std::any_cast<CTupleObject::_tuple_elem*>(anyvalue);
if (!elem->ptr || !elem->ptr->IsCommReference()) return false;
elem->ref.tuple_ref = CTupleObject::_tuple_ref::tuple_ref_val;
std::shared_ptr<CObObject> ob_ptr = std::static_pointer_cast<CReferenceObject>(elem->ptr)->GetData();
if (!init_func(ob_ptr, "*", final_ptr)) return false;
}
if (!bak_search_str.length())
return true;
if (bak_search_str[0] == '.') {
std::any anyvalue = final_ptr->GetElement(static_cast<unsigned int>(-1));
if (!anyvalue.has_value() || anyvalue.type() != typeid(CTupleObject::_tuple_elem*)) return false;
auto elem = std::any_cast<CTupleObject::_tuple_elem*>(anyvalue);
if (!elem->ptr || !elem->ptr->IsStruct()) return false;
search_str = bak_search_str.substr(1);
return func(search_str, pattern, iden_ast_str, iden_par_b_str, iden_par_e_str, iden_arrow_b_str, iden_arrow_e_str, iden_poi_b_str, iden_poi_e_str, iden_arr_str, sub_str, res_str, final_ptr);
}
else if (bak_search_str[0] == '-' && bak_search_str[1] == '>') {
std::any anyvalue = final_ptr->GetElement(static_cast<unsigned int>(-1));
if (!anyvalue.has_value() || anyvalue.type() != typeid(CTupleObject::_tuple_elem*)) return false;
auto elem = std::any_cast<CTupleObject::_tuple_elem*>(anyvalue);
if (!elem->ptr || !elem->ptr->IsCommReference()) return false;
elem->ref.tuple_ref = CTupleObject::_tuple_ref::tuple_ref_val;
std::shared_ptr<CObObject> ob_ptr = std::static_pointer_cast<CReferenceObject>(elem->ptr)->GetData();
if (init_func(ob_ptr, "->", final_ptr)) {
search_str = bak_search_str.substr(2);
return func(search_str, pattern, iden_ast_str, iden_par_b_str, iden_par_e_str, iden_arrow_b_str, iden_arrow_e_str, iden_poi_b_str, iden_poi_e_str, iden_arr_str, sub_str, res_str, final_ptr);
}
return false;
}
else if (std::string dim_str; RE2::FullMatch(bak_search_str, "^\\[([0-9]+)\\]$", &dim_str) && dim_str.length()) {
std::any anyvalue = final_ptr->GetElement(static_cast<unsigned int>(-1));
if (!anyvalue.has_value() || anyvalue.type() != typeid(CTupleObject::_tuple_elem*)) return false;
auto elem = std::any_cast<CTupleObject::_tuple_elem*>(anyvalue);
if (!elem->ptr || !elem->ptr->IsArray()) return false;
unsigned int dim = static_cast<unsigned int>(atoi(dim_str.c_str()));
elem->arr.dim = dim;
return true;
}
}
return false;
}
else if (iden_arr_str.length() && sub_str.length())
{
search_str = iden_arr_str;
std::string bak_sub_str = sub_str;
bool bret = func(search_str, pattern, iden_ast_str, iden_par_b_str, iden_par_e_str, iden_arrow_b_str, iden_arrow_e_str, iden_poi_b_str, iden_poi_e_str, iden_arr_str, sub_str, res_str, final_ptr, bast);
if (bret) {
if (!final_ptr) return false;
std::any anyvalue = final_ptr->GetElement(static_cast<unsigned int>(-1));
if (!anyvalue.has_value() || anyvalue.type() != typeid(CTupleObject::_tuple_elem*)) return false;
auto elem = std::any_cast<CTupleObject::_tuple_elem*>(anyvalue);
if (!elem->ptr || !elem->ptr->IsArray()) return false;
std::shared_ptr<CArrayObject> ob_array = std::static_pointer_cast<CArrayObject>(elem->ptr);
unsigned int dim = static_cast<unsigned int>(atoi(bak_sub_str.c_str()));
if (dim >= ob_array->GetDim()) return false;
elem->arr.dim = dim;
std::shared_ptr<CObObject> ob_ptr = std::static_pointer_cast<CArrayObject>(elem->ptr)->GetData();
return init_func(ob_ptr, "[]", final_ptr);
}
}
}
return false;
}
);
std::shared_ptr<CObObject> iden_ptr = GetSymbolReference(root_str);
if (iden_ptr) return iden_ptr;
else {
std::shared_ptr<CTupleObject> tuple_ptr = std::make_shared<CTupleObject>(root_str);
if (func(root_str, _pattern_vr_ptr, iden_ast_str, iden_par_b_str, iden_par_e_str, iden_arrow_b_str, iden_arrow_e_str, iden_poi_b_str, iden_poi_e_str, iden_arr_str, sub_str, res_str, tuple_ptr))
return tuple_ptr;
}
return nullptr;
}
bool CExpParsing::Parsing(const std::string& exp_str, std::shared_ptr<CParsing> symboltable)
{
if (symboltable == nullptr) return false;
if (!exp_str.length()) return false;
int step = 0;
if (m_expression_str.length()) return false;
std::stack<std::string> operator_stack;
bool last_tok_was_op = true;
auto process_final_op = [this](std::deque<std::shared_ptr<_Value>>& expression_ast) {
if (expression_ast.size() < 2) return;
auto& it_rbegin = expression_ast.crbegin();
if (it_rbegin == expression_ast.crend() || !(*it_rbegin)) return;
if ((*it_rbegin)->GetToken() != token_op) return;
const auto& type_def = _op_type_def.find((*it_rbegin)->GetString());
if (type_def == _op_type_def.end()) return;
if (type_def->second == binary_type) {
if (expression_ast.size() < 3) return;
auto& op = (*it_rbegin);
it_rbegin++;
if (it_rbegin == expression_ast.crend() || !(*it_rbegin)) return;
auto& rhs = (*it_rbegin);
it_rbegin++;
if (it_rbegin == expression_ast.crend() || !(*it_rbegin)) return;
auto& lhs = (*it_rbegin);
if (rhs->isIntegral() && lhs->isIdentifier()) {
std::shared_ptr<CObObject> iden_ptr = GetTokenIdentifier(lhs->GetString());
if (!iden_ptr) return;
if (iden_ptr->IsTuple()) {
auto anyvalue = std::static_pointer_cast<CTupleObject>(iden_ptr)->GetElement(static_cast<unsigned int>(-1));
if (!anyvalue.has_value() || anyvalue.type() != typeid(CTupleObject::_tuple_elem*)) return;
auto elem = std::any_cast<CTupleObject::_tuple_elem*>(anyvalue);
if (elem == nullptr || elem->ptr == nullptr) return;
if (elem->ptr->IsFlag()) {
elem->flag.tuple_flg = CTupleObject::_tuple_flg::tuple_flg_int;
}
}
}
else if (lhs->isIntegral() && rhs->isIdentifier()) {
std::shared_ptr<CObObject> iden_ptr = GetTokenIdentifier(rhs->GetString());
if (!iden_ptr) return;
if (iden_ptr->IsTuple()) {
auto anyvalue = std::static_pointer_cast<CTupleObject>(iden_ptr)->GetElement(static_cast<unsigned int>(-1));
if (!anyvalue.has_value() || anyvalue.type() != typeid(CTupleObject::_tuple_elem*)) return;
auto elem = std::any_cast<CTupleObject::_tuple_elem*>(anyvalue);
if (elem == nullptr || elem->ptr == nullptr) return;
if (elem->ptr->IsFlag()) {
elem->flag.tuple_flg = CTupleObject::_tuple_flg::tuple_flg_int;
}
}
}
else if ((rhs->isIntegral() && lhs->isIntegral()) ||
(rhs->isFloat() && lhs->isFloat()) ||
(rhs->isString() && lhs->isString())) {
std::string op_str = op->GetString();
std::shared_ptr<_Value> new_value = std::make_shared<_Value>(Calculate(op_str, *rhs, *lhs));
if (!new_value || !new_value->isValid()) return;
expression_ast.pop_back();
expression_ast.pop_back();
expression_ast.pop_back();
expression_ast.emplace_back(std::move(new_value));
}
}
return;
};
auto get_token_define = [&](const std::stringstream& iden_token, const std::unique_ptr<RE2>& pattern) ->std::shared_ptr<_Value> {
std::string iden_str, string_str1, string_str2, integral_str, float_str1, float_str2;
std::shared_ptr<_Value> value = nullptr;
if (RE2::FullMatch(iden_token.str(), *pattern, &iden_str, &string_str1, &string_str2, &integral_str, &float_str1, &float_str2))
{
if (iden_str.length())
{
std::shared_ptr<CObObject> ptr = symboltable->GetIdentifier(iden_str);
if (ptr == nullptr) return nullptr;
if (!ptr->IsTuple()) {
std::shared_ptr<CTupleObject> tuple_ptr = std::make_shared<CTupleObject>(ptr->GetName());
CTupleObject::_tuple_elem elem{};
elem.ptr = ptr;
elem.elem_name = ptr->GetName();
if (elem.ptr->IsCommReference()) {
elem.ref.tuple_ref = CTupleObject::_tuple_ref::tuple_ref_addr;
}
else if (elem.ptr->IsArray()) {
elem.arr.dim = static_cast<unsigned int>(-1);
}
else if (elem.ptr->IsStruct()) {
elem.struc.idx = static_cast<unsigned int>(-1);
}
else if (elem.ptr->IsFlag()) {
elem.flag.tuple_flg = CTupleObject::_tuple_flg::tuple_flg_str;
}
else {
elem.arr.dim = 0;
}
if (tuple_ptr->AddElement(elem)) {
AddTokenIdentifier(IDENPAIR(iden_str, tuple_ptr));
}
else {
AddTokenIdentifier(IDENPAIR(iden_str, tuple_ptr));
}
}
else {
AddTokenIdentifier(IDENPAIR(iden_str, ptr));
}
value = std::make_shared<_Value>(iden_str, token_iden);
}
else if (string_str1.length())
{
value = std::make_shared<_Value>(string_str1);
}
else if (string_str2.length())
{
value = std::make_shared<_Value>(string_str2);
}
else if (integral_str.length())
{
char* nodig = 0;
unsigned long long integral = strtoull(integral_str.c_str(), &nodig, 10);
value = std::make_shared<_Value>(integral);
}
else if (float_str1.length())
{
char* nodig = 0;
double float_pointer = strtod(float_str1.c_str(), &nodig);
value = std::make_shared<_Value>(float_pointer);
}
else if (float_str2.length())
{
char* nodig = 0;
double float_pointer = strtod(float_str1.c_str(), &nodig);
value = std::make_shared<_Value>(float_pointer);
}
}
return value;
};
while (exp_str[step] && isspace(exp_str[step])) ++step;
std::stringstream ss;
std::stringstream iden_ss;
do {
bool bstr = false;
while (exp_str[step])
{
if (!bstr) {
if (isspace(exp_str[step])) break;
}
if (exp_str[step] == '\'' || exp_str[step] == '"') {
bstr = !bstr;
if (!bstr) {
iden_ss << exp_str[step++];
break;
}
}
ss << exp_str[step++];
if (!bstr) {
auto it_find = _op_precedence.find(ss.str());
if (it_find != _op_precedence.end()) break;
}
iden_ss << ss.str();
ss.str(""); ss.clear();
}
if (iden_ss.str().length())
{
std::shared_ptr<_Value> value = get_token_define(iden_ss, _pattern_exp_ptr);
if (!value) {
m_expression_ast.clear();
return false;
}
m_expression_ast.emplace_back(std::move(value));
last_tok_was_op = false;
iden_ss.str(""); iden_ss.clear();
}
if(ss.str().length())
{
switch (ss.str()[0]) {
case '(':
operator_stack.push("(");
break;
case ')':
while (operator_stack.top().compare("(")) {
m_expression_ast.push_back(std::make_unique<_Value>(operator_stack.top(), token_op));
process_final_op(m_expression_ast);
operator_stack.pop();
}
operator_stack.pop();
break;
default: {
std::stringstream op_ss;
op_ss << ss.str();
if (exp_str[step] && !isspace(exp_str[step]))
{
op_ss << exp_str[step];
auto it_find = _op_precedence.find(op_ss.str());
if (it_find != _op_precedence.end()) {
ss.str(""); ss.clear();
ss << op_ss.str();
step++;
}
}
while ([&]() ->bool {
if (operator_stack.empty()) return false;
auto it1 = _op_precedence.find(ss.str());
auto it2 = _op_precedence.find(operator_stack.top());
if (it1 == _op_precedence.end()) return false;
if (it2 == _op_precedence.end()) return false;
if (it1->second > it2->second) return false;
return true;
}())
{
m_expression_ast.emplace_back(std::make_unique<_Value>(operator_stack.top(), token_op));
process_final_op(m_expression_ast);
operator_stack.pop();
}
operator_stack.emplace(ss.str());
last_tok_was_op = true;
}
}
ss.str(""); ss.clear();
}
while (exp_str[step] && isspace(exp_str[step])) ++step;
} while (exp_str[step]);
while (!operator_stack.empty()) {
m_expression_ast.emplace_back(std::make_unique<_Value>(operator_stack.top(), token_op));
process_final_op(m_expression_ast);
operator_stack.pop();
}
m_expression_str = exp_str;
return true;
}
bool CExpParsing::SetIdentifierValue(_ValueMap& value_map, const ValuePair& iden_pair) {
ASSERT(iden_pair.first.length());
ASSERT(iden_pair.second.has_value());
if (!iden_pair.first.length()) return false;
if (!iden_pair.second.has_value()) return false;
auto value = value_map.find(iden_pair.first);
if (value != value_map.end())
return true;
std::any anyvalue = GetAnyValue(iden_pair.second);
if (!anyvalue.has_value()) return false;
if (anyvalue.type() == typeid(ULONGLONG))
value_map[iden_pair.first] = _Value(std::any_cast<ULONGLONG>(anyvalue));
else if (anyvalue.type() == typeid(DOUBLE))
value_map[iden_pair.first] = _Value(std::any_cast<DOUBLE>(anyvalue));
else if (anyvalue.type() == typeid(std::string))
value_map[iden_pair.first] = _Value(std::any_cast<std::string>(anyvalue));
else
{
error_log("SetIdentifierValue failed because token incorrect!");
return false;
}
return true;
}
CExpParsing::_Value CExpParsing::Calculate(std::string& op, _Value& rhs, _Value& lhs)
{
if (!op.compare("!")) {
return !rhs;
}
else if (!op.compare("+")) {
return (lhs + rhs);
}
else if (!op.compare("*")) {
return (lhs * rhs);
}
else if (!op.compare("-")) {
return (lhs - rhs);
}
else if (!op.compare("/")) {
return (lhs / rhs);
}
else if (!op.compare("<<")) {
return (lhs << rhs);
}
else if (!op.compare("^")) {
return (lhs ^ rhs);
}
else if (!op.compare(">>")) {
return (lhs >> rhs);
}
else if (!op.compare(">")) {
return (lhs > rhs);
}
else if (!op.compare(">=")) {
return (lhs >= rhs);
}
else if (!op.compare("<")) {
return (lhs < rhs);
}
else if (!op.compare("<=")) {
return (lhs <= rhs);
}
else if (!op.compare("&")) {
return (lhs & rhs);
}
else if (!op.compare("|")) {
return (lhs | rhs);
}
else if (!op.compare("&&")) {
return (lhs && rhs);
}
else if (!op.compare("||")) {
return (lhs || rhs);
}
else if (!op.compare("==")) {
return (lhs == rhs);
}
else if (!op.compare("!=")) {
return (lhs != rhs);
}
else if (!op.compare("="))
{
lhs = rhs;
return lhs;
}
else if (!op.compare("=~"))
{
RE2 pattern(rhs.GetString(), RE2::Quiet);
return (ULONGLONG)RE2::FullMatch(lhs.GetString(), pattern);
}
else if (!op.compare("!~"))
{
RE2 pattern(rhs.GetString(), RE2::Quiet);
return (ULONGLONG)(!RE2::FullMatch(lhs.GetString(), pattern));
}
return {};
}
std::variant<ULONGLONG, std::unique_ptr<CExpParsing::ValuePair>> CExpParsing::EvalExpression(const _ValueMap& value_map)
{
if (!value_map.size()) return 0;
std::unique_ptr<std::queue<std::unique_ptr<_Value>>> expression_ast = std::move(GetQueueExpressionAst());
ASSERT(expression_ast != nullptr && (*expression_ast).size());
if (!expression_ast || !expression_ast->size())
return 0;
std::string last_identifier;
std::stack<_Value> evaluation;
while (!expression_ast->empty()) {
std::unique_ptr<_Value> token = std::move(expression_ast->front());
expression_ast->pop();
if (token->isOp())
{
std::string str = token->GetString();
if (evaluation.size() < 2) {
ASSERT(!evaluation.size());
if (evaluation.size() == 0)
return 0;
_Value value = evaluation.top(); evaluation.pop();
_Value new_value = Calculate(str, value, _Value());
if(new_value.isValid())
evaluation.push(!value);
else return 0;
}
_Value rhs = evaluation.top(); evaluation.pop();
_Value lhs = evaluation.top(); evaluation.pop();
if (!str.compare("="))
{
Calculate(str, rhs, lhs);
if (!lhs.isValid())
return 0;
if (last_identifier.length() && evaluation.size() == 0 && expression_ast->empty())
{
// Back propagation
auto result_value = std::make_unique<ValuePair>(last_identifier, lhs.GetAnyValue());
return result_value;
}
}
else {
_Value new_value = Calculate(str, rhs, lhs);
if (new_value.isValid())
evaluation.push(new_value);
else {
error_log("Unknown operator: {} {} {}.", lhs.GetString(), str, rhs.GetString());
return 0;
}
}
}
else if (token->isConstant())
{
evaluation.push(*token);
}
else if (token->isIdentifier())
{
last_identifier = token->GetString();
auto r_token = value_map.find(token->GetString());
if (r_token == value_map.end())
{
error_log("Unknown identifier: {}", token->GetString());
return 0;
}
evaluation.push(r_token->second);
}
else
{
error_log("Invalid token '{}'.", token->GetString());
return 0;
}
}
if (evaluation.top().GetIntegral() == 0)
return 0;
return evaluation.top().GetIntegral();
}
bool CFunction::AddChecks(const _CheckPackage& package, bool bpre)
{
std::shared_ptr<CCheck> pcheck = std::make_shared<CCheck>(shared_from_this());
if (!pcheck) return false;
auto bin_func([](auto it_b, auto it_e) {
return std::string(it_b, it_e);
});
for (const auto& define : package.define_pairs)
{
std::shared_ptr<CObObject> define_ptr = CLexerTy::GetInstance().GetIdentifier(define.second);
if (define_ptr)
pcheck->AddLocalSymbol(PrototypeArgument(define.first, define_ptr));
}
for (const auto& log : package.logs)
{
std::shared_ptr<CObObject> log_ptr = pcheck->GetIdentifier(log);
if (log_ptr)
pcheck->AddLog(IDENPAIR(log, log_ptr));
}
for (const auto& handle : package.handle_pair)
{
pcheck->AddHandle(handle);
}
for (const auto& check : package.check_pair)
{
if (check.first.length())
{
bool bsuccess = false;
std::list<std::string> split_list;
split_algorithm(std::begin(check.first), std::end(check.first), back_inserter(split_list), '|', bin_func);
for (const auto& it : split_list) {
std::shared_ptr<CObObject> ob_ptr = pcheck->GetIdentifier(it);
if (ob_ptr) {
bsuccess = pcheck->AddIdentifier(IDENPAIR(it, ob_ptr));
if (!bsuccess) break;
}
else {
bsuccess = false;
break;
}
}
if (bsuccess)
pcheck->AddCheck(check.second);
}
}
for (const auto& modify : package.modify_pair)
{
if (modify.first.length())
{
bool bsuccess = false;
std::list<std::string> split_list;
split_algorithm(std::begin(modify.first), std::end(modify.first), back_inserter(split_list), '|', bin_func);
for (const auto& it : split_list) {
std::shared_ptr<CObObject> ob_ptr = pcheck->GetIdentifier(it);
if (ob_ptr) {
bsuccess = pcheck->AddIdentifier(IDENPAIR(it, ob_ptr));
if (!bsuccess) break;
}
else {
bsuccess = false;
break;
}
}
if (bsuccess)
pcheck->AddModify(modify.second);
}
}
if (bpre)
m_pprechecks.push_back(std::move(pcheck));
else
m_ppostchecks.push_back(std::move(pcheck));
return true;
}
processing_status CFunction::CheckReturn(PVOID pnode) const
{
ASSERT(pnode != nullptr);
CHookImplementObject::detour_node* de_node = reinterpret_cast<CHookImplementObject::detour_node*>(pnode);
if (!de_node || !de_node->log_entry) return processing_skip;
ASSERT(de_node->return_va != nullptr);
std::shared_ptr<CObObject> ob_ptr = GetIdentifier(SI_RETURN);
ASSERT(ob_ptr != nullptr);
if (!ob_ptr) return processing_continue;
if (ob_ptr->Success(reinterpret_cast<char*>(de_node->return_va)))
{
return processing_continue;
}
return processing_skip;
}
processing_status CFunction::ProcessingEnsure(PVOID pnode) const
{
ASSERT(pnode != nullptr);
if (!m_pensure) return processing_continue;
CHookImplementObject::detour_node* de_node = reinterpret_cast<CHookImplementObject::detour_node*>(pnode);
if (!de_node || !de_node->log_entry) return processing_skip;
BEGIN_LOG("ensure");
for (const auto& ensure : *m_pensure)
{
ASSERT(ensure.second != nullptr);
if (!ensure.second)
continue;
if (_stricmp(CLexerVr::GetWordRoot(ensure.first).c_str(), SI_RETURN) == 0)
continue;
int arg_offset = GetArgumentOffset(CLexerVr::GetWordRoot(ensure.first));
if (arg_offset == invalid_arg_offset)
{
arg_offset = GetArgumentOffset(ensure.second->GetName());
if (arg_offset == invalid_arg_offset)
continue;
}
ASSERT(de_node->pparams != nullptr);
if (!ensure.second->IsValidValue(de_node->pparams + arg_offset))
{
error_log("Function({}): argument({}) invalid!", GetName(), ensure.first);
return processing_skip;
}
LOGGING(ensure.first, "valid");
}
END_LOG(de_node->log_entry);
return processing_continue;
}
processing_status CFunction::ProcessingLog(PVOID pnode, bool pre) const
{
CHookImplementObject::detour_node* de_node = reinterpret_cast<CHookImplementObject::detour_node*>(pnode);
if (!de_node || !de_node->log_entry) return processing_skip;
std::string log_name;
if (pre)
{
if (!m_pprelog)
return processing_continue;
log_name = "pre_log";
}
else
{
if (!m_ppostlog)
return processing_continue;
log_name = "post_log";
}
BEGIN_LOG(log_name);
ASSERT(GetLogging(pre) != nullptr);
for (const auto& plog : *GetLogging(pre))
{
ASSERT(plog.second != nullptr);
if (!plog.second)
continue;
int arg_offset = 0;
char* param_adress = nullptr;
if (_stricmp(CLexerVr::GetWordRoot(plog.first).c_str(), SI_RETURN) == 0)
{
param_adress = reinterpret_cast<char*>(de_node->return_va);
}
else
{
arg_offset = GetArgumentOffset(CLexerVr::GetWordRoot(plog.first));
if (arg_offset == invalid_arg_offset)
{
arg_offset = GetArgumentOffset(plog.second->GetName());
if (arg_offset == invalid_arg_offset)
continue;
}
param_adress = de_node->pparams;
}
ASSERT(param_adress != nullptr);
std::any anyvalue = plog.second->GetValue(param_adress + arg_offset);
if (!anyvalue.has_value() && plog.second->IsCommReference()) {
PVOID ret_ptr = nullptr;
anyvalue = ret_ptr;
}
std::stringstream ss;
if (ss = OutputAnyValue(anyvalue); !ss.str().length())
{
error_log("Function({}): get argument({}) value failed!", GetName(), plog.first);
continue;
}
LOGGING(plog.first, ss.str());
}
END_LOG(de_node->log_entry);
return processing_continue;
}
bool CFunction::checking(PVOID pnode, const std::shared_ptr<CCheck>& pcheck, const std::shared_ptr<CLogHandle>& log_handle) const
{
BEGIN_LOG("checking");
CHookImplementObject::detour_node* de_node = reinterpret_cast<CHookImplementObject::detour_node*>(pnode);
if (!de_node || !de_node->log_entry) return false;
bool bcheck = true;
for (const auto& check : pcheck->GetChecks())
{
ASSERT(pcheck != nullptr);