[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", "INF",
"NAN", "NAN",
"COMMENT", "COMMENT",
] ]
# needs to check if datetime is valid # 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})?" r"\d{2}:\d{2}:\d{2}(\.\d{1,6})?"
return 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
# needs number grouping (example : flt8 = 224_617.445_991_228) # needs number grouping (example : flt8 = 224_617.445_991_228)
def t_FLOAT(t): def t_FLOAT(t):
@ -94,6 +79,21 @@ def t_BOOL(t):
return 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): def t_COMMENT(t):

View file

@ -1,16 +1,20 @@
from ply import yacc from ply import yacc
from lexer import tokens from lexer import tokens
root_dict = dict()
def p_toml(p): def p_toml(p):
"toml : content" "toml : content"
def p_content_multi(p): def p_content_multi(p):
"content : content tomlEntries" "content : content tomlEntries"
def p_content_single(p): def p_content_single(p):
"content : tomlEntries" "content : tomlEntries"
def p_tomlEntries_table(p): def p_tomlEntries_table(p):
"tomlEntries : table" "tomlEntries : table"
@ -21,12 +25,34 @@ def p_tomlEntries_object(p):
def p_table_simple(p): def p_table_simple(p):
"table : '[' ID ']'" "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): def p_table_array(p):
"table : '[' '[' ID ']' ']'" "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): def p_object_value(p):
"object : key '=' value" "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): def p_object_array(p):
"object : key '=' array" "object : key '=' array"
@ -35,15 +61,19 @@ def p_object_array(p):
def p_object_dict(p): def p_object_dict(p):
"object : key '=' dict" "object : key '=' dict"
def p_array_cont(p): def p_array_cont(p):
"array : '[' aCont ']'" "array : '[' aCont ']'"
def p_array_empty(p): def p_array_empty(p):
"array : '[' ']'" "array : '[' ']'"
def p_aCont_multiple(p): def p_aCont_multiple(p):
"aCont : aCont ',' aElem" "aCont : aCont ',' aElem"
def p_aCont_single(p): def p_aCont_single(p):
"aCont : aElem" "aCont : aElem"
@ -51,9 +81,11 @@ def p_aCont_single(p):
def p_aElem_value(p): def p_aElem_value(p):
"aElem : value" "aElem : value"
def p_aElem_dict(p): def p_aElem_dict(p):
"aElem : dict" "aElem : dict"
def p_aElem_array(p): def p_aElem_array(p):
"aElem : array" "aElem : array"
@ -61,9 +93,11 @@ def p_aElem_array(p):
def p_dict_cont(p): def p_dict_cont(p):
"dict : '{' dictCont '}'" "dict : '{' dictCont '}'"
def p_dict_empty(p): def p_dict_empty(p):
"dict : '{' '}'" "dict : '{' '}'"
def p_dictCont_multiple(p): def p_dictCont_multiple(p):
"dictCont : dictCont ',' dictElem" "dictCont : dictCont ',' dictElem"
@ -71,55 +105,74 @@ def p_dictCont_multiple(p):
def p_dictCont_single(p): def p_dictCont_single(p):
"dictCont : dictElem" "dictCont : dictElem"
def p_dictElem_object(p): def p_dictElem_object(p):
"dictElem : object" "dictElem : object"
def p_key_id(p): def p_key_id(p):
"key : ID" "key : ID"
def p_key_str(p): def p_key_str(p):
"key : STR" "key : STR"
def p_key_float(p): def p_key_float(p):
"key : FLOAT" "key : FLOAT"
def p_key_int(p): def p_key_int(p):
"key : INT" "key : INT"
def p_value_str(p): def p_value_str(p):
"value : STR" "value : STR"
p[0] = p[1]
def p_value_mlstr(p): def p_value_mlstr(p):
"value : MLSTR" "value : MLSTR"
p[0] = p[1]
def p_value_date(p): def p_value_date(p):
"value : DATE" "value : DATE"
p[0] = p[1]
def p_value_datetime(p): def p_value_datetime(p):
"value : DATETIME" "value : DATETIME"
p[0] = p[1]
def p_value_int(p): def p_value_int(p):
"value : INT" "value : INT"
p[0] = int(p[1])
def p_value_float(p): def p_value_float(p):
"value : FLOAT" "value : FLOAT"
p[0] = float(p[1])
def p_value_hex(p): def p_value_hex(p):
"value : HEX" "value : HEX"
p[0] = hex(p[1])
def p_value_bin(p): def p_value_bin(p):
"value : BIN" "value : BIN"
p[0] = bin(p[1])
def p_value_oct(p): def p_value_oct(p):
"value : OCT" "value : OCT"
p[0] = oct(p[1])
def p_value_inf(p): def p_value_inf(p):
"value : INF" "value : INF"
p[0] = float(p[1])
def p_value_nan(p): def p_value_nan(p):
"value : NAN" "value : NAN"
p[0] = p[1]
parser = yacc.yacc()