Changed parsing syntax
parent
5f4f3db48f
commit
f8ff0a0c3e
26
parser.py
26
parser.py
|
@ -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):
|
||||||
|
|
Reference in New Issue