diff --git a/src/example.toml b/src/example.toml index 581a235..fab3766 100644 --- a/src/example.toml +++ b/src/example.toml @@ -7,3 +7,10 @@ othername = """Tom \"The killer\" or \"yo\" or date = 2010-04-23 time = 21:30:00 "lol".yo = "test" + +[[banana.ola.test]] +name = "rosinha" +oi = [{"ola" = 13}] + +[[banana.ola.test]] +name = "laranjinha" diff --git a/src/parser.py b/src/parser.py index e99ffd9..85bf818 100644 --- a/src/parser.py +++ b/src/parser.py @@ -2,177 +2,218 @@ from ply import yacc from lexer import tokens root_dict = dict() +current_header = root_dict + def p_toml(p): - "toml : content" + """toml : content""" def p_content_multi(p): - "content : content tomlEntries" + """content : content tomlEntries""" def p_content_single(p): - "content : tomlEntries" - + """content : tomlEntries""" + def p_tomlEntries_table(p): - "tomlEntries : table" - + """tomlEntries : table""" + def p_tomlEntries_object(p): - "tomlEntries : object" - + """tomlEntries : object""" + def p_table_simple(p): - "table : '[' ID ']'" - headers = p[2].split(".") - temp_dict = {} - for header in headers[::-1]: - temp_dict = {header : temp_dict} - p[0] = temp_dict + """table : '[' ID ']'""" + global root_dict, current_header + headers = p[2].split('.') + temp = root_dict + for header in headers: + if header not in temp: + temp[header] = {} + temp = temp[header] + current_header = temp -# isto ta errado +# isto ta errado def p_table_array(p): - "table : '[' '[' ID ']' ']'" - headers = p[3].split(".") - entry = [] - temp_dict = {} - for header in headers[::-1]: - temp_dict = {header : entry} - - res = temp_dict - p[0] = res + """table : '[' '[' ID ']' ']'""" + global root_dict, current_header + headers = p[3].split('.') + temp = root_dict + for header in headers: + if header not in temp: + temp[header] = [{}] + temp = temp[header][-1] + current_header = temp -def p_object_value(p): - "object : key '=' value" - headers = p[1] - temp_dict = {headers[headers.length-1] : p[3]} - for header in headers[::-1].pop(): - temp_dict = {header : temp_dict} - return temp_dict - - -def p_object_array(p): - "object : key '=' array" - - -def p_object_dict(p): - "object : key '=' dict" +def p_object(p): + """object : key '=' value + | key '=' array + | key '=' dict""" + global current_header + headers = p[1].split('.') + temp = current_header + for header in headers[:-1]: + if header not in temp: + temp[header] = {} + temp = temp[header] + temp[headers[-1]] = p[3] def p_array_cont(p): - "array : '[' aCont ']'" + """array : '[' aCont ']'""" + p[0] = p[2] def p_array_empty(p): - "array : '[' ']'" + """array : '[' ']'""" + p[0] = [] def p_aCont_multiple(p): - "aCont : aCont ',' aElem" + """aCont : aCont ',' aElem""" + arr:list = p[1] + p[0] = arr.append(p[3]) def p_aCont_single(p): - "aCont : aElem" + """aCont : aElem""" + p[0] = [p[1]] -def p_aElem_value(p): - "aElem : value" - - -def p_aElem_dict(p): - "aElem : dict" - - -def p_aElem_array(p): - "aElem : array" +def p_aElem(p): + """aElem : value + | array + | dict""" + p[0] = p[1] def p_dict_cont(p): - "dict : '{' dictCont '}'" + """dict : '{' dictCont '}'""" + p[0] = p[2] def p_dict_empty(p): - "dict : '{' '}'" + """dict : '{' '}'""" + p[0] = {} def p_dictCont_multiple(p): - "dictCont : dictCont ',' dictElem" + """dictCont : dictCont ',' dictElem""" + #checar se os dicts nao teem keys repetidas + p[0] = p[1].update(p[3]) def p_dictCont_single(p): - "dictCont : dictElem" + """dictCont : dictElem""" + p[0] = p[1] def p_dictElem_object(p): - "dictElem : object" + """dictElem : key '=' value + | key '=' array + | key '=' dict""" + headers = p[1].split('.') + p[0] = {} + temp = p[0] + for header in headers[:-1]: + if header not in temp: + temp[header] = {} + temp = temp[header] + temp[headers[-1]] = p[3] def p_key_id(p): - "key : ID" + """key : ID""" + p[0] = p[1] def p_key_str(p): - "key : STR" + """key : STR""" + p[0] = p[1] def p_key_float(p): - "key : FLOAT" + """key : FLOAT""" + p[0] = p[1] def p_key_int(p): - "key : INT" - + """key : INT""" + p[0] = p[1] + def p_value_str(p): - "value : STR" + """value : STR""" p[0] = p[1] def p_value_mlstr(p): - "value : MLSTR" + """value : MLSTR""" p[0] = p[1] + def p_value_date(p): - "value : DATE" + """value : DATE""" p[0] = p[1] + def p_value_datetime(p): - "value : DATETIME" + """value : DATETIME""" + p[0] = p[1] + + +def p_value_time(p): + """value : TIME""" p[0] = p[1] def p_value_int(p): - "value : INT" + """value : INT""" p[0] = int(p[1]) + def p_value_float(p): - "value : FLOAT" + """value : FLOAT""" p[0] = float(p[1]) def p_value_hex(p): - "value : HEX" + """value : HEX""" p[0] = hex(p[1]) + def p_value_bin(p): - "value : BIN" + """value : BIN""" p[0] = bin(p[1]) def p_value_oct(p): - "value : OCT" + """value : OCT""" p[0] = oct(p[1]) + def p_value_inf(p): - "value : INF" + """value : INF""" p[0] = float(p[1]) + def p_value_nan(p): - "value : NAN" + """value : NAN""" p[0] = p[1] +def p_error(p): + print(p) + + parser = yacc.yacc() + +f = open('example.toml', 'r') +parser.parse(f.read()) + +print(root_dict)