[PARSER] changes to tables and values

This commit is contained in:
Tiago Sousa 2023-05-24 18:53:09 +01:00
parent 3ccb736eb9
commit cd6ca88329
2 changed files with 75 additions and 22 deletions

View file

@ -15,9 +15,7 @@ tokens = [
"INF",
"NAN",
"COMMENT",
]
]
# needs to check if datetime is valid
@ -37,19 +35,6 @@ def t_TIME(t):
r"\d{2}:\d{2}:\d{2}(\.\d{1,6})?"
return t
# ID needs to be the last so it doesnt catch everything (literally)
def t_ID(t):
r"(([\w_]+)|(\"[\w_]+\"\.|\'[\w_]+\'\.)([\w_]+|\"[\w_]+\"|\'[\w_]+\'))(\.([\w_]+|\"[\w_]+\"|\'[\w_]+\'))*"
return t
def t_MLSTR(t):
r"(\"\"\"(?:[^\"\\]|\\.)+\"\"\")|(\'\'\'(?:[^\'\\]|\\.)+\'\'\')"
return t
# STR needs to be the first one to catch
def t_STR(t):
r"(?<!^)(\"(?:[^\"\\]|\\.)+\")|(\'[^\']+\')"
return t
# needs number grouping (example : flt8 = 224_617.445_991_228)
def t_FLOAT(t):
@ -94,6 +79,21 @@ def t_BOOL(t):
return t
# ID needs to be the last so it doesnt catch everything (literally)
def t_ID(t):
r"(([\w_]+)|(\"[\w_]+\"\.|\'[\w_]+\'\.)([\w_]+|\"[\w_]+\"|\'[\w_]+\'))(\.([\w_]+|\"[\w_]+\"|\'[\w_]+\'))*"
return t
def t_MLSTR(t):
r"(\"\"\"(?:[^\"\\]|\\.)+\"\"\")|(\'\'\'(?:[^\'\\]|\\.)+\'\'\')"
return t
# STR needs to be the first one to catch
def t_STR(t):
r"(\"(?:[^\"\\]|\\.)+\")|(\'[^\']+\')"
return t
def t_COMMENT(t):

View file

@ -1,49 +1,79 @@
from ply import yacc
from lexer import tokens
root_dict = dict()
def p_toml(p):
"toml : content"
def p_content_multi(p):
"content : content tomlEntries"
def p_content_single(p):
"content : tomlEntries"
def p_tomlEntries_table(p):
"tomlEntries : table"
def p_tomlEntries_object(p):
"tomlEntries : object"
def p_table_simple(p):
"table : '[' ID ']'"
headers = p[2].split(".")
temp_dict = {}
for header in headers[::-1]:
temp_dict = {header : temp_dict}
p[0] = temp_dict
# isto ta errado
def p_table_array(p):
"table : '[' '[' ID ']' ']'"
headers = p[3].split(".")
entry = []
temp_dict = {}
for header in headers[::-1]:
temp_dict = {header : entry}
res = temp_dict
p[0] = res
def p_object_value(p):
"object : key '=' value"
headers = p[1]
temp_dict = {headers[headers.length-1] : p[3]}
for header in headers[::-1].pop():
temp_dict = {header : temp_dict}
return temp_dict
def p_object_array(p):
"object : key '=' array"
def p_object_dict(p):
"object : key '=' dict"
def p_array_cont(p):
"array : '[' aCont ']'"
def p_array_empty(p):
"array : '[' ']'"
def p_aCont_multiple(p):
"aCont : aCont ',' aElem"
def p_aCont_single(p):
"aCont : aElem"
@ -51,9 +81,11 @@ def p_aCont_single(p):
def p_aElem_value(p):
"aElem : value"
def p_aElem_dict(p):
"aElem : dict"
def p_aElem_array(p):
"aElem : array"
@ -61,9 +93,11 @@ def p_aElem_array(p):
def p_dict_cont(p):
"dict : '{' dictCont '}'"
def p_dict_empty(p):
"dict : '{' '}'"
def p_dictCont_multiple(p):
"dictCont : dictCont ',' dictElem"
@ -71,55 +105,74 @@ def p_dictCont_multiple(p):
def p_dictCont_single(p):
"dictCont : dictElem"
def p_dictElem_object(p):
"dictElem : object"
def p_key_id(p):
"key : ID"
def p_key_str(p):
"key : STR"
def p_key_float(p):
"key : FLOAT"
def p_key_int(p):
"key : INT"
def p_value_str(p):
"value : STR"
p[0] = p[1]
def p_value_mlstr(p):
"value : MLSTR"
p[0] = p[1]
def p_value_date(p):
"value : DATE"
p[0] = p[1]
def p_value_datetime(p):
"value : DATETIME"
p[0] = p[1]
def p_value_int(p):
"value : INT"
p[0] = int(p[1])
def p_value_float(p):
"value : FLOAT"
p[0] = float(p[1])
def p_value_hex(p):
"value : HEX"
p[0] = hex(p[1])
def p_value_bin(p):
"value : BIN"
p[0] = bin(p[1])
def p_value_oct(p):
"value : OCT"
p[0] = oct(p[1])
def p_value_inf(p):
"value : INF"
p[0] = float(p[1])
def p_value_nan(p):
"value : NAN"
p[0] = p[1]
parser = yacc.yacc()