2024-08-11 15:06:02 +00:00
|
|
|
# Script to generate abbreviations for tokens. Parses all lexers
|
|
|
|
# and styles files to find all token names and generate a unique
|
|
|
|
# abbreviation for each one. The abbreviations are generated by
|
|
|
|
# taking the uppercase letters of the token name and converting
|
|
|
|
# them to lowercase. If the abbreviation is not unique, the script
|
|
|
|
# will print a warning and exit.
|
|
|
|
|
2024-08-06 23:13:23 +00:00
|
|
|
import sys
|
|
|
|
import string
|
2024-08-09 17:17:24 +00:00
|
|
|
import glob
|
2024-08-06 23:13:23 +00:00
|
|
|
|
2024-08-09 17:17:24 +00:00
|
|
|
tokens = {"Highlight"}
|
|
|
|
abbrevs = {"Highlight": "hl"}
|
2024-08-06 23:13:23 +00:00
|
|
|
|
|
|
|
def abbr(line):
|
|
|
|
return "".join(c for c in line if c in string.ascii_uppercase).lower()
|
|
|
|
|
2024-08-09 17:17:24 +00:00
|
|
|
def check_abbrevs():
|
|
|
|
if len(abbrevs) != len(tokens):
|
|
|
|
print("Warning: Abbreviations are not unique")
|
|
|
|
print(len(abbrevs), len(tokens))
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
# Processes all files in lexers looking for token names
|
|
|
|
for fname in glob.glob("lexers/*.xml"):
|
|
|
|
with open(fname) as f:
|
|
|
|
for line in f:
|
|
|
|
if "<token" not in line:
|
|
|
|
continue
|
|
|
|
line = line.strip()
|
|
|
|
line = line.split('<token ',1)[-1]
|
|
|
|
line = line.split('"')[1]
|
|
|
|
abbrevs[line] = abbr(line)
|
|
|
|
tokens.add(line)
|
|
|
|
check_abbrevs()
|
|
|
|
|
|
|
|
# Processes all files in styles looking for token names too
|
|
|
|
for fname in glob.glob("styles/*.xml"):
|
|
|
|
with open(fname) as f:
|
|
|
|
for line in f:
|
|
|
|
if "<entry" not in line:
|
|
|
|
continue
|
|
|
|
line = line.strip()
|
|
|
|
line = line.split('type=',1)[-1]
|
|
|
|
line = line.split('"')[1]
|
|
|
|
abbrevs[line] = abbr(line)
|
|
|
|
tokens.add(line)
|
|
|
|
check_abbrevs()
|
2024-08-06 23:13:23 +00:00
|
|
|
|
2024-08-11 15:06:02 +00:00
|
|
|
with open ("src/constants/token_abbrevs.cr", "w") as outf:
|
|
|
|
outf.write("module Tartrazine\n")
|
|
|
|
outf.write(" Abbreviations = {\n")
|
|
|
|
for k in sorted(abbrevs.keys()):
|
|
|
|
outf.write(f' "{k}" => "{abbrevs[k]}",\n')
|
2024-08-26 23:18:28 +00:00
|
|
|
outf.write(" }\nend\n")
|