Operators for rule actions
goodmami opened this issue · comments
Rules in pe are just expressions with actions and they can be embedded in other expressions, but often they are only named expressions in a grammar. The specification, however, anticipates rules that are not named expressions by placing their precedence between a sequence and a choice.
Assume we have an operator ->
that assigns an action to the previous sequence expression, and all to the right of the operator until the end of the line is the action. Then we could have:
Value <- Object -> dict
/ Array -> list
/ String -> json_decode
/ Number -> float
/ TRUE -> constant(True)
/ FALSE -> constant(False)
/ NULL -> constant(None)
For security, the action should not be simply ran through eval
, so it might need to be parsed as well, e.g., as some subset of Python. Above it takes some callable that will be called using the args at that point, but it could instead use the result of an expression:
Value <- Object -> dict(args)
/ Array -> list(args)
/ String -> json_decode(s)
/ Number -> float(s)
/ TRUE -> True
/ FALSE -> False
/ NULL -> None
This could also make it convenient for defining failures as well:
Value <- Object -> dict(args)
...
/ . -> fail(f"unexpected JSON value: {s}")
I'm going to close this. I'd rather not define semantic actions in the grammar (aside from the basics: capture, bind, etc.). There is already a Rule operator, but it does not have associated syntax.