-
Notifications
You must be signed in to change notification settings - Fork 130
/
unicode-props.js
211 lines (169 loc) · 6.76 KB
/
unicode-props.js
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
const xregexp = require('xregexp');
const names = require('./unicode-data/names.js');
const core = require('./core.js');
//TODO - the regexes should be tweaked to match full graphemes instead
const graphemeBreaker = require('grapheme-breaker');
function mangled(name) {
return name.toLowerCase(name).replace(/_/g, '');
}
const propIdToNames = {};
for (const propName in names.props) {
const propId = names.props[propName];
if (!propIdToNames[propId]) propIdToNames[propId] = propIdToNames[propId] || [];
propIdToNames[propId].push(propName);
}
function addProp(propName, builder) {
const propId = names.props[propName];
const match = builder(true);
const do_not_match = builder(false);
for (const alias of propIdToNames[propId]) {
exports['uniprop_' + mangled(alias)] = match;
exports['uniprop_not_' + mangled(alias)] = do_not_match;
}
}
function addExtraProp(propName, builder) {
if (!exports['uniprop_' + mangled(propName)]) {
exports['uniprop_' + mangled(propName)] = builder(true);
exports['uniprop_not_' + mangled(propName)] = builder(false);
}
}
function matchClass(shouldMatch, category, negated) {
let regexp = xregexp('\\' + (shouldMatch ? 'p' : 'P') + '{' + category + '}', 'Ay');
return function(target, pos) {
regexp.lastIndex = pos;
if (regexp.test(target)) {
return graphemeBreaker.nextBreak(target, pos) - pos;
} else {
return -1;
}
};
}
for (const prop of ['Lowercase', 'Uppercase', 'White_Space', 'Alphabetic']) {
addProp(prop, match => matchClass(match, prop));
}
addExtraProp('ascii', match => matchClass(match, 'ASCII'));
addExtraProp('any', match => matchClass(match, 'Any'));
function addPropValueProps(propName, builder, filter, extraMangle) {
for (const propValue of names.propValues[names.props[propName]]) {
if (!filter(propValue[0])) continue;
for (const alias of propValue) {
const mangledAlias = mangled(extraMangle ? extraMangle(alias) : alias);
exports['uniprop_' + mangledAlias] = builder(true, propValue[1]);
exports['uniprop_not_' + mangledAlias] = builder(false, propValue[1]);
}
}
}
addPropValueProps('Block',
(match, long) => matchClass(match, 'In' + long),
short => short !== 'NB',
alias => 'In' + alias
);
addPropValueProps('Script',
(match, long) => matchClass(match, long),
short => short !== 'Hrkt' && short !== 'Zzzz');
addPropValueProps('General_Category',
(match, long) => matchClass(match, long.replace(/_/g, '')),
short => short !== 'LC');
function categoriesToRegex(categories) {
return categories ? categories.map(
category => names.regexes[names.props[category]] || '\\p{' + category + '}'
).join('|') : '';
}
function matchDerived(shouldMatch, match, avoid) {
const regexp = maybeNegated(shouldMatch, categoriesToRegex(match), categoriesToRegex(avoid));
return function(target, pos) {
regexp.lastIndex = pos;
if (regexp.test(target)) {
return graphemeBreaker.nextBreak(target, pos) - pos;
} else {
return -1;
}
};
}
const matchLC = match => matchDerived(match, ['Ll', 'Lu', 'Lt']);
addExtraProp('LC', matchLC);
addExtraProp('Cased_Letter', matchLC);
const UnicodeTrie = require('unicode-trie')
const fs = require('fs')
const nativeArgs = require('./native-args.js');
const NativeStrArg = nativeArgs.NativeStrArg;
const numericTypeData = new UnicodeTrie(fs.readFileSync(__dirname + '/unicode-data/NumericType.trie'));
const bidiClassData = new UnicodeTrie(fs.readFileSync(__dirname + '/unicode-data/BidiClass.trie'));
const numericValueData = new UnicodeTrie(fs.readFileSync(__dirname + '/unicode-data/NumericValue.trie'));
function delegateAccepts(shouldMatch, ctx, cursor, obj, code, value) {
const result = cursor['!DELEGATE_ACCEPTS'](ctx, null, cursor, obj, new NativeStrArg(value)).$$getInt();
if (result === (shouldMatch ? 0 : 1)) {
return -1;
} else {
let isPair = 0; // TODO codes that take two bytes
return isPair ? 2 : 1;
}
}
function propWithArgs(shouldMatch, trie, propName, longNames) {
const propId = names.props[propName];
return function(ctx, cursor, target, offset, obj) {
const code = target.codePointAt(offset);
if (code === undefined) return -1;
const propValueId = trie.get(code);
let valueName = names.propValues[propId][propValueId-1][longNames ? 1 : 0];
return delegateAccepts(shouldMatch, ctx, cursor, obj, code, valueName);
};
};
function addPropWithArgs(alias, trie, propName, longNames) {
exports['uniprop_' + alias] = propWithArgs(true, trie, propName, longNames);
exports['uniprop_not_' + alias] = propWithArgs(false, trie, propName, longNames);
}
function matchName(shouldMatch) {
return function(ctx, cursor, target, offset, obj) {
const code = target.codePointAt(offset);
if (code === undefined) return -1;
const name = core.op.getuniname(code);
return delegateAccepts(shouldMatch, ctx, cursor, obj, code, name);
};
}
exports.uniprop_name = matchName(true);
exports.uniprop_not_name = matchName(false);
addPropWithArgs('numerictype', numericTypeData, 'nt', true)
addPropWithArgs('nt', numericTypeData, 'nt', false)
addPropWithArgs('bc', bidiClassData, 'bc', false)
addPropWithArgs('numericvalue', numericValueData, 'nv', false)
function maybeNegated(shouldMatch, main, exclude) {
let regex;
if (exclude !== undefined && exclude !== '') {
regex = shouldMatch
? '(?!' + exclude + ')' + main
: '(?:(?!' + main + ')\\p{Any})|' + exclude;
} else {
regex = shouldMatch ? main : '(?!' + main + ')\\p{Any}';
}
return xregexp(regex, 'yA');
}
function matchRegex(shouldMatch, regexString) {
const regexp = maybeNegated(shouldMatch, regexString);
return function(target, pos) {
regexp.lastIndex = pos;
if (regexp.test(target)) {
return graphemeBreaker.nextBreak(target, pos) - pos;
} else {
return -1;
}
};
}
for (const propId in names.regexes) {
const match = matchRegex(true, names.regexes[propId]);
const negatedMatch = matchRegex(false, names.regexes[propId]);
for (const propName of propIdToNames[propId]) {
exports['uniprop_' + mangled(propName)] = match;
exports['uniprop_not_' + mangled(propName)] = negatedMatch;
}
}
const mathRegex = names.regexes[names.props.Other_Math] + '|\\p{Sm}';
addProp('Math', match => matchRegex(match, mathRegex));
addExtraProp('Assigned', match => matchRegex(true, match ? '\\P{Cn}' : '\\p{Cn}'));
addExtraProp('Unassigned', match => matchRegex(true, match ? '\\p{Cn}' : '\\P{Cn}'));
addProp('ID_Start', match => matchDerived(match,
['Lu', 'Ll', 'Lt', 'Lm', 'Lo', 'Nl', 'Other_ID_Start'],
['Pattern_Syntax', 'Pattern_White_Space']));
addProp('ID_Continue', match => matchDerived(match,
['Lu', 'Ll', 'Lt', 'Lm', 'Lo', 'Nl', 'Mn', 'Mc', 'Nd', 'Pc', 'Other_ID_Continue', 'Other_ID_Start'],
['Pattern_Syntax', 'Pattern_White_Space']));