-
Notifications
You must be signed in to change notification settings - Fork 7
/
pcre4pl.c
1807 lines (1578 loc) · 60.5 KB
/
pcre4pl.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
/* Part of SWI-Prolog
Author: Jan Wielemaker and Peter Ludemann
E-mail: [email protected]
WWW: http://www.swi-prolog.org
Copyright (c) 2017-2023, VU University Amsterdam
SWI-Prolog Solution b.v.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
/* See also: https://www.regular-expressions.info/pcre2.html */
/* See also: https://github.com/PhilipHazel/pcre2/issues/51 */
/* See also: https://www.regular-expressions.info/pcre2.html */
/* See also: https://github.com/i3/i3/issues/4682#issuecomment-973076704 */
/* See also: https://wiki.php.net/rfc/pcre2-migration */
#define PCRE2_CODE_UNIT_WIDTH 8
#include <SWI-Stream.h>
#include <SWI-Prolog.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <pcre2.h>
#ifdef O_DEBUG
#define DEBUG(g) g
#else
#define DEBUG(g) (void)0
#endif
/* For testing systems that don't have JIT on systems with a JIT,
uncomment the following: */
/* #define FORCE_NO_JIT 1 */
/*******************************
* RE STRUCT *
*******************************/
typedef enum cap_type /* capture type */
{ CAP_DEFAULT = 0, /* Use the "global" value for the regex */
CAP_STRING,
CAP_ATOM,
CAP_INTEGER,
CAP_FLOAT,
CAP_NUMBER,
CAP_TERM,
CAP_RANGE
} cap_type;
/* For debugging - map cap_type to string */
static const char *
cap_type_str(uint32_t i) /* cap_type i */
{ switch( i )
{ case CAP_DEFAULT: return "CAP_DEFAULT";
case CAP_STRING: return "CAP_STRING";
case CAP_ATOM: return "CAP_ATOM";
case CAP_INTEGER: return "CAP_INTEGER";
case CAP_FLOAT: return "CAP_FLOAT";
case CAP_NUMBER: return "CAP_NUMBER";
case CAP_TERM: return "CAP_TERM";
case CAP_RANGE: return "CAP_RANGE";
default: return "CAP_???";
}
}
typedef struct cap_how /* capture "how" [name + type] */
{ atom_t name; /* 0 if it's unnamed (referenced by number position) */
cap_type type;
} cap_how;
/* When processing options, only the first value for a flag is used;
any subsequent values are ignored. The `seen` field tracks this.
Some flags use multiple bits (`bsr` and `newline`) - to handle
these, the set_flag() function has a `mask` parameter.
This struct should be initialized to {0,0}.
*/
typedef struct re_options_flags
{ uint32_t seen;
uint32_t flags;
} re_options_flags;
/* The data in a "regex" blob. This is created by re_compile_() and a
local copy is used by the matching functions (see get_re_copy()).
The flags don't need to be kept, but they're convenient for
processing. When get_re_copy() makes a local copy, it clears the
flags that are specific to pcre2_match() - match_options_flags,
start_flags.
*/
typedef struct re_data
{ atom_t symbol; /* regex as blob - for error terms */
atom_t pattern; /* pattern (as atom) */
re_options_flags compile_options_flags;
re_options_flags capture_type; /* Default capture_type: cap_type */
re_options_flags optimise_flags; /* Turns on JIT */
re_options_flags jit_options_flags;
re_options_flags compile_ctx_flags;
re_options_flags compile_bsr_flags;
re_options_flags compile_newline_flags;
re_options_flags match_options_flags;
re_options_flags start_flags; /* The start position (int) - a "match" flag */
uint32_t capture_size; /* # captured subpatterns */
cap_how *capture_names; /* Names for captured data */
pcre2_code_8 *re_compiled; /* The compiled pattern */
} re_data;
static int re_compile_impl(re_data *re, size_t len, char *pats);
static void acquire_pcre(atom_t symbol);
static int release_pcre(atom_t symbol);
static int compare_pcres(atom_t a, atom_t b);
static int write_pcre(IOSTREAM *s, atom_t symbol, int flags);
static int save_pcre(atom_t symbol, IOSTREAM *fd);
static atom_t load_pcre(IOSTREAM *fd);
static PL_blob_t pcre2_blob =
{ .magic = PL_BLOB_MAGIC,
.flags = 0,
.name = "regex",
.acquire = acquire_pcre,
.release = release_pcre,
.compare = compare_pcres,
.write = write_pcre,
.save = save_pcre,
.load = load_pcre
};
/* The start position is PCRE2_SIZE in pcre2.h: */
/* TODO: Our flag (which contains the size) is only 32 bits but PCRE2 allows 64 bits */
#define OPTSTART_MASK ((uint32_t)PCRE2_SIZE_MAX)
static void
init_re_data(re_data *re)
{ memset(re, 0, sizeof *re);
/* See also get_re_copy() */
/* PCRE2_NO_UTF_CHECK means that the pattern string (for compile)
and subject string (for match) isn't checked for validity; this
gives a small performance improvement - we simply trust
SWI-Prolog to do the right thing with UTF8 strings. (The PCRE2
documentation warns of undefined behavior, including crashes or
loops when given invalid UTF). If desired, the test can
explicitly be turned on by utf_check(true). */
re->compile_options_flags.flags = PCRE2_UTF|PCRE2_NO_UTF_CHECK;
re->match_options_flags.flags = PCRE2_NO_UTF_CHECK;
re->capture_type.flags = CAP_STRING;
}
static void
write_re_options(IOSTREAM *s, const char **sep, const re_data *re);
static int
free_pcre(re_data *re)
{ /* TODO: clearing the freed items (by assigning 0 or NULL)
isn't necessary. */
if ( re->pattern )
{ PL_unregister_atom(re->pattern);
re->pattern = 0;
}
pcre2_code_free(re->re_compiled);
re->re_compiled = NULL;
if ( re->capture_names )
{ uint32_t i;
for(i=0; i<re->capture_size+1; i++)
{ if ( re->capture_names[i].name )
{ PL_unregister_atom(re->capture_names[i].name);
re->capture_names[i].name = 0;
}
}
free(re->capture_names);
re->capture_names = NULL;
}
return TRUE;
}
static void
acquire_pcre(atom_t symbol)
{ re_data *re = PL_blob_data(symbol, NULL, NULL);
re->symbol = symbol;
}
static int
release_pcre(atom_t symbol)
{ re_data *re = PL_blob_data(symbol, NULL, NULL);
return free_pcre(re);
}
static functor_t FUNCTOR_pair2; /* -/2 */
/*******************************
* SYMBOL WRAPPER *
*******************************/
#define CMP_FIELD(fld) if (rea->fld.flags < reb->fld.flags) return -1; if (rea->fld.flags > reb->fld.flags) return 1
static int
compare_pcres(atom_t a, atom_t b)
{ const re_data *rea = PL_blob_data(a, NULL, NULL);
const re_data *reb = PL_blob_data(b, NULL, NULL);
int comparison;
if ( rea->pattern == reb->pattern )
{ comparison = 0;
} else
{ const pl_wchar_t *sa, *sb;
PL_STRINGS_MARK();
sa = PL_atom_wchars(rea->pattern, NULL);
sb = PL_atom_wchars(reb->pattern, NULL);
comparison = wcscmp(sa, sb);
PL_STRINGS_RELEASE();
}
if ( comparison )
return comparison;
CMP_FIELD(compile_options_flags);
CMP_FIELD(capture_type);
CMP_FIELD(optimise_flags);
CMP_FIELD(jit_options_flags);
CMP_FIELD(compile_ctx_flags);
CMP_FIELD(compile_bsr_flags);
CMP_FIELD(compile_newline_flags);
CMP_FIELD(match_options_flags);
CMP_FIELD(start_flags);
/* No need to compare capture_names because they because they are
derived from the patterns */
/* Equal so far, so use address (which is stable) to break tie: */
return ( (rea > reb) ? 1 :
(rea < reb) ? -1 : 0
);
}
#undef CMP_FIELD
static int
write_pcre(IOSTREAM *s, atom_t symbol, int flags)
{ (void)flags; /* unused arg */
const re_data *re = PL_blob_data(symbol, NULL, NULL);
/* For blob details: re_portray_() - re_portray/2 */
PL_STRINGS_MARK();
SfprintfX(s, "<regex>(%p, /%Ws/)", re, PL_atom_wchars(re->pattern, NULL));
PL_STRINGS_RELEASE();
return TRUE;
}
static int
save_pcre_options_flag(const re_options_flags *options_flag, IOSTREAM *fd)
{ return
PL_qlf_put_uint32(options_flag->seen, fd) &&
PL_qlf_put_uint32(options_flag->flags, fd);
}
static int
load_pcre_options_flag(IOSTREAM *fd, re_options_flags *options_flag)
{ return
PL_qlf_get_uint32(fd, &options_flag->seen) &&
PL_qlf_get_uint32(fd, &options_flag->flags);
}
static int
save_pcre(atom_t symbol, IOSTREAM *fd)
{ const re_data *re = PL_blob_data(symbol, NULL, NULL);
// capture_names and re_compiled aren't saved, but are
// created in load_pcre() by compiling the pattern.
int rc =
PL_qlf_put_uint32(1, fd) && // version #
PL_qlf_put_atom(re->pattern, fd) &&
save_pcre_options_flag(&re->compile_options_flags, fd) &&
save_pcre_options_flag(&re->capture_type, fd) &&
save_pcre_options_flag(&re->optimise_flags, fd) &&
save_pcre_options_flag(&re->jit_options_flags, fd) &&
save_pcre_options_flag(&re->compile_ctx_flags, fd) &&
save_pcre_options_flag(&re->compile_bsr_flags, fd) &&
save_pcre_options_flag(&re->compile_newline_flags, fd) &&
save_pcre_options_flag(&re->match_options_flags, fd) &&
save_pcre_options_flag(&re->start_flags, fd);
DEBUG(Sdprintf("SAVE_PCRE rc=%d\n", rc));
return rc;
}
static atom_t
load_pcre(IOSTREAM *fd)
{ uint32_t version;
DEBUG(Sdprintf("LOAD_PCRE start\n"));
PL_qlf_get_uint32(fd, &version);
if ( version != 1)
{ PL_warning("Version mismatch for PCRE2 blob load");
Sseterr(fd, SIO_FERR, "Version mismatch for PCRE2 blob load");
return (atom_t)0;
}
re_data re;
memset(&re, 0, sizeof re);
if ( !PL_qlf_get_atom(fd, &re.pattern) )
{ DEBUG(Sdprintf("LOAD_PCRE failed (get_atom)\n"));
PL_warning("Failed to load Pcre2 blob");
return (atom_t)0;
}
PL_register_atom(re.pattern);
// From here on, need to call free_pcre() for any failure to ensure
// re.pattern is freed.
if ( !load_pcre_options_flag(fd, &re.compile_options_flags) ||
!load_pcre_options_flag(fd, &re.capture_type) ||
!load_pcre_options_flag(fd, &re.optimise_flags) ||
!load_pcre_options_flag(fd, &re.jit_options_flags) ||
!load_pcre_options_flag(fd, &re.compile_ctx_flags) ||
!load_pcre_options_flag(fd, &re.compile_bsr_flags) ||
!load_pcre_options_flag(fd, &re.compile_newline_flags) ||
!load_pcre_options_flag(fd, &re.match_options_flags) ||
!load_pcre_options_flag(fd, &re.start_flags) )
{ DEBUG(Sdprintf("LOAD_PCRE failed\n"));
(void)free_pcre(&re);
PL_warning("Failed to load Pcre2 blob");
return (atom_t)0;
}
DEBUG(Sdprintf("LOAD_PCRE done load_pcre_*()\n"));
size_t len;
char *pats;
atom_t result =
( PL_atom_mbchars(re.pattern, &len, &pats, REP_UTF8) &&
re_compile_impl(&re, len, pats) )
? PL_new_blob(&re, sizeof re, &pcre2_blob)
: (atom_t)0;
DEBUG(Sdprintf("LOAD_PCRE result=%" PRIuPTR, result));
if (!result)
(void)free_pcre(&re);
return result;
}
/*******************************
* UTF8 UTIL *
*******************************/
typedef struct re_subject
{ char *subject; /* Subject string */
size_t length; /* Total length in bytes */
size_t charp; /* Character position */
size_t bytep; /* Byte position */
} re_subject;
static void
init_subject(re_subject *subject)
{ memset(subject, 0, sizeof *subject); /* { NULL, 0, 0, 0 }; */
}
#define ISUTF8_CB(c) (((c)&0xc0) == 0x80) /* Is continuation byte */
static inline char *
utf8_skip_char(const char *in)
{ if ( !(in[0]&0x80) )
{ return (char*)in+1;
} else
{ in++;
while ( ISUTF8_CB(in[0]) )
in++;
return (char*)in;
}
}
static size_t
utf8_seek(const char *subject, size_t len, size_t offset)
{ const char *s = subject;
const char *e = subject+len;
for(; offset > 0; offset--)
{ s = utf8_skip_char(s);
if ( s >= e )
return (size_t)-1;
}
return s - subject;
}
static size_t
char_offset(const char *subject, size_t byte_offset)
{ size_t co;
const char *e = subject+byte_offset;
for(co=0; subject < e; subject = utf8_skip_char(subject))
co++;
return co;
}
static size_t
bytep_to_charp(re_subject *subj, size_t bytep)
{ if ( subj->bytep > bytep )
{ subj->bytep = subj->charp = 0;
}
subj->charp += char_offset(subj->subject+subj->bytep, bytep-subj->bytep);
subj->bytep = bytep;
return subj->charp;
}
#define GET_NCHARS_FLAGS (CVT_ATOM|CVT_STRING|CVT_LIST|REP_UTF8|CVT_EXCEPTION)
static int /* bool (FALSE/TRUE), as returned by PL_get_...() etc */
re_get_subject(term_t t, re_subject *subj, unsigned int alloc_flags)
{ init_subject(subj);
return PL_get_nchars(t, &subj->length, &subj->subject, alloc_flags|GET_NCHARS_FLAGS);
}
/*******************************
* PROLOG EXCHANGE *
*******************************/
static int /* bool (FALSE/TRUE), as returned by PL_get_...() etc */
get_re(term_t t, re_data **re)
{ size_t len;
PL_blob_t *type;
if ( PL_get_blob(t, (void **)re, &len, &type) && type == &pcre2_blob )
{ /* assert(len == sizeof **re); */
return TRUE;
}
*re = NULL;
return PL_type_error("regex", t);
}
/* Make a copy of the data in a regex blob, clearing the flags that
are specific to pcre2_match(). */
static int /* bool (FALSE/TRUE), as returned by PL_get_...() etc */
get_re_copy(term_t t, re_data *re)
{ re_data *re_ptr;
if ( !get_re(t, &re_ptr) )
return FALSE;
*re = *re_ptr;
/* Initialize the "match" flags - see also init_re_data() */
/* TODO: Combine with init_re_data(), for defaults */
re->match_options_flags.seen = 0;
re->match_options_flags.flags = PCRE2_NO_UTF_CHECK;
re->start_flags.seen = 0;
re->start_flags.flags = 0;
return TRUE;
}
#define RE_OPTIMISE 0x0001
static int /* FALSE/TRUE or -1 for error */
effective_bool(term_t arg)
{ if ( arg )
{ int v;
if ( PL_get_bool_ex(arg, &v) )
return v;
return -1; /* Error: neither FALSE nor TRUE */
}
return TRUE;
}
/* Set a bit flag from a term (e.g. `anchored(true)`). The `mask` has
1 for where the value can be applied; `value` is the flag value
(e.g., from re_optdefs[i].flag) and is never zero. `arg` is used to
determine whether the flag is set to `value` or its inverse, and
`invert` uses the inverse of `value`. For single bit flags, `mask`
and `value` are the same; for multi-bit flags, `mask` is an "or" of
all the possible values.
*/
static int /* bool (FALSE/TRUE), as returned by PL_get_...() etc */
set_flag(term_t arg, re_options_flags *options_flags, uint32_t mask, uint32_t value, int invert)
{ if ( options_flags->seen&mask )
return TRUE; /* repeated option - ignore */
options_flags->seen |= mask;
options_flags->flags &= ~mask; /* clear the bits where the value goes */
switch( effective_bool(arg) )
{ case TRUE:
if ( invert )
options_flags->flags &= ~value;
else
options_flags->flags |= value;
break;
case FALSE:
if ( invert )
options_flags->flags |= value;
else
options_flags->flags &= ~value;
break;
default: /* -1 - Error: neither FALSE nor TRUE */
return FALSE;
}
return TRUE;
}
typedef struct re_optdef
{ const char *name;
uint32_t flag; /* Flag in pcre2.h */
uint32_t mode; /* RE_COMP_xxx, RE_MATCH_xxx, RE_NEG, etc. */
atom_t atom; /* Initially 0; filled in as-needed by lookup_optdef() */
} re_optdef;
#define RE_NEG 0x0001
#define RE_COMP_BOOL 0x0010
#define RE_COMP_BSR 0x0020 /* re_data.compile_options - \R */
#define RE_COMP_NEWLINE 0x0040 /* re_data.compile_options - newilne */
#define RE_COMP_OPTIMISE 0x0080 /* TODO: see also RE_COMPJIT_BOOL */
#define RE_COMP_COMPAT 0x0100 /* backward compatibility */
#define RE_COMP_CAPTURE 0x0200
#define RE_COMPCTX_BOOL 0x0400 /* re_data.compile_ctx_flags */
#define RE_COMPJIT_BOOL 0x0800 /* for pcre2_jit_compile() */
#define RE_MATCH_BOOL 0x1000 /* re_data.match_options */
#define RE_MATCH_START 0x2000
#define RE_SUBS_BOOL 0x4000 /* for pcre2_substitute() */
static const re_optdef*
lookup_optdef(re_optdef opt_defs[], atom_t name)
{ re_optdef *def;
for(def=opt_defs; def->name; def++)
{ if ( !def->atom ) /* lazily fill in atoms in lookup table */
def->atom = PL_new_atom(def->name);
if ( def->atom == name )
return def;
}
return NULL;
}
static int/* bool (FALSE/TRUE), as returned by PL_..._error() */
lookup_and_apply_optdef(re_optdef opt_defs[], atom_t name,
term_t option_term, term_t arg, uint32_t mask,
re_options_flags *options_flags)
{ const re_optdef *def = lookup_optdef(opt_defs, name);
if ( def )
return set_flag(arg, options_flags, mask, def->flag, def->mode&RE_NEG);
return PL_type_error("option", option_term);
}
static int/* bool (FALSE/TRUE), as returned by PL_..._error() */
lookup_and_apply_optdef_arg(re_optdef opt_defs[], term_t name,
term_t option_term, term_t arg, uint32_t mask,
re_options_flags *options_flags)
{ atom_t aval;
return PL_get_atom_ex(name, &aval) &&
lookup_and_apply_optdef(opt_defs, aval, option_term, arg, mask, options_flags);
}
static re_optdef re_optdefs[] =
{ /* Those with flag==0 are for options with names or numbers: */
{ "bsr", 0, RE_COMP_BSR },
{ "bsr2", 0, RE_COMP_BSR },
{ "capture_type", 0, RE_COMP_CAPTURE },
{ "compat", 0, RE_COMP_COMPAT },
{ "newline", 0, RE_COMP_NEWLINE },
{ "newline2", 0, RE_COMP_NEWLINE },
{ "optimise", 0, RE_COMP_OPTIMISE },
{ "optimize", 0, RE_COMP_OPTIMISE },
{ "start", 0, RE_MATCH_START },
/* The following are in the same order as in pcre2.h, to make it easy to compare them */
{ "anchored", PCRE2_ANCHORED, RE_COMP_BOOL|RE_MATCH_BOOL },
{ "utf_check", PCRE2_NO_UTF_CHECK, RE_COMP_BOOL|RE_MATCH_BOOL },
{ "endanchored", PCRE2_ENDANCHORED, RE_COMP_BOOL|RE_MATCH_BOOL },
{ "allow_empty_class", PCRE2_ALLOW_EMPTY_CLASS, RE_COMP_BOOL },
{ "alt_bsux", PCRE2_ALT_BSUX, RE_COMP_BOOL },
{ "auto_callout", PCRE2_AUTO_CALLOUT, RE_COMP_BOOL },
{ "caseless", PCRE2_CASELESS, RE_COMP_BOOL },
{ "dollar_endonly", PCRE2_DOLLAR_ENDONLY, RE_COMP_BOOL },
{ "dotall", PCRE2_DOTALL, RE_COMP_BOOL },
{ "dupnames", PCRE2_DUPNAMES, RE_COMP_BOOL },
{ "extended", PCRE2_EXTENDED, RE_COMP_BOOL },
{ "firstline", PCRE2_FIRSTLINE, RE_COMP_BOOL },
{ "match_unset_backref", PCRE2_MATCH_UNSET_BACKREF, RE_COMP_BOOL },
{ "multiline", PCRE2_MULTILINE, RE_COMP_BOOL },
{ "never_ucp", PCRE2_NEVER_UCP, RE_COMP_BOOL },
{ "never_utf", PCRE2_NEVER_UTF, RE_COMP_BOOL },
{ "auto_capture", PCRE2_NO_AUTO_CAPTURE, RE_COMP_BOOL|RE_NEG },
{ "no_auto_capture", PCRE2_NO_AUTO_CAPTURE, RE_COMP_BOOL }, /* backwards compatibility */
{ "auto_possess", PCRE2_NO_AUTO_POSSESS, RE_COMP_BOOL|RE_NEG },
{ "dotstar_anchor", PCRE2_NO_DOTSTAR_ANCHOR, RE_COMP_BOOL|RE_NEG },
{ "start_optimize", PCRE2_NO_START_OPTIMIZE, RE_COMP_BOOL|RE_NEG },
{ "ucp", PCRE2_UCP, RE_COMP_BOOL },
{ "greedy", PCRE2_UNGREEDY, RE_COMP_BOOL|RE_NEG },
{ "ungreedy", PCRE2_UNGREEDY, RE_COMP_BOOL }, /* backwards compatibility */
{ "utf", PCRE2_UTF, RE_COMP_BOOL },
{ "never_backslash_c", PCRE2_NEVER_BACKSLASH_C, RE_COMP_BOOL },
{ "alt_circumflex", PCRE2_ALT_CIRCUMFLEX, RE_COMP_BOOL },
{ "alt_verbnames", PCRE2_ALT_VERBNAMES, RE_COMP_BOOL },
{ "use_offset_limit", PCRE2_USE_OFFSET_LIMIT, RE_COMP_BOOL },
{ "extended_more", PCRE2_EXTENDED_MORE, RE_COMP_BOOL },
{ "literal", PCRE2_LITERAL, RE_COMP_BOOL },
#ifdef PCRE2_MATCH_INVALID_UTF
{ "match_invalid_utf", PCRE2_MATCH_INVALID_UTF, RE_COMP_BOOL },
#endif
{ "extra_allow_surrogate_escapes", PCRE2_EXTRA_ALLOW_SURROGATE_ESCAPES, RE_COMPCTX_BOOL },
{ "extra_bad_escape_is_literal", PCRE2_EXTRA_BAD_ESCAPE_IS_LITERAL, RE_COMPCTX_BOOL },
{ "extra_match_word", PCRE2_EXTRA_MATCH_WORD, RE_COMPCTX_BOOL },
{ "extra_match_line", PCRE2_EXTRA_MATCH_LINE, RE_COMPCTX_BOOL },
#ifdef PCRE2_EXTRA_ESCAPED_CR_IS_LF
{ "extra_escaped_cr_is_lf", PCRE2_EXTRA_ESCAPED_CR_IS_LF, RE_COMPCTX_BOOL },
#endif
#ifdef PCRE2_EXTRA_ALT_BSUX
{ "extra_alt_bsux", PCRE2_EXTRA_ALT_BSUX, RE_COMPCTX_BOOL },
#endif
{ "jit_complete", PCRE2_JIT_COMPLETE, RE_COMPJIT_BOOL },
{ "jit_partial_soft", PCRE2_JIT_PARTIAL_SOFT, RE_COMPJIT_BOOL },
{ "jit_partial_hard", PCRE2_JIT_PARTIAL_HARD, RE_COMPJIT_BOOL },
#ifdef PCRE2_JIT_INVALID_UTF
{ "jit_invalid_utf", PCRE2_JIT_INVALID_UTF , RE_COMPJIT_BOOL },
#endif
/* Some of the follwoing are for pcre2_dfa_match() or
pcre2_substitute(), but they're all in the same flag */
{ "bol", PCRE2_NOTBOL, RE_MATCH_BOOL|RE_NEG },
{ "eol", PCRE2_NOTEOL, RE_MATCH_BOOL|RE_NEG },
{ "empty", PCRE2_NOTEMPTY, RE_MATCH_BOOL|RE_NEG },
{ "empty_atstart", PCRE2_NOTEMPTY_ATSTART, RE_MATCH_BOOL|RE_NEG },
{ "partial_soft", PCRE2_PARTIAL_SOFT, RE_MATCH_BOOL },
{ "partial_hard", PCRE2_PARTIAL_HARD, RE_MATCH_BOOL },
{ "dfa_restart", PCRE2_DFA_RESTART, RE_MATCH_BOOL },
{ "dfa_shortest", PCRE2_DFA_SHORTEST, RE_MATCH_BOOL },
{ "substitute_global", PCRE2_SUBSTITUTE_GLOBAL, RE_MATCH_BOOL },
{ "substitute_extended", PCRE2_SUBSTITUTE_EXTENDED, RE_MATCH_BOOL },
{ "substitute_unset_empty", PCRE2_SUBSTITUTE_UNSET_EMPTY, RE_MATCH_BOOL },
{ "substitute_unknown_unset", PCRE2_SUBSTITUTE_UNKNOWN_UNSET, RE_MATCH_BOOL },
{ "substitute_overflow_length", PCRE2_SUBSTITUTE_OVERFLOW_LENGTH, RE_MATCH_BOOL },
{ "jit", PCRE2_NO_JIT, RE_MATCH_BOOL|RE_NEG }, /* TODO: see comment in pcre2.h */
#ifdef PCRE2_COPY_MATCHED_SUBJECT
{ "copy_matched_subject", PCRE2_COPY_MATCHED_SUBJECT, RE_MATCH_BOOL },
#endif
/* TODO: PCRE2_CONVERT_xxx if we support (experimental) pcre2_pattern_convert() */
{ NULL }
};
static re_optdef re_optbsrs[] =
{ { "anycrlf", PCRE2_BSR_ANYCRLF, RE_COMP_BSR },
{ "unicode", PCRE2_BSR_UNICODE, RE_COMP_BSR },
{ NULL }
};
#define OPTBSR_MASK (PCRE2_BSR_ANYCRLF|PCRE2_BSR_UNICODE)
static re_optdef re_optnewlines[] =
/* These are in the same order as in pcre2.h, to make it easy to compare them */
{ { "cr", PCRE2_NEWLINE_CR, RE_COMP_NEWLINE },
{ "lf", PCRE2_NEWLINE_LF, RE_COMP_NEWLINE },
{ "crlf", PCRE2_NEWLINE_CRLF, RE_COMP_NEWLINE },
{ "any", PCRE2_NEWLINE_ANY, RE_COMP_NEWLINE },
{ "anycrlf", PCRE2_NEWLINE_ANYCRLF, RE_COMP_NEWLINE },
{ "nul", PCRE2_NEWLINE_NUL, RE_COMP_NEWLINE },
{ NULL }
};
#define OPTNEWLINE_MASK (PCRE2_NEWLINE_CR|PCRE2_NEWLINE_LF|PCRE2_NEWLINE_CRLF|PCRE2_NEWLINE_ANY|PCRE2_NEWLINE_ANYCRLF)
/* This had "javascript" for PCRE1; the option doesn't exist in PCRE2,
so this empty array causes an error for compat(javascript). */
static re_optdef re_optcompats[] =
{ { NULL }
};
#define OPTCOMPAT_MASK 0
static re_optdef re_optcaptures[] =
{ { "atom", CAP_ATOM, RE_COMP_CAPTURE },
{ "float", CAP_FLOAT, RE_COMP_CAPTURE },
{ "integer", CAP_INTEGER, RE_COMP_CAPTURE },
{ "number", CAP_NUMBER, RE_COMP_CAPTURE },
{ "range", CAP_RANGE, RE_COMP_CAPTURE },
{ "string", CAP_STRING, RE_COMP_CAPTURE },
{ "term", CAP_TERM, RE_COMP_CAPTURE },
{ NULL }
};
#define OPTCAPTURE_MASK (CAP_ATOM|CAP_FLOAT|CAP_INTEGER|CAP_NUMBER|CAP_RANGE|CAP_STRING|CAP_TERM|CAP_DEFAULT)
static int /* bool (FALSE/TRUE), as returned by PL_..._error() */
get_arg_1_if_any(term_t head, atom_t *name, size_t *arity, term_t *arg)
{ if ( PL_get_name_arity(head, name, arity) && *arity <= 1 )
{ term_t argt = PL_new_term_ref();
if ( *arity == 1 )
{ _PL_get_arg(1, head, argt);
*arg = argt;
} else
{ *arg = 0;
}
return TRUE;
}
*arg = 0; /* Make compiler's flow analysis happy */
return PL_type_error("option", head);
}
static int /* bol (FALSE/TRUE), as returned by PL_...error() */
ensure_compile_context(pcre2_compile_context **compile_ctx)
{ if ( !*compile_ctx )
*compile_ctx = pcre2_compile_context_create(NULL);
if ( !*compile_ctx )
return PL_resource_error("memory");
return TRUE;
}
/* re_get_options assumes that re has been initialized by init_re_data() */
/* This is called in two ways:
- re_compile: a new re_data (from init_re_data())
- re_matchsub/re_foldl: a re_data that's already been set up by
re_compile - it's kept as part of the blob - but with initialized
match options (from get_re_copy()). If the same options list is
used by both compile and match, the compile-only options will
already have "seen" flags, so they'll be skipped; only the match
options will be processed. Any new compile options will be
processed but there's nothing in the matching code that uses
them, nor any check that they're ignored.
*/
static int /* bool (FALSE/TRUE), as returned by PL_..._error() */
re_get_options(term_t options, re_data *re)
{ term_t tail = PL_copy_term_ref(options);
term_t head = PL_new_term_ref();
while(PL_get_list_ex(tail, head, tail))
{ atom_t name;
size_t arity;
term_t arg;
if ( !get_arg_1_if_any(head, &name, &arity, &arg) )
return FALSE;
{ const re_optdef *def = lookup_optdef(re_optdefs, name);
/* Some options can appear in multiple situations, so we can't
do "else if"s. */
if ( def )
{ if ( def->mode&RE_COMP_BOOL )
{ if ( !set_flag(arg, &re->compile_options_flags, def->flag, def->flag, def->mode&RE_NEG) )
return FALSE;
}
if ( def->mode&RE_MATCH_BOOL )
{ if ( !set_flag(arg, &re->match_options_flags, def->flag, def->flag, def->mode&RE_NEG) )
return FALSE;
}
if ( def->mode&RE_COMP_BSR )
{ if ( !lookup_and_apply_optdef_arg(re_optbsrs, arg,
head, 0, OPTBSR_MASK, &re->compile_bsr_flags) )
return FALSE;
}
if ( def->mode&RE_COMP_NEWLINE )
{ if ( !lookup_and_apply_optdef_arg(re_optnewlines, arg,
head, 0, OPTNEWLINE_MASK, &re->compile_newline_flags) )
return FALSE;
}
if ( def->mode&RE_COMP_COMPAT )
{ if ( !lookup_and_apply_optdef_arg(re_optcompats, arg,
head, 0, OPTCOMPAT_MASK, &re->compile_options_flags) )
return FALSE;
}
if ( def->mode&RE_COMP_CAPTURE )
{ if ( !lookup_and_apply_optdef_arg(re_optcaptures, arg,
head, 0, OPTCAPTURE_MASK, &re->capture_type) )
return FALSE;
}
if ( def->mode&RE_COMP_OPTIMISE )
{ if ( !set_flag(arg, &re->optimise_flags, RE_OPTIMISE, RE_OPTIMISE, def->mode&RE_NEG) )
return FALSE;
}
if ( def->mode&RE_COMPCTX_BOOL )
{ if ( !set_flag(arg, &re->compile_ctx_flags, def->flag, def->flag, def->mode&RE_NEG) )
return FALSE;
}
if ( def->mode&RE_COMPJIT_BOOL )
{ if ( !set_flag(arg, &re->jit_options_flags, def->flag, def->flag, def->mode&RE_NEG) )
return FALSE;
}
if ( def->mode&RE_MATCH_START ) /* TODO: generalize set_flag() and remove duplicate code */
{ uint64_t start_value;
if ( !PL_get_uint64_ex(arg, &start_value) )
return FALSE;
if ( start_value > (uint64_t)OPTSTART_MASK )
return PL_domain_error("int32", arg);
/* TODO: Use set_flags() style of checking "seen" etc */
/* TODO: allow 64-bit sizes */
if ( !(re->start_flags.seen) )
{ re->start_flags.seen = 1;
re->start_flags.flags = (uint32_t)start_value;
}
}
} /* else: ignore unknown option */
}
}
return PL_get_nil_ex(tail);
}
/*******************************
* PREDICATES *
*******************************/
typedef enum re_config_type
{ CFG_INTEGER,
CFG_INTEGER_BKWD, /* Backwards compatibility with PCRE1 */
CFG_BOOL,
CFG_BOOL_BKWD, /* Backwards compatibility with PCRE1 */
CFG_STRINGBUF,
CFG_STRINGBUF_OPT, /* STRINGBUF that might return PCRE2_ERROR_BADOPTION */
CFG_BSR,
CFG_NEWLINE,
CFG_TRUE_BKWD, /* Backwards compatibility with PCRE1 */
CFG_FALSE_BKWD, /* Backwards compatibility with PCRE1 */
CFG_INVALID
} re_config_type;
typedef struct re_config_opt
{ const char *name;
int id;
re_config_type type;
atom_t atom; /* Initially 0; filled in as-needed by re_config_() */
functor_t functor; /* Initially 0; filled in as-needed by re_config_choice_() */
} re_config_opt;
/* Items with id == -1 are for backwards compatibility with PCRE1 */
/* "bsr" and "newline" have been removed because they return different things now */
static re_config_opt cfg_opts[] =
{ { "bsr2", PCRE2_CONFIG_BSR, CFG_BSR },
{ "compiled_widths", PCRE2_CONFIG_COMPILED_WIDTHS, CFG_INTEGER }, /* PCRE2 */
{ "depthlimit", PCRE2_CONFIG_DEPTHLIMIT, CFG_INTEGER }, /* PCRE2 */
{ "heaplimit", PCRE2_CONFIG_HEAPLIMIT, CFG_INTEGER }, /* PCRE2 */
{ "jit", PCRE2_CONFIG_JIT, CFG_BOOL }, /* PCRE2 */
{ "jittarget", PCRE2_CONFIG_JITTARGET, CFG_STRINGBUF_OPT }, /* PCRE2 */
{ "link_size", PCRE2_CONFIG_LINKSIZE, CFG_INTEGER_BKWD },
{ "linksize", PCRE2_CONFIG_LINKSIZE, CFG_INTEGER }, /* PCRE2 */ /* was LINK_SIZE */
{ "match_limit", PCRE2_CONFIG_MATCHLIMIT, CFG_INTEGER_BKWD },
{ "match_limit_recursion", -1, CFG_INVALID },
{ "matchlimit", PCRE2_CONFIG_MATCHLIMIT, CFG_INTEGER }, /* PCRE2 */
{ "never_backslash_c", PCRE2_CONFIG_NEVER_BACKSLASH_C, CFG_BOOL }, /* PCRE2 */
{ "newline2", PCRE2_CONFIG_NEWLINE, CFG_NEWLINE },
{ "parens_limit", PCRE2_CONFIG_PARENSLIMIT, CFG_INTEGER_BKWD },
{ "parenslimit", PCRE2_CONFIG_PARENSLIMIT, CFG_INTEGER }, /* PCRE2 */ /* was PARENS_LIIMT */
{ "posix_malloc_threshold", -1, CFG_INVALID },
{ "stackrecurse", PCRE2_CONFIG_STACKRECURSE, CFG_BOOL },
{ "unicode", PCRE2_CONFIG_UNICODE, CFG_BOOL }, /* PCRE2 */
{ "unicode_properties", -1, CFG_TRUE_BKWD },
{ "unicode_version", PCRE2_CONFIG_UNICODE_VERSION, CFG_STRINGBUF }, /* PCRE2 */
{ "utf8", PCRE2_CONFIG_UNICODE, CFG_BOOL_BKWD },
{ "version", PCRE2_CONFIG_VERSION, CFG_STRINGBUF }, /* PCRE2 */
/* PCRE2_CONFIG_RECURSIONLIMIT Obsolete synonym */
{ NULL }
};
static intptr_t
next_config(intptr_t i)
{ re_config_opt *def = &cfg_opts[i];
for( ; def->name; def++ )
{ switch(def->type)
{ case CFG_INVALID:
case CFG_BOOL_BKWD:
case CFG_INTEGER_BKWD:
case CFG_TRUE_BKWD:
case CFG_FALSE_BKWD:
break;
default:
if ( !def->atom ) /* lazily fill in atoms in lookup table ... */
def->atom = PL_new_atom(def->name);
if ( !def->functor ) /* ... and functors */
def->functor = PL_new_functor(def->atom, 1);
return def - cfg_opts;
}
}
return -1;
}
/** re_config_choice(--Choice) is nondet.
Unify Choice with an option (a term with the argument
uninstantiated) for re_config/1. Backtracks through all
possibilities. Used by re_config/1 when its argument is
uninstantiated. It's intended that re_config_choice/1 is called
with Choice uninstantiated, to allow backtracking through all the
possibilities (see re_config/1, which calls re_config_().
The context "handle" is the index into cfg_opts - initially 0, and
incremented by 1 for each retry. next_config() is used to get the
next entry, skipping those that are now invalid or are for
backwards compatibility.
*/
static foreign_t
re_config_choice_(term_t choice, control_t handle)
{ intptr_t index;
/* This code could be made slightly more efficient by passing around
the next value of index -- this would avoid a final choicepoint.
*/
switch( PL_foreign_control(handle) )
{ case PL_FIRST_CALL:
index = 0;
break;
case PL_REDO:
index = PL_foreign_context(handle);
break;
case PL_PRUNED:
PL_succeed;
default:
assert(0);
PL_fail;
}
if ( !PL_is_variable(choice) )
return PL_uninstantiation_error(choice);
index = next_config(index);
if ( index >= 0 &&
PL_unify_functor(choice, cfg_opts[index].functor) )
PL_retry(index + 1);
else
PL_fail;
}
#ifdef FORCE_NO_JIT
static int
pcre2_config_(uint32_t flag, void *result)
{
if ( !result )
return pcre2_config(flag, result);
switch( flag )
{ case PCRE2_CONFIG_JIT:
*(uint32_t *)result = 0;
return 0;
case PCRE2_CONFIG_JITTARGET:
return PCRE2_ERROR_BADOPTION;
default:
return pcre2_config(flag, result);
}
}
#else
#define pcre2_config_(flag, result) pcre2_config(flag, result)
#endif
/** re_config(+Term)
For documentation of this function, see pcre.pl
*/
static foreign_t
re_config_(term_t opt)
{ atom_t name;
size_t arity;
if ( PL_get_name_arity(opt, &name, &arity) )
{ if ( arity == 1 )
{ re_config_opt *o;