Debian Codegraph @

vendor/onigmo-source/sample/listcap.c

raw
1 /*
2 * listcap.c
3 *
4 * capture history (?@...) sample.
5 */
6 #include <stdio.h>
7 #include <string.h>
8 #include "onigmo.h"
9
10 static int
11 node_callback(int group, OnigPosition beg, OnigPosition end, int level,
12 int at, void* arg)
13 {
14 int i;
15
16 if (at != ONIG_TRAVERSE_CALLBACK_AT_FIRST)
17 return -1; /* error */
18
19 /* indent */
20 for (i = 0; i < level * 2; i++)
21 fputc(' ', stderr);
22
23 fprintf(stderr, "%d: (%ld-%ld)\n", group, beg, end);
24 return 0;
25 }
26
27 static int
28 node_callback_last(int group, OnigPosition beg, OnigPosition end, int level,
29 int at, void* arg)
30 {
31 int i;
32
33 if (at != ONIG_TRAVERSE_CALLBACK_AT_LAST)
34 return -1; /* error */
35
36 /* indent */
37 for (i = 0; i < level * 2; i++)
38 fputc(' ', stderr);
39
40 fprintf(stderr, "%d: (%ld-%ld)\n", group, beg, end);
41 return 0;
42 }
43
44 extern int ex(unsigned char* str, unsigned char* pattern,
45 OnigSyntaxType* syntax)
46 {
47 int r;
48 unsigned char *start, *range, *end;
49 regex_t* reg;
50 OnigErrorInfo einfo;
51 OnigRegion *region;
52
53 r = onig_new(&reg, pattern, pattern + strlen((char* )pattern),
54 ONIG_OPTION_DEFAULT, ONIG_ENCODING_ASCII, syntax, &einfo);
55 if (r != ONIG_NORMAL) {
56 OnigUChar s[ONIG_MAX_ERROR_MESSAGE_LEN];
57 onig_error_code_to_str(s, r, &einfo);
58 fprintf(stderr, "ERROR: %s\n", s);
59 return -1;
60 }
61
62 fprintf(stderr, "number of captures: %d\n", onig_number_of_captures(reg));
63 fprintf(stderr, "number of capture histories: %d\n",
64 onig_number_of_capture_histories(reg));
65
66 region = onig_region_new();
67
68 end = str + strlen((char* )str);
69 start = str;
70 range = end;
71 r = onig_search(reg, str, end, start, range, region, ONIG_OPTION_NONE);
72 if (r >= 0) {
73 int i;
74
75 fprintf(stderr, "match at %d\n", r);
76 for (i = 0; i < region->num_regs; i++) {
77 fprintf(stderr, "%d: (%ld-%ld)\n", i, region->beg[i], region->end[i]);
78 }
79 fprintf(stderr, "\n");
80
81 r = onig_capture_tree_traverse(region, ONIG_TRAVERSE_CALLBACK_AT_FIRST,
82 node_callback, (void* )0);
83 fprintf(stderr, "\n");
84
85 r = onig_capture_tree_traverse(region, ONIG_TRAVERSE_CALLBACK_AT_LAST,
86 node_callback_last, (void* )0);
87 fprintf(stderr, "\n");
88 }
89 else if (r == ONIG_MISMATCH) {
90 fprintf(stderr, "search fail\n");
91 }
92 else { /* error */
93 OnigUChar s[ONIG_MAX_ERROR_MESSAGE_LEN];
94 onig_error_code_to_str(s, r);
95 return -1;
96 }
97
98 onig_region_free(region, 1 /* 1:free self, 0:free contents only */);
99 onig_free(reg);
100 return 0;
101 }
102
103
104 extern int main(int argc, char* argv[])
105 {
106 int r = 0;
107 OnigSyntaxType syn;
108
109 static UChar* str1 = (UChar* )"((())())";
110 static UChar* pattern1
111 = (UChar* )"\\g<p>(?@<p>\\(\\g<s>\\)){0}(?@<s>(?:\\g<p>)*|){0}";
112
113 static UChar* str2 = (UChar* )"x00x00x00";
114 static UChar* pattern2 = (UChar* )"(?@x(?@\\d+))+";
115
116 static UChar* str3 = (UChar* )"0123";
117 static UChar* pattern3 = (UChar* )"(?@.)(?@.)(?@.)(?@.)";
118
119 static UChar* str4 = (UChar* )"((abc)(abc))";
120 static UChar* pattern4 = (UChar* )"\\((?@(?:[^(]|\\g<0>)*)\\)";
121
122 /* enable capture hostory */
123 onig_copy_syntax(&syn, ONIG_SYNTAX_DEFAULT);
124 onig_set_syntax_op2(&syn,
125 onig_get_syntax_op2(&syn) | ONIG_SYN_OP2_ATMARK_CAPTURE_HISTORY);
126
127 r |= ex(str1, pattern1, &syn);
128 r |= ex(str2, pattern2, &syn);
129 r |= ex(str3, pattern3, &syn);
130 r |= ex(str4, pattern4, &syn);
131
132 onig_end();
133 return r;
134 }