Skip to content

Eldeloto/tree-sitter-apex

Repository files navigation

tree-sitter-apex

Apex grammar fortree-sitter.

Based on the official Java grammarhttps://github /tree-sitter/tree-sitter-java.

compile

To compile the grammar just clone the repo and run the following commands

npm install
./node_modules/.bin/tree-sitter generate
node-gyp configure
node-gyp build

install and use

command line util

Once compiled it can be tested with the command line utility.

tree-sitter parse test/Sample.cls

use in neovim

To use the parser in neovim you will require the neovim pluginnvim-treesitter

In your nvim config you can set the following instructions. Change the url to point to the local path where the repo has been cloned.

localparser_config=require"nvim-treesitter.parsers".get_parser_configs()
parser_config.apex={
install_info={
url="~/Tech/treesitter/tree-sitter-apex",--local path or git repo
files={"src/parser.c"}
},
filetype="apexcode",--if filetype does not agrees with parser name
}

And then install it with:

TSInstall apex

use in rust

In a rust project add the tree-sitter dependency

cargo add tree-sitter

Then add the dependency to the cc compiler in your Cargo.toml

[build-dependencies]
cc="1.0"

And finally create a build.rs file that will compile the tree-sitter lib

externcratecc;

usestd::path::PathBuf;

fnmain()
{
letdir:PathBuf=[<point to the pathwherethe tree-sitter-apex repo has been cloned in your local machine>]
.into_iter()
.collect();

cc::Build::new()
.include(&dir)
.file(dir.join("parser.c"))
.compile("tree-sitter-apex");
}

And for use, in your main.rs you can add this:

usetree_sitter::{Language,Parser};
...
letlanguage =unsafe{tree_sitter_apex()};
letmutparser =Parser::new();
parser.set_language(language).unwrap();

use in lua

Once is installed the grammar in the neovim tree-sitter plugin.

localts_utils=require"nvim-treesitter.ts_utils"

localp_bufnr=vim.api.nvim_get_current_buf()

localparser=vim.treesitter.get_parser(p_bufnr,'apex')
localroot=parser:parse()[1]:root()

use in Python

Install the Python bindings for tree-sitter

And compile the library to be used for the Python program.

fromtree_sitterimportLanguage,Parser

Language.build_library(
'build/tree-sitter-apex.so',
[
'<point to the path where the tree-sitter-apex repo has been cloned in your local machine>'
]
)

And for use

APEX_LANGUAGE=Language('build/tree-sitter-apex.so','apex')

parser=Parser()
parser.set_language(APEX_LANGUAGE)

functionalities

All the functionalities implemented can be checked in the src/Sample.cls file

My personal suggestion is to configure neovim tree_sitter plugin and then use the tree_sitter playground.https://github /nvim-treesitter/playground

  • apex specific modifiers. (global, with/without sharing)
globalwithsharingclassClass1
  • apex string_literal
System.debug('foo');
  • query_literal
List<String>l= [
SELECTfield1,field2
FROMAccount];
  • dml_statement
publicDML_examples() {
List<String>l= [
SELECTfield1,field2
FROMAccount];

insertl;
updatel;
deletel;
}
  • enhanced_dml_statement
publicEnhanced_DML_example() {
delete[SELECTidFROMAccount];
}
  • null_safe_operator
StringmyString=myContact?.Account?.RecordType.Name;
  • not_equals_operators
if(1<>2) {
Stringx='foo';
}

if(1!=2) {
Stringx='foo';
}

questions/issues

Please open an issue on this repo and we'll work through it.

contributing

There are still some parsing error. If you want to fix some of them please feel free to send a PR.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C 98.1%
  • JavaScript 1.6%
  • Rust 0.2%
  • Apex 0.1%
  • C++ 0.0%
  • Lua 0.0%