tables e inlines feitas com as restricoes

This commit is contained in:
Afonso Franco 2023-05-26 21:40:09 +01:00
parent 6e465cab3d
commit 19c572afab
Signed by: afonso
SSH key fingerprint: SHA256:JiuxZNdA5bRWXPMUJChI0AQ75yC+cXY4xM0IaVwEVys
3 changed files with 23 additions and 10 deletions

View file

@ -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}

View file

@ -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

View file

@ -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())