Skip to content
Browse files

Add an example about introducing a new transformation to the grammar.

  • Loading branch information...
1 parent 926bfe1 commit 701fd733b7429428fba2e3478fe656c2fb2dce9d @vy committed
Showing with 28 additions and 0 deletions.
  1. +28 −0 README
View
28 README
@@ -111,6 +111,34 @@ approach yields to another problem: thread safety. Anyway, that was
the best that I can come up with; if you have any other ideas, I'd be
happy to hear them.
+Here is an example introducing a new transformation to the grammar:
+
+ (defun callback (string cursor &rest args)
+ (format t "Callback at char ~S at position ~D. (Args: ~S)~%"
+ (elt string cursor) cursor args))
+
+ (defmethod meta-sexp:transform-grammar
+ (ret ctx (in-meta (eql t)) (directive (eql :callback)) &optional args)
+ "\(:CALLBACK ARG ...)
+
+ Calls CALLBACK function, where the first two arguments passed to the function
+ are the input string and cursor position, and the rest is ARG."
+ `(apply #'callback
+ (meta-sexp::parser-context-data ,ctx)
+ (meta-sexp::parser-context-cursor ,ctx)
+ ,@args))
+
+ (defrule integer-debug? () ()
+ (:or (:rule integer?)
+ (:callback (list :rule :integer-debug?))))
+
+ ; TEST> (integer-debug? (create-parser-context "xy+123"))
+ ; Callback at char #\x at position 0. (Args: (:RULE :INTEGER-DEBUG?))
+ ; NIL
+
+ ; TEST> (integer-debug? (create-parser-context "+123"))
+ ; 123
+
,-------------------------.
| AVAILABLE TYPE CHECKERS |

0 comments on commit 701fd73

Please sign in to comment.
Something went wrong with that request. Please try again.