-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstringmagic_parser.cpp
More file actions
1650 lines (1292 loc) · 41.9 KB
/
stringmagic_parser.cpp
File metadata and controls
1650 lines (1292 loc) · 41.9 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
69
FBC0
2
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
/*******************************************************
* @file stringmagic_parser.cpp *
* @author lrberge *
* @brief main parser of the stringmagic framework, *
* see description below *
* + secondary parsers *
* @version 1.0 *
* @date 2023-05-09 *
*******************************************************/
#include "stringmagic.h"
#include <Rcpp.h>
#include <string>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace Rcpp;
// DEFINITIONS
// - box = operator used to interpolate the variables
// ex: {var}, here {} is the box. .[var], here .[] is the box
// - operator: code name of the operation to be applied to the interpolated variable
// ex: {S, C!1, 2, 3}, here S and C are the operators
// - string_ops/sop: sting operations
// general name of the process of parsing the full string into box, operators and the rest
// - separator: either ? or !
// whether to evaluate from the environment or use verbatim
//
// delimiter class -------------------------------------------------------------
//
class delim{
const char *open;
const char *close;
int size_open;
int size_close;
delim() = delete;
public:
// SEXP: vector of length 2
delim(SEXP);
bool is_open(const char *, int &, int, bool);
bool is_close(const char *, int &, int, bool);
int get_size_open(){return size_open;};
int get_size_close(){return size_close;};
};
delim::delim(SEXP Rstr){
if(Rf_length(Rstr) != 2){
stop("Internal error: Delimiter must be of length 2");
}
if(TYPEOF(Rstr) != STRSXP){
stop("Internal error: Delimiter must be of type character");
}
open = Rf_translateCharUTF8(STRING_ELT(Rstr, 0));
close = Rf_translateCharUTF8(STRING_ELT(Rstr, 1));
size_open = std::strlen(open);
size_close = std::strlen(close);
}
bool check_symbol(const char *symbol, int n_symbol, const char *str, int &i, int n, bool skip){
bool ok_escape = false;
if(skip && str[i] == '\\'){
// what do we do here? we increment i if we have an escaped symbol
// since i does not point at the symbol, it always returns FALSE
// only thing that matters is the i increment
if(i + n_symbol < n){
for(int k=0 ; k<n_symbol ; ++k){
if(str[i + 1 + k] != symbol[k]){
return false;
}
}
ok_escape = true;
}
if(ok_escape){
int j = i - 1;
while(j > 0 && str[j--] == '\\'){
ok_escape = !ok_escape;
}
if(ok_escape){
++i;
}
}
return false;
}
// we don't skip but we still check for escaping
for(int k=0 ; k<n_symbol ; ++k){
if(str[i + k] != symbol[k]){
return false;
}
}
// we check escaping
if(i >= 1 && str[i - 1] == '\\'){
ok_escape = true;
int j = i - 2;
while(j >= 0 && str[j--] == '\\'){
ok_escape = !ok_escape;
}
return !ok_escape;
}
return true;
}
bool delim::is_open(const char *str, int &i, int n, bool skip){
return check_symbol(open, size_open, str, i, n, skip);
}
bool delim::is_close(const char *str, int &i, int n, bool skip){
return check_symbol(close, size_close, str, i, n, skip);
}
//
// END OF: delimiter class -----------------------------------------------------
//
inline bool is_escaped(const char *str, int i){
bool ok_escape = i >= 1 && str[i - 1] == '\\';
if(ok_escape){
int j = i - 2;
while(j >= 0 && str[j--] == '\\'){
ok_escape = !ok_escape;
}
}
return ok_escape;
}
inline bool is_digit(const char * str, int i){
return str[i] >= '0' && str[i] <= '9';
}
inline bool is_special_char(const char * str, int i){
return str[i] == '\'' || str[i] == '"' || str[i] == '`' || str[i] == ':' ||
str[i] == ';' || str[i] == '/';
}
inline bool is_quote(const char * str, int i){
return str[i] == '\'' || str[i] == '"' || str[i] == '`';
}
inline bool is_basic_quote(const char * str, int i){
return str[i] == '\'' || str[i] == '"';
}
inline bool is_separator(const char * str, int i){
return str[i] == '!' || str[i] == '?';
}
// checks inclusion of a character value in a string array
inline bool is_in_string(const char * str, int i, std::string std_str){
// only supports endings of length <= 3
for(size_t k=0 ; k<std_str.size() ; ++k){
if(str[i] == std_str[k]){
return true;
}
}
return false;
}
// adds quotes only when operator_tmp is not quoted
inline void enquote(std::string &operator_tmp){
if(operator_tmp.empty()){
operator_tmp = "''";
return;
}
int n = operator_tmp.length();
if(n > 1){
if((operator_tmp[0] == '\'' && operator_tmp[n - 1] == '\'') ||
(operator_tmp[0] == '"' && operator_tmp[n - 1] == '"')){
return;
}
}
if(operator_tmp[0] == '\''){
operator_tmp = '"' + operator_tmp + '"';
} else {
operator_tmp = '\'' + operator_tmp + '\'';
}
}
void extract_quote(const char * str, int &i, int n,
std::string &operator_tmp, const bool inQuote_only = false){
// extracts a quoted string and adds it to operator_tmp, also updates i
// escaped quotes are ignored
//
// assume str: "Mary: 'hello \\'John Snow\\'! hello dear!' And then he went"
// values of i: i (in, at the quote) i (out, after the quote)
//
// operator_tmp is augmented with: "'hello \\'John Snow\\'! hello dear!'"
// -> note that the quotes are INCLUDED
//
// clean: whether we get only the value in the quote
// we also clean it
char quote = str[i++];
if(!inQuote_only) operator_tmp += quote;
while(i < n){
if(str[i] == quote){
if(str[i - 1] == '\\'){
// we escape the quote and remove the '\\' (if only inquote)
if(inQuote_only) operator_tmp.pop_back();
} else {
break;
}
}
operator_tmp += str[i++];
}
if(i < n){
if(!inQuote_only) operator_tmp += quote;
++i;
}
}
void extract_r_expression(delim &delims, bool &is_pblm, const char * str, int &i, int n,
std::string &operator_tmp, std::string ending_str,
bool check_closing, bool include_closing);
void extract_single_simple_operation(delim &delims, bool &is_pblm, const char * str, int &i, int n,
std::string &operator_tmp, std::string ending_str){
// regular, simple operation
// format: /'arg'op/ or /arg op/ or /'arg' op/
// step 1: argument extraction (if provided)
bool is_arg = false;
if(is_quote(str, i)){
is_arg = true;
extract_quote(str, i, n, operator_tmp);
} else {
// we check for possible argument space separated
while(i < n && str[i] != ',' && str[i] != ' ' && !is_in_string(str, i, ending_str) &&
!delims.is_close(str, i, n, false) && !delims.is_open(str, i, n, false)){
operator_tmp += str[i++];
}
if(i < n && str[i] == ' '){
operator_tmp += str[i++];
is_arg = true;
}
}
if(delims.is_open(str, i, n, false)){
is_pblm = true;
return;
}
// step 2: operator extraction (if not done)
if(is_arg){
while(i < n && str[i] != ',' && !is_in_string(str, i, ending_str) &&
!delims.is_close(str, i, n, false)){
operator_tmp += str[i++];
}
if(delims.is_open(str, i, n, false)){
is_pblm = true;
return;
}
}
// stripping ending WS
while(operator_tmp.length() > 0 && operator_tmp[operator_tmp.length() - 1] == ' '){
operator_tmp.pop_back();
}
if(i == n){
is_pblm = true;
}
}
inline bool is_paren_operator(const char * str, int i, int n){
// the special simple operators
// they're the only ones to allow parentheses
if(n <= i + 3) return false;
return (str[i] == '~' && str[i + 1] == '(') ||
(str[i] == 'i' && str[i + 1] == 'f' && str[i + 2] == '(') ||
(str[i] == 'v' && str[i + 1] == 'i' && str[i + 2] == 'f' && str[i + 3] == '(');
}
void extract_verbatim(delim &delims, bool &is_pblm, const char * str, int &i, int n,
std::string &operator_tmp, std::string ending_str,
bool check_closing, bool include_closing,
bool skip_box_open, bool skip_second_space = false);
void extract_simple_ops_verbatim(delim &delims, bool &is_pblm, const char * str, int &i, int n,
std::string &operator_tmp, std::string ending_str);
void extract_paren_operator(delim &delims, bool &is_pblm, const char * str, int &i, int n,
std::string &operator_tmp){
// ~(simple_ops)
// if(cond ; ops ; ops)
// vif(cond ; verbatim ; verbatim)
// the index i ends after the paren
char op = str[i];
while(str[i] != '(') operator_tmp += str[i++];
operator_tmp += str[i++];
if(op == '~'){
extract_simple_ops_verbatim(delims, is_pblm, str, i, n, operator_tmp, ")");
if(is_pblm) return;
operator_tmp += str[i++];
} else {
// if or vif
extract_r_expression(delims, is_pblm, str, i, n, operator_tmp, ";", false, false);
if(is_pblm) return;
bool is_decorative_space = str[i - 1] == ' ' && i + 1 < n && str[i + 1] == ' ';
if(is_decorative_space){
// we skip the decorative space
++i;
}
++i;
operator_tmp += "_;;;_";
// if => simple operators
// vif => verbatim
if(op == 'i'){
extract_simple_ops_verbatim(delims, is_pblm, str, i, n, operator_tmp, ";)");
} else {
extract_verbatim(delims, is_pblm, str, i, n, operator_tmp, ";)", false, false, false);
}
if(is_pblm) return;
++i;
if(str[i - 1] == ';'){
if(is_decorative_space && str[i] == ' ' && str[i - 2] == ' '){
if(op == 'v'){
// there is no trailing WS for regular operations
operator_tmp.pop_back();
}
++i;
}
operator_tmp += "_;;;_";
// last round
if(op == 'i'){
extract_simple_ops_verbatim(delims, is_pblm, str, i, n, operator_tmp, ")");
} else {
extract_verbatim(delims, is_pblm, str, i, n, operator_tmp, ")", false, false, false);
}
if(is_pblm) return;
operator_tmp += str[i++];
} else {
operator_tmp += ")";
}
}
}
void extract_simple_ops_verbatim(delim &delims, bool &is_pblm, const char * str, int &i, int n,
std::string &operator_tmp, std::string ending_str){
// extracts simple operations
// the basic operations can be of the form:
// 'arg'op, arg op
// they are comma separated. After the comma, tabs and newlines are allowed
// There are special operations which can include parentheses:
// - ~(simple_ops)
// - if(condition ; simple_ops ; simple_ops)
//
// THE INDEX i OUT IS AT THE ENDING!
while(i < n){
// each loop corresponds to a single command
// we go to the first non blank
while(is_blank(str, i)){
operator_tmp += str[i++];
}
if(i == n) break;
if(is_in_string(str, i, ending_str)){
// we're good
break;
} else if(delims.is_close(str, i, n, false)){
// we're a the bound of the box => can be a single variable and not an operation
is_pblm = true;
return;
} else if(is_paren_operator(str, i, n)){
// NOTE: we have a guarantee n > i+3
extract_paren_operator(delims, is_pblm, str, i, n, operator_tmp);
if(is_pblm) return;
if(i < n && str[i] == ','){
operator_tmp += str[i++];
}
} else {
// regular, simple operation
// format: /'arg'op/ or /arg op/
extract_single_simple_operation(delims, is_pblm, str, i, n, operator_tmp, ending_str);
if(str[i] == ','){
operator_tmp += str[i++];
} else {
// means ENDING or box close
break;
}
}
}
if(i == n){
is_pblm = true;
return;
}
}
void extract_r_expression(delim &delims, bool &is_pblm, const char * str, int &i, int n,
std::string &operator_tmp, std::string ending_str,
bool check_closing, bool include_closing){
// extracts a value after a '?' It must always be an R expression
// the string must contain a closing box to be valid (hence cannot end at n)
// the index i is located just after the variable
// this is a light and mini R parser (note that it does not check the syntax)
//
// "bonjour {enum ? persons}"
// i (in) i (out)
// the index is at the bound
//
bool check_ending = ending_str.size() > 0;
// square brackets and curly brackets
int n_cb_open = 0;
int n_sb_open = 0;
int n_par_open = 0;
while(i < n){
while(is_blank(str, i)){
// we go to the first non blank
operator_tmp += str[i++];
}
if(i == 0) break;
if(str[i] == '#'){
// we take the full line w/t checking anything
// limitation of current algo: "# tricky line: \\\n"
// but who would write that in a comment....
while(str[i] != '\n' && str[i - 1] != '\\'){
operator_tmp += str[i++];
}
if(i < n) operator_tmp += str[i++];
} else if(is_quote(str, i)){
// we get the quote
extract_quote(str, i, n, operator_tmp);
if(i == n) break;
} else if(n_cb_open == 0 && n_sb_open == 0 && n_par_open == 0 &&
((check_ending && is_in_string(str, i, ending_str)) ||
(check_closing && delims.is_close(str, i, n, false)))){
// if here: we're done
if(check_closing && delims.is_close(str, i, n, false)){
// we're good! => we need to point at the extremity of the delimiter
for(int k=1 ; k<delims.get_size_close() ; ++k){
if(include_closing){
operator_tmp += str[i++];
} else {
++i;
}
}
}
break;
} else {
if(str[i] == '{'){
++n_cb_open;
} else if(str[i] == '}'){
--n_cb_open;
} else if(str[i] == '['){
++n_sb_open;
} else if(str[i] == ']'){
--n_sb_open;
} else if(str[i] == '('){
++n_par_open;
} else if(str[i] == ')'){
--n_par_open;
}
operator_tmp += str[i++];
}
}
if(i == n){
is_pblm = true;
}
}
void extract_box_verbatim(delim &delims, bool &is_pblm, const char * str, int &i, int n,
std::string &operator_tmp){
// we extract what is inside a box verbatim
// the resulting index i is AFTER the closing of the box
// boxes can be escaped as per usual
bool check_separator = false;
if(str[i] == '/'){
// we extract the verbatim until the closing box
extract_verbatim(delims, is_pblm, str, i, n, operator_tmp, "", true, true, false);
} else if(str[i] == '$' || str[i] == '#'){
// pluralization
operator_tmp += str[i++];
while(i < n && !is_separator(str, i) && !delims.is_close(str, i, n, false)){
if(str[i] != '('){
operator_tmp += str[i++];
} else {
// we handle the (zero;singular;plural)
// => only operator to allow parens
operator_tmp += str[i++];
// we extract the first item
extract_verbatim(delims, is_pblm, str, i, n, operator_tmp, ";", false, false, false);
// NOTA: if i == n this means there is a pblm, so we're out
if(is_pblm) return;
operator_tmp += str[i++];
// we extract the second or last item
extract_verbatim(delims, is_pblm, str, i, n, operator_tmp, ";)", false, false, false);
if(is_pblm) return;
if(i < n && str[i] == ';'){
// we go for another round
operator_tmp += str[i++];
extract_verbatim(delims, is_pblm, str, i, n, operator_tmp, ")", false, false, false);
if(is_pblm) return;
}
operator_tmp += str[i++];
}
}
} else if(str[i] == '&'){
// if-else
// NOTA: the code differs from above via the different ending that is looked after
operator_tmp += str[i++];
extract_r_expression(delims, is_pblm, str, i, n, operator_tmp, ";", false, false);
if(is_pblm) return;
operator_tmp += str[i++];
// we extract the second or last item
extract_verbatim(delims, is_pblm, str, i, n, operator_tmp, ";", true, true, false);
if(is_pblm) return;
if(i < n && str[i] == ';'){
// we go for another round
operator_tmp += str[i++];
extract_verbatim(delims, is_pblm, str, i, n, operator_tmp, "", true, true, false);
if(is_pblm) return;
}
} else {
// regular operators
// "hi {enum ? x}" => enum
// operators containing parentheses:
// - ~(simple_ops)
// - if(condition ; simple_ops ; simple_ops)
// - vif(condition ; verbatim ; verbatim)
// the fun either exits at ?/!, either at a closing box
extract_simple_ops_verbatim(delims, is_pblm, str, i, n, operator_tmp, "?!");
if(is_pblm) return;
if(is_separator(str, i)){
check_separator = true;
}
}
// the index i is at the bound of the box
// otherwise => parsing problem
if(i == n){
is_pblm = true;
return;
}
if(check_separator && is_separator(str, i)){
if(str[i] == '?'){
extract_r_expression(delims, is_pblm, str, i, n, operator_tmp, "", true, true);
} else if(str[i] == '!'){
extract_verbatim(delims, is_pblm, str, i, n, operator_tmp, "", true, true, false, true);
}
if(is_pblm) return;
}
// we add the last character of the closing box element
operator_tmp += str[i++];
}
void extract_verbatim(delim &delims, bool &is_pblm, const char * str, int &i, int n,
std::string &operator_tmp, std::string ending_str,
bool check_closing, bool include_closing,
bool skip_box_open, bool skip_second_space){
//
// the final index i stops at the ending value (and not after it)
// the default for skip_second_space is given in its first declaration
if(skip_second_space){
// case verbatim expression and space before
// ex: {S, " et "c ! bonjour}
// ^ we start here
operator_tmp += str[i++];
// sikpping the space
if(str[i] == ' '){
++i;
}
}
while(i < n){
if(is_in_string(str, i, ending_str)){
if(is_escaped(str, i)){
// means the value has been escaped
operator_tmp.pop_back();
operator_tmp += str[i++];
} else {
// we're done
break;
}
} else if(check_closing && delims.is_close(str, i, n, true)){
// the skipping of the escape is included
// (I've checked it works! you move i by one, then end up in the last 'else', and all is well)
// we need to point at the extremity of the delimiter
for(int k=1 ; k<delims.get_size_close() ; ++k){
if(include_closing){
operator_tmp += str[i++];
} else {
++i;
}
}
break;
} else if(delims.is_open(str, i, n, skip_box_open)){
operator_tmp += str[i++];
for(int k=1 ; k<delims.get_size_open() ; ++k){
operator_tmp += str[i++];
}
extract_box_verbatim(delims, is_pblm, str, i, n, operator_tmp);
} else {
operator_tmp += str[i++];
}
}
if(i == n){
is_pblm = true;
}
}
void parse_box(delim &delims, bool &is_pblm, const char * str, int &i, int n,
std::vector<std::string> &operator_vec, std::string &expr_value,
bool &any_plural, std::string &error_msg){
// modifies the operator and gives the updated i
// the i should start where to evaluate post separator (if present)
// Here is the exhaustive list of operations allowed:
// - "/1, 2, 3" => the split string operator. Cannot contain anything else
// - ".['arg1'op1, 'arg2'op2 ? x]": argument + operator
// - ".[op1, op2, ? x]": operator without argument
// - if(cond;ops_true;ops_false), vif(cond;verbatim_true;verbatim_false): if special operator. Can be chained.
// - ~(simple_ops): conditional operations
// - .[& i %% 3 == 0;':'] ifelse operator
// .[& condition ; verbatim1 : verbatim2]
// - .[$op1, op2, (singular:plural)]: pluralisation
// operators using '(':
// - if, vif, ~
// we first get the operator, if there is an operator
std::string operator_tmp = "";
// whether to extract the ending ? or !
bool extract_separator = false;
if(str[i] == '/'){
//
// split string
//
operator_tmp = "/";
operator_vec.push_back(operator_tmp);
++i;
// this is a simple verbatim extraction without any reparsing
extract_verbatim(delims, is_pblm, str, i, n, expr_value, "", true, false, true);
if(is_pblm || i == n){
error_msg = "slash operator not ending correctly";
is_pblm = true;
return;
}
} else if(str[i] == '&'){
//
// if else
//
operator_tmp = "&";
++i;
if(i < n && str[i] == '&'){
operator_tmp += str[i++];
}
operator_vec.push_back(operator_tmp);
operator_tmp = "";
// step 1: extraction of the condition
extract_r_expression(delims, is_pblm, str, i, n, operator_tmp, ";", false, false);
if(is_pblm){
error_msg = "if-else: error extracting condition";
return;
}
++i;
operator_vec.push_back(operator_tmp);
operator_tmp = "";
// step 2: extraction of the first verbatim
if(i < n && str[i] == ' ' && str[i - 2] == ' '){
// we strip one WS for readability
++i;
}
extract_verbatim(delims, is_pblm, str, i, n, operator_tmp, ";", true, false, false);
if(is_pblm){
error_msg = "if-else: error extracting the first part";
return;
}
bool two_verbatims = str[i] == ';';
if(two_verbatims){
if(str[i - 1] == ' ' && (i + 1 < n && str[i + 1] == ' ')){
// we strip 1 WS on both sides for readability
operator_tmp.pop_back();
i += 2;
} else {
++i;
}
}
operator_vec.push_back(operator_tmp);
operator_tmp = "";
if(two_verbatims){
extract_verbatim(delims, is_pblm, str, i, n, operator_tmp, "", true, false, false);
if(is_pblm){
error_msg = "if-else: error extracting the second part";
return;
}
operator_vec.push_back(operator_tmp);
}
} else if(str[i] == '$' || str[i] == '#'){
//
// pluralisation
//
extract_separator = true;
operator_tmp = str[i];
++i;
operator_vec.push_back(operator_tmp);
operator_tmp = "";
while(i < n && is_blank(str[i])) ++i;
bool in_operator = false;
while(i < n && !is_separator(str, i)){
if(delims.is_close(str, i, n, false)){
// we point at the end of the closing box
for(int k=1 ; k<delims.get_size_close() ; ++k){
++i;
}
break;
}
// pluralization: op1, op2, (sing;plural)
if(str[i] == ' '){
// let's skip WS
++i;
} else if(is_quote(str, i)){
// we extract the quoted argument
// only if the quote is the first element, ie not yet inside an operator
if(!in_operator){
extract_quote(str, i, n, operator_tmp);
} else {
operator_tmp += str[i++];
}
} else if(str[i] == '('){
if(in_operator){
// there was a parsing error. This MUST be without anything before
error_msg = "pluralization: operators cannot contain parentheses";
is_pblm = true;
return;
}
in_operator = false;
// we parse it into: 'stuff'zero 'stuff'singular, 'stuff'plural
++i;
std::string op_first = "", op_second = "", op_third = "";
bool is_third = false, is_special = false;
//
// step 1: extraction of the first verbatim element
//
extract_verbatim(delims, is_pblm, str, i, n, operator_tmp, ";", false, false, false);
if(is_pblm){
error_msg = "pluralization: (v1;v2) error extracting the first verbatim";
return;
}
op_first = operator_tmp;
operator_tmp = "";
++i;
if(i + 1 >= n){
error_msg = "pluralization: (v1;v2) error extracting the first verbatim";
is_pblm = true;
return;
}
//
// step 2: extraction of the second verbatim element
//
// stripping WS
if(str[i] == ' ' && str[i - 2] == ' '){
op_first.pop_back();
++i;
} else if(str[i] == ';'){
// special case (zero;;stuff)
is_special = true;
is_third = true;
++i;
}
extract_verbatim(delims, is_pblm, str, i, n, operator_tmp, ";)", false, false, false);
if(is_pblm){
error_msg = "pluralization: (v1;v2) error extracting the second verbatim";
return;
}
op_second = operator_tmp;
operator_tmp = "";
++i;
if(i == n){
error_msg = "pluralization: (v1;v2) error extracting the second verbatim";
is_pblm = true;
return;
}
//
// step 3: the third element
//
if(str[i - 1] == ';'){
is_third = true;
// stripping WS
if(str[i] == ' ' && str[i - 2] == ' '){
op_second.pop_back();
++i;
}
extract_verbatim(delims, is_pblm, str, i, n, operator_tmp, ")", false, false, false);
if(is_pblm){
error_msg = "pluralization: (v1;v2;v3) error extracting the third verbatim";
return;
}
op_third = operator_tmp;
operator_tmp = "";
++i;
}
// taikng care of the different cases
enquote(op_first);
enquote(op_second);
if(is_third){
enquote(op_third);
op_first += "zero";
if(is_special){
op_third = op_second;
}
op_second += "singular";
op_third += "plural";
} else {
op_first += "singular";
op_second += "plural";
}
operator_vec.push_back(op_first);
operator_vec.push_back(op_second);
if(is_third){
operator_vec.push_back(op_third);
}
} else if(str[i] == ','){
// comma: separator of operators
if(!operator_tmp.empty()){
operator_vec.push_back(operator_tmp);
operator_tmp = "";
}
in_operator = false;
++i;
while(i < n && is_blank(str[i])) ++i;
} else {
// regular operators
if(!in_operator) in_operator = true;
operator_tmp += str[i++];
}
}
if(i == n){
error_msg = "pluralization: the interpolation ends incorrectly";
is_pblm = true;
return;
}
any_plural = true;