diff options
Diffstat (limited to 'admin/notes/tree-sitter/html-manual/Using-Parser.html')
| -rw-r--r-- | admin/notes/tree-sitter/html-manual/Using-Parser.html | 89 |
1 files changed, 46 insertions, 43 deletions
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> |