-
Notifications
You must be signed in to change notification settings - Fork 679
/
Copy pathecma-regexp-object.c
3188 lines (2626 loc) · 89.4 KB
/
ecma-regexp-object.c
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
/* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ecma-regexp-object.h"
#include "ecma-alloc.h"
#include "ecma-array-object.h"
#include "ecma-builtin-helpers.h"
#include "ecma-exceptions.h"
#include "ecma-function-object.h"
#include "ecma-gc.h"
#include "ecma-globals.h"
#include "ecma-objects.h"
#include "jcontext.h"
#include "jrt-libc-includes.h"
#include "lit-char-helpers.h"
#include "re-compiler.h"
#if JERRY_BUILTIN_REGEXP
#define ECMA_BUILTINS_INTERNAL
#include "ecma-builtins-internal.h"
/** \addtogroup ecma ECMA
* @{
*
* \addtogroup ecmaregexpobject ECMA RegExp object related routines
* @{
*/
/**
* Index of the global capturing group
*/
#define RE_GLOBAL_CAPTURE 0
/**
* Parse RegExp flags (global, ignoreCase, multiline)
*
* See also: ECMA-262 v5, 15.10.4.1
*
* @return empty ecma value - if parsed successfully
* error ecma value - otherwise
*
* Returned value must be freed with ecma_free_value
*/
ecma_value_t
ecma_regexp_parse_flags (ecma_string_t *flags_str_p, /**< Input string with flags */
uint16_t *flags_p) /**< [out] parsed flag bits */
{
ecma_value_t ret_value = ECMA_VALUE_EMPTY;
uint16_t result_flags = RE_FLAG_EMPTY;
ECMA_STRING_TO_UTF8_STRING (flags_str_p, flags_start_p, flags_start_size);
const lit_utf8_byte_t *flags_str_curr_p = flags_start_p;
const lit_utf8_byte_t *flags_str_end_p = flags_start_p + flags_start_size;
while (flags_str_curr_p < flags_str_end_p)
{
ecma_regexp_flags_t flag;
switch (*flags_str_curr_p++)
{
case 'g':
{
flag = RE_FLAG_GLOBAL;
break;
}
case 'i':
{
flag = RE_FLAG_IGNORE_CASE;
break;
}
case 'm':
{
flag = RE_FLAG_MULTILINE;
break;
}
case 'y':
{
flag = RE_FLAG_STICKY;
break;
}
case 'u':
{
flag = RE_FLAG_UNICODE;
break;
}
case 's':
{
flag = RE_FLAG_DOTALL;
break;
}
default:
{
flag = RE_FLAG_EMPTY;
break;
}
}
if (flag == RE_FLAG_EMPTY || (result_flags & flag) != 0)
{
ret_value = ecma_raise_syntax_error (ECMA_ERR_INVALID_REGEXP_FLAGS);
break;
}
result_flags = (uint16_t) (result_flags | flag);
}
ECMA_FINALIZE_UTF8_STRING (flags_start_p, flags_start_size);
*flags_p = result_flags;
return ret_value;
} /* ecma_regexp_parse_flags */
/**
* RegExpAlloc method
*
* See also: ECMA-262 v5, 15.10.4.1
* ECMA-262 v6, 21.2.3.2.1
*
* Note:
* Returned value must be freed with ecma_free_value.
*
* @return ecma_object_t
*/
ecma_object_t *
ecma_op_regexp_alloc (ecma_object_t *ctr_obj_p) /**< constructor object pointer */
{
if (ctr_obj_p == NULL)
{
ctr_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_REGEXP);
}
ecma_object_t *proto_obj_p = ecma_op_get_prototype_from_constructor (ctr_obj_p, ECMA_BUILTIN_ID_REGEXP_PROTOTYPE);
if (JERRY_UNLIKELY (proto_obj_p == NULL))
{
return proto_obj_p;
}
ecma_object_t *new_object_p =
ecma_create_object (proto_obj_p, sizeof (ecma_extended_object_t), ECMA_OBJECT_TYPE_CLASS);
ecma_deref_object (proto_obj_p);
ecma_extended_object_t *regexp_obj_p = (ecma_extended_object_t *) new_object_p;
/* Class id will be initialized after the bytecode is compiled. */
regexp_obj_p->u.cls.type = ECMA_OBJECT_CLASS__MAX;
ecma_value_t status = ecma_builtin_helper_def_prop (new_object_p,
ecma_get_magic_string (LIT_MAGIC_STRING_LASTINDEX_UL),
ecma_make_uint32_value (0),
(uint32_t) ECMA_PROPERTY_FLAG_WRITABLE | JERRY_PROP_SHOULD_THROW);
JERRY_ASSERT (ecma_is_value_true (status));
return new_object_p;
} /* ecma_op_regexp_alloc */
/**
* Helper method for initializing an aready existing RegExp object.
*/
static inline void JERRY_ATTR_ALWAYS_INLINE
ecma_op_regexp_initialize (ecma_object_t *regexp_obj_p, /**< RegExp object */
const re_compiled_code_t *bc_p) /**< bytecode */
{
ecma_extended_object_t *ext_obj_p = (ecma_extended_object_t *) regexp_obj_p;
ext_obj_p->u.cls.type = ECMA_OBJECT_CLASS_REGEXP;
ECMA_SET_INTERNAL_VALUE_POINTER (ext_obj_p->u.cls.u3.value, bc_p);
} /* ecma_op_regexp_initialize */
/**
* Method for creating a RegExp object from pattern.
*
* Note:
* Allocation have to happen before invoking this function using ecma_op_regexp_alloc.
*
* @return ecma_value_t
*/
ecma_value_t
ecma_op_create_regexp_from_pattern (ecma_object_t *regexp_obj_p, /**< RegExp object */
ecma_value_t pattern_value, /**< pattern */
ecma_value_t flags_value) /**< flags */
{
ecma_string_t *pattern_str_p = ecma_regexp_read_pattern_str_helper (pattern_value);
uint16_t flags = 0;
if (JERRY_UNLIKELY (pattern_str_p == NULL))
{
return ECMA_VALUE_ERROR;
}
if (!ecma_is_value_undefined (flags_value))
{
ecma_string_t *flags_str_p = ecma_op_to_string (flags_value);
if (JERRY_UNLIKELY (flags_str_p == NULL))
{
ecma_deref_ecma_string (pattern_str_p);
return ECMA_VALUE_ERROR;
}
ecma_value_t parse_flags_value = ecma_regexp_parse_flags (flags_str_p, &flags);
ecma_deref_ecma_string (flags_str_p);
if (ECMA_IS_VALUE_ERROR (parse_flags_value))
{
ecma_deref_ecma_string (pattern_str_p);
return parse_flags_value;
}
JERRY_ASSERT (ecma_is_value_empty (parse_flags_value));
}
re_compiled_code_t *bc_p = re_compile_bytecode (pattern_str_p, flags);
if (JERRY_UNLIKELY (bc_p == NULL))
{
ecma_deref_ecma_string (pattern_str_p);
return ECMA_VALUE_ERROR;
}
ecma_op_regexp_initialize (regexp_obj_p, bc_p);
ecma_deref_ecma_string (pattern_str_p);
return ecma_make_object_value (regexp_obj_p);
} /* ecma_op_create_regexp_from_pattern */
/**
* Method for creating a RegExp object from bytecode.
*
* Note:
* Allocation have to happen before invoking this function using ecma_op_regexp_alloc.
*
* @return ecma_value_t
*/
ecma_value_t
ecma_op_create_regexp_from_bytecode (ecma_object_t *regexp_obj_p, /**< RegExp object */
re_compiled_code_t *bc_p) /**< bytecode */
{
ecma_bytecode_ref ((ecma_compiled_code_t *) bc_p);
ecma_op_regexp_initialize (regexp_obj_p, bc_p);
return ecma_make_object_value (regexp_obj_p);
} /* ecma_op_create_regexp_from_bytecode */
/**
* Method for creating a RegExp object from pattern with already parsed flags.
*
* Note:
* Allocation have to happen before invoking this function using ecma_op_regexp_alloc.
*
* @return ecma_value_t
*/
ecma_value_t
ecma_op_create_regexp_with_flags (ecma_object_t *regexp_obj_p, /**< RegExp object */
ecma_value_t pattern_value, /**< pattern */
uint16_t flags) /**< flags */
{
ecma_string_t *pattern_str_p = ecma_regexp_read_pattern_str_helper (pattern_value);
if (JERRY_UNLIKELY (pattern_str_p == NULL))
{
return ECMA_VALUE_ERROR;
}
re_compiled_code_t *bc_p = re_compile_bytecode (pattern_str_p, flags);
ecma_deref_ecma_string (pattern_str_p);
if (JERRY_UNLIKELY (bc_p == NULL))
{
return ECMA_VALUE_ERROR;
}
ecma_op_regexp_initialize (regexp_obj_p, bc_p);
return ecma_make_object_value (regexp_obj_p);
} /* ecma_op_create_regexp_with_flags */
/**
* Canonicalize a character
*
* @return ecma_char_t canonicalized character
*/
lit_code_point_t
ecma_regexp_canonicalize_char (lit_code_point_t ch, /**< character */
bool unicode) /**< unicode */
{
if (unicode)
{
/* In unicode mode the mappings contained in the CaseFolding.txt file should be used to canonicalize the character.
* These mappings generally correspond to the lowercase variant of the character, however there are some
* differences. In some cases the uppercase variant is used, in others the lowercase of the uppercase character is
* used, and there are also cases where the character has no case folding mapping even though it has upper/lower
* variants. Since lowercasing is the most common this is used as the default behaviour, and characters with
* differing behaviours are encoded in lookup tables. */
if (lit_char_fold_to_upper (ch))
{
ch = lit_char_to_upper_case (ch, NULL);
JERRY_ASSERT (ch != LIT_MULTIPLE_CU);
}
if (lit_char_fold_to_lower (ch))
{
ch = lit_char_to_lower_case (ch, NULL);
JERRY_ASSERT (ch != LIT_MULTIPLE_CU);
}
return ch;
}
JERRY_UNUSED (unicode);
lit_code_point_t cu = lit_char_to_upper_case (ch, NULL);
if (ch <= LIT_UTF8_1_BYTE_CODE_POINT_MAX || (cu > LIT_UTF8_1_BYTE_CODE_POINT_MAX && cu != LIT_MULTIPLE_CU))
{
return cu;
}
return ch;
} /* ecma_regexp_canonicalize_char */
/**
* RegExp Canonicalize abstract operation
*
* See also: ECMA-262 v5, 15.10.2.8
*
* @return ecma_char_t canonicalized character
*/
static inline lit_code_point_t JERRY_ATTR_ALWAYS_INLINE
ecma_regexp_canonicalize (lit_code_point_t ch, /**< character */
uint16_t flags) /**< flags */
{
if (flags & RE_FLAG_IGNORE_CASE)
{
return ecma_regexp_canonicalize_char (ch, flags & RE_FLAG_UNICODE);
}
return ch;
} /* ecma_regexp_canonicalize */
/**
* Check if a code point is matched by a class escape.
*
* @return true, if code point matches escape
* false, otherwise
*/
static bool
ecma_regexp_check_class_escape (lit_code_point_t cp, /**< char */
ecma_class_escape_t escape) /**< escape */
{
switch (escape)
{
case RE_ESCAPE_DIGIT:
{
return (cp >= LIT_CHAR_0 && cp <= LIT_CHAR_9);
}
case RE_ESCAPE_NOT_DIGIT:
{
return (cp < LIT_CHAR_0 || cp > LIT_CHAR_9);
}
case RE_ESCAPE_WORD_CHAR:
{
return lit_char_is_word_char (cp);
}
case RE_ESCAPE_NOT_WORD_CHAR:
{
return !lit_char_is_word_char (cp);
}
case RE_ESCAPE_WHITESPACE:
{
return lit_char_is_white_space ((ecma_char_t) cp);
}
case RE_ESCAPE_NOT_WHITESPACE:
{
return !lit_char_is_white_space ((ecma_char_t) cp);
}
default:
{
JERRY_UNREACHABLE ();
}
}
} /* ecma_regexp_check_class_escape */
/**
* Helper function to get current code point or code unit depending on execution mode,
* and advance the string pointer.
*
* @return lit_code_point_t current code point
*/
static lit_code_point_t
ecma_regexp_advance (ecma_regexp_ctx_t *re_ctx_p, /**< regexp context */
const lit_utf8_byte_t **str_p) /**< reference to string pointer */
{
JERRY_ASSERT (str_p != NULL);
lit_code_point_t cp = lit_cesu8_read_next (str_p);
if (JERRY_UNLIKELY (re_ctx_p->flags & RE_FLAG_UNICODE) && lit_is_code_point_utf16_high_surrogate ((ecma_char_t) cp)
&& *str_p < re_ctx_p->input_end_p)
{
const ecma_char_t next_ch = lit_cesu8_peek_next (*str_p);
if (lit_is_code_point_utf16_low_surrogate (next_ch))
{
cp = lit_convert_surrogate_pair_to_code_point ((ecma_char_t) cp, next_ch);
*str_p += LIT_UTF8_MAX_BYTES_IN_CODE_UNIT;
}
}
return ecma_regexp_canonicalize (cp, re_ctx_p->flags);
} /* ecma_regexp_advance */
/**
* Helper function to get current full unicode code point and advance the string pointer.
*
* @return lit_code_point_t current code point
*/
lit_code_point_t
ecma_regexp_unicode_advance (const lit_utf8_byte_t **str_p, /**< reference to string pointer */
const lit_utf8_byte_t *end_p) /**< string end pointer */
{
JERRY_ASSERT (str_p != NULL);
const lit_utf8_byte_t *current_p = *str_p;
lit_code_point_t ch = lit_cesu8_read_next (¤t_p);
if (lit_is_code_point_utf16_high_surrogate ((ecma_char_t) ch) && current_p < end_p)
{
const ecma_char_t next_ch = lit_cesu8_peek_next (current_p);
if (lit_is_code_point_utf16_low_surrogate (next_ch))
{
ch = lit_convert_surrogate_pair_to_code_point ((ecma_char_t) ch, next_ch);
current_p += LIT_UTF8_MAX_BYTES_IN_CODE_UNIT;
}
}
*str_p = current_p;
return ch;
} /* ecma_regexp_unicode_advance */
/**
* Helper function to revert the string pointer to the previous code point.
*
* @return pointer to previous code point
*/
static JERRY_ATTR_NOINLINE const lit_utf8_byte_t *
ecma_regexp_step_back (ecma_regexp_ctx_t *re_ctx_p, /**< regexp context */
const lit_utf8_byte_t *str_p) /**< reference to string pointer */
{
JERRY_ASSERT (str_p != NULL);
lit_code_point_t ch = lit_cesu8_read_prev (&str_p);
if (JERRY_UNLIKELY (re_ctx_p->flags & RE_FLAG_UNICODE) && lit_is_code_point_utf16_low_surrogate (ch)
&& lit_is_code_point_utf16_high_surrogate (lit_cesu8_peek_prev (str_p)))
{
str_p -= LIT_UTF8_MAX_BYTES_IN_CODE_UNIT;
}
return str_p;
} /* ecma_regexp_step_back */
/**
* Check if the current poisition is on a word boundary.
*
* @return true, if on a word boundary
* false - otherwise
*/
static bool
ecma_regexp_is_word_boundary (ecma_regexp_ctx_t *re_ctx_p, /**< regexp context */
const lit_utf8_byte_t *str_p) /**< string pointer */
{
lit_code_point_t left_cp;
lit_code_point_t right_cp;
if (JERRY_UNLIKELY (str_p <= re_ctx_p->input_start_p))
{
left_cp = LIT_INVALID_CP;
}
else if (JERRY_UNLIKELY ((re_ctx_p->flags & (RE_FLAG_UNICODE | RE_FLAG_IGNORE_CASE))
== (RE_FLAG_UNICODE | RE_FLAG_IGNORE_CASE)))
{
const lit_utf8_byte_t *prev_p = ecma_regexp_step_back (re_ctx_p, str_p);
left_cp = ecma_regexp_advance (re_ctx_p, &prev_p);
JERRY_ASSERT (prev_p == str_p);
}
else
{
left_cp = str_p[-1];
}
if (JERRY_UNLIKELY (str_p >= re_ctx_p->input_end_p))
{
right_cp = LIT_INVALID_CP;
}
else if (JERRY_UNLIKELY ((re_ctx_p->flags & (RE_FLAG_UNICODE | RE_FLAG_IGNORE_CASE))
== (RE_FLAG_UNICODE | RE_FLAG_IGNORE_CASE)))
{
right_cp = ecma_regexp_advance (re_ctx_p, &str_p);
}
else
{
right_cp = str_p[0];
}
return lit_char_is_word_char (left_cp) != lit_char_is_word_char (right_cp);
} /* ecma_regexp_is_word_boundary */
/**
* Recursive function for executing RegExp bytecode.
*
* See also:
* ECMA-262 v5, 15.10.2.1
*
* @return pointer to the end of the currently matched substring
* NULL, if pattern did not match
*/
static const lit_utf8_byte_t *
ecma_regexp_run (ecma_regexp_ctx_t *re_ctx_p, /**< RegExp matcher context */
const uint8_t *bc_p, /**< pointer to the current RegExp bytecode */
const lit_utf8_byte_t *str_curr_p) /**< input string pointer */
{
#if (JERRY_STACK_LIMIT != 0)
if (JERRY_UNLIKELY (ecma_get_current_stack_usage () > CONFIG_MEM_STACK_LIMIT))
{
return ECMA_RE_OUT_OF_STACK;
}
#endif /* JERRY_STACK_LIMIT != 0 */
const lit_utf8_byte_t *str_start_p = str_curr_p;
const uint8_t *next_alternative_p = NULL;
while (true)
{
const re_opcode_t op = re_get_opcode (&bc_p);
switch (op)
{
case RE_OP_EOF:
{
re_ctx_p->captures_p[RE_GLOBAL_CAPTURE].end_p = str_curr_p;
/* FALLTHRU */
}
case RE_OP_ASSERT_END:
case RE_OP_ITERATOR_END:
{
return str_curr_p;
}
case RE_OP_ALTERNATIVE_START:
{
const uint32_t offset = re_get_value (&bc_p);
next_alternative_p = bc_p + offset;
continue;
}
case RE_OP_ALTERNATIVE_NEXT:
{
while (true)
{
const uint32_t offset = re_get_value (&bc_p);
bc_p += offset;
if (*bc_p != RE_OP_ALTERNATIVE_NEXT)
{
break;
}
bc_p++;
}
continue;
}
case RE_OP_NO_ALTERNATIVE:
{
return NULL;
}
case RE_OP_CAPTURING_GROUP_START:
{
const uint32_t group_idx = re_get_value (&bc_p);
ecma_regexp_capture_t *const group_p = re_ctx_p->captures_p + group_idx;
group_p->subcapture_count = re_get_value (&bc_p);
const lit_utf8_byte_t *const saved_begin_p = group_p->begin_p;
const lit_utf8_byte_t *const saved_end_p = group_p->end_p;
const uint32_t saved_iterator = group_p->iterator;
const uint32_t qmin = re_get_value (&bc_p);
group_p->end_p = NULL;
/* If zero iterations are allowed, then execute the end opcode which will handle further iterations,
* otherwise run the 1st iteration immediately by executing group bytecode. */
if (qmin == 0)
{
group_p->iterator = 0;
group_p->begin_p = NULL;
const uint32_t end_offset = re_get_value (&bc_p);
group_p->bc_p = bc_p;
bc_p += end_offset;
}
else
{
group_p->iterator = 1;
group_p->begin_p = str_curr_p;
group_p->bc_p = bc_p;
}
const lit_utf8_byte_t *matched_p = ecma_regexp_run (re_ctx_p, bc_p, str_curr_p);
group_p->iterator = saved_iterator;
if (matched_p == NULL)
{
group_p->begin_p = saved_begin_p;
group_p->end_p = saved_end_p;
goto fail;
}
return matched_p;
}
case RE_OP_NON_CAPTURING_GROUP_START:
{
const uint32_t group_idx = re_get_value (&bc_p);
ecma_regexp_non_capture_t *const group_p = re_ctx_p->non_captures_p + group_idx;
group_p->subcapture_start = re_get_value (&bc_p);
group_p->subcapture_count = re_get_value (&bc_p);
const lit_utf8_byte_t *const saved_begin_p = group_p->begin_p;
const uint32_t saved_iterator = group_p->iterator;
const uint32_t qmin = re_get_value (&bc_p);
/* If zero iterations are allowed, then execute the end opcode which will handle further iterations,
* otherwise run the 1st iteration immediately by executing group bytecode. */
if (qmin == 0)
{
group_p->iterator = 0;
group_p->begin_p = NULL;
const uint32_t end_offset = re_get_value (&bc_p);
group_p->bc_p = bc_p;
bc_p += end_offset;
}
else
{
group_p->iterator = 1;
group_p->begin_p = str_curr_p;
group_p->bc_p = bc_p;
}
const lit_utf8_byte_t *matched_p = ecma_regexp_run (re_ctx_p, bc_p, str_curr_p);
group_p->iterator = saved_iterator;
if (matched_p == NULL)
{
group_p->begin_p = saved_begin_p;
goto fail;
}
return matched_p;
}
case RE_OP_GREEDY_CAPTURING_GROUP_END:
{
const uint32_t group_idx = re_get_value (&bc_p);
ecma_regexp_capture_t *const group_p = re_ctx_p->captures_p + group_idx;
const uint32_t qmin = re_get_value (&bc_p);
if (group_p->iterator < qmin)
{
/* No need to save begin_p since we don't have to backtrack beyond the minimum iteration count, but we have
* to clear nested capturing groups. */
group_p->begin_p = str_curr_p;
for (uint32_t i = 1; i < group_p->subcapture_count; ++i)
{
group_p[i].begin_p = NULL;
}
group_p->iterator++;
const lit_utf8_byte_t *const matched_p = ecma_regexp_run (re_ctx_p, group_p->bc_p, str_curr_p);
if (matched_p != NULL)
{
return matched_p;
}
group_p->iterator--;
goto fail;
}
/* Empty matches are not allowed after reaching the minimum number of iterations. */
if (JERRY_UNLIKELY (group_p->begin_p >= str_curr_p) && (group_p->iterator > qmin))
{
goto fail;
}
const uint32_t qmax = re_get_value (&bc_p) - RE_QMAX_OFFSET;
if (JERRY_UNLIKELY (group_p->iterator >= qmax))
{
/* Reached maximum number of iterations, try to match tail bytecode. */
group_p->end_p = str_curr_p;
const lit_utf8_byte_t *const matched_p = ecma_regexp_run (re_ctx_p, bc_p, str_curr_p);
if (matched_p != NULL)
{
return matched_p;
}
goto fail;
}
{
/* Save and clear all nested capturing groups, and try to iterate. */
JERRY_VLA (const lit_utf8_byte_t *, saved_captures_p, group_p->subcapture_count);
for (uint32_t i = 0; i < group_p->subcapture_count; ++i)
{
saved_captures_p[i] = group_p[i].begin_p;
group_p[i].begin_p = NULL;
}
group_p->iterator++;
group_p->begin_p = str_curr_p;
const lit_utf8_byte_t *const matched_p = ecma_regexp_run (re_ctx_p, group_p->bc_p, str_curr_p);
if (matched_p != NULL)
{
return matched_p;
}
/* Failed to iterate again, backtrack to current match, and try to run tail bytecode. */
for (uint32_t i = 0; i < group_p->subcapture_count; ++i)
{
group_p[i].begin_p = saved_captures_p[i];
}
group_p->iterator--;
group_p->end_p = str_curr_p;
}
const lit_utf8_byte_t *const tail_match_p = ecma_regexp_run (re_ctx_p, bc_p, str_curr_p);
if (tail_match_p != NULL)
{
return tail_match_p;
}
goto fail;
}
case RE_OP_GREEDY_NON_CAPTURING_GROUP_END:
{
const uint32_t group_idx = re_get_value (&bc_p);
ecma_regexp_non_capture_t *const group_p = re_ctx_p->non_captures_p + group_idx;
const uint32_t qmin = re_get_value (&bc_p);
if (group_p->iterator < qmin)
{
/* No need to save begin_p but we have to clear nested capturing groups. */
group_p->begin_p = str_curr_p;
ecma_regexp_capture_t *const capture_p = re_ctx_p->captures_p + group_p->subcapture_start;
for (uint32_t i = 0; i < group_p->subcapture_count; ++i)
{
capture_p[i].begin_p = NULL;
}
group_p->iterator++;
const lit_utf8_byte_t *const matched_p = ecma_regexp_run (re_ctx_p, group_p->bc_p, str_curr_p);
if (matched_p != NULL)
{
return matched_p;
}
group_p->iterator--;
goto fail;
}
/* Empty matches are not allowed after reaching the minimum number of iterations. */
if (JERRY_UNLIKELY (group_p->begin_p >= str_curr_p) && (group_p->iterator > qmin))
{
goto fail;
}
const uint32_t qmax = re_get_value (&bc_p) - RE_QMAX_OFFSET;
if (JERRY_UNLIKELY (group_p->iterator >= qmax))
{
/* Reached maximum number of iterations, try to match tail bytecode. */
const lit_utf8_byte_t *const matched_p = ecma_regexp_run (re_ctx_p, bc_p, str_curr_p);
if (matched_p != NULL)
{
return matched_p;
}
goto fail;
}
{
/* Save and clear all nested capturing groups, and try to iterate. */
JERRY_VLA (const lit_utf8_byte_t *, saved_captures_p, group_p->subcapture_count);
for (uint32_t i = 0; i < group_p->subcapture_count; ++i)
{
ecma_regexp_capture_t *const capture_p = re_ctx_p->captures_p + group_p->subcapture_start + i;
saved_captures_p[i] = capture_p->begin_p;
capture_p->begin_p = NULL;
}
group_p->iterator++;
const lit_utf8_byte_t *const saved_begin_p = group_p->begin_p;
group_p->begin_p = str_curr_p;
const lit_utf8_byte_t *const matched_p = ecma_regexp_run (re_ctx_p, group_p->bc_p, str_curr_p);
if (matched_p != NULL)
{
return matched_p;
}
/* Failed to iterate again, backtrack to current match, and try to run tail bytecode. */
for (uint32_t i = 0; i < group_p->subcapture_count; ++i)
{
ecma_regexp_capture_t *const capture_p = re_ctx_p->captures_p + group_p->subcapture_start + i;
capture_p->begin_p = saved_captures_p[i];
}
group_p->iterator--;
group_p->begin_p = saved_begin_p;
}
const lit_utf8_byte_t *const tail_match_p = ecma_regexp_run (re_ctx_p, bc_p, str_curr_p);
if (tail_match_p != NULL)
{
return tail_match_p;
}
goto fail;
}
case RE_OP_LAZY_CAPTURING_GROUP_END:
{
const uint32_t group_idx = re_get_value (&bc_p);
ecma_regexp_capture_t *const group_p = re_ctx_p->captures_p + group_idx;
const uint32_t qmin = re_get_value (&bc_p);
if (group_p->iterator < qmin)
{
/* No need to save begin_p but we have to clear nested capturing groups. */
group_p->begin_p = str_curr_p;
for (uint32_t i = 1; i < group_p->subcapture_count; ++i)
{
group_p[i].begin_p = NULL;
}
group_p->iterator++;
const lit_utf8_byte_t *const matched_p = ecma_regexp_run (re_ctx_p, group_p->bc_p, str_curr_p);
if (matched_p != NULL)
{
return matched_p;
}
group_p->iterator--;
goto fail;
}
/* Empty matches are not allowed after reaching the minimum number of iterations. */
if (JERRY_UNLIKELY (group_p->begin_p >= str_curr_p) && (group_p->iterator > qmin))
{
goto fail;
}
const uint32_t qmax = re_get_value (&bc_p) - RE_QMAX_OFFSET;
group_p->end_p = str_curr_p;
/* Try to match tail bytecode. */
const lit_utf8_byte_t *const tail_match_p = ecma_regexp_run (re_ctx_p, bc_p, str_curr_p);
if (tail_match_p != NULL)
{
return tail_match_p;
}
if (JERRY_UNLIKELY (group_p->iterator >= qmax))
{
/* Reached maximum number of iterations and tail bytecode did not match. */
goto fail;
}
{
/* Save and clear all nested capturing groups, and try to iterate. */
JERRY_VLA (const lit_utf8_byte_t *, saved_captures_p, group_p->subcapture_count);
for (uint32_t i = 0; i < group_p->subcapture_count; ++i)
{
saved_captures_p[i] = group_p[i].begin_p;
group_p[i].begin_p = NULL;
}
group_p->iterator++;
group_p->begin_p = str_curr_p;
const lit_utf8_byte_t *const matched_p = ecma_regexp_run (re_ctx_p, group_p->bc_p, str_curr_p);
if (matched_p != NULL)
{
return matched_p;
}
/* Backtrack to current match. */
for (uint32_t i = 0; i < group_p->subcapture_count; ++i)
{
group_p[i].begin_p = saved_captures_p[i];
}
group_p->iterator--;
}
goto fail;
}
case RE_OP_LAZY_NON_CAPTURING_GROUP_END:
{
const uint32_t group_idx = re_get_value (&bc_p);
ecma_regexp_non_capture_t *const group_p = re_ctx_p->non_captures_p + group_idx;
const uint32_t qmin = re_get_value (&bc_p);
if (group_p->iterator < qmin)
{
/* Clear nested captures. */
ecma_regexp_capture_t *const capture_p = re_ctx_p->captures_p + group_p->subcapture_start;
for (uint32_t i = 0; i < group_p->subcapture_count; ++i)
{
capture_p[i].begin_p = NULL;
}
group_p->iterator++;
const lit_utf8_byte_t *const matched_p = ecma_regexp_run (re_ctx_p, group_p->bc_p, str_curr_p);
if (matched_p != NULL)
{
return matched_p;
}
group_p->iterator--;
goto fail;
}
/* Empty matches are not allowed after reaching the minimum number of iterations. */
if (JERRY_UNLIKELY (group_p->begin_p >= str_curr_p) && (group_p->iterator > qmin))
{
goto fail;
}
const uint32_t qmax = re_get_value (&bc_p) - RE_QMAX_OFFSET;
/* Try to match tail bytecode. */
const lit_utf8_byte_t *const tail_match_p = ecma_regexp_run (re_ctx_p, bc_p, str_curr_p);
if (tail_match_p != NULL)
{
return tail_match_p;
}
if (JERRY_UNLIKELY (group_p->iterator >= qmax))
{
/* Reached maximum number of iterations and tail bytecode did not match. */
goto fail;
}
{
/* Save and clear all nested capturing groups, and try to iterate. */
JERRY_VLA (const lit_utf8_byte_t *, saved_captures_p, group_p->subcapture_count);
for (uint32_t i = 0; i < group_p->subcapture_count; ++i)
{
ecma_regexp_capture_t *const capture_p = re_ctx_p->captures_p + group_p->subcapture_start + i;
saved_captures_p[i] = capture_p->begin_p;
capture_p->begin_p = NULL;
}
group_p->iterator++;
const lit_utf8_byte_t *const saved_begin_p = group_p->begin_p;
group_p->begin_p = str_curr_p;
const lit_utf8_byte_t *const matched_p = ecma_regexp_run (re_ctx_p, group_p->bc_p, str_curr_p);
if (matched_p != NULL)
{
return matched_p;
}
/* Backtrack to current match. */