由tree-sitter生成的解析器可以同时用于语法突出显示和编译器本身吗?如果不是——为什么?
编写两个不同的解析器并维护它们会适得其反。
注意:我还没有使用过tree-sitter,但考虑使用它来突出显示我自己的编程语言的语法。因此,我可能会误解它的解析器实际上是如何工作的。
compiler-construction parsing syntax-highlighting treesitter
这可能与我不理解关键字提取功能有关,从文档来看,该功能似乎是为了避免关键字和以下表达式之间不存在空格的问题。但是假设我有一个相当标准的变量名、函数名等标识符正则表达式:
/\w*[A-Za-z]\w*/
如何防止它与保留关键字(例如IF
orELSE
或类似的东西)匹配?所以这个表达式会产生错误:
int IF = 5;
虽然这不会:
int x = 5;
在tree-sitter 中,语法从C 和C++ 编译为共享库。然后,这些库由 tree-sitter CLI(一个 Rust 程序)动态加载。树守护者 CLI 通常负责编译语法,尽管它没有-fsanitize
标志。我手动编译了我的语法-fsanitize=address
(最终想要添加undefined
)来帮助调试我作为外部扫描器的一部分编写的一些 C++ 代码。然而,当我尝试使用 tree-sitter CLI rust 程序加载它时,我得到:
打开动态库“~/.cache/tree-sitter/lib/tlaplus.so”时出错
引起:~/.cache/tree-sitter/lib/tlaplus.so:未定义符号:__asan_report_store4
所以 Rust 没有加载 ASAN。我尝试使用 nightly rust 工具链重建树守护者 CLI,并且:
RUSTFLAGS="-Z sanitizer=address" cargo build --target x86_64-unknown-linux-gnu --release
Run Code Online (Sandbox Code Playgroud)
但这仍然给出相同的未定义符号错误。有没有办法从 rust 加载使用 ASAN 编译的 C++ 库,或者我应该将自己的 C++ 前端编写到语法中?
英语不好(抱歉)。我试图让 nvim_treesitter 在我的 Windows 机器上工作,在我的 Linux 机器上工作得很好,但现在当我在 Windows 上尝试时, :checkhealth nvim_treesitter 给出:
tree-sitter
找不到可执行文件git
找到可执行文件。cc
找不到可执行文件。
图例: H[ighlight]、L[ocals]、F[olds]、I[ndents] *) 找到多个解析器,只会使用一个 x) 查询中发现错误,尝试运行 :TSUpdate {lang}
我对这件事完全陌生,任何建议都会有帮助:)
每次在 neovim 中打开文件时,我都会收到来自 treesister 配置的错误消息,并且语法突出显示不起作用。
在阅读 neovim API 文档后,我尝试了以下测试命令:
:lua vim.api.nvim_create_augroup("MyGroup", {clear = false})
Run Code Online (Sandbox Code Playgroud)
抛出错误:
E5108: Error executing lua [string ":lua"]:1: attempt to call field 'nvim_create_augroup' (a nil value)
stack traceback:
[string ":lua"]:1: in main chunk
Run Code Online (Sandbox Code Playgroud)
完整的错误消息是
Error detected while processing /home/strife/.local/share/nvim/plugged/nvim-treesitter/plugin/nvim-treesitter.lua:
E5113: Error while calling lua chunk: .../plugged/nvim-treesitter/lua/nvim-treesitter/configs.lua:104: attempt to call field 'nvim_create_augroup' (a
nil value)
stack traceback:
.../plugged/nvim-treesitter/lua/nvim-treesitter/configs.lua:104: in function 'enable_mod_conf_autocmd'
.../plugged/nvim-treesitter/lua/nvim-treesitter/configs.lua:449: in function 'define_modules'
.../plugged/nvim-treesitter/lua/nvim-treesitter/configs.lua:524: in function 'init'
...are/nvim/plugged/nvim-treesitter/lua/nvim-treesitter.lua:17: in function 'setup'
.../nvim/plugged/nvim-treesitter/plugin/nvim-treesitter.lua:9: in main chunk
Run Code Online (Sandbox Code Playgroud) 我正在尝试在我的 MBP 上设置 nvim,除了最后一步(即安装 nvim-treesitter)之外,一切都很顺利,在短暂的安装例程之后,它会抛出此错误:
nvim-treesitter[phpdoc]: Error during `npm install` (required for parser generation of phpdoc with npm dependencies)
npm WARN old lockfile
npm WARN old lockfile The package-lock.json file was created with an old version of npm,
npm WARN old lockfile so supplemental metadata must be fetched from the registry.
npm WARN old lockfile
npm WARN old lockfile This is a one-time fix-up, please be patient...
npm WARN old lockfile
npm ERR! code 1
npm ERR! path /Users/anton/.local/share/nvim/tree-sitter-phpdoc/node_modules/tree-sitter-cli
npm …
Run Code Online (Sandbox Code Playgroud) I am using NVIM v0.8.0-1210-gd367ed9b2 and I am trying to perform syntax highlighting for haskell in using treesitter. I would like the identifier for a function and its identifier for a function in a type definition to be highlighted differently, similar to how it is in VSCode. Currently, they are both highlighted as a function. If I perform :TSHighlightCaptureUnderCursor
over the identifer for a function type definition, it says it is a capture of @variable
, @function
, @type
. …
如果我在树保姆中有一个简单的语法:
rules: {
expr: $ => choice(
/[0-9]+/,
prec.right(seq($.expr, /[+-]/, $.expr)),
)
}
Run Code Online (Sandbox Code Playgroud)
和一个输入:
3+4
Run Code Online (Sandbox Code Playgroud)
我得到以下 CST:
(start [0, 0] - [0, 3]
(expr [0, 0] - [0, 3]
(expr [0, 0] - [0, 1])
(expr [0, 2] - [0, 3])))
Run Code Online (Sandbox Code Playgroud)
所以我的问题是,如何从这些节点/叶子中获取值,即解析的内容。我必须以某种方式评估这棵树。我当然确信有办法,因为我还可以使用树保姆进行语法突出显示,以满足我需要的值(我猜)。但我阅读了文档并找不到任何注释,如何操作。