From a4e60dd218ee4c8e5d5435f001f2e4a297c2eec6 Mon Sep 17 00:00:00 2001 From: afonsofrancof Date: Thu, 25 May 2023 22:04:53 +0100 Subject: [PATCH] fix some of the string stripping and added more examples --- src/example.toml | 5 +++++ src/lexer.py | 3 ++- src/parser.py | 13 +++++++------ 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/example.toml b/src/example.toml index 8b836bf..8cefb69 100644 --- a/src/example.toml +++ b/src/example.toml @@ -8,6 +8,9 @@ date = 2010-04-23 time = 21:30:00 "lol".yo = "tesaat" +[owner.name] +first = "joao" + [[fruits]] name = "apple" @@ -29,3 +32,5 @@ name = "banana" [[fruits.varieties]] name = "plantain" +integers2 = [1,2,3] + diff --git a/src/lexer.py b/src/lexer.py index 8e16636..ddee0de 100644 --- a/src/lexer.py +++ b/src/lexer.py @@ -81,7 +81,7 @@ def t_BOOL(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_]+\'))*" + r"(([\w_]+)|(\"[\w_]+\"|\'[\w_]+\')\s*\.\s*([\w_]+|\"[\w_]+\"|\'[\w_]+\'))(\s*\.\s*([\w_]+|\"[\w_]+\"|\'[\w_]+\'))*" return t @@ -93,6 +93,7 @@ def t_MLSTR(t): # STR needs to be the first one to catch def t_STR(t): r"(\"(?:[^\"\\]|\\.)+\")|(\'[^\']+\')" + t.value = t.value.strip("\"'") return t diff --git a/src/parser.py b/src/parser.py index 9f8560b..44e430d 100644 --- a/src/parser.py +++ b/src/parser.py @@ -26,7 +26,7 @@ def p_tomlEntries_object(p): def p_table_simple(p): """table : '[' ID ']'""" p.parser.syntax_error = False - headers = [s.strip('"\'') for s in p[2].split('.')] + headers = p[2].split('.') temp = p.parser.root_dict for header in headers[:-1]: if header not in temp: @@ -49,7 +49,7 @@ def p_table_simple(p): def p_table_array(p): """table : '[' '[' ID ']' ']'""" p.parser.syntax_error = False - headers = [s.strip('"\'') for s in p[3].split('.')] + headers = p[3].split('.') temp = p.parser.root_dict for header in headers[:-1]: if header not in temp: @@ -72,13 +72,13 @@ def p_object(p): | key '=' dict""" if p.parser.syntax_error: return - headers = [s.strip('"\'') for s in p[1].split('.')] + headers = 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].strip('"\'') + temp[headers[-1]] = p[3] def p_array_cont(p): @@ -94,7 +94,8 @@ def p_array_empty(p): def p_aCont_multiple(p): """aCont : aCont ',' aElem""" arr: list = p[1] - p[0] = arr.append(p[3]) + arr.append(p[3]) + p[0] = arr def p_aCont_single(p): @@ -134,7 +135,7 @@ def p_dictElem_object(p): """dictElem : key '=' value | key '=' array | key '=' dict""" - headers = [s.strip('"\'') for s in p[1].split('.')] + headers = p[1].split('.') p[0] = {} temp = p[0] for header in headers[:-1]: