Skip to main content

Matlab grammar for tree-sitter

Project description

MATLAB grammar for tree-sitter.

There are screenshots at the end of this README :)

This parser has the objective of generating a tree that is as correct as possible (but sometimes just convenient) with what MATLAB itself executes. It is not intended only for syntax highlight, but also to be used by scripts to whatever it may be needed. In fact, I wrote it because I'm a Neovim/Doom Emacs user and love having text-objects, and was really missing a text object for matrices rows/cells.

Being as correct as possible means that some things are done correctly, for example:

  • Commands are parsed the same way MATLAB does it, by treating arguments as literals, grouping them correctly and only starting comments when allowed. It should perfectly match what MATLAB does.

  • Assignment has its own token, and multiple-variable assignment is NOT an assignment to a matrix (and returning an error is the correct thing to do, as it allows the user to see that something is off with the highlight, meaning something is probably off with the code):

% (assignment (multioutput_variable (identifier) (identifier)) (identifier)) 
[a,b] = d

% this is WRONG:
[a;b] = d
  • Inside a matrix, 1 + 1 and 1 +1 are different things:
a = 1 + 1 % 2
a = 1 +1 %2
[1 + 1] == [2]
[1 +1]  == [1 1]

Being convenient means that sometimes the difference between what is acceptable and what is not acceptable lives in the semantics, so we can't know. In such cases I just accept semantically wrong but syntax correct things and group them in the same token (first example). I do the same when the overhead of generating a specific token would not really pay off (second example).

  • Function calls and Matrix Indexing are the same in MATLAB: A(1) can be any of them and you cannot tell them apart unless you know for sure what A is referring to. So for convenience I just generate a function_call for them and also for cell indexing A{1}. The "problem" with that is that this is a valid indexing but an invalid function call: A(:). However I don't distinguish at all and say that all of them are function_call.

  • Function definitions, when inside a class, accepts a special syntax for the name of the function, allowing it to be preceded by either get. or set., like function get.name(). I could have a method_definition that would allow that to only be valid in the class context, but I doubt that would be worth it. So any function anywhere can have those and be recognize as correct still. Given the existence of external method definition, maybe that is even the correct thing to do, since we don't know if the current file is inside a special class folder.

Installation

This parser is now the default for the following editors:

  • Emacs: Through the tree-sitter-langs package.
  • Helix: Builtin, now in master and will be available in the next release (whatever comes after 23.05).
  • Neovim: Through the nvim-treesitter plugin.

Screenshots

First Screenshot Second Screenshot Third Screenshot

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tree_sitter_matlab-1.0.2.tar.gz (76.3 kB view hashes)

Uploaded Source

Built Distributions

tree_sitter_matlab-1.0.2-pp310-pypy310_pp73-win_amd64.whl (51.5 kB view hashes)

Uploaded PyPy Windows x86-64

tree_sitter_matlab-1.0.2-pp310-pypy310_pp73-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (52.0 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

tree_sitter_matlab-1.0.2-pp310-pypy310_pp73-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (53.8 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

tree_sitter_matlab-1.0.2-pp310-pypy310_pp73-macosx_11_0_arm64.whl (50.3 kB view hashes)

Uploaded PyPy macOS 11.0+ ARM64

tree_sitter_matlab-1.0.2-pp310-pypy310_pp73-macosx_10_15_x86_64.whl (49.1 kB view hashes)

Uploaded PyPy macOS 10.15+ x86-64

tree_sitter_matlab-1.0.2-pp39-pypy39_pp73-win_amd64.whl (51.5 kB view hashes)

Uploaded PyPy Windows x86-64

tree_sitter_matlab-1.0.2-pp39-pypy39_pp73-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (52.0 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

tree_sitter_matlab-1.0.2-pp39-pypy39_pp73-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (53.8 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

tree_sitter_matlab-1.0.2-pp39-pypy39_pp73-macosx_11_0_arm64.whl (50.3 kB view hashes)

Uploaded PyPy macOS 11.0+ ARM64

tree_sitter_matlab-1.0.2-pp39-pypy39_pp73-macosx_10_15_x86_64.whl (49.1 kB view hashes)

Uploaded PyPy macOS 10.15+ x86-64

tree_sitter_matlab-1.0.2-pp38-pypy38_pp73-win_amd64.whl (51.5 kB view hashes)

Uploaded PyPy Windows x86-64

tree_sitter_matlab-1.0.2-pp38-pypy38_pp73-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (52.0 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

tree_sitter_matlab-1.0.2-pp38-pypy38_pp73-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (53.8 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

tree_sitter_matlab-1.0.2-pp38-pypy38_pp73-macosx_11_0_arm64.whl (50.3 kB view hashes)

Uploaded PyPy macOS 11.0+ ARM64

tree_sitter_matlab-1.0.2-pp38-pypy38_pp73-macosx_10_9_x86_64.whl (49.0 kB view hashes)

Uploaded PyPy macOS 10.9+ x86-64

tree_sitter_matlab-1.0.2-cp38-abi3-win_amd64.whl (51.4 kB view hashes)

Uploaded CPython 3.8+ Windows x86-64

tree_sitter_matlab-1.0.2-cp38-abi3-win32.whl (50.7 kB view hashes)

Uploaded CPython 3.8+ Windows x86

tree_sitter_matlab-1.0.2-cp38-abi3-musllinux_1_2_x86_64.whl (76.6 kB view hashes)

Uploaded CPython 3.8+ musllinux: musl 1.2+ x86-64

tree_sitter_matlab-1.0.2-cp38-abi3-musllinux_1_2_i686.whl (77.0 kB view hashes)

Uploaded CPython 3.8+ musllinux: musl 1.2+ i686

tree_sitter_matlab-1.0.2-cp38-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (79.8 kB view hashes)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

tree_sitter_matlab-1.0.2-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (79.1 kB view hashes)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

tree_sitter_matlab-1.0.2-cp38-abi3-macosx_11_0_arm64.whl (51.2 kB view hashes)

Uploaded CPython 3.8+ macOS 11.0+ ARM64

tree_sitter_matlab-1.0.2-cp38-abi3-macosx_10_9_x86_64.whl (49.8 kB view hashes)

Uploaded CPython 3.8+ macOS 10.9+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page