2024-01-31 17:30:20 +01:00
|
|
|
import ply.yacc as yacc
|
|
|
|
|
2024-01-31 18:13:52 +01:00
|
|
|
"""
|
|
|
|
Parser for HECKformat lines using PLY Parser.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from .lexer import tokens
|
2024-01-31 17:30:20 +01:00
|
|
|
|
|
|
|
def p_value(p):
|
|
|
|
"""
|
|
|
|
value : BASE16
|
|
|
|
| BASE10
|
|
|
|
| STRING
|
|
|
|
| ATOM
|
|
|
|
"""
|
|
|
|
#print(p[0], p[1])
|
|
|
|
p[0] = ("value", p[1])
|
|
|
|
|
|
|
|
|
2024-02-03 19:13:14 +01:00
|
|
|
def p_elm(p):
|
|
|
|
"""
|
|
|
|
elm : ATOM
|
|
|
|
| ELEMENT
|
|
|
|
"""
|
|
|
|
p[0] = p[1]
|
|
|
|
|
2024-01-31 17:30:20 +01:00
|
|
|
def p_attribute(p):
|
|
|
|
"""attribute : ATOM ATTRIB value"""
|
|
|
|
# print(p[0], p[1])
|
|
|
|
p[0] = ("attribute", p[1], p[3])
|
|
|
|
|
|
|
|
|
|
|
|
def p_attributes(p):
|
|
|
|
"""
|
|
|
|
attributes : attributes attribute
|
|
|
|
attributes : attribute
|
|
|
|
"""
|
|
|
|
if len(p) == 2:
|
|
|
|
p[0] = ["attributes", p[1]]
|
|
|
|
else:
|
|
|
|
p[0] = p[1]
|
|
|
|
p[0].append(p[2])
|
|
|
|
|
|
|
|
|
|
|
|
def p_section(p):
|
|
|
|
"""
|
2024-02-03 19:13:14 +01:00
|
|
|
section : SECTION elm
|
|
|
|
| SECTION elm attributes
|
2024-01-31 17:30:20 +01:00
|
|
|
"""
|
|
|
|
if (len(p) == 3):
|
|
|
|
p[0] = ("section", p[2])
|
|
|
|
else:
|
|
|
|
p[0] = ("section", p[2], p[3])
|
|
|
|
|
|
|
|
def p_values(p):
|
|
|
|
"""
|
|
|
|
values : values value
|
|
|
|
values : value
|
|
|
|
"""
|
|
|
|
if len(p) == 2:
|
|
|
|
p[0] = ["values", p[1]]
|
|
|
|
else:
|
|
|
|
p[0] = p[1]
|
|
|
|
p[0].append(p[2])
|
|
|
|
|
|
|
|
|
|
|
|
def p_element(p):
|
|
|
|
"""
|
2024-02-03 19:13:14 +01:00
|
|
|
element : elm values
|
|
|
|
| elm values attributes
|
|
|
|
| elm attributes
|
|
|
|
| elm
|
2024-01-31 17:30:20 +01:00
|
|
|
"""
|
|
|
|
# print(len(p))
|
2024-02-03 18:42:20 +01:00
|
|
|
if len(p) <= 2:
|
|
|
|
p[0] = ["element", p[1]]
|
|
|
|
else:
|
|
|
|
p[0] = ["element", p[1], p[2]]
|
|
|
|
if (len(p) == 4):
|
|
|
|
p[0].append(p[3])
|
2024-01-31 17:30:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
def p_statement(p):
|
|
|
|
"""
|
|
|
|
statement : element
|
|
|
|
| DEEP element
|
|
|
|
| section
|
|
|
|
"""
|
|
|
|
if (len(p) > 2):
|
2024-02-03 18:42:20 +01:00
|
|
|
p[0] = ('deep', len(p[1]), p[2])
|
2024-01-31 17:30:20 +01:00
|
|
|
else:
|
|
|
|
p[0] = p[1]
|
|
|
|
|
|
|
|
|
|
|
|
def p_error(p):
|
|
|
|
if not p:
|
|
|
|
return
|
|
|
|
else:
|
2024-02-03 18:42:20 +01:00
|
|
|
print(f"Syntax error {p}")
|
2024-01-31 17:30:20 +01:00
|
|
|
|
|
|
|
parser = yacc.yacc(start="statement")
|
|
|
|
|
|
|
|
|
|
|
|
TEST_STRING = [
|
|
|
|
'%%% heck',
|
|
|
|
'%%% heck foo=bar',
|
|
|
|
'%%% heck bar=-5l quux="hello! how are you today?" fred=69 barney=nice',
|
|
|
|
'title "My website!"',
|
|
|
|
'zoom 5.73',
|
|
|
|
'tags yo fresh',
|
|
|
|
'dumper 1 2 3 4 5 6 7 8 9 dumpped=True',
|
|
|
|
'> big_dumper 32 23 384848',
|
2024-02-03 18:42:20 +01:00
|
|
|
'>> deep_dumper 1 2 3 a=false'
|
2024-01-31 17:30:20 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
for test in TEST_STRING:
|
|
|
|
print(parser.parse(test))
|