Reorganize project layout. Add buildability.
This commit is contained in:
118
python/heckformat/parser.py
Normal file
118
python/heckformat/parser.py
Normal file
@ -0,0 +1,118 @@
|
||||
import ply.yacc as yacc
|
||||
|
||||
"""
|
||||
Parser for HECKformat lines using PLY Parser.
|
||||
"""
|
||||
|
||||
from .lexer import tokens
|
||||
|
||||
def p_value(p):
|
||||
"""
|
||||
value : BASE16
|
||||
| BASE10
|
||||
| STRING
|
||||
| ATOM
|
||||
"""
|
||||
#print(p[0], p[1])
|
||||
p[0] = ("value", p[1])
|
||||
|
||||
|
||||
def p_elm(p):
|
||||
"""
|
||||
elm : ATOM
|
||||
| ELEMENT
|
||||
"""
|
||||
p[0] = p[1]
|
||||
|
||||
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):
|
||||
"""
|
||||
section : SECTION elm
|
||||
| SECTION elm attributes
|
||||
"""
|
||||
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):
|
||||
"""
|
||||
element : elm values
|
||||
| elm values attributes
|
||||
| elm attributes
|
||||
| elm
|
||||
"""
|
||||
# print(len(p))
|
||||
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])
|
||||
|
||||
|
||||
def p_statement(p):
|
||||
"""
|
||||
statement : element
|
||||
| DEEP element
|
||||
| section
|
||||
"""
|
||||
if (len(p) > 2):
|
||||
p[0] = ('deep', len(p[1]), p[2])
|
||||
else:
|
||||
p[0] = p[1]
|
||||
|
||||
|
||||
def p_error(p):
|
||||
if not p:
|
||||
return
|
||||
else:
|
||||
print(f"Syntax error {p}")
|
||||
|
||||
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',
|
||||
'>> deep_dumper 1 2 3 a=false'
|
||||
]
|
||||
|
||||
if __name__ == "__main__":
|
||||
for test in TEST_STRING:
|
||||
print(parser.parse(test))
|
||||
Reference in New Issue
Block a user