-
Notifications
You must be signed in to change notification settings - Fork 1
/
keys.gen
executable file
·57 lines (54 loc) · 1.21 KB
/
keys.gen
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
#!/bin/sh -ue
source=${1?Missing source file}
export LC_ALL=C
echo "/* Automatically generated by $0. Do not edit. */"
echo
echo '#pragma GCC diagnostic push'
echo '#pragma GCC diagnostic ignored "-Wmissing-field-initializers"'
echo '#pragma GCC diagnostic ignored "-Wunused-parameter"'
sed -n '/K_[a-zA-Z_.-]*_parse/{s/.*\(K_[a-zA-Z_.-]*\)_parse.*/\1/p}' "$source" |
sort |
uniq |
while read -r name
do
table=$(
sed -n '/_\(parse\|none\)/! {
/'$name'_[a-zA-Z_.-]*/ {
s:.*\('$name'_\([a-zA-Z_.-]*\)\).*:\2,\t\1:
: subst_dashes
s:^\([^,]*\)__:\1-:
t subst_dashes
p
}
}' "$source" |
sort |
uniq
)
gperf <<EOF
%language=ANSI-C
%7bit
%enum
%readonly-tables
%define hash-function-name ${name}_hash_
%define lookup-function-name ${name}_lookup_
%{
enum ${name}_e_ {
${name}_none
$(printf '%s' "$table" | sed 's/.*\t\(.*\)/\t, \1/')
};
%}
%struct-type
struct ${name}_s_ { char const *const name; enum ${name}_e_ const idx; }
%%
$table
%%
static enum ${name}_e_
${name}_parse (register char const *str)
{
register struct ${name}_s_ const *const h = ${name}_lookup_ (str, strlen(str));
return NULL != h ? h->idx : ${name}_none;
}
EOF
done
echo '#pragma GCC diagnostic pop'
echo '/* vim'':set ft=c: */'