always trimming fat
This commit is contained in:
parent
bf669ee8a7
commit
74d642f297
8 changed files with 808 additions and 1450 deletions
659
parser/src/api.h
659
parser/src/api.h
|
|
@ -1,19 +1,16 @@
|
|||
#ifndef TREE_SITTER_API_H_
|
||||
#define TREE_SITTER_API_H_
|
||||
|
||||
#ifndef TREE_SITTER_HIDE_SYMBOLS
|
||||
#if defined(__GNUC__) || defined(__clang__)
|
||||
#pragma GCC visibility push(default)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define ERROR_STATE 0
|
||||
#define ERROR_COST_PER_RECOVERY 500
|
||||
#define ERROR_COST_PER_MISSING_TREE 110
|
||||
#define ERROR_COST_PER_SKIPPED_TREE 100
|
||||
#define ERROR_COST_PER_SKIPPED_LINE 30
|
||||
#define ERROR_COST_PER_SKIPPED_CHAR 1
|
||||
|
||||
/****************************/
|
||||
/* Section - ABI Versioning */
|
||||
|
|
@ -48,46 +45,54 @@ typedef struct TSQuery TSQuery;
|
|||
typedef struct TSQueryCursor TSQueryCursor;
|
||||
typedef struct TSLookaheadIterator TSLookaheadIterator;
|
||||
|
||||
typedef enum TSInputEncoding {
|
||||
typedef enum TSInputEncoding
|
||||
{
|
||||
TSInputEncodingUTF8,
|
||||
TSInputEncodingUTF16,
|
||||
} TSInputEncoding;
|
||||
|
||||
typedef enum TSSymbolType {
|
||||
typedef enum TSSymbolType
|
||||
{
|
||||
TSSymbolTypeRegular,
|
||||
TSSymbolTypeAnonymous,
|
||||
TSSymbolTypeAuxiliary,
|
||||
} TSSymbolType;
|
||||
|
||||
typedef struct TSPoint {
|
||||
typedef struct TSPoint
|
||||
{
|
||||
uint32_t row;
|
||||
uint32_t column;
|
||||
} TSPoint;
|
||||
|
||||
typedef struct TSRange {
|
||||
typedef struct TSRange
|
||||
{
|
||||
TSPoint start_point;
|
||||
TSPoint end_point;
|
||||
uint32_t start_byte;
|
||||
uint32_t end_byte;
|
||||
} TSRange;
|
||||
|
||||
typedef struct TSInput {
|
||||
typedef struct TSInput
|
||||
{
|
||||
void *payload;
|
||||
const char *(*read)(void *payload, uint32_t byte_index, TSPoint position, uint32_t *bytes_read);
|
||||
TSInputEncoding encoding;
|
||||
} TSInput;
|
||||
|
||||
typedef enum TSLogType {
|
||||
typedef enum TSLogType
|
||||
{
|
||||
TSLogTypeParse,
|
||||
TSLogTypeLex,
|
||||
} TSLogType;
|
||||
|
||||
typedef struct TSLogger {
|
||||
typedef struct TSLogger
|
||||
{
|
||||
void *payload;
|
||||
void (*log)(void *payload, TSLogType log_type, const char *buffer);
|
||||
} TSLogger;
|
||||
|
||||
typedef struct TSInputEdit {
|
||||
typedef struct TSInputEdit
|
||||
{
|
||||
uint32_t start_byte;
|
||||
uint32_t old_end_byte;
|
||||
uint32_t new_end_byte;
|
||||
|
|
@ -96,24 +101,28 @@ typedef struct TSInputEdit {
|
|||
TSPoint new_end_point;
|
||||
} TSInputEdit;
|
||||
|
||||
typedef struct TSNode {
|
||||
typedef struct TSNode
|
||||
{
|
||||
uint32_t context[4];
|
||||
const void *id;
|
||||
const TSTree *tree;
|
||||
} TSNode;
|
||||
|
||||
typedef struct TSTreeCursor {
|
||||
typedef struct TSTreeCursor
|
||||
{
|
||||
const void *tree;
|
||||
const void *id;
|
||||
uint32_t context[3];
|
||||
} TSTreeCursor;
|
||||
|
||||
typedef struct TSQueryCapture {
|
||||
typedef struct TSQueryCapture
|
||||
{
|
||||
TSNode node;
|
||||
uint32_t index;
|
||||
} TSQueryCapture;
|
||||
|
||||
typedef enum TSQuantifier {
|
||||
typedef enum TSQuantifier
|
||||
{
|
||||
TSQuantifierZero = 0, // must match the array initialization value
|
||||
TSQuantifierZeroOrOne,
|
||||
TSQuantifierZeroOrMore,
|
||||
|
|
@ -121,25 +130,29 @@ typedef enum TSQuantifier {
|
|||
TSQuantifierOneOrMore,
|
||||
} TSQuantifier;
|
||||
|
||||
typedef struct TSQueryMatch {
|
||||
typedef struct TSQueryMatch
|
||||
{
|
||||
uint32_t id;
|
||||
uint16_t pattern_index;
|
||||
uint16_t capture_count;
|
||||
const TSQueryCapture *captures;
|
||||
} TSQueryMatch;
|
||||
|
||||
typedef enum TSQueryPredicateStepType {
|
||||
typedef enum TSQueryPredicateStepType
|
||||
{
|
||||
TSQueryPredicateStepTypeDone,
|
||||
TSQueryPredicateStepTypeCapture,
|
||||
TSQueryPredicateStepTypeString,
|
||||
} TSQueryPredicateStepType;
|
||||
|
||||
typedef struct TSQueryPredicateStep {
|
||||
typedef struct TSQueryPredicateStep
|
||||
{
|
||||
TSQueryPredicateStepType type;
|
||||
uint32_t value_id;
|
||||
} TSQueryPredicateStep;
|
||||
|
||||
typedef enum TSQueryError {
|
||||
typedef enum TSQueryError
|
||||
{
|
||||
TSQueryErrorNone = 0,
|
||||
TSQueryErrorSyntax,
|
||||
TSQueryErrorNodeType,
|
||||
|
|
@ -202,11 +215,7 @@ bool ts_parser_set_language(TSParser *self, const TSLanguage *language);
|
|||
* will not be assigned, and this function will return `false`. On success,
|
||||
* this function returns `true`
|
||||
*/
|
||||
bool ts_parser_set_included_ranges(
|
||||
TSParser *self,
|
||||
const TSRange *ranges,
|
||||
uint32_t count
|
||||
);
|
||||
bool ts_parser_set_included_ranges(TSParser *self, const TSRange *ranges, uint32_t count);
|
||||
|
||||
/**
|
||||
* Get the ranges of text that the parser will include when parsing.
|
||||
|
|
@ -215,10 +224,7 @@ bool ts_parser_set_included_ranges(
|
|||
* or write to it. The length of the array will be written to the given
|
||||
* `count` pointer.
|
||||
*/
|
||||
const TSRange *ts_parser_included_ranges(
|
||||
const TSParser *self,
|
||||
uint32_t *count
|
||||
);
|
||||
const TSRange *ts_parser_included_ranges(const TSParser *self, uint32_t *count);
|
||||
|
||||
/**
|
||||
* Use the parser to parse some source code and create a syntax tree.
|
||||
|
|
@ -263,11 +269,7 @@ const TSRange *ts_parser_included_ranges(
|
|||
* [`encoding`]: TSInput::encoding
|
||||
* [`bytes_read`]: TSInput::read
|
||||
*/
|
||||
TSTree *ts_parser_parse(
|
||||
TSParser *self,
|
||||
const TSTree *old_tree,
|
||||
TSInput input
|
||||
);
|
||||
TSTree *ts_parser_parse(TSParser *self, const TSTree *old_tree, TSInput input);
|
||||
|
||||
/**
|
||||
* Use the parser to parse some source code stored in one contiguous buffer.
|
||||
|
|
@ -275,12 +277,7 @@ TSTree *ts_parser_parse(
|
|||
* above. The second two parameters indicate the location of the buffer and its
|
||||
* length in bytes.
|
||||
*/
|
||||
TSTree *ts_parser_parse_string(
|
||||
TSParser *self,
|
||||
const TSTree *old_tree,
|
||||
const char *string,
|
||||
uint32_t length
|
||||
);
|
||||
TSTree *ts_parser_parse_string(TSParser *self, const TSTree *old_tree, const char *string, uint32_t length);
|
||||
|
||||
/**
|
||||
* Use the parser to parse some source code stored in one contiguous buffer with
|
||||
|
|
@ -288,13 +285,8 @@ TSTree *ts_parser_parse_string(
|
|||
* [`ts_parser_parse_string`] method above. The final parameter indicates whether
|
||||
* the text is encoded as UTF8 or UTF16.
|
||||
*/
|
||||
TSTree *ts_parser_parse_string_encoding(
|
||||
TSParser *self,
|
||||
const TSTree *old_tree,
|
||||
const char *string,
|
||||
uint32_t length,
|
||||
TSInputEncoding encoding
|
||||
);
|
||||
TSTree *ts_parser_parse_string_encoding(TSParser *self, const TSTree *old_tree, const char *string, uint32_t length,
|
||||
TSInputEncoding encoding);
|
||||
|
||||
/**
|
||||
* Instruct the parser to start the next parse from the beginning.
|
||||
|
|
@ -383,11 +375,7 @@ TSNode ts_tree_root_node(const TSTree *self);
|
|||
* Get the root node of the syntax tree, but with its position
|
||||
* shifted forward by the given offset.
|
||||
*/
|
||||
TSNode ts_tree_root_node_with_offset(
|
||||
const TSTree *self,
|
||||
uint32_t offset_bytes,
|
||||
TSPoint offset_extent
|
||||
);
|
||||
TSNode ts_tree_root_node_with_offset(const TSTree *self, uint32_t offset_bytes, TSPoint offset_extent);
|
||||
|
||||
/**
|
||||
* Get the language that was used to parse the syntax tree.
|
||||
|
|
@ -424,11 +412,7 @@ void ts_tree_edit(TSTree *self, const TSInputEdit *edit);
|
|||
* for freeing it using `free`. The length of the array will be written to the
|
||||
* given `length` pointer.
|
||||
*/
|
||||
TSRange *ts_tree_get_changed_ranges(
|
||||
const TSTree *old_tree,
|
||||
const TSTree *new_tree,
|
||||
uint32_t *length
|
||||
);
|
||||
TSRange *ts_tree_get_changed_ranges(const TSTree *old_tree, const TSTree *new_tree, uint32_t *length);
|
||||
|
||||
/**
|
||||
* Write a DOT graph describing the syntax tree to the given file.
|
||||
|
|
@ -533,17 +517,17 @@ bool ts_node_has_error(TSNode self);
|
|||
|
||||
/**
|
||||
* Check if the node is a syntax error.
|
||||
*/
|
||||
*/
|
||||
bool ts_node_is_error(TSNode self);
|
||||
|
||||
/**
|
||||
* Get this node's parse state.
|
||||
*/
|
||||
*/
|
||||
TSStateId ts_node_parse_state(TSNode self);
|
||||
|
||||
/**
|
||||
* Get the parse state after this node.
|
||||
*/
|
||||
*/
|
||||
TSStateId ts_node_next_parse_state(TSNode self);
|
||||
|
||||
/**
|
||||
|
|
@ -592,11 +576,7 @@ uint32_t ts_node_named_child_count(TSNode self);
|
|||
/**
|
||||
* Get the node's child with the given field name.
|
||||
*/
|
||||
TSNode ts_node_child_by_field_name(
|
||||
TSNode self,
|
||||
const char *name,
|
||||
uint32_t name_length
|
||||
);
|
||||
TSNode ts_node_child_by_field_name(TSNode self, const char *name, uint32_t name_length);
|
||||
|
||||
/**
|
||||
* Get the node's child with the given numerical field id.
|
||||
|
|
@ -663,360 +643,8 @@ void ts_node_edit(TSNode *self, const TSInputEdit *edit);
|
|||
*/
|
||||
bool ts_node_eq(TSNode self, TSNode other);
|
||||
|
||||
/************************/
|
||||
/* Section - TreeCursor */
|
||||
/************************/
|
||||
|
||||
/**
|
||||
* Create a new tree cursor starting from the given node.
|
||||
*
|
||||
* A tree cursor allows you to walk a syntax tree more efficiently than is
|
||||
* possible using the [`TSNode`] functions. It is a mutable object that is always
|
||||
* on a certain syntax node, and can be moved imperatively to different nodes.
|
||||
*/
|
||||
TSTreeCursor ts_tree_cursor_new(TSNode node);
|
||||
|
||||
/**
|
||||
* Delete a tree cursor, freeing all of the memory that it used.
|
||||
*/
|
||||
void ts_tree_cursor_delete(TSTreeCursor *self);
|
||||
|
||||
/**
|
||||
* Re-initialize a tree cursor to start at a different node.
|
||||
*/
|
||||
void ts_tree_cursor_reset(TSTreeCursor *self, TSNode node);
|
||||
|
||||
/**
|
||||
* Re-initialize a tree cursor to the same position as another cursor.
|
||||
*
|
||||
* Unlike [`ts_tree_cursor_reset`], this will not lose parent information and
|
||||
* allows reusing already created cursors.
|
||||
*/
|
||||
void ts_tree_cursor_reset_to(TSTreeCursor *dst, const TSTreeCursor *src);
|
||||
|
||||
/**
|
||||
* Get the tree cursor's current node.
|
||||
*/
|
||||
TSNode ts_tree_cursor_current_node(const TSTreeCursor *self);
|
||||
|
||||
/**
|
||||
* Get the field name of the tree cursor's current node.
|
||||
*
|
||||
* This returns `NULL` if the current node doesn't have a field.
|
||||
* See also [`ts_node_child_by_field_name`].
|
||||
*/
|
||||
const char *ts_tree_cursor_current_field_name(const TSTreeCursor *self);
|
||||
|
||||
/**
|
||||
* Get the field id of the tree cursor's current node.
|
||||
*
|
||||
* This returns zero if the current node doesn't have a field.
|
||||
* See also [`ts_node_child_by_field_id`], [`ts_language_field_id_for_name`].
|
||||
*/
|
||||
TSFieldId ts_tree_cursor_current_field_id(const TSTreeCursor *self);
|
||||
|
||||
/**
|
||||
* Move the cursor to the parent of its current node.
|
||||
*
|
||||
* This returns `true` if the cursor successfully moved, and returns `false`
|
||||
* if there was no parent node (the cursor was already on the root node).
|
||||
*/
|
||||
bool ts_tree_cursor_goto_parent(TSTreeCursor *self);
|
||||
|
||||
/**
|
||||
* Move the cursor to the next sibling of its current node.
|
||||
*
|
||||
* This returns `true` if the cursor successfully moved, and returns `false`
|
||||
* if there was no next sibling node.
|
||||
*/
|
||||
bool ts_tree_cursor_goto_next_sibling(TSTreeCursor *self);
|
||||
|
||||
/**
|
||||
* Move the cursor to the previous sibling of its current node.
|
||||
*
|
||||
* This returns `true` if the cursor successfully moved, and returns `false` if
|
||||
* there was no previous sibling node.
|
||||
*
|
||||
* Note, that this function may be slower than
|
||||
* [`ts_tree_cursor_goto_next_sibling`] due to how node positions are stored. In
|
||||
* the worst case, this will need to iterate through all the children upto the
|
||||
* previous sibling node to recalculate its position.
|
||||
*/
|
||||
bool ts_tree_cursor_goto_previous_sibling(TSTreeCursor *self);
|
||||
|
||||
/**
|
||||
* Move the cursor to the first child of its current node.
|
||||
*
|
||||
* This returns `true` if the cursor successfully moved, and returns `false`
|
||||
* if there were no children.
|
||||
*/
|
||||
bool ts_tree_cursor_goto_first_child(TSTreeCursor *self);
|
||||
|
||||
/**
|
||||
* Move the cursor to the last child of its current node.
|
||||
*
|
||||
* This returns `true` if the cursor successfully moved, and returns `false` if
|
||||
* there were no children.
|
||||
*
|
||||
* Note that this function may be slower than [`ts_tree_cursor_goto_first_child`]
|
||||
* because it needs to iterate through all the children to compute the child's
|
||||
* position.
|
||||
*/
|
||||
bool ts_tree_cursor_goto_last_child(TSTreeCursor *self);
|
||||
|
||||
/**
|
||||
* Move the cursor to the node that is the nth descendant of
|
||||
* the original node that the cursor was constructed with, where
|
||||
* zero represents the original node itself.
|
||||
*/
|
||||
void ts_tree_cursor_goto_descendant(TSTreeCursor *self, uint32_t goal_descendant_index);
|
||||
|
||||
/**
|
||||
* Get the index of the cursor's current node out of all of the
|
||||
* descendants of the original node that the cursor was constructed with.
|
||||
*/
|
||||
uint32_t ts_tree_cursor_current_descendant_index(const TSTreeCursor *self);
|
||||
|
||||
/**
|
||||
* Get the depth of the cursor's current node relative to the original
|
||||
* node that the cursor was constructed with.
|
||||
*/
|
||||
uint32_t ts_tree_cursor_current_depth(const TSTreeCursor *self);
|
||||
|
||||
/**
|
||||
* Move the cursor to the first child of its current node that extends beyond
|
||||
* the given byte offset or point.
|
||||
*
|
||||
* This returns the index of the child node if one was found, and returns -1
|
||||
* if no such child was found.
|
||||
*/
|
||||
int64_t ts_tree_cursor_goto_first_child_for_byte(TSTreeCursor *self, uint32_t goal_byte);
|
||||
int64_t ts_tree_cursor_goto_first_child_for_point(TSTreeCursor *self, TSPoint goal_point);
|
||||
|
||||
TSTreeCursor ts_tree_cursor_copy(const TSTreeCursor *cursor);
|
||||
|
||||
/*******************/
|
||||
/* Section - Query */
|
||||
/*******************/
|
||||
|
||||
/**
|
||||
* Create a new query from a string containing one or more S-expression
|
||||
* patterns. The query is associated with a particular language, and can
|
||||
* only be run on syntax nodes parsed with that language.
|
||||
*
|
||||
* If all of the given patterns are valid, this returns a [`TSQuery`].
|
||||
* If a pattern is invalid, this returns `NULL`, and provides two pieces
|
||||
* of information about the problem:
|
||||
* 1. The byte offset of the error is written to the `error_offset` parameter.
|
||||
* 2. The type of error is written to the `error_type` parameter.
|
||||
*/
|
||||
TSQuery *ts_query_new(
|
||||
const TSLanguage *language,
|
||||
const char *source,
|
||||
uint32_t source_len,
|
||||
uint32_t *error_offset,
|
||||
TSQueryError *error_type
|
||||
);
|
||||
|
||||
/**
|
||||
* Delete a query, freeing all of the memory that it used.
|
||||
*/
|
||||
void ts_query_delete(TSQuery *self);
|
||||
|
||||
/**
|
||||
* Get the number of patterns, captures, or string literals in the query.
|
||||
*/
|
||||
uint32_t ts_query_pattern_count(const TSQuery *self);
|
||||
uint32_t ts_query_capture_count(const TSQuery *self);
|
||||
uint32_t ts_query_string_count(const TSQuery *self);
|
||||
|
||||
/**
|
||||
* Get the byte offset where the given pattern starts in the query's source.
|
||||
*
|
||||
* This can be useful when combining queries by concatenating their source
|
||||
* code strings.
|
||||
*/
|
||||
uint32_t ts_query_start_byte_for_pattern(const TSQuery *self, uint32_t pattern_index);
|
||||
|
||||
/**
|
||||
* Get all of the predicates for the given pattern in the query.
|
||||
*
|
||||
* The predicates are represented as a single array of steps. There are three
|
||||
* types of steps in this array, which correspond to the three legal values for
|
||||
* the `type` field:
|
||||
* - `TSQueryPredicateStepTypeCapture` - Steps with this type represent names
|
||||
* of captures. Their `value_id` can be used with the
|
||||
* [`ts_query_capture_name_for_id`] function to obtain the name of the capture.
|
||||
* - `TSQueryPredicateStepTypeString` - Steps with this type represent literal
|
||||
* strings. Their `value_id` can be used with the
|
||||
* [`ts_query_string_value_for_id`] function to obtain their string value.
|
||||
* - `TSQueryPredicateStepTypeDone` - Steps with this type are *sentinels*
|
||||
* that represent the end of an individual predicate. If a pattern has two
|
||||
* predicates, then there will be two steps with this `type` in the array.
|
||||
*/
|
||||
const TSQueryPredicateStep *ts_query_predicates_for_pattern(
|
||||
const TSQuery *self,
|
||||
uint32_t pattern_index,
|
||||
uint32_t *step_count
|
||||
);
|
||||
|
||||
/*
|
||||
* Check if the given pattern in the query has a single root node.
|
||||
*/
|
||||
bool ts_query_is_pattern_rooted(const TSQuery *self, uint32_t pattern_index);
|
||||
|
||||
/*
|
||||
* Check if the given pattern in the query is 'non local'.
|
||||
*
|
||||
* A non-local pattern has multiple root nodes and can match within a
|
||||
* repeating sequence of nodes, as specified by the grammar. Non-local
|
||||
* patterns disable certain optimizations that would otherwise be possible
|
||||
* when executing a query on a specific range of a syntax tree.
|
||||
*/
|
||||
bool ts_query_is_pattern_non_local(const TSQuery *self, uint32_t pattern_index);
|
||||
|
||||
/*
|
||||
* Check if a given pattern is guaranteed to match once a given step is reached.
|
||||
* The step is specified by its byte offset in the query's source code.
|
||||
*/
|
||||
bool ts_query_is_pattern_guaranteed_at_step(const TSQuery *self, uint32_t byte_offset);
|
||||
|
||||
/**
|
||||
* Get the name and length of one of the query's captures, or one of the
|
||||
* query's string literals. Each capture and string is associated with a
|
||||
* numeric id based on the order that it appeared in the query's source.
|
||||
*/
|
||||
const char *ts_query_capture_name_for_id(
|
||||
const TSQuery *self,
|
||||
uint32_t index,
|
||||
uint32_t *length
|
||||
);
|
||||
|
||||
/**
|
||||
* Get the quantifier of the query's captures. Each capture is * associated
|
||||
* with a numeric id based on the order that it appeared in the query's source.
|
||||
*/
|
||||
TSQuantifier ts_query_capture_quantifier_for_id(
|
||||
const TSQuery *self,
|
||||
uint32_t pattern_index,
|
||||
uint32_t capture_index
|
||||
);
|
||||
|
||||
const char *ts_query_string_value_for_id(
|
||||
const TSQuery *self,
|
||||
uint32_t index,
|
||||
uint32_t *length
|
||||
);
|
||||
|
||||
/**
|
||||
* Disable a certain capture within a query.
|
||||
*
|
||||
* This prevents the capture from being returned in matches, and also avoids
|
||||
* any resource usage associated with recording the capture. Currently, there
|
||||
* is no way to undo this.
|
||||
*/
|
||||
void ts_query_disable_capture(TSQuery *self, const char *name, uint32_t length);
|
||||
|
||||
/**
|
||||
* Disable a certain pattern within a query.
|
||||
*
|
||||
* This prevents the pattern from matching and removes most of the overhead
|
||||
* associated with the pattern. Currently, there is no way to undo this.
|
||||
*/
|
||||
void ts_query_disable_pattern(TSQuery *self, uint32_t pattern_index);
|
||||
|
||||
/**
|
||||
* Create a new cursor for executing a given query.
|
||||
*
|
||||
* The cursor stores the state that is needed to iteratively search
|
||||
* for matches. To use the query cursor, first call [`ts_query_cursor_exec`]
|
||||
* to start running a given query on a given syntax node. Then, there are
|
||||
* two options for consuming the results of the query:
|
||||
* 1. Repeatedly call [`ts_query_cursor_next_match`] to iterate over all of the
|
||||
* *matches* in the order that they were found. Each match contains the
|
||||
* index of the pattern that matched, and an array of captures. Because
|
||||
* multiple patterns can match the same set of nodes, one match may contain
|
||||
* captures that appear *before* some of the captures from a previous match.
|
||||
* 2. Repeatedly call [`ts_query_cursor_next_capture`] to iterate over all of the
|
||||
* individual *captures* in the order that they appear. This is useful if
|
||||
* don't care about which pattern matched, and just want a single ordered
|
||||
* sequence of captures.
|
||||
*
|
||||
* If you don't care about consuming all of the results, you can stop calling
|
||||
* [`ts_query_cursor_next_match`] or [`ts_query_cursor_next_capture`] at any point.
|
||||
* You can then start executing another query on another node by calling
|
||||
* [`ts_query_cursor_exec`] again.
|
||||
*/
|
||||
TSQueryCursor *ts_query_cursor_new(void);
|
||||
|
||||
/**
|
||||
* Delete a query cursor, freeing all of the memory that it used.
|
||||
*/
|
||||
void ts_query_cursor_delete(TSQueryCursor *self);
|
||||
|
||||
/**
|
||||
* Start running a given query on a given node.
|
||||
*/
|
||||
void ts_query_cursor_exec(TSQueryCursor *self, const TSQuery *query, TSNode node);
|
||||
|
||||
/**
|
||||
* Manage the maximum number of in-progress matches allowed by this query
|
||||
* cursor.
|
||||
*
|
||||
* Query cursors have an optional maximum capacity for storing lists of
|
||||
* in-progress captures. If this capacity is exceeded, then the
|
||||
* earliest-starting match will silently be dropped to make room for further
|
||||
* matches. This maximum capacity is optional — by default, query cursors allow
|
||||
* any number of pending matches, dynamically allocating new space for them as
|
||||
* needed as the query is executed.
|
||||
*/
|
||||
bool ts_query_cursor_did_exceed_match_limit(const TSQueryCursor *self);
|
||||
uint32_t ts_query_cursor_match_limit(const TSQueryCursor *self);
|
||||
void ts_query_cursor_set_match_limit(TSQueryCursor *self, uint32_t limit);
|
||||
|
||||
/**
|
||||
* Set the range of bytes or (row, column) positions in which the query
|
||||
* will be executed.
|
||||
*/
|
||||
void ts_query_cursor_set_byte_range(TSQueryCursor *self, uint32_t start_byte, uint32_t end_byte);
|
||||
void ts_query_cursor_set_point_range(TSQueryCursor *self, TSPoint start_point, TSPoint end_point);
|
||||
|
||||
/**
|
||||
* Advance to the next match of the currently running query.
|
||||
*
|
||||
* If there is a match, write it to `*match` and return `true`.
|
||||
* Otherwise, return `false`.
|
||||
*/
|
||||
bool ts_query_cursor_next_match(TSQueryCursor *self, TSQueryMatch *match);
|
||||
void ts_query_cursor_remove_match(TSQueryCursor *self, uint32_t match_id);
|
||||
|
||||
/**
|
||||
* Advance to the next capture of the currently running query.
|
||||
*
|
||||
* If there is a capture, write its match to `*match` and its index within
|
||||
* the matche's capture list to `*capture_index`. Otherwise, return `false`.
|
||||
*/
|
||||
bool ts_query_cursor_next_capture(
|
||||
TSQueryCursor *self,
|
||||
TSQueryMatch *match,
|
||||
uint32_t *capture_index
|
||||
);
|
||||
|
||||
/**
|
||||
* Set the maximum start depth for a query cursor.
|
||||
*
|
||||
* This prevents cursors from exploring children nodes at a certain depth.
|
||||
* Note if a pattern includes many children, then they will still be checked.
|
||||
*
|
||||
* The zero max start depth value can be used as a special behavior and
|
||||
* it helps to destructure a subtree by staying on a node and using captures
|
||||
* for interested parts. Note that the zero max start depth only limit a search
|
||||
* depth for a pattern's root node but other nodes that are parts of the pattern
|
||||
* may be searched at any depth what defined by the pattern structure.
|
||||
*
|
||||
* Set to `UINT32_MAX` to remove the maximum start depth.
|
||||
*/
|
||||
void ts_query_cursor_set_max_start_depth(TSQueryCursor *self, uint32_t max_start_depth);
|
||||
|
||||
/**********************/
|
||||
/* Section - Language */
|
||||
|
|
@ -1040,7 +668,7 @@ uint32_t ts_language_symbol_count(const TSLanguage *self);
|
|||
|
||||
/**
|
||||
* Get the number of valid states in this language.
|
||||
*/
|
||||
*/
|
||||
uint32_t ts_language_state_count(const TSLanguage *self);
|
||||
|
||||
/**
|
||||
|
|
@ -1051,12 +679,7 @@ const char *ts_language_symbol_name(const TSLanguage *self, TSSymbol symbol);
|
|||
/**
|
||||
* Get the numerical id for the given node type string.
|
||||
*/
|
||||
TSSymbol ts_language_symbol_for_name(
|
||||
const TSLanguage *self,
|
||||
const char *string,
|
||||
uint32_t length,
|
||||
bool is_named
|
||||
);
|
||||
TSSymbol ts_language_symbol_for_name(const TSLanguage *self, const char *string, uint32_t length, bool is_named);
|
||||
|
||||
/**
|
||||
* Get the number of distinct field names in the language.
|
||||
|
|
@ -1094,180 +717,10 @@ uint32_t ts_language_version(const TSLanguage *self);
|
|||
* Get the next parse state. Combine this with lookahead iterators to generate
|
||||
* completion suggestions or valid symbols in error nodes. Use
|
||||
* [`ts_node_grammar_symbol`] for valid symbols.
|
||||
*/
|
||||
*/
|
||||
TSStateId ts_language_next_state(const TSLanguage *self, TSStateId state, TSSymbol symbol);
|
||||
|
||||
/********************************/
|
||||
/* Section - Lookahead Iterator */
|
||||
/********************************/
|
||||
|
||||
/**
|
||||
* Create a new lookahead iterator for the given language and parse state.
|
||||
*
|
||||
* This returns `NULL` if state is invalid for the language.
|
||||
*
|
||||
* Repeatedly using [`ts_lookahead_iterator_next`] and
|
||||
* [`ts_lookahead_iterator_current_symbol`] will generate valid symbols in the
|
||||
* given parse state. Newly created lookahead iterators will contain the `ERROR`
|
||||
* symbol.
|
||||
*
|
||||
* Lookahead iterators can be useful to generate suggestions and improve syntax
|
||||
* error diagnostics. To get symbols valid in an ERROR node, use the lookahead
|
||||
* iterator on its first leaf node state. For `MISSING` nodes, a lookahead
|
||||
* iterator created on the previous non-extra leaf node may be appropriate.
|
||||
*/
|
||||
TSLookaheadIterator *ts_lookahead_iterator_new(const TSLanguage *self, TSStateId state);
|
||||
|
||||
/**
|
||||
* Delete a lookahead iterator freeing all the memory used.
|
||||
*/
|
||||
void ts_lookahead_iterator_delete(TSLookaheadIterator *self);
|
||||
|
||||
/**
|
||||
* Reset the lookahead iterator to another state.
|
||||
*
|
||||
* This returns `true` if the iterator was reset to the given state and `false`
|
||||
* otherwise.
|
||||
*/
|
||||
bool ts_lookahead_iterator_reset_state(TSLookaheadIterator *self, TSStateId state);
|
||||
|
||||
/**
|
||||
* Reset the lookahead iterator.
|
||||
*
|
||||
* This returns `true` if the language was set successfully and `false`
|
||||
* otherwise.
|
||||
*/
|
||||
bool ts_lookahead_iterator_reset(TSLookaheadIterator *self, const TSLanguage *language, TSStateId state);
|
||||
|
||||
/**
|
||||
* Get the current language of the lookahead iterator.
|
||||
*/
|
||||
const TSLanguage *ts_lookahead_iterator_language(const TSLookaheadIterator *self);
|
||||
|
||||
/**
|
||||
* Advance the lookahead iterator to the next symbol.
|
||||
*
|
||||
* This returns `true` if there is a new symbol and `false` otherwise.
|
||||
*/
|
||||
bool ts_lookahead_iterator_next(TSLookaheadIterator *self);
|
||||
|
||||
/**
|
||||
* Get the current symbol of the lookahead iterator;
|
||||
*/
|
||||
TSSymbol ts_lookahead_iterator_current_symbol(const TSLookaheadIterator *self);
|
||||
|
||||
/**
|
||||
* Get the current symbol type of the lookahead iterator as a null terminated
|
||||
* string.
|
||||
*/
|
||||
const char *ts_lookahead_iterator_current_symbol_name(const TSLookaheadIterator *self);
|
||||
|
||||
/*************************************/
|
||||
/* Section - WebAssembly Integration */
|
||||
/************************************/
|
||||
|
||||
typedef struct wasm_engine_t TSWasmEngine;
|
||||
typedef struct TSWasmStore TSWasmStore;
|
||||
|
||||
typedef enum {
|
||||
TSWasmErrorKindNone = 0,
|
||||
TSWasmErrorKindParse,
|
||||
TSWasmErrorKindCompile,
|
||||
TSWasmErrorKindInstantiate,
|
||||
TSWasmErrorKindAllocate,
|
||||
} TSWasmErrorKind;
|
||||
|
||||
typedef struct {
|
||||
TSWasmErrorKind kind;
|
||||
char *message;
|
||||
} TSWasmError;
|
||||
|
||||
/**
|
||||
* Create a Wasm store.
|
||||
*/
|
||||
TSWasmStore *ts_wasm_store_new(
|
||||
TSWasmEngine *engine,
|
||||
TSWasmError *error
|
||||
);
|
||||
|
||||
/**
|
||||
* Free the memory associated with the given Wasm store.
|
||||
*/
|
||||
void ts_wasm_store_delete(TSWasmStore *);
|
||||
|
||||
/**
|
||||
* Create a language from a buffer of Wasm. The resulting language behaves
|
||||
* like any other Tree-sitter language, except that in order to use it with
|
||||
* a parser, that parser must have a Wasm store. Note that the language
|
||||
* can be used with any Wasm store, it doesn't need to be the same store that
|
||||
* was used to originally load it.
|
||||
*/
|
||||
const TSLanguage *ts_wasm_store_load_language(
|
||||
TSWasmStore *,
|
||||
const char *name,
|
||||
const char *wasm,
|
||||
uint32_t wasm_len,
|
||||
TSWasmError *error
|
||||
);
|
||||
|
||||
/**
|
||||
* Get the number of languages instantiated in the given wasm store.
|
||||
*/
|
||||
size_t ts_wasm_store_language_count(const TSWasmStore *);
|
||||
|
||||
/**
|
||||
* Check if the language came from a Wasm module. If so, then in order to use
|
||||
* this language with a Parser, that parser must have a Wasm store assigned.
|
||||
*/
|
||||
bool ts_language_is_wasm(const TSLanguage *);
|
||||
|
||||
/**
|
||||
* Assign the given Wasm store to the parser. A parser must have a Wasm store
|
||||
* in order to use Wasm languages.
|
||||
*/
|
||||
void ts_parser_set_wasm_store(TSParser *, TSWasmStore *);
|
||||
|
||||
/**
|
||||
* Remove the parser's current Wasm store and return it. This returns NULL if
|
||||
* the parser doesn't have a Wasm store.
|
||||
*/
|
||||
TSWasmStore *ts_parser_take_wasm_store(TSParser *);
|
||||
|
||||
/**********************************/
|
||||
/* Section - Global Configuration */
|
||||
/**********************************/
|
||||
|
||||
/**
|
||||
* Set the allocation functions used by the library.
|
||||
*
|
||||
* By default, Tree-sitter uses the standard libc allocation functions,
|
||||
* but aborts the process when an allocation fails. This function lets
|
||||
* you supply alternative allocation functions at runtime.
|
||||
*
|
||||
* If you pass `NULL` for any parameter, Tree-sitter will switch back to
|
||||
* its default implementation of that function.
|
||||
*
|
||||
* If you call this function after the library has already been used, then
|
||||
* you must ensure that either:
|
||||
* 1. All the existing objects have been freed.
|
||||
* 2. The new allocator shares its state with the old one, so it is capable
|
||||
* of freeing memory that was allocated by the old allocator.
|
||||
*/
|
||||
void ts_set_allocator(
|
||||
void *(*new_malloc)(size_t),
|
||||
void *(*new_calloc)(size_t, size_t),
|
||||
void *(*new_realloc)(void *, size_t),
|
||||
void (*new_free)(void *)
|
||||
);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef TREE_SITTER_HIDE_SYMBOLS
|
||||
#if defined(__GNUC__) || defined(__clang__)
|
||||
#pragma GCC visibility pop
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#endif // TREE_SITTER_API_H_
|
||||
|
|
|
|||
|
|
@ -1,11 +1,6 @@
|
|||
#ifndef TREE_SITTER_ERROR_COSTS_H_
|
||||
#define TREE_SITTER_ERROR_COSTS_H_
|
||||
|
||||
#define ERROR_STATE 0
|
||||
#define ERROR_COST_PER_RECOVERY 500
|
||||
#define ERROR_COST_PER_MISSING_TREE 110
|
||||
#define ERROR_COST_PER_SKIPPED_TREE 100
|
||||
#define ERROR_COST_PER_SKIPPED_LINE 30
|
||||
#define ERROR_COST_PER_SKIPPED_CHAR 1
|
||||
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
#define _POSIX_C_SOURCE 200112L
|
||||
|
||||
// #include "./alloc.c"
|
||||
|
||||
#include "./language.c"
|
||||
#include "./lexer.c"
|
||||
#include "./node.c"
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
#ifndef TREE_SITTER_POINT_H_
|
||||
#define TREE_SITTER_POINT_H_
|
||||
|
||||
#include "api.h"
|
||||
#include "./api.h"
|
||||
|
||||
#define POINT_ZERO ((TSPoint) {0, 0})
|
||||
#define POINT_MAX ((TSPoint) {UINT32_MAX, UINT32_MAX})
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
|
||||
|
||||
#include "./array.h"
|
||||
#include "api.h"
|
||||
#include "./api.h"
|
||||
|
||||
typedef struct ReduceAction {
|
||||
uint32_t count;
|
||||
|
|
|
|||
|
|
@ -29,41 +29,10 @@ enum TokenType
|
|||
ESAC,
|
||||
ERROR_RECOVERY,
|
||||
};
|
||||
// enum TokenType {
|
||||
// HEREDOC_START,
|
||||
// SIMPLE_HEREDOC_BODY,
|
||||
// HEREDOC_BODY_BEGINNING,
|
||||
// HEREDOC_CONTENT,
|
||||
// HEREDOC_END,
|
||||
// FILE_DESCRIPTOR,
|
||||
// EMPTY_VALUE,
|
||||
// CONCAT,
|
||||
// VARIABLE_NAME,
|
||||
// TEST_OPERATOR,
|
||||
// REGEX,
|
||||
// REGEX_NO_SLASH,
|
||||
// REGEX_NO_SPACE,
|
||||
// EXPANSION_WORD,
|
||||
// EXTGLOB_PATTERN,
|
||||
// BARE_DOLLAR,
|
||||
// BRACE_START,
|
||||
// IMMEDIATE_DOUBLE_HASH,
|
||||
// EXTERNAL_EXPANSION_SYM_HASH,
|
||||
// EXTERNAL_EXPANSION_SYM_BANG,
|
||||
// EXTERNAL_EXPANSION_SYM_EQUAL,
|
||||
// CLOSING_BRACE,
|
||||
// CLOSING_BRACKET,
|
||||
// HEREDOC_ARROW,
|
||||
// HEREDOC_ARROW_DASH,
|
||||
// NEWLINE,
|
||||
// OPENING_PAREN,
|
||||
// ESAC,
|
||||
// ERROR_RECOVERY,
|
||||
// };
|
||||
|
||||
typedef Array(char) String;
|
||||
|
||||
typedef struct
|
||||
typedef struct Heredoc
|
||||
{
|
||||
bool is_raw;
|
||||
bool started;
|
||||
|
|
@ -81,7 +50,7 @@ typedef struct
|
|||
.current_leading_word = array_new(), \
|
||||
};
|
||||
|
||||
typedef struct
|
||||
typedef struct Scanner
|
||||
{
|
||||
uint8_t last_glob_paren_depth;
|
||||
bool ext_was_in_double_quote;
|
||||
|
|
@ -250,7 +219,6 @@ static inline bool scan_bare_dollar(TSLexer *lexer)
|
|||
while (iswspace(lexer->lookahead) && lexer->lookahead != '\n' && !lexer->eof(lexer))
|
||||
skip(lexer);
|
||||
|
||||
|
||||
if (lexer->lookahead == '$')
|
||||
{
|
||||
advance(lexer);
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
|
|
@ -2,9 +2,8 @@
|
|||
#define TREE_SITTER_PARSE_STACK_H_
|
||||
|
||||
#include "./array.h"
|
||||
#include "./error_costs.h"
|
||||
// #include "./error_costs.h"
|
||||
#include "./subtree.h"
|
||||
#include <stdio.h>
|
||||
|
||||
typedef struct Stack Stack;
|
||||
|
||||
|
|
@ -123,6 +122,4 @@ void ts_stack_remove_version(Stack *, StackVersion);
|
|||
|
||||
void ts_stack_clear(Stack *);
|
||||
|
||||
bool ts_stack_print_dot_graph(Stack *, const TSLanguage *, FILE *);
|
||||
|
||||
#endif // TREE_SITTER_PARSE_STACK_H_
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue