]> git.eshelyaron.com Git - emacs.git/commitdiff
Add new type treesit-compiled-query
authorYuan Fu <casouri@gmail.com>
Tue, 14 Jun 2022 06:01:04 +0000 (23:01 -0700)
committerYuan Fu <casouri@gmail.com>
Tue, 14 Jun 2022 18:44:04 +0000 (11:44 -0700)
No intergration/interaction with the new type, just adding it.

* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add new type.
* src/alloc.c (cleanup_vector): Add gc for the new type.
* src/data.c (Ftype_of): Add switch case for the new type.
(syms_of_data): Add symbols for the new type.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add new type.
* src/treesit.c (Ftreesit_compiled_query_p): New function.
(syms_of_treesit): Add symbol for the new type.
* src/treesit.h (struct Lisp_TS_Query): New struct.
(TS_COMPILED_QUERY_P, XTS_COMPILED_QUERY, CHECK_TS_COMPILED_QUERY):
New macros.
* src/print.c (print_vectorlike): Add printing for the new type.

lisp/emacs-lisp/cl-preloaded.el
src/alloc.c
src/data.c
src/lisp.h
src/print.c
src/treesit.c
src/treesit.h

index 46f5ab35ff781474efb62ab64cd34762649e06b5..812d0af86b123138604a273c0db042409f0a401a 100644 (file)
@@ -80,6 +80,7 @@
     (user-ptr atom)
     (tree-sitter-parser atom)
     (tree-sitter-node atom)
+    (tree-sitter-compiled-query atom)
     ;; Plus, really hand made:
     (null symbol list sequence atom))
   "Alist of supertypes.
index 40a3e235eabf7fa303ab0f8b4b6f7abad6884a22..3c622d05ff103b8f3cd401a0e1401f8cd2ff6e8d 100644 (file)
@@ -3174,6 +3174,13 @@ cleanup_vector (struct Lisp_Vector *vector)
       ts_tree_delete(lisp_parser->tree);
       ts_parser_delete(lisp_parser->parser);
     }
+  else if (PSEUDOVECTOR_TYPEP (&vector->header, PVEC_TS_COMPILED_QUERY))
+    {
+      struct Lisp_TS_Query *lisp_query
+       = PSEUDOVEC_STRUCT (vector, Lisp_TS_Query);
+      ts_query_delete (lisp_query->query);
+      ts_query_cursor_delete (lisp_query->cursor);
+    }
 #endif
 #ifdef HAVE_MODULES
   else if (PSEUDOVECTOR_TYPEP (&vector->header, PVEC_MODULE_FUNCTION))
