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/notes/tree-sitter/html-manual/Pattern-Matching.html | |
| 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/notes/tree-sitter/html-manual/Pattern-Matching.html')
| -rw-r--r-- | admin/notes/tree-sitter/html-manual/Pattern-Matching.html | 199 |
1 files changed, 110 insertions, 89 deletions
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 | ||