Compare commits

...

12 commits

Author SHA1 Message Date
Bruno BELANYI 1cd5fea469 fixup! Add NeoVim test runner 2024-04-11 23:30:16 +01:00
Bruno BELANYI 8b80b7045a Fix indentation in test files 2024-04-11 22:09:42 +01:00
Bruno BELANYI c73eadd348 Add NeoVim test runner
For the queries that can't be tested with `tree-sitter` itself.
2024-04-11 22:09:42 +01:00
Bruno BELANYI cb932811c5 Add 'comment' injection 2024-04-11 19:04:09 +01:00
Bruno BELANYI cc81c286d4 Add folding query 2024-04-11 18:58:33 +01:00
Bruno BELANYI 9457caf3a2 Fix typo in a comment 2024-04-11 18:44:13 +01:00
Bruno BELANYI adec624d74 Tweak comment parsing
This is taken verbatim from tree-sitter-c [1], just in case :-).

[1]: https://github.com/tree-sitter/tree-sitter-c
2024-04-11 18:38:14 +01:00
Bruno BELANYI b62fc19da9 Make comments a single node in the grammar
This looks to be a *strong* idiom in tree-sitter parsers.
2024-04-11 18:36:20 +01:00
Bruno BELANYI aa8472e73f Add test for literals 2024-04-11 18:08:40 +01:00
Bruno BELANYI 508ea00920 Add missing 'PHONY' target 2024-04-11 18:01:32 +01:00
Bruno BELANYI dc1d139d35 Fix 'PHONY' target name 2024-04-11 18:00:12 +01:00
Bruno BELANYI ebb642cb14 Rename to 'tree-sitter-bp'
All checks were successful
ci/woodpecker/push/check Pipeline was successful
This is really just to simplify my life and align with the Vim/NeoVim
filetype name.
2024-04-10 15:32:37 +00:00
23 changed files with 1339 additions and 1208 deletions

2
.gitignore vendored
View file

@ -3,6 +3,6 @@
/.pre-commit-config.yaml
# Tree-sitter artifact
/tree-sitter-blueprint.wasm
/tree-sitter-bp.wasm
# Rust bindings
/target

View file

@ -1,10 +1,10 @@
[package]
name = "tree-sitter-blueprint"
name = "tree-sitter-bp"
description = "Blueprint grammar for the tree-sitter parsing library"
version = "0.1.0"
keywords = ["incremental", "parsing", "android", "blueprint"]
categories = ["parsing", "text-editors"]
repository = "https://git.belanyi.fr/ambroisie/tree-sitter-blueprint"
repository = "https://git.belanyi.fr/ambroisie/tree-sitter-bp"
edition = "2018"
license = "MIT"

View file

@ -1,15 +1,18 @@
.PHONE: all
.PHONY: all
all:
tree-sitter generate
.PHONE: test
.PHONY: test
test: all
tree-sitter test --apply-all-captures
nvim-test-runner
.PHONE: update-tests
.PHONY: update-tests
update-tests: all
tree-sitter test -u --apply-all-captures
nvim-test-runner
.PHONY: playground
playground:
nix shell pkgs#emscripten --command tree-sitter build-wasm
tree-sitter playground

View file

@ -1,4 +1,4 @@
# tree-sitter-blueprint
# tree-sitter-bp
Tree-sitter grammar for [Blueprint][blueprint-aosp], the meta-build system used
in AOSP for its `Android.bp` files.

View file

