mirror of
https://github.com/Julow/Unexpected-Keyboard.git
synced 2025-01-03 20:08:57 +01:00
compose: Parse json files and fix edge cases
There's no json file yet, this was part of an experiment. Add a missing escape rule and detect colliding sequences.
This commit is contained in:
parent
c2d5b14261
commit
940cf0deb8
@ -143,7 +143,7 @@ tasks.register('compileComposeSequences') {
|
|||||||
println "\nGenerating ${out}"
|
println "\nGenerating ${out}"
|
||||||
exec {
|
exec {
|
||||||
def sequences = new File(projectDir, "srcs/compose").listFiles().findAll {
|
def sequences = new File(projectDir, "srcs/compose").listFiles().findAll {
|
||||||
!it.name.endsWith(".py")
|
it.name.endsWith(".pre") || it.name.endsWith(".json")
|
||||||
}
|
}
|
||||||
workingDir = projectDir
|
workingDir = projectDir
|
||||||
commandLine("python", "srcs/compose/compile.py", *sequences)
|
commandLine("python", "srcs/compose/compile.py", *sequences)
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import textwrap, sys, re, string
|
import textwrap, sys, re, string, json
|
||||||
|
|
||||||
# Names not defined in Compose.pre
|
# Names not defined in Compose.pre
|
||||||
xkb_char_extra_names = {
|
xkb_char_extra_names = {
|
||||||
@ -89,25 +89,42 @@ def parse_sequences_file_xkb(fname):
|
|||||||
seqs.append(s)
|
seqs.append(s)
|
||||||
return seqs
|
return seqs
|
||||||
|
|
||||||
|
# Parse from a json file containing a dictionary sequence → result string.
|
||||||
|
def parse_sequences_file_json(fname):
|
||||||
|
with open(fname, "r") as inp:
|
||||||
|
seqs = json.load(inp)
|
||||||
|
return list(seqs.items())
|
||||||
|
|
||||||
# Format of the sequences file is determined by its extension
|
# Format of the sequences file is determined by its extension
|
||||||
def parse_sequences_file(fname):
|
def parse_sequences_file(fname):
|
||||||
if fname.endswith(".pre"):
|
if fname.endswith(".pre"):
|
||||||
return parse_sequences_file_xkb(fname)
|
return parse_sequences_file_xkb(fname)
|
||||||
|
if fname.endswith(".json"):
|
||||||
|
return parse_sequences_file_json(fname)
|
||||||
raise Exception(fname + ": Unsupported format")
|
raise Exception(fname + ": Unsupported format")
|
||||||
|
|
||||||
# Turn a list of sequences into a trie.
|
# Turn a list of sequences into a trie.
|
||||||
def add_sequences_to_trie(seqs, trie):
|
def add_sequences_to_trie(seqs, trie):
|
||||||
for seq, result in seqs:
|
def add_seq_to_trie(t_, seq, result):
|
||||||
t_ = trie
|
t_ = trie
|
||||||
i = 0
|
i = 0
|
||||||
while i < len(seq) - 1:
|
while i < len(seq) - 1:
|
||||||
c = seq[i]
|
c = seq[i]
|
||||||
if c not in t_:
|
if c not in t_:
|
||||||
t_[c] = {}
|
t_[c] = {}
|
||||||
|
if isinstance(t_[c], str):
|
||||||
|
global dropped_sequences
|
||||||
|
dropped_sequences += 1
|
||||||
|
print("Sequence collide: '%s = %s' '%s = %s'" % (
|
||||||
|
seq[:i+1], t_[c], seq, result),
|
||||||
|
file=sys.stderr)
|
||||||
|
return
|
||||||
t_ = t_[c]
|
t_ = t_[c]
|
||||||
i += 1
|
i += 1
|
||||||
c = seq[i]
|
c = seq[i]
|
||||||
t_[c] = result
|
t_[c] = result
|
||||||
|
for seq, result in seqs:
|
||||||
|
add_seq_to_trie(trie, seq, result)
|
||||||
|
|
||||||
# Compile the trie into a state machine.
|
# Compile the trie into a state machine.
|
||||||
def make_automata(tree_root):
|
def make_automata(tree_root):
|
||||||
@ -155,9 +172,11 @@ def gen_java(machine):
|
|||||||
"\"": "\\\"",
|
"\"": "\\\"",
|
||||||
"\\": "\\\\",
|
"\\": "\\\\",
|
||||||
"\n": "\\n",
|
"\n": "\\n",
|
||||||
|
"\r": "\\r",
|
||||||
ord("\""): "\\\"",
|
ord("\""): "\\\"",
|
||||||
ord("\\"): "\\\\",
|
ord("\\"): "\\\\",
|
||||||
ord("\n"): "\\n",
|
ord("\n"): "\\n",
|
||||||
|
ord("\r"): "\\r",
|
||||||
}
|
}
|
||||||
def char_repr(c):
|
def char_repr(c):
|
||||||
if c in chars_map:
|
if c in chars_map:
|
||||||
|
Loading…
Reference in New Issue
Block a user