2020-12-29 09:01:31 +01:00
|
|
|
#!/usr/bin/env python3
|
2015-06-09 16:15:02 +02:00
|
|
|
|
|
|
|
def to_enum_hd(k, prefix):
|
2018-10-17 04:21:06 +02:00
|
|
|
res = prefix
|
2015-06-09 16:15:02 +02:00
|
|
|
for c in k.upper():
|
|
|
|
if c == ':' or c == '-':
|
|
|
|
res += '_'
|
|
|
|
continue
|
|
|
|
res += c
|
|
|
|
return res
|
|
|
|
|
|
|
|
def build_header(headers):
|
|
|
|
res = {}
|
|
|
|
for k in headers:
|
|
|
|
size = len(k)
|
|
|
|
if size not in res:
|
|
|
|
res[size] = {}
|
|
|
|
ent = res[size]
|
|
|
|
c = k[-1]
|
|
|
|
if c not in ent:
|
|
|
|
ent[c] = []
|
|
|
|
ent[c].append(k)
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
def gen_enum(tokens, prefix):
|
2020-12-29 09:01:31 +01:00
|
|
|
print('''\
|
|
|
|
enum {''')
|
2015-06-09 16:15:02 +02:00
|
|
|
for k in sorted(tokens):
|
2020-12-29 09:01:31 +01:00
|
|
|
print('''\
|
|
|
|
{},'''.format(to_enum_hd(k, prefix)))
|
|
|
|
print('''\
|
2018-10-17 04:21:06 +02:00
|
|
|
{}MAXIDX,
|
2020-12-29 09:01:31 +01:00
|
|
|
}};'''.format(prefix))
|
2015-06-09 16:15:02 +02:00
|
|
|
|
2015-07-14 16:36:44 +02:00
|
|
|
def gen_index_header(tokens, prefix, value_type, comp_fun, return_type, fail_value):
|
2020-12-29 09:01:31 +01:00
|
|
|
print('''\
|
2015-07-14 16:36:44 +02:00
|
|
|
{} lookup_token(const {} *name, size_t namelen) {{
|
2020-12-29 09:01:31 +01:00
|
|
|
switch (namelen) {{'''.format(return_type, value_type))
|
2015-06-09 16:15:02 +02:00
|
|
|
b = build_header(tokens)
|
|
|
|
for size in sorted(b.keys()):
|
|
|
|
ents = b[size]
|
2020-12-29 09:01:31 +01:00
|
|
|
print('''\
|
|
|
|
case {}:'''.format(size))
|
|
|
|
print('''\
|
|
|
|
switch (name[{}]) {{'''.format(size - 1))
|
2015-06-09 16:15:02 +02:00
|
|
|
for c in sorted(ents.keys()):
|
|
|
|
headers = sorted(ents[c])
|
2020-12-29 09:01:31 +01:00
|
|
|
print('''\
|
|
|
|
case '{}':'''.format(c))
|
2015-06-09 16:15:02 +02:00
|
|
|
for k in headers:
|
2020-12-29 09:01:31 +01:00
|
|
|
print('''\
|
2015-07-14 16:21:38 +02:00
|
|
|
if ({}("{}", name, {})) {{
|
2015-06-09 16:15:02 +02:00
|
|
|
return {};
|
2020-12-29 09:01:31 +01:00
|
|
|
}}'''.format(comp_fun, k[:-1], size - 1, to_enum_hd(k, prefix)))
|
|
|
|
print('''\
|
|
|
|
break;''')
|
|
|
|
print('''\
|
2015-06-09 16:15:02 +02:00
|
|
|
}
|
2020-12-29 09:01:31 +01:00
|
|
|
break;''')
|
|
|
|
print('''\
|
2015-07-14 16:36:44 +02:00
|
|
|
}}
|
|
|
|
return {};
|
2020-12-29 09:01:31 +01:00
|
|
|
}}'''.format(fail_value))
|
2015-06-09 16:15:02 +02:00
|
|
|
|
2015-07-14 16:36:44 +02:00
|
|
|
def gentokenlookup(tokens, prefix, value_type='uint8_t', comp_fun='util::streq_l', return_type='int', fail_value='-1'):
|
2015-06-09 16:15:02 +02:00
|
|
|
gen_enum(tokens, prefix)
|
2020-12-29 09:01:31 +01:00
|
|
|
print()
|
2015-07-14 16:36:44 +02:00
|
|
|
gen_index_header(tokens, prefix, value_type, comp_fun, return_type, fail_value)
|