fixed inline tables and tables check

This commit is contained in:
Afonso Franco 2023-05-28 12:09:32 +01:00
parent 98d359e7fe
commit 5948922c15
Signed by: afonso
SSH key fingerprint: SHA256:JiuxZNdA5bRWXPMUJChI0AQ75yC+cXY4xM0IaVwEVys
2 changed files with 21 additions and 26 deletions

View file

@ -75,17 +75,8 @@ contributors = [
] ]
[fruie] [fruie]
apple.taste.sweet = true apple.taste.test = {oi = 6}
apple.yeet = {taste = 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
points = [ { x = 1, y = 2, z = 3 }, points = [ { x = 1, y = 2, z = 3 },
{ x = 7, y = 8, z = 9 }, { x = 7, y = 8, z = 9 },

View file

@ -39,8 +39,7 @@ def p_table_simple(p):
temp[header] = {} temp[header] = {}
if isinstance(temp[header], list): if isinstance(temp[header], list):
temp = temp[header][-1] temp = temp[header][-1]
else: elif isinstance(temp[header], dict):
if isinstance(temp[header], dict):
temp = temp[header] temp = temp[header]
if headers[-1] not in temp: if headers[-1] not in temp:
temp[headers[-1]] = {} temp[headers[-1]] = {}
@ -58,6 +57,7 @@ def p_table_array(p):
p.parser.current_inline_tables = [] p.parser.current_inline_tables = []
p.parser.current_tables = [] p.parser.current_tables = []
p.parser.syntax_error = False p.parser.syntax_error = False
path = '.'.join(p.parser.current_header_name + p[1])
headers = p[1] headers = p[1]
temp = p.parser.root_dict temp = p.parser.root_dict
for header in headers[:-1]: for header in headers[:-1]:
@ -68,7 +68,7 @@ def p_table_array(p):
temp[headers[-1]] = [{}] temp[headers[-1]] = [{}]
else: else:
if not isinstance(temp[headers[-1]], list): 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 p.parser.syntax_error = True
temp[headers[-1]].append({}) temp[headers[-1]].append({})
temp = temp[headers[-1]][-1] temp = temp[headers[-1]][-1]
@ -84,18 +84,22 @@ def p_object(p):
return return
headers = p[1] headers = p[1]
temp = p.parser.current_header temp = p.parser.current_header
path = '.'.join(p.parser.current_header_name + p[1])
if isinstance(p[3],dict): is_table = False
for table in p.parser.current_tables:
if p[1][0]==table[0]: if len(p[1])>1:
print("Error, trying to redefine a table")
return
p.parser.current_inline_tables.append(p[1])
elif len(p[1])>1:
for table in p.parser.current_inline_tables: for table in p.parser.current_inline_tables:
if p[1][:len(table)]==table: 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 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]) p.parser.current_tables.append(p[1])
for header in headers[:-1]: for header in headers[:-1]:
if header not in temp: if header not in temp:
@ -105,7 +109,7 @@ def p_object(p):
print("Error, cannot add {p[3]} to a {type(temp)} variable") print("Error, cannot add {p[3]} to a {type(temp)} variable")
return return
if headers[-1] in temp: 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 return
temp[headers[-1]] = p[3] temp[headers[-1]] = p[3]
@ -270,7 +274,7 @@ parser = yacc.yacc()
parser.root_dict = dict() parser.root_dict = dict()
parser.current_header = parser.root_dict parser.current_header = parser.root_dict
parser.current_header_name = "" parser.current_header_name = []
parser.syntax_error = False parser.syntax_error = False
parser.current_inline_tables = [] parser.current_inline_tables = []
parser.current_tables = [] parser.current_tables = []
@ -278,4 +282,4 @@ parser.current_tables = []
f = open("example.toml", "r") f = open("example.toml", "r")
parser.parse(f.read()) parser.parse(f.read())
print(json.dumps(parser.root_dict, indent=2)) #print(json.dumps(parser.root_dict, indent=2))