static void
ts_symbol_to_c_name (char *symbol_name)
{
- for (int idx=0; idx < strlen (symbol_name); idx++)
+ for (int idx = 0; idx < strlen (symbol_name); idx++)
{
if (symbol_name[idx] == '-')
symbol_name[idx] = '_';
}
static bool
-ts_find_override_name
-(Lisp_Object language_symbol, Lisp_Object *name, Lisp_Object *c_symbol)
+ts_find_override_name (Lisp_Object language_symbol, Lisp_Object *name,
+ Lisp_Object *c_symbol)
{
for (Lisp_Object list = Vtreesit_load_name_override_list;
!NILP (list); list = XCDR (list))
}
/* For example, if Vdynamic_library_suffixes is (".so", ".dylib"),
- thsi function pushes "lib_base_name.so" and "lib_base_name.dylib"
- into *path_candidates. Obiviously path_candidates should be a Lisp
+ this function pushes "lib_base_name.so" and "lib_base_name.dylib"
+ into *path_candidates. Obiviously path_candidates should be a Lisp
list of Lisp strings. */
static void
-ts_load_language_push_for_each_suffix
-(Lisp_Object lib_base_name, Lisp_Object *path_candidates)
+ts_load_language_push_for_each_suffix (Lisp_Object lib_base_name,
+ Lisp_Object *path_candidates)
{
for (Lisp_Object suffixes = Vdynamic_library_suffixes;
- !NILP (suffixes); suffixes = XCDR (suffixes)) {
- *path_candidates = Fcons (concat2 (lib_base_name, XCAR (suffixes)),
- *path_candidates);
- }
+ !NILP (suffixes); suffixes = XCDR (suffixes))
+ {
+ *path_candidates = Fcons (concat2 (lib_base_name, XCAR (suffixes)),
+ *path_candidates);
+ }
}
/* Load the dynamic library of LANGUAGE_SYMBOL and return the pointer
/* Figure out the library name and C name. */
Lisp_Object lib_base_name =
- (concat2 (build_pure_c_string ("libtree-sitter-"), symbol_name));
+ concat2 (build_pure_c_string ("libtree-sitter-"), symbol_name);
Lisp_Object base_name =
- (concat2 (build_pure_c_string ("tree-sitter-"), symbol_name));
+ concat2 (build_pure_c_string ("tree-sitter-"), symbol_name);
+ /* FIXME: The result of strdup leaks memory in some cases. */
char *c_name = strdup (SSDATA (base_name));
ts_symbol_to_c_name (c_name);
/* Override the library name and C name, if appropriate. */
Lisp_Object override_name;
Lisp_Object override_c_name;
- bool found_override = ts_find_override_name
- (language_symbol, &override_name, &override_c_name);
+ bool found_override = ts_find_override_name (language_symbol, &override_name,
+ &override_c_name);
if (found_override)
{
lib_base_name = override_name;
Lisp_Object path_candidates = Qnil;
/* First push just the filenames to the candidate list, which will
make dynlib_open look under standard system load paths. */
- ts_load_language_push_for_each_suffix
- (lib_base_name, &path_candidates);
+ ts_load_language_push_for_each_suffix (lib_base_name, &path_candidates);
/* Then push ~/.emacs.d/tree-sitter paths. */
- ts_load_language_push_for_each_suffix
- (Fexpand_file_name
- (concat2 (build_string ("tree-sitter/"), lib_base_name),
- Fsymbol_value (Quser_emacs_directory)),
- &path_candidates);
+ Lisp_Object lib_name =
+ Fexpand_file_name (concat2 (build_string ("tree-sitter/"), lib_base_name),
+ Fsymbol_value (Quser_emacs_directory));
+ ts_load_language_push_for_each_suffix (lib_name, &path_candidates);
/* Then push paths from treesit-extra-load-path. */
for (Lisp_Object tail = Freverse (Vtreesit_extra_load_path);
!NILP (tail); tail = XCDR (tail))
{
- ts_load_language_push_for_each_suffix
- (Fexpand_file_name (lib_base_name, XCAR (tail)),
- &path_candidates);
+ ts_load_language_push_for_each_suffix (Fexpand_file_name (lib_base_name,
+ XCAR (tail)),
+ &path_candidates);
}
/* Try loading the dynamic library by each path candidate. Stop
ts_initialize ();
Lisp_Object signal_symbol = Qnil;
Lisp_Object signal_data = Qnil;
- if (ts_load_language(language, &signal_symbol, &signal_data) == NULL)
+ if (ts_load_language (language, &signal_symbol, &signal_data) == NULL)
{
if (NILP (detail))
return Qnil;
Streesit_language_version,
0, 1, 0,
doc: /* Return the language version of tree-sitter library.
-If MIN-COMPATIBLE non-nil, return the minimal compatible version. */)
+If MIN-COMPATIBLE is non-nil, return the minimal compatible version. */)
(Lisp_Object min_compatible)
{
if (NILP (min_compatible))
eassert (start_byte <= old_end_byte);
eassert (start_byte <= new_end_byte);
TSPoint dummy_point = {0, 0};
+ /* FIXME: We should signal an error below if START_BYTE
+ etc. overflow the 32-bit unsigned data type. */
TSInputEdit edit = {(uint32_t) start_byte,
(uint32_t) old_end_byte,
(uint32_t) new_end_byte,
ts_record_change (ptrdiff_t start_byte, ptrdiff_t old_end_byte,
ptrdiff_t new_end_byte)
{
- for (Lisp_Object parser_list
- = BVAR (current_buffer, ts_parser_list);
+ for (Lisp_Object parser_list = BVAR (current_buffer, ts_parser_list);
!NILP (parser_list);
parser_list = XCDR (parser_list))
{
if (old_end_byte > new_end_byte)
{
/* Move backward. */
- visi_beg_delta = min (visible_beg, new_end_byte)
- - min (visible_beg, old_end_byte);
+ visi_beg_delta =
+ min (visible_beg, new_end_byte)
+ - min (visible_beg, old_end_byte);
}
else
{
/* Move forward. */
- visi_beg_delta = old_end_byte < visible_beg
- ? new_end_byte - old_end_byte : 0;
+ visi_beg_delta =
+ old_end_byte < visible_beg ? new_end_byte - old_end_byte : 0;
}
- XTS_PARSER (lisp_parser)->visible_beg
- = visible_beg + visi_beg_delta;
+ XTS_PARSER (lisp_parser)->visible_beg = visible_beg + visi_beg_delta;
XTS_PARSER (lisp_parser)->visible_end
- = visible_end + visi_beg_delta
- + (new_end_offset - old_end_offset);
+ = visible_end + visi_beg_delta + (new_end_offset - old_end_offset);
eassert (XTS_PARSER (lisp_parser)->visible_beg >= 0);
eassert (XTS_PARSER (lisp_parser)->visible_beg
<= XTS_PARSER (lisp_parser)->visible_end);
static void
ts_check_buffer_size (struct buffer *buffer)
{
- ptrdiff_t buffer_size =
- (BUF_Z (buffer) - BUF_BEG (buffer));
+ ptrdiff_t buffer_size = (BUF_Z (buffer) - BUF_BEG (buffer));
if (buffer_size > UINT32_MAX)
xsignal2 (Qtreesit_buffer_too_large,
build_pure_c_string ("Buffer size larger than 4GB, size:"),
ts_read_buffer (void *parser, uint32_t byte_index,
TSPoint position, uint32_t *bytes_read)
{
- struct buffer *buffer =
- XBUFFER (((struct Lisp_TS_Parser *) parser)->buffer);
+ struct buffer *buffer = XBUFFER (((struct Lisp_TS_Parser *) parser)->buffer);
ptrdiff_t visible_beg = ((struct Lisp_TS_Parser *) parser)->visible_beg;
ptrdiff_t visible_end = ((struct Lisp_TS_Parser *) parser)->visible_end;
ptrdiff_t byte_pos = byte_index + visible_beg;
make_ts_parser (Lisp_Object buffer, TSParser *parser,
TSTree *tree, Lisp_Object language_symbol)
{
- struct Lisp_TS_Parser *lisp_parser
- = ALLOCATE_PSEUDOVECTOR
- (struct Lisp_TS_Parser, buffer, PVEC_TS_PARSER);
+ struct Lisp_TS_Parser *lisp_parser =
+ ALLOCATE_PSEUDOVECTOR (struct Lisp_TS_Parser, buffer, PVEC_TS_PARSER);
lisp_parser->language_symbol = language_symbol;
lisp_parser->buffer = buffer;
Lisp_Object
make_ts_node (Lisp_Object parser, TSNode node)
{
- struct Lisp_TS_Node *lisp_node
- = ALLOCATE_PSEUDOVECTOR (struct Lisp_TS_Node, parser, PVEC_TS_NODE);
+ struct Lisp_TS_Node *lisp_node =
+ ALLOCATE_PSEUDOVECTOR (struct Lisp_TS_Node, parser, PVEC_TS_NODE);
lisp_node->parser = parser;
lisp_node->node = node;
lisp_node->timestamp = XTS_PARSER (parser)->timestamp;
make_ts_query (Lisp_Object query, Lisp_Object language)
{
TSQueryCursor *ts_cursor = ts_query_cursor_new ();
- struct Lisp_TS_Query *lisp_query
- = ALLOCATE_PSEUDOVECTOR (struct Lisp_TS_Query, source,
- PVEC_TS_COMPILED_QUERY);
+ struct Lisp_TS_Query *lisp_query =
+ ALLOCATE_PSEUDOVECTOR (struct Lisp_TS_Query, source,
+ PVEC_TS_COMPILED_QUERY);
lisp_query->language = language;
lisp_query->source = query;
void
ts_delete_parser (struct Lisp_TS_Parser *lisp_parser)
{
- ts_tree_delete(lisp_parser->tree);
- ts_parser_delete(lisp_parser->parser);
+ ts_tree_delete (lisp_parser->tree);
+ ts_parser_delete (lisp_parser->parser);
}
void
}
static Lisp_Object
-ts_compose_query_signal_data
-(uint32_t error_offset, TSQueryError error_type)
+ts_compose_query_signal_data (uint32_t error_offset, TSQueryError error_type)
{
- return list3 (build_string
- (ts_query_error_to_string (error_type)),
+ return list3 (build_string (ts_query_error_to_string (error_type)),
make_fixnum (error_offset + 1),
build_pure_c_string("Debug the query with `treesit-query-validate'"));
}
bound. If error occures, return NULL, and assign SIGNAL_SYMBOL and
SIGNAL_DATA accordingly. */
static TSQuery *
-ts_ensure_query_compiled
-(Lisp_Object query, Lisp_Object *signal_symbol, Lisp_Object *signal_data)
+ts_ensure_query_compiled (Lisp_Object query, Lisp_Object *signal_symbol,
+ Lisp_Object *signal_data)
{
/* If query is already compiled (not null), return that, otherwise
compile and return it. */
{
struct Lisp_TS_Parser *parser = XTS_PARSER (XCAR (tail));
if (EQ (parser->language_symbol, language))
- {
- return XCAR (tail);
- }
+ return XCAR (tail);
}
/* Load language. */
= make_ts_parser (Fcurrent_buffer (), parser, NULL, language);
/* Update parser-list. */
- BVAR (buf, ts_parser_list)
- = Fcons (lisp_parser, BVAR (buf, ts_parser_list));
+ BVAR (buf, ts_parser_list) = Fcons (lisp_parser, BVAR (buf, ts_parser_list));
return lisp_parser;
}
Lisp_Object buffer = XTS_PARSER (parser)->buffer;
struct buffer *buf = XBUFFER (buffer);
- BVAR (buf, ts_parser_list)
- = Fdelete (parser, BVAR (buf, ts_parser_list));
+ BVAR (buf, ts_parser_list) = Fdelete (parser, BVAR (buf, ts_parser_list));
XTS_PARSER (parser)->deleted = true;
return Qnil;
ptrdiff_t point_max = BUF_ZV (buffer);
EMACS_INT last_point = point_min;
- for (Lisp_Object tail = ranges;
- !NILP (tail); tail = XCDR (tail))
+ for (Lisp_Object tail = ranges; !NILP (tail); tail = XCDR (tail))
{
CHECK_CONS (tail);
Lisp_Object range = XCAR (tail);
EMACS_INT end = XFIXNUM (XCDR (range));
if (!(last_point <= beg && beg <= end && end <= point_max))
xsignal2 (Qtreesit_range_invalid,
- build_pure_c_string
- ("RANGE is either overlapping or out-of-order or out-of-range"),
+ build_pure_c_string ("RANGE is either overlapping or out-of-order or out-of-range"),
ranges);
last_point = end;
}
To do that we give tree-sitter a 0 length, the range is a
dummy. */
TSRange ts_range = {{0, 0}, {0, 0}, 0, 0};
- success = ts_parser_set_included_ranges
- (XTS_PARSER (parser)->parser, &ts_range , 0);
+ success = ts_parser_set_included_ranges (XTS_PARSER (parser)->parser,
+ &ts_range , 0);
}
else
{
/* Set ranges for PARSER. */
ptrdiff_t len = list_length (ranges);
+ /* FIXME: We should test the return value of malloc below. */
TSRange *ts_ranges = malloc (sizeof(TSRange) * len);
struct buffer *buffer = XBUFFER (XTS_PARSER (parser)->buffer);
- for (int idx=0; !NILP (ranges); idx++, ranges = XCDR (ranges))
+ for (int idx = 0; !NILP (ranges); idx++, ranges = XCDR (ranges))
{
Lisp_Object range = XCAR (ranges);
- EMACS_INT beg_byte = buf_charpos_to_bytepos
- (buffer, XFIXNUM (XCAR (range)));
- EMACS_INT end_byte = buf_charpos_to_bytepos
- (buffer, XFIXNUM (XCDR (range)));
+ EMACS_INT beg_byte = buf_charpos_to_bytepos (buffer,
+ XFIXNUM (XCAR (range)));
+ EMACS_INT end_byte = buf_charpos_to_bytepos (buffer,
+ XFIXNUM (XCDR (range)));
/* We don't care about start and end points, put in dummy
- value. */
+ values. */
TSRange rg = {{0,0}, {0,0},
(uint32_t) beg_byte - BUF_BEGV_BYTE (buffer),
(uint32_t) end_byte - BUF_BEGV_BYTE (buffer)};
ts_ranges[idx] = rg;
}
- success = ts_parser_set_included_ranges
- (XTS_PARSER (parser)->parser, ts_ranges, (uint32_t) len);
+ /* FIXME: 'len' below is ptrdiff_t, so can overflow a 32-bit
+ unsigned data type. */
+ success = ts_parser_set_included_ranges (XTS_PARSER (parser)->parser,
+ ts_ranges, (uint32_t) len);
/* Although XFIXNUM could signal, it should be impossible
because we have checked the input by ts_check_range_argument.
So there is no need for unwind-protect. */
if (!success)
xsignal2 (Qtreesit_range_invalid,
- build_pure_c_string
- ("Something went wrong when setting ranges"),
+ build_pure_c_string ("Something went wrong when setting ranges"),
ranges);
XTS_PARSER (parser)->need_reparse = true;
Streesit_parser_included_ranges,
1, 1, 0,
doc: /* Return the ranges set for PARSER.
-See `treesit-parser-set-ranges'. If no range is set, return
-nil. */)
+See `treesit-parser-set-ranges'. If no range is set, return nil. */)
(Lisp_Object parser)
{
ts_check_parser (parser);
ts_initialize ();
uint32_t len;
- const TSRange *ranges = ts_parser_included_ranges
- (XTS_PARSER (parser)->parser, &len);
+ const TSRange *ranges =
+ ts_parser_included_ranges (XTS_PARSER (parser)->parser, &len);
if (len == 0)
return Qnil;
struct buffer *buffer = XBUFFER (XTS_PARSER (parser)->buffer);
Lisp_Object list = Qnil;
- for (int idx=0; idx < len; idx++)
+ for (int idx = 0; idx < len; idx++)
{
TSRange range = ranges[idx];
uint32_t beg_byte = range.start_byte + BUF_BEGV_BYTE (buffer);
{
CHECK_TS_NODE (obj);
Lisp_Object lisp_parser = XTS_NODE (obj)->parser;
- if (XTS_NODE (obj)->timestamp !=
- XTS_PARSER (lisp_parser)->timestamp)
+ if (XTS_NODE (obj)->timestamp != XTS_PARSER (lisp_parser)->timestamp)
xsignal1 (Qtreesit_node_outdated, obj);
}
ts_initialize ();
TSNode ts_node = XTS_NODE (node)->node;
- ptrdiff_t visible_beg =
- XTS_PARSER (XTS_NODE (node)->parser)->visible_beg;
+ ptrdiff_t visible_beg = XTS_PARSER (XTS_NODE (node)->parser)->visible_beg;
uint32_t start_byte_offset = ts_node_start_byte (ts_node);
struct buffer *buffer =
XBUFFER (XTS_PARSER (XTS_NODE (node)->parser)->buffer);
- ptrdiff_t start_pos = buf_bytepos_to_charpos
- (buffer, start_byte_offset + visible_beg);
+ ptrdiff_t start_pos =
+ buf_bytepos_to_charpos (buffer, start_byte_offset + visible_beg);
return make_fixnum (start_pos);
}
ts_initialize ();
TSNode ts_node = XTS_NODE (node)->node;
- ptrdiff_t visible_beg =
- XTS_PARSER (XTS_NODE (node)->parser)->visible_beg;
+ ptrdiff_t visible_beg = XTS_PARSER (XTS_NODE (node)->parser)->visible_beg;
uint32_t end_byte_offset = ts_node_end_byte (ts_node);
struct buffer *buffer =
XBUFFER (XTS_PARSER (XTS_NODE (node)->parser)->buffer);
- ptrdiff_t end_pos = buf_bytepos_to_charpos
- (buffer, end_byte_offset + visible_beg);
+ ptrdiff_t end_pos =
+ buf_bytepos_to_charpos (buffer, end_byte_offset + visible_beg);
return make_fixnum (end_pos);
}
ts_check_node (node);
ts_check_positive_integer (n);
EMACS_INT idx = XFIXNUM (n);
- if (idx > UINT32_MAX) xsignal1 (Qargs_out_of_range, n);
+ if (idx > UINT32_MAX)
+ xsignal1 (Qargs_out_of_range, n);
ts_initialize ();
TSNode ts_node = XTS_NODE (node)->node;
Ftreesit_node_check, Streesit_node_check, 2, 2, 0,
doc: /* Return non-nil if NODE has PROPERTY, nil otherwise.
-PROPERTY could be 'named, 'missing, 'extra, 'has-changes, 'has-error.
+PROPERTY could be `named', `missing', `extra', `has-changes', or `has-error'.
Named nodes correspond to named rules in the language definition,
whereas "anonymous" nodes correspond to string literals in the
language definition.
language definition, but can appear anywhere.
A node "has changes" if the buffer changed since the node is
-created. (Don't forget the "s" at the end of 'has-changes.)
+created. (Don't forget the "s" at the end of `has-changes'.)
A node "has error" if itself is a syntax error or contains any syntax
errors. */)
else if (EQ (property, Qhas_changes))
result = ts_node_has_changes (ts_node);
else
- signal_error ("Expecting 'named, 'missing, 'extra, 'has-changes or 'has-error, got",
+ signal_error ("Expecting `named', `missing', `extra', `has-changes' or `has-error', but got",
property);
return result ? Qt : Qnil;
}
If NODE is nil, return nil. */)
(Lisp_Object node, Lisp_Object n)
{
- if (NILP (node)) return Qnil;
+ if (NILP (node))
+ return Qnil;
ts_check_node (node);
ts_check_positive_integer (n);
EMACS_INT idx = XFIXNUM (n);
- if (idx > UINT32_MAX) xsignal1 (Qargs_out_of_range, n);
+ if (idx > UINT32_MAX)
+ xsignal1 (Qargs_out_of_range, n);
ts_initialize ();
TSNode ts_node = XTS_NODE (node)->node;
Streesit_node_child_count, 1, 2, 0,
doc: /* Return the number of children of NODE.
-If NAMED is non-nil, count named child only. NAMED defaults to
+If NAMED is non-nil, count named children only. NAMED defaults to
nil. If NODE is nil, return nil. */)
(Lisp_Object node, Lisp_Object named)
{
- if (NILP (node)) return Qnil;
+ if (NILP (node))
+ return Qnil;
ts_check_node (node);
ts_initialize ();
Return nil if there isn't any. If NODE is nil, return nil. */)
(Lisp_Object node, Lisp_Object field_name)
{
- if (NILP (node)) return Qnil;
+ if (NILP (node))
+ return Qnil;
ts_check_node (node);
CHECK_STRING (field_name);
ts_initialize ();
TSNode child
= ts_node_child_by_field_name (ts_node, name_str, strlen (name_str));
- if (ts_node_is_null(child))
+ if (ts_node_is_null (child))
return Qnil;
return make_ts_node(XTS_NODE (node)->parser, child);
doc: /* Return the next sibling of NODE.
Return nil if there isn't any. If NAMED is non-nil, look for named
-child only. NAMED defaults to nil. If NODE is nil, return nil. */)
+siblings only. NAMED defaults to nil. If NODE is nil, return nil. */)
(Lisp_Object node, Lisp_Object named)
{
if (NILP (node)) return Qnil;
doc: /* Return the previous sibling of NODE.
Return nil if there isn't any. If NAMED is non-nil, look for named
-child only. NAMED defaults to nil. If NODE is nil, return nil. */)
+siblings only. NAMED defaults to nil. If NODE is nil, return nil. */)
(Lisp_Object node, Lisp_Object named)
{
if (NILP (node)) return Qnil;
Specifically, return the first child that extends beyond POS. POS is
a position in the buffer. Return nil if there isn't any. If NAMED is
-non-nil, look for named child only. NAMED defaults to nil. Note that
+non-nil, look for named children only. NAMED defaults to nil. Note that
this function returns an immediate child, not the smallest
(grand)child. If NODE is nil, return nil. */)
(Lisp_Object node, Lisp_Object pos, Lisp_Object named)
{
- if (NILP (node)) return Qnil;
+ if (NILP (node))
+ return Qnil;
ts_check_node (node);
ts_check_positive_integer (pos);
- struct buffer *buf =
- XBUFFER (XTS_PARSER (XTS_NODE (node)->parser)->buffer);
- ptrdiff_t visible_beg =
- XTS_PARSER (XTS_NODE (node)->parser)->visible_beg;
+ struct buffer *buf = XBUFFER (XTS_PARSER (XTS_NODE (node)->parser)->buffer);
+ ptrdiff_t visible_beg = XTS_PARSER (XTS_NODE (node)->parser)->visible_beg;
ptrdiff_t byte_pos = buf_charpos_to_bytepos (buf, XFIXNUM (pos));
if (byte_pos < BUF_BEGV_BYTE (buf) || byte_pos > BUF_ZV_BYTE (buf))
TSNode ts_node = XTS_NODE (node)->node;
TSNode child;
if (NILP (named))
- child = ts_node_first_child_for_byte
- (ts_node, byte_pos - visible_beg);
+ child = ts_node_first_child_for_byte (ts_node, byte_pos - visible_beg);
else
- child = ts_node_first_named_child_for_byte
- (ts_node, byte_pos - visible_beg);
+ child = ts_node_first_named_child_for_byte (ts_node,
+ byte_pos - visible_beg);
if (ts_node_is_null (child))
return Qnil;
CHECK_INTEGER (beg);
CHECK_INTEGER (end);
- struct buffer *buf =
- XBUFFER (XTS_PARSER (XTS_NODE (node)->parser)->buffer);
- ptrdiff_t visible_beg =
- XTS_PARSER (XTS_NODE (node)->parser)->visible_beg;
+ struct buffer *buf = XBUFFER (XTS_PARSER (XTS_NODE (node)->parser)->buffer);
+ ptrdiff_t visible_beg = XTS_PARSER (XTS_NODE (node)->parser)->visible_beg;
ptrdiff_t byte_beg = buf_charpos_to_bytepos (buf, XFIXNUM (beg));
ptrdiff_t byte_end = buf_charpos_to_bytepos (buf, XFIXNUM (end));
TSNode ts_node = XTS_NODE (node)->node;
TSNode child;
if (NILP (named))
- child = ts_node_descendant_for_byte_range
- (ts_node, byte_beg - visible_beg , byte_end - visible_beg);
+ child = ts_node_descendant_for_byte_range (ts_node, byte_beg - visible_beg,
+ byte_end - visible_beg);
else
- child = ts_node_named_descendant_for_byte_range
- (ts_node, byte_beg - visible_beg, byte_end - visible_beg);
+ child = ts_node_named_descendant_for_byte_range (ts_node,
+ byte_beg - visible_beg,
+ byte_end - visible_beg);
if (ts_node_is_null (child))
return Qnil;
build_pure_c_string (VECTORP (pattern) ? "]" : ")");
if (VECTORP (pattern) || CONSP (pattern))
return concat3 (opening_delimeter,
- Fmapconcat (intern_c_string
- ("treesit-pattern-expand"),
+ Fmapconcat (intern_c_string ("treesit-pattern-expand"),
pattern,
build_pure_c_string (" ")),
closing_delimiter);
ts_query_predicates_for_pattern (query, pattern_index, &len);
Lisp_Object result = Qnil;
Lisp_Object predicate = Qnil;
- for (int idx=0; idx < len; idx++)
+ for (int idx = 0; idx < len; idx++)
{
TSQueryPredicateStep step = predicate_list[idx];
switch (step.type)
case TSQueryPredicateStepTypeCapture:
{
uint32_t str_len;
- const char *str = ts_query_capture_name_for_id
- (query, step.value_id, &str_len);
+ const char *str = ts_query_capture_name_for_id (query,
+ step.value_id,
+ &str_len);
predicate = Fcons (intern_c_string_1 (str, str_len),
predicate);
break;
case TSQueryPredicateStepTypeString:
{
uint32_t str_len;
- const char *str = ts_query_string_value_for_id
- (query, step.value_id, &str_len);
+ const char *str = ts_query_string_value_for_id (query,
+ step.value_id,
+ &str_len);
predicate = Fcons (make_string (str, str_len), predicate);
break;
}
/* Translate a capture NAME (symbol) to the text of the captured node.
Signals treesit-query-error if such node is not captured. */
static Lisp_Object
-ts_predicate_capture_name_to_text
-(Lisp_Object name, struct capture_range captures)
+ts_predicate_capture_name_to_text (Lisp_Object name,
+ struct capture_range captures)
{
Lisp_Object node = Qnil;
- for (Lisp_Object tail = captures.start;
- !EQ (tail, captures.end); tail = XCDR (tail))
+ for (Lisp_Object tail = captures.start; !EQ (tail, captures.end);
+ tail = XCDR (tail))
{
if (EQ (XCAR (XCAR (tail)), name))
{
name, build_pure_c_string ("A predicate can only refer to captured nodes in the same pattern"));
struct buffer *old_buffer = current_buffer;
- set_buffer_internal
- (XBUFFER (XTS_PARSER (XTS_NODE (node)->parser)->buffer));
- Lisp_Object text = Fbuffer_substring
- (Ftreesit_node_start (node), Ftreesit_node_end (node));
+ set_buffer_internal (XBUFFER (XTS_PARSER (XTS_NODE (node)->parser)->buffer));
+ Lisp_Object text = Fbuffer_substring (Ftreesit_node_start (node),
+ Ftreesit_node_end (node));
set_buffer_internal (old_buffer);
return text;
}
The capture name evaluates to the text its captured node spans in
the buffer. */
static bool
-ts_predicate_equal
-(Lisp_Object args, struct capture_range captures)
+ts_predicate_equal (Lisp_Object args, struct capture_range captures)
{
if (XFIXNUM (Flength (args)) != 2)
- xsignal2 (Qtreesit_query_error, build_pure_c_string ("Predicate `equal' requires two arguments but only given"), Flength (args));
+ xsignal2 (Qtreesit_query_error,
+ build_pure_c_string ("Predicate `equal' requires two arguments but only given"),
+ Flength (args));
Lisp_Object arg1 = XCAR (args);
Lisp_Object arg2 = XCAR (XCDR (args));
- Lisp_Object text1 = STRINGP (arg1) ? arg1 :
- ts_predicate_capture_name_to_text (arg1, captures);
- Lisp_Object text2 = STRINGP (arg2) ? arg2 :
- ts_predicate_capture_name_to_text (arg2, captures);
+ Lisp_Object text1 =
+ STRINGP (arg1) ? arg1 : ts_predicate_capture_name_to_text (arg1, captures);
+ Lisp_Object text2 =
+ STRINGP (arg2) ? arg2 : ts_predicate_capture_name_to_text (arg2, captures);
- if (NILP (Fstring_equal (text1, text2)))
- return false;
- else
- return true;
+ return !NILP (Fstring_equal (text1, text2));
}
/* Handles predicate (#match "regexp" @node). Return true if "regexp"
(Lisp_Object args, struct capture_range captures)
{
if (XFIXNUM (Flength (args)) != 2)
- xsignal2 (Qtreesit_query_error, build_pure_c_string ("Predicate `equal' requires two arguments but only given"), Flength (args));
+ xsignal2 (Qtreesit_query_error,
+ build_pure_c_string ("Predicate `equal' requires two arguments but only given"),
+ Flength (args));
Lisp_Object regexp = XCAR (args);
Lisp_Object capture_name = XCAR (XCDR (args));
- Lisp_Object text = ts_predicate_capture_name_to_text
- (capture_name, captures);
+ Lisp_Object text = ts_predicate_capture_name_to_text (capture_name, captures);
/* It's probably common to get the argument order backwards. Catch
this mistake early and show helpful explanation, because Emacs
loves you. (We put the regexp first because that's what
string-match does.) */
if (!STRINGP (regexp))
- xsignal1 (Qtreesit_query_error, build_pure_c_string ("The first argument to `match' should be a regexp string, not a capture name"));
+ xsignal1 (Qtreesit_query_error,
+ build_pure_c_string ("The first argument to `match' should be a regexp string, not a capture name"));
if (!SYMBOLP (capture_name))
- xsignal1 (Qtreesit_query_error, build_pure_c_string ("The second argument to `match' should be a capture name, not a string"));
+ xsignal1 (Qtreesit_query_error,
+ build_pure_c_string ("The second argument to `match' should be a capture name, not a string"));
if (fast_string_match (regexp, text) >= 0)
return true;
/* If all predicates in PREDICATES passes, return true; otherwise
return false. */
static bool
-ts_eval_predicates
-(struct capture_range captures, Lisp_Object predicates)
+ts_eval_predicates (struct capture_range captures, Lisp_Object predicates)
{
bool pass = true;
/* Evaluate each predicates. */
Lisp_Object args = XCDR (predicate);
if (!NILP (Fstring_equal (fn, build_pure_c_string("equal"))))
pass = ts_predicate_equal (args, captures);
- else if (!NILP (Fstring_equal
- (fn, build_pure_c_string("match"))))
+ else if (!NILP (Fstring_equal (fn, build_pure_c_string("match"))))
pass = ts_predicate_match (args, captures);
else
xsignal3 (Qtreesit_query_error,
/* Maybe actually compile. */
if (NILP (eager))
- return lisp_query;
+ return lisp_query;
else
{
Lisp_Object signal_symbol = Qnil;
Lisp_Object signal_data = Qnil;
- TSQuery *ts_query = ts_ensure_query_compiled
- (lisp_query, &signal_symbol, &signal_data);
+ TSQuery *ts_query = ts_ensure_query_compiled (lisp_query, &signal_symbol,
+ &signal_data);
if (ts_query == NULL)
xsignal (signal_symbol, signal_data);
}
else
xsignal2 (Qwrong_type_argument,
- list4 (Qor, Qtreesit_node_p,
- Qtreesit_parser_p, Qsymbolp),
+ list4 (Qor, Qtreesit_node_p, Qtreesit_parser_p, Qsymbolp),
node);
ts_initialize ();
Lisp_Object lisp_parser = XTS_NODE (lisp_node)->parser;
ptrdiff_t visible_beg =
XTS_PARSER (XTS_NODE (lisp_node)->parser)->visible_beg;
- const TSLanguage *lang = ts_parser_language
- (XTS_PARSER (lisp_parser)->parser);
+ const TSLanguage *lang =
+ ts_parser_language (XTS_PARSER (lisp_parser)->parser);
/* Initialize query objects. At the end of this block, we should
have a working TSQuery and a TSQueryCursor. */
{
Lisp_Object signal_symbol = Qnil;
Lisp_Object signal_data = Qnil;
- ts_query = ts_ensure_query_compiled
- (query, &signal_symbol, &signal_data);
+ ts_query = ts_ensure_query_compiled (query, &signal_symbol, &signal_data);
cursor = XTS_COMPILED_QUERY (query)->cursor;
/* We don't need to free ts_query and cursor because they
are stored in a lisp object, which is tracked by gc. */
needs_to_free_query_and_cursor = false;
if (ts_query == NULL)
- {
- xsignal (signal_symbol, signal_data);
- }
+ xsignal (signal_symbol, signal_data);
}
else
{
ts_query = ts_query_new (lang, query_string, strlen (query_string),
&error_offset, &error_type);
if (ts_query == NULL)
- {
- xsignal (Qtreesit_query_error, ts_compose_query_signal_data
- (error_offset, error_type));
- }
+ xsignal (Qtreesit_query_error,
+ ts_compose_query_signal_data (error_offset, error_type));
cursor = ts_query_cursor_new ();
needs_to_free_query_and_cursor = true;
}
{
EMACS_INT beg_byte = XFIXNUM (beg);
EMACS_INT end_byte = XFIXNUM (end);
- ts_query_cursor_set_byte_range
- (cursor, (uint32_t) beg_byte - visible_beg,
- (uint32_t) end_byte - visible_beg);
+ /* FIXME: EMACS_INT values could overflow uint32_t. */
+ ts_query_cursor_set_byte_range (cursor, (uint32_t) beg_byte - visible_beg,
+ (uint32_t) end_byte - visible_beg);
}
/* Execute query. */
prev_result = result;
/* Get captured nodes. */
const TSQueryCapture *captures = match.captures;
- for (int idx=0; idx < match.capture_count; idx++)
+ for (int idx = 0; idx < match.capture_count; idx++)
{
uint32_t capture_name_len;
TSQueryCapture capture = captures[idx];
- Lisp_Object captured_node =
- make_ts_node(lisp_parser, capture.node);
+ Lisp_Object captured_node = make_ts_node(lisp_parser, capture.node);
Lisp_Object cap;
if (NILP (node_only))
{
- const char *capture_name = ts_query_capture_name_for_id
- (ts_query, capture.index, &capture_name_len);
- cap =
- Fcons (intern_c_string_1 (capture_name, capture_name_len),
- captured_node);
+ const char *capture_name =
+ ts_query_capture_name_for_id (ts_query, capture.index,
+ &capture_name_len);
+ cap = Fcons (intern_c_string_1 (capture_name, capture_name_len),
+ captured_node);
}
else
- {
- cap = captured_node;
- }
+ cap = captured_node;
+
result = Fcons (cap, result);
}
/* Get predicates. */
/* Return true if NODE matches PRED. PRED can be a string or a
function. This function doesn't check for PRED's type. */
static bool
-ts_traverse_match_predicate
-(TSNode node, Lisp_Object pred, Lisp_Object parser)
+ts_traverse_match_predicate (TSNode node, Lisp_Object pred, Lisp_Object parser)
{
if (STRINGP (pred))
{
const char *type = ts_node_type (node);
- return (fast_c_string_match_ignore_case
- (pred, type, strlen (type)) >= 0);
+ return fast_c_string_match_ignore_case (pred, type, strlen (type)) >= 0;
}
else
{
is true, only traverse named nodes, if false, all nodes. If
SKIP_ROOT is true, don't match ROOT. */
static bool
-ts_search_dfs
-(TSNode *root, Lisp_Object pred, Lisp_Object parser,
- bool named, bool forward, ptrdiff_t limit, bool no_limit,
- bool skip_root)
+ts_search_dfs (TSNode *root, Lisp_Object pred, Lisp_Object parser,
+ bool named, bool forward, ptrdiff_t limit, bool no_limit,
+ bool skip_root)
{
/* TSTreeCursor doesn't allow us to move backward, so we can't use
it. We could use limit == -1 to indicate no_limit == true, but
return false;
else
{
- int count = named ?
- ts_node_named_child_count( node)
- : ts_node_child_count (node);
- for (int offset=0; offset < count; offset++)
+ int count =
+ named ? ts_node_named_child_count( node) : ts_node_child_count (node);
+ for (int offset = 0; offset < count; offset++)
{
- uint32_t idx = forward ? offset
- : count - offset - 1;
+ uint32_t idx = forward ? offset : count - offset - 1;
TSNode child = ts_node_child (node, idx);
if (!ts_node_is_null (child)
sibling and parents. If SKIP_START is true, don'tt match
START. */
static bool
-ts_search_forward
-(TSNode *start, Lisp_Object pred, Lisp_Object parser,
- bool named, bool forward, bool up_only, bool skip_start)
+ts_search_forward (TSNode *start, Lisp_Object pred, Lisp_Object parser,
+ bool named, bool forward, bool up_only, bool skip_start)
{
TSNode node = *start;
- if (!up_only && ts_search_dfs
- (start, pred, parser, named, forward, 0, true, skip_start))
+ if (!up_only
+ && ts_search_dfs (start, pred, parser, named, forward, 0, true,
+ skip_start))
return true;
TSNode next = ts_traverse_sibling_helper (node, forward, named);
}
next = ts_traverse_sibling_helper (node, forward, named);
}
- if (ts_search_forward
- (&next, pred, parser, named, forward, up_only, false))
+ if (ts_search_forward (&next, pred, parser, named, forward, up_only, false))
{
*start = next;
return true;
TSNode ts_node = XTS_NODE (node)->node;
Lisp_Object parser = XTS_NODE (node)->parser;
- if (ts_search_dfs
- (&ts_node, predicate, parser, NILP (all),
- NILP (backward), the_limit, no_limit, false))
- {
- return make_ts_node (parser, ts_node);
- }
+ if (ts_search_dfs (&ts_node, predicate, parser, NILP (all),
+ NILP (backward), the_limit, no_limit, false))
+ return make_ts_node (parser, ts_node);
else
return Qnil;
}
TSNode ts_start = XTS_NODE (start)->node;
Lisp_Object parser = XTS_NODE (start)->parser;
- if (ts_search_forward
- (&ts_start, predicate, parser, NILP (all),
- NILP (backward), !NILP (up), true))
- {
- return make_ts_node (parser, ts_start);
- }
+ if (ts_search_forward (&ts_start, predicate, parser, NILP (all),
+ NILP (backward), !NILP (up), true))
+ return make_ts_node (parser, ts_start);
else
return Qnil;
}
Note that the top-level children list is reversed, because
reasons. */
static void
-ts_build_sparse_tree
-(TSTreeCursor *cursor, Lisp_Object parent, Lisp_Object pred,
- Lisp_Object process_fn, ptrdiff_t limit,
- bool no_limit, Lisp_Object parser)
+ts_build_sparse_tree (TSTreeCursor *cursor, Lisp_Object parent,
+ Lisp_Object pred, Lisp_Object process_fn, ptrdiff_t limit,
+ bool no_limit, Lisp_Object parser)
{
TSNode node = ts_tree_cursor_current_node (cursor);
children list. */
Lisp_Object lisp_node = make_ts_node (parser, node);
if (!NILP (process_fn))
- {
- lisp_node = CALLN (Ffuncall, process_fn, lisp_node);
- }
+ lisp_node = CALLN (Ffuncall, process_fn, lisp_node);
+
Lisp_Object this = Fcons (lisp_node, Qnil);
Fsetcdr (parent, Fcons (this, Fcdr (parent)));
/* Now for children nodes, this is the new parent. */
/* Make sure not to use node after the recursive funcall.
Then C compilers should be smart enough not to copy NODE
to stack. */
- ts_build_sparse_tree
- (cursor, parent, pred, process_fn,
- limit - 1, no_limit, parser);
+ ts_build_sparse_tree (cursor, parent, pred, process_fn,
+ limit - 1, no_limit, parser);
}
while (ts_tree_cursor_goto_next_sibling (cursor));
/* Don't forget to come back to this node. */
TSTreeCursor cursor = ts_tree_cursor_new (XTS_NODE (root)->node);
Lisp_Object parser = XTS_NODE (root)->parser;
Lisp_Object parent = Fcons (Qnil, Qnil);
- ts_build_sparse_tree
- (&cursor, parent, predicate, process_fn,
- the_limit, no_limit, parser);
+ ts_build_sparse_tree (&cursor, parent, predicate, process_fn,
+ the_limit, no_limit, parser);
Fsetcdr (parent, Fnreverse (Fcdr (parent)));
if (NILP (Fcdr (parent)))
return Qnil;