https://github.com/alextsao1999/ast-buffer
Fast incremental parsing using piece table and tree-sitter to generate syntax tree
https://github.com/alextsao1999/ast-buffer
ast buffer data-structures piece-table text text-buffer text-editor tree-sitter tree-sitter-cpp
Last synced: 7 months ago
JSON representation
Fast incremental parsing using piece table and tree-sitter to generate syntax tree
- Host: GitHub
- URL: https://github.com/alextsao1999/ast-buffer
- Owner: alextsao1999
- License: mit
- Created: 2020-05-07T12:59:40.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-05-12T14:52:02.000Z (over 5 years ago)
- Last Synced: 2024-10-23T20:15:44.552Z (12 months ago)
- Topics: ast, buffer, data-structures, piece-table, text, text-buffer, text-editor, tree-sitter, tree-sitter-cpp
- Language: C
- Homepage:
- Size: 600 KB
- Stars: 13
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ASTBuffer
Fast incremental parsing using tree-sitter and piece table使用tree-sitter+piece-table实现快速增量解析 并生成ast
piece-table 测试了一下300MB左右的文件5000行中插入数据需要48ms
* 主要是给我的code editor改进一下储存结构 🤭
* 如果喜欢的话或者可以给您的editor加成个 [LSP](https://github.com/alextsao1999/lsp-cpp) ~## Usage
```c++
#include
#include
int main() {
ASTBuffer ast(ts::Language::cpp());
ast.parser().set_timeout(500);
//size_t position = 50000;
//ast.parser().set_cancel_position(&position);
//Origin origin("your/file/path/xxxx.txt");
//ast.append_origin((char *) origin.ptr(), origin.size());ast.append("int main(int abc) {\n");
ast.append(" auto *str = \"asdf\";\n");
ast.append(" *str = test();\n");
ast.append(" return 0;\n");
ast.append("}\n");
ast.append("test(get(\"1234\"));\n");
//std::cout << ast.tree().root().string();
ast.append("int add(int x, int y) {return x + y;}\n");
ast.dump();auto query = ts::Language::cpp().query(
"(number_literal) @number"
"(string_literal) @string"
"(call_expression function: (identifier) @fun-call)"
"(binary_expression left: (identifier) @left right: (identifier) @right)");auto cursor = query.exec(ast.tree().root());
uint32_t index;
while (cursor.next_capture(index)) {
auto node = cursor.capture_node(index);
auto name = cursor.capture_name(index);
std::cout <<"capture:" << node.string() << " " << name << " -> " << ast.node_string(node) << std::endl;
}
return 0;
}
```