@@ -9,15 +9,17 @@ plugin already, [guidance](nvim-treesitter-setup.md).
99
1010## Usage
1111
12- ### Install
12+ ### Node
13+
14+ #### Install
1315
1416``` sh
1517npm install tree-sitter
1618
1719npm install tree-sitter-sfapex
1820```
1921
20- ### Example
22+ #### Example
2123
2224``` JavaScript
2325// import libraries
@@ -79,6 +81,92 @@ function printTree(node, indent = 0) {
7981
8082```
8183
84+ ### Rust
85+
86+ #### Install
87+
88+ ``` sh
89+ cargo add tree-sitter
90+
91+ cargo add tree-sitter-sfapex
92+ ```
93+
94+ #### Example
95+
96+ ``` Rust
97+ use tree_sitter :: {Parser , TreeCursor };
98+
99+ // just a super simple example of printing the discovered nodes
100+ // to see the anonymous nodes (syntax without formal names) set this to `true`
101+ const INCLUDE_ANONYMOUS_NODES : bool = true ;
102+
103+ fn main () {
104+ let mut parser = Parser :: new ();
105+
106+ let language_fn = tree_sitter_sfapex :: apex :: LANGUAGE ;
107+ parser . set_language (& language_fn . into ()). unwrap ();
108+
109+ let source_code = r # "
110+ /**
111+ * block comment
112+ */
113+ global class TestClass implements TestInterface {
114+ public static String Prop1 = 'TestVal';
115+
116+ global Account setName(Account acct, String nameVal){
117+ acct.Name = nameVal;
118+ return acct;
119+ }
120+ }" # ;
121+ let tree = parser . parse (source_code , None ). unwrap ();
122+
123+
124+ println! (" APEX TREE" );
125+ print_tree (& mut tree . root_node (). walk (), INCLUDE_ANONYMOUS_NODES , 0 );
126+
127+ // do it with some SOQL this time
128+ let language_fn = tree_sitter_sfapex :: soql :: LANGUAGE ;
129+ parser . set_language (& language_fn . into ()). unwrap ();
130+
131+ let soql_source_code = r # "
132+ SELECT Id, Name, Parent.Name,
133+ TYPEOF Owner
134+ WHEN User THEN Id, Username, FederationId
135+ WHEN Group THEN Name
136+ END,
137+ (SELECT Id, Name FROM Contacts)
138+ FROM Account
139+ WHERE Name = 'Robots' AND Are_Coming__c = FALSE" # ;
140+
141+ let tree = parser . parse (soql_source_code , None ). unwrap ();
142+
143+ println! (" SOQL TREE" );
144+ print_tree (& mut tree . root_node (). walk (), INCLUDE_ANONYMOUS_NODES , 0 );
145+
146+ }
147+
148+ fn print_tree (cursor : & mut TreeCursor , include_anonymous_nodes : bool , indent : usize ) {
149+ let t_node = cursor . node ();
150+ println! (" {}{}{}{}" , " " . repeat (indent ),
151+ (if t_node . is_named () {" (" } else {" \ "" }). to_owned (),
152+ t_node . kind (),
153+ (if t_node . is_named () {" )" } else {" \ "" }). to_owned ());
154+
155+ if cursor . goto_first_child () {
156+ loop {
157+ if cursor . node (). is_named () || include_anonymous_nodes {
158+ print_tree (cursor , include_anonymous_nodes , indent + 2 );
159+ }
160+ if ! cursor . goto_next_sibling () {
161+ break ;
162+ }
163+ }
164+ // when we're done here, go up to the parent again
165+ cursor . goto_parent ();
166+ }
167+ }
168+ ```
169+
82170## Status
83171
84172Most of the parsers are built and tested on large corpus of Apex, I still intend to write automated tests that parse large Apex libraries as part of evaluating the grammar.
0 commit comments