forked from k-takata/Onigmo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testconv.rb
executable file
·246 lines (212 loc) · 5.34 KB
/
testconv.rb
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
#!/usr/local/bin/ruby -Ke
# testconv.rb
# Copyright (C) 2003-2006 K.Kosako (sndgk393 AT ybb DOT ne DOT jp)
WINDOWS = (ARGV.size > 0 && /^-win/i =~ ARGV[0])
ARGV.shift if WINDOWS
if WINDOWS
REGENC = 'ONIG_ENCODING_SJIS'
else
REGENC = 'ONIG_ENCODING_EUC_JP'
end
def conv_reg(s)
s.gsub!(/\\/, '\\\\\\\\') #'
s.gsub!(/\?\?/, '?\\\\?') # escape ANSI trigraph (??)
s
end
def conv_str(s)
if (s[0] == ?')
s = s[1..-2]
return s.gsub(/\\/, '\\\\\\\\') #'
else
return s[1..-2]
end
end
print(<<"EOS")
/*
* This program was generated by testconv.rb.
*/
#include "config.h"
#ifdef ONIG_ESCAPE_UCHAR_COLLISION
#undef ONIG_ESCAPE_UCHAR_COLLISION
#endif
#include <stdio.h>
#ifdef POSIX_TEST
#include "onigposix.h"
#else
#include "oniguruma.h"
#endif
#ifdef HAVE_STRING_H
# include <string.h>
#else
# include <strings.h>
#endif
#define SLEN(s) strlen(s)
static int nsucc = 0;
static int nfail = 0;
static int nerror = 0;
static FILE* err_file;
#ifndef POSIX_TEST
static OnigRegion* region;
#endif
static void xx(char* pattern, char* str, int from, int to, int mem, int not)
{
int r;
#ifdef POSIX_TEST
regex_t reg;
char buf[200];
regmatch_t pmatch[25];
r = regcomp(®, pattern, REG_EXTENDED | REG_NEWLINE);
if (r) {
regerror(r, ®, buf, sizeof(buf));
fprintf(err_file, "ERROR: %s\\n", buf);
nerror++;
return ;
}
r = regexec(®, str, reg.re_nsub + 1, pmatch, 0);
if (r != 0 && r != REG_NOMATCH) {
regerror(r, ®, buf, sizeof(buf));
fprintf(err_file, "ERROR: %s\\n", buf);
nerror++;
return ;
}
if (r == REG_NOMATCH) {
if (not) {
fprintf(stdout, "OK(N): /%s/ '%s'\\n", pattern, str);
nsucc++;
}
else {
fprintf(stdout, "FAIL: /%s/ '%s'\\n", pattern, str);
nfail++;
}
}
else {
if (not) {
fprintf(stdout, "FAIL(N): /%s/ '%s'\\n", pattern, str);
nfail++;
}
else {
if (pmatch[mem].rm_so == from && pmatch[mem].rm_eo == to) {
fprintf(stdout, "OK: /%s/ '%s'\\n", pattern, str);
nsucc++;
}
else {
fprintf(stdout, "FAIL: /%s/ '%s' %d-%d : %d-%d\\n", pattern, str,
from, to, pmatch[mem].rm_so, pmatch[mem].rm_eo);
nfail++;
}
}
}
regfree(®);
#else
regex_t* reg;
OnigErrorInfo einfo;
OnigSyntaxType syn = *ONIG_SYNTAX_DEFAULT;
/* ONIG_OPTION_OFF(syn.options, ONIG_OPTION_ASCII_RANGE); */
r = onig_new(®, (UChar* )pattern, (UChar* )(pattern + SLEN(pattern)),
ONIG_OPTION_DEFAULT, #{REGENC}, &syn, &einfo);
if (r) {
char s[ONIG_MAX_ERROR_MESSAGE_LEN];
onig_error_code_to_str((UChar* )s, r, &einfo);
fprintf(err_file, "ERROR: %s\\n", s);
nerror++;
return ;
}
r = onig_search(reg, (UChar* )str, (UChar* )(str + SLEN(str)),
(UChar* )str, (UChar* )(str + SLEN(str)),
region, ONIG_OPTION_NONE);
if (r < ONIG_MISMATCH) {
char s[ONIG_MAX_ERROR_MESSAGE_LEN];
onig_error_code_to_str((UChar* )s, r);
fprintf(err_file, "ERROR: %s\\n", s);
nerror++;
return ;
}
if (r == ONIG_MISMATCH) {
if (not) {
fprintf(stdout, "OK(N): /%s/ '%s'\\n", pattern, str);
nsucc++;
}
else {
fprintf(stdout, "FAIL: /%s/ '%s'\\n", pattern, str);
nfail++;
}
}
else {
if (not) {
fprintf(stdout, "FAIL(N): /%s/ '%s'\\n", pattern, str);
nfail++;
}
else {
if (region->beg[mem] == from && region->end[mem] == to) {
fprintf(stdout, "OK: /%s/ '%s'\\n", pattern, str);
nsucc++;
}
else {
fprintf(stdout, "FAIL: /%s/ '%s' %d-%d : %d-%d\\n", pattern, str,
from, to, region->beg[mem], region->end[mem]);
nfail++;
}
}
}
onig_free(reg);
#endif
}
static void x2(char* pattern, char* str, int from, int to)
{
xx(pattern, str, from, to, 0, 0);
}
static void x3(char* pattern, char* str, int from, int to, int mem)
{
xx(pattern, str, from, to, mem, 0);
}
static void n(char* pattern, char* str)
{
xx(pattern, str, 0, 0, 0, 1);
}
extern int main(int argc, char* argv[])
{
err_file = stdout;
#ifdef POSIX_TEST
reg_set_encoding(#{REGENC.sub(/\AONIG_/, 'REG_POSIX_')});
#else
region = onig_region_new();
#endif
EOS
PAT = '\\/([^\\\\\\/]*(?:\\\\.[^\\\\\\/]*)*)\\/'
CM = '\s*,\s*'
RX2 = %r{\Ax\(#{PAT}#{CM}('[^']*'|"[^"]*")#{CM}(\S+)#{CM}(\S+)\)}
RI2 = %r{\Ai\(#{PAT}#{CM}('[^']*'|"[^"]*")#{CM}(\S+)#{CM}(\S+)\)}
RX3 = %r{\Ax\(#{PAT}#{CM}('[^']*'|"[^"]*")#{CM}(\S+)#{CM}(\S+)#{CM}(\S+)\)}
RN = %r{\An\(#{PAT}#{CM}('[^']*'|"[^"]*")\)} #'
while line = gets()
if (m = RX2.match(line))
reg = conv_reg(m[1])
str = conv_str(m[2])
printf(" x2(\"%s\", \"%s\", %s, %s);\n", reg, str, m[3], m[4])
elsif (m = RI2.match(line))
reg = conv_reg(m[1])
str = conv_str(m[2])
printf(" x2(\"%s\", \"%s\", %s, %s);\n", reg, str, m[3], m[4])
elsif (m = RX3.match(line))
reg = conv_reg(m[1])
str = conv_str(m[2])
printf(" x3(\"%s\", \"%s\", %s, %s, %s);\n", reg, str, m[3], m[4], m[5])
elsif (m = RN.match(line))
reg = conv_reg(m[1])
str = conv_str(m[2])
printf(" n(\"%s\", \"%s\");\n", reg, str)
else
end
end
print(<<'EOS')
fprintf(stdout,
"\nRESULT SUCC: %d, FAIL: %d, ERROR: %d (by Onigmo %s)\n",
nsucc, nfail, nerror, onig_version());
#ifndef POSIX_TEST
onig_region_free(region, 1);
onig_end();
#endif
return ((nfail == 0 && nerror == 0) ? 0 : -1);
}
EOS
# END OF SCRIPT