chiark / gitweb /
src/lexer-proto.lisp: New parser `must'.
authorMark Wooding <mdw@distorted.org.uk>
Thu, 26 May 2016 08:26:09 +0000 (09:26 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Fri, 8 Jun 2018 18:58:28 +0000 (19:58 +0100)
This is a simple wrapper around `error' for mandatory delimiters.  If it
doesn't find the delimiter, it reports the error and continues, possibly
producing a default value.

doc/SYMBOLS
doc/parsing.tex
src/lexer-proto.lisp

index 909f993dc8e30f10906b7639fd12fc431a1cbdaf..c506fcfe1b30c194c1b0e56f634da4e18ad4dfda 100644 (file)
@@ -464,6 +464,7 @@ lexer-proto.lisp
   define-indicator                              function
   cl:error                                      function class parser
   lexer-error                                   function
+  must                                          parser
   scan-comment                                  function
   skip-until                                    function parser
   sod-token-scanner                             class
@@ -1913,6 +1914,7 @@ expand-parser-form
   list-parser (eql cl:type) t
   token-parser-context (eql token) t
   token-scanner-context (eql cl:error) t
+  token-scanner-context (eql sod:must) t
   token-scanner-context (eql sod:skip-until) t
 expand-parser-spec
   t (eql :eof)
index 439d6d9bf4eb5f78caee6a1be7154f15c1167018..0d3487d1107c89a4fa54b5c141db3b167f35ab62 100644 (file)
@@ -797,6 +797,9 @@ file-location protocols.
       @<sub-parser> @<recover-parser>}
 \end{describe}
 
+\begin{describe}{parseform}{must @<sub-parser> @[@<default>@]}
+\end{describe}
+
 \begin{describe}{fun}
     {scan-comment @<char-scanner>
       @> @<result> @<success-flag> @<consumed-flag>}
index 122da753d99051275570d4f62ac457c32eefd169..b045be75589e8e243b011bcf92673eae7fa39f8b 100644 (file)
@@ -141,6 +141,14 @@ (defparse error (:context (context token-scanner-context)
                        :ignore-unconsumed ,ignore-unconsumed
                        :force-progress ,force-progress))
 
+(export 'must)
+(defparse must (:context (context token-scanner-context)
+               sub &optional default)
+  "Try to parse SUB; if it fails, report an error, and return DEFAULT.
+
+   This parser can't actually fail."
+  `(parse (error () ,sub (t ,default))))
+
 ;;;--------------------------------------------------------------------------
 ;;; Lexical analysis utilities.