diff options
| author | Yuan Fu | 2022-11-03 11:41:42 -0700 |
|---|---|---|
| committer | Yuan Fu | 2022-11-03 11:43:44 -0700 |
| commit | 5416ae5990337f5fb2b3e0fbf9c4575508da808e (patch) | |
| tree | 10d24c612d033ce0e21828161eefaa37e7e38559 /admin | |
| parent | 99096528494a653d1ee9d7832ee3956a613aeb95 (diff) | |
| download | emacs-5416ae5990337f5fb2b3e0fbf9c4575508da808e.tar.gz emacs-5416ae5990337f5fb2b3e0fbf9c4575508da808e.zip | |
; Update guides in /admin/notes/tree-sitter
* admin/notes/tree-sitter/html-manual/Language-Definitions.html
* admin/notes/tree-sitter/html-manual/Multiple-Languages.html
* admin/notes/tree-sitter/html-manual/Parser_002dbased-Font-Lock.html
* admin/notes/tree-sitter/html-manual/Parser_002dbased-Indentation.html
* admin/notes/tree-sitter/html-manual/Parsing-Program-Source.html
* admin/notes/tree-sitter/html-manual/Pattern-Matching.html
* admin/notes/tree-sitter/html-manual/Retrieving-Node.html
* admin/notes/tree-sitter/html-manual/Tree_002dsitter-C-API.html
* admin/notes/tree-sitter/html-manual/Using-Parser.html
* admin/notes/tree-sitter/starter-guide: Update to reflect changes
made recently.
Diffstat (limited to 'admin')
10 files changed, 1008 insertions, 772 deletions
diff --git a/admin/notes/tree-sitter/html-manual/Language-Definitions.html b/admin/notes/tree-sitter/html-manual/Language-Definitions.html index ba3eeb9eeb9..6df676b1680 100644 --- a/admin/notes/tree-sitter/html-manual/Language-Definitions.html +++ b/admin/notes/tree-sitter/html-manual/Language-Definitions.html | |||
| @@ -66,14 +66,17 @@ Next: <a href="Using-Parser.html" accesskey="n" rel="next">Using Tree-sitter Par | |||
| 66 | </div> | 66 | </div> |
| 67 | <hr> | 67 | <hr> |
| 68 | <span id="Tree_002dsitter-Language-Definitions"></span><h3 class="section">37.1 Tree-sitter Language Definitions</h3> | 68 | <span id="Tree_002dsitter-Language-Definitions"></span><h3 class="section">37.1 Tree-sitter Language Definitions</h3> |
| 69 | <span id="index-language-definitions_002c-for-tree_002dsitter"></span> | ||
| 69 | 70 | ||
| 70 | <span id="Loading-a-language-definition"></span><h3 class="heading">Loading a language definition</h3> | 71 | <span id="Loading-a-language-definition"></span><h3 class="heading">Loading a language definition</h3> |
| 72 | <span id="index-loading-language-definition-for-tree_002dsitter"></span> | ||
| 71 | 73 | ||
| 74 | <span id="index-language-argument_002c-for-tree_002dsitter"></span> | ||
| 72 | <p>Tree-sitter relies on language definitions to parse text in that | 75 | <p>Tree-sitter relies on language definitions to parse text in that |
| 73 | language. In Emacs, A language definition is represented by a symbol. | 76 | language. In Emacs, a language definition is represented by a symbol. |
| 74 | For example, C language definition is represented as <code>c</code>, and | 77 | For example, the C language definition is represented as the symbol |
| 75 | <code>c</code> can be passed to tree-sitter functions as the <var>language</var> | 78 | <code>c</code>, and <code>c</code> can be passed to tree-sitter functions as the |
| 76 | argument. | 79 | <var>language</var> argument. |
| 77 | </p> | 80 | </p> |
| 78 | <span id="index-treesit_002dextra_002dload_002dpath"></span> | 81 | <span id="index-treesit_002dextra_002dload_002dpath"></span> |
| 79 | <span id="index-treesit_002dload_002dlanguage_002derror"></span> | 82 | <span id="index-treesit_002dload_002dlanguage_002derror"></span> |
| @@ -81,55 +84,92 @@ argument. | |||
| 81 | <p>Tree-sitter language definitions are distributed as dynamic libraries. | 84 | <p>Tree-sitter language definitions are distributed as dynamic libraries. |
| 82 | In order to use a language definition in Emacs, you need to make sure | 85 | In order to use a language definition in Emacs, you need to make sure |
| 83 | that the dynamic library is installed on the system. Emacs looks for | 86 | that the dynamic library is installed on the system. Emacs looks for |
| 84 | language definitions under load paths in | 87 | language definitions in several places, in the following order: |
| 85 | <code>treesit-extra-load-path</code>, <code>user-emacs-directory</code>/tree-sitter, | 88 | </p> |
| 86 | and system default locations for dynamic libraries, in that order. | 89 | <ul> |
| 87 | Emacs tries each extensions in <code>treesit-load-suffixes</code>. If Emacs | 90 | <li> first, in the list of directories specified by the variable |
| 88 | cannot find the library or has problem loading it, Emacs signals | 91 | <code>treesit-extra-load-path</code>; |
| 89 | <code>treesit-load-language-error</code>. The signal data is a list of | 92 | </li><li> then, in the <samp>tree-sitter</samp> subdirectory of the directory |
| 90 | specific error messages. | 93 | specified by <code>user-emacs-directory</code> (see <a href="Init-File.html">The Init File</a>); |
| 94 | </li><li> and finally, in the system’s default locations for dynamic libraries. | ||
| 95 | </li></ul> | ||
| 96 | |||
| 97 | <p>In each of these directories, Emacs looks for a file with file-name | ||
| 98 | extensions specified by the variable <code>treesit-load-suffixes</code>. | ||
| 99 | </p> | ||
| 100 | <p>If Emacs cannot find the library or has problems loading it, Emacs | ||
| 101 | signals the <code>treesit-load-language-error</code> error. The data of | ||
| 102 | that signal could be one of the following: | ||
| 103 | </p> | ||
| 104 | <dl compact="compact"> | ||
| 105 | <dt><span><code>(not-found <var>error-msg</var> …)</code></span></dt> | ||
| 106 | <dd><p>This means that Emacs could not find the language definition library. | ||
| 107 | </p></dd> | ||
| 108 | <dt><span><code>(symbol-error <var>error-msg</var>)</code></span></dt> | ||
| 109 | <dd><p>This means that Emacs could not find in the library the expected function | ||
| 110 | that every language definition library should export. | ||
| 111 | </p></dd> | ||
| 112 | <dt><span><code>(version-mismatch <var>error-msg</var>)</code></span></dt> | ||
| 113 | <dd><p>This means that the version of language definition library is incompatible | ||
| 114 | with that of the tree-sitter library. | ||
| 115 | </p></dd> | ||
| 116 | </dl> | ||
| 117 | |||
| 118 | <p>In all of these cases, <var>error-msg</var> might provide additional | ||
| 119 | details about the failure. | ||
| 91 | </p> | 120 | </p> |
| 92 | <dl class="def"> | 121 | <dl class="def"> |
| 93 | <dt id="index-treesit_002dlanguage_002davailable_002dp"><span class="category">Function: </span><span><strong>treesit-language-available-p</strong> <em>language</em><a href='#index-treesit_002dlanguage_002davailable_002dp' class='copiable-anchor'> ¶</a></span></dt> | 122 | <dt id="index-treesit_002dlanguage_002davailable_002dp"><span class="category">Function: </span><span><strong>treesit-language-available-p</strong> <em>language &optional detail</em><a href='#index-treesit_002dlanguage_002davailable_002dp' class='copiable-anchor'> ¶</a></span></dt> |
| 94 | <dd><p>This function checks whether the dynamic library for <var>language</var> is | 123 | <dd><p>This function returns non-<code>nil</code> if the language definitions for |
| 95 | present on the system, and return non-nil if it is. | 124 | <var>language</var> exist and can be loaded. |
| 125 | </p> | ||
| 126 | <p>If <var>detail</var> is non-<code>nil</code>, return <code>(t . nil)</code> when | ||
| 127 | <var>language</var> is available, and <code>(nil . <var>data</var>)</code> when it’s | ||
| 128 | unavailable. <var>data</var> is the signal data of | ||
| 129 | <code>treesit-load-language-error</code>. | ||
| 96 | </p></dd></dl> | 130 | </p></dd></dl> |
| 97 | 131 | ||
| 98 | <span id="index-treesit_002dload_002dname_002doverride_002dlist"></span> | 132 | <span id="index-treesit_002dload_002dname_002doverride_002dlist"></span> |
| 99 | <p>By convention, the dynamic library for <var>language</var> is | 133 | <p>By convention, the file name of the dynamic library for <var>language</var> is |
| 100 | <code>libtree-sitter-<var>language</var>.<var>ext</var></code>, where <var>ext</var> is the | 134 | <samp>libtree-sitter-<var>language</var>.<var>ext</var></samp>, where <var>ext</var> is the |
| 101 | system-specific extension for dynamic libraries. Also by convention, | 135 | system-specific extension for dynamic libraries. Also by convention, |
| 102 | the function provided by that library is named | 136 | the function provided by that library is named |
| 103 | <code>tree_sitter_<var>language</var></code>. If a language definition doesn’t | 137 | <code>tree_sitter_<var>language</var></code>. If a language definition library |
| 104 | follow this convention, you should add an entry | 138 | doesn’t follow this convention, you should add an entry |
| 105 | </p> | 139 | </p> |
| 106 | <div class="example"> | 140 | <div class="example"> |
| 107 | <pre class="example">(<var>language</var> <var>library-base-name</var> <var>function-name</var>) | 141 | <pre class="example">(<var>language</var> <var>library-base-name</var> <var>function-name</var>) |
| 108 | </pre></div> | 142 | </pre></div> |
| 109 | 143 | ||
| 110 | <p>to <code>treesit-load-name-override-list</code>, where | 144 | <p>to the list in the variable <code>treesit-load-name-override-list</code>, where |
| 111 | <var>library-base-name</var> is the base filename for the dynamic library | 145 | <var>library-base-name</var> is the basename of the dynamic library’s file name, |
| 112 | (conventionally <code>libtree-sitter-<var>language</var></code>), and | 146 | (usually, <samp>libtree-sitter-<var>language</var></samp>), and |
| 113 | <var>function-name</var> is the function provided by the library | 147 | <var>function-name</var> is the function provided by the library |
| 114 | (conventionally <code>tree_sitter_<var>language</var></code>). For example, | 148 | (usually, <code>tree_sitter_<var>language</var></code>). For example, |
| 115 | </p> | 149 | </p> |
| 116 | <div class="example"> | 150 | <div class="example"> |
| 117 | <pre class="example">(cool-lang "libtree-sitter-coool" "tree_sitter_cooool") | 151 | <pre class="example">(cool-lang "libtree-sitter-coool" "tree_sitter_cooool") |
| 118 | </pre></div> | 152 | </pre></div> |
| 119 | 153 | ||
| 120 | <p>for a language too cool to abide by conventions. | 154 | <p>for a language that considers itself too “cool” to abide by |
| 155 | conventions. | ||
| 121 | </p> | 156 | </p> |
| 157 | <span id="index-language_002ddefinition-version_002c-compatibility"></span> | ||
| 122 | <dl class="def"> | 158 | <dl class="def"> |
| 123 | <dt id="index-treesit_002dlanguage_002dversion"><span class="category">Function: </span><span><strong>treesit-language-version</strong> <em>&optional min-compatible</em><a href='#index-treesit_002dlanguage_002dversion' class='copiable-anchor'> ¶</a></span></dt> | 159 | <dt id="index-treesit_002dlanguage_002dversion"><span class="category">Function: </span><span><strong>treesit-language-version</strong> <em>&optional min-compatible</em><a href='#index-treesit_002dlanguage_002dversion' class='copiable-anchor'> ¶</a></span></dt> |
| 124 | <dd><p>Tree-sitter library has a <em>language version</em>, a language | 160 | <dd><p>This function returns the version of the language-definition |
| 125 | definition’s version needs to match this version to be compatible. | 161 | Application Binary Interface (<acronym>ABI</acronym>) supported by the |
| 126 | </p> | 162 | tree-sitter library. By default, it returns the latest ABI version |
| 127 | <p>This function returns tree-sitter library’s language version. If | 163 | supported by the library, but if <var>min-compatible</var> is |
| 128 | <var>min-compatible</var> is non-nil, it returns the minimal compatible | 164 | non-<code>nil</code>, it returns the oldest ABI version which the library |
| 129 | version. | 165 | still can support. Language definition libraries must be built for |
| 166 | ABI versions between the oldest and the latest versions supported by | ||
| 167 | the tree-sitter library, otherwise the library will be unable to load | ||
| 168 | them. | ||
| 130 | </p></dd></dl> | 169 | </p></dd></dl> |
| 131 | 170 | ||
| 132 | <span id="Concrete-syntax-tree"></span><h3 class="heading">Concrete syntax tree</h3> | 171 | <span id="Concrete-syntax-tree"></span><h3 class="heading">Concrete syntax tree</h3> |
| 172 | <span id="index-syntax-tree_002c-concrete"></span> | ||
| 133 | 173 | ||
| 134 | <p>A syntax tree is what a parser generates. In a syntax tree, each node | 174 | <p>A syntax tree is what a parser generates. In a syntax tree, each node |
| 135 | represents a piece of text, and is connected to each other by a | 175 | represents a piece of text, and is connected to each other by a |
| @@ -155,31 +195,34 @@ parent-child relationship. For example, if the source text is | |||
| 155 | +------------+ +--------------+ +------------+ | 195 | +------------+ +--------------+ +------------+ |
| 156 | </pre></div> | 196 | </pre></div> |
| 157 | 197 | ||
| 158 | <p>We can also represent it in s-expression: | 198 | <p>We can also represent it as an s-expression: |
| 159 | </p> | 199 | </p> |
| 160 | <div class="example"> | 200 | <div class="example"> |
| 161 | <pre class="example">(root (expression (number) (operator) (number))) | 201 | <pre class="example">(root (expression (number) (operator) (number))) |
| 162 | </pre></div> | 202 | </pre></div> |
| 163 | 203 | ||
| 164 | <span id="Node-types"></span><h4 class="subheading">Node types</h4> | 204 | <span id="Node-types"></span><h4 class="subheading">Node types</h4> |
| 165 | 205 | <span id="index-node-types_002c-in-a-syntax-tree"></span> | |
| 166 | <span id="index-tree_002dsitter-node-type"></span> | 206 | |
| 167 | <span id="tree_002dsitter-node-type"></span><span id="index-tree_002dsitter-named-node"></span> | 207 | <span id="index-type-of-node_002c-tree_002dsitter"></span> |
| 168 | <span id="tree_002dsitter-named-node"></span><span id="index-tree_002dsitter-anonymous-node"></span> | 208 | <span id="tree_002dsitter-node-type"></span><span id="index-named-node_002c-tree_002dsitter"></span> |
| 169 | <p>Names like <code>root</code>, <code>expression</code>, <code>number</code>, | 209 | <span id="tree_002dsitter-named-node"></span><span id="index-anonymous-node_002c-tree_002dsitter"></span> |
| 170 | <code>operator</code> are nodes’ <em>type</em>. However, not all nodes in a | 210 | <p>Names like <code>root</code>, <code>expression</code>, <code>number</code>, and |
| 171 | syntax tree have a type. Nodes that don’t are <em>anonymous nodes</em>, | 211 | <code>operator</code> specify the <em>type</em> of the nodes. However, not all |
| 172 | and nodes with a type are <em>named nodes</em>. Anonymous nodes are | 212 | nodes in a syntax tree have a type. Nodes that don’t have a type are |
| 173 | tokens with fixed spellings, including punctuation characters like | 213 | known as <em>anonymous nodes</em>, and nodes with a type are <em>named |
| 174 | bracket ‘<samp>]</samp>’, and keywords like <code>return</code>. | 214 | nodes</em>. Anonymous nodes are tokens with fixed spellings, including |
| 215 | punctuation characters like bracket ‘<samp>]</samp>’, and keywords like | ||
| 216 | <code>return</code>. | ||
| 175 | </p> | 217 | </p> |
| 176 | <span id="Field-names"></span><h4 class="subheading">Field names</h4> | 218 | <span id="Field-names"></span><h4 class="subheading">Field names</h4> |
| 177 | 219 | ||
| 220 | <span id="index-field-name_002c-tree_002dsitter"></span> | ||
| 178 | <span id="index-tree_002dsitter-node-field-name"></span> | 221 | <span id="index-tree_002dsitter-node-field-name"></span> |
| 179 | <span id="tree_002dsitter-node-field-name"></span><p>To make the syntax tree easier to | 222 | <span id="tree_002dsitter-node-field-name"></span><p>To make the syntax tree easier to analyze, many language definitions |
| 180 | analyze, many language definitions assign <em>field names</em> to child | 223 | assign <em>field names</em> to child nodes. For example, a |
| 181 | nodes. For example, a <code>function_definition</code> node could have a | 224 | <code>function_definition</code> node could have a <code>declarator</code> and a |
| 182 | <code>declarator</code> and a <code>body</code>: | 225 | <code>body</code>: |
| 183 | </p> | 226 | </p> |
| 184 | <div class="example"> | 227 | <div class="example"> |
| 185 | <pre class="example">(function_definition | 228 | <pre class="example">(function_definition |
| @@ -189,39 +232,40 @@ nodes. For example, a <code>function_definition</code> node could have a | |||
| 189 | 232 | ||
| 190 | <dl class="def"> | 233 | <dl class="def"> |
| 191 | <dt id="index-treesit_002dinspect_002dmode"><span class="category">Command: </span><span><strong>treesit-inspect-mode</strong><a href='#index-treesit_002dinspect_002dmode' class='copiable-anchor'> ¶</a></span></dt> | 234 | <dt id="index-treesit_002dinspect_002dmode"><span class="category">Command: </span><span><strong>treesit-inspect-mode</strong><a href='#index-treesit_002dinspect_002dmode' class='copiable-anchor'> ¶</a></span></dt> |
| 192 | <dd><p>This minor mode displays the node that <em>starts</em> at point in | 235 | <dd><p>This minor mode displays on the mode-line the node that <em>starts</em> |
| 193 | mode-line. The mode-line will display | 236 | at point. The mode-line will display |
| 194 | </p> | 237 | </p> |
| 195 | <div class="example"> | 238 | <div class="example"> |
| 196 | <pre class="example"><var>parent</var> <var>field-name</var>: (<var>child</var> (<var>grand-child</var> (...))) | 239 | <pre class="example"><var>parent</var> <var>field</var>: (<var>child</var> (<var>grandchild</var> (…))) |
| 197 | </pre></div> | 240 | </pre></div> |
| 198 | 241 | ||
| 199 | <p><var>child</var>, <var>grand-child</var>, and <var>grand-grand-child</var>, etc, are | 242 | <p><var>child</var>, <var>grand</var>, <var>grand-grandchild</var>, etc., are nodes that |
| 200 | nodes that have their beginning at point. And <var>parent</var> is the | 243 | begin at point. <var>parent</var> is the parent node of <var>child</var>. |
| 201 | parent of <var>child</var>. | ||
| 202 | </p> | 244 | </p> |
| 203 | <p>If there is no node that starts at point, i.e., point is in the middle | 245 | <p>If there is no node that starts at point, i.e., point is in the middle |
| 204 | of a node, then the mode-line only displays the smallest node that | 246 | of a node, then the mode-line only displays the smallest node that |
| 205 | spans point, and its immediate parent. | 247 | spans the position of point, and its immediate parent. |
| 206 | </p> | 248 | </p> |
| 207 | <p>This minor mode doesn’t create parsers on its own. It simply uses the | 249 | <p>This minor mode doesn’t create parsers on its own. It simply uses the |
| 208 | first parser in <code>(treesit-parser-list)</code> (see <a href="Using-Parser.html">Using Tree-sitter Parser</a>). | 250 | first parser in <code>(treesit-parser-list)</code> (see <a href="Using-Parser.html">Using Tree-sitter Parser</a>). |
| 209 | </p></dd></dl> | 251 | </p></dd></dl> |
| 210 | 252 | ||
| 211 | <span id="Reading-the-grammar-definition"></span><h3 class="heading">Reading the grammar definition</h3> | 253 | <span id="Reading-the-grammar-definition"></span><h3 class="heading">Reading the grammar definition</h3> |
| 254 | <span id="index-reading-grammar-definition_002c-tree_002dsitter"></span> | ||
| 212 | 255 | ||
| 213 | <p>Authors of language definitions define the <em>grammar</em> of a | 256 | <p>Authors of language definitions define the <em>grammar</em> of a |
| 214 | language, and this grammar determines how does a parser construct a | 257 | programming language, which determines how a parser constructs a |
| 215 | concrete syntax tree out of the text. In order to use the syntax | 258 | concrete syntax tree out of the program text. In order to use the |
| 216 | tree effectively, we need to read the <em>grammar file</em>. | 259 | syntax tree effectively, you need to consult the <em>grammar file</em>. |
| 217 | </p> | 260 | </p> |
| 218 | <p>The grammar file is usually <code>grammar.js</code> in a language | 261 | <p>The grammar file is usually <samp>grammar.js</samp> in a language |
| 219 | definition’s project repository. The link to a language definition’s | 262 | definition’s project repository. The link to a language definition’s |
| 220 | home page can be found in tree-sitter’s homepage | 263 | home page can be found on |
| 221 | (<a href="https://tree-sitter.github.io/tree-sitter">https://tree-sitter.github.io/tree-sitter</a>). | 264 | <a href="https://tree-sitter.github.io/tree-sitter">tree-sitter’s |
| 265 | homepage</a>. | ||
| 222 | </p> | 266 | </p> |
| 223 | <p>The grammar is written in JavaScript syntax. For example, the rule | 267 | <p>The grammar definition is written in JavaScript. For example, the |
| 224 | matching a <code>function_definition</code> node looks like | 268 | rule matching a <code>function_definition</code> node looks like |
| 225 | </p> | 269 | </p> |
| 226 | <div class="example"> | 270 | <div class="example"> |
| 227 | <pre class="example">function_definition: $ => seq( | 271 | <pre class="example">function_definition: $ => seq( |
| @@ -231,12 +275,12 @@ matching a <code>function_definition</code> node looks like | |||
| 231 | ) | 275 | ) |
| 232 | </pre></div> | 276 | </pre></div> |
| 233 | 277 | ||
| 234 | <p>The rule is represented by a function that takes a single argument | 278 | <p>The rules are represented by functions that take a single argument |
| 235 | <var>$</var>, representing the whole grammar. The function itself is | 279 | <var>$</var>, representing the whole grammar. The function itself is |
| 236 | constructed by other functions: the <code>seq</code> function puts together a | 280 | constructed by other functions: the <code>seq</code> function puts together |
| 237 | sequence of children; the <code>field</code> function annotates a child with | 281 | a sequence of children; the <code>field</code> function annotates a child |
| 238 | a field name. If we write the above definition in BNF syntax, it | 282 | with a field name. If we write the above definition in the so-called |
| 239 | would look like | 283 | <em>Backus-Naur Form</em> (<acronym>BNF</acronym>) syntax, it would look like |
| 240 | </p> | 284 | </p> |
| 241 | <div class="example"> | 285 | <div class="example"> |
| 242 | <pre class="example">function_definition := | 286 | <pre class="example">function_definition := |
| @@ -252,66 +296,77 @@ would look like | |||
| 252 | body: (compound_statement)) | 296 | body: (compound_statement)) |
| 253 | </pre></div> | 297 | </pre></div> |
| 254 | 298 | ||
| 255 | <p>Below is a list of functions that one will see in a grammar | 299 | <p>Below is a list of functions that one can see in a grammar definition. |
| 256 | definition. Each function takes other rules as arguments and returns | 300 | Each function takes other rules as arguments and returns a new rule. |
| 257 | a new rule. | ||
| 258 | </p> | 301 | </p> |
| 259 | <ul> | 302 | <dl compact="compact"> |
| 260 | <li> <code>seq(rule1, rule2, ...)</code> matches each rule one after another. | 303 | <dt><span><code>seq(<var>rule1</var>, <var>rule2</var>, …)</code></span></dt> |
| 261 | 304 | <dd><p>matches each rule one after another. | |
| 262 | </li><li> <code>choice(rule1, rule2, ...)</code> matches one of the rules in its | 305 | </p></dd> |
| 263 | arguments. | 306 | <dt><span><code>choice(<var>rule1</var>, <var>rule2</var>, …)</code></span></dt> |
| 264 | 307 | <dd><p>matches one of the rules in its arguments. | |
| 265 | </li><li> <code>repeat(rule)</code> matches <var>rule</var> for <em>zero or more</em> times. | 308 | </p></dd> |
| 309 | <dt><span><code>repeat(<var>rule</var>)</code></span></dt> | ||
| 310 | <dd><p>matches <var>rule</var> for <em>zero or more</em> times. | ||
| 266 | This is like the ‘<samp>*</samp>’ operator in regular expressions. | 311 | This is like the ‘<samp>*</samp>’ operator in regular expressions. |
| 267 | 312 | </p></dd> | |
| 268 | </li><li> <code>repeat1(rule)</code> matches <var>rule</var> for <em>one or more</em> times. | 313 | <dt><span><code>repeat1(<var>rule</var>)</code></span></dt> |
| 314 | <dd><p>matches <var>rule</var> for <em>one or more</em> times. | ||
| 269 | This is like the ‘<samp>+</samp>’ operator in regular expressions. | 315 | This is like the ‘<samp>+</samp>’ operator in regular expressions. |
| 270 | 316 | </p></dd> | |
| 271 | </li><li> <code>optional(rule)</code> matches <var>rule</var> for <em>zero or one</em> time. | 317 | <dt><span><code>optional(<var>rule</var>)</code></span></dt> |
| 318 | <dd><p>matches <var>rule</var> for <em>zero or one</em> time. | ||
| 272 | This is like the ‘<samp>?</samp>’ operator in regular expressions. | 319 | This is like the ‘<samp>?</samp>’ operator in regular expressions. |
| 273 | 320 | </p></dd> | |
| 274 | </li><li> <code>field(name, rule)</code> assigns field name <var>name</var> to the child | 321 | <dt><span><code>field(<var>name</var>, <var>rule</var>)</code></span></dt> |
| 275 | node matched by <var>rule</var>. | 322 | <dd><p>assigns field name <var>name</var> to the child node matched by <var>rule</var>. |
| 276 | 323 | </p></dd> | |
| 277 | </li><li> <code>alias(rule, alias)</code> makes nodes matched by <var>rule</var> appear as | 324 | <dt><span><code>alias(<var>rule</var>, <var>alias</var>)</code></span></dt> |
| 278 | <var>alias</var> in the syntax tree generated by the parser. For example, | 325 | <dd><p>makes nodes matched by <var>rule</var> appear as <var>alias</var> in the syntax |
| 279 | 326 | tree generated by the parser. For example, | |
| 327 | </p> | ||
| 280 | <div class="example"> | 328 | <div class="example"> |
| 281 | <pre class="example">alias(preprocessor_call_exp, call_expression) | 329 | <pre class="example">alias(preprocessor_call_exp, call_expression) |
| 282 | </pre></div> | 330 | </pre></div> |
| 283 | 331 | ||
| 284 | <p>makes any node matched by <code>preprocessor_call_exp</code> to appear as | 332 | <p>makes any node matched by <code>preprocessor_call_exp</code> appear as |
| 285 | <code>call_expression</code>. | 333 | <code>call_expression</code>. |
| 286 | </p></li></ul> | 334 | </p></dd> |
| 335 | </dl> | ||
| 287 | 336 | ||
| 288 | <p>Below are grammar functions less interesting for a reader of a | 337 | <p>Below are grammar functions of lesser importance for reading a |
| 289 | language definition. | 338 | language definition. |
| 290 | </p> | 339 | </p> |
| 291 | <ul> | 340 | <dl compact="compact"> |
| 292 | <li> <code>token(rule)</code> marks <var>rule</var> to produce a single leaf node. | 341 | <dt><span><code>token(<var>rule</var>)</code></span></dt> |
| 293 | That is, instead of generating a parent node with individual child | 342 | <dd><p>marks <var>rule</var> to produce a single leaf node. That is, instead of |
| 294 | nodes under it, everything is combined into a single leaf node. | 343 | generating a parent node with individual child nodes under it, |
| 295 | 344 | everything is combined into a single leaf node. | |
| 296 | </li><li> Normally, grammar rules ignore preceding whitespaces, | 345 | </p></dd> |
| 297 | <code>token.immediate(rule)</code> changes <var>rule</var> to match only when | 346 | <dt><span><code>token.immediate(<var>rule</var>)</code></span></dt> |
| 298 | there is no preceding whitespaces. | 347 | <dd><p>Normally, grammar rules ignore preceding whitespace; this |
| 299 | 348 | changes <var>rule</var> to match only when there is no preceding | |
| 300 | </li><li> <code>prec(n, rule)</code> gives <var>rule</var> a level <var>n</var> precedence. | 349 | whitespaces. |
| 301 | 350 | </p></dd> | |
| 302 | </li><li> <code>prec.left([n,] rule)</code> marks <var>rule</var> as left-associative, | 351 | <dt><span><code>prec(<var>n</var>, <var>rule</var>)</code></span></dt> |
| 303 | optionally with level <var>n</var>. | 352 | <dd><p>gives <var>rule</var> the level-<var>n</var> precedence. |
| 304 | 353 | </p></dd> | |
| 305 | </li><li> <code>prec.right([n,] rule)</code> marks <var>rule</var> as right-associative, | 354 | <dt><span><code>prec.left([<var>n</var>,] <var>rule</var>)</code></span></dt> |
| 306 | optionally with level <var>n</var>. | 355 | <dd><p>marks <var>rule</var> as left-associative, optionally with level <var>n</var>. |
| 307 | 356 | </p></dd> | |
| 308 | </li><li> <code>prec.dynamic(n, rule)</code> is like <code>prec</code>, but the precedence | 357 | <dt><span><code>prec.right([<var>n</var>,] <var>rule</var>)</code></span></dt> |
| 309 | is applied at runtime instead. | 358 | <dd><p>marks <var>rule</var> as right-associative, optionally with level <var>n</var>. |
| 310 | </li></ul> | 359 | </p></dd> |
| 311 | 360 | <dt><span><code>prec.dynamic(<var>n</var>, <var>rule</var>)</code></span></dt> | |
| 312 | <p>The tree-sitter project talks about writing a grammar in more detail: | 361 | <dd><p>this is like <code>prec</code>, but the precedence is applied at runtime |
| 313 | <a href="https://tree-sitter.github.io/tree-sitter/creating-parsers">https://tree-sitter.github.io/tree-sitter/creating-parsers</a>. | 362 | instead. |
| 314 | Read especially “The Grammar DSL” section. | 363 | </p></dd> |
| 364 | </dl> | ||
| 365 | |||
| 366 | <p>The documentation of the tree-sitter project has | ||
| 367 | <a href="https://tree-sitter.github.io/tree-sitter/creating-parsers">more | ||
| 368 | about writing a grammar</a>. Read especially “The Grammar DSL” | ||
| 369 | section. | ||
| 315 | </p> | 370 | </p> |
| 316 | </div> | 371 | </div> |
| 317 | <hr> | 372 | <hr> |
diff --git a/admin/notes/tree-sitter/html-manual/Multiple-Languages.html b/admin/notes/tree-sitter/html-manual/Multiple-Languages.html index 1ee2df7f442..eac142921f1 100644 --- a/admin/notes/tree-sitter/html-manual/Multiple-Languages.html +++ b/admin/notes/tree-sitter/html-manual/Multiple-Languages.html | |||
| @@ -33,7 +33,7 @@ developing GNU and promoting software freedom." --> | |||
| 33 | <link href="Index.html" rel="index" title="Index"> | 33 | <link href="Index.html" rel="index" title="Index"> |
| 34 | <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents"> | 34 | <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents"> |
| 35 | <link href="Parsing-Program-Source.html" rel="up" title="Parsing Program Source"> | 35 | <link href="Parsing-Program-Source.html" rel="up" title="Parsing Program Source"> |
| 36 | <link href="Tree_002dsitter-C-API.html" rel="next" title="Tree-sitter C API"> | 36 | <link href="Tree_002dsitter-major-modes.html" rel="next" title="Tree-sitter major modes"> |
| 37 | <link href="Pattern-Matching.html" rel="prev" title="Pattern Matching"> | 37 | <link href="Pattern-Matching.html" rel="prev" title="Pattern Matching"> |
| 38 | <style type="text/css"> | 38 | <style type="text/css"> |
| 39 | <!-- | 39 | <!-- |
| @@ -63,27 +63,29 @@ ul.no-bullet {list-style: none} | |||
| 63 | <div class="section" id="Multiple-Languages"> | 63 | <div class="section" id="Multiple-Languages"> |
| 64 | <div class="header"> | 64 | <div class="header"> |
| 65 | <p> | 65 | <p> |
| 66 | Next: <a href="Tree_002dsitter-C-API.html" accesskey="n" rel="next">Tree-sitter C API Correspondence</a>, Previous: <a href="Pattern-Matching.html" accesskey="p" rel="prev">Pattern Matching Tree-sitter Nodes</a>, Up: <a href="Parsing-Program-Source.html" accesskey="u" rel="up">Parsing Program Source</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Index.html" title="Index" rel="index">Index</a>]</p> | 66 | Next: <a href="Tree_002dsitter-major-modes.html" accesskey="n" rel="next">Developing major modes with tree-sitter</a>, Previous: <a href="Pattern-Matching.html" accesskey="p" rel="prev">Pattern Matching Tree-sitter Nodes</a>, Up: <a href="Parsing-Program-Source.html" accesskey="u" rel="up">Parsing Program Source</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Index.html" title="Index" rel="index">Index</a>]</p> |
| 67 | </div> | 67 | </div> |
| 68 | <hr> | 68 | <hr> |
| 69 | <span id="Parsing-Text-in-Multiple-Languages"></span><h3 class="section">37.6 Parsing Text in Multiple Languages</h3> | 69 | <span id="Parsing-Text-in-Multiple-Languages"></span><h3 class="section">37.6 Parsing Text in Multiple Languages</h3> |
| 70 | 70 | ||
| 71 | <p>Sometimes, the source of a programming language could contain sources | 71 | <span id="index-multiple-languages_002c-parsing-with-tree_002dsitter"></span> |
| 72 | of other languages, HTML + CSS + JavaScript is one example. In that | 72 | <span id="index-parsing-multiple-languages-with-tree_002dsitter"></span> |
| 73 | case, we need to assign individual parsers to text segments written in | 73 | <p>Sometimes, the source of a programming language could contain snippets |
| 74 | different languages. Traditionally this is achieved by using | 74 | of other languages; <acronym>HTML</acronym> + <acronym>CSS</acronym> + JavaScript is one |
| 75 | narrowing. While tree-sitter works with narrowing (see <a href="Using-Parser.html#tree_002dsitter-narrowing">narrowing</a>), the recommended way is to set ranges in which | 75 | example. In that case, text segments written in different languages |
| 76 | a parser will operate. | 76 | need to be assigned different parsers. Traditionally, this is |
| 77 | achieved by using narrowing. While tree-sitter works with narrowing | ||
| 78 | (see <a href="Using-Parser.html#tree_002dsitter-narrowing">narrowing</a>), the recommended way is | ||
| 79 | instead to set regions of buffer text in which a parser will operate. | ||
| 77 | </p> | 80 | </p> |
| 78 | <dl class="def"> | 81 | <dl class="def"> |
| 79 | <dt id="index-treesit_002dparser_002dset_002dincluded_002dranges"><span class="category">Function: </span><span><strong>treesit-parser-set-included-ranges</strong> <em>parser ranges</em><a href='#index-treesit_002dparser_002dset_002dincluded_002dranges' class='copiable-anchor'> ¶</a></span></dt> | 82 | <dt id="index-treesit_002dparser_002dset_002dincluded_002dranges"><span class="category">Function: </span><span><strong>treesit-parser-set-included-ranges</strong> <em>parser ranges</em><a href='#index-treesit_002dparser_002dset_002dincluded_002dranges' class='copiable-anchor'> ¶</a></span></dt> |
| 80 | <dd><p>This function sets the range of <var>parser</var> to <var>ranges</var>. Then | 83 | <dd><p>This function sets up <var>parser</var> to operate on <var>ranges</var>. The |
| 81 | <var>parser</var> will only read the text covered in each range. Each | 84 | <var>parser</var> will only read the text of the specified ranges. Each |
| 82 | range in <var>ranges</var> is a list of cons <code>(<var>beg</var> | 85 | range in <var>ranges</var> is a list of the form <code>(<var>beg</var> . <var>end</var>)</code><!-- /@w -->. |
| 83 | . <var>end</var>)</code>. | ||
| 84 | </p> | 86 | </p> |
| 85 | <p>Each range in <var>ranges</var> must come in order and not overlap. That | 87 | <p>The ranges in <var>ranges</var> must come in order and must not overlap. |
| 86 | is, in pseudo code: | 88 | That is, in pseudo code: |
| 87 | </p> | 89 | </p> |
| 88 | <div class="example"> | 90 | <div class="example"> |
| 89 | <pre class="example">(cl-loop for idx from 1 to (1- (length ranges)) | 91 | <pre class="example">(cl-loop for idx from 1 to (1- (length ranges)) |
| @@ -95,12 +97,12 @@ is, in pseudo code: | |||
| 95 | 97 | ||
| 96 | <span id="index-treesit_002drange_002dinvalid"></span> | 98 | <span id="index-treesit_002drange_002dinvalid"></span> |
| 97 | <p>If <var>ranges</var> violates this constraint, or something else went | 99 | <p>If <var>ranges</var> violates this constraint, or something else went |
| 98 | wrong, this function signals a <code>treesit-range-invalid</code>. The | 100 | wrong, this function signals the <code>treesit-range-invalid</code> error. |
| 99 | signal data contains a specific error message and the ranges we are | 101 | The signal data contains a specific error message and the ranges we |
| 100 | trying to set. | 102 | are trying to set. |
| 101 | </p> | 103 | </p> |
| 102 | <p>This function can also be used for disabling ranges. If <var>ranges</var> | 104 | <p>This function can also be used for disabling ranges. If <var>ranges</var> |
| 103 | is nil, the parser is set to parse the whole buffer. | 105 | is <code>nil</code>, the parser is set to parse the whole buffer. |
| 104 | </p> | 106 | </p> |
| 105 | <p>Example: | 107 | <p>Example: |
| 106 | </p> | 108 | </p> |
| @@ -114,9 +116,9 @@ is nil, the parser is set to parse the whole buffer. | |||
| 114 | <dt id="index-treesit_002dparser_002dincluded_002dranges"><span class="category">Function: </span><span><strong>treesit-parser-included-ranges</strong> <em>parser</em><a href='#index-treesit_002dparser_002dincluded_002dranges' class='copiable-anchor'> ¶</a></span></dt> | 116 | <dt id="index-treesit_002dparser_002dincluded_002dranges"><span class="category">Function: </span><span><strong>treesit-parser-included-ranges</strong> <em>parser</em><a href='#index-treesit_002dparser_002dincluded_002dranges' class='copiable-anchor'> ¶</a></span></dt> |
| 115 | <dd><p>This function returns the ranges set for <var>parser</var>. The return | 117 | <dd><p>This function returns the ranges set for <var>parser</var>. The return |
| 116 | value is the same as the <var>ranges</var> argument of | 118 | value is the same as the <var>ranges</var> argument of |
| 117 | <code>treesit-parser-included-ranges</code>: a list of cons | 119 | <code>treesit-parser-included-ranges</code>: a list of cons cells of the form |
| 118 | <code>(<var>beg</var> . <var>end</var>)</code>. And if <var>parser</var> doesn’t have any | 120 | <code>(<var>beg</var> . <var>end</var>)</code><!-- /@w -->. If <var>parser</var> doesn’t have any |
| 119 | ranges, the return value is nil. | 121 | ranges, the return value is <code>nil</code>. |
| 120 | </p> | 122 | </p> |
| 121 | <div class="example"> | 123 | <div class="example"> |
| 122 | <pre class="example">(treesit-parser-included-ranges parser) | 124 | <pre class="example">(treesit-parser-included-ranges parser) |
| @@ -131,7 +133,7 @@ the ranges of <var>parser-or-lang</var> to <var>ranges</var>. Conveniently, | |||
| 131 | <var>parser-or-lang</var> could be either a parser or a language. If it is | 133 | <var>parser-or-lang</var> could be either a parser or a language. If it is |
| 132 | a language, this function looks for the first parser in | 134 | a language, this function looks for the first parser in |
| 133 | <code>(treesit-parser-list)</code> for that language in the current buffer, | 135 | <code>(treesit-parser-list)</code> for that language in the current buffer, |
| 134 | and set range for it. | 136 | and sets the ranges for it. |
| 135 | </p></dd></dl> | 137 | </p></dd></dl> |
| 136 | 138 | ||
| 137 | <dl class="def"> | 139 | <dl class="def"> |
| @@ -145,68 +147,76 @@ a language symbol. | |||
| 145 | <dl class="def"> | 147 | <dl class="def"> |
| 146 | <dt id="index-treesit_002dquery_002drange"><span class="category">Function: </span><span><strong>treesit-query-range</strong> <em>source query &optional beg end</em><a href='#index-treesit_002dquery_002drange' class='copiable-anchor'> ¶</a></span></dt> | 148 | <dt id="index-treesit_002dquery_002drange"><span class="category">Function: </span><span><strong>treesit-query-range</strong> <em>source query &optional beg end</em><a href='#index-treesit_002dquery_002drange' class='copiable-anchor'> ¶</a></span></dt> |
| 147 | <dd><p>This function matches <var>source</var> with <var>query</var> and returns the | 149 | <dd><p>This function matches <var>source</var> with <var>query</var> and returns the |
| 148 | ranges of captured nodes. The return value has the same shape of | 150 | ranges of captured nodes. The return value is a list of cons cells of |
| 149 | other functions: a list of <code>(<var>beg</var> . <var>end</var>)</code>. | 151 | the form <code>(<var>beg</var> . <var>end</var>)</code><!-- /@w -->, where <var>beg</var> and |
| 152 | <var>end</var> specify the beginning and the end of a region of text. | ||
| 150 | </p> | 153 | </p> |
| 151 | <p>For convenience, <var>source</var> can be a language symbol, a parser, or a | 154 | <p>For convenience, <var>source</var> can be a language symbol, a parser, or a |
| 152 | node. If a language symbol, this function matches in the root node of | 155 | node. If it’s a language symbol, this function matches in the root |
| 153 | the first parser using that language; if a parser, this function | 156 | node of the first parser using that language; if a parser, this |
| 154 | matches in the root node of that parser; if a node, this function | 157 | function matches in the root node of that parser; if a node, this |
| 155 | matches in that node. | 158 | function matches in that node. |
| 156 | </p> | 159 | </p> |
| 157 | <p>Parameter <var>query</var> is the query used to capture nodes | 160 | <p>The argument <var>query</var> is the query used to capture nodes |
| 158 | (see <a href="Pattern-Matching.html">Pattern Matching Tree-sitter Nodes</a>). The capture names don’t matter. Parameter | 161 | (see <a href="Pattern-Matching.html">Pattern Matching Tree-sitter Nodes</a>). The capture names don’t matter. The |
| 159 | <var>beg</var> and <var>end</var>, if both non-nil, limits the range in which | 162 | arguments <var>beg</var> and <var>end</var>, if both non-<code>nil</code>, limit the |
| 160 | this function queries. | 163 | range in which this function queries. |
| 161 | </p> | 164 | </p> |
| 162 | <p>Like other query functions, this function raises an | 165 | <p>Like other query functions, this function raises the |
| 163 | <var>treesit-query-error</var> if <var>query</var> is malformed. | 166 | <code>treesit-query-error</code> error if <var>query</var> is malformed. |
| 164 | </p></dd></dl> | ||
| 165 | |||
| 166 | <dl class="def"> | ||
| 167 | <dt id="index-treesit_002dlanguage_002dat"><span class="category">Function: </span><span><strong>treesit-language-at</strong> <em>point</em><a href='#index-treesit_002dlanguage_002dat' class='copiable-anchor'> ¶</a></span></dt> | ||
| 168 | <dd><p>This function tries to figure out which language is responsible for | ||
| 169 | the text at <var>point</var>. It goes over each parser in | ||
| 170 | <code>(treesit-parser-list)</code> and see if that parser’s range covers | ||
| 171 | <var>point</var>. | ||
| 172 | </p></dd></dl> | 167 | </p></dd></dl> |
| 173 | 168 | ||
| 174 | <dl class="def"> | 169 | <dl class="def"> |
| 175 | <dt id="index-treesit_002drange_002dfunctions"><span class="category">Variable: </span><span><strong>treesit-range-functions</strong><a href='#index-treesit_002drange_002dfunctions' class='copiable-anchor'> ¶</a></span></dt> | 170 | <dt id="index-treesit_002drange_002dfunctions"><span class="category">Variable: </span><span><strong>treesit-range-functions</strong><a href='#index-treesit_002drange_002dfunctions' class='copiable-anchor'> ¶</a></span></dt> |
| 176 | <dd><p>A list of range functions. Font-locking and indenting code uses | 171 | <dd><p>This variable holds the list of range functions. Font-locking and |
| 177 | functions in this alist to set correct ranges for a language parser | 172 | indenting code use functions in this list to set correct ranges for |
| 178 | before using it. | 173 | a language parser before using it. |
| 179 | </p> | 174 | </p> |
| 180 | <p>The signature of each function should be | 175 | <p>The signature of each function in the list should be: |
| 181 | </p> | 176 | </p> |
| 182 | <div class="example"> | 177 | <div class="example"> |
| 183 | <pre class="example">(<var>start</var> <var>end</var> &rest <var>_</var>) | 178 | <pre class="example">(<var>start</var> <var>end</var> &rest <var>_</var>) |
| 184 | </pre></div> | 179 | </pre></div> |
| 185 | 180 | ||
| 186 | <p>where <var>start</var> and <var>end</var> marks the region that is about to be | 181 | <p>where <var>start</var> and <var>end</var> specify the region that is about to be |
| 187 | used. A range function only need to (but not limited to) update | 182 | used. A range function only needs to (but is not limited to) update |
| 188 | ranges in that region. | 183 | ranges in that region. |
| 189 | </p> | 184 | </p> |
| 190 | <p>Each function in the list is called in-order. | 185 | <p>The functions in the list are called in order. |
| 191 | </p></dd></dl> | 186 | </p></dd></dl> |
| 192 | 187 | ||
| 193 | <dl class="def"> | 188 | <dl class="def"> |
| 194 | <dt id="index-treesit_002dupdate_002dranges"><span class="category">Function: </span><span><strong>treesit-update-ranges</strong> <em>&optional start end</em><a href='#index-treesit_002dupdate_002dranges' class='copiable-anchor'> ¶</a></span></dt> | 189 | <dt id="index-treesit_002dupdate_002dranges"><span class="category">Function: </span><span><strong>treesit-update-ranges</strong> <em>&optional start end</em><a href='#index-treesit_002dupdate_002dranges' class='copiable-anchor'> ¶</a></span></dt> |
| 195 | <dd><p>This function is used by font-lock and indent to update ranges before | 190 | <dd><p>This function is used by font-lock and indentation to update ranges |
| 196 | using any parser. Each range function in | 191 | before using any parser. Each range function in |
| 197 | <var>treesit-range-functions</var> is called in-order. Arguments | 192 | <var>treesit-range-functions</var> is called in-order. Arguments |
| 198 | <var>start</var> and <var>end</var> are passed to each range function. | 193 | <var>start</var> and <var>end</var> are passed to each range function. |
| 199 | </p></dd></dl> | 194 | </p></dd></dl> |
| 200 | 195 | ||
| 196 | <span id="index-treesit_002dlanguage_002dat_002dpoint_002dfunction"></span> | ||
| 197 | <dl class="def"> | ||
| 198 | <dt id="index-treesit_002dlanguage_002dat"><span class="category">Function: </span><span><strong>treesit-language-at</strong> <em>pos</em><a href='#index-treesit_002dlanguage_002dat' class='copiable-anchor'> ¶</a></span></dt> | ||
| 199 | <dd><p>This function tries to figure out which language is responsible for | ||
| 200 | the text at buffer position <var>pos</var>. Under the hood it just calls | ||
| 201 | <code>treesit-language-at-point-function</code>. | ||
| 202 | </p> | ||
| 203 | <p>Various Lisp programs use this function. For example, the indentation | ||
| 204 | program uses this function to determine which language’s rule to use | ||
| 205 | in a multi-language buffer. So it is important to provide | ||
| 206 | <code>treesit-language-at-point-function</code> for a multi-language major | ||
| 207 | mode. | ||
| 208 | </p></dd></dl> | ||
| 209 | |||
| 201 | <span id="An-example"></span><h3 class="heading">An example</h3> | 210 | <span id="An-example"></span><h3 class="heading">An example</h3> |
| 202 | 211 | ||
| 203 | <p>Normally, in a set of languages that can be mixed together, there is a | 212 | <p>Normally, in a set of languages that can be mixed together, there is a |
| 204 | major language and several embedded languages. We first parse the | 213 | major language and several embedded languages. A Lisp program usually |
| 205 | whole document with the major language’s parser, set ranges for the | 214 | first parses the whole document with the major language’s parser, sets |
| 206 | embedded languages, then parse the embedded languages. | 215 | ranges for the embedded languages, and then parses the embedded |
| 216 | languages. | ||
| 207 | </p> | 217 | </p> |
| 208 | <p>Suppose we want to parse a very simple document that mixes HTML, CSS | 218 | <p>Suppose we need to parse a very simple document that mixes |
| 209 | and JavaScript: | 219 | <acronym>HTML</acronym>, <acronym>CSS</acronym> and JavaScript: |
| 210 | </p> | 220 | </p> |
| 211 | <div class="example"> | 221 | <div class="example"> |
| 212 | <pre class="example"><html> | 222 | <pre class="example"><html> |
| @@ -215,22 +225,25 @@ and JavaScript: | |||
| 215 | </html> | 225 | </html> |
| 216 | </pre></div> | 226 | </pre></div> |
| 217 | 227 | ||
| 218 | <p>We first parse with HTML, then set ranges for CSS and JavaScript: | 228 | <p>We first parse with <acronym>HTML</acronym>, then set ranges for <acronym>CSS</acronym> |
| 229 | and JavaScript: | ||
| 219 | </p> | 230 | </p> |
| 220 | <div class="example"> | 231 | <div class="example"> |
| 221 | <pre class="example">;; Create parsers. | 232 | <pre class="example">;; Create parsers. |
| 222 | (setq html (treesit-get-parser-create 'html)) | 233 | (setq html (treesit-get-parser-create 'html)) |
| 223 | (setq css (treesit-get-parser-create 'css)) | 234 | (setq css (treesit-get-parser-create 'css)) |
| 224 | (setq js (treesit-get-parser-create 'javascript)) | 235 | (setq js (treesit-get-parser-create 'javascript)) |
| 236 | </pre><pre class="example"> | ||
| 225 | 237 | ||
| 226 | ;; Set CSS ranges. | 238 | </pre><pre class="example">;; Set CSS ranges. |
| 227 | (setq css-range | 239 | (setq css-range |
| 228 | (treesit-query-range | 240 | (treesit-query-range |
| 229 | 'html | 241 | 'html |
| 230 | "(style_element (raw_text) @capture)")) | 242 | "(style_element (raw_text) @capture)")) |
| 231 | (treesit-parser-set-included-ranges css css-range) | 243 | (treesit-parser-set-included-ranges css css-range) |
| 244 | </pre><pre class="example"> | ||
| 232 | 245 | ||
| 233 | ;; Set JavaScript ranges. | 246 | </pre><pre class="example">;; Set JavaScript ranges. |
| 234 | (setq js-range | 247 | (setq js-range |
| 235 | (treesit-query-range | 248 | (treesit-query-range |
| 236 | 'html | 249 | 'html |
| @@ -238,15 +251,15 @@ and JavaScript: | |||
| 238 | (treesit-parser-set-included-ranges js js-range) | 251 | (treesit-parser-set-included-ranges js js-range) |
| 239 | </pre></div> | 252 | </pre></div> |
| 240 | 253 | ||
| 241 | <p>We use a query pattern <code>(style_element (raw_text) @capture)</code> to | 254 | <p>We use a query pattern <code><span class="nolinebreak">(style_element</span> <span class="nolinebreak">(raw_text)</span> @capture)</code><!-- /@w --> |
| 242 | find CSS nodes in the HTML parse tree. For how to write query | 255 | to find <acronym>CSS</acronym> nodes in the <acronym>HTML</acronym> parse tree. For how |
| 243 | patterns, see <a href="Pattern-Matching.html">Pattern Matching Tree-sitter Nodes</a>. | 256 | to write query patterns, see <a href="Pattern-Matching.html">Pattern Matching Tree-sitter Nodes</a>. |
| 244 | </p> | 257 | </p> |
| 245 | </div> | 258 | </div> |
| 246 | <hr> | 259 | <hr> |
| 247 | <div class="header"> | 260 | <div class="header"> |
| 248 | <p> | 261 | <p> |
| 249 | Next: <a href="Tree_002dsitter-C-API.html">Tree-sitter C API Correspondence</a>, Previous: <a href="Pattern-Matching.html">Pattern Matching Tree-sitter Nodes</a>, Up: <a href="Parsing-Program-Source.html">Parsing Program Source</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Index.html" title="Index" rel="index">Index</a>]</p> | 262 | Next: <a href="Tree_002dsitter-major-modes.html">Developing major modes with tree-sitter</a>, Previous: <a href="Pattern-Matching.html">Pattern Matching Tree-sitter Nodes</a>, Up: <a href="Parsing-Program-Source.html">Parsing Program Source</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Index.html" title="Index" rel="index">Index</a>]</p> |
| 250 | </div> | 263 | </div> |
| 251 | 264 | ||
| 252 | 265 | ||
diff --git a/admin/notes/tree-sitter/html-manual/Parser_002dbased-Font-Lock.html b/admin/notes/tree-sitter/html-manual/Parser_002dbased-Font-Lock.html index c91152edc0e..4f2933c985d 100644 --- a/admin/notes/tree-sitter/html-manual/Parser_002dbased-Font-Lock.html +++ b/admin/notes/tree-sitter/html-manual/Parser_002dbased-Font-Lock.html | |||
| @@ -66,47 +66,78 @@ Previous: <a href="Multiline-Font-Lock.html" accesskey="p" rel="prev">Multiline | |||
| 66 | </div> | 66 | </div> |
| 67 | <hr> | 67 | <hr> |
| 68 | <span id="Parser_002dbased-Font-Lock-1"></span><h4 class="subsection">24.6.10 Parser-based Font Lock</h4> | 68 | <span id="Parser_002dbased-Font-Lock-1"></span><h4 class="subsection">24.6.10 Parser-based Font Lock</h4> |
| 69 | <span id="index-parser_002dbased-font_002dlock"></span> | ||
| 69 | 70 | ||
| 70 | 71 | ||
| 71 | <p>Besides simple syntactic font lock and regexp-based font lock, Emacs | 72 | <p>Besides simple syntactic font lock and regexp-based font lock, Emacs |
| 72 | also provides complete syntactic font lock with the help of a parser, | 73 | also provides complete syntactic font lock with the help of a parser. |
| 73 | currently provided by the tree-sitter library (see <a href="Parsing-Program-Source.html">Parsing Program Source</a>). | 74 | Currently, Emacs uses the tree-sitter library (see <a href="Parsing-Program-Source.html">Parsing Program Source</a>) for this purpose. |
| 74 | </p> | 75 | </p> |
| 75 | <dl class="def"> | 76 | <p>Parser-based font lock and other font lock mechanisms are not mutually |
| 76 | <dt id="index-treesit_002dfont_002dlock_002denable"><span class="category">Function: </span><span><strong>treesit-font-lock-enable</strong><a href='#index-treesit_002dfont_002dlock_002denable' class='copiable-anchor'> ¶</a></span></dt> | ||
| 77 | <dd><p>This function enables parser-based font lock in the current buffer. | ||
| 78 | </p></dd></dl> | ||
| 79 | |||
| 80 | <p>Parser-based font lock and other font lock mechanism are not mutually | ||
| 81 | exclusive. By default, if enabled, parser-based font lock runs first, | 77 | exclusive. By default, if enabled, parser-based font lock runs first, |
| 82 | then the simple syntactic font lock (if enabled), then regexp-based | 78 | replacing syntactic font lock, then the regexp-based font lock. |
| 83 | font lock. | ||
| 84 | </p> | 79 | </p> |
| 85 | <p>Although parser-based font lock doesn’t share the same customization | 80 | <p>Although parser-based font lock doesn’t share the same customization |
| 86 | variables with regexp-based font lock, parser-based font lock uses | 81 | variables with regexp-based font lock, it uses similar customization |
| 87 | similar customization schemes. The tree-sitter counterpart of | 82 | schemes. The tree-sitter counterpart of <var>font-lock-keywords</var> is |
| 88 | <var>font-lock-keywords</var> is <var>treesit-font-lock-settings</var>. | 83 | <var>treesit-font-lock-settings</var>. |
| 84 | </p> | ||
| 85 | <span id="index-tree_002dsitter-fontifications_002c-overview"></span> | ||
| 86 | <span id="index-fontifications-with-tree_002dsitter_002c-overview"></span> | ||
| 87 | <p>In general, tree-sitter fontification works as follows: | ||
| 88 | </p> | ||
| 89 | <ul> | ||
| 90 | <li> A Lisp program (usually, part of a major mode) provides a <em>query</em> | ||
| 91 | consisting of <em>patterns</em>, each pattern associated with a | ||
| 92 | <em>capture name</em>. | ||
| 93 | |||
| 94 | </li><li> The tree-sitter library finds the nodes in the parse tree | ||
| 95 | that match these patterns, tags the nodes with the corresponding | ||
| 96 | capture names, and returns them to the Lisp program. | ||
| 97 | |||
| 98 | </li><li> The Lisp program uses the returned nodes to highlight the portions of | ||
| 99 | buffer text corresponding to each node as appropriate, using the | ||
| 100 | tagged capture names of the nodes to determine the correct | ||
| 101 | fontification. For example, a node tagged <code>font-lock-keyword</code> | ||
| 102 | would be highlighted in <code>font-lock-keyword</code> face. | ||
| 103 | </li></ul> | ||
| 104 | |||
| 105 | <p>For more information about queries, patterns, and capture names, see | ||
| 106 | <a href="Pattern-Matching.html">Pattern Matching Tree-sitter Nodes</a>. | ||
| 107 | </p> | ||
| 108 | <p>To setup tree-sitter fontification, a major mode should first set | ||
| 109 | <code>treesit-font-lock-settings</code> with the output of | ||
| 110 | <code>treesit-font-lock-rules</code>, then call | ||
| 111 | <code>treesit-major-mode-setup</code>. | ||
| 89 | </p> | 112 | </p> |
| 90 | <dl class="def"> | 113 | <dl class="def"> |
| 91 | <dt id="index-treesit_002dfont_002dlock_002drules"><span class="category">Function: </span><span><strong>treesit-font-lock-rules</strong> <em>:keyword value query...</em><a href='#index-treesit_002dfont_002dlock_002drules' class='copiable-anchor'> ¶</a></span></dt> | 114 | <dt id="index-treesit_002dfont_002dlock_002drules"><span class="category">Function: </span><span><strong>treesit-font-lock-rules</strong> <em>:keyword value query...</em><a href='#index-treesit_002dfont_002dlock_002drules' class='copiable-anchor'> ¶</a></span></dt> |
| 92 | <dd><p>This function is used to set <var>treesit-font-lock-settings</var>. It | 115 | <dd><p>This function is used to set <var>treesit-font-lock-settings</var>. It |
| 93 | takes care of compiling queries and other post-processing and outputs | 116 | takes care of compiling queries and other post-processing, and outputs |
| 94 | a value that <var>treesit-font-lock-settings</var> accepts. An example: | 117 | a value that <var>treesit-font-lock-settings</var> accepts. Here’s an |
| 118 | example: | ||
| 95 | </p> | 119 | </p> |
| 96 | <div class="example"> | 120 | <div class="example"> |
| 97 | <pre class="example">(treesit-font-lock-rules | 121 | <pre class="example">(treesit-font-lock-rules |
| 98 | :language 'javascript | 122 | :language 'javascript |
| 123 | :feature 'constant | ||
| 99 | :override t | 124 | :override t |
| 100 | '((true) @font-lock-constant-face | 125 | '((true) @font-lock-constant-face |
| 101 | (false) @font-lock-constant-face) | 126 | (false) @font-lock-constant-face) |
| 102 | :language 'html | 127 | :language 'html |
| 128 | :feature 'script | ||
| 103 | "(script_element) @font-lock-builtin-face") | 129 | "(script_element) @font-lock-builtin-face") |
| 104 | </pre></div> | 130 | </pre></div> |
| 105 | 131 | ||
| 106 | <p>This function takes a list of text or s-exp queries. Before each | 132 | <p>This function takes a list of text or s-exp queries. Before each |
| 107 | query, there are <var>:keyword</var> and <var>value</var> pairs that configure | 133 | query, there are <var>:keyword</var>-<var>value</var> pairs that configure |
| 108 | that query. The <code>:lang</code> keyword sets the query’s language and | 134 | that query. The <code>:lang</code> keyword sets the query’s language and |
| 109 | every query must specify the language. Other keywords are optional: | 135 | every query must specify the language. The <code>:feature</code> keyword |
| 136 | sets the feature name of the query. Users can control which features | ||
| 137 | are enabled with <code>font-lock-maximum-decoration</code> and | ||
| 138 | <code>treesit-font-lock-feature-list</code> (see below). | ||
| 139 | </p> | ||
| 140 | <p>Other keywords are optional: | ||
| 110 | </p> | 141 | </p> |
| 111 | <table> | 142 | <table> |
| 112 | <thead><tr><th width="15%">Keyword</th><th width="15%">Value</th><th width="60%">Description</th></tr></thead> | 143 | <thead><tr><th width="15%">Keyword</th><th width="15%">Value</th><th width="60%">Description</th></tr></thead> |
| @@ -115,43 +146,92 @@ every query must specify the language. Other keywords are optional: | |||
| 115 | <tr><td width="15%"></td><td width="15%"><code>append</code></td><td width="60%">Append the new face to existing ones</td></tr> | 146 | <tr><td width="15%"></td><td width="15%"><code>append</code></td><td width="60%">Append the new face to existing ones</td></tr> |
| 116 | <tr><td width="15%"></td><td width="15%"><code>prepend</code></td><td width="60%">Prepend the new face to existing ones</td></tr> | 147 | <tr><td width="15%"></td><td width="15%"><code>prepend</code></td><td width="60%">Prepend the new face to existing ones</td></tr> |
| 117 | <tr><td width="15%"></td><td width="15%"><code>keep</code></td><td width="60%">Fill-in regions without an existing face</td></tr> | 148 | <tr><td width="15%"></td><td width="15%"><code>keep</code></td><td width="60%">Fill-in regions without an existing face</td></tr> |
| 118 | <tr><td width="15%"><code>:toggle</code></td><td width="15%"><var>symbol</var></td><td width="60%">If non-nil, its value should be a variable name. The variable’s value | ||
| 119 | (nil/non-nil) disables/enables the query during fontification.</td></tr> | ||
| 120 | <tr><td width="15%"></td><td width="15%">nil</td><td width="60%">Always enable this query.</td></tr> | ||
| 121 | <tr><td width="15%"><code>:level</code></td><td width="15%"><var>integer</var></td><td width="60%">If non-nil, its value should be the decoration level for this query. | ||
| 122 | Decoration level is controlled by <code>font-lock-maximum-decoration</code>.</td></tr> | ||
| 123 | <tr><td width="15%"></td><td width="15%">nil</td><td width="60%">Always enable this query.</td></tr> | ||
| 124 | </table> | 149 | </table> |
| 125 | 150 | ||
| 126 | <p>Note that a query is applied only when both <code>:toggle</code> and | 151 | <p>Lisp programs mark patterns in the query with capture names (names |
| 127 | <code>:level</code> permit it. <code>:level</code> is used for global, | 152 | that starts with <code>@</code>), and tree-sitter will return matched nodes |
| 128 | coarse-grained control, whereas <code>:toggle</code> is for local, | 153 | tagged with those same capture names. For the purpose of |
| 129 | fine-grained control. | 154 | fontification, capture names in <var>query</var> should be face names like |
| 130 | </p> | ||
| 131 | <p>Capture names in <var>query</var> should be face names like | ||
| 132 | <code>font-lock-keyword-face</code>. The captured node will be fontified | 155 | <code>font-lock-keyword-face</code>. The captured node will be fontified |
| 133 | with that face. Capture names can also be function names, in which | 156 | with that face. |
| 134 | case the function is called with (<var>start</var> <var>end</var> <var>node</var>), | 157 | </p> |
| 135 | where <var>start</var> and <var>end</var> are the start and end position of the | 158 | <span id="index-treesit_002dfontify_002dwith_002doverride"></span> |
| 136 | node in buffer, and <var>node</var> is the node itself. If a capture name | 159 | <p>Capture names can also be function names, in which case the function |
| 137 | is both a face and a function, the face takes priority. If a capture | 160 | is called with 4 arguments: <var>node</var> and <var>override</var>, <var>start</var> |
| 138 | name is not a face name nor a function name, it is ignored. | 161 | and <var>end</var>, where <var>node</var> is the node itself, <var>override</var> is |
| 162 | the override property of the rule which captured this node, and | ||
| 163 | <var>start</var> and <var>end</var> limits the region in which this function | ||
| 164 | should fontify. (If this function wants to respect the <var>override</var> | ||
| 165 | argument, it can use <code>treesit-fontify-with-override</code>.) | ||
| 166 | </p> | ||
| 167 | <p>Beyond the 4 arguments presented, this function should accept more | ||
| 168 | arguments as optional arguments for future extensibility. | ||
| 169 | </p> | ||
| 170 | <p>If a capture name is both a face and a function, the face takes | ||
| 171 | priority. If a capture name is neither a face nor a function, it is | ||
| 172 | ignored. | ||
| 139 | </p></dd></dl> | 173 | </p></dd></dl> |
| 140 | 174 | ||
| 175 | <p>Contextual entities, like multi-line strings, or <code>/* */</code> style | ||
| 176 | comments, need special care, because change in these entities might | ||
| 177 | cause change in a large portion of the buffer. For example, inserting | ||
| 178 | the closing comment delimiter <code>*/</code> will change all the text | ||
| 179 | between it and the opening delimiter to comment face. Such entities | ||
| 180 | should be captured in a special name <code>contextual</code>, so Emacs can | ||
| 181 | correctly update their fontification. Here is an example for | ||
| 182 | comments: | ||
| 183 | </p> | ||
| 184 | <div class="example"> | ||
| 185 | <pre class="example">(treesit-font-lock-rules | ||
| 186 | :language 'javascript | ||
| 187 | :feature 'comment | ||
| 188 | :override t | ||
| 189 | '((comment) @font-lock-comment-face) | ||
| 190 | (comment) @contextual)) | ||
| 191 | </pre></div> | ||
| 192 | |||
| 141 | <dl class="def"> | 193 | <dl class="def"> |
| 142 | <dt id="index-treesit_002dfont_002dlock_002dsettings"><span class="category">Variable: </span><span><strong>treesit-font-lock-settings</strong><a href='#index-treesit_002dfont_002dlock_002dsettings' class='copiable-anchor'> ¶</a></span></dt> | 194 | <dt id="index-treesit_002dfont_002dlock_002dfeature_002dlist"><span class="category">Variable: </span><span><strong>treesit-font-lock-feature-list</strong><a href='#index-treesit_002dfont_002dlock_002dfeature_002dlist' class='copiable-anchor'> ¶</a></span></dt> |
| 143 | <dd><p>A list of <var>setting</var>s for tree-sitter font lock. The exact format | 195 | <dd><p>This is a list of lists of feature symbols. Each element of the list |
| 144 | of this variable is considered internal. One should always use | 196 | is a list that represents a decoration level. |
| 145 | <code>treesit-font-lock-rules</code> to set this variable. | 197 | <code>font-lock-maximum-decoration</code> controls which levels are |
| 198 | activated. | ||
| 146 | </p> | 199 | </p> |
| 147 | <p>Each <var>setting</var> is of form | 200 | <p>Each element of the list is a list of the form <code>(<var>feature</var> …)</code><!-- /@w -->, where each <var>feature</var> corresponds to the |
| 201 | <code>:feature</code> value of a query defined in | ||
| 202 | <code>treesit-font-lock-rules</code>. Removing a feature symbol from this | ||
| 203 | list disables the corresponding query during font-lock. | ||
| 148 | </p> | 204 | </p> |
| 149 | <div class="example"> | 205 | <p>Common feature names, for many programming languages, include |
| 150 | <pre class="example">(<var>language</var> <var>query</var>) | 206 | function-name, type, variable-name (left-hand-side or <acronym>LHS</acronym> of |
| 207 | assignments), builtin, constant, keyword, string-interpolation, | ||
| 208 | comment, doc, string, operator, preprocessor, escape-sequence, and key | ||
| 209 | (in key-value pairs). Major modes are free to subdivide or extend | ||
| 210 | these common features. | ||
| 211 | </p> | ||
| 212 | <p>For example, the value of this variable could be: | ||
| 213 | </p><div class="example"> | ||
| 214 | <pre class="example">((comment string doc) ; level 1 | ||
| 215 | (function-name keyword type builtin constant) ; level 2 | ||
| 216 | (variable-name string-interpolation key)) ; level 3 | ||
| 151 | </pre></div> | 217 | </pre></div> |
| 152 | 218 | ||
| 153 | <p>Each <var>setting</var> controls one parser (often of different language). | 219 | <p>Major modes should set this variable before calling |
| 154 | And <var>language</var> is the language symbol (see <a href="Language-Definitions.html">Tree-sitter Language Definitions</a>); <var>query</var> is the query (see <a href="Pattern-Matching.html">Pattern Matching Tree-sitter Nodes</a>). | 220 | <code>treesit-major-mode-setup</code>. |
| 221 | </p> | ||
| 222 | <span id="index-treesit_002dfont_002dlock_002drecompute_002dfeatures"></span> | ||
| 223 | <p>For this variable to take effect, a Lisp program should call | ||
| 224 | <code>treesit-font-lock-recompute-features</code> (which resets | ||
| 225 | <code>treesit-font-lock-settings</code> accordingly), or | ||
| 226 | <code>treesit-major-mode-setup</code> (which calls | ||
| 227 | <code>treesit-font-lock-recompute-features</code>). | ||
| 228 | </p></dd></dl> | ||
| 229 | |||
| 230 | <dl class="def"> | ||
| 231 | <dt id="index-treesit_002dfont_002dlock_002dsettings"><span class="category">Variable: </span><span><strong>treesit-font-lock-settings</strong><a href='#index-treesit_002dfont_002dlock_002dsettings' class='copiable-anchor'> ¶</a></span></dt> | ||
| 232 | <dd><p>A list of settings for tree-sitter based font lock. The exact format | ||
| 233 | of this variable is considered internal. One should always use | ||
| 234 | <code>treesit-font-lock-rules</code> to set this variable. | ||
| 155 | </p></dd></dl> | 235 | </p></dd></dl> |
| 156 | 236 | ||
| 157 | <p>Multi-language major modes should provide range functions in | 237 | <p>Multi-language major modes should provide range functions in |
diff --git a/admin/notes/tree-sitter/html-manual/Parser_002dbased-Indentation.html b/admin/notes/tree-sitter/html-manual/Parser_002dbased-Indentation.html index 691c8fba8c7..2fdb50df7c1 100644 --- a/admin/notes/tree-sitter/html-manual/Parser_002dbased-Indentation.html +++ b/admin/notes/tree-sitter/html-manual/Parser_002dbased-Indentation.html | |||
| @@ -66,170 +66,176 @@ Previous: <a href="SMIE.html" accesskey="p" rel="prev">Simple Minded Indentation | |||
| 66 | </div> | 66 | </div> |
| 67 | <hr> | 67 | <hr> |
| 68 | <span id="Parser_002dbased-Indentation-1"></span><h4 class="subsection">24.7.2 Parser-based Indentation</h4> | 68 | <span id="Parser_002dbased-Indentation-1"></span><h4 class="subsection">24.7.2 Parser-based Indentation</h4> |
| 69 | <span id="index-parser_002dbased-indentation"></span> | ||
| 69 | 70 | ||
| 70 | 71 | ||
| 71 | <p>When built with the tree-sitter library (see <a href="Parsing-Program-Source.html">Parsing Program Source</a>), Emacs could parse program source and produce a syntax tree. | 72 | <p>When built with the tree-sitter library (see <a href="Parsing-Program-Source.html">Parsing Program Source</a>), Emacs is capable of parsing the program source and producing |
| 72 | And this syntax tree can be used for indentation. For maximum | 73 | a syntax tree. This syntax tree can be used for guiding the program |
| 73 | flexibility, we could write a custom indent function that queries the | 74 | source indentation commands. For maximum flexibility, it is possible |
| 74 | syntax tree and indents accordingly for each language, but that would | 75 | to write a custom indentation function that queries the syntax tree |
| 75 | be a lot of work. It is more convenient to use the simple indentation | 76 | and indents accordingly for each language, but that is a lot of work. |
| 76 | engine described below: we only need to write some indentation rules | 77 | It is more convenient to use the simple indentation engine described |
| 78 | below: then the major mode needs only to write some indentation rules | ||
| 77 | and the engine takes care of the rest. | 79 | and the engine takes care of the rest. |
| 78 | </p> | 80 | </p> |
| 79 | <p>To enable the indentation engine, set the value of | 81 | <p>To enable the parser-based indentation engine, either set |
| 82 | <var>treesit-simple-indent-rules</var> and call | ||
| 83 | <code>treesit-major-mode-setup</code>, or equivalently, set the value of | ||
| 80 | <code>indent-line-function</code> to <code>treesit-indent</code>. | 84 | <code>indent-line-function</code> to <code>treesit-indent</code>. |
| 81 | </p> | 85 | </p> |
| 82 | <dl class="def"> | 86 | <dl class="def"> |
| 83 | <dt id="index-treesit_002dindent_002dfunction"><span class="category">Variable: </span><span><strong>treesit-indent-function</strong><a href='#index-treesit_002dindent_002dfunction' class='copiable-anchor'> ¶</a></span></dt> | 87 | <dt id="index-treesit_002dindent_002dfunction"><span class="category">Variable: </span><span><strong>treesit-indent-function</strong><a href='#index-treesit_002dindent_002dfunction' class='copiable-anchor'> ¶</a></span></dt> |
| 84 | <dd><p>This variable stores the actual function called by | 88 | <dd><p>This variable stores the actual function called by |
| 85 | <code>treesit-indent</code>. By default, its value is | 89 | <code>treesit-indent</code>. By default, its value is |
| 86 | <code>treesit-simple-indent</code>. In the future we might add other | 90 | <code>treesit-simple-indent</code>. In the future we might add other, |
| 87 | more complex indentation engines. | 91 | more complex indentation engines. |
| 88 | </p></dd></dl> | 92 | </p></dd></dl> |
| 89 | 93 | ||
| 90 | <span id="Writing-indentation-rules"></span><h3 class="heading">Writing indentation rules</h3> | 94 | <span id="Writing-indentation-rules"></span><h3 class="heading">Writing indentation rules</h3> |
| 95 | <span id="index-indentation-rules_002c-for-parser_002dbased-indentation"></span> | ||
| 91 | 96 | ||
| 92 | <dl class="def"> | 97 | <dl class="def"> |
| 93 | <dt id="index-treesit_002dsimple_002dindent_002drules"><span class="category">Variable: </span><span><strong>treesit-simple-indent-rules</strong><a href='#index-treesit_002dsimple_002dindent_002drules' class='copiable-anchor'> ¶</a></span></dt> | 98 | <dt id="index-treesit_002dsimple_002dindent_002drules"><span class="category">Variable: </span><span><strong>treesit-simple-indent-rules</strong><a href='#index-treesit_002dsimple_002dindent_002drules' class='copiable-anchor'> ¶</a></span></dt> |
| 94 | <dd><p>This local variable stores indentation rules for every language. It is | 99 | <dd><p>This local variable stores indentation rules for every language. It is |
| 95 | a list of | 100 | a list of the form: <code>(<var>language</var> . <var>rules</var>)</code><!-- /@w -->, where |
| 96 | </p> | 101 | <var>language</var> is a language symbol, and <var>rules</var> is a list of the |
| 97 | <div class="example"> | 102 | form <code>(<var>matcher</var> <var>anchor</var> <var>offset</var>)</code><!-- /@w -->. |
| 98 | <pre class="example">(<var>language</var> . <var>rules</var>) | 103 | </p> |
| 99 | </pre></div> | 104 | <p>First, Emacs passes the smallest tree-sitter node at the beginning of |
| 100 | 105 | the current line to <var>matcher</var>; if it returns non-<code>nil</code>, this | |
| 101 | <p>where <var>language</var> is a language symbol, and <var>rules</var> is a list | 106 | rule is applicable. Then Emacs passes the node to <var>anchor</var>, which |
| 102 | of | 107 | returns a buffer position. Emacs takes the column number of that |
| 103 | </p> | 108 | position, adds <var>offset</var> to it, and the result is the indentation |
| 104 | <div class="example"> | 109 | column for the current line. |
| 105 | <pre class="example">(<var>matcher</var> <var>anchor</var> <var>offset</var>) | ||
| 106 | </pre></div> | ||
| 107 | |||
| 108 | <p>First Emacs passes the node at point to <var>matcher</var>, if it return | ||
| 109 | non-nil, this rule applies. Then Emacs passes the node to | ||
| 110 | <var>anchor</var>, it returns a point. Emacs takes the column number of | ||
| 111 | that point, add <var>offset</var> to it, and the result is the indent for | ||
| 112 | the current line. | ||
| 113 | </p> | 110 | </p> |
| 114 | <p>The <var>matcher</var> and <var>anchor</var> are functions, and Emacs provides | 111 | <p>The <var>matcher</var> and <var>anchor</var> are functions, and Emacs provides |
| 115 | convenient presets for them. You can skip over to | 112 | convenient defaults for them. |
| 116 | <code>treesit-simple-indent-presets</code> below, those presets should be | 113 | </p> |
| 117 | more than enough. | 114 | <p>Each <var>matcher</var> or <var>anchor</var> is a function that takes three |
| 118 | </p> | 115 | arguments: <var>node</var>, <var>parent</var>, and <var>bol</var>. The argument |
| 119 | <p>A <var>matcher</var> or an <var>anchor</var> is a function that takes three | 116 | <var>bol</var> is the buffer position whose indentation is required: the |
| 120 | arguments (<var>node</var> <var>parent</var> <var>bol</var>). Argument <var>bol</var> is | 117 | position of the first non-whitespace character after the beginning of |
| 121 | the point at where we are indenting: the position of the first | 118 | the line. The argument <var>node</var> is the largest (highest-in-tree) |
| 122 | non-whitespace character from the beginning of line; <var>node</var> is the | 119 | node that starts at that position; and <var>parent</var> is the parent of |
| 123 | largest (highest-in-tree) node that starts at that point; <var>parent</var> | 120 | <var>node</var>. However, when that position is on a whitespace or inside |
| 124 | is the parent of <var>node</var>. A <var>matcher</var> returns nil/non-nil, and | 121 | a multi-line string, no node that starts at that position, so |
| 125 | <var>anchor</var> returns a point. | 122 | <var>node</var> is <code>nil</code>. In that case, <var>parent</var> would be the |
| 123 | smallest node that spans that position. | ||
| 124 | </p> | ||
| 125 | <p>Emacs finds <var>bol</var>, <var>node</var> and <var>parent</var> and | ||
| 126 | passes them to each <var>matcher</var> and <var>anchor</var>. <var>matcher</var> | ||
| 127 | should return non-<code>nil</code> if the rule is applicable, and | ||
| 128 | <var>anchor</var> should return a buffer position. | ||
| 126 | </p></dd></dl> | 129 | </p></dd></dl> |
| 127 | 130 | ||
| 128 | <dl class="def"> | 131 | <dl class="def"> |
| 129 | <dt id="index-treesit_002dsimple_002dindent_002dpresets"><span class="category">Variable: </span><span><strong>treesit-simple-indent-presets</strong><a href='#index-treesit_002dsimple_002dindent_002dpresets' class='copiable-anchor'> ¶</a></span></dt> | 132 | <dt id="index-treesit_002dsimple_002dindent_002dpresets"><span class="category">Variable: </span><span><strong>treesit-simple-indent-presets</strong><a href='#index-treesit_002dsimple_002dindent_002dpresets' class='copiable-anchor'> ¶</a></span></dt> |
| 130 | <dd><p>This is a list of presets for <var>matcher</var>s and <var>anchor</var>s in | 133 | <dd><p>This is a list of defaults for <var>matcher</var>s and <var>anchor</var>s in |
| 131 | <code>treesit-simple-indent-rules</code>. Each of them represent a function | 134 | <code>treesit-simple-indent-rules</code>. Each of them represents a function |
| 132 | that takes <var>node</var>, <var>parent</var> and <var>bol</var> as arguments. | 135 | that takes 3 arguments: <var>node</var>, <var>parent</var> and <var>bol</var>. The |
| 133 | </p> | 136 | available default functions are: |
| 134 | <div class="example"> | 137 | </p> |
| 135 | <pre class="example">no-node | 138 | <dl compact="compact"> |
| 136 | </pre></div> | 139 | <dt id='index-no_002dnode'><span><code>no-node</code><a href='#index-no_002dnode' class='copiable-anchor'> ¶</a></span></dt> |
| 137 | 140 | <dd><p>This matcher is a function that is called with 3 arguments: | |
| 138 | <p>This matcher matches the case where <var>node</var> is nil, i.e., there is | 141 | <var>node</var>, <var>parent</var>, and <var>bol</var>, and returns non-<code>nil</code>, |
| 139 | no node that starts at <var>bol</var>. This is the case when <var>bol</var> is | 142 | indicating a match, if <var>node</var> is <code>nil</code>, i.e., there is no |
| 140 | at an empty line or inside a multi-line string, etc. | 143 | node that starts at <var>bol</var>. This is the case when <var>bol</var> is on |
| 141 | </p> | 144 | an empty line or inside a multi-line string, etc. |
| 142 | <div class="example"> | 145 | </p> |
| 143 | <pre class="example">(parent-is <var>type</var>) | 146 | </dd> |
| 144 | </pre></div> | 147 | <dt id='index-parent_002dis'><span><code>parent-is</code><a href='#index-parent_002dis' class='copiable-anchor'> ¶</a></span></dt> |
| 145 | 148 | <dd><p>This matcher is a function of one argument, <var>type</var>; it returns a | |
| 146 | <p>This matcher matches if <var>parent</var>’s type is <var>type</var>. | 149 | function that is called with 3 arguments: <var>node</var>, <var>parent</var>, |
| 147 | </p> | 150 | and <var>bol</var>, and returns non-<code>nil</code> (i.e., a match) if |
| 148 | <div class="example"> | 151 | <var>parent</var>’s type matches regexp <var>type</var>. |
| 149 | <pre class="example">(node-is <var>type</var>) | 152 | </p> |
| 150 | </pre></div> | 153 | </dd> |
| 151 | 154 | <dt id='index-node_002dis'><span><code>node-is</code><a href='#index-node_002dis' class='copiable-anchor'> ¶</a></span></dt> | |
| 152 | <p>This matcher matches if <var>node</var>’s type is <var>type</var>. | 155 | <dd><p>This matcher is a function of one argument, <var>type</var>; it returns a |
| 153 | </p> | 156 | function that is called with 3 arguments: <var>node</var>, <var>parent</var>, |
| 154 | <div class="example"> | 157 | and <var>bol</var>, and returns non-<code>nil</code> if <var>node</var>’s type matches |
| 155 | <pre class="example">(query <var>query</var>) | 158 | regexp <var>type</var>. |
| 156 | </pre></div> | 159 | </p> |
| 157 | 160 | </dd> | |
| 158 | <p>This matcher matches if querying <var>parent</var> with <var>query</var> | 161 | <dt id='index-query'><span><code>query</code><a href='#index-query' class='copiable-anchor'> ¶</a></span></dt> |
| 159 | captures <var>node</var>. The capture name does not matter. | 162 | <dd><p>This matcher is a function of one argument, <var>query</var>; it returns a |
| 160 | </p> | 163 | function that is called with 3 arguments: <var>node</var>, <var>parent</var>, |
| 161 | <div class="example"> | 164 | and <var>bol</var>, and returns non-<code>nil</code> if querying <var>parent</var> |
| 162 | <pre class="example">(match <var>node-type</var> <var>parent-type</var> | 165 | with <var>query</var> captures <var>node</var> (see <a href="Pattern-Matching.html">Pattern Matching Tree-sitter Nodes</a>). |
| 163 | <var>node-field</var> <var>node-index-min</var> <var>node-index-max</var>) | 166 | </p> |
| 164 | </pre></div> | 167 | </dd> |
| 165 | 168 | <dt id='index-match'><span><code>match</code><a href='#index-match' class='copiable-anchor'> ¶</a></span></dt> | |
| 166 | <p>This matcher checks if <var>node</var>’s type is <var>node-type</var>, | 169 | <dd><p>This matcher is a function of 5 arguments: <var>node-type</var>, |
| 167 | <var>parent</var>’s type is <var>parent-type</var>, <var>node</var>’s field name in | 170 | <var>parent-type</var>, <var>node-field</var>, <var>node-index-min</var>, and |
| 168 | <var>parent</var> is <var>node-field</var>, and <var>node</var>’s index among its | 171 | <var>node-index-max</var>). It returns a function that is called with 3 |
| 169 | siblings is between <var>node-index-min</var> and <var>node-index-max</var>. If | 172 | arguments: <var>node</var>, <var>parent</var>, and <var>bol</var>, and returns |
| 170 | the value of a constraint is nil, this matcher doesn’t check for that | 173 | non-<code>nil</code> if <var>node</var>’s type matches regexp <var>node-type</var>, |
| 171 | constraint. For example, to match the first child where parent is | 174 | <var>parent</var>’s type matches regexp <var>parent-type</var>, <var>node</var>’s |
| 172 | <code>argument_list</code>, use | 175 | field name in <var>parent</var> matches regexp <var>node-field</var>, and |
| 176 | <var>node</var>’s index among its siblings is between <var>node-index-min</var> | ||
| 177 | and <var>node-index-max</var>. If the value of an argument is <code>nil</code>, | ||
| 178 | this matcher doesn’t check that argument. For example, to match the | ||
| 179 | first child where parent is <code>argument_list</code>, use | ||
| 173 | </p> | 180 | </p> |
| 174 | <div class="example"> | 181 | <div class="example"> |
| 175 | <pre class="example">(match nil "argument_list" nil nil 0 0) | 182 | <pre class="example">(match nil "argument_list" nil nil 0 0) |
| 176 | </pre></div> | 183 | </pre></div> |
| 177 | 184 | ||
| 178 | <div class="example"> | 185 | </dd> |
| 179 | <pre class="example">first-sibling | 186 | <dt id='index-first_002dsibling'><span><code>first-sibling</code><a href='#index-first_002dsibling' class='copiable-anchor'> ¶</a></span></dt> |
| 180 | </pre></div> | 187 | <dd><p>This anchor is a function that is called with 3 arguments: <var>node</var>, |
| 181 | 188 | <var>parent</var>, and <var>bol</var>, and returns the start of the first child | |
| 182 | <p>This anchor returns the start of the first child of <var>parent</var>. | 189 | of <var>parent</var>. |
| 183 | </p> | 190 | </p> |
| 184 | <div class="example"> | 191 | </dd> |
| 185 | <pre class="example">parent | 192 | <dt id='index-parent'><span><code>parent</code><a href='#index-parent' class='copiable-anchor'> ¶</a></span></dt> |
| 186 | </pre></div> | 193 | <dd><p>This anchor is a function that is called with 3 arguments: <var>node</var>, |
| 187 | 194 | <var>parent</var>, and <var>bol</var>, and returns the start of <var>parent</var>. | |
| 188 | <p>This anchor returns the start of <var>parent</var>. | 195 | </p> |
| 189 | </p> | 196 | </dd> |
| 190 | <div class="example"> | 197 | <dt id='index-parent_002dbol'><span><code>parent-bol</code><a href='#index-parent_002dbol' class='copiable-anchor'> ¶</a></span></dt> |
| 191 | <pre class="example">parent-bol | 198 | <dd><p>This anchor is a function that is called with 3 arguments: <var>node</var>, |
| 192 | </pre></div> | 199 | <var>parent</var>, and <var>bol</var>, and returns the first non-space character |
| 193 | 200 | on the line of <var>parent</var>. | |
| 194 | <p>This anchor returns the beginning of non-space characters on the line | 201 | </p> |
| 195 | where <var>parent</var> is on. | 202 | </dd> |
| 196 | </p> | 203 | <dt id='index-prev_002dsibling'><span><code>prev-sibling</code><a href='#index-prev_002dsibling' class='copiable-anchor'> ¶</a></span></dt> |
| 197 | <div class="example"> | 204 | <dd><p>This anchor is a function that is called with 3 arguments: <var>node</var>, |
| 198 | <pre class="example">prev-sibling | 205 | <var>parent</var>, and <var>bol</var>, and returns the start of the previous |
| 199 | </pre></div> | 206 | sibling of <var>node</var>. |
| 200 | 207 | </p> | |
| 201 | <p>This anchor returns the start of the previous sibling of <var>node</var>. | 208 | </dd> |
| 202 | </p> | 209 | <dt id='index-no_002dindent'><span><code>no-indent</code><a href='#index-no_002dindent' class='copiable-anchor'> ¶</a></span></dt> |
| 203 | <div class="example"> | 210 | <dd><p>This anchor is a function that is called with 3 arguments: <var>node</var>, |
| 204 | <pre class="example">no-indent | 211 | <var>parent</var>, and <var>bol</var>, and returns the start of <var>node</var>. |
| 205 | </pre></div> | 212 | </p> |
| 206 | 213 | </dd> | |
| 207 | <p>This anchor returns the start of <var>node</var>, i.e., no indent. | 214 | <dt id='index-prev_002dline'><span><code>prev-line</code><a href='#index-prev_002dline' class='copiable-anchor'> ¶</a></span></dt> |
| 208 | </p> | 215 | <dd><p>This anchor is a function that is called with 3 arguments: <var>node</var>, |
| 209 | <div class="example"> | 216 | <var>parent</var>, and <var>bol</var>, and returns the first non-whitespace |
| 210 | <pre class="example">prev-line | 217 | charater on the previous line. |
| 211 | </pre></div> | 218 | </p></dd> |
| 212 | 219 | </dl> | |
| 213 | <p>This anchor returns the first non-whitespace charater on the previous | 220 | |
| 214 | line. | 221 | </dd></dl> |
| 215 | </p></dd></dl> | ||
| 216 | 222 | ||
| 217 | <span id="Indentation-utilities"></span><h3 class="heading">Indentation utilities</h3> | 223 | <span id="Indentation-utilities"></span><h3 class="heading">Indentation utilities</h3> |
| 224 | <span id="index-utility-functions-for-parser_002dbased-indentation"></span> | ||
| 218 | 225 | ||
| 219 | <p>Here are some utility functions that can help writing indentation | 226 | <p>Here are some utility functions that can help writing parser-based |
| 220 | rules. | 227 | indentation rules. |
| 221 | </p> | 228 | </p> |
| 222 | <dl class="def"> | 229 | <dl class="def"> |
| 223 | <dt id="index-treesit_002dcheck_002dindent"><span class="category">Function: </span><span><strong>treesit-check-indent</strong> <em>mode</em><a href='#index-treesit_002dcheck_002dindent' class='copiable-anchor'> ¶</a></span></dt> | 230 | <dt id="index-treesit_002dcheck_002dindent"><span class="category">Function: </span><span><strong>treesit-check-indent</strong> <em>mode</em><a href='#index-treesit_002dcheck_002dindent' class='copiable-anchor'> ¶</a></span></dt> |
| 224 | <dd><p>This function checks current buffer’s indentation against major mode | 231 | <dd><p>This function checks the current buffer’s indentation against major |
| 225 | <var>mode</var>. It indents the current buffer in <var>mode</var> and compares | 232 | mode <var>mode</var>. It indents the current buffer according to |
| 226 | the indentation with the current indentation. Then it pops up a diff | 233 | <var>mode</var> and compares the results with the current indentation. |
| 227 | buffer showing the difference. Correct indentation (target) is in | 234 | Then it pops up a buffer showing the differences. Correct |
| 228 | green, current indentation is in red. | 235 | indentation (target) is shown in green color, current indentation is |
| 229 | </p></dd></dl> | 236 | shown in red color. </p></dd></dl> |
| 230 | 237 | ||
| 231 | <p>It is also helpful to use <code>treesit-inspect-mode</code> when writing | 238 | <p>It is also helpful to use <code>treesit-inspect-mode</code> (see <a href="Language-Definitions.html">Tree-sitter Language Definitions</a>) when writing indentation rules. |
| 232 | indentation rules. | ||
| 233 | </p> | 239 | </p> |
| 234 | </div> | 240 | </div> |
| 235 | <hr> | 241 | <hr> |
diff --git a/admin/notes/tree-sitter/html-manual/Parsing-Program-Source.html b/admin/notes/tree-sitter/html-manual/Parsing-Program-Source.html index 81b42f7f526..ea22421ac4c 100644 --- a/admin/notes/tree-sitter/html-manual/Parsing-Program-Source.html +++ b/admin/notes/tree-sitter/html-manual/Parsing-Program-Source.html | |||
| @@ -68,53 +68,50 @@ Next: <a href="Abbrevs.html" accesskey="n" rel="next">Abbrevs and Abbrev Expansi | |||
| 68 | <hr> | 68 | <hr> |
| 69 | <span id="Parsing-Program-Source-1"></span><h2 class="chapter">37 Parsing Program Source</h2> | 69 | <span id="Parsing-Program-Source-1"></span><h2 class="chapter">37 Parsing Program Source</h2> |
| 70 | 70 | ||
| 71 | <span id="index-syntax-tree_002c-from-parsing-program-source"></span> | ||
| 71 | <p>Emacs provides various ways to parse program source text and produce a | 72 | <p>Emacs provides various ways to parse program source text and produce a |
| 72 | <em>syntax tree</em>. In a syntax tree, text is no longer a | 73 | <em>syntax tree</em>. In a syntax tree, text is no longer considered a |
| 73 | one-dimensional stream but a structured tree of nodes, where each node | 74 | one-dimensional stream of characters, but a structured tree of nodes, |
| 74 | representing a piece of text. Thus a syntax tree can enable | 75 | where each node representing a piece of text. Thus, a syntax tree can |
| 75 | interesting features like precise fontification, indentation, | 76 | enable interesting features like precise fontification, indentation, |
| 76 | navigation, structured editing, etc. | 77 | navigation, structured editing, etc. |
| 77 | </p> | 78 | </p> |
| 78 | <p>Emacs has a simple facility for parsing balanced expressions | 79 | <p>Emacs has a simple facility for parsing balanced expressions |
| 79 | (see <a href="Parsing-Expressions.html">Parsing Expressions</a>). There is also SMIE library for generic | 80 | (see <a href="Parsing-Expressions.html">Parsing Expressions</a>). There is also the SMIE library for |
| 80 | navigation and indentation (see <a href="SMIE.html">Simple Minded Indentation Engine</a>). | 81 | generic navigation and indentation (see <a href="SMIE.html">Simple Minded Indentation Engine</a>). |
| 81 | </p> | 82 | </p> |
| 82 | <p>Emacs also provides integration with tree-sitter library | 83 | <p>In addition to those, Emacs also provides integration with |
| 83 | (<a href="https://tree-sitter.github.io/tree-sitter">https://tree-sitter.github.io/tree-sitter</a>) if compiled with | 84 | <a href="https://tree-sitter.github.io/tree-sitter">the tree-sitter |
| 84 | it. The tree-sitter library implements an incremental parser and has | 85 | library</a>) if support for it was compiled in. The tree-sitter library |
| 85 | support from a wide range of programming languages. | 86 | implements an incremental parser and has support from a wide range of |
| 87 | programming languages. | ||
| 86 | </p> | 88 | </p> |
| 87 | <dl class="def"> | 89 | <dl class="def"> |
| 88 | <dt id="index-treesit_002davailable_002dp"><span class="category">Function: </span><span><strong>treesit-available-p</strong><a href='#index-treesit_002davailable_002dp' class='copiable-anchor'> ¶</a></span></dt> | 90 | <dt id="index-treesit_002davailable_002dp"><span class="category">Function: </span><span><strong>treesit-available-p</strong><a href='#index-treesit_002davailable_002dp' class='copiable-anchor'> ¶</a></span></dt> |
| 89 | <dd><p>This function returns non-nil if tree-sitter features are available | 91 | <dd><p>This function returns non-<code>nil</code> if tree-sitter features are |
| 90 | for this Emacs instance. | 92 | available for the current Emacs session. |
| 91 | </p></dd></dl> | 93 | </p></dd></dl> |
| 92 | 94 | ||
| 93 | <p>For tree-sitter integration with existing Emacs features, | 95 | <p>To be able to parse the program source using the tree-sitter library |
| 94 | see <a href="Parser_002dbased-Font-Lock.html">Parser-based Font Lock</a>, <a href="Parser_002dbased-Indentation.html">Parser-based Indentation</a>, and | 96 | and access the syntax tree of the program, a Lisp program needs to |
| 95 | <a href="List-Motion.html">Moving over Balanced Expressions</a>. | 97 | load a language definition library, and create a parser for that |
| 96 | </p> | 98 | language and the current buffer. After that, the Lisp program can |
| 97 | <p>About naming convention: use “tree-sitter” when referring to it as a | 99 | query the parser about specific nodes of the syntax tree. Then, it |
| 98 | noun, like <code>python-use-tree-sitter</code>, but use “treesit” for | 100 | can access various kinds of information about each node, and search |
| 99 | prefixes, like <code>python-treesit-indent-function</code>. | 101 | for nodes using a powerful pattern-matching syntax. This chapter |
| 100 | </p> | 102 | explains how to do all this, and also how a Lisp program can work with |
| 101 | <p>To access the syntax tree of the text in a buffer, we need to first | 103 | source files that mix multiple programming languages. |
| 102 | load a language definition and create a parser with it. Next, we can | ||
| 103 | query the parser for specific nodes in the syntax tree. Then, we can | ||
| 104 | access various information about the node, and we can pattern-match a | ||
| 105 | node with a powerful syntax. Finally, we explain how to work with | ||
| 106 | source files that mixes multiple languages. The following sections | ||
| 107 | explain how to do each of the tasks in detail. | ||
| 108 | </p> | 104 | </p> |
| 109 | 105 | ||
| 110 | <ul class="section-toc"> | 106 | <ul class="section-toc"> |
| 111 | <li><a href="Language-Definitions.html" accesskey="1">Tree-sitter Language Definitions</a></li> | 107 | <li><a href="Language-Definitions.html" accesskey="1">Tree-sitter Language Definitions</a></li> |
| 112 | <li><a href="Using-Parser.html" accesskey="2">Using Tree-sitter Parser</a></li> | 108 | <li><a href="Using-Parser.html" accesskey="2">Using Tree-sitter Parser</a></li> |
| 113 | <li><a href="Retrieving-Node.html" accesskey="3">Retrieving Node</a></li> | 109 | <li><a href="Retrieving-Node.html" accesskey="3">Retrieving Node</a></li> |
| 114 | <li><a href="Accessing-Node.html" accesskey="4">Accessing Node Information</a></li> | 110 | <li><a href="Accessing-Node-Information.html" accesskey="4">Accessing Node Information</a></li> |
| 115 | <li><a href="Pattern-Matching.html" accesskey="5">Pattern Matching Tree-sitter Nodes</a></li> | 111 | <li><a href="Pattern-Matching.html" accesskey="5">Pattern Matching Tree-sitter Nodes</a></li> |
| 116 | <li><a href="Multiple-Languages.html" accesskey="6">Parsing Text in Multiple Languages</a></li> | 112 | <li><a href="Multiple-Languages.html" accesskey="6">Parsing Text in Multiple Languages</a></li> |
| 117 | <li><a href="Tree_002dsitter-C-API.html" accesskey="7">Tree-sitter C API Correspondence</a></li> | 113 | <li><a href="Tree_002dsitter-major-modes.html" accesskey="7">Developing major modes with tree-sitter</a></li> |
| 114 | <li><a href="Tree_002dsitter-C-API.html" accesskey="8">Tree-sitter C API Correspondence</a></li> | ||
| 118 | </ul> | 115 | </ul> |
| 119 | </div> | 116 | </div> |
| 120 | <hr> | 117 | <hr> |
diff --git a/admin/notes/tree-sitter/html-manual/Pattern-Matching.html b/admin/notes/tree-sitter/html-manual/Pattern-Matching.html index e14efe71629..21eb4702b12 100644 --- a/admin/notes/tree-sitter/html-manual/Pattern-Matching.html +++ b/admin/notes/tree-sitter/html-manual/Pattern-Matching.html | |||
| @@ -34,7 +34,7 @@ developing GNU and promoting software freedom." --> | |||
| 34 | <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents"> | 34 | <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents"> |
| 35 | <link href="Parsing-Program-Source.html" rel="up" title="Parsing Program Source"> | 35 | <link href="Parsing-Program-Source.html" rel="up" title="Parsing Program Source"> |
| 36 | <link href="Multiple-Languages.html" rel="next" title="Multiple Languages"> | 36 | <link href="Multiple-Languages.html" rel="next" title="Multiple Languages"> |
| 37 | <link href="Accessing-Node.html" rel="prev" title="Accessing Node"> | 37 | <link href="Accessing-Node-Information.html" rel="prev" title="Accessing Node Information"> |
| 38 | <style type="text/css"> | 38 | <style type="text/css"> |
| 39 | <!-- | 39 | <!-- |
| 40 | a.copiable-anchor {visibility: hidden; text-decoration: none; line-height: 0em} | 40 | a.copiable-anchor {visibility: hidden; text-decoration: none; line-height: 0em} |
| @@ -63,32 +63,32 @@ ul.no-bullet {list-style: none} | |||
| 63 | <div class="section" id="Pattern-Matching"> | 63 | <div class="section" id="Pattern-Matching"> |
| 64 | <div class="header"> | 64 | <div class="header"> |
| 65 | <p> | 65 | <p> |
| 66 | Next: <a href="Multiple-Languages.html" accesskey="n" rel="next">Parsing Text in Multiple Languages</a>, Previous: <a href="Accessing-Node.html" accesskey="p" rel="prev">Accessing Node Information</a>, Up: <a href="Parsing-Program-Source.html" accesskey="u" rel="up">Parsing Program Source</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Index.html" title="Index" rel="index">Index</a>]</p> | 66 | Next: <a href="Multiple-Languages.html" accesskey="n" rel="next">Parsing Text in Multiple Languages</a>, Previous: <a href="Accessing-Node-Information.html" accesskey="p" rel="prev">Accessing Node Information</a>, Up: <a href="Parsing-Program-Source.html" accesskey="u" rel="up">Parsing Program Source</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Index.html" title="Index" rel="index">Index</a>]</p> |
| 67 | </div> | 67 | </div> |
| 68 | <hr> | 68 | <hr> |
| 69 | <span id="Pattern-Matching-Tree_002dsitter-Nodes"></span><h3 class="section">37.5 Pattern Matching Tree-sitter Nodes</h3> | 69 | <span id="Pattern-Matching-Tree_002dsitter-Nodes"></span><h3 class="section">37.5 Pattern Matching Tree-sitter Nodes</h3> |
| 70 | <span id="index-pattern-matching-with-tree_002dsitter-nodes"></span> | ||
| 70 | 71 | ||
| 71 | <p>Tree-sitter let us pattern match with a small declarative language. | 72 | <span id="index-capturing_002c-tree_002dsitter-node"></span> |
| 72 | Pattern matching consists of two steps: first tree-sitter matches a | 73 | <p>Tree-sitter lets Lisp programs match patterns using a small |
| 73 | <em>pattern</em> against nodes in the syntax tree, then it <em>captures</em> | 74 | declarative language. This pattern matching consists of two steps: |
| 74 | specific nodes in that pattern and returns the captured nodes. | 75 | first tree-sitter matches a <em>pattern</em> against nodes in the syntax |
| 76 | tree, then it <em>captures</em> specific nodes that matched the pattern | ||
| 77 | and returns the captured nodes. | ||
| 75 | </p> | 78 | </p> |
| 76 | <p>We describe first how to write the most basic query pattern and how to | 79 | <p>We describe first how to write the most basic query pattern and how to |
| 77 | capture nodes in a pattern, then the pattern-match function, finally | 80 | capture nodes in a pattern, then the pattern-matching function, and |
| 78 | more advanced pattern syntax. | 81 | finally the more advanced pattern syntax. |
| 79 | </p> | 82 | </p> |
| 80 | <span id="Basic-query-syntax"></span><h3 class="heading">Basic query syntax</h3> | 83 | <span id="Basic-query-syntax"></span><h3 class="heading">Basic query syntax</h3> |
| 81 | 84 | ||
| 82 | <span id="index-Tree_002dsitter-query-syntax"></span> | 85 | <span id="index-tree_002dsitter-query-pattern-syntax"></span> |
| 83 | <span id="index-Tree_002dsitter-query-pattern"></span> | 86 | <span id="index-pattern-syntax_002c-tree_002dsitter-query"></span> |
| 87 | <span id="index-query_002c-tree_002dsitter"></span> | ||
| 84 | <p>A <em>query</em> consists of multiple <em>patterns</em>. Each pattern is an | 88 | <p>A <em>query</em> consists of multiple <em>patterns</em>. Each pattern is an |
| 85 | s-expression that matches a certain node in the syntax node. A | 89 | s-expression that matches a certain node in the syntax node. A |
| 86 | pattern has the following shape: | 90 | pattern has the form <code>(<var>type</var> (<var>child</var>…))</code><!-- /@w --> |
| 87 | </p> | 91 | </p> |
| 88 | <div class="example"> | ||
| 89 | <pre class="example">(<var>type</var> <var>child</var>...) | ||
| 90 | </pre></div> | ||
| 91 | |||
| 92 | <p>For example, a pattern that matches a <code>binary_expression</code> node that | 92 | <p>For example, a pattern that matches a <code>binary_expression</code> node that |
| 93 | contains <code>number_literal</code> child nodes would look like | 93 | contains <code>number_literal</code> child nodes would look like |
| 94 | </p> | 94 | </p> |
| @@ -96,19 +96,20 @@ contains <code>number_literal</code> child nodes would look like | |||
| 96 | <pre class="example">(binary_expression (number_literal)) | 96 | <pre class="example">(binary_expression (number_literal)) |
| 97 | </pre></div> | 97 | </pre></div> |
| 98 | 98 | ||
| 99 | <p>To <em>capture</em> a node in the query pattern above, append | 99 | <p>To <em>capture</em> a node using the query pattern above, append |
| 100 | <code>@capture-name</code> after the node pattern you want to capture. For | 100 | <code>@<var>capture-name</var></code> after the node pattern you want to |
| 101 | example, | 101 | capture. For example, |
| 102 | </p> | 102 | </p> |
| 103 | <div class="example"> | 103 | <div class="example"> |
| 104 | <pre class="example">(binary_expression (number_literal) @number-in-exp) | 104 | <pre class="example">(binary_expression (number_literal) @number-in-exp) |
| 105 | </pre></div> | 105 | </pre></div> |
| 106 | 106 | ||
| 107 | <p>captures <code>number_literal</code> nodes that are inside a | 107 | <p>captures <code>number_literal</code> nodes that are inside a |
| 108 | <code>binary_expression</code> node with capture name <code>number-in-exp</code>. | 108 | <code>binary_expression</code> node with the capture name |
| 109 | <code>number-in-exp</code>. | ||
| 109 | </p> | 110 | </p> |
| 110 | <p>We can capture the <code>binary_expression</code> node too, with capture | 111 | <p>We can capture the <code>binary_expression</code> node as well, with, for |
| 111 | name <code>biexp</code>: | 112 | example, the capture name <code>biexp</code>: |
| 112 | </p> | 113 | </p> |
| 113 | <div class="example"> | 114 | <div class="example"> |
| 114 | <pre class="example">(binary_expression | 115 | <pre class="example">(binary_expression |
| @@ -117,34 +118,40 @@ name <code>biexp</code>: | |||
| 117 | 118 | ||
| 118 | <span id="Query-function"></span><h3 class="heading">Query function</h3> | 119 | <span id="Query-function"></span><h3 class="heading">Query function</h3> |
| 119 | 120 | ||
| 120 | <p>Now we can introduce the query functions. | 121 | <span id="index-query-functions_002c-tree_002dsitter"></span> |
| 122 | <p>Now we can introduce the <em>query functions</em>. | ||
| 121 | </p> | 123 | </p> |
| 122 | <dl class="def"> | 124 | <dl class="def"> |
| 123 | <dt id="index-treesit_002dquery_002dcapture"><span class="category">Function: </span><span><strong>treesit-query-capture</strong> <em>node query &optional beg end node-only</em><a href='#index-treesit_002dquery_002dcapture' class='copiable-anchor'> ¶</a></span></dt> | 125 | <dt id="index-treesit_002dquery_002dcapture"><span class="category">Function: </span><span><strong>treesit-query-capture</strong> <em>node query &optional beg end node-only</em><a href='#index-treesit_002dquery_002dcapture' class='copiable-anchor'> ¶</a></span></dt> |
| 124 | <dd><p>This function matches patterns in <var>query</var> in <var>node</var>. | 126 | <dd><p>This function matches patterns in <var>query</var> within <var>node</var>. |
| 125 | Parameter <var>query</var> can be either a string, a s-expression, or a | 127 | The argument <var>query</var> can be either a string, a s-expression, or a |
| 126 | compiled query object. For now, we focus on the string syntax; | 128 | compiled query object. For now, we focus on the string syntax; |
| 127 | s-expression syntax and compiled query are described at the end of the | 129 | s-expression syntax and compiled query are described at the end of the |
| 128 | section. | 130 | section. |
| 129 | </p> | 131 | </p> |
| 130 | <p>Parameter <var>node</var> can also be a parser or a language symbol. A | 132 | <p>The argument <var>node</var> can also be a parser or a language symbol. A |
| 131 | parser means using its root node, a language symbol means find or | 133 | parser means using its root node, a language symbol means find or |
| 132 | create a parser for that language in the current buffer, and use the | 134 | create a parser for that language in the current buffer, and use the |
| 133 | root node. | 135 | root node. |
| 134 | </p> | 136 | </p> |
| 135 | <p>The function returns all captured nodes in a list of | 137 | <p>The function returns all the captured nodes in a list of the form |
| 136 | <code>(<var>capture_name</var> . <var>node</var>)</code>. If <var>node-only</var> is | 138 | <code>(<var><span class="nolinebreak">capture_name</span></var> . <var>node</var>)</code><!-- /@w -->. If <var>node-only</var> is |
| 137 | non-nil, a list of node is returned instead. If <var>beg</var> and | 139 | non-<code>nil</code>, it returns the list of nodes instead. By default the |
| 138 | <var>end</var> are both non-nil, this function only pattern matches nodes | 140 | entire text of <var>node</var> is searched, but if <var>beg</var> and <var>end</var> |
| 139 | in that range. | 141 | are both non-<code>nil</code>, they specify the region of buffer text where |
| 142 | this function should match nodes. Any matching node whose span | ||
| 143 | overlaps with the region between <var>beg</var> and <var>end</var> are captured, | ||
| 144 | it doesn’t have to be completely in the region. | ||
| 140 | </p> | 145 | </p> |
| 141 | <span id="index-treesit_002dquery_002derror"></span> | 146 | <span id="index-treesit_002dquery_002derror"></span> |
| 142 | <p>This function raise a <var>treesit-query-error</var> if <var>query</var> is | 147 | <span id="index-treesit_002dquery_002dvalidate"></span> |
| 143 | malformed. The signal data contains a description of the specific | 148 | <p>This function raises the <code>treesit-query-error</code> error if |
| 144 | error. You can use <code>treesit-query-validate</code> to debug the query. | 149 | <var>query</var> is malformed. The signal data contains a description of |
| 150 | the specific error. You can use <code>treesit-query-validate</code> to | ||
| 151 | validate and debug the query. | ||
| 145 | </p></dd></dl> | 152 | </p></dd></dl> |
| 146 | 153 | ||
| 147 | <p>For example, suppose <var>node</var>’s content is <code>1 + 2</code>, and | 154 | <p>For example, suppose <var>node</var>’s text is <code>1 + 2</code>, and |
| 148 | <var>query</var> is | 155 | <var>query</var> is |
| 149 | </p> | 156 | </p> |
| 150 | <div class="example"> | 157 | <div class="example"> |
| @@ -153,7 +160,7 @@ error. You can use <code>treesit-query-validate</code> to debug the query. | |||
| 153 | (number_literal) @number-in-exp) @biexp") | 160 | (number_literal) @number-in-exp) @biexp") |
| 154 | </pre></div> | 161 | </pre></div> |
| 155 | 162 | ||
| 156 | <p>Querying that query would return | 163 | <p>Matching that query would return |
| 157 | </p> | 164 | </p> |
| 158 | <div class="example"> | 165 | <div class="example"> |
| 159 | <pre class="example">(treesit-query-capture node query) | 166 | <pre class="example">(treesit-query-capture node query) |
| @@ -162,8 +169,8 @@ error. You can use <code>treesit-query-validate</code> to debug the query. | |||
| 162 | (number-in-exp . <var><node for "2"></var>)) | 169 | (number-in-exp . <var><node for "2"></var>)) |
| 163 | </pre></div> | 170 | </pre></div> |
| 164 | 171 | ||
| 165 | <p>As we mentioned earlier, a <var>query</var> could contain multiple | 172 | <p>As mentioned earlier, <var>query</var> could contain multiple patterns. |
| 166 | patterns. For example, it could have two top-level patterns: | 173 | For example, it could have two top-level patterns: |
| 167 | </p> | 174 | </p> |
| 168 | <div class="example"> | 175 | <div class="example"> |
| 169 | <pre class="example">(setq query | 176 | <pre class="example">(setq query |
| @@ -173,15 +180,15 @@ patterns. For example, it could have two top-level patterns: | |||
| 173 | 180 | ||
| 174 | <dl class="def"> | 181 | <dl class="def"> |
| 175 | <dt id="index-treesit_002dquery_002dstring"><span class="category">Function: </span><span><strong>treesit-query-string</strong> <em>string query language</em><a href='#index-treesit_002dquery_002dstring' class='copiable-anchor'> ¶</a></span></dt> | 182 | <dt id="index-treesit_002dquery_002dstring"><span class="category">Function: </span><span><strong>treesit-query-string</strong> <em>string query language</em><a href='#index-treesit_002dquery_002dstring' class='copiable-anchor'> ¶</a></span></dt> |
| 176 | <dd><p>This function parses <var>string</var> with <var>language</var>, pattern matches | 183 | <dd><p>This function parses <var>string</var> with <var>language</var>, matches its |
| 177 | its root node with <var>query</var>, and returns the result. | 184 | root node with <var>query</var>, and returns the result. |
| 178 | </p></dd></dl> | 185 | </p></dd></dl> |
| 179 | 186 | ||
| 180 | <span id="More-query-syntax"></span><h3 class="heading">More query syntax</h3> | 187 | <span id="More-query-syntax"></span><h3 class="heading">More query syntax</h3> |
| 181 | 188 | ||
| 182 | <p>Besides node type and capture, tree-sitter’s query syntax can express | 189 | <p>Besides node type and capture, tree-sitter’s pattern syntax can |
| 183 | anonymous node, field name, wildcard, quantification, grouping, | 190 | express anonymous node, field name, wildcard, quantification, |
| 184 | alternation, anchor, and predicate. | 191 | grouping, alternation, anchor, and predicate. |
| 185 | </p> | 192 | </p> |
| 186 | <span id="Anonymous-node"></span><h4 class="subheading">Anonymous node</h4> | 193 | <span id="Anonymous-node"></span><h4 class="subheading">Anonymous node</h4> |
| 187 | 194 | ||
| @@ -194,9 +201,9 @@ pattern matching (and capturing) keyword <code>return</code> would be | |||
| 194 | 201 | ||
| 195 | <span id="Wild-card"></span><h4 class="subheading">Wild card</h4> | 202 | <span id="Wild-card"></span><h4 class="subheading">Wild card</h4> |
| 196 | 203 | ||
| 197 | <p>In a query pattern, ‘<samp>(_)</samp>’ matches any named node, and ‘<samp>_</samp>’ | 204 | <p>In a pattern, ‘<samp>(_)</samp>’ matches any named node, and ‘<samp>_</samp>’ matches |
| 198 | matches any named and anonymous node. For example, to capture any | 205 | any named and anonymous node. For example, to capture any named child |
| 199 | named child of a <code>binary_expression</code> node, the pattern would be | 206 | of a <code>binary_expression</code> node, the pattern would be |
| 200 | </p> | 207 | </p> |
| 201 | <div class="example"> | 208 | <div class="example"> |
| 202 | <pre class="example">(binary_expression (_) @in_biexp) | 209 | <pre class="example">(binary_expression (_) @in_biexp) |
| @@ -204,7 +211,9 @@ named child of a <code>binary_expression</code> node, the pattern would be | |||
| 204 | 211 | ||
| 205 | <span id="Field-name"></span><h4 class="subheading">Field name</h4> | 212 | <span id="Field-name"></span><h4 class="subheading">Field name</h4> |
| 206 | 213 | ||
| 207 | <p>We can capture child nodes that has specific field names: | 214 | <p>It is possible to capture child nodes that have specific field names. |
| 215 | In the pattern below, <code>declarator</code> and <code>body</code> are field | ||
| 216 | names, indicated by the colon following them. | ||
| 208 | </p> | 217 | </p> |
| 209 | <div class="example"> | 218 | <div class="example"> |
| 210 | <pre class="example">(function_definition | 219 | <pre class="example">(function_definition |
| @@ -212,8 +221,8 @@ named child of a <code>binary_expression</code> node, the pattern would be | |||
| 212 | body: (_) @func-body) | 221 | body: (_) @func-body) |
| 213 | </pre></div> | 222 | </pre></div> |
| 214 | 223 | ||
| 215 | <p>We can also capture a node that doesn’t have certain field, say, a | 224 | <p>It is also possible to capture a node that doesn’t have a certain |
| 216 | <code>function_definition</code> without a <code>body</code> field. | 225 | field, say, a <code>function_definition</code> without a <code>body</code> field. |
| 217 | </p> | 226 | </p> |
| 218 | <div class="example"> | 227 | <div class="example"> |
| 219 | <pre class="example">(function_definition !body) @func-no-body | 228 | <pre class="example">(function_definition !body) @func-no-body |
| @@ -221,19 +230,20 @@ named child of a <code>binary_expression</code> node, the pattern would be | |||
| 221 | 230 | ||
| 222 | <span id="Quantify-node"></span><h4 class="subheading">Quantify node</h4> | 231 | <span id="Quantify-node"></span><h4 class="subheading">Quantify node</h4> |
| 223 | 232 | ||
| 233 | <span id="index-quantify-node_002c-tree_002dsitter"></span> | ||
| 224 | <p>Tree-sitter recognizes quantification operators ‘<samp>*</samp>’, ‘<samp>+</samp>’ and | 234 | <p>Tree-sitter recognizes quantification operators ‘<samp>*</samp>’, ‘<samp>+</samp>’ and |
| 225 | ‘<samp>?</samp>’. Their meanings are the same as in regular expressions: | 235 | ‘<samp>?</samp>’. Their meanings are the same as in regular expressions: |
| 226 | ‘<samp>*</samp>’ matches the preceding pattern zero or more times, ‘<samp>+</samp>’ | 236 | ‘<samp>*</samp>’ matches the preceding pattern zero or more times, ‘<samp>+</samp>’ |
| 227 | matches one or more times, and ‘<samp>?</samp>’ matches zero or one time. | 237 | matches one or more times, and ‘<samp>?</samp>’ matches zero or one time. |
| 228 | </p> | 238 | </p> |
| 229 | <p>For example, this pattern matches <code>type_declaration</code> nodes | 239 | <p>For example, the following pattern matches <code>type_declaration</code> |
| 230 | that has <em>zero or more</em> <code>long</code> keyword. | 240 | nodes that has <em>zero or more</em> <code>long</code> keyword. |
| 231 | </p> | 241 | </p> |
| 232 | <div class="example"> | 242 | <div class="example"> |
| 233 | <pre class="example">(type_declaration "long"*) @long-type | 243 | <pre class="example">(type_declaration "long"*) @long-type |
| 234 | </pre></div> | 244 | </pre></div> |
| 235 | 245 | ||
| 236 | <p>And this pattern matches a type declaration that has zero or one | 246 | <p>The following pattern matches a type declaration that has zero or one |
| 237 | <code>long</code> keyword: | 247 | <code>long</code> keyword: |
| 238 | </p> | 248 | </p> |
| 239 | <div class="example"> | 249 | <div class="example"> |
| @@ -242,8 +252,8 @@ that has <em>zero or more</em> <code>long</code> keyword. | |||
| 242 | 252 | ||
| 243 | <span id="Grouping"></span><h4 class="subheading">Grouping</h4> | 253 | <span id="Grouping"></span><h4 class="subheading">Grouping</h4> |
| 244 | 254 | ||
| 245 | <p>Similar to groups in regular expression, we can bundle patterns into a | 255 | <p>Similar to groups in regular expression, we can bundle patterns into |
| 246 | group and apply quantification operators to it. For example, to | 256 | groups and apply quantification operators to them. For example, to |
| 247 | express a comma separated list of identifiers, one could write | 257 | express a comma separated list of identifiers, one could write |
| 248 | </p> | 258 | </p> |
| 249 | <div class="example"> | 259 | <div class="example"> |
| @@ -253,9 +263,9 @@ express a comma separated list of identifiers, one could write | |||
| 253 | <span id="Alternation"></span><h4 class="subheading">Alternation</h4> | 263 | <span id="Alternation"></span><h4 class="subheading">Alternation</h4> |
| 254 | 264 | ||
| 255 | <p>Again, similar to regular expressions, we can express “match anyone | 265 | <p>Again, similar to regular expressions, we can express “match anyone |
| 256 | from this group of patterns” in the query pattern. The syntax is a | 266 | from this group of patterns” in a pattern. The syntax is a list of |
| 257 | list of patterns enclosed in square brackets. For example, to capture | 267 | patterns enclosed in square brackets. For example, to capture some |
| 258 | some keywords in C, the query pattern would be | 268 | keywords in C, the pattern would be |
| 259 | </p> | 269 | </p> |
| 260 | <div class="example"> | 270 | <div class="example"> |
| 261 | <pre class="example">[ | 271 | <pre class="example">[ |
| @@ -277,11 +287,13 @@ adjacent children: | |||
| 277 | <div class="example"> | 287 | <div class="example"> |
| 278 | <pre class="example">;; Anchor the child with the end of its parent. | 288 | <pre class="example">;; Anchor the child with the end of its parent. |
| 279 | (compound_expression (_) @last-child .) | 289 | (compound_expression (_) @last-child .) |
| 290 | </pre><pre class="example"> | ||
| 280 | 291 | ||
| 281 | ;; Anchor the child with the beginning of its parent. | 292 | </pre><pre class="example">;; Anchor the child with the beginning of its parent. |
| 282 | (compound_expression . (_) @first-child) | 293 | (compound_expression . (_) @first-child) |
| 294 | </pre><pre class="example"> | ||
| 283 | 295 | ||
| 284 | ;; Anchor two adjacent children. | 296 | </pre><pre class="example">;; Anchor two adjacent children. |
| 285 | (compound_expression | 297 | (compound_expression |
| 286 | (_) @prev-child | 298 | (_) @prev-child |
| 287 | . | 299 | . |
| @@ -293,8 +305,8 @@ nodes. | |||
| 293 | </p> | 305 | </p> |
| 294 | <span id="Predicate"></span><h4 class="subheading">Predicate</h4> | 306 | <span id="Predicate"></span><h4 class="subheading">Predicate</h4> |
| 295 | 307 | ||
| 296 | <p>We can add predicate constraints to a pattern. For example, if we use | 308 | <p>It is possible to add predicate constraints to a pattern. For |
| 297 | the following query pattern | 309 | example, with the following pattern: |
| 298 | </p> | 310 | </p> |
| 299 | <div class="example"> | 311 | <div class="example"> |
| 300 | <pre class="example">( | 312 | <pre class="example">( |
| @@ -303,33 +315,35 @@ the following query pattern | |||
| 303 | ) | 315 | ) |
| 304 | </pre></div> | 316 | </pre></div> |
| 305 | 317 | ||
| 306 | <p>Then tree-sitter only matches arrays where the first element equals to | 318 | <p>tree-sitter only matches arrays where the first element equals to |
| 307 | the last element. To attach a predicate to a pattern, we need to | 319 | the last element. To attach a predicate to a pattern, we need to |
| 308 | group then together. A predicate always starts with a ‘<samp>#</samp>’. | 320 | group them together. A predicate always starts with a ‘<samp>#</samp>’. |
| 309 | Currently there are two predicates, <code>#equal</code> and <code>#match</code>. | 321 | Currently there are two predicates, <code>#equal</code> and <code>#match</code>. |
| 310 | </p> | 322 | </p> |
| 311 | <dl class="def"> | 323 | <dl class="def"> |
| 312 | <dt id="index-equal-1"><span class="category">Predicate: </span><span><strong>equal</strong> <em>arg1 arg2</em><a href='#index-equal-1' class='copiable-anchor'> ¶</a></span></dt> | 324 | <dt id="index-equal-1"><span class="category">Predicate: </span><span><strong>equal</strong> <em>arg1 arg2</em><a href='#index-equal-1' class='copiable-anchor'> ¶</a></span></dt> |
| 313 | <dd><p>Matches if <var>arg1</var> equals to <var>arg2</var>. Arguments can be either a | 325 | <dd><p>Matches if <var>arg1</var> equals to <var>arg2</var>. Arguments can be either |
| 314 | string or a capture name. Capture names represent the text that the | 326 | strings or capture names. Capture names represent the text that the |
| 315 | captured node spans in the buffer. | 327 | captured node spans in the buffer. |
| 316 | </p></dd></dl> | 328 | </p></dd></dl> |
| 317 | 329 | ||
| 318 | <dl class="def"> | 330 | <dl class="def"> |
| 319 | <dt id="index-match"><span class="category">Predicate: </span><span><strong>match</strong> <em>regexp capture-name</em><a href='#index-match' class='copiable-anchor'> ¶</a></span></dt> | 331 | <dt id="index-match-1"><span class="category">Predicate: </span><span><strong>match</strong> <em>regexp capture-name</em><a href='#index-match-1' class='copiable-anchor'> ¶</a></span></dt> |
| 320 | <dd><p>Matches if the text that <var>capture-name</var>’s node spans in the buffer | 332 | <dd><p>Matches if the text that <var>capture-name</var>’s node spans in the buffer |
| 321 | matches regular expression <var>regexp</var>. Matching is case-sensitive. | 333 | matches regular expression <var>regexp</var>. Matching is case-sensitive. |
| 322 | </p></dd></dl> | 334 | </p></dd></dl> |
| 323 | 335 | ||
| 324 | <p>Note that a predicate can only refer to capture names appeared in the | 336 | <p>Note that a predicate can only refer to capture names that appear in |
| 325 | same pattern. Indeed, it makes little sense to refer to capture names | 337 | the same pattern. Indeed, it makes little sense to refer to capture |
| 326 | in other patterns anyway. | 338 | names in other patterns. |
| 327 | </p> | 339 | </p> |
| 328 | <span id="S_002dexpression-patterns"></span><h3 class="heading">S-expression patterns</h3> | 340 | <span id="S_002dexpression-patterns"></span><h3 class="heading">S-expression patterns</h3> |
| 329 | 341 | ||
| 330 | <p>Besides strings, Emacs provides a s-expression based syntax for query | 342 | <span id="index-tree_002dsitter-patterns-as-sexps"></span> |
| 331 | patterns. It largely resembles the string-based syntax. For example, | 343 | <span id="index-patterns_002c-tree_002dsitter_002c-in-sexp-form"></span> |
| 332 | the following pattern | 344 | <p>Besides strings, Emacs provides a s-expression based syntax for |
| 345 | tree-sitter patterns. It largely resembles the string-based syntax. | ||
| 346 | For example, the following query | ||
| 333 | </p> | 347 | </p> |
| 334 | <div class="example"> | 348 | <div class="example"> |
| 335 | <pre class="example">(treesit-query-capture | 349 | <pre class="example">(treesit-query-capture |
| @@ -353,9 +367,8 @@ the following pattern | |||
| 353 | ["return" "break"] @keyword)) | 367 | ["return" "break"] @keyword)) |
| 354 | </pre></div> | 368 | </pre></div> |
| 355 | 369 | ||
| 356 | <p>Most pattern syntax can be written directly as strange but | 370 | <p>Most patterns can be written directly as strange but nevertheless |
| 357 | never-the-less valid s-expressions. Only a few of them needs | 371 | valid s-expressions. Only a few of them needs modification: |
| 358 | modification: | ||
| 359 | </p> | 372 | </p> |
| 360 | <ul> | 373 | <ul> |
| 361 | <li> Anchor ‘<samp>.</samp>’ is written as <code>:anchor</code>. | 374 | <li> Anchor ‘<samp>.</samp>’ is written as <code>:anchor</code>. |
| @@ -386,42 +399,50 @@ change their ‘<samp>#</samp>’ to ‘<samp>:</samp>’. | |||
| 386 | 399 | ||
| 387 | <span id="Compiling-queries"></span><h3 class="heading">Compiling queries</h3> | 400 | <span id="Compiling-queries"></span><h3 class="heading">Compiling queries</h3> |
| 388 | 401 | ||
| 389 | <p>If a query will be used repeatedly, especially in tight loops, it is | 402 | <span id="index-compiling-tree_002dsitter-queries"></span> |
| 390 | important to compile that query, because a compiled query is much | 403 | <span id="index-queries_002c-compiling"></span> |
| 391 | faster than an uncompiled one. A compiled query can be used anywhere | 404 | <p>If a query is intended to be used repeatedly, especially in tight |
| 392 | a query is accepted. | 405 | loops, it is important to compile that query, because a compiled query |
| 406 | is much faster than an uncompiled one. A compiled query can be used | ||
| 407 | anywhere a query is accepted. | ||
| 393 | </p> | 408 | </p> |
| 394 | <dl class="def"> | 409 | <dl class="def"> |
| 395 | <dt id="index-treesit_002dquery_002dcompile"><span class="category">Function: </span><span><strong>treesit-query-compile</strong> <em>language query</em><a href='#index-treesit_002dquery_002dcompile' class='copiable-anchor'> ¶</a></span></dt> | 410 | <dt id="index-treesit_002dquery_002dcompile"><span class="category">Function: </span><span><strong>treesit-query-compile</strong> <em>language query</em><a href='#index-treesit_002dquery_002dcompile' class='copiable-anchor'> ¶</a></span></dt> |
| 396 | <dd><p>This function compiles <var>query</var> for <var>language</var> into a compiled | 411 | <dd><p>This function compiles <var>query</var> for <var>language</var> into a compiled |
| 397 | query object and returns it. | 412 | query object and returns it. |
| 398 | </p> | 413 | </p> |
| 399 | <p>This function raise a <var>treesit-query-error</var> if <var>query</var> is | 414 | <p>This function raises the <code>treesit-query-error</code> error if |
| 400 | malformed. The signal data contains a description of the specific | 415 | <var>query</var> is malformed. The signal data contains a description of |
| 401 | error. You can use <code>treesit-query-validate</code> to debug the query. | 416 | the specific error. You can use <code>treesit-query-validate</code> to |
| 417 | validate and debug the query. | ||
| 418 | </p></dd></dl> | ||
| 419 | |||
| 420 | <dl class="def"> | ||
| 421 | <dt id="index-treesit_002dquery_002dlanguage"><span class="category">Function: </span><span><strong>treesit-query-language</strong> <em>query</em><a href='#index-treesit_002dquery_002dlanguage' class='copiable-anchor'> ¶</a></span></dt> | ||
| 422 | <dd><p>This function return the language of <var>query</var>. | ||
| 402 | </p></dd></dl> | 423 | </p></dd></dl> |
| 403 | 424 | ||
| 404 | <dl class="def"> | 425 | <dl class="def"> |
| 405 | <dt id="index-treesit_002dquery_002dexpand"><span class="category">Function: </span><span><strong>treesit-query-expand</strong> <em>query</em><a href='#index-treesit_002dquery_002dexpand' class='copiable-anchor'> ¶</a></span></dt> | 426 | <dt id="index-treesit_002dquery_002dexpand"><span class="category">Function: </span><span><strong>treesit-query-expand</strong> <em>query</em><a href='#index-treesit_002dquery_002dexpand' class='copiable-anchor'> ¶</a></span></dt> |
| 406 | <dd><p>This function expands the s-expression <var>query</var> into a string | 427 | <dd><p>This function converts the s-expression <var>query</var> into the string |
| 407 | query. | 428 | format. |
| 408 | </p></dd></dl> | 429 | </p></dd></dl> |
| 409 | 430 | ||
| 410 | <dl class="def"> | 431 | <dl class="def"> |
| 411 | <dt id="index-treesit_002dpattern_002dexpand"><span class="category">Function: </span><span><strong>treesit-pattern-expand</strong> <em>pattern</em><a href='#index-treesit_002dpattern_002dexpand' class='copiable-anchor'> ¶</a></span></dt> | 432 | <dt id="index-treesit_002dpattern_002dexpand"><span class="category">Function: </span><span><strong>treesit-pattern-expand</strong> <em>pattern</em><a href='#index-treesit_002dpattern_002dexpand' class='copiable-anchor'> ¶</a></span></dt> |
| 412 | <dd><p>This function expands the s-expression <var>pattern</var> into a string | 433 | <dd><p>This function converts the s-expression <var>pattern</var> into the string |
| 413 | pattern. | 434 | format. |
| 414 | </p></dd></dl> | 435 | </p></dd></dl> |
| 415 | 436 | ||
| 416 | <p>Finally, tree-sitter project’s documentation about | 437 | <p>For more details, read the tree-sitter project’s documentation about |
| 417 | pattern-matching can be found at | 438 | pattern-matching, which can be found at |
| 418 | <a href="https://tree-sitter.github.io/tree-sitter/using-parsers#pattern-matching-with-queries">https://tree-sitter.github.io/tree-sitter/using-parsers#pattern-matching-with-queries</a>. | 439 | <a href="https://tree-sitter.github.io/tree-sitter/using-parsers#pattern-matching-with-queries">https://tree-sitter.github.io/tree-sitter/using-parsers#pattern-matching-with-queries</a>. |
| 419 | </p> | 440 | </p> |
| 420 | </div> | 441 | </div> |
| 421 | <hr> | 442 | <hr> |
| 422 | <div class="header"> | 443 | <div class="header"> |
| 423 | <p> | 444 | <p> |
| 424 | Next: <a href="Multiple-Languages.html">Parsing Text in Multiple Languages</a>, Previous: <a href="Accessing-Node.html">Accessing Node Information</a>, Up: <a href="Parsing-Program-Source.html">Parsing Program Source</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Index.html" title="Index" rel="index">Index</a>]</p> | 445 | Next: <a href="Multiple-Languages.html">Parsing Text in Multiple Languages</a>, Previous: <a href="Accessing-Node-Information.html">Accessing Node Information</a>, Up: <a href="Parsing-Program-Source.html">Parsing Program Source</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Index.html" title="Index" rel="index">Index</a>]</p> |
| 425 | </div> | 446 | </div> |
| 426 | 447 | ||
| 427 | 448 | ||
diff --git a/admin/notes/tree-sitter/html-manual/Retrieving-Node.html b/admin/notes/tree-sitter/html-manual/Retrieving-Node.html index 1bea0dde76b..e1de2007077 100644 --- a/admin/notes/tree-sitter/html-manual/Retrieving-Node.html +++ b/admin/notes/tree-sitter/html-manual/Retrieving-Node.html | |||
| @@ -33,7 +33,7 @@ developing GNU and promoting software freedom." --> | |||
| 33 | <link href="Index.html" rel="index" title="Index"> | 33 | <link href="Index.html" rel="index" title="Index"> |
| 34 | <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents"> | 34 | <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents"> |
| 35 | <link href="Parsing-Program-Source.html" rel="up" title="Parsing Program Source"> | 35 | <link href="Parsing-Program-Source.html" rel="up" title="Parsing Program Source"> |
| 36 | <link href="Accessing-Node.html" rel="next" title="Accessing Node"> | 36 | <link href="Accessing-Node-Information.html" rel="next" title="Accessing Node Information"> |
| 37 | <link href="Using-Parser.html" rel="prev" title="Using Parser"> | 37 | <link href="Using-Parser.html" rel="prev" title="Using Parser"> |
| 38 | <style type="text/css"> | 38 | <style type="text/css"> |
| 39 | <!-- | 39 | <!-- |
| @@ -63,77 +63,90 @@ ul.no-bullet {list-style: none} | |||
| 63 | <div class="section" id="Retrieving-Node"> | 63 | <div class="section" id="Retrieving-Node"> |
| 64 | <div class="header"> | 64 | <div class="header"> |
| 65 | <p> | 65 | <p> |
| 66 | Next: <a href="Accessing-Node.html" accesskey="n" rel="next">Accessing Node Information</a>, Previous: <a href="Using-Parser.html" accesskey="p" rel="prev">Using Tree-sitter Parser</a>, Up: <a href="Parsing-Program-Source.html" accesskey="u" rel="up">Parsing Program Source</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Index.html" title="Index" rel="index">Index</a>]</p> | 66 | Next: <a href="Accessing-Node-Information.html" accesskey="n" rel="next">Accessing Node Information</a>, Previous: <a href="Using-Parser.html" accesskey="p" rel="prev">Using Tree-sitter Parser</a>, Up: <a href="Parsing-Program-Source.html" accesskey="u" rel="up">Parsing Program Source</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Index.html" title="Index" rel="index">Index</a>]</p> |
| 67 | </div> | 67 | </div> |
| 68 | <hr> | 68 | <hr> |
| 69 | <span id="Retrieving-Node-1"></span><h3 class="section">37.3 Retrieving Node</h3> | 69 | <span id="Retrieving-Node-1"></span><h3 class="section">37.3 Retrieving Node</h3> |
| 70 | <span id="index-retrieve-node_002c-tree_002dsitter"></span> | ||
| 71 | <span id="index-tree_002dsitter_002c-find-node"></span> | ||
| 72 | <span id="index-get-node_002c-tree_002dsitter"></span> | ||
| 70 | 73 | ||
| 71 | <span id="index-tree_002dsitter-find-node"></span> | 74 | <span id="index-terminology_002c-for-tree_002dsitter-functions"></span> |
| 72 | <span id="index-tree_002dsitter-get-node"></span> | 75 | <p>Here’s some terminology and conventions we use when documenting |
| 73 | <p>Before we continue, lets go over some conventions of tree-sitter | 76 | tree-sitter functions. |
| 74 | functions. | ||
| 75 | </p> | 77 | </p> |
| 76 | <p>We talk about a node being “smaller” or “larger”, and “lower” or | 78 | <p>We talk about a node being “smaller” or “larger”, and “lower” or |
| 77 | “higher”. A smaller and lower node is lower in the syntax tree and | 79 | “higher”. A smaller and lower node is lower in the syntax tree and |
| 78 | therefore spans a smaller piece of text; a larger and higher node is | 80 | therefore spans a smaller portion of buffer text; a larger and higher |
| 79 | higher up in the syntax tree, containing many smaller nodes as its | 81 | node is higher up in the syntax tree, it contains many smaller nodes |
| 80 | children, and therefore spans a larger piece of text. | 82 | as its children, and therefore spans a larger portion of text. |
| 81 | </p> | 83 | </p> |
| 82 | <p>When a function cannot find a node, it returns nil. And for the | 84 | <p>When a function cannot find a node, it returns <code>nil</code>. For |
| 83 | convenience for function chaining, all the functions that take a node | 85 | convenience, all functions that take a node as argument and return |
| 84 | as argument and returns a node accept the node to be nil; in that | 86 | a node, also accept the node argument of <code>nil</code> and in that case |
| 85 | case, the function just returns nil. | 87 | just return <code>nil</code>. |
| 86 | </p> | 88 | </p> |
| 87 | <span id="index-treesit_002dnode_002doutdated"></span> | 89 | <span id="index-treesit_002dnode_002doutdated"></span> |
| 88 | <p>Nodes are not automatically updated when the associated buffer is | 90 | <p>Nodes are not automatically updated when the associated buffer is |
| 89 | modified. And there is no way to update a node once it is retrieved. | 91 | modified, and there is no way to update a node once it is retrieved. |
| 90 | Using an outdated node throws <code>treesit-node-outdated</code> error. | 92 | Using an outdated node signals the <code>treesit-node-outdated</code> error. |
| 91 | </p> | 93 | </p> |
| 92 | <span id="Retrieving-node-from-syntax-tree"></span><h3 class="heading">Retrieving node from syntax tree</h3> | 94 | <span id="Retrieving-node-from-syntax-tree"></span><h3 class="heading">Retrieving node from syntax tree</h3> |
| 95 | <span id="index-retrieving-tree_002dsitter-nodes"></span> | ||
| 96 | <span id="index-syntax-tree_002c-retrieving-nodes"></span> | ||
| 93 | 97 | ||
| 94 | <dl class="def"> | 98 | <dl class="def"> |
| 95 | <dt id="index-treesit_002dnode_002dat"><span class="category">Function: </span><span><strong>treesit-node-at</strong> <em>beg end &optional parser-or-lang named</em><a href='#index-treesit_002dnode_002dat' class='copiable-anchor'> ¶</a></span></dt> | 99 | <dt id="index-treesit_002dnode_002dat"><span class="category">Function: </span><span><strong>treesit-node-at</strong> <em>pos &optional parser-or-lang named</em><a href='#index-treesit_002dnode_002dat' class='copiable-anchor'> ¶</a></span></dt> |
| 96 | <dd><p>This function returns the <em>smallest</em> node that starts at or after | 100 | <dd><p>This function returns the <em>smallest</em> node that starts at or after |
| 97 | the <var>point</var>. In other words, the start of the node is equal or | 101 | the buffer position <var>pos</var>. In other words, the start of the node |
| 98 | greater than <var>point</var>. | 102 | is greater or equal to <var>pos</var>. |
| 99 | </p> | 103 | </p> |
| 100 | <p>When <var>parser-or-lang</var> is nil, this function uses the first parser | 104 | <p>When <var>parser-or-lang</var> is <code>nil</code> or omitted, this function uses |
| 101 | in <code>(treesit-parser-list)</code> in the current buffer. If | 105 | the first parser in <code>(treesit-parser-list)</code> of the current |
| 102 | <var>parser-or-lang</var> is a parser object, it use that parser; if | 106 | buffer. If <var>parser-or-lang</var> is a parser object, it uses that |
| 103 | <var>parser-or-lang</var> is a language, it finds the first parser using | 107 | parser; if <var>parser-or-lang</var> is a language, it finds the first |
| 104 | that language in <code>(treesit-parser-list)</code> and use that. | 108 | parser using that language in <code>(treesit-parser-list)</code>, and uses |
| 109 | that. | ||
| 105 | </p> | 110 | </p> |
| 106 | <p>If <var>named</var> is non-nil, this function looks for a named node | 111 | <p>If <var>named</var> is non-<code>nil</code>, this function looks for a named node |
| 107 | only (see <a href="Language-Definitions.html#tree_002dsitter-named-node">named node</a>). | 112 | only (see <a href="Language-Definitions.html#tree_002dsitter-named-node">named node</a>). |
| 108 | </p> | 113 | </p> |
| 114 | <p>When <var>pos</var> is after all the text in the buffer, technically there | ||
| 115 | is no node after <var>pos</var>. But for convenience, this function will | ||
| 116 | return the last leaf node in the parse tree. If <var>strict</var> is | ||
| 117 | non-<code>nil</code>, this function will strictly comply to the semantics and | ||
| 118 | return <var>nil</var>. | ||
| 119 | </p> | ||
| 109 | <p>Example: | 120 | <p>Example: |
| 110 | </p><div class="example"> | 121 | </p> |
| 122 | <div class="example"> | ||
| 111 | <pre class="example">;; Find the node at point in a C parser's syntax tree. | 123 | <pre class="example">;; Find the node at point in a C parser's syntax tree. |
| 112 | (treesit-node-at (point) 'c) | 124 | (treesit-node-at (point) 'c) |
| 113 | </pre></div> | 125 | ⇒ #<treesit-node (primitive_type) in 23-27> |
| 126 | </pre></div> | ||
| 114 | </dd></dl> | 127 | </dd></dl> |
| 115 | 128 | ||
| 116 | <dl class="def"> | 129 | <dl class="def"> |
| 117 | <dt id="index-treesit_002dnode_002don"><span class="category">Function: </span><span><strong>treesit-node-on</strong> <em>beg end &optional parser-or-lang named</em><a href='#index-treesit_002dnode_002don' class='copiable-anchor'> ¶</a></span></dt> | 130 | <dt id="index-treesit_002dnode_002don"><span class="category">Function: </span><span><strong>treesit-node-on</strong> <em>beg end &optional parser-or-lang named</em><a href='#index-treesit_002dnode_002don' class='copiable-anchor'> ¶</a></span></dt> |
| 118 | <dd><p>This function returns the <em>smallest</em> node that covers the span | 131 | <dd><p>This function returns the <em>smallest</em> node that covers the region |
| 119 | from <var>beg</var> to <var>end</var>. In other words, the start of the node is | 132 | of buffer text between <var>beg</var> and <var>end</var>. In other words, the |
| 120 | less or equal to <var>beg</var>, and the end of the node is greater or | 133 | start of the node is before or at <var>beg</var>, and the end of the node |
| 121 | equal to <var>end</var>. | 134 | is at or after <var>end</var>. |
| 122 | </p> | 135 | </p> |
| 123 | <p><em>Beware</em> that calling this function on an empty line that is not | 136 | <p><em>Beware:</em> calling this function on an empty line that is not |
| 124 | inside any top-level construct (function definition, etc) most | 137 | inside any top-level construct (function definition, etc.) most |
| 125 | probably will give you the root node, because the root node is the | 138 | probably will give you the root node, because the root node is the |
| 126 | smallest node that covers that empty line. Most of the time, you want | 139 | smallest node that covers that empty line. Most of the time, you want |
| 127 | to use <code>treesit-node-at</code>. | 140 | to use <code>treesit-node-at</code>, described above, instead. |
| 128 | </p> | 141 | </p> |
| 129 | <p>When <var>parser-or-lang</var> is nil, this function uses the first parser | 142 | <p>When <var>parser-or-lang</var> is <code>nil</code>, this function uses the first |
| 130 | in <code>(treesit-parser-list)</code> in the current buffer. If | 143 | parser in <code>(treesit-parser-list)</code> of the current buffer. If |
| 131 | <var>parser-or-lang</var> is a parser object, it use that parser; if | 144 | <var>parser-or-lang</var> is a parser object, it uses that parser; if |
| 132 | <var>parser-or-lang</var> is a language, it finds the first parser using | 145 | <var>parser-or-lang</var> is a language, it finds the first parser using |
| 133 | that language in <code>(treesit-parser-list)</code> and use that. | 146 | that language in <code>(treesit-parser-list)</code>, and uses that. |
| 134 | </p> | 147 | </p> |
| 135 | <p>If <var>named</var> is non-nil, this function looks for a named node only | 148 | <p>If <var>named</var> is non-<code>nil</code>, this function looks for a named node |
| 136 | (see <a href="Language-Definitions.html#tree_002dsitter-named-node">named node</a>). | 149 | only (see <a href="Language-Definitions.html#tree_002dsitter-named-node">named node</a>). |
| 137 | </p></dd></dl> | 150 | </p></dd></dl> |
| 138 | 151 | ||
| 139 | <dl class="def"> | 152 | <dl class="def"> |
| @@ -145,17 +158,21 @@ that language in <code>(treesit-parser-list)</code> and use that. | |||
| 145 | <dl class="def"> | 158 | <dl class="def"> |
| 146 | <dt id="index-treesit_002dbuffer_002droot_002dnode"><span class="category">Function: </span><span><strong>treesit-buffer-root-node</strong> <em>&optional language</em><a href='#index-treesit_002dbuffer_002droot_002dnode' class='copiable-anchor'> ¶</a></span></dt> | 159 | <dt id="index-treesit_002dbuffer_002droot_002dnode"><span class="category">Function: </span><span><strong>treesit-buffer-root-node</strong> <em>&optional language</em><a href='#index-treesit_002dbuffer_002droot_002dnode' class='copiable-anchor'> ¶</a></span></dt> |
| 147 | <dd><p>This function finds the first parser that uses <var>language</var> in | 160 | <dd><p>This function finds the first parser that uses <var>language</var> in |
| 148 | <code>(treesit-parser-list)</code> in the current buffer, and returns the | 161 | <code>(treesit-parser-list)</code> of the current buffer, and returns the |
| 149 | root node of that buffer. If it cannot find an appropriate parser, | 162 | root node generated by that parser. If it cannot find an appropriate |
| 150 | nil is returned. | 163 | parser, it returns <code>nil</code>. |
| 151 | </p></dd></dl> | 164 | </p></dd></dl> |
| 152 | 165 | ||
| 153 | <p>Once we have a node, we can retrieve other nodes from it, or query for | 166 | <p>Given a node, a Lisp program can retrieve other nodes starting from |
| 154 | information about this node. | 167 | it, or query for information about this node. |
| 155 | </p> | 168 | </p> |
| 156 | <span id="Retrieving-node-from-other-nodes"></span><h3 class="heading">Retrieving node from other nodes</h3> | 169 | <span id="Retrieving-node-from-other-nodes"></span><h3 class="heading">Retrieving node from other nodes</h3> |
| 170 | <span id="index-syntax-tree-nodes_002c-retrieving-from-other-nodes"></span> | ||
| 157 | 171 | ||
| 158 | <span id="By-kinship"></span><h4 class="subheading">By kinship</h4> | 172 | <span id="By-kinship"></span><h4 class="subheading">By kinship</h4> |
| 173 | <span id="index-kinship_002c-syntax-tree-nodes"></span> | ||
| 174 | <span id="index-nodes_002c-by-kinship"></span> | ||
| 175 | <span id="index-syntax-tree-nodes_002c-by-kinship"></span> | ||
| 159 | 176 | ||
| 160 | <dl class="def"> | 177 | <dl class="def"> |
| 161 | <dt id="index-treesit_002dnode_002dparent"><span class="category">Function: </span><span><strong>treesit-node-parent</strong> <em>node</em><a href='#index-treesit_002dnode_002dparent' class='copiable-anchor'> ¶</a></span></dt> | 178 | <dt id="index-treesit_002dnode_002dparent"><span class="category">Function: </span><span><strong>treesit-node-parent</strong> <em>node</em><a href='#index-treesit_002dnode_002dparent' class='copiable-anchor'> ¶</a></span></dt> |
| @@ -165,132 +182,162 @@ information about this node. | |||
| 165 | <dl class="def"> | 182 | <dl class="def"> |
| 166 | <dt id="index-treesit_002dnode_002dchild"><span class="category">Function: </span><span><strong>treesit-node-child</strong> <em>node n &optional named</em><a href='#index-treesit_002dnode_002dchild' class='copiable-anchor'> ¶</a></span></dt> | 183 | <dt id="index-treesit_002dnode_002dchild"><span class="category">Function: </span><span><strong>treesit-node-child</strong> <em>node n &optional named</em><a href='#index-treesit_002dnode_002dchild' class='copiable-anchor'> ¶</a></span></dt> |
| 167 | <dd><p>This function returns the <var>n</var>’th child of <var>node</var>. If | 184 | <dd><p>This function returns the <var>n</var>’th child of <var>node</var>. If |
| 168 | <var>named</var> is non-nil, then it only counts named nodes | 185 | <var>named</var> is non-<code>nil</code>, it counts only named nodes |
| 169 | (see <a href="Language-Definitions.html#tree_002dsitter-named-node">named node</a>). For example, in a node | 186 | (see <a href="Language-Definitions.html#tree_002dsitter-named-node">named node</a>). |
| 170 | that represents a string: <code>"text"</code>, there are three children | 187 | </p> |
| 171 | nodes: the opening quote <code>"</code>, the string content <code>text</code>, and | 188 | <p>For example, in a node that represents a string <code>"text"</code>, there |
| 172 | the enclosing quote <code>"</code>. Among these nodes, the first child is | 189 | are three children nodes: the opening quote <code>"</code>, the string text |
| 173 | the opening quote <code>"</code>, the first named child is the string | 190 | <code>text</code>, and the closing quote <code>"</code>. Among these nodes, the |
| 174 | content <code>text</code>. | 191 | first child is the opening quote <code>"</code>, and the first named child |
| 192 | is the string text. | ||
| 193 | </p> | ||
| 194 | <p>This function returns <code>nil</code> if there is no <var>n</var>’th child. | ||
| 195 | <var>n</var> could be negative, e.g., <code>-1</code> represents the last child. | ||
| 175 | </p></dd></dl> | 196 | </p></dd></dl> |
| 176 | 197 | ||
| 177 | <dl class="def"> | 198 | <dl class="def"> |
| 178 | <dt id="index-treesit_002dnode_002dchildren"><span class="category">Function: </span><span><strong>treesit-node-children</strong> <em>node &optional named</em><a href='#index-treesit_002dnode_002dchildren' class='copiable-anchor'> ¶</a></span></dt> | 199 | <dt id="index-treesit_002dnode_002dchildren"><span class="category">Function: </span><span><strong>treesit-node-children</strong> <em>node &optional named</em><a href='#index-treesit_002dnode_002dchildren' class='copiable-anchor'> ¶</a></span></dt> |
| 179 | <dd><p>This function returns all of <var>node</var>’s children in a list. If | 200 | <dd><p>This function returns all of <var>node</var>’s children as a list. If |
| 180 | <var>named</var> is non-nil, then it only retrieves named nodes. | 201 | <var>named</var> is non-<code>nil</code>, it retrieves only named nodes. |
| 181 | </p></dd></dl> | 202 | </p></dd></dl> |
| 182 | 203 | ||
| 183 | <dl class="def"> | 204 | <dl class="def"> |
| 184 | <dt id="index-treesit_002dnext_002dsibling"><span class="category">Function: </span><span><strong>treesit-next-sibling</strong> <em>node &optional named</em><a href='#index-treesit_002dnext_002dsibling' class='copiable-anchor'> ¶</a></span></dt> | 205 | <dt id="index-treesit_002dnext_002dsibling"><span class="category">Function: </span><span><strong>treesit-next-sibling</strong> <em>node &optional named</em><a href='#index-treesit_002dnext_002dsibling' class='copiable-anchor'> ¶</a></span></dt> |
| 185 | <dd><p>This function finds the next sibling of <var>node</var>. If <var>named</var> is | 206 | <dd><p>This function finds the next sibling of <var>node</var>. If <var>named</var> is |
| 186 | non-nil, it finds the next named sibling. | 207 | non-<code>nil</code>, it finds the next named sibling. |
| 187 | </p></dd></dl> | 208 | </p></dd></dl> |
| 188 | 209 | ||
| 189 | <dl class="def"> | 210 | <dl class="def"> |
| 190 | <dt id="index-treesit_002dprev_002dsibling"><span class="category">Function: </span><span><strong>treesit-prev-sibling</strong> <em>node &optional named</em><a href='#index-treesit_002dprev_002dsibling' class='copiable-anchor'> ¶</a></span></dt> | 211 | <dt id="index-treesit_002dprev_002dsibling"><span class="category">Function: </span><span><strong>treesit-prev-sibling</strong> <em>node &optional named</em><a href='#index-treesit_002dprev_002dsibling' class='copiable-anchor'> ¶</a></span></dt> |
| 191 | <dd><p>This function finds the previous sibling of <var>node</var>. If | 212 | <dd><p>This function finds the previous sibling of <var>node</var>. If |
| 192 | <var>named</var> is non-nil, it finds the previous named sibling. | 213 | <var>named</var> is non-<code>nil</code>, it finds the previous named sibling. |
| 193 | </p></dd></dl> | 214 | </p></dd></dl> |
| 194 | 215 | ||
| 195 | <span id="By-field-name"></span><h4 class="subheading">By field name</h4> | 216 | <span id="By-field-name"></span><h4 class="subheading">By field name</h4> |
| 217 | <span id="index-nodes_002c-by-field-name"></span> | ||
| 218 | <span id="index-syntax-tree-nodes_002c-by-field-name"></span> | ||
| 196 | 219 | ||
| 197 | <p>To make the syntax tree easier to analyze, many language definitions | 220 | <p>To make the syntax tree easier to analyze, many language definitions |
| 198 | assign <em>field names</em> to child nodes (see <a href="Language-Definitions.html#tree_002dsitter-node-field-name">field name</a>). For example, a <code>function_definition</code> node | 221 | assign <em>field names</em> to child nodes (see <a href="Language-Definitions.html#tree_002dsitter-node-field-name">field name</a>). For example, a <code>function_definition</code> node |
| 199 | could have a <code>declarator</code> and a <code>body</code>. | 222 | could have a <code>declarator</code> node and a <code>body</code> node. |
| 200 | </p> | 223 | </p> |
| 201 | <dl class="def"> | 224 | <dl class="def"> |
| 202 | <dt id="index-treesit_002dchild_002dby_002dfield_002dname"><span class="category">Function: </span><span><strong>treesit-child-by-field-name</strong> <em>node field-name</em><a href='#index-treesit_002dchild_002dby_002dfield_002dname' class='copiable-anchor'> ¶</a></span></dt> | 225 | <dt id="index-treesit_002dchild_002dby_002dfield_002dname"><span class="category">Function: </span><span><strong>treesit-child-by-field-name</strong> <em>node field-name</em><a href='#index-treesit_002dchild_002dby_002dfield_002dname' class='copiable-anchor'> ¶</a></span></dt> |
| 203 | <dd><p>This function finds the child of <var>node</var> that has <var>field-name</var> | 226 | <dd><p>This function finds the child of <var>node</var> whose field name is |
| 204 | as its field name. | 227 | <var>field-name</var>, a string. |
| 205 | </p> | 228 | </p> |
| 206 | <div class="example"> | 229 | <div class="example"> |
| 207 | <pre class="example">;; Get the child that has "body" as its field name. | 230 | <pre class="example">;; Get the child that has "body" as its field name. |
| 208 | (treesit-child-by-field-name node "body") | 231 | (treesit-child-by-field-name node "body") |
| 209 | </pre></div> | 232 | ⇒ #<treesit-node (compound_statement) in 45-89> |
| 233 | </pre></div> | ||
| 210 | </dd></dl> | 234 | </dd></dl> |
| 211 | 235 | ||
| 212 | <span id="By-position"></span><h4 class="subheading">By position</h4> | 236 | <span id="By-position"></span><h4 class="subheading">By position</h4> |
| 237 | <span id="index-nodes_002c-by-position"></span> | ||
| 238 | <span id="index-syntax-tree-nodes_002c-by-position"></span> | ||
| 213 | 239 | ||
| 214 | <dl class="def"> | 240 | <dl class="def"> |
| 215 | <dt id="index-treesit_002dfirst_002dchild_002dfor_002dpos"><span class="category">Function: </span><span><strong>treesit-first-child-for-pos</strong> <em>node pos &optional named</em><a href='#index-treesit_002dfirst_002dchild_002dfor_002dpos' class='copiable-anchor'> ¶</a></span></dt> | 241 | <dt id="index-treesit_002dfirst_002dchild_002dfor_002dpos"><span class="category">Function: </span><span><strong>treesit-first-child-for-pos</strong> <em>node pos &optional named</em><a href='#index-treesit_002dfirst_002dchild_002dfor_002dpos' class='copiable-anchor'> ¶</a></span></dt> |
| 216 | <dd><p>This function finds the first child of <var>node</var> that extends beyond | 242 | <dd><p>This function finds the first child of <var>node</var> that extends beyond |
| 217 | <var>pos</var>. “Extend beyond” means the end of the child node >= | 243 | buffer position <var>pos</var>. “Extends beyond” means the end of the |
| 218 | <var>pos</var>. This function only looks for immediate children of | 244 | child node is greater or equal to <var>pos</var>. This function only looks |
| 219 | <var>node</var>, and doesn’t look in its grand children. If <var>named</var> is | 245 | for immediate children of <var>node</var>, and doesn’t look in its |
| 220 | non-nil, it only looks for named child (see <a href="Language-Definitions.html#tree_002dsitter-named-node">named node</a>). | 246 | grandchildren. If <var>named</var> is non-<code>nil</code>, it looks for the |
| 247 | first named child (see <a href="Language-Definitions.html#tree_002dsitter-named-node">named node</a>). | ||
| 221 | </p></dd></dl> | 248 | </p></dd></dl> |
| 222 | 249 | ||
| 223 | <dl class="def"> | 250 | <dl class="def"> |
| 224 | <dt id="index-treesit_002dnode_002ddescendant_002dfor_002drange"><span class="category">Function: </span><span><strong>treesit-node-descendant-for-range</strong> <em>node beg end &optional named</em><a href='#index-treesit_002dnode_002ddescendant_002dfor_002drange' class='copiable-anchor'> ¶</a></span></dt> | 251 | <dt id="index-treesit_002dnode_002ddescendant_002dfor_002drange"><span class="category">Function: </span><span><strong>treesit-node-descendant-for-range</strong> <em>node beg end &optional named</em><a href='#index-treesit_002dnode_002ddescendant_002dfor_002drange' class='copiable-anchor'> ¶</a></span></dt> |
| 225 | <dd><p>This function finds the <em>smallest</em> child/grandchild... of | 252 | <dd><p>This function finds the <em>smallest</em> descendant node of <var>node</var> |
| 226 | <var>node</var> that spans the range from <var>beg</var> to <var>end</var>. It is | 253 | that spans the region of text between positions <var>beg</var> and |
| 227 | similar to <code>treesit-node-at</code>. If <var>named</var> is non-nil, it only | 254 | <var>end</var>. It is similar to <code>treesit-node-at</code>. If <var>named</var> |
| 228 | looks for named child. | 255 | is non-<code>nil</code>, it looks for smallest named child. |
| 229 | </p></dd></dl> | 256 | </p></dd></dl> |
| 230 | 257 | ||
| 231 | <span id="Searching-for-node"></span><h3 class="heading">Searching for node</h3> | 258 | <span id="Searching-for-node"></span><h3 class="heading">Searching for node</h3> |
| 232 | 259 | ||
| 233 | <dl class="def"> | 260 | <dl class="def"> |
| 234 | <dt id="index-treesit_002dsearch_002dsubtree"><span class="category">Function: </span><span><strong>treesit-search-subtree</strong> <em>node predicate &optional all backward limit</em><a href='#index-treesit_002dsearch_002dsubtree' class='copiable-anchor'> ¶</a></span></dt> | 261 | <dt id="index-treesit_002dsearch_002dsubtree"><span class="category">Function: </span><span><strong>treesit-search-subtree</strong> <em>node predicate &optional backward all limit</em><a href='#index-treesit_002dsearch_002dsubtree' class='copiable-anchor'> ¶</a></span></dt> |
| 235 | <dd><p>This function traverses the subtree of <var>node</var> (including | 262 | <dd><p>This function traverses the subtree of <var>node</var> (including |
| 236 | <var>node</var>), and match <var>predicate</var> with each node along the way. | 263 | <var>node</var> itself), looking for a node for which <var>predicate</var> |
| 237 | And <var>predicate</var> is a regexp that matches (case-insensitively) | 264 | returns non-<code>nil</code>. <var>predicate</var> is a regexp that is matched |
| 238 | against each node’s type, or a function that takes a node and returns | 265 | (case-insensitively) against each node’s type, or a predicate function |
| 239 | nil/non-nil. If a node matches, that node is returned, if no node | 266 | that takes a node and returns non-<code>nil</code> if the node matches. The |
| 240 | ever matches, nil is returned. | 267 | function returns the first node that matches, or <code>nil</code> if none |
| 268 | does. | ||
| 241 | </p> | 269 | </p> |
| 242 | <p>By default, this function only traverses named nodes, if <var>all</var> is | 270 | <p>By default, this function only traverses named nodes, but if <var>all</var> |
| 243 | non-nil, it traverses all nodes. If <var>backward</var> is non-nil, it | 271 | is non-<code>nil</code>, it traverses all the nodes. If <var>backward</var> is |
| 244 | traverses backwards. If <var>limit</var> is non-nil, it only traverses | 272 | non-<code>nil</code>, it traverses backwards (i.e., it visits the last child first |
| 245 | that number of levels down in the tree. | 273 | when traversing down the tree). If <var>limit</var> is non-<code>nil</code>, it |
| 274 | must be a number that limits the tree traversal to that many levels | ||
| 275 | down the tree. | ||
| 246 | </p></dd></dl> | 276 | </p></dd></dl> |
| 247 | 277 | ||
| 248 | <dl class="def"> | 278 | <dl class="def"> |
| 249 | <dt id="index-treesit_002dsearch_002dforward"><span class="category">Function: </span><span><strong>treesit-search-forward</strong> <em>start predicate &optional all backward up</em><a href='#index-treesit_002dsearch_002dforward' class='copiable-anchor'> ¶</a></span></dt> | 279 | <dt id="index-treesit_002dsearch_002dforward"><span class="category">Function: </span><span><strong>treesit-search-forward</strong> <em>start predicate &optional backward all</em><a href='#index-treesit_002dsearch_002dforward' class='copiable-anchor'> ¶</a></span></dt> |
| 250 | <dd><p>This function is somewhat similar to <code>treesit-search-subtree</code>. | 280 | <dd><p>Like <code>treesit-search-subtree</code>, this function also traverses the |
| 251 | It also traverse the parse tree and match each node with | 281 | parse tree and matches each node with <var>predicate</var> (except for |
| 252 | <var>predicate</var> (except for <var>start</var>), where <var>predicate</var> can be | 282 | <var>start</var>), where <var>predicate</var> can be a (case-insensitive) regexp |
| 253 | a (case-insensitive) regexp or a function. For a tree like the below | 283 | or a function. For a tree like the below where <var>start</var> is marked |
| 254 | where <var>start</var> is marked 1, this function traverses as numbered: | 284 | S, this function traverses as numbered from 1 to 12: |
| 255 | </p> | 285 | </p> |
| 256 | <div class="example"> | 286 | <div class="example"> |
| 257 | <pre class="example"> o | 287 | <pre class="example"> 12 |
| 258 | | | 288 | | |
| 259 | 3--------4-----------8 | 289 | S--------3----------11 |
| 260 | | | | | 290 | | | | |
| 261 | o--o-+--1 5--+--6 9---+-----12 | 291 | o--o-+--o 1--+--2 6--+-----10 |
| 262 | | | | | | | | 292 | | | | | |
| 263 | o o 2 7 +-+-+ +--+--+ | 293 | o o +-+-+ +--+--+ |
| 264 | | | | | | | 294 | | | | | | |
| 265 | 10 11 13 14 15 | 295 | 4 5 7 8 9 |
| 266 | </pre></div> | 296 | </pre></div> |
| 267 | 297 | ||
| 268 | <p>Same as in <code>treesit-search-subtree</code>, this function only searches | 298 | <p>Note that this function doesn’t traverse the subtree of <var>start</var>, |
| 269 | for named nodes by default. But if <var>all</var> is non-nil, it searches | 299 | and it always traverse leaf nodes first, then upwards. |
| 270 | for all nodes. If <var>backward</var> is non-nil, it searches backwards. | 300 | </p> |
| 301 | <p>Like <code>treesit-search-subtree</code>, this function only searches for | ||
| 302 | named nodes by default, but if <var>all</var> is non-<code>nil</code>, it | ||
| 303 | searches for all nodes. If <var>backward</var> is non-<code>nil</code>, it | ||
| 304 | searches backwards. | ||
| 305 | </p> | ||
| 306 | <p>While <code>treesit-search-subtree</code> traverses the subtree of a node, | ||
| 307 | this function starts with node <var>start</var> and traverses every node | ||
| 308 | that comes after it in the buffer position order, i.e., nodes with | ||
| 309 | start positions greater than the end position of <var>start</var>. | ||
| 271 | </p> | 310 | </p> |
| 272 | <p>If <var>up</var> is non-nil, this function will only traverse to siblings | 311 | <p>In the tree shown above, <code>treesit-search-subtree</code> traverses node |
| 273 | and parents. In that case, only 1 3 4 8 would be traversed. | 312 | S (<var>start</var>) and nodes marked with <code>o</code>, where this function |
| 313 | traverses the nodes marked with numbers. This function is useful for | ||
| 314 | answering questions like “what is the first node after <var>start</var> in | ||
| 315 | the buffer that satisfies some condition?” | ||
| 274 | </p></dd></dl> | 316 | </p></dd></dl> |
| 275 | 317 | ||
| 276 | <dl class="def"> | 318 | <dl class="def"> |
| 277 | <dt id="index-treesit_002dsearch_002dforward_002dgoto"><span class="category">Function: </span><span><strong>treesit-search-forward-goto</strong> <em>predicate side &optional all backward up</em><a href='#index-treesit_002dsearch_002dforward_002dgoto' class='copiable-anchor'> ¶</a></span></dt> | 319 | <dt id="index-treesit_002dsearch_002dforward_002dgoto"><span class="category">Function: </span><span><strong>treesit-search-forward-goto</strong> <em>node predicate &optional start backward all</em><a href='#index-treesit_002dsearch_002dforward_002dgoto' class='copiable-anchor'> ¶</a></span></dt> |
| 278 | <dd><p>This function jumps to the start or end of the next node in buffer | 320 | <dd><p>This function moves point to the start or end of the next node after |
| 279 | that matches <var>predicate</var>. Parameters <var>predicate</var>, <var>all</var>, | 321 | <var>node</var> in the buffer that matches <var>predicate</var>. If <var>start</var> |
| 280 | <var>backward</var>, and <var>up</var> are the same as in | 322 | is non-<code>nil</code>, stop at the beginning rather than the end of a node. |
| 281 | <code>treesit-search-forward</code>. And <var>side</var> controls which side of | 323 | </p> |
| 282 | the matched no do we stop at, it can be <code>start</code> or <code>end</code>. | 324 | <p>This function guarantees that the matched node it returns makes |
| 325 | progress in terms of buffer position: the start/end position of the | ||
| 326 | returned node is always greater than that of <var>node</var>. | ||
| 327 | </p> | ||
| 328 | <p>Arguments <var>predicate</var>, <var>backward</var> and <var>all</var> are the same | ||
| 329 | as in <code>treesit-search-forward</code>. | ||
| 283 | </p></dd></dl> | 330 | </p></dd></dl> |
| 284 | 331 | ||
| 285 | <dl class="def"> | 332 | <dl class="def"> |
| 286 | <dt id="index-treesit_002dinduce_002dsparse_002dtree"><span class="category">Function: </span><span><strong>treesit-induce-sparse-tree</strong> <em>root predicate &optional process-fn limit</em><a href='#index-treesit_002dinduce_002dsparse_002dtree' class='copiable-anchor'> ¶</a></span></dt> | 333 | <dt id="index-treesit_002dinduce_002dsparse_002dtree"><span class="category">Function: </span><span><strong>treesit-induce-sparse-tree</strong> <em>root predicate &optional process-fn limit</em><a href='#index-treesit_002dinduce_002dsparse_002dtree' class='copiable-anchor'> ¶</a></span></dt> |
| 287 | <dd><p>This function creates a sparse tree from <var>root</var>’s subtree. | 334 | <dd><p>This function creates a sparse tree from <var>root</var>’s subtree. |
| 288 | </p> | 335 | </p> |
| 289 | <p>Basically, it takes the subtree under <var>root</var>, and combs it so only | 336 | <p>It takes the subtree under <var>root</var>, and combs it so only the nodes |
| 290 | the nodes that match <var>predicate</var> are left, like picking out grapes | 337 | that match <var>predicate</var> are left. Like previous functions, the |
| 291 | on the vine. Like previous functions, <var>predicate</var> can be a regexp | 338 | <var>predicate</var> can be a regexp string that matches against each |
| 292 | string that matches against each node’s type case-insensitively, or a | 339 | node’s type case-insensitively, or a function that takes a node and |
| 293 | function that takes a node and return nil/non-nil. | 340 | return non-<code>nil</code> if it matches. |
| 294 | </p> | 341 | </p> |
| 295 | <p>For example, for a subtree on the left that consist of both numbers | 342 | <p>For example, for a subtree on the left that consist of both numbers |
| 296 | and letters, if <var>predicate</var> is “letter only”, the returned tree | 343 | and letters, if <var>predicate</var> is “letter only”, the returned tree |
| @@ -310,50 +357,63 @@ b 1 2 b | | b c d | |||
| 310 | e 5 e | 357 | e 5 e |
| 311 | </pre></div> | 358 | </pre></div> |
| 312 | 359 | ||
| 313 | <p>If <var>process-fn</var> is non-nil, instead of returning the matched | 360 | <p>If <var>process-fn</var> is non-<code>nil</code>, instead of returning the matched |
| 314 | nodes, this function passes each node to <var>process-fn</var> and uses the | 361 | nodes, this function passes each node to <var>process-fn</var> and uses the |
| 315 | returned value instead. If non-nil, <var>limit</var> is the number of | 362 | returned value instead. If non-<code>nil</code>, <var>limit</var> is the number of |
| 316 | levels to go down from <var>root</var>. | 363 | levels to go down from <var>root</var>. |
| 317 | </p> | 364 | </p> |
| 318 | <p>Each node in the returned tree looks like <code>(<var>tree-sitter | 365 | <p>Each node in the returned tree looks like |
| 319 | node</var> . (<var>child</var> ...))</code>. The <var>tree-sitter node</var> of the root | 366 | <code>(<var><span class="nolinebreak">tree-sitter-node</span></var> . (<var>child</var> …))</code><!-- /@w -->. The |
| 320 | of this tree will be nil if <var>ROOT</var> doesn’t match <var>pred</var>. If | 367 | <var>tree-sitter-node</var> of the root of this tree will be nil if |
| 321 | no node matches <var>predicate</var>, return nil. | 368 | <var>root</var> doesn’t match <var>predicate</var>. If no node matches |
| 369 | <var>predicate</var>, the function returns <code>nil</code>. | ||
| 322 | </p></dd></dl> | 370 | </p></dd></dl> |
| 323 | 371 | ||
| 324 | <span id="More-convenient-functions"></span><h3 class="heading">More convenient functions</h3> | 372 | <span id="More-convenience-functions"></span><h3 class="heading">More convenience functions</h3> |
| 325 | 373 | ||
| 326 | <dl class="def"> | 374 | <dl class="def"> |
| 327 | <dt id="index-treesit_002dfilter_002dchild"><span class="category">Function: </span><span><strong>treesit-filter-child</strong> <em>node pred &optional named</em><a href='#index-treesit_002dfilter_002dchild' class='copiable-anchor'> ¶</a></span></dt> | 375 | <dt id="index-treesit_002dfilter_002dchild"><span class="category">Function: </span><span><strong>treesit-filter-child</strong> <em>node predicate &optional named</em><a href='#index-treesit_002dfilter_002dchild' class='copiable-anchor'> ¶</a></span></dt> |
| 328 | <dd><p>This function finds immediate children of <var>node</var> that satisfies | 376 | <dd><p>This function finds immediate children of <var>node</var> that satisfy |
| 329 | <var>pred</var>. | 377 | <var>predicate</var>. |
| 330 | </p> | 378 | </p> |
| 331 | <p>Function <var>pred</var> takes the child node as the argument and should | 379 | <p>The <var>predicate</var> function takes a node as the argument and should |
| 332 | return non-nil to indicated keeping the child. If <var>named</var> | 380 | return non-<code>nil</code> to indicate that the node should be kept. If |
| 333 | non-nil, this function only searches for named nodes. | 381 | <var>named</var> is non-<code>nil</code>, this function only examines the named |
| 382 | nodes. | ||
| 334 | </p></dd></dl> | 383 | </p></dd></dl> |
| 335 | 384 | ||
| 336 | <dl class="def"> | 385 | <dl class="def"> |
| 337 | <dt id="index-treesit_002dparent_002duntil"><span class="category">Function: </span><span><strong>treesit-parent-until</strong> <em>node pred</em><a href='#index-treesit_002dparent_002duntil' class='copiable-anchor'> ¶</a></span></dt> | 386 | <dt id="index-treesit_002dparent_002duntil"><span class="category">Function: </span><span><strong>treesit-parent-until</strong> <em>node predicate</em><a href='#index-treesit_002dparent_002duntil' class='copiable-anchor'> ¶</a></span></dt> |
| 338 | <dd><p>This function repeatedly finds the parent of <var>node</var>, and returns | 387 | <dd><p>This function repeatedly finds the parents of <var>node</var>, and returns |
| 339 | the parent if it satisfies <var>pred</var> (which takes the parent as the | 388 | the parent that satisfies <var>predicate</var>, a function that takes a |
| 340 | argument). If no parent satisfies <var>pred</var>, this function returns | 389 | node as the argument. If no parent satisfies <var>predicate</var>, this |
| 341 | nil. | 390 | function returns <code>nil</code>. |
| 342 | </p></dd></dl> | 391 | </p></dd></dl> |
| 343 | 392 | ||
| 344 | <dl class="def"> | 393 | <dl class="def"> |
| 345 | <dt id="index-treesit_002dparent_002dwhile"><span class="category">Function: </span><span><strong>treesit-parent-while</strong><a href='#index-treesit_002dparent_002dwhile' class='copiable-anchor'> ¶</a></span></dt> | 394 | <dt id="index-treesit_002dparent_002dwhile"><span class="category">Function: </span><span><strong>treesit-parent-while</strong> <em>node predicate</em><a href='#index-treesit_002dparent_002dwhile' class='copiable-anchor'> ¶</a></span></dt> |
| 346 | <dd><p>This function repeatedly finds the parent of <var>node</var>, and keeps | 395 | <dd><p>This function repeatedly finds the parent of <var>node</var>, and keeps |
| 347 | doing so as long as the parent satisfies <var>pred</var> (which takes the | 396 | doing so as long as the nodes satisfy <var>predicate</var>, a function that |
| 348 | parent as the single argument). I.e., this function returns the | 397 | takes a node as the argument. That is, this function returns the |
| 349 | farthest parent that still satisfies <var>pred</var>. | 398 | farthest parent that still satisfies <var>predicate</var>. |
| 399 | </p></dd></dl> | ||
| 400 | |||
| 401 | <dl class="def"> | ||
| 402 | <dt id="index-treesit_002dnode_002dtop_002dlevel"><span class="category">Function: </span><span><strong>treesit-node-top-level</strong> <em>node &optional type</em><a href='#index-treesit_002dnode_002dtop_002dlevel' class='copiable-anchor'> ¶</a></span></dt> | ||
| 403 | <dd><p>This function returns the highest parent of <var>node</var> that has the | ||
| 404 | same type as <var>node</var>. If no such parent exists, it returns | ||
| 405 | <code>nil</code>. Therefore this function is also useful for testing | ||
| 406 | whether <var>node</var> is top-level. | ||
| 407 | </p> | ||
| 408 | <p>If <var>type</var> is non-<code>nil</code>, this function matches each parent’s | ||
| 409 | type with <var>type</var> as a regexp, rather than using <var>node</var>’s type. | ||
| 350 | </p></dd></dl> | 410 | </p></dd></dl> |
| 351 | 411 | ||
| 352 | </div> | 412 | </div> |
| 353 | <hr> | 413 | <hr> |
| 354 | <div class="header"> | 414 | <div class="header"> |
| 355 | <p> | 415 | <p> |
| 356 | Next: <a href="Accessing-Node.html">Accessing Node Information</a>, Previous: <a href="Using-Parser.html">Using Tree-sitter Parser</a>, Up: <a href="Parsing-Program-Source.html">Parsing Program Source</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Index.html" title="Index" rel="index">Index</a>]</p> | 416 | Next: <a href="Accessing-Node-Information.html">Accessing Node Information</a>, Previous: <a href="Using-Parser.html">Using Tree-sitter Parser</a>, Up: <a href="Parsing-Program-Source.html">Parsing Program Source</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Index.html" title="Index" rel="index">Index</a>]</p> |
| 357 | </div> | 417 | </div> |
| 358 | 418 | ||
| 359 | 419 | ||
diff --git a/admin/notes/tree-sitter/html-manual/Tree_002dsitter-C-API.html b/admin/notes/tree-sitter/html-manual/Tree_002dsitter-C-API.html index 77cea6b3f95..a80c2326160 100644 --- a/admin/notes/tree-sitter/html-manual/Tree_002dsitter-C-API.html +++ b/admin/notes/tree-sitter/html-manual/Tree_002dsitter-C-API.html | |||
| @@ -33,7 +33,7 @@ developing GNU and promoting software freedom." --> | |||
| 33 | <link href="Index.html" rel="index" title="Index"> | 33 | <link href="Index.html" rel="index" title="Index"> |
| 34 | <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents"> | 34 | <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents"> |
| 35 | <link href="Parsing-Program-Source.html" rel="up" title="Parsing Program Source"> | 35 | <link href="Parsing-Program-Source.html" rel="up" title="Parsing Program Source"> |
| 36 | <link href="Multiple-Languages.html" rel="prev" title="Multiple Languages"> | 36 | <link href="Tree_002dsitter-major-modes.html" rel="prev" title="Tree-sitter major modes"> |
| 37 | <style type="text/css"> | 37 | <style type="text/css"> |
| 38 | <!-- | 38 | <!-- |
| 39 | a.copiable-anchor {visibility: hidden; text-decoration: none; line-height: 0em} | 39 | a.copiable-anchor {visibility: hidden; text-decoration: none; line-height: 0em} |
| @@ -62,23 +62,23 @@ ul.no-bullet {list-style: none} | |||
| 62 | <div class="section" id="Tree_002dsitter-C-API"> | 62 | <div class="section" id="Tree_002dsitter-C-API"> |
| 63 | <div class="header"> | 63 | <div class="header"> |
| 64 | <p> | 64 | <p> |
| 65 | Previous: <a href="Multiple-Languages.html" accesskey="p" rel="prev">Parsing Text in Multiple Languages</a>, Up: <a href="Parsing-Program-Source.html" accesskey="u" rel="up">Parsing Program Source</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Index.html" title="Index" rel="index">Index</a>]</p> | 65 | Previous: <a href="Tree_002dsitter-major-modes.html" accesskey="p" rel="prev">Developing major modes with tree-sitter</a>, Up: <a href="Parsing-Program-Source.html" accesskey="u" rel="up">Parsing Program Source</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Index.html" title="Index" rel="index">Index</a>]</p> |
| 66 | </div> | 66 | </div> |
| 67 | <hr> | 67 | <hr> |
| 68 | <span id="Tree_002dsitter-C-API-Correspondence"></span><h3 class="section">37.7 Tree-sitter C API Correspondence</h3> | 68 | <span id="Tree_002dsitter-C-API-Correspondence"></span><h3 class="section">37.8 Tree-sitter C API Correspondence</h3> |
| 69 | 69 | ||
| 70 | <p>Emacs’ tree-sitter integration doesn’t expose every feature | 70 | <p>Emacs’ tree-sitter integration doesn’t expose every feature |
| 71 | tree-sitter’s C API provides. Missing features include: | 71 | provided by tree-sitter’s C API. Missing features include: |
| 72 | </p> | 72 | </p> |
| 73 | <ul> | 73 | <ul> |
| 74 | <li> Creating a tree cursor and navigating the syntax tree with it. | 74 | <li> Creating a tree cursor and navigating the syntax tree with it. |
| 75 | </li><li> Setting timeout and cancellation flag for a parser. | 75 | </li><li> Setting timeout and cancellation flag for a parser. |
| 76 | </li><li> Setting the logger for a parser. | 76 | </li><li> Setting the logger for a parser. |
| 77 | </li><li> Printing a DOT graph of the syntax tree to a file. | 77 | </li><li> Printing a <acronym>DOT</acronym> graph of the syntax tree to a file. |
| 78 | </li><li> Coping and modifying a syntax tree. (Emacs doesn’t expose a tree | 78 | </li><li> Copying and modifying a syntax tree. (Emacs doesn’t expose a tree |
| 79 | object.) | 79 | object.) |
| 80 | </li><li> Using (row, column) coordinates as position. | 80 | </li><li> Using (row, column) coordinates as position. |
| 81 | </li><li> Updating a node with changes. (In Emacs, retrieve a new node instead | 81 | </li><li> Updating a node with changes. (In Emacs, retrieve a new node instead |
| 82 | of updating the existing one.) | 82 | of updating the existing one.) |
| 83 | </li><li> Querying statics of a language definition. | 83 | </li><li> Querying statics of a language definition. |
| 84 | </li></ul> | 84 | </li></ul> |
| @@ -87,7 +87,7 @@ of updating the existing one.) | |||
| 87 | convenient and idiomatic: | 87 | convenient and idiomatic: |
| 88 | </p> | 88 | </p> |
| 89 | <ul> | 89 | <ul> |
| 90 | <li> Instead of using byte positions, the ELisp API uses character | 90 | <li> Instead of using byte positions, the Emacs Lisp API uses character |
| 91 | positions. | 91 | positions. |
| 92 | </li><li> Null nodes are converted to nil. | 92 | </li><li> Null nodes are converted to nil. |
| 93 | </li></ul> | 93 | </li></ul> |
| @@ -203,7 +203,7 @@ ts_language_version | |||
| 203 | <hr> | 203 | <hr> |
| 204 | <div class="header"> | 204 | <div class="header"> |
| 205 | <p> | 205 | <p> |
| 206 | Previous: <a href="Multiple-Languages.html">Parsing Text in Multiple Languages</a>, Up: <a href="Parsing-Program-Source.html">Parsing Program Source</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Index.html" title="Index" rel="index">Index</a>]</p> | 206 | Previous: <a href="Tree_002dsitter-major-modes.html">Developing major modes with tree-sitter</a>, Up: <a href="Parsing-Program-Source.html">Parsing Program Source</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Index.html" title="Index" rel="index">Index</a>]</p> |
| 207 | </div> | 207 | </div> |
| 208 | 208 | ||
| 209 | 209 | ||
diff --git a/admin/notes/tree-sitter/html-manual/Using-Parser.html b/admin/notes/tree-sitter/html-manual/Using-Parser.html index 438e3858f1b..c478a39e556 100644 --- a/admin/notes/tree-sitter/html-manual/Using-Parser.html +++ b/admin/notes/tree-sitter/html-manual/Using-Parser.html | |||
| @@ -67,12 +67,12 @@ Next: <a href="Retrieving-Node.html" accesskey="n" rel="next">Retrieving Node</a | |||
| 67 | </div> | 67 | </div> |
| 68 | <hr> | 68 | <hr> |
| 69 | <span id="Using-Tree_002dsitter-Parser"></span><h3 class="section">37.2 Using Tree-sitter Parser</h3> | 69 | <span id="Using-Tree_002dsitter-Parser"></span><h3 class="section">37.2 Using Tree-sitter Parser</h3> |
| 70 | <span id="index-Tree_002dsitter-parser"></span> | 70 | <span id="index-tree_002dsitter-parser_002c-using"></span> |
| 71 | 71 | ||
| 72 | <p>This section described how to create and configure a tree-sitter | 72 | <p>This section describes how to create and configure a tree-sitter |
| 73 | parser. In Emacs, each tree-sitter parser is associated with a | 73 | parser. In Emacs, each tree-sitter parser is associated with a |
| 74 | buffer. As we edit the buffer, the associated parser and the syntax | 74 | buffer. As the user edits the buffer, the associated parser and |
| 75 | tree is automatically kept up-to-date. | 75 | syntax tree are automatically kept up-to-date. |
| 76 | </p> | 76 | </p> |
| 77 | <dl class="def"> | 77 | <dl class="def"> |
| 78 | <dt id="index-treesit_002dmax_002dbuffer_002dsize"><span class="category">Variable: </span><span><strong>treesit-max-buffer-size</strong><a href='#index-treesit_002dmax_002dbuffer_002dsize' class='copiable-anchor'> ¶</a></span></dt> | 78 | <dt id="index-treesit_002dmax_002dbuffer_002dsize"><span class="category">Variable: </span><span><strong>treesit-max-buffer-size</strong><a href='#index-treesit_002dmax_002dbuffer_002dsize' class='copiable-anchor'> ¶</a></span></dt> |
| @@ -88,48 +88,49 @@ activating tree-sitter features. It basically checks | |||
| 88 | <code>treesit-available-p</code> and <code>treesit-max-buffer-size</code>. | 88 | <code>treesit-available-p</code> and <code>treesit-max-buffer-size</code>. |
| 89 | </p></dd></dl> | 89 | </p></dd></dl> |
| 90 | 90 | ||
| 91 | <span id="index-Creating-tree_002dsitter-parsers"></span> | 91 | <span id="index-creating-tree_002dsitter-parsers"></span> |
| 92 | <span id="index-tree_002dsitter-parser_002c-creating"></span> | ||
| 92 | <dl class="def"> | 93 | <dl class="def"> |
| 93 | <dt id="index-treesit_002dparser_002dcreate"><span class="category">Function: </span><span><strong>treesit-parser-create</strong> <em>language &optional buffer no-reuse</em><a href='#index-treesit_002dparser_002dcreate' class='copiable-anchor'> ¶</a></span></dt> | 94 | <dt id="index-treesit_002dparser_002dcreate"><span class="category">Function: </span><span><strong>treesit-parser-create</strong> <em>language &optional buffer no-reuse</em><a href='#index-treesit_002dparser_002dcreate' class='copiable-anchor'> ¶</a></span></dt> |
| 94 | <dd><p>To create a parser, we provide a <var>buffer</var> and the <var>language</var> | 95 | <dd><p>Create a parser for the specified <var>buffer</var> and <var>language</var> |
| 95 | to use (see <a href="Language-Definitions.html">Tree-sitter Language Definitions</a>). If <var>buffer</var> is nil, the | 96 | (see <a href="Language-Definitions.html">Tree-sitter Language Definitions</a>). If <var>buffer</var> is omitted or |
| 96 | current buffer is used. | 97 | <code>nil</code>, it stands for the current buffer. |
| 97 | </p> | 98 | </p> |
| 98 | <p>By default, this function reuses a parser if one already exists for | 99 | <p>By default, this function reuses a parser if one already exists for |
| 99 | <var>language</var> in <var>buffer</var>, if <var>no-reuse</var> is non-nil, this | 100 | <var>language</var> in <var>buffer</var>, but if <var>no-reuse</var> is |
| 100 | function always creates a new parser. | 101 | non-<code>nil</code>, this function always creates a new parser. |
| 101 | </p></dd></dl> | 102 | </p></dd></dl> |
| 102 | 103 | ||
| 103 | <p>Given a parser, we can query information about it: | 104 | <p>Given a parser, we can query information about it. |
| 104 | </p> | 105 | </p> |
| 105 | <dl class="def"> | 106 | <dl class="def"> |
| 106 | <dt id="index-treesit_002dparser_002dbuffer"><span class="category">Function: </span><span><strong>treesit-parser-buffer</strong> <em>parser</em><a href='#index-treesit_002dparser_002dbuffer' class='copiable-anchor'> ¶</a></span></dt> | 107 | <dt id="index-treesit_002dparser_002dbuffer"><span class="category">Function: </span><span><strong>treesit-parser-buffer</strong> <em>parser</em><a href='#index-treesit_002dparser_002dbuffer' class='copiable-anchor'> ¶</a></span></dt> |
| 107 | <dd><p>Returns the buffer associated with <var>parser</var>. | 108 | <dd><p>This function returns the buffer associated with <var>parser</var>. |
| 108 | </p></dd></dl> | 109 | </p></dd></dl> |
| 109 | 110 | ||
| 110 | <dl class="def"> | 111 | <dl class="def"> |
| 111 | <dt id="index-treesit_002dparser_002dlanguage"><span class="category">Function: </span><span><strong>treesit-parser-language</strong> <em>parser</em><a href='#index-treesit_002dparser_002dlanguage' class='copiable-anchor'> ¶</a></span></dt> | 112 | <dt id="index-treesit_002dparser_002dlanguage"><span class="category">Function: </span><span><strong>treesit-parser-language</strong> <em>parser</em><a href='#index-treesit_002dparser_002dlanguage' class='copiable-anchor'> ¶</a></span></dt> |
| 112 | <dd><p>Returns the language that <var>parser</var> uses. | 113 | <dd><p>This function returns the language used by <var>parser</var>. |
| 113 | </p></dd></dl> | 114 | </p></dd></dl> |
| 114 | 115 | ||
| 115 | <dl class="def"> | 116 | <dl class="def"> |
| 116 | <dt id="index-treesit_002dparser_002dp"><span class="category">Function: </span><span><strong>treesit-parser-p</strong> <em>object</em><a href='#index-treesit_002dparser_002dp' class='copiable-anchor'> ¶</a></span></dt> | 117 | <dt id="index-treesit_002dparser_002dp"><span class="category">Function: </span><span><strong>treesit-parser-p</strong> <em>object</em><a href='#index-treesit_002dparser_002dp' class='copiable-anchor'> ¶</a></span></dt> |
| 117 | <dd><p>Checks if <var>object</var> is a tree-sitter parser. Return non-nil if it | 118 | <dd><p>This function checks if <var>object</var> is a tree-sitter parser, and |
| 118 | is, return nil otherwise. | 119 | returns non-<code>nil</code> if it is, and <code>nil</code> otherwise. |
| 119 | </p></dd></dl> | 120 | </p></dd></dl> |
| 120 | 121 | ||
| 121 | <p>There is no need to explicitly parse a buffer, because parsing is done | 122 | <p>There is no need to explicitly parse a buffer, because parsing is done |
| 122 | automatically and lazily. A parser only parses when we query for a | 123 | automatically and lazily. A parser only parses when a Lisp program |
| 123 | node in its syntax tree. Therefore, when a parser is first created, | 124 | queries for a node in its syntax tree. Therefore, when a parser is |
| 124 | it doesn’t parse the buffer; it waits until we query for a node for | 125 | first created, it doesn’t parse the buffer; it waits until the Lisp |
| 125 | the first time. Similarly, when some change is made in the buffer, a | 126 | program queries for a node for the first time. Similarly, when some |
| 126 | parser doesn’t re-parse immediately. | 127 | change is made in the buffer, a parser doesn’t re-parse immediately. |
| 127 | </p> | 128 | </p> |
| 128 | <span id="index-treesit_002dbuffer_002dtoo_002dlarge"></span> | 129 | <span id="index-treesit_002dbuffer_002dtoo_002dlarge"></span> |
| 129 | <p>When a parser do parse, it checks for the size of the buffer. | 130 | <p>When a parser does parse, it checks for the size of the buffer. |
| 130 | Tree-sitter can only handle buffer no larger than about 4GB. If the | 131 | Tree-sitter can only handle buffer no larger than about 4GB. If the |
| 131 | size exceeds that, Emacs signals <code>treesit-buffer-too-large</code> | 132 | size exceeds that, Emacs signals the <code>treesit-buffer-too-large</code> |
| 132 | with signal data being the buffer size. | 133 | error with signal data being the buffer size. |
| 133 | </p> | 134 | </p> |
| 134 | <p>Once a parser is created, Emacs automatically adds it to the | 135 | <p>Once a parser is created, Emacs automatically adds it to the |
| 135 | internal parser list. Every time a change is made to the buffer, | 136 | internal parser list. Every time a change is made to the buffer, |
| @@ -138,8 +139,9 @@ tree incrementally. | |||
| 138 | </p> | 139 | </p> |
| 139 | <dl class="def"> | 140 | <dl class="def"> |
| 140 | <dt id="index-treesit_002dparser_002dlist"><span class="category">Function: </span><span><strong>treesit-parser-list</strong> <em>&optional buffer</em><a href='#index-treesit_002dparser_002dlist' class='copiable-anchor'> ¶</a></span></dt> | 141 | <dt id="index-treesit_002dparser_002dlist"><span class="category">Function: </span><span><strong>treesit-parser-list</strong> <em>&optional buffer</em><a href='#index-treesit_002dparser_002dlist' class='copiable-anchor'> ¶</a></span></dt> |
| 141 | <dd><p>This function returns the parser list of <var>buffer</var>. And | 142 | <dd><p>This function returns the parser list of <var>buffer</var>. If |
| 142 | <var>buffer</var> defaults to the current buffer. | 143 | <var>buffer</var> is <code>nil</code> or omitted, it defaults to the current |
| 144 | buffer. | ||
| 143 | </p></dd></dl> | 145 | </p></dd></dl> |
| 144 | 146 | ||
| 145 | <dl class="def"> | 147 | <dl class="def"> |
| @@ -148,29 +150,30 @@ tree incrementally. | |||
| 148 | </p></dd></dl> | 150 | </p></dd></dl> |
| 149 | 151 | ||
| 150 | <span id="index-tree_002dsitter-narrowing"></span> | 152 | <span id="index-tree_002dsitter-narrowing"></span> |
| 151 | <span id="tree_002dsitter-narrowing"></span><p>Normally, a parser “sees” the whole | 153 | <span id="tree_002dsitter-narrowing"></span><p>Normally, a parser “sees” the whole buffer, but when the buffer is |
| 152 | buffer, but when the buffer is narrowed (see <a href="Narrowing.html">Narrowing</a>), the | 154 | narrowed (see <a href="Narrowing.html">Narrowing</a>), the parser will only see the accessible |
| 153 | parser will only see the visible region. As far as the parser can | 155 | portion of the buffer. As far as the parser can tell, the hidden |
| 154 | tell, the hidden region is deleted. And when the buffer is later | 156 | region was deleted. When the buffer is later widened, the parser |
| 155 | widened, the parser thinks text is inserted in the beginning and in | 157 | thinks text is inserted at the beginning and at the end. Although |
| 156 | the end. Although parsers respect narrowing, narrowing shouldn’t be | 158 | parsers respect narrowing, modes should not use narrowing as a means |
| 157 | the mean to handle a multi-language buffer; instead, set the ranges in | 159 | to handle a multi-language buffer; instead, set the ranges in which the |
| 158 | which a parser should operate in. See <a href="Multiple-Languages.html">Parsing Text in Multiple Languages</a>. | 160 | parser should operate. See <a href="Multiple-Languages.html">Parsing Text in Multiple Languages</a>. |
| 159 | </p> | 161 | </p> |
| 160 | <p>Because a parser parses lazily, when we narrow the buffer, the parser | 162 | <p>Because a parser parses lazily, when the user or a Lisp program |
| 161 | is not affected immediately; as long as we don’t query for a node | 163 | narrows the buffer, the parser is not affected immediately; as long as |
| 162 | while the buffer is narrowed, the parser is oblivious of the | 164 | the mode doesn’t query for a node while the buffer is narrowed, the |
| 163 | narrowing. | 165 | parser is oblivious of the narrowing. |
| 164 | </p> | 166 | </p> |
| 165 | <span id="index-tree_002dsitter-parse-string"></span> | 167 | <span id="index-tree_002dsitter-parse-string"></span> |
| 166 | <dl class="def"> | 168 | <span id="index-parse-string_002c-tree_002dsitter"></span> |
| 167 | <dt id="index-treesit_002dparse_002dstring"><span class="category">Function: </span><span><strong>treesit-parse-string</strong> <em>string language</em><a href='#index-treesit_002dparse_002dstring' class='copiable-anchor'> ¶</a></span></dt> | 169 | <p>Besides creating a parser for a buffer, a Lisp program can also parse a |
| 168 | <dd><p>Besides creating a parser for a buffer, we can also just parse a | 170 | string. Unlike a buffer, parsing a string is a one-off operation, and |
| 169 | string. Unlike a buffer, parsing a string is a one-time deal, and | ||
| 170 | there is no way to update the result. | 171 | there is no way to update the result. |
| 171 | </p> | 172 | </p> |
| 172 | <p>This function parses <var>string</var> with <var>language</var>, and returns the | 173 | <dl class="def"> |
| 173 | root node of the generated syntax tree. | 174 | <dt id="index-treesit_002dparse_002dstring"><span class="category">Function: </span><span><strong>treesit-parse-string</strong> <em>string language</em><a href='#index-treesit_002dparse_002dstring' class='copiable-anchor'> ¶</a></span></dt> |
| 175 | <dd><p>This function parses <var>string</var> using <var>language</var>, and returns | ||
| 176 | the root node of the generated syntax tree. | ||
| 174 | </p></dd></dl> | 177 | </p></dd></dl> |
| 175 | 178 | ||
| 176 | </div> | 179 | </div> |
diff --git a/admin/notes/tree-sitter/starter-guide b/admin/notes/tree-sitter/starter-guide index e5736038622..84118c6f57b 100644 --- a/admin/notes/tree-sitter/starter-guide +++ b/admin/notes/tree-sitter/starter-guide | |||
| @@ -78,28 +78,25 @@ Now check if Emacs is built with tree-sitter library | |||
| 78 | 78 | ||
| 79 | (treesit-available-p) | 79 | (treesit-available-p) |
| 80 | 80 | ||
| 81 | For your major mode, first create a tree-sitter switch: | 81 | Users toggle tree-sitter for each major mode with a central variable, |
| 82 | 82 | ‘treesit-settings’. You can check whether to enable tree-sitter with | |
| 83 | #+begin_src elisp | 83 | ‘treesit-ready-p’, which takes a major-mode symbol and one or more |
| 84 | (defcustom python-use-tree-sitter nil | 84 | language symbol. The major mode body should use a branch like this: |
| 85 | "If non-nil, `python-mode' tries to use tree-sitter. | 85 | |
| 86 | Currently `python-mode' can utilize tree-sitter for font-locking, | 86 | #+begin_src emacs-lisp |
| 87 | imenu, and movement functions." | 87 | (cond |
| 88 | :type 'boolean) | 88 | ;; Tree-sitter setup. |
| 89 | #+end_src | 89 | ((treesit-ready-p 'python-mode 'python) |
| 90 | 90 | ...) | |
| 91 | Then in other places, we decide on whether to enable tree-sitter by | 91 | (t |
| 92 | 92 | ;; Non-tree-sitter setup. | |
| 93 | #+begin_src elisp | 93 | ...)) |
| 94 | (and python-use-tree-sitter | ||
| 95 | (treesit-can-enable-p)) | ||
| 96 | #+end_src | 94 | #+end_src |
| 97 | 95 | ||
| 98 | * Naming convention | 96 | * Naming convention |
| 99 | 97 | ||
| 100 | When referring to tree-sitter as a noun, use “tree-sitter”, like | 98 | Use tree-sitter for text (documentation, comment), use treesit for |
| 101 | python-use-tree-sitter. For prefix use “treesit”, like | 99 | symbol (variable, function). |
| 102 | python-treesit-indent. | ||
| 103 | 100 | ||
| 104 | * Font-lock | 101 | * Font-lock |
| 105 | 102 | ||
| @@ -108,10 +105,23 @@ capture names, tree-sitter finds the nodes that match these patterns, | |||
| 108 | tag the corresponding capture names onto the nodes and return them to | 105 | tag the corresponding capture names onto the nodes and return them to |
| 109 | you. The query function returns a list of (capture-name . node). For | 106 | you. The query function returns a list of (capture-name . node). For |
| 110 | font-lock, we use face names as capture names. And the captured node | 107 | font-lock, we use face names as capture names. And the captured node |
| 111 | will be fontified in their capture name. The capture name could also | 108 | will be fontified in their capture name. |
| 112 | be a function, in which case (START END NODE) is passed to the | 109 | |
| 113 | function for font-lock. START and END is the start and end the | 110 | The capture name could also be a function, in which case (NODE |
| 114 | captured NODE. | 111 | OVERRIDE START END) is passed to the function for fontification. START |
| 112 | and END is the start and end of the region to be fontified. The | ||
| 113 | function should only fontify within that region. The function should | ||
| 114 | also allow more optional arguments with (&rest _), for future | ||
| 115 | extensibility. For OVERRIDE check out the docstring of | ||
| 116 | treesit-font-lock-rules. | ||
| 117 | |||
| 118 | Contextual syntax like multi-line comments and multi-line strings, | ||
| 119 | needs special care. Because change in this type of things can affect | ||
| 120 | a large portion of the buffer. Think of inserting a closing comment | ||
| 121 | delimeter, it causes all the text before it (to the opening comment | ||
| 122 | delimeter) to change to comment face. These things needs to be | ||
| 123 | captured in a special name “contextual”, so that Emacs can give them | ||
| 124 | special treatment. Se the example below for how it looks like. | ||
| 115 | 125 | ||
| 116 | ** Query syntax | 126 | ** Query syntax |
| 117 | 127 | ||
| @@ -171,52 +181,64 @@ The manual explains how to read grammar files in the bottom of section | |||
| 171 | 181 | ||
| 172 | ** Debugging queires | 182 | ** Debugging queires |
| 173 | 183 | ||
| 174 | If your query has problems, it usually cannot compile. In that case | 184 | If your query has problems, use ‘treesit-query-validate’ to debug the |
| 175 | use ‘treesit-query-validate’ to debug the query. It will pop a buffer | 185 | query. It will pop a buffer containing the query (in text format) and |
| 176 | containing the query (in text format) and mark the offending part in | 186 | mark the offending part in red. |
| 177 | red. | ||
| 178 | 187 | ||
| 179 | ** Code | 188 | ** Code |
| 180 | 189 | ||
| 181 | To enable tree-sitter font-lock, set ‘treesit-font-lock-settings’ | 190 | To enable tree-sitter font-lock, set ‘treesit-font-lock-settings’ and |
| 182 | buffer-locally and call ‘treesit-font-lock-enable’. For example, see | 191 | ‘treesit-font-lock-feature-list’ buffer-locally and call |
| 192 | ‘treesit-major-mode-setup’. For example, see | ||
| 183 | ‘python--treesit-settings’ in python.el. Below I paste a snippet of | 193 | ‘python--treesit-settings’ in python.el. Below I paste a snippet of |
| 184 | it. | 194 | it. |
| 185 | 195 | ||
| 186 | Note that like the current font-lock, if the to-be-fontified region | 196 | Note that like the current font-lock, if the to-be-fontified region |
| 187 | already has a face (ie, an earlier match fontified part/all of the | 197 | already has a face (ie, an earlier match fontified part/all of the |
| 188 | region), the new face is discarded rather than applied. If you want | 198 | region), the new face is discarded rather than applied. If you want |
| 189 | later matches always override earlier matches, use the :override | 199 | later matches always override earlier matches, use the :override |
| 190 | keyword. | 200 | keyword. |
| 191 | 201 | ||
| 202 | Each rule should have a :feature, like function-name, | ||
| 203 | string-interpolation, builtin, etc. Users can then enable/disable each | ||
| 204 | feature individually. | ||
| 205 | |||
| 192 | #+begin_src elisp | 206 | #+begin_src elisp |
| 193 | (defvar python--treesit-settings | 207 | (defvar python--treesit-settings |
| 194 | (treesit-font-lock-rules | 208 | (treesit-font-lock-rules |
| 209 | :feature 'comment | ||
| 210 | :language 'python | ||
| 211 | '((comment) @font-lock-comment-face) | ||
| 212 | |||
| 213 | :feature 'string | ||
| 195 | :language 'python | 214 | :language 'python |
| 196 | :override t | 215 | '((string) @font-lock-string-face |
| 197 | `(;; Queries for def and class. | 216 | (string) @contextual) ; Contextual special treatment. |
| 198 | (function_definition | ||
| 199 | name: (identifier) @font-lock-function-name-face) | ||
| 200 | 217 | ||
| 201 | (class_definition | 218 | :feature 'function-name |
| 202 | name: (identifier) @font-lock-type-face) | 219 | :language 'python |
| 220 | '((function_definition | ||
| 221 | name: (identifier) @font-lock-function-name-face)) | ||
| 203 | 222 | ||
| 204 | ;; Comment and string. | 223 | :feature 'class-name |
| 205 | (comment) @font-lock-comment-face | 224 | :language 'python |
| 225 | '((class_definition | ||
| 226 | name: (identifier) @font-lock-type-face)) | ||
| 206 | 227 | ||
| 207 | ...))) | 228 | ...)) |
| 208 | #+end_src | 229 | #+end_src |
| 209 | 230 | ||
| 210 | Then in ‘python-mode’, enable tree-sitter font-lock: | 231 | Then in ‘python-mode’, enable tree-sitter font-lock: |
| 211 | 232 | ||
| 212 | #+begin_src elisp | 233 | #+begin_src elisp |
| 213 | (treesit-parser-create 'python) | 234 | (treesit-parser-create 'python) |
| 214 | ;; This turns off the syntax-based font-lock for comments and | 235 | (setq-local treesit-font-lock-settings python--treesit-settings) |
| 215 | ;; strings. So it doesn’t override tree-sitter’s fontification. | 236 | (setq-local treesit-font-lock-feature-list |
| 216 | (setq-local font-lock-keywords-only t) | 237 | '((comment string function-name) |
| 217 | (setq-local treesit-font-lock-settings | 238 | (class-name keyword builtin) |
| 218 | python--treesit-settings) | 239 | (string-interpolation decorator))) |
| 219 | (treesit-font-lock-enable) | 240 | ... |
| 241 | (treesit-major-mode-setup) | ||
| 220 | #+end_src | 242 | #+end_src |
| 221 | 243 | ||
| 222 | Concretely, something like this: | 244 | Concretely, something like this: |
| @@ -224,29 +246,22 @@ Concretely, something like this: | |||
| 224 | #+begin_src elisp | 246 | #+begin_src elisp |
| 225 | (define-derived-mode python-mode prog-mode "Python" | 247 | (define-derived-mode python-mode prog-mode "Python" |
| 226 | ... | 248 | ... |
| 227 | 249 | (cond | |
| 228 | (treesit-parser-create 'python) | 250 | ;; Tree-sitter. |
| 229 | 251 | ((treesit-ready-p 'python-mode 'python) | |
| 230 | (if (and python-use-tree-sitter | 252 | (treesit-parser-create 'python) |
| 231 | (treesit-can-enable-p)) | 253 | (setq-local treesit-font-lock-settings python--treesit-settings) |
| 232 | ;; Tree-sitter. | 254 | (setq-local treesit-font-lock-feature-list |
| 233 | (progn | 255 | '((comment string function-name) |
| 234 | (setq-local font-lock-keywords-only t) | 256 | (class-name keyword builtin) |
| 235 | (setq-local treesit-font-lock-settings | 257 | (string-interpolation decorator))) |
| 236 | python--treesit-settings) | 258 | (treesit-major-mode-setup)) |
| 237 | (treesit-font-lock-enable)) | 259 | (t |
| 238 | ;; No tree-sitter | 260 | ;; No tree-sitter |
| 239 | (setq-local font-lock-defaults ...)) | 261 | (setq-local font-lock-defaults ...) |
| 240 | 262 | ...))) | |
| 241 | ...) | ||
| 242 | #+end_src | 263 | #+end_src |
| 243 | 264 | ||
| 244 | You’ll notice that tree-sitter’s font-lock doesn’t respect | ||
| 245 | ‘font-lock-maximum-decoration’, major modes are free to set | ||
| 246 | ‘treesit-font-lock-settings’ based on the value of | ||
| 247 | ‘font-lock-maximum-decoration’, or provide more fine-grained control | ||
| 248 | through other mode-specific means. (Towards that end, the :toggle option in treesit-font-lock-rules is very useful.) | ||
| 249 | |||
| 250 | * Indent | 265 | * Indent |
| 251 | 266 | ||
| 252 | Indent works like this: We have a bunch of rules that look like | 267 | Indent works like this: We have a bunch of rules that look like |
| @@ -262,10 +277,14 @@ previous line. We find the column number of that point (eg, 4), add | |||
| 262 | OFFSET to it (eg, 0), and that is the column we want to indent the | 277 | OFFSET to it (eg, 0), and that is the column we want to indent the |
| 263 | current line to (4 + 0 = 4). | 278 | current line to (4 + 0 = 4). |
| 264 | 279 | ||
| 280 | Matchers and anchors are functions that takes (NODE PARENT BOL &rest | ||
| 281 | _). Matches return nil/non-nil for no match/match, and anchors return | ||
| 282 | the anchor point. Below are some convenient builtin matchers and anchors. | ||
| 283 | |||
| 265 | For MATHCER we have | 284 | For MATHCER we have |
| 266 | 285 | ||
| 267 | (parent-is TYPE) | 286 | (parent-is TYPE) => matches if PARENT’s type matches TYPE as regexp |
| 268 | (node-is TYPE) | 287 | (node-is TYPE) => mathces NODE’s type |
| 269 | (query QUERY) => matches if querying PARENT with QUERY | 288 | (query QUERY) => matches if querying PARENT with QUERY |
| 270 | captures NODE. | 289 | captures NODE. |
| 271 | 290 | ||
| @@ -280,9 +299,9 @@ For ANCHOR we have | |||
| 280 | first-sibling => start of the first sibling | 299 | first-sibling => start of the first sibling |
| 281 | parent => start of parent | 300 | parent => start of parent |
| 282 | parent-bol => BOL of the line parent is on. | 301 | parent-bol => BOL of the line parent is on. |
| 283 | prev-sibling | 302 | prev-sibling => start of previous sibling |
| 284 | no-indent => don’t indent | 303 | no-indent => current position (don’t indent) |
| 285 | prev-line => same indent as previous line | 304 | prev-line => start of previous line |
| 286 | 305 | ||
| 287 | There is also a manual section for indent: "Parser-based Indentation". | 306 | There is also a manual section for indent: "Parser-based Indentation". |
| 288 | 307 | ||
| @@ -301,7 +320,7 @@ tells you which rule is applied in the echo area. | |||
| 301 | ((node-is ")") parent-bol 0) | 320 | ((node-is ")") parent-bol 0) |
| 302 | ((node-is "]") parent-bol 0) | 321 | ((node-is "]") parent-bol 0) |
| 303 | ((node-is ">") parent-bol 0) | 322 | ((node-is ">") parent-bol 0) |
| 304 | ((node-is ".") parent-bol ,offset) | 323 | ((node-is "\\.") parent-bol ,offset) |
| 305 | ((parent-is "ternary_expression") parent-bol ,offset) | 324 | ((parent-is "ternary_expression") parent-bol ,offset) |
| 306 | ((parent-is "named_imports") parent-bol ,offset) | 325 | ((parent-is "named_imports") parent-bol ,offset) |
| 307 | ((parent-is "statement_block") parent-bol ,offset) | 326 | ((parent-is "statement_block") parent-bol ,offset) |
| @@ -320,21 +339,21 @@ tells you which rule is applied in the echo area. | |||
| 320 | ...)))) | 339 | ...)))) |
| 321 | #+end_src | 340 | #+end_src |
| 322 | 341 | ||
| 323 | Then you set ‘treesit-simple-indent-rules’ to your rules, and set | 342 | Then you set ‘treesit-simple-indent-rules’ to your rules, and call |
| 324 | ‘indent-line-function’: | 343 | ‘treesit-major-mode-setup’: |
| 325 | 344 | ||
| 326 | #+begin_src elisp | 345 | #+begin_src elisp |
| 327 | (setq-local treesit-simple-indent-rules typescript-mode-indent-rules) | 346 | (setq-local treesit-simple-indent-rules typescript-mode-indent-rules) |
| 328 | (setq-local indent-line-function #'treesit-indent) | 347 | (treesit-major-mode-setup) |
| 329 | #+end_src | 348 | #+end_src |
| 330 | 349 | ||
| 331 | * Imenu | 350 | * Imenu |
| 332 | 351 | ||
| 333 | Not much to say except for utilizing ‘treesit-induce-sparse-tree’. | 352 | Not much to say except for utilizing ‘treesit-induce-sparse-tree’. |
| 334 | See ‘python--imenu-treesit-create-index-1’ in python.el for an | 353 | See ‘js--treesit-imenu-1’ in js.el for an example. |
| 335 | example. | ||
| 336 | 354 | ||
| 337 | Once you have the index builder, set ‘imenu-create-index-function’. | 355 | Once you have the index builder, set ‘imenu-create-index-function’ to |
| 356 | it. | ||
| 338 | 357 | ||
| 339 | * Navigation | 358 | * Navigation |
| 340 | 359 | ||
| @@ -344,51 +363,33 @@ You can find the end of a defun with something like | |||
| 344 | (treesit-search-forward-goto "function_definition" 'end) | 363 | (treesit-search-forward-goto "function_definition" 'end) |
| 345 | 364 | ||
| 346 | where "function_definition" matches the node type of a function | 365 | where "function_definition" matches the node type of a function |
| 347 | definition node, and ’end means we want to go to the end of that | 366 | definition node, and ’end means we want to go to the end of that node. |
| 348 | node. | 367 | |
| 349 | 368 | Tree-sitter has default implementations for | |
| 350 | Something like this should suffice: | 369 | ‘beginning-of-defun-function’ and ‘end-of-defun-function’. So for |
| 351 | 370 | ordinary languages, it is suffice to set ‘treesit-defun-type-regexp’ | |
| 352 | #+begin_src elisp | 371 | to something that matches all the defun struct types in the language, |
| 353 | (defun js--treesit-beginning-of-defun (&optional arg) | 372 | and call ‘treesit-major-mode-setup’. For example, |
| 354 | (let ((arg (or arg 1))) | 373 | |
| 355 | (if (> arg 0) | 374 | #+begin_src emacs-lisp |
| 356 | ;; Go backward. | 375 | (setq-local treesit-defun-type-regexp (rx bol |
| 357 | (while (and (> arg 0) | 376 | (or "function" "class") |
| 358 | (treesit-search-forward-goto | 377 | "_definition" |
| 359 | "function_definition" 'start nil t)) | 378 | eol)) |
| 360 | (setq arg (1- arg))) | 379 | (treesit-major-mode-setup) |
| 361 | ;; Go forward. | 380 | #+end_src> |
| 362 | (while (and (< arg 0) | ||
| 363 | (treesit-search-forward-goto | ||
| 364 | "function_definition" 'start)) | ||
| 365 | (setq arg (1+ arg)))))) | ||
| 366 | |||
| 367 | (defun xxx-end-of-defun (&optional arg) | ||
| 368 | (let ((arg (or arg 1))) | ||
| 369 | (if (< arg 0) | ||
| 370 | ;; Go backward. | ||
| 371 | (while (and (< arg 0) | ||
| 372 | (treesit-search-forward-goto | ||
| 373 | "function_definition" 'end nil t)) | ||
| 374 | (setq arg (1+ arg))) | ||
| 375 | ;; Go forward. | ||
| 376 | (while (and (> arg 0) | ||
| 377 | (treesit-search-forward-goto | ||
| 378 | "function_definition" 'end)) | ||
| 379 | (setq arg (1- arg)))))) | ||
| 380 | |||
| 381 | (setq-local beginning-of-defun-function #'xxx-beginning-of-defun) | ||
| 382 | (setq-local end-of-defun-function #'xxx-end-of-defun) | ||
| 383 | #+end_src | ||
| 384 | 381 | ||
| 385 | * Which-func | 382 | * Which-func |
| 386 | 383 | ||
| 387 | You can find the current function by going up the tree and looking for | 384 | If you have an imenu implementation, set ‘which-func-functions’ to |
| 388 | the function_definition node. See ‘python-info-treesit-current-defun’ | 385 | nil, and which-func will automatically use imenu’s data. |
| 389 | in python.el for an example. Since Python allows nested function | 386 | |
| 390 | definitions, that function keeps going until it reaches the root node, | 387 | If you want independent implementation for which-func, you can find |
| 391 | and records all the function names along the way. | 388 | the current function by going up the tree and looking for the |
| 389 | function_definition node. See the function below for an example. | ||
| 390 | Since Python allows nested function definitions, that function keeps | ||
| 391 | going until it reaches the root node, and records all the function | ||
| 392 | names along the way. | ||
| 392 | 393 | ||
| 393 | #+begin_src elisp | 394 | #+begin_src elisp |
| 394 | (defun python-info-treesit-current-defun (&optional include-type) | 395 | (defun python-info-treesit-current-defun (&optional include-type) |