• 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
          • Syntax-for-tools
            • Disambiguator
            • Abstract-syntax
            • Parser
              • Parse-exprs/decls/stmts
              • Check-full-ppnumber
              • Read-char
              • Parstate
              • Lex-identifier/keyword
              • Lex-oct-iconst-/-dec-fconst
              • Lex-lexeme
              • Parse-external-declaration
              • Parse-cast-expression
              • Lex-isuffix-if-present
              • Parse-expression-or-type-name
              • Parse-postfix-expression
              • Lex-hex-iconst/fconst
              • Lex-dec-iconst/fconst
              • Lex-block-comment
              • Lex-escape-sequence
              • Read-token
              • Token
              • Lex-*-hexadecimal-digit
              • Lex-*-c-char
              • Lex-*-s-char
              • Parse-specifier/qualifier
              • Lex-*-digit
              • Lex-line-comment
              • Char-to-msg
                • Parse-primary-expression
                • Parse-declaration-specifier
                • Lex-iconst/fconst
                • Parse-external-declaration-list
                • Parse-declaration-specifiers
                • Lex-dec-expo-if-present
                • Init-parstate
                • Reread-to-token
                • Parse-asm-name-specifier
                • Lex-character-constant
                • Parse-expression-rest
                • Parstate$
                • Lex-dec-fconst
                • Token-unary-expression-start-p
                • Parse-declarator-or-abstract-declarator
                • Parse-asm-goto-labels
                • Parse-asm-clobbers
                • Lex-stringlit
                • Lex-non-octal-digit
                • Position
                • Lexeme
                • Lex-fsuffix-if-present
                • Parse-translation-unit
                • Lex-hexadecimal-digit
                • Token-type-specifier-keyword-p
                • Token-postfix-expression-rest-start-p
                • To-parstate$
                • Parse-?-asm-name-specifier
                • Parse-postfix-expression-rest
                • Parse-expression
                • Make-expr-unary-with-preinc/predec-ops
                • Lex-sign-if-present
                • Lex-dec-expo
                • Lex-bin-expo
                • Unread-to-token
                • Parse-*-stringlit
                • Parse-statement
                • Parse-fileset
                • Token-option
                • Lexeme-option
                • Token-struct-declaration-start-p
                • Parse-*-attribute-specifier
                • Parse-initializer-list
                • Parse-file
                • Parse-pointer
                • Parse-array/function-declarator
                • Unread-chars
                • Span
                • Read-stringlit
                • Lex-hex-quad
                • Unread-tokens
                • Token-type-qualifier-p
                • Read-identifier
                • Parse-*-asm-qualifier
                • Unread-char
                • Parse-attribute-name
                • Parse-argument-expressions
                • Make-expr-cast/add-or-cast/sub-ambig
                • Read-punctuator
                • Parse-struct-or-union-specifier
                • Parse-assignment-expression
                • Parse-asm-clobber
                • Token-specifier/qualifier-start-p
                • Token-primary-expression-start-p
                • Token-function-specifier-p
                • Reterr-msg
                • Read-keyword
                • Parse-*-increment/decrement
                • Parse-direct-abstract-declarator
                • Parse-declaration-or-statement
                • Char+position
                • Unread-token
                • Token+span
                • Token-expression-start-p
                • Update-parstate->tokens-unread
                • Update-parstate->chars-unread
                • Token-to-msg
                • Update-parstate->tokens-read
                • Update-parstate->tokens-length
                • Update-parstate->position
                • Update-parstate->chars-read
                • Parse-parameter-declaration
                • Parse-argument-expressions-rest
                • Update-parstate->chars-length
                • Token-to-type-specifier-keyword
                • Update-parstate->bytes
                • Parse-unary-expression
                • Parse-generic-associations-rest
                • Parse-conditional-expression
                • Update-parstate->size
                • Update-parstate->gcc
                • Parsize
                • Parse-direct-abstract-declarator-rest
                • Token-declaration-specifier-start-p
                • Parse-designator-list
                • Token-designation?-initializer-start-p
                • Token-abstract-declarator-start-p
                • Parse-?-asm-output-operands
                • Parse-?-asm-input-operands
                • Parse-struct-declaration
                • Parse-specifier-qualifier-list
                • Parse-parameter-declaration-list
                • Parse-constant-expression
                • Token-type-specifier-start-p
                • Token-type-qualifier-or-attribute-specifier-start-p
                • Parse-static-assert-declaration
                • Parse-fileset-loop
                • Parse-direct-declarator
                • Parse-declaration
                • Parse-attribute-parameters
                • Token-unary-operator-p
                • Token-to-unary-operator
                • Token-to-type-qualifier
                • Token-storage-class-specifier-p
                • Token-punctuatorp
                • Token-direct-abstract-declarator-start-p
                • Token-declarator-start-p
                • Parse-direct-declarator-rest
                • Token-to-storage-class-specifier
                • Token-to-assignment-operator
                • Token-to-asm-qualifier
                • Token-struct-declarator-start-p
                • Token-keywordp
                • Token-initializer-start-p
                • Token-direct-declarator-start-p
                • Token-assignment-operator-p
                • Parse-type-qualifier-and-attribute-specifier-list
                • Parse-enumerator-list
                • Parse-designation?-initializer
                • Parse-compound-literal
                • Parse-block-item
                • Token-type-name-start-p
                • Token-to-function-specifier
                • Token-preinc/predec-operator-p
                • Token-multiplicative-operator-p
                • Token-designator-start-p
                • Token-designation-start-p
                • Parstate->tokens-unread
                • Parstate->chars-unread
                • Parstate->bytes
                • Parse-initializer
                • Parse-generic-association
                • Parse-declaration-list
                • Parse-attribute-specifier
                • Parse-asm-output-operands
                • Token-to-relational-operator
                • Token-to-preinc/predec-operator
                • Token-to-multiplicative-operator
                • Token-relational-operator-p
                • Token-equality-operator-p
                • Token-asm-qualifier-p
                • Token-additive-operator-p
                • Span-join
                • Parstate->tokens-read
                • Parstate->tokens-length
                • Parstate->size
                • Parstate->chars-read
                • Parstate->chars-length
                • Parse-asm-statement
                • Parse-asm-input-operands
                • Update-parstate->token
                • Update-parstate->char
                • Token-to-equality-operator
                • Token-to-additive-operator
                • Token-shift-operator-p
                • To-parstate$-tokens-unread
                • To-parstate$-chars-unread
                • Position-inc-line
                • Position-inc-column
                • Parstate->gcc
                • Parse-type-name
                • Parse-struct-declarator-list
                • Parse-struct-declaration-list
                • Parse-relational-expression-rest
                • Parse-multiplicative-expression-rest
                • Parse-logical-or-expression-rest
                • Parse-logical-and-expression-rest
                • Parse-inclusive-or-expression-rest
                • Parse-exclusive-or-expression-rest
                • Parse-equality-expression-rest
                • Parse-array/function-abstract-declarator
                • Parse-additive-expression-rest
                • Token-to-shift-operator
                • To-parstate$-tokens-read
                • Parse-struct-declarator
                • Parse-shift-expression-rest
                • Parse-member-designor
                • Parse-init-declarator-list
                • Parse-init-declarator
                • Parse-and-expression-rest
                • Parse-alignment-specifier
                • To-parstate$-chars-read
                • Position-to-msg
                • Parstate->token
                • Parstate->char
                • Parse-shift-expression
                • Parse-relational-expression
                • Parse-multiplicative-expression
                • Parse-logical-or-expression
                • Parse-logical-and-expression
                • Parse-inclusive-or-expression
                • Parse-exclusive-or-expression
                • Parse-equality-expression
                • Parse-enum-specifier
                • Parse-block-item-list
                • Parse-attribute-list
                • Parse-and-expression
                • Parse-additive-expression
                • Parse-abstract-declarator
                • Char+position-list
                • Token+span-list
                • Span-to-msg
                • Parstate-fix
                • Parse-member-designor-rest
                • Position-init
                • Parstate->position
                • Parse-designator
                • Parse-declarator
                • Parse-attribute
                • Irr-token
                • Irr-span
                • Irr-position
                • Irr-lexeme
                • Token-list
                • Parse-type-qualifier-or-attribute-specifier
                • Parse-enumerator
                • Parse-asm-output-operand
                • Parse-asm-input-operand
              • Validator
              • Printer
              • Formalized-subset
              • Mapping-to-language-definition
              • Input-files
              • Defpred
              • Output-files
              • Abstract-syntax-operations
              • Validation-information
              • Implementation-environments
              • Concrete-syntax
              • Unambiguity
              • Ascii-identifiers
              • Preprocessing
              • Abstraction-mapping
            • Atc
            • Language
            • Representation
            • Transformation-tools
            • Insertion-sort
            • Pack
          • Bv
          • Imp-language
          • Event-macros
          • Java
          • Bitcoin
          • Ethereum
          • Yul
          • 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

    Char-to-msg

    Represent an optional character as a message.

    Signature
    (char-to-msg char) → msg
    Arguments
    char — Guard (nat-optionp char).
    Returns
    msg — Type (msgp msg).

    As mentioned in parstate, we represent characters as natural numbers, meant to be Unicode code points (more precisely, Unicode scalar values), including ASCII codes as a subset. When an unexpected character is encountered during lexing, we return user-oriented error messages that include a description of the unexpected character. This ACL2 function constructs that description.

    We use read-char (defined later) to read characters. That function recognizes three new-line delimiters: line feed, carriage return, and carriage return followed by line feed. That function turns all these three into just a line feed. Thus, when this function is called to convert to a message a character coming from read-char, that character has never code 13 (for carriage return), and if it has code 10 (line feed) it is not necessarily a line feed in the file, but it could be a carriage return possibly followed by line feed. For this reason, we treat the case of code 10 a bit differently, and our *ascii-control-char-names* table has an internal-error-signaling entry for codes 10 and 13, because we do not access that table for those two codes.

    We also allow the character to be absent, i.e. to be nil. This happens when we reach the end of the file: attempting to read a character returns nil, instead of a natural number (see read-char). For error messages, it is convenient to treat this case similarly to the case of an actual character. So, for nil, this function returns a description of `end of file'.

    Definitions and Theorems

    Function: char-to-msg

    (defun char-to-msg (char)
      (declare (xargs :guard (nat-optionp char)))
      (let ((__function__ 'char-to-msg))
        (declare (ignorable __function__))
        (cond ((not char) "end of file")
              ((= char 10)
               (msg "the new-line character (LF, CR, or CR LF)"))
              ((< char 32)
               (msg "the ~s0 character (ASCII code ~x1)"
                    (nth char *ascii-control-char-names*)
                    char))
              ((= char 32)
               "the SP (space) character (ASCII code 32)")
              ((and (<= 33 char) (<= char 126))
               (msg "the ~s0 character (ASCII code ~x1)"
                    (acl2::implode (list (code-char char)))
                    char))
              ((= char 127)
               "the DEL (delete) character (ASCII code 127)")
              (t (msg "the non-ASCII Unicode character with code ~x0"
                      char)))))

    Theorem: msgp-of-char-to-msg

    (defthm msgp-of-char-to-msg
      (b* ((msg (char-to-msg char)))
        (msgp msg))
      :rule-classes :rewrite)