Emacs Lisp:验证树结构的标准方法?

kdb*_*kdb 5 types elisp data-structures

在emacs lisp中,各种树结构很常见.custom.el通过:type参数提供了defcustom一种标准方法来定义自定义变量的预期形状.但有没有一种标准的方法来验证一些随机emacs lisp值的结构?

可以说,我有一份表格清单

LIST = (ENTRY ...)
ENTRY = (NAME . ((1 VAL1) (2 VAL2) ...))
Run Code Online (Sandbox Code Playgroud)

我能以某种方式定义类似于自定义类型的结构,然后检查该结构定义吗?

sds*_*sds 5

在文件中lisp/wid-edit.el有这个功能:

(defun widget-type-match (widget value)
  "Non-nil if the :type value of WIDGET matches VALUE.

The value of the :type attribute should be an unconverted widget type."
  (widget-apply (widget-convert (widget-get widget :type)) :match value))
Run Code Online (Sandbox Code Playgroud)

你可以适应你的需求:

(defun my-type-match (type value)
  (widget-apply (widget-convert type) :match value))

(my-type-match 'string "foo")
==> t
(my-type-match 'string 10)
==> nil
(my-type-match '(choice (const 1) (const 2) (const t)) 10)
==> nil
(my-type-match '(choice (const 1) (const 2) (const t)) 2)
==> t
Run Code Online (Sandbox Code Playgroud)