From f04cd34656459a5a1f4a39348dee7a3a3e11d25e Mon Sep 17 00:00:00 2001 From: tiago Date: Sun, 28 May 2023 18:06:12 +0100 Subject: [PATCH] fixed grammar, comment cleanup and greetings --- src/grammar | 16 +++++++++++----- src/lexer.py | 10 +--------- src/parser.py | 3 +-- src/pltoml.py | 43 ++++++++++++++++++++++++++++++++----------- 4 files changed, 45 insertions(+), 27 deletions(-) diff --git a/src/grammar b/src/grammar index f427601..ed34ef7 100644 --- a/src/grammar +++ b/src/grammar @@ -4,16 +4,20 @@ / / / /_/ / / / / /___/ ____/ ___ |/ _, _/___/ / /___/ _, _/ /_/ \____/_/ /_/_____/_/ /_/ |_/_/ |_|/____/_____/_/ |_| -toml : content +toml : newlines content + | content content : content tomlEntries | tomlEntries -tomlEntries : table - | object +tomlEntries : table newlines + | object newlines -table : '[' ID ']' - | '[' '[' ID ']' ']' +table : TABLE + | ARRTABLE + +newlines : newlines NEWLINE + | NEWLINE object : key '=' value | key '=' array @@ -48,6 +52,7 @@ key : ID | BIN | OCT | INF + | BOOL | NAN value : STR @@ -59,5 +64,6 @@ value : STR | BIN | OCT | INF + | BOOL | NAN diff --git a/src/lexer.py b/src/lexer.py index 99fb004..b180117 100644 --- a/src/lexer.py +++ b/src/lexer.py @@ -13,7 +13,7 @@ tokens = [ "HEX", "BIN", "OCT", - "FLOAT", # need to implement exponents check https://toml.io/en/ + "FLOAT", "BOOL", "INF", "NAN", @@ -65,25 +65,21 @@ def t_dict_close_dict(t): t.lexer.pop_state() return t -# needs to check if datetime is valid def t_DATETIME(t): r"\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}:\d{2}(\.\d{1,6})?(Z|[+-]\d{2}:\d{2})" return t -# needs to check if date is valid def t_DATE(t): r"\d{4}-\d{2}-\d{2}" return t -# needs to check if time is valid def t_TIME(t): r"\d{2}:\d{2}:\d{2}(\.\d{1,6})?" return t -# needs number grouping (example : flt8 = 224_617.445_991_228) def t_FLOAT(t): r"[+-]?\d+(_\d+)*\s*\.\s*\d+(_\d+)*([eE][-+]?\d+(_\d+)*)?" #case where float appears on the left side with spaces in between @@ -93,13 +89,11 @@ def t_FLOAT(t): return t -# needs number grouping (example : int6 = 5_349_221) def t_INT(t): r"[-+]?(\d+(_\d+)*)" return t -# needs number grouping (example : hex3 = 0xdead_beef) def t_HEX(t): r"0x[0-9a-fA-F]+(_[0-9a-fA-F]+)*" return t @@ -131,7 +125,6 @@ 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_]+\')\s*\.\s*([\w_]+|\"[\w_]+\"|\'[\w_]+\'))(\s*\.\s*([\w_]+|\"[\w_]+\"|\'[\w_]+\'))*" t.value = [s.strip(" \"'") for s in t.value.split('.')] @@ -144,7 +137,6 @@ def t_MLSTR(t): return t -# STR needs to be the first one to catch def t_STR(t): r"(\"(?:[^\"\\]|\\.)*\")|(\'[^\']*\')" t.value = t.value.strip("\"'") diff --git a/src/parser.py b/src/parser.py index 64054df..60fc49d 100644 --- a/src/parser.py +++ b/src/parser.py @@ -158,7 +158,6 @@ def p_dict_empty(p): def p_dictCont_multiple(p): """dictCont : dictCont ',' dictElem""" - # checar se os dicts nao teem keys repetidas duplicate_list = [k for k in p[1] if k in p[3]] for dup in duplicate_list: print(f"Duplicate inline-table key {dup}") @@ -192,7 +191,7 @@ def p_key_id(p): p[0] = p[1] -# the rest of the cases are the specific cases where the key as the same format as a float/int/etc +# the rest of the cases are the specific cases where the key is the same format as a float/int/etc # so we need make them a singleton list. def p_key_rest(p): """key : STR diff --git a/src/pltoml.py b/src/pltoml.py index 59b97b6..5ca2607 100755 --- a/src/pltoml.py +++ b/src/pltoml.py @@ -1,23 +1,44 @@ #! /usr/bin/env python3 -from parser import parse +from parser import parse from tokenizer import tokenizer import sys import argparse +def greetings(): + print(r" ____ __ __________ __ _____ ") + print(r" / __ \/ / /_ __/ __ \/ |/ / / ") + print(r" / /_/ / / / / / / / / /|_/ / / ") + print(r" / ____/ /___/ / / /_/ / / / / /___") + print(r" /_/ /_____/_/ \____/_/ /_/_____/") + print(r"--------------------------------------") + print(r"Authors: Tiago Sousa and Afonso Franco") + print(r"--------------------------------------") + def main(): - sys.argv - argv_parser = argparse.ArgumentParser(prog="PLTOML",description="A command line tool to convert toml files into json using ply.") - argv_parser.add_argument("-i","--input",help="The filepath to the target input file") - argv_parser.add_argument("-o","--output",help="The filepath to the target output file") - argv_parser.add_argument("-t","--tokenizer",help="This feature allows you to inspect all the tokens captured by the lexer (should only be used for debugging)") - + if sys.stdout.isatty(): + greetings() + argv_parser = argparse.ArgumentParser( + prog="PLTOML", + description="A command line tool to convert toml files into json using ply.", + ) + argv_parser.add_argument( + "-i", "--input", help="The filepath to the target input file" + ) + argv_parser.add_argument( + "-o", "--output", help="The filepath to the target output file" + ) + argv_parser.add_argument( + "-t", + "--tokenizer", + help="This feature allows you to inspect all the tokens captured by the lexer (should only be used for debugging)", + ) + args = argv_parser.parse_args() if args.tokenizer is not None: - tokenizer(args.input,args.output) + tokenizer(args.input, args.output) else: - parse(args.input,args.output) + parse(args.input, args.output) + if __name__ == "__main__": main() - -