index a28bf4141472ef90dc0be0376938586884639525..8dbb2902a72b2e8e7bf464195e61052e0b745c55 100644 (file)
@@ -265,6 +265,8 @@ for example, (type-of 1) returns `integer'.  */)
          return Qtreesit_parser;
        case PVEC_TS_NODE:
          return Qtreesit_node;
+       case PVEC_TS_COMPILED_QUERY:
+         return Qtreesit_compiled_query;
         case PVEC_SQLITE:
           return Qsqlite;
         /* "Impossible" cases.  */
@@ -4264,6 +4266,7 @@ syms_of_data (void)
   DEFSYM (Qxwidget_view, "xwidget-view");
   DEFSYM (Qtreesit_parser, "treesit-parser");
   DEFSYM (Qtreesit_node, "treesit-node");
+  DEFSYM (Qtreesit_compiled_query, "treesit-compiled-query");
 
   DEFSYM (Qdefun, "defun");
 
index eb1f1ec2c2184366b979c097481fc059d1411718..8832e76b447d34a0a04ab321adb7dbe0d4c36c51 100644 (file)
@@ -1060,6 +1060,7 @@ enum pvec_type
   PVEC_NATIVE_COMP_UNIT,
   PVEC_TS_PARSER,
   PVEC_TS_NODE,
+  PVEC_TS_COMPILED_QUERY,
   PVEC_SQLITE,
 
   /* These should be last, for internal_equal and sxhash_obj.  */
index d8b8513f311bce4dfe646c84208187a7b1ef9b97..81b524d79fe2cbfc7414047c81a5e4cc44ff37ee 100644 (file)
@@ -1982,6 +1982,9 @@ print_vectorlike (Lisp_Object obj, Lisp_Object printcharfun, bool escapeflag,
                    printcharfun, escapeflag);
       printchar ('>', printcharfun);
       break;
+    case PVEC_TS_COMPILED_QUERY:
+      print_c_string ("#<treesit-compiled-query>", printcharfun);
+      break;
 #endif
 
     case PVEC_SQLITE:
index 91114b06f1042eed01c07b0017e793f26268edc6..19f83437650320af31ad335fc1c8942ee79aadb9 100644 (file)
@@ -558,6 +558,17 @@ DEFUN ("treesit-node-p",
     return Qnil;
 }
 
+DEFUN ("treesit-compiled-query-p",
+       Ftreesit_compiled_query_p, Streesit_compiled_query_p, 1, 1, 0,
+       doc: /* Return t if OBJECT is a compiled tree-sitter query.  */)
+  (Lisp_Object object)
+{
+  if (TS_COMPILED_QUERY_P (object))
+    return Qt;
+  else
+    return Qnil;
+}
+
 DEFUN ("treesit-node-parser",
        Ftreesit_node_parser, Streesit_node_parser,
        1, 1, 0,
@@ -1568,6 +1579,7 @@ syms_of_treesit (void)
 {
   DEFSYM (Qtreesit_parser_p, "treesit-parser-p");
   DEFSYM (Qtreesit_node_p, "treesit-node-p");
+  DEFSYM (Qtreesit_compiled_query_p, "treesit-compiled-query-p");
   DEFSYM (Qnamed, "named");
   DEFSYM (Qmissing, "missing");
   DEFSYM (Qextra, "extra");
@@ -1648,6 +1660,7 @@ dynamic libraries, in that order.  */);
 
   defsubr (&Streesit_parser_p);
   defsubr (&Streesit_node_p);
+  defsubr (&Streesit_compiled_query_p);
 
   defsubr (&Streesit_node_parser);
 
index 639c4eedc552e26bcbd84223a2711b16f4f7594d..cb00fee1113844af0c80cb53341487a63a62d5ee 100644 (file)
@@ -81,6 +81,17 @@ struct Lisp_TS_Node
   ptrdiff_t timestamp;
 };
 
+/* A compiled tree-sitter query.  */
+struct Lisp_TS_Query
+{
+  union vectorlike_header header;
+  /* Pointer to the query object.  */
+  TSQuery *query;
+  /* Pointer to a cursor.  If we are storing the query object, we
+     might as well store a cursor, too.  */
+  TSQueryCursor *cursor;
+};
+
 INLINE bool
 TS_PARSERP (Lisp_Object x)
 {
@@ -107,6 +118,19 @@ XTS_NODE (Lisp_Object a)
   return XUNTAG (a, Lisp_Vectorlike, struct Lisp_TS_Node);
 }
 
+INLINE bool
+TS_COMPILED_QUERY_P (Lisp_Object x)
+{
+  return PSEUDOVECTORP (x, PVEC_TS_COMPILED_QUERY);
+}
+
+INLINE struct Lisp_TS_Query *
+XTS_COMPILED_QUERY (Lisp_Object a)
+{
+  eassert (TS_COMPILED_QUERY_P (a));
+  return XUNTAG (a, Lisp_Vectorlike, struct Lisp_TS_Query);
+}
+
 INLINE void
 CHECK_TS_PARSER (Lisp_Object parser)
 {
@@ -119,6 +143,13 @@ CHECK_TS_NODE (Lisp_Object node)
   CHECK_TYPE (TS_NODEP (node), Qtreesit_node_p, node);
 }
 
+INLINE void
+CHECK_TS_COMPILED_QUERY (Lisp_Object query)
+{
+  CHECK_TYPE (TS_COMPILED_QUERY_P (query),
+             Qtreesit_compiled_query_p, query);
+}
+
 void
 ts_record_change (ptrdiff_t start_byte, ptrdiff_t old_end_byte,
                  ptrdiff_t new_end_byte);