美文网首页
tree-sitter 实现学习

tree-sitter 实现学习

作者: onedam | 来源:发表于2021-06-08 14:00 被阅读0次

tree-sitter 用 js 格式来描述 上下文无关语法. 在早期版本使用 c++ 来根据 json来生成解析器 parser.c .
这个生成后, 可以直接用该文件独立的解析语法了. 但要增量解析. 需要用该parser.c 来结合 lib runtimec
.

2018 8 26 加入了这个原理性文档. 目前(2021年6月8日) 生成解析parser.c 使用rust替换了cpp .
Tree-sitter consists of two separate libraries, both of which expose C APIs.

The first library, libcompiler, is used to generate a parser for a language by supplying a context-free grammar describing the language. libcompiler is a build tool; it is no longer needed once a parser has been generated. It is written in C++, but exposes a C interface, which is declared in the header file compiler.h.

The second library, libruntime, is used in combination with the parsers generated by libcompiler, to produce syntax trees from source code and keep the syntax trees up-to-date as the source code changes. libruntime is designed to be embedded in applications. It is written in plain C. Its interface is specified in the header file runtime.h.

Tree sitter由两个独立的库组成,这两个库都公开了C API。

第一个库libcompiler通过提供描述语言的上下文无关语法来生成语言的解析器。libcompiler是一个构建工具;一旦生成了解析器,就不再需要它了。它是用C++编写的,但它公开了一个C接口,它在头文件编译器h中声明。

第二个库libruntime与libcompiler生成的解析器结合使用,从源代码生成语法树,并在源代码更改时使语法树保持最新。libruntime被设计成嵌入到应用程序中。它是用普通的C写的。它的接口在头文件runtime.h中指定。

image.png
image.png
image.png

相关文章

网友评论

      本文标题:tree-sitter 实现学习

      本文链接:https://www.haomeiwen.com/subject/oxvdeltx.html