#org #parser #incremental

tree-sitter-org

tree-sitter 解析库的 org 语法

4 个稳定版本

1.3.3 2022 年 10 月 27 日
1.3.2 2022 年 9 月 12 日
1.3.1 2022 年 8 月 16 日
1.3.0 2022 年 8 月 12 日

#219 in 文本编辑器

Download history 11/week @ 2024-03-07 6/week @ 2024-03-14 17/week @ 2024-03-28 11/week @ 2024-04-04 55/week @ 2024-04-11 9/week @ 2024-04-18 4/week @ 2024-04-25

69 每月下载量
cbfmt 中使用

MIT 许可证

2.5MB
95K SLoC

C 94K SLoC JavaScript 306 SLoC // 0.0% comments C++ 239 SLoC // 0.0% comments Scheme 59 SLoC // 0.3% comments Rust 42 SLoC // 0.2% comments

tree-sitter-org

tree-sitter 的 org 语法。在这里,目标是实现一个语法,可以有效地解析 org 文件,以便在所有使用 tree-sitter 解析器的库中使用。它并不旨在精确实现 emacs 的 orgmode 解析器,后者本质上比 tree-sitter 容易允许的更动态。

概述

本节旨在作为快速参考,而不是详尽描述。请参阅 corpus 中的测试用例。

  • 顶级节点:(document)
  • 文档包含:(directive)* (body)? (section)*
  • 部分包含:(headline) (plan)? (property_drawer)? (body)?
  • 标题包含:((stars), (item)?, (tag_list)?)
  • 正文包含:(element)+
  • 元素包含:(directive)* choose(paragraph, drawer, comment, footnote def, list, block, dynamic block, table) 或裸的 (directive)
  • 段落包含:(expr)+
  • 表达式包含:'str'、'num'、'sym' 和任何非字母数字的 ascii 符号的匿名节点。(有关详细信息,请参阅 grammar.js 的顶部和查询。)

与许多正则表达式系统一样,*/+ 表示 "0 或多次",而 ? 表示 0 或 1。

示例

#+TITLE: Example

Some *marked up* words

* TODO Title
<2020-06-07 Sun>

  - list a
  - [-] list a
    - [ ] list b
    - [x] list b
  - list a

** Subsection :tag:

Text

解析为

(document [0, 0] - [16, 0]
  body: (body [0, 0] - [4, 0]
    directive: (directive [0, 0] - [1, 0]
      name: (expr [0, 2] - [0, 7])
      value: (value [0, 9] - [0, 16]
        (expr [0, 9] - [0, 16])))
    (paragraph [2, 0] - [3, 0]
      (expr [2, 0] - [2, 4])
      (expr [2, 5] - [2, 12])
      (expr [2, 13] - [2, 16])
      (expr [2, 17] - [2, 22])))
  subsection: (section [4, 0] - [16, 0]
    headline: (headline [4, 0] - [5, 0]
      stars: (stars [4, 0] - [4, 1])
      item: (item [4, 2] - [4, 12]
        (expr [4, 2] - [4, 6])
        (expr [4, 7] - [4, 12])))
    plan: (plan [5, 0] - [6, 0]
      (entry [5, 0] - [5, 16]
        timestamp: (timestamp [5, 0] - [5, 16]
          date: (date [5, 1] - [5, 11])
          day: (day [5, 12] - [5, 15]))))
    body: (body [6, 0] - [13, 0]
      (list [7, 0] - [12, 0]
        (listitem [7, 2] - [8, 0]
          bullet: (bullet [7, 2] - [7, 3])
          contents: (paragraph [7, 4] - [8, 0]
            (expr [7, 4] - [7, 8])
            (expr [7, 9] - [7, 10])))
        (listitem [8, 2] - [11, 0]
          bullet: (bullet [8, 2] - [8, 3])
          checkbox: (checkbox [8, 4] - [8, 7]
            status: (expr [8, 5] - [8, 6]))
          contents: (paragraph [8, 8] - [9, 0]
            (expr [8, 8] - [8, 12])
            (expr [8, 13] - [8, 14]))
          contents: (list [9, 0] - [11, 0]
            (listitem [9, 4] - [10, 0]
              bullet: (bullet [9, 4] - [9, 5])
              checkbox: (checkbox [9, 6] - [9, 9])
              contents: (paragraph [9, 10] - [10, 0]
                (expr [9, 10] - [9, 14])
                (expr [9, 15] - [9, 16])))
            (listitem [10, 4] - [11, 0]
              bullet: (bullet [10, 4] - [10, 5])
              checkbox: (checkbox [10, 6] - [10, 9]
                status: (expr [10, 7] - [10, 8]))
              contents: (paragraph [10, 10] - [11, 0]
                (expr [10, 10] - [10, 14])
                (expr [10, 15] - [10, 16])))))
        (listitem [11, 2] - [12, 0]
          bullet: (bullet [11, 2] - [11, 3])
          contents: (paragraph [11, 4] - [12, 0]
            (expr [11, 4] - [11, 8])
            (expr [11, 9] - [11, 10])))))
    subsection: (section [13, 0] - [16, 0]
      headline: (headline [13, 0] - [14, 0]
        stars: (stars [13, 0] - [13, 2])
        item: (item [13, 3] - [13, 13]
          (expr [13, 3] - [13, 13]))
        tags: (tag_list [13, 14] - [13, 19]
          tag: (tag [13, 15] - [13, 18])))
      body: (body [14, 0] - [16, 0]
        (paragraph [15, 0] - [16, 0]
          (expr [15, 0] - [15, 4]))))))

安装

对于手动安装,使用 make

对于 neovim,使用 nvim-treesitter/nvim-treesitter,添加到您的配置中

local parser_config = require "nvim-treesitter.parsers".get_parser_configs()
parser_config.org = {
  install_info = {
    url = 'https://github.com/milisims/tree-sitter-org',
    revision = 'main',
    files = { 'src/parser.c', 'src/scanner.cc' },
  },
  filetype = 'org',
}

使用 npm 构建解析器并运行测试

  1. 按照 tree-sitter 文档 中所述安装 node.js
  2. 克隆此存储库:git clone https://github.com/milisims/tree-sitter-orgcd 进入它
  3. 使用 npm 安装 tree-sitter:npm install
  4. 运行测试:./node_modules/.bin/tree-sitter generate && ./node_modules/.bin/tree-sitter test

依赖项

~2.8–4MB
~72K SLoC