diff options
author | colin <colin@cicadas.surf> | 2023-07-19 06:55:24 -0700 |
---|---|---|
committer | colin <colin@cicadas.surf> | 2023-07-19 06:55:24 -0700 |
commit | fbe7ae96e394e87e0dfa890e7419064ef3525cfc (patch) | |
tree | a8568a0931b79c0d96a4360881651d703bf9dd55 /argot.lisp | |
parent | 5cd0bbe5ff4d571ef51645210e490e7613dcb8f2 (diff) |
Change: rule results are bound to rule name for action
Diffstat (limited to 'argot.lisp')
-rw-r--r-- | argot.lisp | 39 |
1 files changed, 22 insertions, 17 deletions
@@ -70,23 +70,28 @@ and it returns VAR in that case." (defmacro defgrammar (name (&key (documentation "")) &body ruledefs) (let ((bindings-var (gensym "BINDINGS"))) - `(progn - (defvar ,name nil ,documentation) - (setf ,name (make-hash-table :test #'eq)) - ,@(loop :for ruledef :in ruledefs - :for (lhs pattern vars action) := (parse-rule-def ruledef) - :for bindings - := (loop :for var :in vars - :collect `(,var (cdr (assoc ',var ,bindings-var)))) - :collect `(include-rule - ,name - (make-rule - :lhs ',lhs - :pattern ',pattern - ,@(when action - (list - :action `(lambda (,bindings-var) (let ,bindings ,action))))))) - ,name))) + (flet ((collect-let-bindings (lhs vars) + "Create let-bindings forms for the body of the :action. " + (loop :for var :in vars + :collect `(,var (cdr (assoc ',var ,bindings-var))) :into bs + :finally (return (list* lhs `(cdr (assoc ',lhs ,bindings-var)) bs)))) + (rule-includer (name lhs pattern &optional action bindings) + `(include-rule + ,name + (make-rule + :lhs ',lhs + :pattern ',pattern + ,@(when action + (list + :action `(lambda (,bindings-var) (let ,bindings ,action)))))))) + `(progn + (defvar ,name nil ,documentation) + (setf ,name (make-hash-table :test #'eq)) + ,@(loop :for ruledef :in ruledefs + :for (lhs pattern vars action) := (parse-rule-def ruledef) + :for bindings := (collect-let-bindings lhs vars) + :collect (rule-includer name lhs pattern action bindings)) + ,name)))) (defvar *position*) (defvar *grammar*) |