@acristoffers/tree-sitter-matlab 1.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +19 -0
- package/README.md +97 -0
- package/binding.gyp +35 -0
- package/bindings/node/binding.cc +19 -0
- package/bindings/node/binding_test.js +9 -0
- package/bindings/node/index.d.ts +27 -0
- package/bindings/node/index.js +11 -0
- package/grammar.js +1000 -0
- package/package.json +59 -0
- package/queries/emacs/highlights.scm +176 -0
- package/queries/emacs/textobjects.scm +93 -0
- package/queries/helix/context.scm +41 -0
- package/queries/helix/folds.scm +11 -0
- package/queries/helix/highlights.scm +127 -0
- package/queries/helix/indents.scm +24 -0
- package/queries/helix/injections.scm +2 -0
- package/queries/helix/locals.scm +19 -0
- package/queries/helix/textobjects.scm +9 -0
- package/queries/neovim/context.scm +41 -0
- package/queries/neovim/folds.scm +11 -0
- package/queries/neovim/highlights.scm +157 -0
- package/queries/neovim/indents.scm +36 -0
- package/queries/neovim/injections.scm +1 -0
- package/queries/neovim/locals.scm +20 -0
- package/queries/neovim/tags.scm +10 -0
- package/queries/neovim/textobjects.scm +110 -0
- package/src/grammar.json +6184 -0
- package/src/node-types.json +3719 -0
- package/src/parser.c +113495 -0
- package/src/scanner.c +1094 -0
- package/src/tree_sitter/alloc.h +54 -0
- package/src/tree_sitter/array.h +291 -0
- package/src/tree_sitter/parser.h +286 -0
- package/tree-sitter.json +36 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright (c) 2023 Álan Crístoffer
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
4
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
5
|
+
the Software without restriction, including without limitation the rights to
|
|
6
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
7
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
|
8
|
+
so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
|
11
|
+
copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
# MATLAB grammar for tree-sitter.
|
|
2
|
+
|
|
3
|
+
There are screenshots at the end of this README :)
|
|
4
|
+
|
|
5
|
+
This parser has the objective of generating a tree that is as correct as
|
|
6
|
+
possible (but sometimes just convenient) with what MATLAB itself executes. It
|
|
7
|
+
is not intended only for syntax highlight, but also to be used by scripts to
|
|
8
|
+
whatever it may be needed. In fact, I wrote it because I'm a Neovim/Doom Emacs
|
|
9
|
+
user and love having text-objects, and was really missing a text object for
|
|
10
|
+
matrices rows/cells.
|
|
11
|
+
|
|
12
|
+
Being as correct as possible means that some things are done correctly, for
|
|
13
|
+
example:
|
|
14
|
+
|
|
15
|
+
- Commands are parsed the same way MATLAB does it, by treating arguments as
|
|
16
|
+
literals, grouping them correctly and only starting comments when allowed. It
|
|
17
|
+
should perfectly match what MATLAB does.
|
|
18
|
+
|
|
19
|
+
- Assignment has its own token, and multiple-variable assignment is NOT an
|
|
20
|
+
assignment to a matrix (and returning an error is the correct thing to do, as
|
|
21
|
+
it allows the user to see that something is off with the highlight, meaning
|
|
22
|
+
something is probably off with the code):
|
|
23
|
+
|
|
24
|
+
```matlab
|
|
25
|
+
% (assignment (multioutput_variable (identifier) (identifier)) (identifier))
|
|
26
|
+
[a,b] = d
|
|
27
|
+
|
|
28
|
+
% this is WRONG:
|
|
29
|
+
[a;b] = d
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
- Inside a matrix, `1 + 1` and `1 +1` are different things:
|
|
33
|
+
|
|
34
|
+
```matlab
|
|
35
|
+
a = 1 + 1 % 2
|
|
36
|
+
a = 1 +1 %2
|
|
37
|
+
[1 + 1] == [2]
|
|
38
|
+
[1 +1] == [1 1]
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Being convenient means that sometimes the difference between what is acceptable
|
|
42
|
+
and what is not acceptable lives in the semantics, so we can't know. In such
|
|
43
|
+
cases I just accept semantically wrong but syntax correct things and group them
|
|
44
|
+
in the same token (first example). I do the same when the overhead of
|
|
45
|
+
generating a specific token would not really pay off (second example).
|
|
46
|
+
|
|
47
|
+
- Function calls and Matrix Indexing are the same in MATLAB: `A(1)` can be any
|
|
48
|
+
of them and you cannot tell them apart unless you know for sure what `A` is
|
|
49
|
+
referring to. So for convenience I just generate a `function_call` for them and
|
|
50
|
+
also for cell indexing `A{1}`. The "problem" with that is that this is a valid
|
|
51
|
+
indexing but an invalid function call: `A(:)`. However I don't distinguish at
|
|
52
|
+
all and say that all of them are `function_call`.
|
|
53
|
+
|
|
54
|
+
- Function definitions, when inside a class, accepts a special syntax for the
|
|
55
|
+
name of the function, allowing it to be preceded by either `get.` or `set.`,
|
|
56
|
+
like `function get.name()`. I could have a `method_definition` that would allow
|
|
57
|
+
that to only be valid in the class context, but I doubt that would be worth it.
|
|
58
|
+
So any function anywhere can have those and be recognize as correct still.
|
|
59
|
+
Given the existence of external method definition, maybe that is even the
|
|
60
|
+
correct thing to do, since we don't know if the current file is inside a
|
|
61
|
+
special class folder.
|
|
62
|
+
|
|
63
|
+
# Known problems
|
|
64
|
+
|
|
65
|
+
Newlines, just like whitespaces, are mostly ignored. In the case of spaces, it
|
|
66
|
+
allows `abs( a )` and `abs(a)` to be described by the same, simple rule. In the
|
|
67
|
+
case of newlines, it allows many multiline constructs (like, `if`, `while`,
|
|
68
|
+
`function`) to be expressed the same way.
|
|
69
|
+
|
|
70
|
+
This creates the undesired side-effect that some constructs, which are not
|
|
71
|
+
accepted by MATLAB, are correctly parsed, like:
|
|
72
|
+
|
|
73
|
+
```matlab
|
|
74
|
+
function (
|
|
75
|
+
a
|
|
76
|
+
)
|
|
77
|
+
end
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
This, however, is hard to fix. The assumption that newlines are ignored by
|
|
81
|
+
default is all over the grammar and changing it requires making changes to too
|
|
82
|
+
many rules, which also make them all more complex and fragile. Therefore, this
|
|
83
|
+
change won't be made.
|
|
84
|
+
|
|
85
|
+
# Installation
|
|
86
|
+
|
|
87
|
+
This parser is now the default for the following editors:
|
|
88
|
+
|
|
89
|
+
- Emacs: Through the `tree-sitter-langs` package.
|
|
90
|
+
- Helix: Builtin, now in master and will be available in the next release (whatever comes after 23.05).
|
|
91
|
+
- Neovim: Through the `nvim-treesitter` plugin.
|
|
92
|
+
|
|
93
|
+
# Screenshots
|
|
94
|
+
|
|
95
|
+

|
|
96
|
+

|
|
97
|
+

|
package/binding.gyp
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"targets": [
|
|
3
|
+
{
|
|
4
|
+
"target_name": "tree_sitter_matlab_binding",
|
|
5
|
+
"dependencies": [
|
|
6
|
+
"<!(node -p \"require('node-addon-api').targets\"):node_addon_api_except",
|
|
7
|
+
],
|
|
8
|
+
"include_dirs": [
|
|
9
|
+
"src",
|
|
10
|
+
],
|
|
11
|
+
"sources": [
|
|
12
|
+
"bindings/node/binding.cc",
|
|
13
|
+
"src/parser.c",
|
|
14
|
+
],
|
|
15
|
+
"variables": {
|
|
16
|
+
"has_scanner": "<!(node -p \"fs.existsSync('src/scanner.c')\")"
|
|
17
|
+
},
|
|
18
|
+
"conditions": [
|
|
19
|
+
["has_scanner=='true'", {
|
|
20
|
+
"sources+": ["src/scanner.c"],
|
|
21
|
+
}],
|
|
22
|
+
["OS!='win'", {
|
|
23
|
+
"cflags_c": [
|
|
24
|
+
"-std=c11",
|
|
25
|
+
],
|
|
26
|
+
}, { # OS == "win"
|
|
27
|
+
"cflags_c": [
|
|
28
|
+
"/std:c11",
|
|
29
|
+
"/utf-8",
|
|
30
|
+
],
|
|
31
|
+
}],
|
|
32
|
+
],
|
|
33
|
+
}
|
|
34
|
+
]
|
|
35
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
#include <napi.h>
|
|
2
|
+
|
|
3
|
+
typedef struct TSLanguage TSLanguage;
|
|
4
|
+
|
|
5
|
+
extern "C" TSLanguage *tree_sitter_matlab();
|
|
6
|
+
|
|
7
|
+
// "tree-sitter", "language" hashed with BLAKE2
|
|
8
|
+
const napi_type_tag LANGUAGE_TYPE_TAG = {
|
|
9
|
+
0x8AF2E5212AD58ABF, 0xD5006CAD83ABBA16
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
Napi::Object Init(Napi::Env env, Napi::Object exports) {
|
|
13
|
+
auto language = Napi::External<TSLanguage>::New(env, tree_sitter_matlab());
|
|
14
|
+
language.TypeTag(&LANGUAGE_TYPE_TAG);
|
|
15
|
+
exports["language"] = language;
|
|
16
|
+
return exports;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
NODE_API_MODULE(tree_sitter_matlab_binding, Init)
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
type BaseNode = {
|
|
2
|
+
type: string;
|
|
3
|
+
named: boolean;
|
|
4
|
+
};
|
|
5
|
+
|
|
6
|
+
type ChildNode = {
|
|
7
|
+
multiple: boolean;
|
|
8
|
+
required: boolean;
|
|
9
|
+
types: BaseNode[];
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
type NodeInfo =
|
|
13
|
+
| (BaseNode & {
|
|
14
|
+
subtypes: BaseNode[];
|
|
15
|
+
})
|
|
16
|
+
| (BaseNode & {
|
|
17
|
+
fields: { [name: string]: ChildNode };
|
|
18
|
+
children: ChildNode[];
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
type Language = {
|
|
22
|
+
language: unknown;
|
|
23
|
+
nodeTypeInfo: NodeInfo[];
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
declare const language: Language;
|
|
27
|
+
export = language;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
const root = require("path").join(__dirname, "..", "..");
|
|
2
|
+
|
|
3
|
+
module.exports =
|
|
4
|
+
typeof process.versions.bun === "string"
|
|
5
|
+
// Support `bun build --compile` by being statically analyzable enough to find the .node file at build-time
|
|
6
|
+
? require(`../../prebuilds/${process.platform}-${process.arch}/tree-sitter-matlab.node`)
|
|
7
|
+
: require("node-gyp-build")(root);
|
|
8
|
+
|
|
9
|
+
try {
|
|
10
|
+
module.exports.nodeTypeInfo = require("../../src/node-types.json");
|
|
11
|
+
} catch (_) {}
|