From 5948922c1500b13285378c29f9cc507db4d20b37 Mon Sep 17 00:00:00 2001 From: afonsofrancof Date: Sun, 28 May 2023 12:09:32 +0100 Subject: [PATCH] fixed inline tables and tables check --- src/example.toml | 13 ++----------- src/parser.py | 34 +++++++++++++++++++--------------- 2 files changed, 21 insertions(+), 26 deletions(-) diff --git a/src/example.toml b/src/example.toml index dd4069f..99a8566 100644 --- a/src/example.toml +++ b/src/example.toml @@ -75,17 +75,8 @@ contributors = [ ] [fruie] -apple.taste.sweet = true - -[fruie.apple.color] -test={"oi" = 14} -[fruie.apple.color.test] -test2="oi" - -[fruie.apple.texture] # you can add sub-tables -smooth = true -smooth = false - +apple.taste.test = {oi = 6} +apple.yeet = {taste = true} points = [ { x = 1, y = 2, z = 3 }, { x = 7, y = 8, z = 9 }, diff --git a/src/parser.py b/src/parser.py index b9e60c0..07b719a 100644 --- a/src/parser.py +++ b/src/parser.py @@ -39,8 +39,7 @@ def p_table_simple(p): temp[header] = {} if isinstance(temp[header], list): temp = temp[header][-1] - else: - if isinstance(temp[header], dict): + elif isinstance(temp[header], dict): temp = temp[header] if headers[-1] not in temp: temp[headers[-1]] = {} @@ -58,6 +57,7 @@ def p_table_array(p): p.parser.current_inline_tables = [] p.parser.current_tables = [] p.parser.syntax_error = False + path = '.'.join(p.parser.current_header_name + p[1]) headers = p[1] temp = p.parser.root_dict for header in headers[:-1]: @@ -68,7 +68,7 @@ def p_table_array(p): temp[headers[-1]] = [{}] else: if not isinstance(temp[headers[-1]], list): - print("Error, type of object is not list") + print(f"Error, cannot define {path} is not a list") p.parser.syntax_error = True temp[headers[-1]].append({}) temp = temp[headers[-1]][-1] @@ -84,18 +84,22 @@ def p_object(p): return headers = p[1] temp = p.parser.current_header - - 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]) - elif len(p[1])>1: + path = '.'.join(p.parser.current_header_name + p[1]) + is_table = False + + if len(p[1])>1: for table in p.parser.current_inline_tables: if p[1][:len(table)]==table: - print("Error, trying to redefine an inline table") + print(f"Error, trying to redefine {path}, an inline table") return + is_table=True + if isinstance(p[3],dict): + for table in p.parser.current_tables: + if table[:len(p[1])]==p[1]: + print(f"Error, trying to redefine {path}, a table") + return + p.parser.current_inline_tables.append(p[1]) + if is_table: p.parser.current_tables.append(p[1]) for header in headers[:-1]: if header not in temp: @@ -105,7 +109,7 @@ def p_object(p): print("Error, cannot add {p[3]} to a {type(temp)} variable") return if headers[-1] in temp: - print(f"Error, cannot define {'.'.join(p.parser.current_header_name + p[1])} twice") + print(f"Error, cannot define {path} twice") return temp[headers[-1]] = p[3] @@ -270,7 +274,7 @@ parser = yacc.yacc() parser.root_dict = dict() parser.current_header = parser.root_dict -parser.current_header_name = "" +parser.current_header_name = [] parser.syntax_error = False parser.current_inline_tables = [] parser.current_tables = [] @@ -278,4 +282,4 @@ parser.current_tables = [] f = open("example.toml", "r") parser.parse(f.read()) -print(json.dumps(parser.root_dict, indent=2)) +#print(json.dumps(parser.root_dict, indent=2))