Function: treesit-parse-string

treesit-parse-string is a function defined in treesit.c.

Signature

(treesit-parse-string STRING LANGUAGE)

Documentation

Parse STRING using a parser for LANGUAGE.

Return the root node of the result parse tree. DO NOT use this function in a loop: this function is intended for one-off use and isn't optimized; for heavy workload, use a temporary buffer instead.

Other relevant functions are documented in the treesit group.

View in manual

Shortdoc

;; treesit
(treesit-parse-string "int c = 0;" 'c)
    e.g. => #<treesit-node (translation_unit) in 1-11>

Source Code

// Defined in /usr/src/emacs/src/treesit.c
{
  CHECK_SYMBOL (language);
  CHECK_STRING (string);

  Lisp_Object name_str = build_string (" *treesit-parse-string*");
  Lisp_Object buffer_name = Fgenerate_new_buffer_name (name_str, Qnil);
  Lisp_Object buffer = Fget_buffer_create (buffer_name, Qnil);

  struct buffer *old_buffer = current_buffer;
  set_buffer_internal (XBUFFER (buffer));
  insert1 (string);
  set_buffer_internal (old_buffer);

  Lisp_Object parser = Ftreesit_parser_create (language, buffer, Qt, Qnil);
  XTS_PARSER (parser)->need_to_gc_buffer = true;

  /* Make sure the temp buffer doesn't reference the parser, otherwise
     the buffer and parser cross-reference each other and the parser is
     never garbage-collected.  */
  BVAR (XBUFFER (buffer), ts_parser_list) = Qnil;

  return Ftreesit_parser_root_node (parser);
}