Skip to content

Getting started

A parser needs the Logrotate grammar and a Tree-sitter runtime. The two objects are separate so an application can load several grammars into one runtime.

Install the grammar with the compatible Node runtime:

Terminal window
npm install tree-sitter-logrotate tree-sitter@^0.25.1

Create a parser, set its language, and parse text:

import Parser from "tree-sitter";
import Logrotate from "tree-sitter-logrotate";
const parser = new Parser();
parser.setLanguage(Logrotate);
const source = `/var/log/application.log {
daily
rotate 7
}`;
const tree = parser.parse(source);
console.log(tree.rootNode.toString());
console.log(tree.rootNode.hasError);

hasError is false when the complete input matches the grammar. The parser still returns a tree for incomplete input, which allows editors to keep working while a file is being changed.

The core extra installs a compatible Python Tree-sitter runtime with the grammar:

Terminal window
python -m pip install "tree-sitter-logrotate[core]"
from tree_sitter import Language, Parser
from tree_sitter_logrotate import language
parser = Parser(Language(language()))
source = b"/var/log/application.log {\n daily\n rotate 7\n}\n"
tree = parser.parse(source)
print(tree.root_node)
print(tree.root_node.has_error)

The Python binding exposes a capsule through language(). Wrap that capsule in tree_sitter.Language before passing it to Parser.

Install web-tree-sitter and tree-sitter-logrotate, then serve both WASM files with the application:

Terminal window
npm install web-tree-sitter tree-sitter-logrotate
import { Language, Parser } from "web-tree-sitter";
await Parser.init({
locateFile: () => "/web-tree-sitter.wasm",
});
const logrotate = await Language.load("/tree-sitter-logrotate.wasm");
const parser = new Parser();
parser.setLanguage(logrotate);
const tree = parser.parse("/var/log/application.log {\n daily\n}\n");
console.log(tree.rootNode.toString());

Copy web-tree-sitter.wasm from the web-tree-sitter package and tree-sitter-logrotate.wasm from the grammar package into the application’s public assets. The browser build and Node build use the same public syntax tree.

The example produces a source_file containing one rotation_block. The block has a paths field and repeated body fields for its directives. See Syntax tree for the named nodes, fields, and recovery behavior that consumers can rely on.