Feito array of tables e tables. Falta fazer os checks de tipos e estatico vs dinamico
This commit is contained in:
parent
33145189ed
commit
daf20c6e0b
2 changed files with 66 additions and 30 deletions
|
@ -6,11 +6,26 @@ othername = """Tom \"The killer\" or \"yo\" or
|
||||||
'you' Preston-Werner"""
|
'you' Preston-Werner"""
|
||||||
date = 2010-04-23
|
date = 2010-04-23
|
||||||
time = 21:30:00
|
time = 21:30:00
|
||||||
"lol".yo = "test"
|
"lol".yo = "tesaat"
|
||||||
|
|
||||||
[[banana.ola.test]]
|
[[fruits]]
|
||||||
name = "rosinha"
|
name = "apple"
|
||||||
oi = [{"ola" = 13}]
|
|
||||||
|
|
||||||
[[banana.ola.test]]
|
[fruits.physical]
|
||||||
name = "laranjinha"
|
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"
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
from ply import yacc
|
from ply import yacc
|
||||||
from lexer import tokens
|
from lexer import tokens
|
||||||
|
import json
|
||||||
root_dict = dict()
|
|
||||||
current_header = root_dict
|
|
||||||
|
|
||||||
|
|
||||||
def p_toml(p):
|
def p_toml(p):
|
||||||
|
@ -27,41 +25,60 @@ def p_tomlEntries_object(p):
|
||||||
|
|
||||||
def p_table_simple(p):
|
def p_table_simple(p):
|
||||||
"""table : '[' ID ']'"""
|
"""table : '[' ID ']'"""
|
||||||
global root_dict, current_header
|
p.parser.syntax_error = False
|
||||||
headers = p[2].split('.')
|
headers = [s.strip('"\'') for s in p[2].split('.')]
|
||||||
temp = root_dict
|
temp = p.parser.root_dict
|
||||||
for header in headers:
|
for header in headers[:-1]:
|
||||||
if header not in temp:
|
if header not in temp:
|
||||||
temp[header] = {}
|
temp[header] = {}
|
||||||
temp = temp[header]
|
if isinstance(temp[header], list):
|
||||||
current_header = temp
|
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
|
# isto ta errado
|
||||||
def p_table_array(p):
|
def p_table_array(p):
|
||||||
"""table : '[' '[' ID ']' ']'"""
|
"""table : '[' '[' ID ']' ']'"""
|
||||||
global root_dict, current_header
|
p.parser.syntax_error = False
|
||||||
headers = p[3].split('.')
|
headers = [s.strip('"\'') for s in p[3].split('.')]
|
||||||
temp = root_dict
|
temp = p.parser.root_dict
|
||||||
for header in headers:
|
for header in headers[:-1]:
|
||||||
if header not in temp:
|
if header not in temp:
|
||||||
temp[header] = [{}]
|
temp[header] = [{}]
|
||||||
temp = temp[header][-1]
|
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):
|
def p_object(p):
|
||||||
"""object : key '=' value
|
"""object : key '=' value
|
||||||
| key '=' array
|
| key '=' array
|
||||||
| key '=' dict"""
|
| key '=' dict"""
|
||||||
global current_header
|
if p.parser.syntax_error:
|
||||||
headers = p[1].split('.')
|
return
|
||||||
temp = current_header
|
headers = [s.strip('"\'') for s in p[1].split('.')]
|
||||||
|
temp = p.parser.current_header
|
||||||
for header in headers[:-1]:
|
for header in headers[:-1]:
|
||||||
if header not in temp:
|
if header not in temp:
|
||||||
temp[header] = {}
|
temp[header] = {}
|
||||||
temp = temp[header]
|
temp = temp[header]
|
||||||
temp[headers[-1]] = p[3]
|
temp[headers[-1]] = p[3].strip('"\'')
|
||||||
|
|
||||||
|
|
||||||
def p_array_cont(p):
|
def p_array_cont(p):
|
||||||
|
@ -76,7 +93,7 @@ def p_array_empty(p):
|
||||||
|
|
||||||
def p_aCont_multiple(p):
|
def p_aCont_multiple(p):
|
||||||
"""aCont : aCont ',' aElem"""
|
"""aCont : aCont ',' aElem"""
|
||||||
arr:list = p[1]
|
arr: list = p[1]
|
||||||
p[0] = arr.append(p[3])
|
p[0] = arr.append(p[3])
|
||||||
|
|
||||||
|
|
||||||
|
@ -87,8 +104,8 @@ def p_aCont_single(p):
|
||||||
|
|
||||||
def p_aElem(p):
|
def p_aElem(p):
|
||||||
"""aElem : value
|
"""aElem : value
|
||||||
| array
|
| array
|
||||||
| dict"""
|
| dict"""
|
||||||
p[0] = p[1]
|
p[0] = p[1]
|
||||||
|
|
||||||
|
|
||||||
|
@ -104,7 +121,7 @@ def p_dict_empty(p):
|
||||||
|
|
||||||
def p_dictCont_multiple(p):
|
def p_dictCont_multiple(p):
|
||||||
"""dictCont : dictCont ',' dictElem"""
|
"""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])
|
p[0] = p[1].update(p[3])
|
||||||
|
|
||||||
|
|
||||||
|
@ -117,7 +134,7 @@ def p_dictElem_object(p):
|
||||||
"""dictElem : key '=' value
|
"""dictElem : key '=' value
|
||||||
| key '=' array
|
| key '=' array
|
||||||
| key '=' dict"""
|
| key '=' dict"""
|
||||||
headers = p[1].split('.')
|
headers = [s.strip('"\'') for s in p[1].split('.')]
|
||||||
p[0] = {}
|
p[0] = {}
|
||||||
temp = p[0]
|
temp = p[0]
|
||||||
for header in headers[:-1]:
|
for header in headers[:-1]:
|
||||||
|
@ -213,7 +230,11 @@ def p_error(p):
|
||||||
|
|
||||||
parser = yacc.yacc()
|
parser = yacc.yacc()
|
||||||
|
|
||||||
|
parser.root_dict = dict()
|
||||||
|
parser.current_header = parser.root_dict
|
||||||
|
parser.syntax_error = False
|
||||||
|
|
||||||
f = open('example.toml', 'r')
|
f = open('example.toml', 'r')
|
||||||
parser.parse(f.read())
|
parser.parse(f.read())
|
||||||
|
|
||||||
print(root_dict)
|
print(json.dumps(parser.root_dict, indent=2))
|
||||||
|
|
Loading…
Reference in a new issue