Fun 0.41.5
The programming language that makes you have fun!
Loading...
Searching...
No Matches
match.c
Go to the documentation of this file.
1/*
2 * This file is part of the Fun programming language.
3 * https://fun-lang.xyz/
4 *
5 * Copyright 2025 Johannes Findeisen <you@hanez.org>
6 * Licensed under the terms of the Apache-2.0 license.
7 * https://opensource.org/license/apache-2-0
8 */
9
10/**
11 * @file match.c
12 * @brief Implements the OP_PCRE2_MATCH opcode (conditional build).
13 *
14 * Executes a single PCRE2 pattern match against a subject string and returns
15 * a map describing the first match and its capture groups when FUN_WITH_PCRE2
16 * is enabled. Returns Nil when there is no match or on error. When PCRE2
17 * support is disabled at build time, the opcode always returns Nil.
18 */
19
20/**
21 * OP_PCRE2_MATCH: (pattern:any, text:any, flags:int|bool=0) -> map|Nil
22 *
23 * Behavior when FUN_WITH_PCRE2 is enabled:
24 * - Pops: pattern, text, flags. Converts pattern/text to strings.
25 * - Flags bits:
26 * - 1 = PCRE2_CASELESS (I)
27 * - 2 = PCRE2_MULTILINE (M)
28 * - 4 = PCRE2_DOTALL (S)
29 * - 8 = PCRE2_UTF (U)
30 * - 16 = PCRE2_EXTENDED (X)
31 * - On successful match, returns a map with keys:
32 * - "full": matched substring (group 0)
33 * - "start": start index (int)
34 * - "end": end index (int, exclusive)
35 * - "groups": array of captured group strings (excluding group 0)
36 * - On no match or compile error, returns Nil.
37 *
38 * Behavior when FUN_WITH_PCRE2 is disabled:
39 * - Pops three values and returns Nil.
40 */
41
42/* PCRE2_MATCH */
44#ifdef FUN_WITH_PCRE2
45 Value vflags = pop_value(vm);
48 int flags = 0;
49 if (vflags.type == VAL_INT || vflags.type == VAL_BOOL) flags = (int)vflags.i;
52 free_value(vflags);
55 if (!pattern || !subject) {
56 if (pattern) free(pattern);
57 if (subject) free(subject);
58 push_value(vm, make_nil());
59 break;
60 }
61#ifndef PCRE2_CODE_UNIT_WIDTH
62#define PCRE2_CODE_UNIT_WIDTH 8
63#endif
64#include <pcre2.h>
66 PCRE2_SIZE erroff;
67 uint32_t opt = 0;
68 if (flags & 1) opt |= PCRE2_CASELESS; /* I */
69 if (flags & 2) opt |= PCRE2_MULTILINE; /* M */
70 if (flags & 4) opt |= PCRE2_DOTALL; /* S */
71 if (flags & 8) opt |= PCRE2_UTF; /* U */
72 if (flags & 16) opt |= PCRE2_EXTENDED; /* X */
73 pcre2_code *re = pcre2_compile((PCRE2_SPTR)pattern, PCRE2_ZERO_TERMINATED, opt, &errorcode, &erroff, NULL);
74 if (!re) {
77 push_value(vm, make_nil());
78 break;
79 }
80 pcre2_match_data *mdata = pcre2_match_data_create_from_pattern(re, NULL);
81 int rc = pcre2_match(re, (PCRE2_SPTR)subject, (PCRE2_SIZE)strlen(subject), 0, 0, mdata, NULL);
82 if (rc <= 0) {
87 push_value(vm, make_nil());
88 break;
89 }
90 PCRE2_SIZE *ov = pcre2_get_ovector_pointer(mdata);
91 /* Build result map */
93 int start0 = (int)ov[0];
94 int end0 = (int)ov[1];
96 (void)map_set(&res, "full", make_string(full ? full : ""));
97 if (full) free(full);
98 (void)map_set(&res, "start", make_int(start0));
99 (void)map_set(&res, "end", make_int(end0));
100 /* groups array (excluding group 0) */
101 Value groups = make_array_from_values(NULL, 0);
102 for (int i = 1; i < rc; ++i) {
103 int s = (int)ov[2 * i];
104 int e = (int)ov[2 * i + 1];
105 char *gstr = (s >= 0 && e >= s) ? string_substr(subject, s, e - s) : NULL;
106 Value gv = make_string(gstr ? gstr : "");
107 if (gstr) free(gstr);
108 (void)array_push(&groups, gv);
109 }
110 (void)map_set(&res, "groups", groups);
116#else
117 Value a = pop_value(vm);
118 free_value(a);
119 Value b = pop_value(vm);
120 free_value(b);
121 Value c = pop_value(vm);
122 free_value(c);
123 push_value(vm, make_nil());
124#endif
125 break;
126}
Value a
Definition add.c:37
uint32_t b
Definition band.c:32
@ OP_PCRE2_MATCH
Definition bytecode.h:193
pcsc_ctx_entry * e
Definition connect.c:38
CURLcode rc
Definition download.c:71
uint32_t opt
Definition findall.c:69
Value vtext
Definition findall.c:48
pcre2_code * re
Definition findall.c:75
Value vpat
Definition findall.c:49
pcre2_match_data * mdata
Definition findall.c:82
int errorcode
Definition findall.c:67
PCRE2_SIZE erroff
Definition findall.c:68
char * subject
Definition findall.c:53
char * pattern
Definition findall.c:52
const char * res
Definition get_string.c:38
Value c
Definition load_const.c:31
int map_set(Value *vm, const char *key, Value v)
Insert or replace a key in the map.
Definition map.c:79
Value make_map_empty(void)
Construct a new empty map Value.
Definition map.c:35
pcre2_match_data_free(mdata)
int start0
Definition match.c:93
free_value(vflags)
char * full
Definition match.c:95
PCRE2_SIZE * ov
Definition match.c:90
free(pattern)
pcre2_code_free(re)
push_value(vm, res)
int end0
Definition match.c:94
uint32_t s
Definition rol.c:31
char * string_substr(const char *s, int start, int len)
Create a newly allocated substring of s.
Definition str_utils.c:35
int flags
Definition stringify.c:38
Tagged union representing a Fun value.
Definition value.h:68
int64_t i
Definition value.h:71
ValueType type
Definition value.h:69
Value make_nil(void)
Construct a nil Value.
Definition value.c:126
Value make_string(const char *s)
Construct a string Value by duplicating the given C string.
Definition value.c:95
char * value_to_string_alloc(const Value *v)
Allocate a printable C string for a Value.
Definition value.c:641
int array_push(Value *v, Value newElem)
Append a Value to an array.
Definition value.c:257
Value make_int(int64_t v)
Construct a Value representing a 64-bit integer.
Definition value.c:51
Value make_array_from_values(const Value *vals, int count)
Create an array Value by copying items from an input span.
Definition value.c:142
@ VAL_BOOL
Definition value.h:52
@ VAL_INT
Definition value.h:51
static Value pop_value(VM *vm)
Pop a Value from the VM operand stack.
Definition vm.c:580