Created
May 28, 2017 18:45
-
-
Save tera3939/a7e84da9ca103f458e06948baffbf388 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def _to_snake_case(word): | |
final = '' | |
for item in word: | |
if item.isupper(): | |
final += "_" + item.lower() | |
else: | |
final += item | |
if final[0] == "_": | |
final = final[1:] | |
return final | |
def _to_lower_snake_case(word): | |
return "".join(map(lambda x: x.lower() if x.isupper() else x, word)) | |
def gen(name, i): | |
print("named!({}(&[u8]) -> {}, do_parse!(".format(_to_lower_snake_case(name), name)) | |
for j in i: | |
print(" {}: {} >>".format( | |
_to_snake_case(j[0]), | |
"take_byte" if j[1] == "BYTE" or j[1] == "u8" else | |
"take_word" if j[1] == "WORD" or j[1] == "u16" else | |
"take_dword" if j[1] == "DWORD" or j[1] == "u32" else | |
"image_data_directory" if j[1] == "IMAGE_DATA_DIRECTORY" else | |
"count!(image_data_directory, IMAGE_NUMBEROF_DIRECTORY_ENTRIES)" if j[1] == "Vec<IMAGE_DATA_DIRECTORY>" else | |
"Error: I dont know this term!!")) | |
print(" ( {} {{".format(name)) | |
for j in i: | |
print(" {}: {},".format(j[0], _to_snake_case(j[0]))) | |
print(" } )") | |
print("));") | |
def until_char(i: str, c: str) -> (str, str): | |
index = i.find(c) | |
r = i[:index] | |
l = i[index:] | |
return (r, l) | |
def parse(i): | |
a = tuple(map(lambda x: x.split(":"), i.split(",")))[:-1] | |
b = map(lambda x: (x[0].split()[1], x[1].lstrip()), a) | |
return tuple(b) | |
def goot(i): | |
(a,b) = until_char(i, "{") | |
name = a.split()[-1] | |
(c, _) = until_char(b[1:], "}") | |
struct = parse(c) | |
return gen(name, struct) | |
def main(): | |
test = """ | |
pub struct StreamHeader { | |
pub Offset: u32, | |
pub Size: u32, | |
pub Name: String, | |
} | |
""" | |
goot(test) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment