Feito array of tables e tables. Falta fazer os checks de tipos e estatico vs dinamico

This commit is contained in:
Afonso Franco 2023-05-25 17:25:59 +01:00
parent 33145189ed
commit daf20c6e0b
Signed by: afonso
SSH key fingerprint: SHA256:JiuxZNdA5bRWXPMUJChI0AQ75yC+cXY4xM0IaVwEVys
2 changed files with 66 additions and 30 deletions

View file

@ -6,11 +6,26 @@ othername = """Tom \"The killer\" or \"yo\" or
'you' Preston-Werner"""
date = 2010-04-23
time = 21:30:00
"lol".yo = "test"
"lol".yo = "tesaat"
[[banana.ola.test]]
name = "rosinha"
oi = [{"ola" = 13}]
[[fruits]]
name = "apple"
[[banana.ola.test]]
name = "laranjinha"
[fruits.physical]
color = "red"
shape = "round"
[[fruits.varieties]]
name = "red delicious"
[fruits.varieties]
type = "Fruit"
[[fruits.varieties]]
name = "granny smith"
[[fruits]]
name = "banana"
[[fruits.varieties]]
name = "plantain"

View file

@ -1,8 +1,6 @@
from ply import yacc
from lexer import tokens
root_dict = dict()
current_header = root_dict
import json
def p_toml(p):
@ -27,41 +25,60 @@ def p_tomlEntries_object(p):
def p_table_simple(p):
"""table : '[' ID ']'"""
global root_dict, current_header
headers = p[2].split('.')
temp = root_dict
for header in headers:
p.parser.syntax_error = False
headers = [s.strip('"\'') for s in p[2].split('.')]
temp = p.parser.root_dict
for header in headers[:-1]:
if header not in temp:
temp[header] = {}
temp = temp[header]
current_header = temp
if isinstance(temp[header], list):
temp = temp[header][-1]
else:
if isinstance(temp[header], dict):
temp = temp[header]
if headers[-1] not in temp:
temp[headers[-1]] = {}
temp = temp[headers[-1]]
else:
print('Cannot define the same table twice')
p.parser.syntax_error = True
p.parser.current_header = temp
# isto ta errado
def p_table_array(p):
"""table : '[' '[' ID ']' ']'"""
global root_dict, current_header
headers = p[3].split('.')
temp = root_dict
for header in headers:
p.parser.syntax_error = False
headers = [s.strip('"\'') for s in p[3].split('.')]
temp = p.parser.root_dict
for header in headers[:-1]:
if header not in temp:
temp[header] = [{}]
temp = temp[header][-1]
current_header = temp
if headers[-1] not in temp:
temp[headers[-1]] = [{}]
else:
if not isinstance(temp[headers[-1]], list):
print('Error, type of object is not list')
p.parser.syntax_error = True
temp[headers[-1]].append({})
temp = temp[headers[-1]][-1]
p.parser.current_header = temp
def p_object(p):
"""object : key '=' value
| key '=' array
| key '=' dict"""
global current_header
headers = p[1].split('.')
temp = current_header
if p.parser.syntax_error:
return
headers = [s.strip('"\'') for s in p[1].split('.')]
temp = p.parser.current_header
for header in headers[:-1]:
if header not in temp:
temp[header] = {}
temp = temp[header]
temp[headers[-1]] = p[3]
temp[headers[-1]] = p[3].strip('"\'')
def p_array_cont(p):
@ -76,7 +93,7 @@ def p_array_empty(p):
def p_aCont_multiple(p):
"""aCont : aCont ',' aElem"""
arr:list = p[1]
arr: list = p[1]
p[0] = arr.append(p[3])
@ -87,8 +104,8 @@ def p_aCont_single(p):
def p_aElem(p):
"""aElem : value
| array
| dict"""
| array
| dict"""
p[0] = p[1]
@ -104,7 +121,7 @@ def p_dict_empty(p):
def p_dictCont_multiple(p):
"""dictCont : dictCont ',' dictElem"""
#checar se os dicts nao teem keys repetidas
# checar se os dicts nao teem keys repetidas
p[0] = p[1].update(p[3])
@ -117,7 +134,7 @@ def p_dictElem_object(p):
"""dictElem : key '=' value
| key '=' array
| key '=' dict"""
headers = p[1].split('.')
headers = [s.strip('"\'') for s in p[1].split('.')]
p[0] = {}
temp = p[0]
for header in headers[:-1]:
@ -213,7 +230,11 @@ def p_error(p):
parser = yacc.yacc()
parser.root_dict = dict()
parser.current_header = parser.root_dict
parser.syntax_error = False
f = open('example.toml', 'r')
parser.parse(f.read())
print(root_dict)
print(json.dumps(parser.root_dict, indent=2))