git.fiddlerwoaroof.com
Browse code

Add a let form for the binary parser

Ed Langley authored on 01/11/2018 06:28:43
Showing 2 changed files
... ...
@@ -2,3 +2,5 @@
2 2
 .[#]*
3 3
 [#]*
4 4
 *~
5
+.*.sw?
6
+*.*fsl
... ...
@@ -1,9 +1,10 @@
1 1
 (uiop:define-package :fwoar.bin-parser
2 2
     (:use :cl)
3 3
   (:mix :fw.lu :alexandria :serapeum)
4
-  (:export :extract
5
-           :le->int
6
-           :read-bytes))
4
+  (:export :le->int
5
+           :read-bytes
6
+           :extract
7
+           :extract-let))
7 8
 
8 9
 (in-package :fwoar.bin-parser)
9 10
 
... ...
@@ -81,3 +82,13 @@
81 82
           (append next-segment
82 83
                   (extract remainder s (append next-segment bindings)))
83 84
           next-segment))))
85
+
86
+(defmacro extract-let ((&rest bindings) parser s &body body)
87
+  (labels ((collect-binding (binding-spec)
88
+             (destructuring-bind (name target) binding-spec
89
+               `(,name (cdr (assoc ,target it)))))
90
+           (collect-bindings ()
91
+             (mapcar #'collect-binding bindings)))
92
+    `(let* ((it (extract ,parser ,s))
93
+            ,@(collect-bindings))
94
+       ,@body)))