| 1 |
#!/usr/bin/env python3 |
| 2 |
# |
| 3 |
# Portions of this script have been (shamelessly) stolen from the |
| 4 |
# prior work of Daniel Veillard (genUnicode.py) |
| 5 |
# |
| 6 |
# I, however, take full credit for any bugs, errors or difficulties :-) |
| 7 |
# |
| 8 |
# William Brack |
| 9 |
# October 2003 |
| 10 |
# |
| 11 |
# 18 October 2003 |
| 12 |
# Modified to maintain binary compatibility with previous library versions |
| 13 |
# by adding a suffix 'Q' ('quick') to the macro generated for the original, |
| 14 |
# function, and adding generation of a function (with the original name) which |
| 15 |
# instantiates the macro. |
| 16 |
# |
| 17 |
|
| 18 |
import sys |
| 19 |
import rangetab |
| 20 |
|
| 21 |
# |
| 22 |
# A routine to take a list of yes/no (1, 0) values and turn it |
| 23 |
# into a list of ranges. This will later be used to determine whether |
| 24 |
# to generate single-byte lookup tables, or inline comparisons |
| 25 |
# |
| 26 |
def makeRange(lst): |
| 27 |
ret = [] |
| 28 |
pos = 0 |
| 29 |
while pos < len(lst): |
| 30 |
try: # index generates exception if not present |
| 31 |
s = lst[pos:].index(1) # look for start of next range |
| 32 |
except: |
| 33 |
break # if no more, finished |
| 34 |
pos += s # pointer to start of possible range |
| 35 |
try: |
| 36 |
e = lst[pos:].index(0) # look for end of range |
| 37 |
e += pos |
| 38 |
except: # if no end, set to end of list |
| 39 |
e = len(lst) |
| 40 |
ret.append((pos, e-1)) # append range tuple to list |
| 41 |
pos = e + 1 # ready to check for next range |
| 42 |
return ret |
| 43 |
|
| 44 |
# minTableSize gives the minimum number of ranges which must be present |
| 45 |
# before a 256-byte lookup table is produced. If there are less than this |
| 46 |
# number, a macro with inline comparisons is generated |
| 47 |
minTableSize = 6 |
| 48 |
|
| 49 |
# dictionary of functions, key=name, element contains char-map and range-list |
| 50 |
Functs = {} |
| 51 |
|
| 52 |
state = 0 |
| 53 |
|
| 54 |
try: |
| 55 |
defines = open("codegen/ranges.def", "r") |
| 56 |
except: |
| 57 |
print("Missing codegen/ranges.def, aborting ...") |
| 58 |
sys.exit(1) |
| 59 |
|
| 60 |
# |
| 61 |
# The lines in the .def file have three types:- |
| 62 |
# name: Defines a new function block |
| 63 |
# ur: Defines individual or ranges of unicode values |
| 64 |
# end: Indicates the end of the function block |
| 65 |
# |
| 66 |
# These lines are processed below. |
| 67 |
# |
| 68 |
for line in defines.readlines(): |
| 69 |
# ignore blank lines, or lines beginning with '#' |
| 70 |
if line[0] == '#': |
| 71 |
continue |
| 72 |
line = line.strip() |
| 73 |
if line == '': |
| 74 |
continue |
| 75 |
# split line into space-separated fields, then split on type |
| 76 |
try: |
| 77 |
fields = line.split(' ') |
| 78 |
# |
| 79 |
# name line: |
| 80 |
# validate any previous function block already ended |
| 81 |
# validate this function not already defined |
| 82 |
# initialize an entry in the function dicitonary |
| 83 |
# including a mask table with no values yet defined |
| 84 |
# |
| 85 |
if fields[0] == 'name': |
| 86 |
name = fields[1] |
| 87 |
if state != 0: |
| 88 |
print("'name' %s found before previous name" \ |
| 89 |
"completed" % (fields[1])) |
| 90 |
continue |
| 91 |
state = 1 |
| 92 |
if name in Functs: |
| 93 |
print("name '%s' already present - may give" \ |
| 94 |
" wrong results" % (name)) |
| 95 |
else: |
| 96 |
# dict entry with two list elements (chdata, rangedata) |
| 97 |
Functs[name] = [ [], [] ] |
| 98 |
for v in range(256): |
| 99 |
Functs[name][0].append(0) |
| 100 |
# |
| 101 |
# end line: |
| 102 |
# validate there was a preceding function name line |
| 103 |
# set state to show no current function active |
| 104 |
# |
| 105 |
elif fields[0] == 'end': |
| 106 |
if state == 0: |
| 107 |
print("'end' found outside of function block") |
| 108 |
continue |
| 109 |
state = 0 |
| 110 |
|
| 111 |
# |
| 112 |
# ur line: |
| 113 |
# validate function has been defined |
| 114 |
# process remaining fields on the line, which may be either |
| 115 |
# individual unicode values or ranges of values |
| 116 |
# |
| 117 |
elif fields[0] == 'ur': |
| 118 |
if state != 1: |
| 119 |
raise Exception("'ur' found outside of 'name' block") |
| 120 |
for el in fields[1:]: |
| 121 |
pos = el.find('..') |
| 122 |
# pos <=0 means not a range, so must be individual value |
| 123 |
if pos <= 0: |
| 124 |
# cheap handling of hex or decimal values |
| 125 |
if el[0:2] == '0x': |
| 126 |
value = int(el[2:],16) |
| 127 |
elif el[0] == "'": |
| 128 |
value = ord(el[1]) |
| 129 |
else: |
| 130 |
value = int(el) |
| 131 |
if ((value < 0) | (value > 0x1fffff)): |
| 132 |
raise Exception('Illegal value (%s) in ch for'\ |
| 133 |
' name %s' % (el,name)) |
| 134 |
# for ur we have only ranges (makes things simpler), |
| 135 |
# so convert val to range |
| 136 |
currange = (value, value) |
| 137 |
# pos > 0 means this is a range, so isolate/validate |
| 138 |
# the interval |
| 139 |
else: |
| 140 |
# split the range into it's first-val, last-val |
| 141 |
(first, last) = el.split("..") |
| 142 |
# convert values from text into binary |
| 143 |
if first[0:2] == '0x': |
| 144 |
start = int(first[2:],16) |
| 145 |
elif first[0] == "'": |
| 146 |
start = ord(first[1]) |
| 147 |
else: |
| 148 |
start = int(first) |
| 149 |
if last[0:2] == '0x': |
| 150 |
end = int(last[2:],16) |
| 151 |
elif last[0] == "'": |
| 152 |
end = ord(last[1]) |
| 153 |
else: |
| 154 |
end = int(last) |
| 155 |
if (start < 0) | (end > 0x1fffff) | (start > end): |
| 156 |
raise Exception("Invalid range '%s'" % el) |
| 157 |
currange = (start, end) |
| 158 |
# common path - 'currange' has the range, now take care of it |
| 159 |
# We split on single-byte values vs. multibyte |
| 160 |
if currange[1] < 0x100: # single-byte |
| 161 |
for ch in range(currange[0],currange[1]+1): |
| 162 |
# validate that value not previously defined |
| 163 |
if Functs[name][0][ch]: |
| 164 |
msg = "Duplicate ch value '%s' for name '%s'" % (el, name) |
| 165 |
raise Exception(msg) |
| 166 |
Functs[name][0][ch] = 1 |
| 167 |
else: # multi-byte |
| 168 |
if currange in Functs[name][1]: |
| 169 |
raise Exception("range already defined in" \ |
| 170 |
" function") |
| 171 |
else: |
| 172 |
Functs[name][1].append(currange) |
| 173 |
|
| 174 |
except: |
| 175 |
print("Failed to process line: %s" % (line)) |
| 176 |
raise |
| 177 |
|
| 178 |
try: |
| 179 |
output = open("codegen/ranges.inc", "w") |
| 180 |
except: |
| 181 |
print("Failed to open codegen/ranges.inc") |
| 182 |
sys.exit(1) |
| 183 |
|
| 184 |
# |
| 185 |
# Now output the generated data. |
| 186 |
# |
| 187 |
|
| 188 |
fkeys = sorted(Functs.keys()) |
| 189 |
|
| 190 |
for f in fkeys: |
| 191 |
|
| 192 |
# First we convert the specified single-byte values into a group of ranges. |
| 193 |
if max(Functs[f][0]) > 0: # only check if at least one entry |
| 194 |
rangeTable = makeRange(Functs[f][0]) |
| 195 |
numRanges = len(rangeTable) |
| 196 |
if numRanges >= minTableSize: # table is worthwhile |
| 197 |
# write the constant data to the code file |
| 198 |
output.write("const unsigned char %s_tab[256] = {\n" % f) |
| 199 |
pline = " " |
| 200 |
for n in range(255): |
| 201 |
pline += " 0x%02x," % Functs[f][0][n] |
| 202 |
if len(pline) > 72: |
| 203 |
output.write(pline + "\n") |
| 204 |
pline = " " |
| 205 |
output.write(pline + " 0x%02x };\n\n" % Functs[f][0][255]) |
| 206 |
|
| 207 |
# |
| 208 |
# Next we do the unicode ranges |
| 209 |
# |
| 210 |
|
| 211 |
for f in fkeys: |
| 212 |
if len(Functs[f][1]) > 0: # only generate if unicode ranges present |
| 213 |
rangeTable = Functs[f][1] |
| 214 |
rangeTable.sort() # ascending tuple sequence |
| 215 |
group = rangetab.gen_range_tables(output, f, '_srng', '_lrng', |
| 216 |
rangeTable) |
| 217 |
|
| 218 |
output.write("const xmlChRangeGroup %sGroup =\n\t%s;\n\n" % |
| 219 |
(f, group)) |
| 220 |
|
| 221 |
output.close() |
| 222 |
|