summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorColin Okay <cbeok@protonmail.com>2020-04-24 17:33:12 -0500
committerColin Okay <cbeok@protonmail.com>2020-04-24 17:33:12 -0500
commitecb957d5fafa014eb8e9387bca17568cb08c36dc (patch)
tree449dfa7c30d90f284e9c24ee749d6958cfe1b93d
parent47c2db531c0e18c25ef3d4261346de0294100cf5 (diff)
ignorable bindings in <<let
-rw-r--r--parzival.lisp14
1 files changed, 13 insertions, 1 deletions
diff --git a/parzival.lisp b/parzival.lisp
index 068d3b5..582c1da 100644
--- a/parzival.lisp
+++ b/parzival.lisp
@@ -210,10 +210,18 @@ in then. If the parse fails the combinator else is run instead."
"Introduce bindings for the results of several consequtive parses. If one of
them fails then the whole expression fails. BINDINGS should be analogus to a
let expression, i.e. (variable parser) pairs. EXPRESSION should be any
- expression that returns a parser - it can make use of the bound variables"
+ expression that returns a parser - it can make use of the bound variables.
+
+ Any binding variable whose SYMBOL-NAME starts with an underscore is declared
+ as IGNORE. This is handy if you want to run a parser in sequence but don't
+ care about its result value - you can binding to a variable like _VAR and the
+ compiler won't complain.
+ "
(if (null bindings) expression
`(<<bind ,(cadar bindings)
(lambda (,(caar bindings))
+ ,(when (ignorable-symbol-p (caar bindings))
+ (list 'declare (list 'ignore (caar bindings))))
(<<let ,(cdr bindings) ,expression)))))
@@ -539,3 +547,7 @@ the character C."
"Returns a lambda that returns x no matter what it gets as an argument"
(lambda (ignore) x))
+
+(defun ignorable-symbol-p (symb)
+ "Returns true if a symbol name starts with _"
+ (eql #\_ (elt (symbol-name symb) 0)))