@ -1,7 +1,7 @@
{
"targets": [
{
"target_name": "tree_sitter_blueprint_binding",
"target_name": "tree_sitter_bp_binding",
"include_dirs": [
"<!(node -e \"require('nan')\")",
"src"

View file

@ -4,7 +4,7 @@
using namespace v8;
extern "C" TSLanguage * tree_sitter_blueprint();
extern "C" TSLanguage * tree_sitter_bp();
namespace {
@ -17,12 +17,12 @@ void Init(Local<Object> exports, Local<Object> module) {
Local<Function> constructor = Nan::GetFunction(tpl).ToLocalChecked();
Local<Object> instance = constructor->NewInstance(Nan::GetCurrentContext()).ToLocalChecked();
Nan::SetInternalFieldPointer(instance, 0, tree_sitter_blueprint());
Nan::SetInternalFieldPointer(instance, 0, tree_sitter_bp());
Nan::Set(instance, Nan::New("name").ToLocalChecked(), Nan::New("blueprint").ToLocalChecked());
Nan::Set(instance, Nan::New("name").ToLocalChecked(), Nan::New("bp").ToLocalChecked());
Nan::Set(module, Nan::New("exports").ToLocalChecked(), instance);
}
NODE_MODULE(tree_sitter_blueprint_binding, Init)
NODE_MODULE(tree_sitter_bp_binding, Init)
} // namespace

View file

@ -1,11 +1,11 @@
try {
module.exports = require("../../build/Release/tree_sitter_blueprint_binding");
module.exports = require("../../build/Release/tree_sitter_bp_binding");
} catch (error1) {
if (error1.code !== 'MODULE_NOT_FOUND') {
throw error1;
}
try {
module.exports = require("../../build/Debug/tree_sitter_blueprint_binding");
module.exports = require("../../build/Debug/tree_sitter_bp_binding");
} catch (error2) {
if (error2.code !== 'MODULE_NOT_FOUND') {
throw error2;

View file

@ -1,4 +1,4 @@
//! This crate provides blueprint support for the [tree-sitter][] parsing library.
//! This crate provides bp support for the [tree-sitter][] parsing library.
//!
//! Typically, you will use the [language][language func] function to add this language to a
//! tree-sitter [Parser][], and then use the parser to parse some code:
@ -6,7 +6,7 @@
//! ```
//! let code = "";
//! let mut parser = tree_sitter::Parser::new();
//! parser.set_language(tree_sitter_blueprint::language()).expect("Error loading txtpb grammar");
//! parser.set_language(tree_sitter_bp::language()).expect("Error loading txtpb grammar");
//! let tree = parser.parse(code, None).unwrap();
//! ```
//!
@ -18,14 +18,14 @@
use tree_sitter::Language;
extern "C" {
fn tree_sitter_blueprint() -> Language;
fn tree_sitter_bp() -> Language;
}
/// Get the tree-sitter [Language][] for this grammar.
///
/// [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
pub fn language() -> Language {
unsafe { tree_sitter_blueprint() }
unsafe { tree_sitter_bp() }
}
/// The content of the [`node-types.json`][] file for this grammar.
@ -47,6 +47,6 @@ mod tests {
let mut parser = tree_sitter::Parser::new();
parser
.set_language(super::language())
.expect("Error loading blueprint language");
.expect("Error loading bp language");
}
}

View file

@ -37,6 +37,7 @@
}:
let
inherit (flake-utils.lib) eachDefaultSystem;
inherit (nixpkgs) lib;
in
eachDefaultSystem
(system:
@ -46,6 +47,25 @@
overlays = [ self.overlays.default ];
};
nvim-test-runner = pkgs.writeShellApplication {
name = "nvim-test-runner";
runtimeInputs = with pkgs; [
highlight-assertions
neovim
];
text = ''
export NVIM_PLENARY='${pkgs.vimPlugins.plenary-nvim}'
export NVIM_TREESITTER='${pkgs.vimPlugins.nvim-treesitter}'
export NVIM_TREESITTER_TEXTOBJECTS='${pkgs.vimPlugins.nvim-treesitter-textobjects}'
export NVIM_TREESITTER_PARSER='${pkgs.vimPlugins.nvim-treesitter.grammarToPlugin self.packages.${system}.tree-sitter-bp}'
nvim --headless --noplugin -u ${scripts/minimal_init.lua} \
-c "PlenaryBustedDirectory test/ { minimal_init = '${./scripts/minimal_init.lua}' }"
'';
};
bump-version = pkgs.writeShellScriptBin "bump-version" ''
set -eu
@ -90,6 +110,13 @@
enable = true;
};
nvim-test-runner = {
enable = true;
name = "nvim tests";
entry = "${lib.getExe nvim-test-runner}";
pass_filenames = false;
};
tree-sitter = {
enable = true;
name = "tree-sitter tests";
@ -112,6 +139,7 @@
nativeBuildInputs = with pkgs; [
bump-version
nodejs
nvim-test-runner
# FIXME: waiting on #301336
# (tree-sitter.override { webUISupport = true; })
tree-sitter
@ -122,9 +150,9 @@
};
packages = {
default = packages.tree-sitter-blueprint;
default = packages.tree-sitter-bp;
inherit (pkgs.tree-sitter.passthru.builtGrammars) tree-sitter-blueprint;
inherit (pkgs.tree-sitter.passthru.builtGrammars) tree-sitter-bp;
inherit (pkgs) tree-sitter;
};
@ -133,7 +161,7 @@
default = final: prev: {
tree-sitter = prev.tree-sitter.override {
extraGrammars = {
tree-sitter-blueprint = {
tree-sitter-bp = {
src = ./.;
};
};

View file

@ -7,12 +7,11 @@ function trailingCommaSeparated(elem) {
}
module.exports = grammar({
name: "blueprint",
name: "bp",
extras: ($) => [
/\s+/,
$.line_comment,
$.block_comment,
$.comment,
],
rules: {
@ -23,9 +22,10 @@ module.exports = grammar({
$.module,
),
line_comment: (_) => seq("//", /[^\n]*/),
block_comment: (_) => seq("/*", /[^*]*\*+([^/*][^*]*\*+)*/, '/'),
comment: (_) => choice(
seq("//", /(\\+(.|\r?\n)|[^\\\n])*/),
seq("/*", /[^*]*\*+([^/*][^*]*\*+)*/, "/"),
),
// Definitions {{{

View file

@ -1,5 +1,5 @@
{
"name": "tree-sitter-blueprint",
"name": "tree-sitter-bp",
"version": "0.1.0",
"description": "Blueprint grammar for tree-sitter",
"main": "bindings/node",
@ -18,7 +18,7 @@
},
"tree-sitter": [
{
"scope": "source.blueprint",
"scope": "source.bp",
"file-types": [
"bp"
],

7
queries/folds.scm Normal file
View file

@ -0,0 +1,7 @@
[
(list_expression)
(map_expression)
(module)
] @fold
; vim: sw=2 foldmethod=marker

View file

@ -1,7 +1,4 @@
[
(line_comment)
(block_comment)
] @comment
(comment) @comment
; Operators {{{
(operator) @operator

22
queries/indents.scm Normal file
View file

@ -0,0 +1,22 @@
; Expressions {{{
(list_expression) @indent.begin
(list_expression
"]" @indent.branch)
(map_expression) @indent.begin
(map_expression
"}" @indent.end)
(assignment) @indent.begin ; FIXME: do I need it?
; }}}
; Declarations {{{
(module) @indent.begin
(module
")" @indent.end)
(module
"}" @indent.end)
; }}}

4
queries/injections.scm Normal file
View file

@ -0,0 +1,4 @@
((comment) @injection.content
(#set! injection.language "comment"))
; vim: sw=2 foldmethod=marker

43
scripts/minimal_init.lua Normal file
View file

@ -0,0 +1,43 @@
vim.opt.runtimepath:append(vim.env.NVIM_PLENARY)
vim.opt.runtimepath:append(vim.env.NVIM_TREESITTER)
vim.opt.runtimepath:append(vim.env.NVIM_TREESITTER_TEXTOBJECTS)
vim.opt.runtimepath:append(vim.env.NVIM_TREESITTER_PARSER)
vim.cmd.runtime { "plugin/plenary.vim", bang = true }
vim.cmd.runtime { "plugin/nvim-treesitter.lua", bang = true }
vim.cmd.runtime { "plugin/nvim-treesitter-textobjects.lua", bang = true }
local Path = require("plenary.path")
local project_root = Path:new(".") -- FIXME: relies on current working directory
vim.filetype.add({
extension = {
bp = "bp",
},
})
vim.o.swapfile = false
vim.bo.swapfile = false
local parser_config = require("nvim-treesitter.parsers").get_parser_configs()
parser_config.bp = {
install_info = {
url = project_root.filename,
files = {"src/parser.c"},
},
}
require("nvim-treesitter.configs").setup({
indent = { enable = true },
highlight = { enable = true },
})
-- Add queries, overriding potential `nvim-treesitter`-provided ones
local scan_dir = require("plenary.scandir").scan_dir
local queries_dir = project_root / "queries"
for _, name in ipairs(scan_dir(queries_dir.filename)) do
local content = Path:new(name):read()
local basename = name:gsub(".*/", "")
vim.treesitter.query.set("bp", basename:gsub(".scm$", ""), content)
end

59
src/grammar.json generated
View file

@ -1,5 +1,5 @@
{
"name": "blueprint",
"name": "bp",
"rules": {
"source_file": {
"type": "REPEAT",
@ -21,33 +21,38 @@
}
]
},
"line_comment": {
"type": "SEQ",
"comment": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "//"
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "//"
},
{
"type": "PATTERN",
"value": "(\\\\+(.|\\r?\\n)|[^\\\\\\n])*"
}
]
},
{
"type": "PATTERN",
"value": "[^\\n]*"
}
]
},
"block_comment": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "/*"
},
{
"type": "PATTERN",
"value": "[^*]*\\*+([^/*][^*]*\\*+)*"
},
{
"type": "STRING",
"value": "/"
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "/*"
},
{
"type": "PATTERN",
"value": "[^*]*\\*+([^/*][^*]*\\*+)*"
},
{
"type": "STRING",
"value": "/"
}
]
}
]
},
@ -956,11 +961,7 @@
},
{
"type": "SYMBOL",
"name": "line_comment"
},
{
"type": "SYMBOL",
"name": "block_comment"
"name": "comment"
}
],
"conflicts": [],

9
src/node-types.json generated
View file

@ -168,12 +168,12 @@
}
},
{
"type": "block_comment",
"type": "boolean_literal",
"named": true,
"fields": {}
},
{
"type": "boolean_literal",
"type": "comment",
"named": true,
"fields": {}
},
@ -259,11 +259,6 @@
]
}
},
{
"type": "line_comment",
"named": true,
"fields": {}
},
{
"type": "list_expression",
"named": true,

2126
src/parser.c generated

File diff suppressed because it is too large Load diff

View file

@ -7,7 +7,7 @@ Empty comment
--------------------------------------------------------------------------------
(source_file
(line_comment))
(comment))
================================================================================
Single comment
@ -18,7 +18,7 @@ Single comment
--------------------------------------------------------------------------------
(source_file
(line_comment))
(comment))
================================================================================
Multiple comments
@ -30,8 +30,8 @@ Multiple comments
--------------------------------------------------------------------------------
(source_file
(line_comment)
(line_comment))
(comment)
(comment))
================================================================================
Empty block comment
@ -42,7 +42,7 @@ Empty block comment
--------------------------------------------------------------------------------
(source_file
(block_comment))
(comment))
================================================================================
Whitespace block comment
@ -53,7 +53,7 @@ Whitespace block comment
--------------------------------------------------------------------------------
(source_file
(block_comment))
(comment))
================================================================================
Block comment
@ -64,7 +64,7 @@ Block comment
--------------------------------------------------------------------------------
(source_file
(block_comment))
(comment))
================================================================================
Block comment with slashes
@ -75,7 +75,7 @@ Block comment with slashes
--------------------------------------------------------------------------------
(source_file
(block_comment))
(comment))
================================================================================
Block comment with asterisks
@ -86,7 +86,7 @@ Block comment with asterisks
--------------------------------------------------------------------------------
(source_file
(block_comment))
(comment))
================================================================================
Block comment (multiline)
@ -103,7 +103,7 @@ Block comment (multiline)
--------------------------------------------------------------------------------
(source_file
(block_comment))
(comment))
================================================================================
Block comment is not recursive
@ -114,7 +114,7 @@ Block comment is not recursive
--------------------------------------------------------------------------------
(source_file
(block_comment))
(comment))
================================================================================
Unterminated comment

134
test/corpus/literals.txt Normal file
View file

@ -0,0 +1,134 @@
================================================================================
Booelan literal
================================================================================
foo = true
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(operator)
(boolean_literal)))
================================================================================
Integer literal
================================================================================
foo = 42
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(operator)
(integer_literal)))
================================================================================
String literal
================================================================================
foo = "Hello World!"
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(operator)
(interpreted_string_literal)))
================================================================================
String literal special character escapes
================================================================================
foo = "Hello\nWorld!"
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(operator)
(interpreted_string_literal
(escape_sequence))))
================================================================================
String literal octal
================================================================================
foo = "Hello World\041"
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(operator)
(interpreted_string_literal
(escape_sequence))))
================================================================================
String literal hex
================================================================================
foo = "Hello World\x21"
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(operator)
(interpreted_string_literal
(escape_sequence))))
================================================================================
String literal character escapes
================================================================================
foo = "Hello\\\"World\""
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(operator)
(interpreted_string_literal
(escape_sequence)
(escape_sequence)
(escape_sequence))))
================================================================================
Unterminated string literal
================================================================================
foo = "
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(operator)
(interpreted_string_literal
(MISSING """))))
================================================================================
String literal unterminated escape
================================================================================
foo = "\"
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(operator)
(interpreted_string_literal
(escape_sequence)
(MISSING """))))

View file

@ -1,10 +1,9 @@
/* This is comment */
/* <- comment
^ comment
^ comment
*/
/* This is a comment */
// <- comment
// ^ comment
// ^ comment
// And another comment
/* <- comment
^ comment
^ comment
*/
// <- comment
// ^ comment
// ^ comment

View file

@ -4,16 +4,16 @@ foo {}
foo ()
// <- function.call
foo {
// <- function.call
some_module {
// ^ function.call
field: 12,
// <- variable.parameter
another_field: 27,
// <- variable.parameter
}
foo (
// <- function.call
some_module (
// ^ function.call
field = 42,
// <- variable.parameter
done = false,