glibc/locale/gen-translit.py

178 lines
5.1 KiB
Python
Raw Normal View History

#!/usr/bin/python3
# Generate the locale/C-translit.h file.
# Copyright (C) 2018-2022 Free Software Foundation, Inc.
# This file is part of the GNU C Library.
#
# The GNU C Library 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.1 of the License, or (at your option) any later version.
#
# The GNU C Library 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 the GNU C Library; if not, see
Prefer https to http for gnu.org and fsf.org URLs Also, change sources.redhat.com to sourceware.org. This patch was automatically generated by running the following shell script, which uses GNU sed, and which avoids modifying files imported from upstream: sed -ri ' s,(http|ftp)(://(.*\.)?(gnu|fsf|sourceware)\.org($|[^.]|\.[^a-z])),https\2,g s,(http|ftp)(://(.*\.)?)sources\.redhat\.com($|[^.]|\.[^a-z]),https\2sourceware.org\4,g ' \ $(find $(git ls-files) -prune -type f \ ! -name '*.po' \ ! -name 'ChangeLog*' \ ! -path COPYING ! -path COPYING.LIB \ ! -path manual/fdl-1.3.texi ! -path manual/lgpl-2.1.texi \ ! -path manual/texinfo.tex ! -path scripts/config.guess \ ! -path scripts/config.sub ! -path scripts/install-sh \ ! -path scripts/mkinstalldirs ! -path scripts/move-if-change \ ! -path INSTALL ! -path locale/programs/charmap-kw.h \ ! -path po/libc.pot ! -path sysdeps/gnu/errlist.c \ ! '(' -name configure \ -execdir test -f configure.ac -o -f configure.in ';' ')' \ ! '(' -name preconfigure \ -execdir test -f preconfigure.ac ';' ')' \ -print) and then by running 'make dist-prepare' to regenerate files built from the altered files, and then executing the following to cleanup: chmod a+x sysdeps/unix/sysv/linux/riscv/configure # Omit irrelevant whitespace and comment-only changes, # perhaps from a slightly-different Autoconf version. git checkout -f \ sysdeps/csky/configure \ sysdeps/hppa/configure \ sysdeps/riscv/configure \ sysdeps/unix/sysv/linux/csky/configure # Omit changes that caused a pre-commit check to fail like this: # remote: *** error: sysdeps/powerpc/powerpc64/ppc-mcount.S: trailing lines git checkout -f \ sysdeps/powerpc/powerpc64/ppc-mcount.S \ sysdeps/unix/sysv/linux/s390/s390-64/syscall.S # Omit change that caused a pre-commit check to fail like this: # remote: *** error: sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S: last line does not end in newline git checkout -f sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S
2019-09-07 13:40:42 +08:00
# <https://www.gnu.org/licenses/>.
import re
import sys
class StringLiteral:
"Source of a string literal and its decomposition into code points."
def __init__(self, s):
# States:
# 0 regular character sequence
# 1 backslash seen
# 2 in hexadecimal escape sequence
state = 0
result = []
for ch in s:
if state == 0:
if ch == '\\':
state = 1
else:
result.append(ord(ch))
elif state == 1:
if ch in "\\\"":
result.append(ord(ch))
state = 0
elif ch == 'x':
state = 2
result.append(0)
else:
raise ValueError("invalid character {!r} in {!r}".format(
ch, s))
elif state == 2:
if ch in "0123456789abcdefABCDEF":
result[-1] = result[-1] * 16 + int(ch, 16)
else:
if ch == '\\':
state = 1
else:
state = 0
if state == 1:
raise ValueError("trailing backslash in {!r}".format(s))
self.source = s
self.decoded = tuple(result)
class Translit:
"Pair of transliteration and source."
__RE_TRANSLIT = re.compile(
r'^"((?:[^"\\]|\\x[0-9a-fA-F])+)"\s+'
r'"((?:[^"\\]|\\["\\])*)"\s*(?:#.*)?$')
def __init__(self, line):
match = self.__RE_TRANSLIT.match(line)
if not match:
raise IOError("invalid line {}: {!r}".format(
lineno + 1, line))
codepoints, replacement = match.groups()
self.codepoints = StringLiteral(codepoints)
self.replacement = StringLiteral(replacement)
# List of Translit objects.
translits = []
# Read transliterations from standard input.
for lineno, line in enumerate(sys.stdin):
line = line.strip()
# Skip empty lines and comments.
if (not line) or line[0] == '#':
continue
translit = Translit(line)
# Check ordering of codepoints.
if translits \
and translit.codepoints.decoded <= translits[-1].codepoints.decoded:
raise IOError("unexpected codepoint {!r} on line {}: {!r}".format(
2021-05-11 15:25:11 +08:00
translit.codepoints.decoded, lineno + 1, line))
translits.append(translit)
# Generate the C sources.
write = sys.stdout.write
write("#include <stdint.h>\n")
write("#define NTRANSLIT {}\n".format(len(translits)))
write("static const uint32_t translit_from_idx[] =\n{\n ")
col = 2
total = 0
for translit in translits:
if total > 0:
if col + 7 >= 79:
write(",\n ")
col = 2
else:
write(", ")
col += 2
write("{:4}".format(total))
total += len(translit.codepoints.decoded) + 1
col += 4
write("\n};\n")
write("static const wchar_t translit_from_tbl[] =\n ")
col = 1
first = True
for translit in translits:
if first:
first = False
else:
if col + 6 >= 79:
write("\n ")
col = 1
write(" L\"\\0\"")
col += 6
if col > 2 and col + len(translit.codepoints.source) + 4 >= 79:
write("\n ")
col = 2
else:
write(" ")
col += 1
write("L\"{}\"".format(translit.codepoints.source))
col += len(translit.codepoints.source) + 3
write(";\n")
write("static const uint32_t translit_to_idx[] =\n{\n ")
col = 2
total = 0
for translit in translits:
if total > 0:
if col + 7 >= 79:
write(",\n ")
col = 2
else:
write(", ")
col += 2
write("{:4}".format(total))
total += len(translit.replacement.decoded) + 2
col += 4
write("\n};\n")
write("static const wchar_t translit_to_tbl[] =\n ")
col = 1
first = True
for translit in translits:
if first:
first = False
else:
if col + 6 >= 79:
write("\n ")
col = 1
write(" L\"\\0\"")
col += 6
if col > 2 and col + len(translit.replacement.source) + 6 >= 79:
write("\n ")
col = 2
else:
write(" ")
col += 1
write("L\"{}\\0\"".format(translit.replacement.source))
col += len(translit.replacement.source) + 5
write(";\n")