Changed parsing syntax

master
Mark 2022-10-21 14:41:24 -07:00
parent 5f4f3db48f
commit f8ff0a0c3e
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
1 changed files with 15 additions and 11 deletions

View File

@ -14,8 +14,6 @@ class Parser:
lp = pp.Suppress("(") lp = pp.Suppress("(")
rp = pp.Suppress(")") rp = pp.Suppress(")")
func_char = pp.Suppress("->")
macro_char = pp.Suppress("=")
# Simple tokens # Simple tokens
pp_expr = pp.Forward() pp_expr = pp.Forward()
@ -26,14 +24,14 @@ class Parser:
# Right associative. # Right associative.
# #
# <var> => <exp> # <var> => <exp>
pp_lambda_fun = pp_name + func_char + pp_expr pp_lambda_fun = (pp.Suppress("λ") | pp.Suppress("\\")) + pp_name + pp.Suppress(".") + pp_expr
pp_lambda_fun.set_parse_action(tokens.lambda_func.from_parse) pp_lambda_fun.set_parse_action(tokens.lambda_func.from_parse)
# Assignment. # Assignment.
# Can only be found at the start of a line. # Can only be found at the start of a line.
# #
# <var> = <exp> # <var> = <exp>
pp_macro_def = pp.line_start() + pp_name + macro_char + pp_expr pp_macro_def = pp.line_start() + pp_name + pp.Suppress("=") + pp_expr
pp_macro_def.set_parse_action(tokens.macro_expression.from_parse) pp_macro_def.set_parse_action(tokens.macro_expression.from_parse)
# Function calls. # Function calls.
@ -50,15 +48,21 @@ class Parser:
pp_expr <<= pp_lambda_fun ^ (lp + pp_expr + rp) ^ pp_name ^ (lp + pp_call + rp) pp_expr <<= pp_lambda_fun ^ (lp + pp_expr + rp) ^ pp_name ^ (lp + pp_call + rp)
pp_all = pp_expr | pp_macro_def pp_all = pp_expr | pp_macro_def
@staticmethod pp_command = ":" + pp_name
def parse_expression(line): pp_command.set_parse_action(tokens.command.from_parse)
return Parser.pp_expr.parse_string(line, parse_all = True)[0]
@staticmethod @staticmethod
def parse_assign(line): def parse_line(line):
return ( k = (
Parser.pp_macro_def Parser.pp_expr ^
).parse_string(line, parse_all = True)[0] Parser.pp_macro_def ^
Parser.pp_command ^ Parser.pp_call
).parse_string(
line,
parse_all = True
)[0]
print(k)
return(k)
@staticmethod @staticmethod
def run_tests(lines): def run_tests(lines):