Attempts to eat a literal and build a literal AST node.
(parse-literal tokens) → (mv ast-node tokens-after-literal-or-reserr)
Returns two values: an optional literal AST node and either the list of remaining tokens or a reserr.
If a valid literal token is found, the first value returned is a literal AST node of the appropriate kind. Different kinds have different substructure.
If no literal is found, the first value returned is
Function:
(defun parse-literal (tokens) (declare (xargs :guard (abnf::tree-listp tokens))) (let ((__function__ 'parse-literal)) (declare (ignorable __function__)) (b* (((when (endp tokens)) (mv nil (reserrf (cons "ran out of tokens when trying to parse literal" tokens)))) ((unless (abnf::tree-listp tokens)) (mv nil (reserrf "guard of parse-literal"))) (putative-literal-tree (first tokens)) ((unless (and (abnf::tree-case putative-literal-tree :nonleaf) (equal (abnf::tree-nonleaf->rulename? putative-literal-tree) (abnf::rulename "literal")))) (mv nil (reserrf (cons "token is not a literal" tokens)))) (branches (abnf::tree-nonleaf->branches putative-literal-tree)) ((unless (and (listp branches) (equal (len branches) 1) (listp (car branches)) (equal (len (car branches)) 1) (abnf::treep (caar branches)) (abnf::tree-case (caar branches) :nonleaf))) (prog2$ (er hard? 'top-level "literal token seems to have the wrong structure for a literal") (mv nil (reserrf (cons "program logic error 2" tokens))))) (parsed-literal-kind (cst2ast-literal-kind (caar branches))) ((when (null parsed-literal-kind)) (mv nil (reserrf "problem with literal substructure")))) (mv parsed-literal-kind (rest tokens)))))
Theorem:
(defthm literal-optionp-of-parse-literal.ast-node (b* (((mv ?ast-node ?tokens-after-literal-or-reserr) (parse-literal tokens))) (literal-optionp ast-node)) :rule-classes :rewrite)
Theorem:
(defthm tree-list-resultp-of-parse-literal.tokens-after-literal-or-reserr (b* (((mv ?ast-node ?tokens-after-literal-or-reserr) (parse-literal tokens))) (abnf::tree-list-resultp tokens-after-literal-or-reserr)) :rule-classes :rewrite)
Theorem:
(defthm len-of-parse-literal-< (b* (((mv ?ast-node ?tokens-after-literal-or-reserr) (parse-literal tokens))) (implies (not (reserrp tokens-after-literal-or-reserr)) (< (len tokens-after-literal-or-reserr) (len tokens)))) :rule-classes :linear)