/
create_property_class.js
executable file
·168 lines (155 loc) · 4.54 KB
/
create_property_class.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
#!/usr/bin/nodejs
// Copyright (C) 2012-2016 Jacob R. Lifshay
// This file is part of Voxels.
//
// Voxels is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// Voxels is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Voxels; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
// MA 02110-1301, USA.
//
//
var fs = require("fs");
if(process.argv.length != 3 || process.argv[2] == '' || process.argv[2].substr(0, 1) == '-')
{
console.log("usage: ./create_property_function.js <unicode property>");
console.log("generates a C++ function that recognizes the Unicode property <unicode property>.");
process.exit(1);
}
var generateProperty = process.argv[2];
function writeProgress(progress)
{
if(progress == null)
console.log("done. ");
else
process.stdout.write("loading... " + (' ' + Math.floor(progress).toString()).substr(-2) + "%\r");
}
function loadUnicodeData()
{
var unicodeData = fs.readFileSync("UCD/PropList.txt", {encoding:"utf-8"}).split('\n');
var characterProperty = [];
console.assert(unicodeData.length > 0);
if(unicodeData[unicodeData.length - 1] === '')
{
unicodeData.pop();
}
for(var i = 0; i < unicodeData.length; i++)
{
var line = unicodeData[i].split('#')[0].trim();
if(line === '')
continue;
line = line.split(';').map(function(v){return v.trim();});
var property = line[1];
console.assert(line.length == 2);
if(property === generateProperty)
{
var rangeMatch = line[0].match(/^([a-fA-F0-9]+)\.\.([a-fA-F0-9]+)$/);
if(rangeMatch)
{
var start = +('0x' + rangeMatch[1]);
var end = +('0x' + rangeMatch[2]);
for(var j = start; j <= end; j++)
{
characterProperty[j] = property;
}
}
else
{
characterProperty[+('0x' + line[0])] = property;
}
}
}
return characterProperty;
}
var characterProperty = loadUnicodeData();
function Match(start, end)
{
this.start = start >>> 0;
this.end = end >>> 0;
}
Match.escapeChar = function(v)
{
v >>>= 0;
if(v < 0x7F && v >= 0x20)
{
var char = String.fromCharCode(v);
if('\\[]^:-'.indexOf(char) === -1)
return char;
}
if(v <= 0x7F)
{
return '\\x' + ('00' + v.toString(16).toUpperCase()).slice(-2);
}
if(v <= 0xFFFF)
{
return '\\u' + ('0000' + v.toString(16).toUpperCase()).slice(-4);
}
return '\\U' + ('00000000' + v.toString(16).toUpperCase()).slice(-8);
}
Match.prototype =
{
isSingle: function()
{
return this.start == this.end;
},
includes: function(codePoint)
{
codePoint >>>= 0;
return codePoint >= this.start && codePoint <= this.end;
},
toString: function()
{
if(this.start == this.end)
{
return Match.escapeChar(this.start);
}
return Match.escapeChar(this.start) + '-' + Match.escapeChar(this.end);
},
};
var matches = [];
for(var codePoint = 0, lastMatched = false; codePoint < characterProperty.length; codePoint++)
{
if(characterProperty[codePoint] === generateProperty)
{
if(!lastMatched)
{
matches.push(new Match(codePoint, codePoint));
}
else
{
matches[matches.length - 1].end = codePoint;
}
lastMatched = true;
}
else
{
lastMatched = false;
}
}
var line = '';
var code = 'UnicodeProperty' + generateProperty + ':char ';
var indent = '';
while(indent.length < code.length)
indent += ' ';
code += '= [';
var variableNumber = 0;
for(var i = 0; i < matches.length; i++)
{
if(line.length > 100)
{
code += line + "]:char" + variableNumber + " {$$ = char" + variableNumber++ + ";}\n" + indent + "/ [";
line = "";
}
line += matches[i];
}
code += line + "]:char" + variableNumber + " {$$ = char" + variableNumber + ";};";
console.log(code);