diff --git a/src/example.toml b/src/example.toml index 8cefb69..edd1201 100644 --- a/src/example.toml +++ b/src/example.toml @@ -8,9 +8,6 @@ date = 2010-04-23 time = 21:30:00 "lol".yo = "tesaat" -[owner.name] -first = "joao" - [[fruits]] name = "apple" @@ -21,9 +18,6 @@ shape = "round" [[fruits.varieties]] name = "red delicious" -[fruits.varieties] -type = "Fruit" - [[fruits.varieties]] name = "granny smith" @@ -32,5 +26,6 @@ name = "banana" [[fruits.varieties]] name = "plantain" -integers2 = [1,2,3] +integers2.name = "Nail" +integers2 = {"oi"=5} diff --git a/src/lexer.py b/src/lexer.py index 735bcf9..09cd739 100644 --- a/src/lexer.py +++ b/src/lexer.py @@ -40,7 +40,7 @@ def t_TIME(t): def t_FLOAT(t): r"[+-]?\d+(\s*\.\s*\d+)?([eE][-+]?\d+)?" #case where float appears on the left side with spaces in between - if t.value.__contains(' '): + if t.value.__contains__(' '): t.type = "ID" t.value = [s.strip(' ') for s in t.value.split('.')] return t diff --git a/src/parser.py b/src/parser.py index 88bf63d..5ebcb76 100644 --- a/src/parser.py +++ b/src/parser.py @@ -25,6 +25,8 @@ def p_tomlEntries_object(p): def p_table_simple(p): """table : '[' ID ']'""" + p.parser.current_inline_tables = [] + p.parser.current_tables = [] p.parser.syntax_error = False headers = p[2] temp = p.parser.root_dict @@ -48,8 +50,10 @@ def p_table_simple(p): # isto ta errado def p_table_array(p): """table : '[' '[' ID ']' ']'""" + p.parser.current_inline_tables = [] + p.parser.current_tables = [] p.parser.syntax_error = False - headers = p[3].split(".") + headers = p[3] temp = p.parser.root_dict for header in headers[:-1]: if header not in temp: @@ -72,7 +76,19 @@ def p_object(p): | key '=' dict""" if p.parser.syntax_error: return - headers = p[1].split(".") + headers = p[1] + if isinstance(p[3],dict): + for table in p.parser.current_tables: + if p[1][0]==table[0]: + print("Error, trying to redefine a table") + return + p.parser.current_inline_tables.append(p[1]) + else: + for table in p.parser.current_inline_tables: + if p[1][:len(table)]==table: + print("Error, trying to redefine an inline table") + return + p.parser.current_tables.append(p[1]) temp = p.parser.current_header for header in headers[:-1]: if header not in temp: @@ -233,6 +249,8 @@ parser = yacc.yacc() parser.root_dict = dict() parser.current_header = parser.root_dict parser.syntax_error = False +parser.current_inline_tables = [] +parser.current_tables = [] f = open("example.toml", "r") parser.parse(f.read())