def test_parse_enums_4(self): """parse_enums Unit test 4: Elemate with value""" input4 = ''' typedef enum{aa=5} a; ''' output4 = [{'elements': [{'name': 'aa', 'value': '5'}], 'name': 'a'}] self.assertEqual(parse_enums(prepare_for_parsing(input4)), output4)
def test_parse_typedefs_2(self): """parse_typedefs Unit test 2: One typedef""" input2 = ''' typedef unsigned char u_int8; ''' output2 = [{'type' : 'unsigned char', 'name' : 'u_int8'}] self.assertEqual(parse_typedefs(prepare_for_parsing(input2)), output2)
def test_prepare_for_parsing_23(self): """prepare_for_parsing Unit test 23: Enum must be in one line""" input23 = '''enum a{ int a; int b;};''' output23 = 'enum a{int a;int b;};\n' self.assertEqual(prepare_for_parsing(input23), output23)
def test_parse_functions_4(self): """parse_functions Unit test 4: Array arguments""" input3 = 'int a(int b[5]);' output3 = [{'type': 'int', 'name': 'a', 'arguments': [{'type': 'int', 'name': 'b', 'array': '5'}]}] self.assertEqual(parse_functions(prepare_for_parsing(input3)), output3)
def test_prepare_for_parsing_22(self): """prepare_for_parsing Unit test 22: Struct must be in one line""" input22 = '''struct a{ int a; int b;};''' output22 = 'struct a{int a;int b;};\n' self.assertEqual(prepare_for_parsing(input22), output22)
def test_parse_enums_2(self): """parse_enums Unit test 2: One enum""" input2 = ''' typedef enum max_config_key_int64 { MAX_CONFIG_PCIE_TIMEOUT, MAX_CONFIG_WFI_TIMEOUT, MAX_CONFIG_TOPOLOGY_TIMEOUT, MAX_CONFIG_DEBUG_MODE, MAX_CONFIG_SHUTDOWN_TIMEOUT, MAX_CONFIG_DFEPRINTF_TIMEOUT_CYCLES_SIMULATION, MAX_CONFIG_DFEPRINTF_TIMEOUT_CYCLES_DFE, MAX_CONFIG_NB_INTEGERS } max_config_key_int64_t; ''' output2 = [ {'elements': [{'name': 'MAX_CONFIG_PCIE_TIMEOUT'}, {'name': 'MAX_CONFIG_WFI_TIMEOUT'}, {'name': 'MAX_CONFIG_TOPOLOGY_TIMEOUT'}, {'name': 'MAX_CONFIG_DEBUG_MODE'}, {'name': 'MAX_CONFIG_SHUTDOWN_TIMEOUT'}, {'name': 'MAX_CONFIG_DFEPRINTF_TIMEOUT_CYCLES_SIMULATION'}, {'name': 'MAX_CONFIG_DFEPRINTF_TIMEOUT_CYCLES_DFE'}, {'name': 'MAX_CONFIG_NB_INTEGERS'}], 'name': 'max_config_key_int64_t'} ] self.assertEqual(parse_enums(prepare_for_parsing(input2)), output2)
def test_parse_structs_3(self): """parse_structs Unit test 2: Several struct""" input3 = ''' typedef struct event_atomic { uint32_t event_id; } event_atomic_t; typedef struct event_atomic { uint32_t event_id; } event_atomic_t; typedef struct event_atomic { uint32_t event_id; } event_atomic_t; ''' output3 = [{ 'name': 'event_atomic_t', 'arguments': [{ 'type': 'uint32_t', 'name': 'event_id' }] }, { 'name': 'event_atomic_t', 'arguments': [{ 'type': 'uint32_t', 'name': 'event_id' }] }, { 'name': 'event_atomic_t', 'arguments': [{ 'type': 'uint32_t', 'name': 'event_id' }] }] self.assertEqual(parse_structs(prepare_for_parsing(input3)), output3)
def test_parse_typedefs_2(self): """parse_typedefs Unit test 2: One typedef""" input2 = ''' typedef unsigned char u_int8; ''' output2 = [{'type': 'unsigned char', 'name': 'u_int8'}] self.assertEqual(parse_typedefs(prepare_for_parsing(input2)), output2)
def test_parse_functions_2(self): """parse_functions Unit test 2: One function""" input2 = ''' void VectorAddition_writeLMem( int64_t param_address, int64_t param_nbytes, const int32_t *instream_cpu_to_lmem); ''' output2 = [{ 'type': 'void', 'name': 'VectorAddition_writeLMem', 'arguments': [{ 'type': 'int64_t', 'name': 'param_address' }, { 'type': 'int64_t', 'name': 'param_nbytes' }, { 'type': 'const int32_t*', 'name': 'instream_cpu_to_lmem' }] }] self.assertEqual(parse_functions(prepare_for_parsing(input2)), output2)
def test_parse_typedefs_3(self): """parse_typedefs Unit test 3: Several typedefs""" input3 = ''' typedef unsigned char u_int8; typedef unsigned char u_int8; ''' output3 = [{'type' : 'unsigned char', 'name' : 'u_int8'}, {'type' : 'unsigned char', 'name' : 'u_int8'}] self.assertEqual(parse_typedefs(prepare_for_parsing(input3)), output3)
def test_parse_enums_3(self): """parse_enums Unit test 3: Several enums""" input3 = ''' typedef enum{aa} a; typedef enum bbb {bb} b; ''' output3 = [{'elements': [{'name': 'aa'}], 'name': 'a'}, {'elements': [{'name': 'bb'}], 'name': 'b'}] self.assertEqual(parse_enums(prepare_for_parsing(input3)), output3)
def test_parse_enums_1(self): """parse_enums Unit test 1: No enums""" input1 = ''' void VectorAddition_writeLMem{ int64_t param_address, int64_t param_nbytes, const int32_t *instream_cpu_to_lmem}; ''' output1 = [] self.assertEqual(parse_enums(prepare_for_parsing(input1)), output1)
def test_parse_typedefs_1(self): """parse_typedefs Unit test 1: No typedefs""" input1 = ''' void VectorAddition_writeLMem{ int64_t param_address, int64_t param_nbytes, const int32_t *instream_cpu_to_lmem}; ''' output1 = [] self.assertEqual(parse_typedefs(prepare_for_parsing(input1)), output1)
def test_parse_structs_2(self): """parse_structs Unit test 2: One struct""" input2 = ''' typedef struct event_atomic { uint32_t event_id; } event_atomic_t; ''' output2 = [{'name': 'event_atomic_t', 'arguments': [{'type': 'uint32_t', 'name': 'event_id'}]}] self.assertEqual(parse_structs(prepare_for_parsing(input2)), output2)
def test_parse_structs_4(self): """parse_structs Unit test 4: Array argument""" input2 = ''' struct event_atomic { uint32_t event_id[10]; }; ''' output2 = [{'name': 'struct event_atomic', 'arguments': [{'type': 'uint32_t', 'name': 'event_id', 'array': '10'}]}] self.assertEqual(parse_structs(prepare_for_parsing(input2)), output2)
def test_parse_functions_4(self): """parse_functions Unit test 4: Array arguments""" input3 = 'int a(int b[5]);' output3 = [{ 'type': 'int', 'name': 'a', 'arguments': [{ 'type': 'int', 'name': 'b', 'array': '5' }] }] self.assertEqual(parse_functions(prepare_for_parsing(input3)), output3)
def test_parse_typedefs_3(self): """parse_typedefs Unit test 3: Several typedefs""" input3 = ''' typedef unsigned char u_int8; typedef unsigned char u_int8; ''' output3 = [{ 'type': 'unsigned char', 'name': 'u_int8' }, { 'type': 'unsigned char', 'name': 'u_int8' }] self.assertEqual(parse_typedefs(prepare_for_parsing(input3)), output3)
def test_parse_functions_2(self): """parse_functions Unit test 2: One function""" input2 = ''' void VectorAddition_writeLMem( int64_t param_address, int64_t param_nbytes, const int32_t *instream_cpu_to_lmem); ''' output2 = [{'type': 'void', 'name': 'VectorAddition_writeLMem', 'arguments': [{'type': 'int64_t', 'name': 'param_address'}, {'type': 'int64_t', 'name': 'param_nbytes'}, {'type': 'const int32_t*', 'name': 'instream_cpu_to_lmem'}]}] self.assertEqual(parse_functions(prepare_for_parsing(input2)), output2)
def test_parse_structs_4(self): """parse_structs Unit test 4: Array argument""" input2 = ''' struct event_atomic { uint32_t event_id[10]; }; ''' output2 = [{ 'name': 'struct event_atomic', 'arguments': [{ 'type': 'uint32_t', 'name': 'event_id', 'array': '10' }] }] self.assertEqual(parse_structs(prepare_for_parsing(input2)), output2)
def test_parse_enums_3(self): """parse_enums Unit test 3: Several enums""" input3 = ''' typedef enum{aa} a; typedef enum bbb {bb} b; ''' output3 = [{ 'elements': [{ 'name': 'aa' }], 'name': 'a' }, { 'elements': [{ 'name': 'bb' }], 'name': 'b' }] self.assertEqual(parse_enums(prepare_for_parsing(input3)), output3)
def test_parse_functions_3(self): """parse_functions Unit test 3: Several functions""" input3 = ''' void VectorAddition_writeLMem( int64_t param_address, int64_t param_nbytes, const int32_t *instream_cpu_to_lmem); int a(); double* b(); ''' output3 = [{'type': 'void', 'name': 'VectorAddition_writeLMem', 'arguments': [{'type': 'int64_t', 'name': 'param_address'}, {'type': 'int64_t', 'name': 'param_nbytes'}, {'type': 'const int32_t*', 'name': 'instream_cpu_to_lmem'}]}, {'type': 'int', 'name': 'a', 'arguments': []}, {'type': 'double*', 'name': 'b', 'arguments': []}] self.assertEqual(parse_functions(prepare_for_parsing(input3)), output3)
def test_parse_enums_2(self): """parse_enums Unit test 2: One enum""" input2 = ''' typedef enum max_config_key_int64 { MAX_CONFIG_PCIE_TIMEOUT, MAX_CONFIG_WFI_TIMEOUT, MAX_CONFIG_TOPOLOGY_TIMEOUT, MAX_CONFIG_DEBUG_MODE, MAX_CONFIG_SHUTDOWN_TIMEOUT, MAX_CONFIG_DFEPRINTF_TIMEOUT_CYCLES_SIMULATION, MAX_CONFIG_DFEPRINTF_TIMEOUT_CYCLES_DFE, MAX_CONFIG_NB_INTEGERS } max_config_key_int64_t; ''' output2 = [{ 'elements': [{ 'name': 'MAX_CONFIG_PCIE_TIMEOUT' }, { 'name': 'MAX_CONFIG_WFI_TIMEOUT' }, { 'name': 'MAX_CONFIG_TOPOLOGY_TIMEOUT' }, { 'name': 'MAX_CONFIG_DEBUG_MODE' }, { 'name': 'MAX_CONFIG_SHUTDOWN_TIMEOUT' }, { 'name': 'MAX_CONFIG_DFEPRINTF_TIMEOUT_CYCLES_SIMULATION' }, { 'name': 'MAX_CONFIG_DFEPRINTF_TIMEOUT_CYCLES_DFE' }, { 'name': 'MAX_CONFIG_NB_INTEGERS' }], 'name': 'max_config_key_int64_t' }] self.assertEqual(parse_enums(prepare_for_parsing(input2)), output2)
def test_parse_functions_3(self): """parse_functions Unit test 3: Several functions""" input3 = ''' void VectorAddition_writeLMem( int64_t param_address, int64_t param_nbytes, const int32_t *instream_cpu_to_lmem); int a(); double* b(); ''' output3 = [{ 'type': 'void', 'name': 'VectorAddition_writeLMem', 'arguments': [{ 'type': 'int64_t', 'name': 'param_address' }, { 'type': 'int64_t', 'name': 'param_nbytes' }, { 'type': 'const int32_t*', 'name': 'instream_cpu_to_lmem' }] }, { 'type': 'int', 'name': 'a', 'arguments': [] }, { 'type': 'double*', 'name': 'b', 'arguments': [] }] self.assertEqual(parse_functions(prepare_for_parsing(input3)), output3)
def test_prepare_for_parsing_21(self): """prepare_for_parsing Unit test 21: [] --> [ ]""" input21 = '[]' output21 = '[ ]' self.assertEqual(prepare_for_parsing(input21), output21)
def test_prepare_for_parsing_17(self): """prepare_for_parsing Unit test 17: Removing __extension__""" input17 = '__extension__ a' output17 = 'a' self.assertEqual(prepare_for_parsing(input17), output17)
def test_prepare_for_parsing_18(self): """prepare_for_parsing Unit test 18: Removing __inline""" input18 = '__inline a' output18 = 'a' self.assertEqual(prepare_for_parsing(input18), output18)
def test_prepare_for_parsing_03(self): """prepare_for_parsing Unit test 3: Removing excess ' '""" input3 = ' ' output3 = '' self.assertEqual(prepare_for_parsing(input3), output3)
def parse(code): """ Parses code and returns functions, enums, structures and typedefs. Usage: For this function to work properly input code needs to be preprocessed first, function cpp -I /opt/maxcompiler/include/slic code new_code has to be called to pre-process the code first. Input arguments: code -- code which we are parsing Output: Dictionary with functions, enums, structs and typedefs. functions -- list of functions Each function has: type -- type of function (located from beginnig to ' ') name -- name of function (located between ' ' and '(') arguments -- list of arguments (located between '(' and ')' and separated by comma) Each argument has: type -- type of the argument name -- name of the argument array -- array lenght of argument (optional) enums -- list of enums Each enum has: name -- name of enum (located between '} ' and ';') elements -- list of elements (located between '{' and '}' and separated by comma) Each element has: name -- name of the element value -- value of the element (only if the element has value) structs -- list of structures Each structure has: name -- name of structure (located between '}' and ';') arguments -- list of arguments (located between '{' and '}' and separated by ';') Each argument has: type -- type of the argument name -- name of the argument array -- array lenght of argument (optional) typedefs -- list of typedefs. Each typedef has: type -- type of the typedef (located between first ' ' and last ' ') name -- name of the typedef (located between last ' ' and ';') Example: >>> code = '''typedef unsigned char u_int8; ... ... max_actarray_t* max_aaaaaaactarray_init( ... max_file_t *maxfile, ... int nb_actions); ... ... max_actarray_t* max_mixed_actarray_init( ... max_file_t **maxfiles, ... int nb_actions); ... ... typedef enum max_debug_mode { ... MAX_DEBUG_NEVER = 0, ... MAX_DEBUG_ON_ERROR = 1, ... MAX_DEBUG_ALWAYS = 2 ... } max_debug_mode_t; ... ... typedef struct max_event_atomic { ... uint32_t event_id; ... } max_event_atomic_t; ... ... typedef struct max_event_startstop { ... uint32_t event_id; ... } max_event_startstop_t;''' >>> code = prepare_for_parsing(code) >>> print parse(code) {'functions': [{'type': 'max_actarray_t*', 'name': 'max_aaaaaaa ctarray_init', 'arguments': [{'type': 'max_file_t*', 'name': 'm axfile'}, {'type': 'int', 'name': 'nb_actions'}]}, {'type': 'ma x_actarray_t*', 'name': 'max_mixed_actarray_init', 'arguments': [{'type': 'max_file_t**', 'name': 'maxfiles'}, {'type': 'int', 'name': 'nb_actions'}]}], 'enums': [{'elements': [{'name': 'MA X_DEBUG_NEVER', 'value': '0'}, {'name': 'MAX_DEBUG_ON_ERROR', ' value': '1'}, {'name': 'MAX_DEBUG_ALWAYS', 'value': '2'}], 'nam e': 'max_debug_mode_t'}], 'structs': [{'name': 'max_event_atomi c_t', 'argument': [{'type': 'uint32_t', 'name': 'event_id'}]}, {'name': 'max_event_startstop_t', 'argument': [{'type': 'uint32 _t', 'name': 'event_id'}]}], 'typedefs': [{'type' : 'unsigned c har', 'name' : 'u_int8'}]} """ code = prepare_for_parsing(code) return { 'enums': parse_enums(code), 'functions': parse_functions(code), 'structs': parse_structs(code), 'typedefs': parse_typedefs(code) }
def test_prepare_for_parsing_05(self): """prepare_for_parsing Unit test 5: Removing ' ' before *""" input5 = 'df ** fl** ' output5 = 'df** fl**' self.assertEqual(prepare_for_parsing(input5), output5)
def test_prepare_for_parsing_02(self): """prepare_for_parsing Unit test 2: Removing lines with #""" input2 = '''hi # bye''' output2 = 'hi' self.assertEqual(prepare_for_parsing(input2), output2)
def test_prepare_for_parsing_16(self): """prepare_for_parsing Unit test 16: Removing __signed__""" input16 = '__signed__ a' output16 = 'a' self.assertEqual(prepare_for_parsing(input16), output16)
def test_prepare_for_parsing_13(self): """prepare_for_parsing Unit test 13: Removing ' ' before [""" input13 = 'a [ b' output13 = 'a[ b' self.assertEqual(prepare_for_parsing(input13), output13)
def test_prepare_for_parsing_24(self): """prepare_for_parsing Unit test 24: Removing __attribute__""" input24 = '''int a()__attribute__(());''' output24 = 'int a();\n' self.assertEqual(prepare_for_parsing(input24), output24)
def test_prepare_for_parsing_25(self): """prepare_for_parsing Unit test 25: Removing __asm__""" input25 = '''int a()__asm__(());''' output25 = 'int a();\n' self.assertEqual(prepare_for_parsing(input25), output25)
def test_prepare_for_parsing_01(self): """prepare_for_parsing Unit test 1: Removing empty lines""" input1 = '''hi bye''' output1 = 'hi bye' self.assertEqual(prepare_for_parsing(input1), output1)
def test_prepare_for_parsing_15(self): """prepare_for_parsing Unit test 15: Removing __restrict""" input15 = '__restrict a' output15 = ' a' self.assertEqual(prepare_for_parsing(input15), output15)
def test_prepare_for_parsing_20(self): """prepare_for_parsing Unit test 20: (void) --> ()""" input20 = '(void)' output20 = '()' self.assertEqual(prepare_for_parsing(input20), output20)
def test_prepare_for_parsing_14(self): """prepare_for_parsing Unit test 14: Removing extern""" input14 = 'extern a' output14 = 'a' self.assertEqual(prepare_for_parsing(input14), output14)
def test_prepare_for_parsing_19(self): """prepare_for_parsing Unit test 19: Removing volatile""" input19 = 'volatile a' output19 = 'a' self.assertEqual(prepare_for_parsing(input19), output19)
def test_prepare_for_parsing_12(self): """prepare_for_parsing Unit test 12: Removing ' ' around =""" input12 = 'a = b' output12 = 'a=b' self.assertEqual(prepare_for_parsing(input12), output12)
def test_prepare_for_parsing_04(self): """prepare_for_parsing Unit test 4: Lines must end with ;""" input4 = 'int a; int b;' output4 = 'int a;\nint b;\n' self.assertEqual(prepare_for_parsing(input4), output4)