def read_enum(filename, type_name, prefix, class_name, g=lambda m: m.group(1)): """Read an enumeration declaration from :param filename:.""" pat_decl = re.compile(r" type {0} is$".format(type_name)) pat_enum = re.compile(r" {0}(\w+),?( *-- .*)?$".format(prefix)) pat_comment = re.compile(r" *-- .*$") lr = pnodes.linereader(filename) while not pat_decl.match(lr.get()): pass line = lr.get() if line != " (\n": raise pnodes.ParseError( lr, "{}:{}: missing open parenthesis".format(filename, lr.lineno) ) toks = [] while True: line = lr.get() if line == " );\n": break m = pat_enum.match(line) if m: toks.append(g(m)) elif pat_comment.match(line): pass elif line == "\n": pass else: print(line, file=sys.stderr) raise pnodes.ParseError( lr, "{}:{}: incorrect line in enum {}".format( filename, lr.lineno, type_name ), ) print_enum(class_name, toks)
def do_libghdl_names(): pat_name_first = re.compile( r" Name_(\w+)\s+: constant Name_Id := (\d+);") pat_name_def = re.compile( r" Name_(\w+)\s+:\s+constant Name_Id :=\s+Name_(\w+)( \+ (\d+))?;") dict = {} lr = pnodes.linereader("../std_names.ads") while True: line = lr.get() m = pat_name_first.match(line) if m: name_def = m.group(1) val = int(m.group(2)) dict[name_def] = val res = [(name_def, val)] break val_max = 1 while True: line = lr.get() if line == "end Std_Names;\n": break if line.endswith(":=\n"): line = line.rstrip() + lr.get() m = pat_name_def.match(line) if m: name_def = m.group(1) name_ref = m.group(2) val = m.group(4) if not val: val = 0 val_ref = dict.get(name_ref, None) if not val_ref: raise pnodes.ParseError(lr, "name {0} not found".format(name_ref)) val = val_ref + int(val) val_max = max(val_max, val) dict[name_def] = val res.append((name_def, val)) print_file_header(includeIntEnumUnique=False, includeBindToLibGHDL=False) print(dedent(""" @export class Name: """), end='') for n, v in res: # Avoid clash with Python names if n in ["False", "True", "None"]: n = "N" + n print(" {0} = {1}".format(n, v))