emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

feature/tree-sitter 8f3b872e30 08/26: Add new type treesit-compiled-quer


From: Yuan Fu
Subject: feature/tree-sitter 8f3b872e30 08/26: Add new type treesit-compiled-query
Date: Thu, 16 Jun 2022 14:53:45 -0400 (EDT)

branch: feature/tree-sitter
commit 8f3b872e30cc1055b1c5d35acfcf1ef7d483b01e
Author: Yuan Fu <casouri@gmail.com>
Commit: Yuan Fu <casouri@gmail.com>

    Add new type treesit-compiled-query
    
    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 |  1 +
 src/alloc.c                     |  7 +++++++
 src/data.c                      |  3 +++
 src/lisp.h                      |  1 +
 src/print.c                     |  3 +++
 src/treesit.c                   | 13 +++++++++++++
 src/treesit.h                   | 31 +++++++++++++++++++++++++++++++
 7 files changed, 59 insertions(+)

diff --git a/lisp/emacs-lisp/cl-preloaded.el b/lisp/emacs-lisp/cl-preloaded.el
index 46f5ab35ff..812d0af86b 100644
--- a/lisp/emacs-lisp/cl-preloaded.el
+++ b/lisp/emacs-lisp/cl-preloaded.el
@@ -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.
diff --git a/src/alloc.c b/src/alloc.c
index 40a3e235ea..3c622d05ff 100644
--- a/src/alloc.c
+++ b/src/alloc.c
@@ -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))
diff --git a/src/data.c b/src/data.c
index a28bf41414..8dbb2902a7 100644
--- a/src/data.c
+++ b/src/data.c
@@ -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");
 
diff --git a/src/lisp.h b/src/lisp.h
index eb1f1ec2c2..8832e76b44 100644
--- a/src/lisp.h
+++ b/src/lisp.h
@@ -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.  */
diff --git a/src/print.c b/src/print.c
index d8b8513f31..81b524d79f 100644
--- a/src/print.c
+++ b/src/print.c
@@ -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:
diff --git a/src/treesit.c b/src/treesit.c
index 91114b06f1..19f8343765 100644
--- a/src/treesit.c
+++ b/src/treesit.c
@@ -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);
 
diff --git a/src/treesit.h b/src/treesit.h
index 639c4eedc5..cb00fee111 100644
--- a/src/treesit.h
+++ b/src/treesit.h
@@ -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);



reply via email to

[Prev in Thread] Current Thread [Next in Thread]