Try to parse a Verilog expression from an ACL2 string.
It is occasionally convenient to parse Verilog expressions from strings, for instance in ACL2::symbolic-test-vectors we want to let the user write Verilog-style hierarchical identifiers to describe the wires they are interested in.
(vl-parse-expr-from-str str) returns a vl-expr-p or
We expect that
We have arbitrarily decided not to preprocess the string. This means you
can't use
We don't transform the expression at all. This means, for instance, that if you parse an expression like "foo[3-1:0]" then it'll still have the subtract there. Furthermore, we don't know what module the expression belongs in, so it won't be sized, may refer to invalid wires, etc.
Function:
(defun vl-parse-expr-from-str (str) (declare (xargs :guard (stringp str))) (b* ((echars (vl-echarlist-from-str str)) ((mv successp tokens warnings) (vl-lex echars :config *vl-default-loadconfig* :warnings nil)) ((unless successp) (cw "; vl-parse-expr-from-str: Lexing failed for ~s0.~%" str)) ((when warnings) (vl-cw-ps-seq (vl-println "Warnings from VL-PARSE-EXPR-FROM-STR:") (vl-print-warnings warnings)) (cw "; vl-parse-expr-from-str: Lexer warnings for ~s0.~%" str)) ((mv tokens ?cmap) (vl-kill-whitespace-and-comments tokens)) (pstate (make-vl-parsestate :warnings nil)) ((local-stobjs tokstream) (mv val tokstream)) (tokstream (vl-tokstream-update-tokens tokens)) (tokstream (vl-tokstream-update-pstate pstate)) ((mv err val tokstream) (vl-parse-expression :config *vl-default-loadconfig*)) ((when err) (vl-report-parse-error err tokens) (cw "; vl-parse-expr-from-str: Parsing failed for ~s0.~%" str) (mv nil tokstream)) ((vl-parsestate pstate) (vl-tokstream->pstate)) ((when pstate.warnings) (vl-cw-ps-seq (vl-println "Warnings from VL-PARSE-EXPR-FROM-STR:") (vl-print-warnings pstate.warnings)) (cw "; vl-parse-expr-from-str: Parser warnings for ~s0." str) (mv nil tokstream)) ((when (vl-tokstream->tokens)) (cw "; vl-parse-expr-from-str: Content remains after parsing an ~ expression from the string.~% ~ - Original string: ~s0~% ~ - First expr: ~s1~% ~ - Remaining after parse: ~s2~%" str (vl-pps-expr val) (vl-tokenlist->string-with-spaces (vl-tokstream->tokens))) (mv nil tokstream))) (mv val tokstream)))
Theorem:
(defthm vl-expr-p-of-vl-parse-expr-from-string (equal (vl-expr-p (vl-parse-expr-from-str str)) (if (vl-parse-expr-from-str str) t nil)))