aboutsummaryrefslogtreecommitdiffstats
path: root/admin/notes/tree-sitter/html-manual/Using-Parser.html
diff options
context:
space:
mode:
authorYuan Fu2022-11-03 11:41:42 -0700
committerYuan Fu2022-11-03 11:43:44 -0700
commit5416ae5990337f5fb2b3e0fbf9c4575508da808e (patch)
tree10d24c612d033ce0e21828161eefaa37e7e38559 /admin/notes/tree-sitter/html-manual/Using-Parser.html
parent99096528494a653d1ee9d7832ee3956a613aeb95 (diff)
downloademacs-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/Using-Parser.html')
-rw-r--r--admin/notes/tree-sitter/html-manual/Using-Parser.html89
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
73parser. In Emacs, each tree-sitter parser is associated with a 73parser. In Emacs, each tree-sitter parser is associated with a
74buffer. As we edit the buffer, the associated parser and the syntax 74buffer. As the user edits the buffer, the associated parser and
75tree is automatically kept up-to-date. 75syntax 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'> &para;</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'> &para;</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 &amp;optional buffer no-reuse</em><a href='#index-treesit_002dparser_002dcreate' class='copiable-anchor'> &para;</a></span></dt> 94<dt id="index-treesit_002dparser_002dcreate"><span class="category">Function: </span><span><strong>treesit-parser-create</strong> <em>language &amp;optional buffer no-reuse</em><a href='#index-treesit_002dparser_002dcreate' class='copiable-anchor'> &para;</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>
95to 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
96current 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
100function always creates a new parser. 101non-<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'> &para;</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'> &para;</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'> &para;</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'> &para;</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'> &para;</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'> &para;</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
118is, return nil otherwise. 119returns 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
122automatically and lazily. A parser only parses when we query for a 123automatically and lazily. A parser only parses when a Lisp program
123node in its syntax tree. Therefore, when a parser is first created, 124queries for a node in its syntax tree. Therefore, when a parser is
124it doesn&rsquo;t parse the buffer; it waits until we query for a node for 125first created, it doesn&rsquo;t parse the buffer; it waits until the Lisp
125the first time. Similarly, when some change is made in the buffer, a 126program queries for a node for the first time. Similarly, when some
126parser doesn&rsquo;t re-parse immediately. 127change is made in the buffer, a parser doesn&rsquo;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.
130Tree-sitter can only handle buffer no larger than about 4GB. If the 131Tree-sitter can only handle buffer no larger than about 4GB. If the
131size exceeds that, Emacs signals <code>treesit-buffer-too-large</code> 132size exceeds that, Emacs signals the <code>treesit-buffer-too-large</code>
132with signal data being the buffer size. 133error 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
135internal parser list. Every time a change is made to the buffer, 136internal 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>&amp;optional buffer</em><a href='#index-treesit_002dparser_002dlist' class='copiable-anchor'> &para;</a></span></dt> 141<dt id="index-treesit_002dparser_002dlist"><span class="category">Function: </span><span><strong>treesit-parser-list</strong> <em>&amp;optional buffer</em><a href='#index-treesit_002dparser_002dlist' class='copiable-anchor'> &para;</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
144buffer.
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 &ldquo;sees&rdquo; the whole 153<span id="tree_002dsitter-narrowing"></span><p>Normally, a parser &ldquo;sees&rdquo; the whole buffer, but when the buffer is
152buffer, but when the buffer is narrowed (see <a href="Narrowing.html">Narrowing</a>), the 154narrowed (see <a href="Narrowing.html">Narrowing</a>), the parser will only see the accessible
153parser will only see the visible region. As far as the parser can 155portion of the buffer. As far as the parser can tell, the hidden
154tell, the hidden region is deleted. And when the buffer is later 156region was deleted. When the buffer is later widened, the parser
155widened, the parser thinks text is inserted in the beginning and in 157thinks text is inserted at the beginning and at the end. Although
156the end. Although parsers respect narrowing, narrowing shouldn&rsquo;t be 158parsers respect narrowing, modes should not use narrowing as a means
157the mean to handle a multi-language buffer; instead, set the ranges in 159to handle a multi-language buffer; instead, set the ranges in which the
158which a parser should operate in. See <a href="Multiple-Languages.html">Parsing Text in Multiple Languages</a>. 160parser 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
161is not affected immediately; as long as we don&rsquo;t query for a node 163narrows the buffer, the parser is not affected immediately; as long as
162while the buffer is narrowed, the parser is oblivious of the 164the mode doesn&rsquo;t query for a node while the buffer is narrowed, the
163narrowing. 165parser 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'> &para;</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 170string. Unlike a buffer, parsing a string is a one-off operation, and
169string. Unlike a buffer, parsing a string is a one-time deal, and
170there is no way to update the result. 171there 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">
173root 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'> &para;</a></span></dt>
175<dd><p>This function parses <var>string</var> using <var>language</var>, and returns
176the root node of the generated syntax tree.
174</p></dd></dl> 177</p></dd></dl>
175 178
176</div> 179</div>