diff options
| author | Yuan Fu | 2023-09-01 17:29:06 -0700 |
|---|---|---|
| committer | Yuan Fu | 2023-09-01 20:38:31 -0700 |
| commit | 452697a81ab330d5fe7b4b4ece3acfddd5330a72 (patch) | |
| tree | 060879fe8a857103ac86d3c2a7c7280cae0d78de /src | |
| parent | 2547fbe9c44c0c4642426c008db8f1b4d6867f4c (diff) | |
| download | emacs-452697a81ab330d5fe7b4b4ece3acfddd5330a72.tar.gz emacs-452697a81ab330d5fe7b4b4ece3acfddd5330a72.zip | |
Add an optional arg to Ftreesit_node_match_p
* src/treesit.c (treesit_traverse_child_helper): Fix typo.
(treesit_traverse_get_predicate): Add ignore_missing.
(Ftreesit_search_subtree)
(Ftreesit_search_forward)
(Ftreesit_induce_sparse_tree): Update docstring.
(Ftreesit_node_match_p): Add ignore_missing.
Diffstat (limited to 'src')
| -rw-r--r-- | src/treesit.c | 70 |
1 files changed, 46 insertions, 24 deletions
diff --git a/src/treesit.c b/src/treesit.c index 550bc8b9ee0..ce9d867fb2c 100644 --- a/src/treesit.c +++ b/src/treesit.c | |||
| @@ -3220,7 +3220,7 @@ treesit_traverse_child_helper (TSTreeCursor *cursor, | |||
| 3220 | } | 3220 | } |
| 3221 | 3221 | ||
| 3222 | /* Given a symbol THING, and a language symbol LANGUAGE, find the | 3222 | /* Given a symbol THING, and a language symbol LANGUAGE, find the |
| 3223 | corresponding predicate definition in treesit-things-settings. | 3223 | corresponding predicate definition in treesit-thing-settings. |
| 3224 | Don't check for the type of THING and LANGUAGE. | 3224 | Don't check for the type of THING and LANGUAGE. |
| 3225 | 3225 | ||
| 3226 | If there isn't one, return Qnil. */ | 3226 | If there isn't one, return Qnil. */ |
| @@ -3246,13 +3246,15 @@ treesit_traverse_get_predicate (Lisp_Object thing, Lisp_Object language) | |||
| 3246 | return false, otherwise return true. This function also check for | 3246 | return false, otherwise return true. This function also check for |
| 3247 | recusion levels: we place a arbitrary 100 level limit on recursive | 3247 | recusion levels: we place a arbitrary 100 level limit on recursive |
| 3248 | predicates. RECURSION_LEVEL is the current recursion level (that | 3248 | predicates. RECURSION_LEVEL is the current recursion level (that |
| 3249 | starts at 0), if it goes over 99, return false and set | 3249 | starts at 0), if it goes over 99, return false and set SIGNAL_DATA. |
| 3250 | SIGNAL_DATA. LANGUAGE is a LANGUAGE symbol. */ | 3250 | LANGUAGE is a LANGUAGE symbol. See `Ftreesit_node_match_p' for |
| 3251 | ignore_missing. */ | ||
| 3251 | static bool | 3252 | static bool |
| 3252 | treesit_traverse_validate_predicate (Lisp_Object pred, | 3253 | treesit_traverse_validate_predicate (Lisp_Object pred, |
| 3253 | Lisp_Object language, | 3254 | Lisp_Object language, |
| 3254 | Lisp_Object *signal_data, | 3255 | Lisp_Object *signal_data, |
| 3255 | ptrdiff_t recursion_level) | 3256 | ptrdiff_t recursion_level, |
| 3257 | bool ignore_missing) | ||
| 3256 | { | 3258 | { |
| 3257 | if (recursion_level > 99) | 3259 | if (recursion_level > 99) |
| 3258 | { | 3260 | { |
| @@ -3271,6 +3273,8 @@ treesit_traverse_validate_predicate (Lisp_Object pred, | |||
| 3271 | language); | 3273 | language); |
| 3272 | if (NILP (definition)) | 3274 | if (NILP (definition)) |
| 3273 | { | 3275 | { |
| 3276 | if (ignore_missing) | ||
| 3277 | return false; | ||
| 3274 | *signal_data = list2 (build_string ("Cannot find the definition " | 3278 | *signal_data = list2 (build_string ("Cannot find the definition " |
| 3275 | "of the predicate in " | 3279 | "of the predicate in " |
| 3276 | "`treesit-thing-settings'"), | 3280 | "`treesit-thing-settings'"), |
| @@ -3280,7 +3284,8 @@ treesit_traverse_validate_predicate (Lisp_Object pred, | |||
| 3280 | return treesit_traverse_validate_predicate (definition, | 3284 | return treesit_traverse_validate_predicate (definition, |
| 3281 | language, | 3285 | language, |
| 3282 | signal_data, | 3286 | signal_data, |
| 3283 | recursion_level + 1); | 3287 | recursion_level + 1, |
| 3288 | ignore_missing); | ||
| 3284 | } | 3289 | } |
| 3285 | else if (CONSP (pred)) | 3290 | else if (CONSP (pred)) |
| 3286 | { | 3291 | { |
| @@ -3306,7 +3311,8 @@ treesit_traverse_validate_predicate (Lisp_Object pred, | |||
| 3306 | return treesit_traverse_validate_predicate (XCAR (cdr), | 3311 | return treesit_traverse_validate_predicate (XCAR (cdr), |
| 3307 | language, | 3312 | language, |
| 3308 | signal_data, | 3313 | signal_data, |
| 3309 | recursion_level + 1); | 3314 | recursion_level + 1, |
| 3315 | ignore_missing); | ||
| 3310 | } | 3316 | } |
| 3311 | else if (BASE_EQ (car, Qor)) | 3317 | else if (BASE_EQ (car, Qor)) |
| 3312 | { | 3318 | { |
| @@ -3323,7 +3329,8 @@ treesit_traverse_validate_predicate (Lisp_Object pred, | |||
| 3323 | if (!treesit_traverse_validate_predicate (XCAR (cdr), | 3329 | if (!treesit_traverse_validate_predicate (XCAR (cdr), |
| 3324 | language, | 3330 | language, |
| 3325 | signal_data, | 3331 | signal_data, |
| 3326 | recursion_level + 1)) | 3332 | recursion_level + 1, |
| 3333 | ignore_missing)) | ||
| 3327 | return false; | 3334 | return false; |
| 3328 | } | 3335 | } |
| 3329 | return true; | 3336 | return true; |
| @@ -3512,8 +3519,10 @@ DEFUN ("treesit-search-subtree", | |||
| 3512 | doc: /* Traverse the parse tree of NODE depth-first using PREDICATE. | 3519 | doc: /* Traverse the parse tree of NODE depth-first using PREDICATE. |
| 3513 | 3520 | ||
| 3514 | Traverse the subtree of NODE, and match PREDICATE with each node along | 3521 | Traverse the subtree of NODE, and match PREDICATE with each node along |
| 3515 | the way. PREDICATE is a regexp string that matches against each | 3522 | the way. PREDICATE can be a regexp string that matches against each |
| 3516 | node's type, or a function that takes a node and returns nil/non-nil. | 3523 | node's type, a predicate function, and more. See |
| 3524 | `treesit-thing-settings' for all possible predicates. PREDICATE can | ||
| 3525 | also be a thing defined in `treesit-thing-settings'. | ||
| 3517 | 3526 | ||
| 3518 | By default, only traverse named nodes, but if ALL is non-nil, traverse | 3527 | By default, only traverse named nodes, but if ALL is non-nil, traverse |
| 3519 | all nodes. If BACKWARD is non-nil, traverse backwards. If DEPTH is | 3528 | all nodes. If BACKWARD is non-nil, traverse backwards. If DEPTH is |
| @@ -3544,7 +3553,7 @@ Return the first matched node, or nil if none matches. */) | |||
| 3544 | 3553 | ||
| 3545 | Lisp_Object signal_data = Qnil; | 3554 | Lisp_Object signal_data = Qnil; |
| 3546 | if (!treesit_traverse_validate_predicate (predicate, language, | 3555 | if (!treesit_traverse_validate_predicate (predicate, language, |
| 3547 | &signal_data, 0)) | 3556 | &signal_data, 0, false)) |
| 3548 | xsignal1 (Qtreesit_invalid_predicate, signal_data); | 3557 | xsignal1 (Qtreesit_invalid_predicate, signal_data); |
| 3549 | 3558 | ||
| 3550 | Lisp_Object return_value = Qnil; | 3559 | Lisp_Object return_value = Qnil; |
| @@ -3571,9 +3580,11 @@ DEFUN ("treesit-search-forward", | |||
| 3571 | doc: /* Search for node matching PREDICATE in the parse tree of START. | 3580 | doc: /* Search for node matching PREDICATE in the parse tree of START. |
| 3572 | 3581 | ||
| 3573 | Start traversing the tree from node START, and match PREDICATE with | 3582 | Start traversing the tree from node START, and match PREDICATE with |
| 3574 | each node (except START itself) along the way. PREDICATE is a regexp | 3583 | each node (except START itself) along the way. PREDICATE can be a |
| 3575 | string that matches against each node's type, or a function that takes | 3584 | regexp string that matches against each node's type, a predicate |
| 3576 | a node and returns non-nil if it matches. | 3585 | function, and more. See `treesit-thing-settings' for all possible |
| 3586 | predicates. PREDICATE can also be a thing defined in | ||
| 3587 | `treesit-thing-settings'. | ||
| 3577 | 3588 | ||
| 3578 | By default, only search for named nodes, but if ALL is non-nil, search | 3589 | By default, only search for named nodes, but if ALL is non-nil, search |
| 3579 | for all nodes. If BACKWARD is non-nil, search backwards. | 3590 | for all nodes. If BACKWARD is non-nil, search backwards. |
| @@ -3609,7 +3620,7 @@ always traverse leaf nodes first, then upwards. */) | |||
| 3609 | 3620 | ||
| 3610 | Lisp_Object signal_data = Qnil; | 3621 | Lisp_Object signal_data = Qnil; |
| 3611 | if (!treesit_traverse_validate_predicate (predicate, language, | 3622 | if (!treesit_traverse_validate_predicate (predicate, language, |
| 3612 | &signal_data, 0)) | 3623 | &signal_data, 0, false)) |
| 3613 | xsignal1 (Qtreesit_invalid_predicate, signal_data); | 3624 | xsignal1 (Qtreesit_invalid_predicate, signal_data); |
| 3614 | 3625 | ||
| 3615 | Lisp_Object return_value = Qnil; | 3626 | Lisp_Object return_value = Qnil; |
| @@ -3683,9 +3694,12 @@ DEFUN ("treesit-induce-sparse-tree", | |||
| 3683 | Streesit_induce_sparse_tree, 2, 4, 0, | 3694 | Streesit_induce_sparse_tree, 2, 4, 0, |
| 3684 | doc: /* Create a sparse tree of ROOT's subtree. | 3695 | doc: /* Create a sparse tree of ROOT's subtree. |
| 3685 | 3696 | ||
| 3686 | This takes the subtree under ROOT, and combs it so only the nodes | 3697 | This takes the subtree under ROOT, and combs it so only the nodes that |
| 3687 | that match PREDICATE are left, like picking out grapes on the vine. | 3698 | match PREDICATE are left, like picking out grapes on the vine. |
| 3688 | PREDICATE is a regexp string that matches against each node's type. | 3699 | PREDICATE can be a regexp string that matches against each node's |
| 3700 | type, a predicate function, and more. See `treesit-thing-settings' | ||
| 3701 | for all possible predicates. PREDICATE can also be a thing defined in | ||
| 3702 | `treesit-thing-settings'. | ||
| 3689 | 3703 | ||
| 3690 | For a subtree on the left that consist of both numbers and letters, if | 3704 | For a subtree on the left that consist of both numbers and letters, if |
| 3691 | PREDICATE is "is letter", the returned tree is the one on the right. | 3705 | PREDICATE is "is letter", the returned tree is the one on the right. |
| @@ -3741,7 +3755,7 @@ a regexp. */) | |||
| 3741 | 3755 | ||
| 3742 | Lisp_Object signal_data = Qnil; | 3756 | Lisp_Object signal_data = Qnil; |
| 3743 | if (!treesit_traverse_validate_predicate (predicate, language, | 3757 | if (!treesit_traverse_validate_predicate (predicate, language, |
| 3744 | &signal_data, 0)) | 3758 | &signal_data, 0, false)) |
| 3745 | xsignal1 (Qtreesit_invalid_predicate, signal_data); | 3759 | xsignal1 (Qtreesit_invalid_predicate, signal_data); |
| 3746 | 3760 | ||
| 3747 | Lisp_Object parent = Fcons (Qnil, Qnil); | 3761 | Lisp_Object parent = Fcons (Qnil, Qnil); |
| @@ -3767,13 +3781,20 @@ a regexp. */) | |||
| 3767 | 3781 | ||
| 3768 | DEFUN ("treesit-node-match-p", | 3782 | DEFUN ("treesit-node-match-p", |
| 3769 | Ftreesit_node_match_p, | 3783 | Ftreesit_node_match_p, |
| 3770 | Streesit_node_match_p, 2, 2, 0, | 3784 | Streesit_node_match_p, 2, 3, 0, |
| 3771 | doc: /* Check whether NODE matches PREDICATE. | 3785 | doc: /* Check whether NODE matches PREDICATE. |
| 3772 | 3786 | ||
| 3773 | PREDICATE can be a regexp matching node type, a predicate function, | 3787 | PREDICATE can be a symbol representing a thing in |
| 3774 | and more, see `treesit-thing-settings' for detail. Return non-nil | 3788 | `treesit-thing-settings', or an predicate, like regexp matching node |
| 3775 | if NODE matches PRED, nil otherwise. */) | 3789 | type, etc. See `treesit-thing-settings' for detail. |
| 3776 | (Lisp_Object node, Lisp_Object predicate) | 3790 | |
| 3791 | Return non-nil if NODE matches PRED, nil otherwise. | ||
| 3792 | |||
| 3793 | Signals `treesit-invalid-predicate' if there's no definition of THING | ||
| 3794 | in `treesit-thing-settings', or the predicate is malformed. If | ||
| 3795 | IGNORE-MISSING is non-nil, don't signal for missing THING definition, | ||
| 3796 | but still signal for malformed predicate. */) | ||
| 3797 | (Lisp_Object node, Lisp_Object predicate, Lisp_Object ignore_missing) | ||
| 3777 | { | 3798 | { |
| 3778 | CHECK_TS_NODE (node); | 3799 | CHECK_TS_NODE (node); |
| 3779 | 3800 | ||
| @@ -3782,7 +3803,8 @@ if NODE matches PRED, nil otherwise. */) | |||
| 3782 | 3803 | ||
| 3783 | Lisp_Object signal_data = Qnil; | 3804 | Lisp_Object signal_data = Qnil; |
| 3784 | if (!treesit_traverse_validate_predicate (predicate, language, | 3805 | if (!treesit_traverse_validate_predicate (predicate, language, |
| 3785 | &signal_data, 0)) | 3806 | &signal_data, 0, |
| 3807 | !NILP (ignore_missing))) | ||
| 3786 | xsignal1 (Qtreesit_invalid_predicate, signal_data); | 3808 | xsignal1 (Qtreesit_invalid_predicate, signal_data); |
| 3787 | 3809 | ||
| 3788 | TSTreeCursor cursor = ts_tree_cursor_new (XTS_NODE (node)->node); | 3810 | TSTreeCursor cursor = ts_tree_cursor_new (XTS_NODE (node)->node); |