• Top
    • Documentation
    • Books
    • Boolean-reasoning
    • Projects
    • Debugging
    • Std
    • Proof-automation
    • Macro-libraries
    • ACL2
    • Interfacing-tools
    • Hardware-verification
    • Software-verification
      • Kestrel-books
        • Crypto-hdwallet
        • Apt
        • Error-checking
        • Fty-extensions
        • Isar
        • Kestrel-utilities
        • Set
        • Soft
        • C
        • Bv
        • Imp-language
        • Event-macros
        • Java
        • Bitcoin
        • Ethereum
        • Yul
          • Transformations
          • Language
            • Abstract-syntax
            • Dynamic-semantics
            • Concrete-syntax
              • Lexer
              • Parser
                • Parse-keyword
                  • Parse-variable-declaration
                  • Parse-symbol
                  • Parse-literal
                  • Parse-identifier
                  • Hex-chars-and-uscores-to-hex-string-rest-element-list
                  • Parse-assignment-statement
                  • Parse-identifier-and-open-paren
                  • Parse-*-comma-identifier
                  • Parse-*-.-identifier
                  • Parse-continue-statement
                  • Parse-*-comma-path
                  • Parse-path
                  • Parse-leave-statement
                  • Parse-break-statement
                  • Cst2ast-hex-string
                  • Cst2ast-string-literal-content
                  • Cst2ast-escape-sequence
                  • Cst2ast-string-literal-contents
                  • Cst2ast-quoted-printable
                  • Parse-yul
                  • Cst2ast-string-literal
                  • Parse-yul-bytes
                  • Cst2ast-hex-number
                  • Cst2ast-uhhhh
                  • Cst2ast-literal-kind
                  • Cst2ast-decimal-number
                  • Cst2ast-xhh
                  • Cst2ast-single-char
                  • Cst2ast-boolean
                  • Parse-*-case-clause
                  • Looks-like-hex-string-fringe
                  • Cst2ast-hex-digit-char-list
                  • Parse-*-statement
                  • Parse-*-comma-expression
                  • Parse-switch-statement
                  • Parse-if-statement
                  • Parse-for-statement
                  • Parse-expression
                  • Parse-case-clause
                  • Parse-fundef
                  • Parse-function-call
                  • Parse-block
                  • *single-quote-tree-list*
                  • *double-quote-tree-list*
                  • *yul-keywords*
                  • *single-quoted-content-rulenames*
                  • *list-leafterm-x*
                  • *list-leafterm-u*
                  • *list-leafterm-92*
                  • *double-quoted-content-rulenames*
                  • *yul-symbols*
                • Grammar-old
                • Grammar
                • Tokenizer
              • Static-soundness
              • Static-semantics
              • Errors
            • Yul-json
          • Zcash
          • ACL2-programming-language
          • Prime-fields
          • Json
          • Syntheto
          • File-io-light
          • Cryptography
          • Number-theory
          • Lists-light
          • Axe
          • Builtins
          • Solidity
          • Helpers
          • Htclient
          • Typed-lists-light
          • Arithmetic-light
        • X86isa
        • Axe
        • Execloader
      • Math
      • Testing-utilities
    • Parser

    Parse-keyword

    Attempts to eat the named keyword.

    Signature
    (parse-keyword keyword tokens) 
      → 
    (mv ast-node tokens-after-keyword-or-reserr)
    Arguments
    keyword — Guard (stringp keyword).
    tokens — Guard (abnf::tree-listp tokens).
    Returns
    ast-node — Type (statement-optionp ast-node).
    tokens-after-keyword-or-reserr — Type (abnf::tree-list-resultp tokens-after-keyword-or-reserr).

    Returns two values: an optional statement AST node and either the list of remaining tokens or a reserr.

    If a keyword is found, and if it is leave, break, or continue, then the appropriate statement is built and returned as the first value. If a different keyword is found, the first value returned is NIL, since no other Yul AST node has surface syntax consisting solely of a single keyword. In either case, the second value returns is the list of remaining tokens after eating the keyword token.

    If no keyword is found, the first value returned is NIL and the second is a reserr.

    In this context, keyword is a nonterminal in the ABNF grammar for Yul, and its alternatives are terminals (aka terminal symbols) that are the actual keywords. See grammar.abnf.

    Parsing a keyword as a concrete syntax tree means we look for a nonleaf tree where the rulename is "keyword" and the leafterm has the bytes (ASCII codes) of the keyword string.

    Definitions and Theorems

    Function: parse-keyword

    (defun parse-keyword (keyword tokens)
     (declare (xargs :guard (and (stringp keyword)
                                 (abnf::tree-listp tokens))))
     (let ((__function__ 'parse-keyword))
      (declare (ignorable __function__))
      (if
       (not (member-equal keyword *yul-keywords*))
       (prog2$
        (er
         hard? 'top-level
         (string-append
             "parse-keyword called on something not in *yul-keywords*: "
             keyword))
        (mv nil
            (reserrf (cons "program logic error" tokens))))
       (b*
        (((when (endp tokens))
          (mv
           nil
           (reserrf
            (cons
                 (string-append
                      "ran out of tokens when trying to parse keyword: "
                      keyword)
                 tokens))))
         (putative-keyword-tree (first tokens))
         ((unless
           (and
            (abnf::tree-case putative-keyword-tree :nonleaf)
            (equal (abnf::tree-nonleaf->rulename? putative-keyword-tree)
                   (abnf::rulename "keyword"))))
          (mv nil
              (reserrf (cons "token is not a keyword" tokens))))
         (branches (abnf::tree-nonleaf->branches putative-keyword-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)
                                        :leafterm)))
          (prog2$
           (er
            hard? 'top-level
            (string-append
             "keyword token seems to have the wrong structure for keyword:"
             keyword))
           (mv nil
               (reserrf (cons "cst structure error" tokens)))))
         (leafterm-nats (abnf::tree-leafterm->get (caar branches)))
         ((unless (unsigned-byte-listp 8 leafterm-nats))
          (prog2$
           (er
            hard? 'top-level
            (string-append
               "unexpected type of leafterm nats when parsing keyword: "
               keyword))
           (mv nil
               (reserrf (cons "cst structure error 2" tokens)))))
         (terminal-keyword (nats=>string leafterm-nats))
         ((unless (equal keyword terminal-keyword))
          (mv
            nil
            (reserrf
                 (cons (concatenate 'string
                                    "looking for keyword: '"
                                    keyword "', but received keyword: '"
                                    terminal-keyword "'")
                       tokens)))))
        (mv (cond ((equal keyword "leave")
                   (make-statement-leave))
                  ((equal keyword "break")
                   (make-statement-break))
                  ((equal keyword "continue")
                   (make-statement-continue))
                  (t nil))
            (abnf::tree-list-fix (rest tokens)))))))

    Theorem: statement-optionp-of-parse-keyword.ast-node

    (defthm statement-optionp-of-parse-keyword.ast-node
      (b* (((mv ?ast-node
                ?tokens-after-keyword-or-reserr)
            (parse-keyword keyword tokens)))
        (statement-optionp ast-node))
      :rule-classes :rewrite)

    Theorem: tree-list-resultp-of-parse-keyword.tokens-after-keyword-or-reserr

    (defthm
      tree-list-resultp-of-parse-keyword.tokens-after-keyword-or-reserr
      (b* (((mv ?ast-node
                ?tokens-after-keyword-or-reserr)
            (parse-keyword keyword tokens)))
        (abnf::tree-list-resultp tokens-after-keyword-or-reserr))
      :rule-classes :rewrite)

    Theorem: len-of-parse-keyword-<

    (defthm len-of-parse-keyword-<
      (b* (((mv ?ast-node
                ?tokens-after-keyword-or-reserr)
            (parse-keyword keyword tokens)))
        (implies (not (reserrp tokens-after-keyword-or-reserr))
                 (< (len tokens-after-keyword-or-reserr)
                    (len tokens))))
      :rule-classes :linear)