【问题标题】:What is the best YAML parser in elisp?elisp 中最好的 YAML 解析器是什么?
【发布时间】:2012-04-10 17:09:36
【问题描述】:

我想用 elisp 代码读取 YAML 中的配置。搜索但未在 elisp 中找到现成的解析器。我错过了什么有用的东西吗?

【问题讨论】:

  • 我不知道答案,但如果没有,那么您可以调用其他解析器来读取 yaml 并让它输出一个文本 lisp 表示,您可以使用 read 读取它.
  • 谢谢!这是个好主意。也许我可以试试pymacs
  • 我不确定你的意思 - 这里有一个 yaml 模式:github.com/yoshiki/yaml-mode - 但你想让解析器读取 yaml 并用它做什么?
  • 我想从 YAML 文件中读取一些配置。

标签: emacs elisp yaml


【解决方案1】:

三年后,我们有了dynamic modules,而emacs-libyaml 看起来很有趣。它使用动态模块系统在 Elisp 中公开 libyaml 的 C 绑定。虽然我还没有测试过,但我预计性能会很棒。

【讨论】:

    【解决方案2】:

    6 个月后,答案似乎是 “不存在可靠且易于使用的 elisp YAML 解析器。”

    如果您真的想在 elisp 中读取 YAML 文档并将其转换为 elisp 可以与之交互的内容,您将不得不进行一些繁琐的工作。 EmacsWiki YAML page 对你来说没什么用,规范的 YAML mode 有语法提示,但没有实际的解析器。幸运的是,有人有implemented a YAML-parsing web-app,它接受 YAML 并输出 JSON 或 Python - 您可以尝试深入了解它,或者使用它来检查您自己编写的任何 YAML 解析器。

    祝你好运。

    【讨论】:

    • 附录:在 elisp 中编写自己的递归下降解析器时,请参阅 this SO answer 了解一些入门代码。
    • edward.oconnor.cx/2006/03/json.el 也可能对比较和想法有用。显然,YAML 不是 JSON,但它们肯定有一些相似之处。
    【解决方案3】:

    几个月后:我想要它,所以这里是如何在 python 的帮助下做到这一点:

       (defun yaml-parse ()
      "yaml to json to a hashmap of current buffer, with python.
    
       There is no yaml parser in elisp.
       You need pyYaml and some yaml datatypes like dates are not supported by json."
      (interactive)
      (let ((json-object-type 'hash-table))
        (setq  myyaml (json-read-from-string (shell-command-to-string (concat "python -c 'import sys, yaml, json; json.dump(yaml.load(sys.stdin), sys.stdout, indent=4)' < " (buffer-file-name))))))
      ;; code here
      )
    

    json.el的帮助下,它将当前缓冲区的yaml转换为elisp hashmap。

    您需要 python 的 pyyaml:pip install PyYaml

    json.el:http://edward.oconnor.cx/2006/03/json.el

    【讨论】:

    • 我的意思是一方面这是一个让我畏缩的黑客,但另一方面,我不能让自己不尊重“我需要的工具不存在,所以我会自己写。”赞成。
    【解决方案4】:

    三年后,我很高兴地说,现在有一个用 Elisp 编写的 YAML 解析器:https://melpa.org/#/yaml

    它的API类似于json-parse-string,你可以指定它的对象和列表类型。以下是其用法示例:

    (yaml-parse-string "
    -
      \"flow in block\"
    - >
     Block scalar
    - !!map # Block collection
      foo : bar" :object-type 'alist)
    ;; => ["flow in block" "Block scalar\n" (("foo" . "bar"))]
    

    【讨论】:

      【解决方案5】:

      使用 python 改进 Ehvince 答案,这是一种更通用的方法,允许解析字符串、缓冲区和文件:

      (defun yaml-parse (string)
        "yaml STRING to json to a hashmap of current buffer, with python."
        (interactive)
        (with-temp-buffer
          (insert string)
          (when (zerop (call-process-region (point-min) (point-max) "python" t t nil "-c" "import sys, yaml, json; json.dump(yaml.load(sys.stdin), sys.stdout)"))
            (goto-char (point-min))
            (json-read))))
      
      (defun yaml-parse-buffer (&optional buffer)
        "Parse yaml BUFFER."
        (with-current-buffer (or buffer (current-buffer))
          (yaml-parse (buffer-substring-no-properties (point-min) (point-max)))))
      
      (defun yaml-parse-file (file)
        "Parse yaml FILE."
        (with-temp-buffer
          (insert-file-contents-literally file)
          (yaml-parse (buffer-substring-no-properties (point-min) (point-max)))))
      

      您可以使用 json-* 变量来控制类型映射。

      编辑:添加 yaml-parse-file

      【讨论】:

      • 尝试将其与来自 symfony 项目 ix.io/2emU/yaml 的示例 yaml 文件一起使用。不去。调试器进入--Lisp错误:(json-number-format 2) signal(json-number-format(2)) json-read-number(t) json-read-number() json-read()
      • @RichieHH:那个 yaml 对我来说看起来无效。那些 \: 是什么?
      • 键的一部分,它是一个路径。我不是专家。 symfony 包附带的 YAML 文件。
      猜你喜欢
      • 2010-10-03
      • 1970-01-01
      • 1970-01-01
      • 2011-03-08
      • 2011-04-12
      • 2018-03-23
      • 1970-01-01
      • 2011-03-04
      相关资源
      最近更新 更多