Commit fa55c6c0
fa55c6c0dd16Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 5 files · +2 added · ~3 modified
Description
Merge pull request #292 from ryanseys/rs-node-back-ref
feat(parse,codegen,runtime): support BackReferenceReadNode ($&, $~, $', $`)
Details
- Commit
- fa55c6c0dd16
- Parent
- 2f8bb45747b7
- Merge parent
- f33acd4f8b22
lib/sp_runtime.hmodifiedView file
@@ -230,7 +230,9 @@
230230
#define SP_GC_MARK_STACK_MAX (1024*64)231231
static void**sp_gc_mark_stack=NULL;static int sp_gc_mark_top=0;232232
static void sp_gc_mark(void*obj){if(!obj)return;unsigned char pm=((unsigned char*)obj)[-1];if(pm==0xfe){((char*)obj)[-1]=(char)0xfc;return;}if(pm==0xfc||pm==0xff)return;sp_gc_hdr*h=(sp_gc_hdr*)((char*)obj-sizeof(sp_gc_hdr));if(h->marked)return;h->marked=1;if(h->scan){if(sp_gc_mark_stack&&sp_gc_mark_top<SP_GC_MARK_STACK_MAX){sp_gc_mark_stack[sp_gc_mark_top++]=obj;}else{h->scan(obj);}}}233-
static void sp_gc_mark_all(void){if(!sp_gc_mark_stack)sp_gc_mark_stack=(void**)malloc(sizeof(void*)*SP_GC_MARK_STACK_MAX);sp_gc_mark_top=0;for(int i=0;i<sp_gc_nroots;i++){void*obj=*sp_gc_roots[i];if(obj)sp_gc_mark(obj);}while(sp_gc_mark_top>0){void*obj=sp_gc_mark_stack[--sp_gc_mark_top];sp_gc_hdr*h=(sp_gc_hdr*)((char*)obj-sizeof(sp_gc_hdr));if(h->scan)h->scan(obj);}}233+
/* Forward decl: defined alongside the regex globals it marks. */234+
static void sp_re_mark_globals(void);235+
static void sp_gc_mark_all(void){if(!sp_gc_mark_stack)sp_gc_mark_stack=(void**)malloc(sizeof(void*)*SP_GC_MARK_STACK_MAX);sp_gc_mark_top=0;for(int i=0;i<sp_gc_nroots;i++){void*obj=*sp_gc_roots[i];if(obj)sp_gc_mark(obj);}sp_re_mark_globals();while(sp_gc_mark_top>0){void*obj=sp_gc_mark_stack[--sp_gc_mark_top];sp_gc_hdr*h=(sp_gc_hdr*)((char*)obj-sizeof(sp_gc_hdr));if(h->scan)h->scan(obj);}}234236
static void sp_gc_cleanup(int*p){sp_gc_nroots=*p;}235237
#define SP_GC_NBUCKETS 32236238
static sp_gc_hdr*sp_gc_buckets[SP_GC_NBUCKETS];@@ -582,6 +584,30 @@
582584
static int sp_re_caps[64];583585
static const char *sp_re_last_str = "";584586
587+
/* Symbolic back-references populated alongside the numbered captures.588+
Read by codegen's BackReferenceReadNode arm:589+
$& -> sp_re_match_str (the whole matched substring)590+
$` -> sp_re_match_pre (substring before the match)591+
$' -> sp_re_match_post (substring after the match)592+
$~ falls back to $& since Spinel has no MatchData wrapper. */593+
static const char *sp_re_match_str = NULL;594+
static const char *sp_re_match_pre = NULL;595+
static const char *sp_re_match_post = NULL;596+
597+
/* Mark the regex globals as live during GC. Each holds a pointer to a598+
string allocated via sp_str_alloc_raw on the str-heap; without this599+
sp_str_sweep would reap them on the next collect, leaving dangling600+
pointers in $1..$9, $&, $`, $'. sp_mark_string is null-safe and601+
no-ops on non-heap strings (the empty-string default of602+
sp_re_last_str), so it's safe to call unconditionally. */603+
static void sp_re_mark_globals(void) {604+
sp_mark_string(sp_re_last_str);605+
for (int i = 0; i < 10; i++) sp_mark_string(sp_re_captures[i]);606+
sp_mark_string(sp_re_match_str);607+
sp_mark_string(sp_re_match_pre);608+
sp_mark_string(sp_re_match_post);609+
}610+
585611
static void sp_re_set_captures(const char *str, int *caps, int ncaps) {586612
sp_re_last_str = str;587613
for (int i = 0; i < 10; i++) sp_re_captures[i] = NULL;@@ -593,6 +619,26 @@
593619
sp_re_captures[i] = buf;594620
}595621
}622+
/* Populate the symbolic back-references from caps[0]/[1] (the whole623+
match span). NULL when the match failed; the codegen ternary624+
falls back to "". */625+
sp_re_match_str = NULL;626+
sp_re_match_pre = NULL;627+
sp_re_match_post = NULL;628+
if (ncaps >= 1 && caps[0] >= 0 && caps[1] >= 0) {629+
int slen = (int)strlen(str);630+
int mlen = caps[1] - caps[0];631+
char *m = sp_str_alloc_raw(mlen + 1);632+
memcpy(m, str + caps[0], mlen); m[mlen] = 0;633+
sp_re_match_str = m;634+
char *pre = sp_str_alloc_raw(caps[0] + 1);635+
memcpy(pre, str, caps[0]); pre[caps[0]] = 0;636+
sp_re_match_pre = pre;637+
int post_len = slen - caps[1];638+
char *post = sp_str_alloc_raw(post_len + 1);639+
memcpy(post, str + caps[1], post_len); post[post_len] = 0;640+
sp_re_match_post = post;641+
}596642
}597643
598644
static mrb_int sp_re_match(mrb_regexp_pattern *pat, const char *str) {spinel_codegen.rbmodifiedView file
Loading diff…
spinel_parse.cmodifiedView file
Loading diff…
test/back_ref.rbaddedView file
Loading diff…
test/back_ref.rb.expectedaddedView file
Loading diff…