fixed grammar, comment cleanup and greetings
This commit is contained in:
parent
acd8a6a8c7
commit
f04cd34656
4 changed files with 45 additions and 27 deletions
16
src/grammar
16
src/grammar
|
@ -4,16 +4,20 @@
|
||||||
/ / / /_/ / / / / /___/ ____/ ___ |/ _, _/___/ / /___/ _, _/
|
/ / / /_/ / / / / /___/ ____/ ___ |/ _, _/___/ / /___/ _, _/
|
||||||
/_/ \____/_/ /_/_____/_/ /_/ |_/_/ |_|/____/_____/_/ |_|
|
/_/ \____/_/ /_/_____/_/ /_/ |_/_/ |_|/____/_____/_/ |_|
|
||||||
|
|
||||||
toml : content
|
toml : newlines content
|
||||||
|
| content
|
||||||
|
|
||||||
content : content tomlEntries
|
content : content tomlEntries
|
||||||
| tomlEntries
|
| tomlEntries
|
||||||
|
|
||||||
tomlEntries : table
|
tomlEntries : table newlines
|
||||||
| object
|
| object newlines
|
||||||
|
|
||||||
table : '[' ID ']'
|
table : TABLE
|
||||||
| '[' '[' ID ']' ']'
|
| ARRTABLE
|
||||||
|
|
||||||
|
newlines : newlines NEWLINE
|
||||||
|
| NEWLINE
|
||||||
|
|
||||||
object : key '=' value
|
object : key '=' value
|
||||||
| key '=' array
|
| key '=' array
|
||||||
|
@ -48,6 +52,7 @@ key : ID
|
||||||
| BIN
|
| BIN
|
||||||
| OCT
|
| OCT
|
||||||
| INF
|
| INF
|
||||||
|
| BOOL
|
||||||
| NAN
|
| NAN
|
||||||
|
|
||||||
value : STR
|
value : STR
|
||||||
|
@ -59,5 +64,6 @@ value : STR
|
||||||
| BIN
|
| BIN
|
||||||
| OCT
|
| OCT
|
||||||
| INF
|
| INF
|
||||||
|
| BOOL
|
||||||
| NAN
|
| NAN
|
||||||
|
|
||||||
|
|
10
src/lexer.py
10
src/lexer.py
|
@ -13,7 +13,7 @@ tokens = [
|
||||||
"HEX",
|
"HEX",
|
||||||
"BIN",
|
"BIN",
|
||||||
"OCT",
|
"OCT",
|
||||||
"FLOAT", # need to implement exponents check https://toml.io/en/
|
"FLOAT",
|
||||||
"BOOL",
|
"BOOL",
|
||||||
"INF",
|
"INF",
|
||||||
"NAN",
|
"NAN",
|
||||||
|
@ -65,25 +65,21 @@ def t_dict_close_dict(t):
|
||||||
t.lexer.pop_state()
|
t.lexer.pop_state()
|
||||||
return t
|
return t
|
||||||
|
|
||||||
# needs to check if datetime is valid
|
|
||||||
def t_DATETIME(t):
|
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})"
|
r"\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}:\d{2}(\.\d{1,6})?(Z|[+-]\d{2}:\d{2})"
|
||||||
return t
|
return t
|
||||||
|
|
||||||
|
|
||||||
# needs to check if date is valid
|
|
||||||
def t_DATE(t):
|
def t_DATE(t):
|
||||||
r"\d{4}-\d{2}-\d{2}"
|
r"\d{4}-\d{2}-\d{2}"
|
||||||
return t
|
return t
|
||||||
|
|
||||||
|
|
||||||
# needs to check if time is valid
|
|
||||||
def t_TIME(t):
|
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
|
||||||
|
|
||||||
|
|
||||||
# needs number grouping (example : flt8 = 224_617.445_991_228)
|
|
||||||
def t_FLOAT(t):
|
def t_FLOAT(t):
|
||||||
r"[+-]?\d+(_\d+)*\s*\.\s*\d+(_\d+)*([eE][-+]?\d+(_\d+)*)?"
|
r"[+-]?\d+(_\d+)*\s*\.\s*\d+(_\d+)*([eE][-+]?\d+(_\d+)*)?"
|
||||||
#case where float appears on the left side with spaces in between
|
#case where float appears on the left side with spaces in between
|
||||||
|
@ -93,13 +89,11 @@ def t_FLOAT(t):
|
||||||
return t
|
return t
|
||||||
|
|
||||||
|
|
||||||
# needs number grouping (example : int6 = 5_349_221)
|
|
||||||
def t_INT(t):
|
def t_INT(t):
|
||||||
r"[-+]?(\d+(_\d+)*)"
|
r"[-+]?(\d+(_\d+)*)"
|
||||||
return t
|
return t
|
||||||
|
|
||||||
|
|
||||||
# needs number grouping (example : hex3 = 0xdead_beef)
|
|
||||||
def t_HEX(t):
|
def t_HEX(t):
|
||||||
r"0x[0-9a-fA-F]+(_[0-9a-fA-F]+)*"
|
r"0x[0-9a-fA-F]+(_[0-9a-fA-F]+)*"
|
||||||
return t
|
return t
|
||||||
|
@ -131,7 +125,6 @@ def t_BOOL(t):
|
||||||
return t
|
return t
|
||||||
|
|
||||||
|
|
||||||
# ID needs to be the last so it doesnt catch everything (literally)
|
|
||||||
def t_ID(t):
|
def t_ID(t):
|
||||||
r"(([\w_]+)|(\"[\w_]+\"|\'[\w_]+\')\s*\.\s*([\w_]+|\"[\w_]+\"|\'[\w_]+\'))(\s*\.\s*([\w_]+|\"[\w_]+\"|\'[\w_]+\'))*"
|
r"(([\w_]+)|(\"[\w_]+\"|\'[\w_]+\')\s*\.\s*([\w_]+|\"[\w_]+\"|\'[\w_]+\'))(\s*\.\s*([\w_]+|\"[\w_]+\"|\'[\w_]+\'))*"
|
||||||
t.value = [s.strip(" \"'") for s in t.value.split('.')]
|
t.value = [s.strip(" \"'") for s in t.value.split('.')]
|
||||||
|
@ -144,7 +137,6 @@ def t_MLSTR(t):
|
||||||
return t
|
return t
|
||||||
|
|
||||||
|
|
||||||
# STR needs to be the first one to catch
|
|
||||||
def t_STR(t):
|
def t_STR(t):
|
||||||
r"(\"(?:[^\"\\]|\\.)*\")|(\'[^\']*\')"
|
r"(\"(?:[^\"\\]|\\.)*\")|(\'[^\']*\')"
|
||||||
t.value = t.value.strip("\"'")
|
t.value = t.value.strip("\"'")
|
||||||
|
|
|
@ -158,7 +158,6 @@ 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
|
|
||||||
duplicate_list = [k for k in p[1] if k in p[3]]
|
duplicate_list = [k for k in p[1] if k in p[3]]
|
||||||
for dup in duplicate_list:
|
for dup in duplicate_list:
|
||||||
print(f"Duplicate inline-table key {dup}")
|
print(f"Duplicate inline-table key {dup}")
|
||||||
|
@ -192,7 +191,7 @@ def p_key_id(p):
|
||||||
p[0] = p[1]
|
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.
|
# so we need make them a singleton list.
|
||||||
def p_key_rest(p):
|
def p_key_rest(p):
|
||||||
"""key : STR
|
"""key : STR
|
||||||
|
|
|
@ -1,23 +1,44 @@
|
||||||
#! /usr/bin/env python3
|
#! /usr/bin/env python3
|
||||||
from parser import parse
|
from parser import parse
|
||||||
from tokenizer import tokenizer
|
from tokenizer import tokenizer
|
||||||
import sys
|
import sys
|
||||||
import argparse
|
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():
|
def main():
|
||||||
sys.argv
|
if sys.stdout.isatty():
|
||||||
argv_parser = argparse.ArgumentParser(prog="PLTOML",description="A command line tool to convert toml files into json using ply.")
|
greetings()
|
||||||
argv_parser.add_argument("-i","--input",help="The filepath to the target input file")
|
argv_parser = argparse.ArgumentParser(
|
||||||
argv_parser.add_argument("-o","--output",help="The filepath to the target output file")
|
prog="PLTOML",
|
||||||
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)")
|
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()
|
args = argv_parser.parse_args()
|
||||||
if args.tokenizer is not None:
|
if args.tokenizer is not None:
|
||||||
tokenizer(args.input,args.output)
|
tokenizer(args.input, args.output)
|
||||||
else:
|
else:
|
||||||
parse(args.input,args.output)
|
parse(args.input, args.output)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue