Compare commits

...

15 commits
v0.4.0 ... main

Author SHA1 Message Date
ee641d1539 Release 0.7.0
All checks were successful
ci/woodpecker/push/check Pipeline was successful
ci/woodpecker/tag/check Pipeline was successful
2025-04-02 10:16:51 +00:00
e6473f64ae Add integer select pattern
All checks were successful
ci/woodpecker/push/check Pipeline was successful
2025-04-02 10:15:11 +00:00
2b7323eaf0 Test non-recursive comments
All checks were successful
ci/woodpecker/push/check Pipeline was successful
2025-03-24 12:07:55 +00:00
4c2a1d0012 Test negative integer literal
All checks were successful
ci/woodpecker/push/check Pipeline was successful
2025-03-12 21:33:34 +00:00
bb0d5d6a40 Release 0.6.0
All checks were successful
ci/woodpecker/push/check Pipeline was successful
ci/woodpecker/tag/check Pipeline was successful
2025-03-12 21:24:44 +00:00
f9dbeed40a Handle 'tree-sitter.json' in 'bump-version' 2025-03-12 21:23:50 +00:00
9f5e6b0141 Bump flake inputs
Remove deleted input, update generated files.
2025-03-12 21:23:50 +00:00
16c43068ec Align queries with 'nvim-treesitter'
All checks were successful
ci/woodpecker/push/check Pipeline was successful
Makes it easier to update either way.
2024-12-23 23:08:26 -05:00
aeb1fc2bb5 Allow optional trailing comma in 'select'
All checks were successful
ci/woodpecker/push/check Pipeline was successful
2024-12-23 22:59:05 -05:00
fd92a31674 Fix typos 2024-12-23 22:55:38 -05:00
4e60cf3c2e Release 0.5.0
All checks were successful
ci/woodpecker/push/check Pipeline was successful
ci/woodpecker/tag/check Pipeline was successful
2024-07-01 13:56:45 +00:00
6b0cdd9122 Add select binding locals query 2024-07-01 13:56:21 +00:00
1fd9b67dbe Add select binding 2024-07-01 13:54:40 +00:00
3e685cb691 Add 'any' highlighting 2024-07-01 13:50:43 +00:00
970241b323 Add 'any' select pattern 2024-07-01 13:50:30 +00:00
24 changed files with 2482 additions and 4511 deletions

View file

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

View file

@ -6,7 +6,7 @@
//! ```
//! let code = "";
//! let mut parser = tree_sitter::Parser::new();
//! parser.set_language(tree_sitter_bp::language()).expect("Error loading txtpb grammar");
//! parser.set_language(tree_sitter_bp::language()).expect("Error loading bp grammar");
//! let tree = parser.parse(code, None).unwrap();
//! ```
//!

24
flake.lock generated
View file

@ -21,11 +21,11 @@
"systems": "systems"
},
"locked": {
"lastModified": 1710146030,
"narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=",
"lastModified": 1731533236,
"narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a",
"rev": "11707dc2f618dd54ca8739b309ec4fc024de578b",
"type": "github"
},
"original": {
@ -58,11 +58,11 @@
},
"nixpkgs": {
"locked": {
"lastModified": 1713687659,
"narHash": "sha256-Yd8KuOBpZ0Slau/NxFhMPJI0gBxeax0vq/FD0rqKwuQ=",
"lastModified": 1741802033,
"narHash": "sha256-AOazEmkhbKnNQi0LqJdAcqPQx711L84a0Vkaos/ZW/A=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "f2d7a289c5a5ece8521dd082b81ac7e4a57c2c5c",
"rev": "8c1f3147639f009f09d2bfffc64bcf8485bf3fd2",
"type": "github"
},
"original": {
@ -75,23 +75,17 @@
"pre-commit-hooks": {
"inputs": {
"flake-compat": "flake-compat",
"flake-utils": [
"flake-utils"
],
"gitignore": "gitignore",
"nixpkgs": [
"nixpkgs"
],
"nixpkgs-stable": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1713775815,
"narHash": "sha256-Wu9cdYTnGQQwtT20QQMg7jzkANKQjwBD9iccfGKkfls=",
"lastModified": 1735882644,
"narHash": "sha256-3FZAG+pGt3OElQjesCAWeMkQ7C/nB1oTHLRQ8ceP110=",
"owner": "cachix",
"repo": "pre-commit-hooks.nix",
"rev": "2ac4dcbf55ed43f3be0bae15e181f08a57af24a4",
"rev": "a5a961387e75ae44cc20f0a57ae463da5e959656",
"type": "github"
},
"original": {

View file

@ -22,9 +22,7 @@
repo = "pre-commit-hooks.nix";
ref = "master";
inputs = {
flake-utils.follows = "flake-utils";
nixpkgs.follows = "nixpkgs";
nixpkgs-stable.follows = "nixpkgs";
};
};
};
@ -77,10 +75,14 @@
${pkgs.jq}/bin/jq ".version = \"''${NEW_VERSION}\"" package.json > package.json.tmp
mv package.json.tmp package.json
${pkgs.jq}/bin/jq ".metadata.version = \"''${NEW_VERSION}\"" tree-sitter.json > tree-sitter.json.tmp
mv tree-sitter.json.tmp tree-sitter.json
${pkgs.gnused}/bin/sed -i -e "s/version = \"[0-9.]\\+\"/version = \"''${NEW_VERSION}\"/" Cargo.toml
${pkgs.gnused}/bin/sed -i -e "s/version = \"[0-9.]\\+\"/version = \"''${NEW_VERSION}\"/" pyproject.toml
git add Cargo.toml package.json pyproject.toml
make
git add Cargo.toml package.json pyproject.toml tree-sitter.json src/parser.c
echo "Release ''${NEW_VERSION}" | git commit -eF -
git tag -a "v''${NEW_VERSION}" -m "Release ''${NEW_VERSION}"
'';

View file

@ -145,7 +145,7 @@ module.exports = grammar({
select_cases: ($) => seq(
"{",
optional(commaSeparatedTrailing($.select_case)),
optional(commaSeparatedOptTrailing($.select_case)),
"}",
),
@ -163,9 +163,18 @@ module.exports = grammar({
_select_pattern: ($) => choice(
$._string_literal,
$.boolean_literal,
$.integer_literal,
alias("any", $.any),
$.pattern_binding,
alias("default", $.default),
),
pattern_binding: ($) => seq(
field("value", alias("any", $.any)),
field("operator", alias("@", $.operator)),
field("binding", $.identifier),
),
_case_value: ($) => choice(
alias("unset", $.unset),
$._expr,

View file

@ -1,6 +1,6 @@
{
"name": "tree-sitter-bp",
"version": "0.4.0",
"version": "0.7.0",
"description": "Blueprint grammar for tree-sitter",
"main": "bindings/node",
"types": "bindings/node",

View file

@ -5,7 +5,7 @@ build-backend = "setuptools.build_meta"
[project]
name = "tree-sitter-bp"
description = "Bp grammar for tree-sitter"
version = "0.4.0"
version = "0.7.0"
keywords = ["incremental", "parsing", "tree-sitter", "bp"]
classifiers = [
"Intended Audience :: Developers",

View file

@ -4,5 +4,3 @@
(module)
(select_expression)
] @fold
; vim: sw=2 foldmethod=marker

View file

@ -1,41 +1,35 @@
(comment) @comment
; Operators {{{
(operator) @operator
(integer_literal ("-") @operator)
; }}}
(integer_literal
"-" @operator)
; Punctuation {{{
[
","
":"
","
":"
] @punctuation.delimiter
[
"("
")"
"["
"]"
"{"
"}"
"("
")"
"["
"]"
"{"
"}"
] @punctuation.bracket
; }}}
; Literal {{{
(boolean_literal) @boolean
(integer_literal) @number
[
(raw_string_literal)
(interpreted_string_literal)
(raw_string_literal)
(interpreted_string_literal)
] @string
(escape_sequence) @string.escape
; }}}
; Declarations {{{
(identifier) @variable
(module
@ -44,24 +38,19 @@
(module
(property
field: (identifier) @variable.parameter))
; }}}
; Built-ins {{{
[
(unset)
(default)
(unset)
(default)
(any)
] @variable.builtin
(condition
name: (identifier) @function.builtin)
; }}}
; Expressions {{{
(condition
name: (identifier) @function.builtin)
(map_expression
(property
field: (identifier) @property))
(select_expression
"select" @keyword.conditional)
; }}}
; vim: sw=2 foldmethod=marker

View file

@ -1,36 +1,38 @@
; Expressions {{{
(list_expression) @indent.begin
(list_expression
"]" @indent.branch)
(map_expression) @indent.begin
(map_expression
"}" @indent.branch)
(select_expression) @indent.begin
(select_expression
")" @indent.branch)
(select_value) @indent.begin
(select_value
")" @indent.branch)
(select_pattern
"(" @indent.begin)
(select_pattern
")" @indent.branch)
(select_cases) @indent.begin
(select_cases
"}" @indent.branch)
; }}}
; Declarations {{{
(module) @indent.begin
(module
")" @indent.branch)
(module
"}" @indent.branch)
; }}}
; vim: sw=2 foldmethod=marker

View file

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

View file

@ -9,6 +9,7 @@
(assignment
left: (identifier) @local.definition.var)
(identifier) @local.reference
(pattern_binding
binding: (identifier) @local.definition.var)
; vim: sw=2 foldmethod=marker
(identifier) @local.reference

109
src/grammar.json generated
View file

@ -1,4 +1,5 @@
{
"$schema": "https://tree-sitter.github.io/tree-sitter/assets/schemas/grammar.schema.json",
"name": "bp",
"rules": {
"source_file": {
@ -614,20 +615,41 @@
"type": "CHOICE",
"members": [
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "select_case"
},
{
"type": "STRING",
"value": ","
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "select_case"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "SYMBOL",
"name": "select_case"
}
]
}
]
}
},
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "BLANK"
}
]
}
]
},
{
"type": "BLANK"
@ -735,6 +757,23 @@
"type": "SYMBOL",
"name": "boolean_literal"
},
{
"type": "SYMBOL",
"name": "integer_literal"
},
{
"type": "ALIAS",
"content": {
"type": "STRING",
"value": "any"
},
"named": true,
"value": "any"
},
{
"type": "SYMBOL",
"name": "pattern_binding"
},
{
"type": "ALIAS",
"content": {
@ -746,6 +785,45 @@
}
]
},
"pattern_binding": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "value",
"content": {
"type": "ALIAS",
"content": {
"type": "STRING",
"value": "any"
},
"named": true,
"value": "any"
}
},
{
"type": "FIELD",
"name": "operator",
"content": {
"type": "ALIAS",
"content": {
"type": "STRING",
"value": "@"
},
"named": true,
"value": "operator"
}
},
{
"type": "FIELD",
"name": "binding",
"content": {
"type": "SYMBOL",
"name": "identifier"
}
}
]
},
"_case_value": {
"type": "CHOICE",
"members": [
@ -1009,5 +1087,6 @@
"precedences": [],
"externals": [],
"inline": [],
"supertypes": []
}
"supertypes": [],
"reserved": {}
}

53
src/node-types.json generated
View file

@ -321,6 +321,42 @@
}
}
},
{
"type": "pattern_binding",
"named": true,
"fields": {
"binding": {
"multiple": false,
"required": true,
"types": [
{
"type": "identifier",
"named": true
}
]
},
"operator": {
"multiple": false,
"required": true,
"types": [
{
"type": "operator",
"named": true
}
]
},
"value": {
"multiple": false,
"required": true,
"types": [
{
"type": "any",
"named": true
}
]
}
}
},
{
"type": "property",
"named": true,
@ -483,6 +519,10 @@
"multiple": true,
"required": true,
"types": [
{
"type": "any",
"named": true
},
{
"type": "boolean_literal",
"named": true
@ -491,10 +531,18 @@
"type": "default",
"named": true
},
{
"type": "integer_literal",
"named": true
},
{
"type": "interpreted_string_literal",
"named": true
},
{
"type": "pattern_binding",
"named": true
},
{
"type": "raw_string_literal",
"named": true
@ -520,6 +568,7 @@
{
"type": "source_file",
"named": true,
"root": true,
"fields": {},
"children": {
"multiple": true,
@ -584,6 +633,10 @@
"type": "]",
"named": false
},
{
"type": "any",
"named": true
},
{
"type": "default",
"named": true

6461
src/parser.c generated

File diff suppressed because it is too large Load diff

View file

@ -12,10 +12,10 @@ extern "C" {
// Allow clients to override allocation functions
#ifdef TREE_SITTER_REUSE_ALLOCATOR
extern void *(*ts_current_malloc)(size_t);
extern void *(*ts_current_calloc)(size_t, size_t);
extern void *(*ts_current_realloc)(void *, size_t);
extern void (*ts_current_free)(void *);
extern void *(*ts_current_malloc)(size_t size);
extern void *(*ts_current_calloc)(size_t count, size_t size);
extern void *(*ts_current_realloc)(void *ptr, size_t size);
extern void (*ts_current_free)(void *ptr);
#ifndef ts_malloc
#define ts_malloc ts_current_malloc

View file

@ -14,6 +14,7 @@ extern "C" {
#include <string.h>
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4101)
#elif defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic push
@ -278,7 +279,7 @@ static inline void _array__splice(Array *self, size_t element_size,
#define _compare_int(a, b) ((int)*(a) - (int)(b))
#ifdef _MSC_VER
#pragma warning(default : 4101)
#pragma warning(pop)
#elif defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop
#endif

View file

@ -18,6 +18,12 @@ typedef uint16_t TSStateId;
typedef uint16_t TSSymbol;
typedef uint16_t TSFieldId;
typedef struct TSLanguage TSLanguage;
typedef struct TSLanguageMetadata TSLanguageMetadata;
typedef struct TSLanguageMetadata {
uint8_t major_version;
uint8_t minor_version;
uint8_t patch_version;
} TSLanguageMetadata;
#endif
typedef struct {
@ -26,10 +32,11 @@ typedef struct {
bool inherited;
} TSFieldMapEntry;
// Used to index the field and supertype maps.
typedef struct {
uint16_t index;
uint16_t length;
} TSFieldMapSlice;
} TSMapSlice;
typedef struct {
bool visible;
@ -47,6 +54,7 @@ struct TSLexer {
uint32_t (*get_column)(TSLexer *);
bool (*is_at_included_range_start)(const TSLexer *);
bool (*eof)(const TSLexer *);
void (*log)(const TSLexer *, const char *, ...);
};
typedef enum {
@ -78,6 +86,12 @@ typedef struct {
uint16_t external_lex_state;
} TSLexMode;
typedef struct {
uint16_t lex_state;
uint16_t external_lex_state;
uint16_t reserved_word_set_id;
} TSLexerMode;
typedef union {
TSParseAction action;
struct {
@ -86,8 +100,13 @@ typedef union {
} entry;
} TSParseActionEntry;
typedef struct {
int32_t start;
int32_t end;
} TSCharacterRange;
struct TSLanguage {
uint32_t version;
uint32_t abi_version;
uint32_t symbol_count;
uint32_t alias_count;
uint32_t token_count;
@ -103,13 +122,13 @@ struct TSLanguage {
const TSParseActionEntry *parse_actions;
const char * const *symbol_names;
const char * const *field_names;
const TSFieldMapSlice *field_map_slices;
const TSMapSlice *field_map_slices;
const TSFieldMapEntry *field_map_entries;
const TSSymbolMetadata *symbol_metadata;
const TSSymbol *public_symbol_map;
const uint16_t *alias_map;
const TSSymbol *alias_sequences;
const TSLexMode *lex_modes;
const TSLexerMode *lex_modes;
bool (*lex_fn)(TSLexer *, TSStateId);
bool (*keyword_lex_fn)(TSLexer *, TSStateId);
TSSymbol keyword_capture_token;
@ -123,8 +142,34 @@ struct TSLanguage {
void (*deserialize)(void *, const char *, unsigned);
} external_scanner;
const TSStateId *primary_state_ids;
const char *name;
const TSSymbol *reserved_words;
uint16_t max_reserved_word_set_size;
uint32_t supertype_count;
const TSSymbol *supertype_symbols;
const TSMapSlice *supertype_map_slices;
const TSSymbol *supertype_map_entries;
TSLanguageMetadata metadata;
};
static inline bool set_contains(const TSCharacterRange *ranges, uint32_t len, int32_t lookahead) {
uint32_t index = 0;
uint32_t size = len - index;
while (size > 1) {
uint32_t half_size = size / 2;
uint32_t mid_index = index + half_size;
const TSCharacterRange *range = &ranges[mid_index];
if (lookahead >= range->start && lookahead <= range->end) {
return true;
} else if (lookahead > range->end) {
index = mid_index;
}
size -= half_size;
}
const TSCharacterRange *range = &ranges[index];
return (lookahead >= range->start && lookahead <= range->end);
}
/*
* Lexer Macros
*/
@ -154,6 +199,17 @@ struct TSLanguage {
goto next_state; \
}
#define ADVANCE_MAP(...) \
{ \
static const uint16_t map[] = { __VA_ARGS__ }; \
for (uint32_t i = 0; i < sizeof(map) / sizeof(map[0]); i += 2) { \
if (map[i] == lookahead) { \
state = map[i + 1]; \
goto next_state; \
} \
} \
}
#define SKIP(state_value) \
{ \
skip = true; \
@ -203,14 +259,15 @@ struct TSLanguage {
} \
}}
#define REDUCE(symbol_val, child_count_val, ...) \
{{ \
.reduce = { \
.type = TSParseActionTypeReduce, \
.symbol = symbol_val, \
.child_count = child_count_val, \
__VA_ARGS__ \
}, \
#define REDUCE(symbol_name, children, precedence, prod_id) \
{{ \
.reduce = { \
.type = TSParseActionTypeReduce, \
.symbol = symbol_name, \
.child_count = children, \
.dynamic_precedence = precedence, \
.production_id = prod_id \
}, \
}}
#define RECOVER() \

View file

@ -116,6 +116,19 @@ Block comment is not recursive
(source_file
(comment))
================================================================================
Block comment is not recursive (error)
================================================================================
/* /* */ */
--------------------------------------------------------------------------------
(source_file
(comment)
(ERROR
(UNEXPECTED '*')))
================================================================================
Unterminated comment
================================================================================

View file

@ -20,6 +20,35 @@ foo = 42
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(operator)
(integer_literal)))
================================================================================
Integer literal (negative)
================================================================================
foo = -42
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(operator)
(integer_literal)))
================================================================================
Integer literal (negative space)
================================================================================
foo = - 42
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)

View file

@ -4,6 +4,7 @@ Select
foo = select(release_variable("RELEASE_TEST"), {
"d": "d2",
any: unset,
default: unset,
})
@ -23,6 +24,35 @@ foo = select(release_variable("RELEASE_TEST"), {
(select_pattern
(interpreted_string_literal))
(interpreted_string_literal))
(select_case
(select_pattern
(any))
(unset))
(select_case
(select_pattern
(default))
(unset))))))
================================================================================
Select (no trailing comma)
================================================================================
foo = select(release_variable("RELEASE_TEST"), {
default: unset
})
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(operator)
(select_expression
(select_value
(condition
(identifier)
(interpreted_string_literal)))
(select_cases
(select_case
(select_pattern
(default))
@ -35,6 +65,7 @@ Select (soong config variable)
foo = select(soong_config_variable("my_namespace", "my_var"), {
"foo": unset,
"default": "bar",
any @ foo: unset,
})
--------------------------------------------------------------------------------
@ -57,7 +88,14 @@ foo = select(soong_config_variable("my_namespace", "my_var"), {
(select_case
(select_pattern
(interpreted_string_literal))
(interpreted_string_literal))))))
(interpreted_string_literal))
(select_case
(select_pattern
(pattern_binding
(any)
(operator)
(identifier)))
(unset))))))
================================================================================
Select (no default)
@ -371,6 +409,46 @@ foo = select(some_boolean("IS_TRUE"), {
(boolean_literal))
(interpreted_string_literal))))))
================================================================================
Select (integer typed)
================================================================================
foo = select(some_integer("VALUE"), {
0: "0",
-1: "-1",
1: "1",
default: "default",
})
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(operator)
(select_expression
(select_value
(condition
(identifier)
(interpreted_string_literal)))
(select_cases
(select_case
(select_pattern
(integer_literal))
(interpreted_string_literal))
(select_case
(select_pattern
(integer_literal))
(interpreted_string_literal))
(select_case
(select_pattern
(integer_literal))
(interpreted_string_literal))
(select_case
(select_pattern
(default))
(interpreted_string_literal))))))
================================================================================
Select as an identifier
================================================================================

View file

@ -2,6 +2,8 @@ foo = select(soong_config_variable("my_namespace", "my_var"), {
// ^ function.builtin
"foo": unset,
// ^ variable.builtin
any: unset,
// <- variable.builtin
default: select(variant("VARIANT"), {}),
// <- variable.builtin
// ^ function.builtin

View file

@ -1,6 +1,9 @@
foo = select(soong_config_variable("my_namespace", "my_var"), {
// ^ keyword.conditional
"foo": unset,
any @ foo: unset,
// ^ operator
// ^ variable
default: "bar",
})

30
tree-sitter.json Normal file
View file

@ -0,0 +1,30 @@
{
"grammars": [
{
"name": "bp",
"camelcase": "Bp",
"scope": "source.bp",
"path": ".",
"file-types": [
"bp"
],
"injection-regex": "^bp$"
}
],
"metadata": {
"version": "0.7.0",
"license": "MIT",
"description": "Blueprint grammar for tree-sitter",
"links": {
"repository": "https://github.com/tree-sitter/tree-sitter-bp"
}
},
"bindings": {
"c": true,
"go": true,
"node": true,
"python": true,
"rust": true,
"swift": true
}
}