diff --git a/parser/src/api.h b/parser/src/api.h index deb2364e..d924935f 100644 --- a/parser/src/api.h +++ b/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 -#include #include +#include +#include + +#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 */ @@ -38,115 +35,131 @@ extern "C" { /* Section - Types */ /*******************/ -typedef uint16_t TSStateId; -typedef uint16_t TSSymbol; -typedef uint16_t TSFieldId; -typedef struct TSLanguage TSLanguage; -typedef struct TSParser TSParser; -typedef struct TSTree TSTree; -typedef struct TSQuery TSQuery; -typedef struct TSQueryCursor TSQueryCursor; +typedef uint16_t TSStateId; +typedef uint16_t TSSymbol; +typedef uint16_t TSFieldId; +typedef struct TSLanguage TSLanguage; +typedef struct TSParser TSParser; +typedef struct TSTree TSTree; +typedef struct TSQuery TSQuery; +typedef struct TSQueryCursor TSQueryCursor; typedef struct TSLookaheadIterator TSLookaheadIterator; -typedef enum TSInputEncoding { - TSInputEncodingUTF8, - TSInputEncodingUTF16, +typedef enum TSInputEncoding +{ + TSInputEncodingUTF8, + TSInputEncodingUTF16, } TSInputEncoding; -typedef enum TSSymbolType { - TSSymbolTypeRegular, - TSSymbolTypeAnonymous, - TSSymbolTypeAuxiliary, +typedef enum TSSymbolType +{ + TSSymbolTypeRegular, + TSSymbolTypeAnonymous, + TSSymbolTypeAuxiliary, } TSSymbolType; -typedef struct TSPoint { - uint32_t row; - uint32_t column; +typedef struct TSPoint +{ + uint32_t row; + uint32_t column; } TSPoint; -typedef struct TSRange { - TSPoint start_point; - TSPoint end_point; - uint32_t start_byte; - uint32_t end_byte; +typedef struct TSRange +{ + TSPoint start_point; + TSPoint end_point; + uint32_t start_byte; + uint32_t end_byte; } TSRange; -typedef struct TSInput { - void *payload; - const char *(*read)(void *payload, uint32_t byte_index, TSPoint position, uint32_t *bytes_read); - TSInputEncoding encoding; +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 { - TSLogTypeParse, - TSLogTypeLex, +typedef enum TSLogType +{ + TSLogTypeParse, + TSLogTypeLex, } TSLogType; -typedef struct TSLogger { - void *payload; - void (*log)(void *payload, TSLogType log_type, const char *buffer); +typedef struct TSLogger +{ + void *payload; + void (*log)(void *payload, TSLogType log_type, const char *buffer); } TSLogger; -typedef struct TSInputEdit { - uint32_t start_byte; - uint32_t old_end_byte; - uint32_t new_end_byte; - TSPoint start_point; - TSPoint old_end_point; - TSPoint new_end_point; +typedef struct TSInputEdit +{ + uint32_t start_byte; + uint32_t old_end_byte; + uint32_t new_end_byte; + TSPoint start_point; + TSPoint old_end_point; + TSPoint new_end_point; } TSInputEdit; -typedef struct TSNode { - uint32_t context[4]; - const void *id; - const TSTree *tree; +typedef struct TSNode +{ + uint32_t context[4]; + const void *id; + const TSTree *tree; } TSNode; -typedef struct TSTreeCursor { - const void *tree; - const void *id; - uint32_t context[3]; +typedef struct TSTreeCursor +{ + const void *tree; + const void *id; + uint32_t context[3]; } TSTreeCursor; -typedef struct TSQueryCapture { - TSNode node; - uint32_t index; +typedef struct TSQueryCapture +{ + TSNode node; + uint32_t index; } TSQueryCapture; -typedef enum TSQuantifier { - TSQuantifierZero = 0, // must match the array initialization value - TSQuantifierZeroOrOne, - TSQuantifierZeroOrMore, - TSQuantifierOne, - TSQuantifierOneOrMore, +typedef enum TSQuantifier +{ + TSQuantifierZero = 0, // must match the array initialization value + TSQuantifierZeroOrOne, + TSQuantifierZeroOrMore, + TSQuantifierOne, + TSQuantifierOneOrMore, } TSQuantifier; -typedef struct TSQueryMatch { - uint32_t id; - uint16_t pattern_index; - uint16_t capture_count; - const TSQueryCapture *captures; +typedef struct TSQueryMatch +{ + uint32_t id; + uint16_t pattern_index; + uint16_t capture_count; + const TSQueryCapture *captures; } TSQueryMatch; -typedef enum TSQueryPredicateStepType { - TSQueryPredicateStepTypeDone, - TSQueryPredicateStepTypeCapture, - TSQueryPredicateStepTypeString, +typedef enum TSQueryPredicateStepType +{ + TSQueryPredicateStepTypeDone, + TSQueryPredicateStepTypeCapture, + TSQueryPredicateStepTypeString, } TSQueryPredicateStepType; -typedef struct TSQueryPredicateStep { - TSQueryPredicateStepType type; - uint32_t value_id; +typedef struct TSQueryPredicateStep +{ + TSQueryPredicateStepType type; + uint32_t value_id; } TSQueryPredicateStep; -typedef enum TSQueryError { - TSQueryErrorNone = 0, - TSQueryErrorSyntax, - TSQueryErrorNodeType, - TSQueryErrorField, - TSQueryErrorCapture, - TSQueryErrorStructure, - TSQueryErrorLanguage, +typedef enum TSQueryError +{ + TSQueryErrorNone = 0, + TSQueryErrorSyntax, + TSQueryErrorNodeType, + TSQueryErrorField, + TSQueryErrorCapture, + TSQueryErrorStructure, + TSQueryErrorLanguage, } TSQueryError; /********************/ @@ -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. @@ -247,7 +253,7 @@ const TSRange *ts_parser_included_ranges( * This function returns a syntax tree on success, and `NULL` on failure. There * are three possible reasons for failure: * 1. The parser does not have a language assigned. Check for this using the - [`ts_parser_language`] function. + [`ts_parser_language`] function. * 2. Parsing was cancelled due to a timeout that was set by an earlier call to * the [`ts_parser_set_timeout_micros`] function. You can resume parsing from * where the parser left out by calling [`ts_parser_parse`] again with the @@ -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_ +#endif // TREE_SITTER_API_H_ diff --git a/parser/src/error_costs.h b/parser/src/error_costs.h index 32d3666a..2f58a206 100644 --- a/parser/src/error_costs.h +++ b/parser/src/error_costs.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 diff --git a/parser/src/lib.c b/parser/src/lib.c index bf6bb574..c1b85fa8 100644 --- a/parser/src/lib.c +++ b/parser/src/lib.c @@ -1,6 +1,6 @@ #define _POSIX_C_SOURCE 200112L -// #include "./alloc.c" + #include "./language.c" #include "./lexer.c" #include "./node.c" diff --git a/parser/src/point.h b/parser/src/point.h index 942a86e1..1c8b7133 100644 --- a/parser/src/point.h +++ b/parser/src/point.h @@ -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}) diff --git a/parser/src/reduce_action.h b/parser/src/reduce_action.h index 02ae2cc9..e8dfda18 100644 --- a/parser/src/reduce_action.h +++ b/parser/src/reduce_action.h @@ -3,7 +3,7 @@ #include "./array.h" -#include "api.h" +#include "./api.h" typedef struct ReduceAction { uint32_t count; diff --git a/parser/src/scanner.c b/parser/src/scanner.c index 77b3f133..4e04ce9c 100644 --- a/parser/src/scanner.c +++ b/parser/src/scanner.c @@ -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); diff --git a/parser/src/stack.c b/parser/src/stack.c index 2d2c0f5e..b94a65bd 100644 --- a/parser/src/stack.c +++ b/parser/src/stack.c @@ -1,10 +1,10 @@ -#include "me/mem/mem.h" -#include "./language.h" -#include "./subtree.h" -#include "./array.h" #include "./stack.h" +#include "./array.h" +#include "./language.h" #include "./length.h" +#include "./subtree.h" +#include "me/mem/mem.h" #include #include #include @@ -13,888 +13,833 @@ #define MAX_NODE_POOL_SIZE 50 #define MAX_ITERATOR_COUNT 64 -#if defined _WIN32 && !defined __GNUC__ -#define forceinline __forceinline -#else -#define forceinline static inline __attribute__((always_inline)) -#endif - typedef struct StackNode StackNode; -typedef struct { - StackNode *node; - Subtree subtree; - bool is_pending; +typedef struct StackLink +{ + StackNode *node; + Subtree subtree; + bool is_pending; } StackLink; -struct StackNode { - TSStateId state; - Length position; - StackLink links[MAX_LINK_COUNT]; - short unsigned int link_count; - uint32_t ref_count; - unsigned error_cost; - unsigned node_count; - int dynamic_precedence; +struct StackNode +{ + TSStateId state; + Length position; + StackLink links[MAX_LINK_COUNT]; + short unsigned int link_count; + uint32_t ref_count; + unsigned error_cost; + unsigned node_count; + int dynamic_precedence; }; -typedef struct { - StackNode *node; - SubtreeArray subtrees; - uint32_t subtree_count; - bool is_pending; +typedef struct StackIterator +{ + StackNode *node; + SubtreeArray subtrees; + uint32_t subtree_count; + bool is_pending; } StackIterator; typedef Array(StackNode *) StackNodeArray; -typedef enum { - StackStatusActive, - StackStatusPaused, - StackStatusHalted, +typedef enum StackStatus +{ + StackStatusActive, + StackStatusPaused, + StackStatusHalted, } StackStatus; -typedef struct { - StackNode *node; - StackSummary *summary; - unsigned node_count_at_last_error; - Subtree last_external_token; - Subtree lookahead_when_paused; - StackStatus status; +typedef struct StackHead +{ + StackNode *node; + StackSummary *summary; + unsigned node_count_at_last_error; + Subtree last_external_token; + Subtree lookahead_when_paused; + StackStatus status; } StackHead; -struct Stack { - Array(StackHead) heads; - StackSliceArray slices; - Array(StackIterator) iterators; - StackNodeArray node_pool; - StackNode *base_node; - SubtreePool *subtree_pool; +struct Stack +{ + Array(StackHead) heads; + StackSliceArray slices; + Array(StackIterator) iterators; + StackNodeArray node_pool; + StackNode *base_node; + SubtreePool *subtree_pool; }; typedef unsigned StackAction; -enum { - StackActionNone, - StackActionStop = 1, - StackActionPop = 2, +enum StackAction +{ + StackActionNone, + StackActionStop = 1, + StackActionPop = 2, }; typedef StackAction (*StackCallback)(void *, const StackIterator *); -static void stack_node_retain(StackNode *self) { - if (!self) - return; - assert(self->ref_count > 0); - self->ref_count++; - assert(self->ref_count != 0); +static void stack_node_retain(StackNode *self) +{ + if (!self) + return; + assert(self->ref_count > 0); + self->ref_count++; + assert(self->ref_count != 0); } -static void stack_node_release( - StackNode *self, - StackNodeArray *pool, - SubtreePool *subtree_pool -) { +static void stack_node_release(StackNode *self, StackNodeArray *pool, SubtreePool *subtree_pool) +{ recur: - assert(self->ref_count != 0); - self->ref_count--; - if (self->ref_count > 0) return; + assert(self->ref_count != 0); + self->ref_count--; + if (self->ref_count > 0) + return; - StackNode *first_predecessor = NULL; - if (self->link_count > 0) { - for (unsigned i = self->link_count - 1; i > 0; i--) { - StackLink link = self->links[i]; - if (link.subtree.ptr) ts_subtree_release(subtree_pool, link.subtree); - stack_node_release(link.node, pool, subtree_pool); - } - StackLink link = self->links[0]; - if (link.subtree.ptr) ts_subtree_release(subtree_pool, link.subtree); - first_predecessor = self->links[0].node; - } + StackNode *first_predecessor = NULL; + if (self->link_count > 0) + { + for (unsigned i = self->link_count - 1; i > 0; i--) + { + StackLink link = self->links[i]; + if (link.subtree.ptr) + ts_subtree_release(subtree_pool, link.subtree); + stack_node_release(link.node, pool, subtree_pool); + } + StackLink link = self->links[0]; + if (link.subtree.ptr) + ts_subtree_release(subtree_pool, link.subtree); + first_predecessor = self->links[0].node; + } - if (pool->size < MAX_NODE_POOL_SIZE) { - array_push(pool, self); - } else { - mem_free(self); - } + if (pool->size < MAX_NODE_POOL_SIZE) + { + array_push(pool, self); + } + else + { + mem_free(self); + } - if (first_predecessor) { - self = first_predecessor; - goto recur; - } + if (first_predecessor) + { + self = first_predecessor; + goto recur; + } } /// Get the number of nodes in the subtree, for the purpose of measuring /// how much progress has been made by a given version of the stack. -static uint32_t stack__subtree_node_count(Subtree subtree) { - uint32_t count = ts_subtree_visible_descendant_count(subtree); - if (ts_subtree_visible(subtree)) count++; +static uint32_t stack__subtree_node_count(Subtree subtree) +{ + uint32_t count = ts_subtree_visible_descendant_count(subtree); + if (ts_subtree_visible(subtree)) + count++; - // Count intermediate error nodes even though they are not visible, - // because a stack version's node count is used to check whether it - // has made any progress since the last time it encountered an error. - if (ts_subtree_symbol(subtree) == ts_builtin_sym_error_repeat) count++; + // Count intermediate error nodes even though they are not visible, + // because a stack version's node count is used to check whether it + // has made any progress since the last time it encountered an error. + if (ts_subtree_symbol(subtree) == ts_builtin_sym_error_repeat) + count++; - return count; + return count; } -static StackNode *stack_node_new( - StackNode *previous_node, - Subtree subtree, - bool is_pending, - TSStateId state, - StackNodeArray *pool -) { - StackNode *node = pool->size > 0 - ? array_pop(pool) - : mem_alloc(sizeof(StackNode)); - *node = (StackNode) { - .ref_count = 1, - .link_count = 0, - .state = state - }; +static StackNode *stack_node_new(StackNode *previous_node, Subtree subtree, bool is_pending, TSStateId state, StackNodeArray *pool) +{ + StackNode *node = pool->size > 0 ? array_pop(pool) : mem_alloc(sizeof(StackNode)); + *node = (StackNode){.ref_count = 1, .link_count = 0, .state = state}; - if (previous_node) { - node->link_count = 1; - node->links[0] = (StackLink) { - .node = previous_node, - .subtree = subtree, - .is_pending = is_pending, - }; + if (previous_node) + { + node->link_count = 1; + node->links[0] = (StackLink){ + .node = previous_node, + .subtree = subtree, + .is_pending = is_pending, + }; - node->position = previous_node->position; - node->error_cost = previous_node->error_cost; - node->dynamic_precedence = previous_node->dynamic_precedence; - node->node_count = previous_node->node_count; + node->position = previous_node->position; + node->error_cost = previous_node->error_cost; + node->dynamic_precedence = previous_node->dynamic_precedence; + node->node_count = previous_node->node_count; - if (subtree.ptr) { - node->error_cost += ts_subtree_error_cost(subtree); - node->position = length_add(node->position, ts_subtree_total_size(subtree)); - node->node_count += stack__subtree_node_count(subtree); - node->dynamic_precedence += ts_subtree_dynamic_precedence(subtree); - } - } else { - node->position = length_zero(); - node->error_cost = 0; - } + if (subtree.ptr) + { + node->error_cost += ts_subtree_error_cost(subtree); + node->position = length_add(node->position, ts_subtree_total_size(subtree)); + node->node_count += stack__subtree_node_count(subtree); + node->dynamic_precedence += ts_subtree_dynamic_precedence(subtree); + } + } + else + { + node->position = length_zero(); + node->error_cost = 0; + } - return node; + return node; } -static bool stack__subtree_is_equivalent(Subtree left, Subtree right) { - if (left.ptr == right.ptr) return true; - if (!left.ptr || !right.ptr) return false; +static bool stack__subtree_is_equivalent(Subtree left, Subtree right) +{ + if (left.ptr == right.ptr) + return true; + if (!left.ptr || !right.ptr) + return false; - // Symbols must match - if (ts_subtree_symbol(left) != ts_subtree_symbol(right)) return false; + // Symbols must match + if (ts_subtree_symbol(left) != ts_subtree_symbol(right)) + return false; - // If both have errors, don't bother keeping both. - if (ts_subtree_error_cost(left) > 0 && ts_subtree_error_cost(right) > 0) return true; + // If both have errors, don't bother keeping both. + if (ts_subtree_error_cost(left) > 0 && ts_subtree_error_cost(right) > 0) + return true; - return ( - ts_subtree_padding(left).bytes == ts_subtree_padding(right).bytes && - ts_subtree_size(left).bytes == ts_subtree_size(right).bytes && - ts_subtree_child_count(left) == ts_subtree_child_count(right) && - ts_subtree_extra(left) == ts_subtree_extra(right) && - ts_subtree_external_scanner_state_eq(left, right) - ); + return (ts_subtree_padding(left).bytes == ts_subtree_padding(right).bytes && + ts_subtree_size(left).bytes == ts_subtree_size(right).bytes && ts_subtree_child_count(left) == ts_subtree_child_count(right) && + ts_subtree_extra(left) == ts_subtree_extra(right) && ts_subtree_external_scanner_state_eq(left, right)); } -static void stack_node_add_link( - StackNode *self, - StackLink link, - SubtreePool *subtree_pool -) { - if (link.node == self) return; +static void stack_node_add_link(StackNode *self, StackLink link, SubtreePool *subtree_pool) +{ + if (link.node == self) + return; - for (int i = 0; i < self->link_count; i++) { - StackLink *existing_link = &self->links[i]; - if (stack__subtree_is_equivalent(existing_link->subtree, link.subtree)) { - // In general, we preserve ambiguities until they are removed from the stack - // during a pop operation where multiple paths lead to the same node. But in - // the special case where two links directly connect the same pair of nodes, - // we can safely remove the ambiguity ahead of time without changing behavior. - if (existing_link->node == link.node) { - if ( - ts_subtree_dynamic_precedence(link.subtree) > - ts_subtree_dynamic_precedence(existing_link->subtree) - ) { - ts_subtree_retain(link.subtree); - ts_subtree_release(subtree_pool, existing_link->subtree); - existing_link->subtree = link.subtree; - self->dynamic_precedence = - link.node->dynamic_precedence + ts_subtree_dynamic_precedence(link.subtree); - } - return; - } + for (int i = 0; i < self->link_count; i++) + { + StackLink *existing_link = &self->links[i]; + if (stack__subtree_is_equivalent(existing_link->subtree, link.subtree)) + { + // In general, we preserve ambiguities until they are removed from the stack + // during a pop operation where multiple paths lead to the same node. But in + // the special case where two links directly connect the same pair of nodes, + // we can safely remove the ambiguity ahead of time without changing behavior. + if (existing_link->node == link.node) + { + if (ts_subtree_dynamic_precedence(link.subtree) > ts_subtree_dynamic_precedence(existing_link->subtree)) + { + ts_subtree_retain(link.subtree); + ts_subtree_release(subtree_pool, existing_link->subtree); + existing_link->subtree = link.subtree; + self->dynamic_precedence = link.node->dynamic_precedence + ts_subtree_dynamic_precedence(link.subtree); + } + return; + } - // If the previous nodes are mergeable, merge them recursively. - if ( - existing_link->node->state == link.node->state && - existing_link->node->position.bytes == link.node->position.bytes && - existing_link->node->error_cost == link.node->error_cost - ) { - for (int j = 0; j < link.node->link_count; j++) { - stack_node_add_link(existing_link->node, link.node->links[j], subtree_pool); - } - int32_t dynamic_precedence = link.node->dynamic_precedence; - if (link.subtree.ptr) { - dynamic_precedence += ts_subtree_dynamic_precedence(link.subtree); - } - if (dynamic_precedence > self->dynamic_precedence) { - self->dynamic_precedence = dynamic_precedence; - } - return; - } - } - } + // If the previous nodes are mergeable, merge them recursively. + if (existing_link->node->state == link.node->state && existing_link->node->position.bytes == link.node->position.bytes && + existing_link->node->error_cost == link.node->error_cost) + { + for (int j = 0; j < link.node->link_count; j++) + { + stack_node_add_link(existing_link->node, link.node->links[j], subtree_pool); + } + int32_t dynamic_precedence = link.node->dynamic_precedence; + if (link.subtree.ptr) + { + dynamic_precedence += ts_subtree_dynamic_precedence(link.subtree); + } + if (dynamic_precedence > self->dynamic_precedence) + { + self->dynamic_precedence = dynamic_precedence; + } + return; + } + } + } - if (self->link_count == MAX_LINK_COUNT) return; + if (self->link_count == MAX_LINK_COUNT) + return; - stack_node_retain(link.node); - unsigned node_count = link.node->node_count; - int dynamic_precedence = link.node->dynamic_precedence; - self->links[self->link_count++] = link; + stack_node_retain(link.node); + unsigned node_count = link.node->node_count; + int dynamic_precedence = link.node->dynamic_precedence; + self->links[self->link_count++] = link; - if (link.subtree.ptr) { - ts_subtree_retain(link.subtree); - node_count += stack__subtree_node_count(link.subtree); - dynamic_precedence += ts_subtree_dynamic_precedence(link.subtree); - } + if (link.subtree.ptr) + { + ts_subtree_retain(link.subtree); + node_count += stack__subtree_node_count(link.subtree); + dynamic_precedence += ts_subtree_dynamic_precedence(link.subtree); + } - if (node_count > self->node_count) self->node_count = node_count; - if (dynamic_precedence > self->dynamic_precedence) self->dynamic_precedence = dynamic_precedence; + if (node_count > self->node_count) + self->node_count = node_count; + if (dynamic_precedence > self->dynamic_precedence) + self->dynamic_precedence = dynamic_precedence; } -static void stack_head_delete( - StackHead *self, - StackNodeArray *pool, - SubtreePool *subtree_pool -) { - if (self->node) { - if (self->last_external_token.ptr) { - ts_subtree_release(subtree_pool, self->last_external_token); - } - if (self->lookahead_when_paused.ptr) { - ts_subtree_release(subtree_pool, self->lookahead_when_paused); - } - if (self->summary) { - array_delete(self->summary); - mem_free(self->summary); - } - stack_node_release(self->node, pool, subtree_pool); - } +static void stack_head_delete(StackHead *self, StackNodeArray *pool, SubtreePool *subtree_pool) +{ + if (self->node) + { + if (self->last_external_token.ptr) + { + ts_subtree_release(subtree_pool, self->last_external_token); + } + if (self->lookahead_when_paused.ptr) + { + ts_subtree_release(subtree_pool, self->lookahead_when_paused); + } + if (self->summary) + { + array_delete(self->summary); + mem_free(self->summary); + } + stack_node_release(self->node, pool, subtree_pool); + } } -static StackVersion ts_stack__add_version( - Stack *self, - StackVersion original_version, - StackNode *node -) { - StackHead head = { - .node = node, - .node_count_at_last_error = self->heads.contents[original_version].node_count_at_last_error, - .last_external_token = self->heads.contents[original_version].last_external_token, - .status = StackStatusActive, - .lookahead_when_paused = NULL_SUBTREE, - }; - array_push(&self->heads, head); - stack_node_retain(node); - if (head.last_external_token.ptr) ts_subtree_retain(head.last_external_token); - return (StackVersion)(self->heads.size - 1); +static StackVersion ts_stack__add_version(Stack *self, StackVersion original_version, StackNode *node) +{ + StackHead head = { + .node = node, + .node_count_at_last_error = self->heads.contents[original_version].node_count_at_last_error, + .last_external_token = self->heads.contents[original_version].last_external_token, + .status = StackStatusActive, + .lookahead_when_paused = NULL_SUBTREE, + }; + array_push(&self->heads, head); + stack_node_retain(node); + if (head.last_external_token.ptr) + ts_subtree_retain(head.last_external_token); + return (StackVersion)(self->heads.size - 1); } -static void ts_stack__add_slice( - Stack *self, - StackVersion original_version, - StackNode *node, - SubtreeArray *subtrees -) { - for (uint32_t i = self->slices.size - 1; i + 1 > 0; i--) { - StackVersion version = self->slices.contents[i].version; - if (self->heads.contents[version].node == node) { - StackSlice slice = {*subtrees, version}; - array_insert(&self->slices, i + 1, slice); - return; - } - } +static void ts_stack__add_slice(Stack *self, StackVersion original_version, StackNode *node, SubtreeArray *subtrees) +{ + for (uint32_t i = self->slices.size - 1; i + 1 > 0; i--) + { + StackVersion version = self->slices.contents[i].version; + if (self->heads.contents[version].node == node) + { + StackSlice slice = {*subtrees, version}; + array_insert(&self->slices, i + 1, slice); + return; + } + } - StackVersion version = ts_stack__add_version(self, original_version, node); - StackSlice slice = { *subtrees, version }; - array_push(&self->slices, slice); + StackVersion version = ts_stack__add_version(self, original_version, node); + StackSlice slice = {*subtrees, version}; + array_push(&self->slices, slice); } -static StackSliceArray stack__iter( - Stack *self, - StackVersion version, - StackCallback callback, - void *payload, - int goal_subtree_count -) { - array_clear(&self->slices); - array_clear(&self->iterators); +static StackSliceArray stack__iter(Stack *self, StackVersion version, StackCallback callback, void *payload, int goal_subtree_count) +{ + array_clear(&self->slices); + array_clear(&self->iterators); - StackHead *head = array_get(&self->heads, version); - StackIterator new_iterator = { - .node = head->node, - .subtrees = array_new(), - .subtree_count = 0, - .is_pending = true, - }; + StackHead *head = array_get(&self->heads, version); + StackIterator new_iterator = { + .node = head->node, + .subtrees = array_new(), + .subtree_count = 0, + .is_pending = true, + }; - bool include_subtrees = false; - if (goal_subtree_count >= 0) { - include_subtrees = true; - array_reserve(&new_iterator.subtrees, (uint32_t)ts_subtree_alloc_size(goal_subtree_count) / sizeof(Subtree)); - } + bool include_subtrees = false; + if (goal_subtree_count >= 0) + { + include_subtrees = true; + array_reserve(&new_iterator.subtrees, (uint32_t)ts_subtree_alloc_size(goal_subtree_count) / sizeof(Subtree)); + } - array_push(&self->iterators, new_iterator); + array_push(&self->iterators, new_iterator); - while (self->iterators.size > 0) { - for (uint32_t i = 0, size = self->iterators.size; i < size; i++) { - StackIterator *iterator = &self->iterators.contents[i]; - StackNode *node = iterator->node; + while (self->iterators.size > 0) + { + for (uint32_t i = 0, size = self->iterators.size; i < size; i++) + { + StackIterator *iterator = &self->iterators.contents[i]; + StackNode *node = iterator->node; - StackAction action = callback(payload, iterator); - bool should_pop = action & StackActionPop; - bool should_stop = action & StackActionStop || node->link_count == 0; + StackAction action = callback(payload, iterator); + bool should_pop = action & StackActionPop; + bool should_stop = action & StackActionStop || node->link_count == 0; - if (should_pop) { - SubtreeArray subtrees = iterator->subtrees; - if (!should_stop) { - ts_subtree_array_copy(subtrees, &subtrees); - } - ts_subtree_array_reverse(&subtrees); - ts_stack__add_slice( - self, - version, - node, - &subtrees - ); - } + if (should_pop) + { + SubtreeArray subtrees = iterator->subtrees; + if (!should_stop) + { + ts_subtree_array_copy(subtrees, &subtrees); + } + ts_subtree_array_reverse(&subtrees); + ts_stack__add_slice(self, version, node, &subtrees); + } - if (should_stop) { - if (!should_pop) { - ts_subtree_array_delete(self->subtree_pool, &iterator->subtrees); - } - array_erase(&self->iterators, i); - i--, size--; - continue; - } + if (should_stop) + { + if (!should_pop) + { + ts_subtree_array_delete(self->subtree_pool, &iterator->subtrees); + } + array_erase(&self->iterators, i); + i--, size--; + continue; + } - for (uint32_t j = 1; j <= node->link_count; j++) { - StackIterator *next_iterator; - StackLink link; - if (j == node->link_count) { - link = node->links[0]; - next_iterator = &self->iterators.contents[i]; - } else { - if (self->iterators.size >= MAX_ITERATOR_COUNT) continue; - link = node->links[j]; - StackIterator current_iterator = self->iterators.contents[i]; - array_push(&self->iterators, current_iterator); - next_iterator = array_back(&self->iterators); - ts_subtree_array_copy(next_iterator->subtrees, &next_iterator->subtrees); - } + for (uint32_t j = 1; j <= node->link_count; j++) + { + StackIterator *next_iterator; + StackLink link; + if (j == node->link_count) + { + link = node->links[0]; + next_iterator = &self->iterators.contents[i]; + } + else + { + if (self->iterators.size >= MAX_ITERATOR_COUNT) + continue; + link = node->links[j]; + StackIterator current_iterator = self->iterators.contents[i]; + array_push(&self->iterators, current_iterator); + next_iterator = array_back(&self->iterators); + ts_subtree_array_copy(next_iterator->subtrees, &next_iterator->subtrees); + } - next_iterator->node = link.node; - if (link.subtree.ptr) { - if (include_subtrees) { - array_push(&next_iterator->subtrees, link.subtree); - ts_subtree_retain(link.subtree); - } + next_iterator->node = link.node; + if (link.subtree.ptr) + { + if (include_subtrees) + { + array_push(&next_iterator->subtrees, link.subtree); + ts_subtree_retain(link.subtree); + } - if (!ts_subtree_extra(link.subtree)) { - next_iterator->subtree_count++; - if (!link.is_pending) { - next_iterator->is_pending = false; - } - } - } else { - next_iterator->subtree_count++; - next_iterator->is_pending = false; - } - } - } - } + if (!ts_subtree_extra(link.subtree)) + { + next_iterator->subtree_count++; + if (!link.is_pending) + { + next_iterator->is_pending = false; + } + } + } + else + { + next_iterator->subtree_count++; + next_iterator->is_pending = false; + } + } + } + } - return self->slices; + return self->slices; } -Stack *ts_stack_new(SubtreePool *subtree_pool) { - Stack *self = mem_alloc_array(1, sizeof(Stack)); +Stack *ts_stack_new(SubtreePool *subtree_pool) +{ + Stack *self = mem_alloc_array(1, sizeof(Stack)); - array_init(&self->heads); - array_init(&self->slices); - array_init(&self->iterators); - array_init(&self->node_pool); - array_reserve(&self->heads, 4); - array_reserve(&self->slices, 4); - array_reserve(&self->iterators, 4); - array_reserve(&self->node_pool, MAX_NODE_POOL_SIZE); + array_init(&self->heads); + array_init(&self->slices); + array_init(&self->iterators); + array_init(&self->node_pool); + array_reserve(&self->heads, 4); + array_reserve(&self->slices, 4); + array_reserve(&self->iterators, 4); + array_reserve(&self->node_pool, MAX_NODE_POOL_SIZE); - self->subtree_pool = subtree_pool; - self->base_node = stack_node_new(NULL, NULL_SUBTREE, false, 1, &self->node_pool); - ts_stack_clear(self); + self->subtree_pool = subtree_pool; + self->base_node = stack_node_new(NULL, NULL_SUBTREE, false, 1, &self->node_pool); + ts_stack_clear(self); - return self; + return self; } -void ts_stack_delete(Stack *self) { - if (self->slices.contents) - array_delete(&self->slices); - if (self->iterators.contents) - array_delete(&self->iterators); - stack_node_release(self->base_node, &self->node_pool, self->subtree_pool); - for (uint32_t i = 0; i < self->heads.size; i++) { - stack_head_delete(&self->heads.contents[i], &self->node_pool, self->subtree_pool); - } - array_clear(&self->heads); - if (self->node_pool.contents) { - for (uint32_t i = 0; i < self->node_pool.size; i++) - mem_free(self->node_pool.contents[i]); - array_delete(&self->node_pool); - } - array_delete(&self->heads); - mem_free(self); +void ts_stack_delete(Stack *self) +{ + if (self->slices.contents) + array_delete(&self->slices); + if (self->iterators.contents) + array_delete(&self->iterators); + stack_node_release(self->base_node, &self->node_pool, self->subtree_pool); + for (uint32_t i = 0; i < self->heads.size; i++) + { + stack_head_delete(&self->heads.contents[i], &self->node_pool, self->subtree_pool); + } + array_clear(&self->heads); + if (self->node_pool.contents) + { + for (uint32_t i = 0; i < self->node_pool.size; i++) + mem_free(self->node_pool.contents[i]); + array_delete(&self->node_pool); + } + array_delete(&self->heads); + mem_free(self); } -uint32_t ts_stack_version_count(const Stack *self) { - return self->heads.size; +uint32_t ts_stack_version_count(const Stack *self) +{ + return self->heads.size; } -TSStateId ts_stack_state(const Stack *self, StackVersion version) { - return array_get(&self->heads, version)->node->state; +TSStateId ts_stack_state(const Stack *self, StackVersion version) +{ + return array_get(&self->heads, version)->node->state; } -Length ts_stack_position(const Stack *self, StackVersion version) { - return array_get(&self->heads, version)->node->position; +Length ts_stack_position(const Stack *self, StackVersion version) +{ + return array_get(&self->heads, version)->node->position; } -Subtree ts_stack_last_external_token(const Stack *self, StackVersion version) { - return array_get(&self->heads, version)->last_external_token; +Subtree ts_stack_last_external_token(const Stack *self, StackVersion version) +{ + return array_get(&self->heads, version)->last_external_token; } -void ts_stack_set_last_external_token(Stack *self, StackVersion version, Subtree token) { - StackHead *head = array_get(&self->heads, version); - if (token.ptr) ts_subtree_retain(token); - if (head->last_external_token.ptr) ts_subtree_release(self->subtree_pool, head->last_external_token); - head->last_external_token = token; +void ts_stack_set_last_external_token(Stack *self, StackVersion version, Subtree token) +{ + StackHead *head = array_get(&self->heads, version); + if (token.ptr) + ts_subtree_retain(token); + if (head->last_external_token.ptr) + ts_subtree_release(self->subtree_pool, head->last_external_token); + head->last_external_token = token; } -unsigned ts_stack_error_cost(const Stack *self, StackVersion version) { - StackHead *head = array_get(&self->heads, version); - unsigned result = head->node->error_cost; - if ( - head->status == StackStatusPaused || - (head->node->state == ERROR_STATE && !head->node->links[0].subtree.ptr)) { - result += ERROR_COST_PER_RECOVERY; - } - return result; +unsigned ts_stack_error_cost(const Stack *self, StackVersion version) +{ + StackHead *head = array_get(&self->heads, version); + unsigned result = head->node->error_cost; + if (head->status == StackStatusPaused || (head->node->state == ERROR_STATE && !head->node->links[0].subtree.ptr)) + { + result += ERROR_COST_PER_RECOVERY; + } + return result; } -unsigned ts_stack_node_count_since_error(const Stack *self, StackVersion version) { - StackHead *head = array_get(&self->heads, version); - if (head->node->node_count < head->node_count_at_last_error) { - head->node_count_at_last_error = head->node->node_count; - } - return head->node->node_count - head->node_count_at_last_error; +unsigned ts_stack_node_count_since_error(const Stack *self, StackVersion version) +{ + StackHead *head = array_get(&self->heads, version); + if (head->node->node_count < head->node_count_at_last_error) + { + head->node_count_at_last_error = head->node->node_count; + } + return head->node->node_count - head->node_count_at_last_error; } -void ts_stack_push( - Stack *self, - StackVersion version, - Subtree subtree, - bool pending, - TSStateId state -) { - StackHead *head = array_get(&self->heads, version); - StackNode *new_node = stack_node_new(head->node, subtree, pending, state, &self->node_pool); - if (!subtree.ptr) head->node_count_at_last_error = new_node->node_count; - head->node = new_node; +void ts_stack_push(Stack *self, StackVersion version, Subtree subtree, bool pending, TSStateId state) +{ + StackHead *head = array_get(&self->heads, version); + StackNode *new_node = stack_node_new(head->node, subtree, pending, state, &self->node_pool); + if (!subtree.ptr) + head->node_count_at_last_error = new_node->node_count; + head->node = new_node; } -forceinline StackAction pop_count_callback(void *payload, const StackIterator *iterator) { - unsigned *goal_subtree_count = payload; - if (iterator->subtree_count == *goal_subtree_count) { - return StackActionPop | StackActionStop; - } else { - return StackActionNone; - } +StackAction pop_count_callback(void *payload, const StackIterator *iterator) +{ + unsigned *goal_subtree_count = payload; + if (iterator->subtree_count == *goal_subtree_count) + { + return StackActionPop | StackActionStop; + } + else + { + return StackActionNone; + } } -StackSliceArray ts_stack_pop_count(Stack *self, StackVersion version, uint32_t count) { - return stack__iter(self, version, pop_count_callback, &count, (int)count); +StackSliceArray ts_stack_pop_count(Stack *self, StackVersion version, uint32_t count) +{ + return stack__iter(self, version, pop_count_callback, &count, (int)count); } -forceinline StackAction pop_pending_callback(void *payload, const StackIterator *iterator) { - (void)payload; - if (iterator->subtree_count >= 1) { - if (iterator->is_pending) { - return StackActionPop | StackActionStop; - } else { - return StackActionStop; - } - } else { - return StackActionNone; - } +StackAction pop_pending_callback(void *payload, const StackIterator *iterator) +{ + (void)payload; + if (iterator->subtree_count >= 1) + { + if (iterator->is_pending) + { + return StackActionPop | StackActionStop; + } + else + { + return StackActionStop; + } + } + else + { + return StackActionNone; + } } -StackSliceArray ts_stack_pop_pending(Stack *self, StackVersion version) { - StackSliceArray pop = stack__iter(self, version, pop_pending_callback, NULL, 0); - if (pop.size > 0) { - ts_stack_renumber_version(self, pop.contents[0].version, version); - pop.contents[0].version = version; - } - return pop; +StackSliceArray ts_stack_pop_pending(Stack *self, StackVersion version) +{ + StackSliceArray pop = stack__iter(self, version, pop_pending_callback, NULL, 0); + if (pop.size > 0) + { + ts_stack_renumber_version(self, pop.contents[0].version, version); + pop.contents[0].version = version; + } + return pop; } -forceinline StackAction pop_error_callback(void *payload, const StackIterator *iterator) { - if (iterator->subtrees.size > 0) { - bool *found_error = payload; - if (!*found_error && ts_subtree_is_error(iterator->subtrees.contents[0])) { - *found_error = true; - return StackActionPop | StackActionStop; - } else { - return StackActionStop; - } - } else { - return StackActionNone; - } +StackAction pop_error_callback(void *payload, const StackIterator *iterator) +{ + if (iterator->subtrees.size > 0) + { + bool *found_error = payload; + if (!*found_error && ts_subtree_is_error(iterator->subtrees.contents[0])) + { + *found_error = true; + return StackActionPop | StackActionStop; + } + else + { + return StackActionStop; + } + } + else + { + return StackActionNone; + } } -SubtreeArray ts_stack_pop_error(Stack *self, StackVersion version) { - StackNode *node = array_get(&self->heads, version)->node; - for (unsigned i = 0; i < node->link_count; i++) { - if (node->links[i].subtree.ptr && ts_subtree_is_error(node->links[i].subtree)) { - bool found_error = false; - StackSliceArray pop = stack__iter(self, version, pop_error_callback, &found_error, 1); - if (pop.size > 0) { - assert(pop.size == 1); - ts_stack_renumber_version(self, pop.contents[0].version, version); - return pop.contents[0].subtrees; - } - break; - } - } - return (SubtreeArray) {.size = 0}; +SubtreeArray ts_stack_pop_error(Stack *self, StackVersion version) +{ + StackNode *node = array_get(&self->heads, version)->node; + for (unsigned i = 0; i < node->link_count; i++) + { + if (node->links[i].subtree.ptr && ts_subtree_is_error(node->links[i].subtree)) + { + bool found_error = false; + StackSliceArray pop = stack__iter(self, version, pop_error_callback, &found_error, 1); + if (pop.size > 0) + { + assert(pop.size == 1); + ts_stack_renumber_version(self, pop.contents[0].version, version); + return pop.contents[0].subtrees; + } + break; + } + } + return (SubtreeArray){.size = 0}; } -forceinline StackAction pop_all_callback(void *payload, const StackIterator *iterator) { - (void)payload; - return iterator->node->link_count == 0 ? StackActionPop : StackActionNone; +StackAction pop_all_callback(void *payload, const StackIterator *iterator) +{ + (void)payload; + return iterator->node->link_count == 0 ? StackActionPop : StackActionNone; } -StackSliceArray ts_stack_pop_all(Stack *self, StackVersion version) { - return stack__iter(self, version, pop_all_callback, NULL, 0); +StackSliceArray ts_stack_pop_all(Stack *self, StackVersion version) +{ + return stack__iter(self, version, pop_all_callback, NULL, 0); } -typedef struct { - StackSummary *summary; - unsigned max_depth; +typedef struct SummarizeStackSession +{ + StackSummary *summary; + unsigned max_depth; } SummarizeStackSession; -forceinline StackAction summarize_stack_callback(void *payload, const StackIterator *iterator) { - SummarizeStackSession *session = payload; - TSStateId state = iterator->node->state; - unsigned depth = iterator->subtree_count; - if (depth > session->max_depth) return StackActionStop; - for (unsigned i = session->summary->size - 1; i + 1 > 0; i--) { - StackSummaryEntry entry = session->summary->contents[i]; - if (entry.depth < depth) break; - if (entry.depth == depth && entry.state == state) return StackActionNone; - } - array_push(session->summary, ((StackSummaryEntry) { - .position = iterator->node->position, - .depth = depth, - .state = state, - })); - return StackActionNone; +StackAction summarize_stack_callback(void *payload, const StackIterator *iterator) +{ + SummarizeStackSession *session = payload; + TSStateId state = iterator->node->state; + unsigned depth = iterator->subtree_count; + if (depth > session->max_depth) + return StackActionStop; + for (unsigned i = session->summary->size - 1; i + 1 > 0; i--) + { + StackSummaryEntry entry = session->summary->contents[i]; + if (entry.depth < depth) + break; + if (entry.depth == depth && entry.state == state) + return StackActionNone; + } + array_push(session->summary, ((StackSummaryEntry){ + .position = iterator->node->position, + .depth = depth, + .state = state, + })); + return StackActionNone; } -void ts_stack_record_summary(Stack *self, StackVersion version, unsigned max_depth) { - SummarizeStackSession session = { - .summary = mem_alloc(sizeof(StackSummary)), - .max_depth = max_depth - }; - array_init(session.summary); - stack__iter(self, version, summarize_stack_callback, &session, -1); - StackHead *head = &self->heads.contents[version]; - if (head->summary) { - array_delete(head->summary); - mem_free(head->summary); - } - head->summary = session.summary; +void ts_stack_record_summary(Stack *self, StackVersion version, unsigned max_depth) +{ + SummarizeStackSession session = {.summary = mem_alloc(sizeof(StackSummary)), .max_depth = max_depth}; + array_init(session.summary); + stack__iter(self, version, summarize_stack_callback, &session, -1); + StackHead *head = &self->heads.contents[version]; + if (head->summary) + { + array_delete(head->summary); + mem_free(head->summary); + } + head->summary = session.summary; } -StackSummary *ts_stack_get_summary(Stack *self, StackVersion version) { - return array_get(&self->heads, version)->summary; +StackSummary *ts_stack_get_summary(Stack *self, StackVersion version) +{ + return array_get(&self->heads, version)->summary; } -int ts_stack_dynamic_precedence(Stack *self, StackVersion version) { - return array_get(&self->heads, version)->node->dynamic_precedence; +int ts_stack_dynamic_precedence(Stack *self, StackVersion version) +{ + return array_get(&self->heads, version)->node->dynamic_precedence; } -bool ts_stack_has_advanced_since_error(const Stack *self, StackVersion version) { - const StackHead *head = array_get(&self->heads, version); - const StackNode *node = head->node; - if (node->error_cost == 0) return true; - while (node) { - if (node->link_count > 0) { - Subtree subtree = node->links[0].subtree; - if (subtree.ptr) { - if (ts_subtree_total_bytes(subtree) > 0) { - return true; - } else if ( - node->node_count > head->node_count_at_last_error && - ts_subtree_error_cost(subtree) == 0 - ) { - node = node->links[0].node; - continue; - } - } - } - break; - } - return false; +bool ts_stack_has_advanced_since_error(const Stack *self, StackVersion version) +{ + const StackHead *head = array_get(&self->heads, version); + const StackNode *node = head->node; + if (node->error_cost == 0) + return true; + while (node) + { + if (node->link_count > 0) + { + Subtree subtree = node->links[0].subtree; + if (subtree.ptr) + { + if (ts_subtree_total_bytes(subtree) > 0) + { + return true; + } + else if (node->node_count > head->node_count_at_last_error && ts_subtree_error_cost(subtree) == 0) + { + node = node->links[0].node; + continue; + } + } + } + break; + } + return false; } -void ts_stack_remove_version(Stack *self, StackVersion version) { - stack_head_delete(array_get(&self->heads, version), &self->node_pool, self->subtree_pool); - array_erase(&self->heads, version); +void ts_stack_remove_version(Stack *self, StackVersion version) +{ + stack_head_delete(array_get(&self->heads, version), &self->node_pool, self->subtree_pool); + array_erase(&self->heads, version); } -void ts_stack_renumber_version(Stack *self, StackVersion v1, StackVersion v2) { - if (v1 == v2) return; - assert(v2 < v1); - assert((uint32_t)v1 < self->heads.size); - StackHead *source_head = &self->heads.contents[v1]; - StackHead *target_head = &self->heads.contents[v2]; - if (target_head->summary && !source_head->summary) { - source_head->summary = target_head->summary; - target_head->summary = NULL; - } - stack_head_delete(target_head, &self->node_pool, self->subtree_pool); - *target_head = *source_head; - array_erase(&self->heads, v1); +void ts_stack_renumber_version(Stack *self, StackVersion v1, StackVersion v2) +{ + if (v1 == v2) + return; + assert(v2 < v1); + assert((uint32_t)v1 < self->heads.size); + StackHead *source_head = &self->heads.contents[v1]; + StackHead *target_head = &self->heads.contents[v2]; + if (target_head->summary && !source_head->summary) + { + source_head->summary = target_head->summary; + target_head->summary = NULL; + } + stack_head_delete(target_head, &self->node_pool, self->subtree_pool); + *target_head = *source_head; + array_erase(&self->heads, v1); } -void ts_stack_swap_versions(Stack *self, StackVersion v1, StackVersion v2) { - StackHead temporary_head = self->heads.contents[v1]; - self->heads.contents[v1] = self->heads.contents[v2]; - self->heads.contents[v2] = temporary_head; +void ts_stack_swap_versions(Stack *self, StackVersion v1, StackVersion v2) +{ + StackHead temporary_head = self->heads.contents[v1]; + self->heads.contents[v1] = self->heads.contents[v2]; + self->heads.contents[v2] = temporary_head; } -StackVersion ts_stack_copy_version(Stack *self, StackVersion version) { - assert(version < self->heads.size); - array_push(&self->heads, self->heads.contents[version]); - StackHead *head = array_back(&self->heads); - stack_node_retain(head->node); - if (head->last_external_token.ptr) ts_subtree_retain(head->last_external_token); - head->summary = NULL; - return self->heads.size - 1; +StackVersion ts_stack_copy_version(Stack *self, StackVersion version) +{ + assert(version < self->heads.size); + array_push(&self->heads, self->heads.contents[version]); + StackHead *head = array_back(&self->heads); + stack_node_retain(head->node); + if (head->last_external_token.ptr) + ts_subtree_retain(head->last_external_token); + head->summary = NULL; + return self->heads.size - 1; } -bool ts_stack_merge(Stack *self, StackVersion version1, StackVersion version2) { - if (!ts_stack_can_merge(self, version1, version2)) return false; - StackHead *head1 = &self->heads.contents[version1]; - StackHead *head2 = &self->heads.contents[version2]; - for (uint32_t i = 0; i < head2->node->link_count; i++) { - stack_node_add_link(head1->node, head2->node->links[i], self->subtree_pool); - } - if (head1->node->state == ERROR_STATE) { - head1->node_count_at_last_error = head1->node->node_count; - } - ts_stack_remove_version(self, version2); - return true; +bool ts_stack_merge(Stack *self, StackVersion version1, StackVersion version2) +{ + if (!ts_stack_can_merge(self, version1, version2)) + return false; + StackHead *head1 = &self->heads.contents[version1]; + StackHead *head2 = &self->heads.contents[version2]; + for (uint32_t i = 0; i < head2->node->link_count; i++) + { + stack_node_add_link(head1->node, head2->node->links[i], self->subtree_pool); + } + if (head1->node->state == ERROR_STATE) + { + head1->node_count_at_last_error = head1->node->node_count; + } + ts_stack_remove_version(self, version2); + return true; } -bool ts_stack_can_merge(Stack *self, StackVersion version1, StackVersion version2) { - StackHead *head1 = &self->heads.contents[version1]; - StackHead *head2 = &self->heads.contents[version2]; - return - head1->status == StackStatusActive && - head2->status == StackStatusActive && - head1->node->state == head2->node->state && - head1->node->position.bytes == head2->node->position.bytes && - head1->node->error_cost == head2->node->error_cost && - ts_subtree_external_scanner_state_eq(head1->last_external_token, head2->last_external_token); +bool ts_stack_can_merge(Stack *self, StackVersion version1, StackVersion version2) +{ + StackHead *head1 = &self->heads.contents[version1]; + StackHead *head2 = &self->heads.contents[version2]; + return head1->status == StackStatusActive && head2->status == StackStatusActive && head1->node->state == head2->node->state && + head1->node->position.bytes == head2->node->position.bytes && head1->node->error_cost == head2->node->error_cost && + ts_subtree_external_scanner_state_eq(head1->last_external_token, head2->last_external_token); } -void ts_stack_halt(Stack *self, StackVersion version) { - array_get(&self->heads, version)->status = StackStatusHalted; +void ts_stack_halt(Stack *self, StackVersion version) +{ + array_get(&self->heads, version)->status = StackStatusHalted; } -void ts_stack_pause(Stack *self, StackVersion version, Subtree lookahead) { - StackHead *head = array_get(&self->heads, version); - head->status = StackStatusPaused; - head->lookahead_when_paused = lookahead; - head->node_count_at_last_error = head->node->node_count; +void ts_stack_pause(Stack *self, StackVersion version, Subtree lookahead) +{ + StackHead *head = array_get(&self->heads, version); + head->status = StackStatusPaused; + head->lookahead_when_paused = lookahead; + head->node_count_at_last_error = head->node->node_count; } -bool ts_stack_is_active(const Stack *self, StackVersion version) { - return array_get(&self->heads, version)->status == StackStatusActive; +bool ts_stack_is_active(const Stack *self, StackVersion version) +{ + return array_get(&self->heads, version)->status == StackStatusActive; } -bool ts_stack_is_halted(const Stack *self, StackVersion version) { - return array_get(&self->heads, version)->status == StackStatusHalted; +bool ts_stack_is_halted(const Stack *self, StackVersion version) +{ + return array_get(&self->heads, version)->status == StackStatusHalted; } -bool ts_stack_is_paused(const Stack *self, StackVersion version) { - return array_get(&self->heads, version)->status == StackStatusPaused; +bool ts_stack_is_paused(const Stack *self, StackVersion version) +{ + return array_get(&self->heads, version)->status == StackStatusPaused; } -Subtree ts_stack_resume(Stack *self, StackVersion version) { - StackHead *head = array_get(&self->heads, version); - assert(head->status == StackStatusPaused); - Subtree result = head->lookahead_when_paused; - head->status = StackStatusActive; - head->lookahead_when_paused = NULL_SUBTREE; - return result; +Subtree ts_stack_resume(Stack *self, StackVersion version) +{ + StackHead *head = array_get(&self->heads, version); + assert(head->status == StackStatusPaused); + Subtree result = head->lookahead_when_paused; + head->status = StackStatusActive; + head->lookahead_when_paused = NULL_SUBTREE; + return result; } -void ts_stack_clear(Stack *self) { - stack_node_retain(self->base_node); - for (uint32_t i = 0; i < self->heads.size; i++) { - stack_head_delete(&self->heads.contents[i], &self->node_pool, self->subtree_pool); - } - array_clear(&self->heads); - array_push(&self->heads, ((StackHead) { - .node = self->base_node, - .status = StackStatusActive, - .last_external_token = NULL_SUBTREE, - .lookahead_when_paused = NULL_SUBTREE, - })); +void ts_stack_clear(Stack *self) +{ + stack_node_retain(self->base_node); + for (uint32_t i = 0; i < self->heads.size; i++) + { + stack_head_delete(&self->heads.contents[i], &self->node_pool, self->subtree_pool); + } + array_clear(&self->heads); + array_push(&self->heads, ((StackHead){ + .node = self->base_node, + .status = StackStatusActive, + .last_external_token = NULL_SUBTREE, + .lookahead_when_paused = NULL_SUBTREE, + })); } -// bool ts_stack_print_dot_graph(Stack *self, const TSLanguage *language, FILE *f) { -// array_reserve(&self->iterators, 32); -// if (!f) f = stderr; - -// fprintf(f, "digraph stack {\n"); -// fprintf(f, "rankdir=\"RL\";\n"); -// fprintf(f, "edge [arrowhead=none]\n"); - -// Array(StackNode *) visited_nodes = array_new(); - -// array_clear(&self->iterators); -// for (uint32_t i = 0; i < self->heads.size; i++) { -// StackHead *head = &self->heads.contents[i]; -// if (head->status == StackStatusHalted) continue; - -// fprintf(f, "node_head_%u [shape=none, label=\"\"]\n", i); -// fprintf(f, "node_head_%u -> node_%p [", i, (void *)head->node); - -// if (head->status == StackStatusPaused) { -// fprintf(f, "color=red "); -// } -// fprintf(f, -// "label=%u, fontcolor=blue, weight=10000, labeltooltip=\"node_count: %u\nerror_cost: %u", -// i, -// ts_stack_node_count_since_error(self, i), -// ts_stack_error_cost(self, i) -// ); - -// if (head->summary) { -// fprintf(f, "\nsummary:"); -// for (uint32_t j = 0; j < head->summary->size; j++) fprintf(f, " %u", head->summary->contents[j].state); -// } - -// if (head->last_external_token.ptr) { -// const ExternalScannerState *state = &head->last_external_token.ptr->external_scanner_state; -// const char *data = ts_external_scanner_state_data(state); -// fprintf(f, "\nexternal_scanner_state:"); -// for (uint32_t j = 0; j < state->length; j++) fprintf(f, " %2X", data[j]); -// } - -// fprintf(f, "\"]\n"); -// array_push(&self->iterators, ((StackIterator) { -// .node = head->node -// })); -// } - -// bool all_iterators_done = false; -// while (!all_iterators_done) { -// all_iterators_done = true; - -// for (uint32_t i = 0; i < self->iterators.size; i++) { -// StackIterator iterator = self->iterators.contents[i]; -// StackNode *node = iterator.node; - -// for (uint32_t j = 0; j < visited_nodes.size; j++) { -// if (visited_nodes.contents[j] == node) { -// node = NULL; -// break; -// } -// } - -// if (!node) continue; -// all_iterators_done = false; - -// fprintf(f, "node_%p [", (void *)node); -// if (node->state == ERROR_STATE) { -// fprintf(f, "label=\"?\""); -// } else if ( -// node->link_count == 1 && -// node->links[0].subtree.ptr && -// ts_subtree_extra(node->links[0].subtree) -// ) { -// fprintf(f, "shape=point margin=0 label=\"\""); -// } else { -// fprintf(f, "label=\"%d\"", node->state); -// } - -// fprintf( -// f, -// " tooltip=\"position: %u,%u\nnode_count:%u\nerror_cost: %u\ndynamic_precedence: %d\"];\n", -// node->position.extent.row + 1, -// node->position.extent.column, -// node->node_count, -// node->error_cost, -// node->dynamic_precedence -// ); - -// for (int j = 0; j < node->link_count; j++) { -// StackLink link = node->links[j]; -// fprintf(f, "node_%p -> node_%p [", (void *)node, (void *)link.node); -// if (link.is_pending) fprintf(f, "style=dashed "); -// if (link.subtree.ptr && ts_subtree_extra(link.subtree)) fprintf(f, "fontcolor=gray "); - -// if (!link.subtree.ptr) { -// fprintf(f, "color=red"); -// } else { -// fprintf(f, "label=\""); -// bool quoted = ts_subtree_visible(link.subtree) && !ts_subtree_named(link.subtree); -// if (quoted) fprintf(f, "'"); -// ts_language_write_symbol_as_dot_string(language, f, ts_subtree_symbol(link.subtree)); -// if (quoted) fprintf(f, "'"); -// fprintf(f, "\""); -// fprintf( -// f, -// "labeltooltip=\"error_cost: %u\ndynamic_precedence: %" PRId32 "\"", -// ts_subtree_error_cost(link.subtree), -// ts_subtree_dynamic_precedence(link.subtree) -// ); -// } - -// fprintf(f, "];\n"); - -// StackIterator *next_iterator; -// if (j == 0) { -// next_iterator = &self->iterators.contents[i]; -// } else { -// array_push(&self->iterators, iterator); -// next_iterator = array_back(&self->iterators); -// } -// next_iterator->node = link.node; -// } - -// array_push(&visited_nodes, node); -// } -// } - -// fprintf(f, "}\n"); - -// array_delete(&visited_nodes); -// return true; -// } - #undef forceinline diff --git a/parser/src/stack.h b/parser/src/stack.h index 11c005e5..269a5faa 100644 --- a/parser/src/stack.h +++ b/parser/src/stack.h @@ -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 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_