diff --git a/.github/workflows/tree-sitter.yml b/.github/workflows/tree-sitter.yml new file mode 100644 index 00000000..373fc5a2 --- /dev/null +++ b/.github/workflows/tree-sitter.yml @@ -0,0 +1,38 @@ +# The tree-sitter grammar (tooling/tree-sitter-rx): src/ is generated but committed, since Zed compiles src/parser.c +# as it is, so check it is what grammar.js generates; then the corpus, and every tracked .rx file with no ERROR or +# MISSING node. +name: tree-sitter + +on: + push: + branches: [main] + paths: ["tooling/tree-sitter-rx/**", "**/*.rx", ".github/workflows/tree-sitter.yml"] + pull_request: + paths: ["tooling/tree-sitter-rx/**", "**/*.rx", ".github/workflows/tree-sitter.yml"] + workflow_dispatch: + +jobs: + grammar: + runs-on: ubuntu-latest + defaults: + run: + working-directory: tooling/tree-sitter-rx + steps: + - uses: actions/checkout@v5 + - uses: pnpm/action-setup@v4 + with: + version: 11.1.1 # as editor.yml + - uses: actions/setup-node@v5 + with: + node-version: 22 + - run: pnpm install --frozen-lockfile + - name: src/ is what grammar.js generates + run: | + pnpm run generate + git diff --exit-code -- src/ + - name: Corpus + run: pnpm test + - name: Every tracked .rx file + run: pnpm run parse:repo + - name: Queries + run: pnpm run query -- ../../examples/*.rx > /dev/null diff --git a/tooling/tree-sitter-rx/.gitattributes b/tooling/tree-sitter-rx/.gitattributes new file mode 100644 index 00000000..b58e2a54 --- /dev/null +++ b/tooling/tree-sitter-rx/.gitattributes @@ -0,0 +1,4 @@ +src/parser.c linguist-generated +src/grammar.json linguist-generated +src/node-types.json linguist-generated +src/tree_sitter/** linguist-generated diff --git a/tooling/tree-sitter-rx/.gitignore b/tooling/tree-sitter-rx/.gitignore new file mode 100644 index 00000000..04b019fc --- /dev/null +++ b/tooling/tree-sitter-rx/.gitignore @@ -0,0 +1,9 @@ +node_modules/ +build/ +*.wasm +*.so +*.dylib +*.dll +*.o +log.html +.rx-files diff --git a/tooling/tree-sitter-rx/README.md b/tooling/tree-sitter-rx/README.md new file mode 100644 index 00000000..26806792 --- /dev/null +++ b/tooling/tree-sitter-rx/README.md @@ -0,0 +1,55 @@ +# tree-sitter-rx + +A [tree-sitter](https://tree-sitter.github.io) grammar for `.rx` (and `.d.rx`), +for editors that highlight and navigate with tree-sitter: Zed (`../zed-rx`), +Neovim, Helix. It is not the language: `compiler/src/parse.rs` is, and this +grammar follows it. Where the two disagree, `parse.rs` is right and this is a +bug. + +`grammar.js` extends tree-sitter-typescript's TSX grammar +(`grammar(tsx, { … })`, as TypeScript's extends JavaScript's). Expressions, +types and JSX keep TSX's node names, so a TSX query applies to them unchanged. +What `.rx` adds has nodes of its own: + +| | nodes | +| --- | --- | +| module | `import_statement` (`side`, `pure`, `fails_clause`), `export_statement` | +| declarations | `source_declaration`, `equation`, `key_clause`, `fn_declaration` (`fn_body`), `fn_signature` (`.d.rx`, `rejects_clause`), `server_function_declaration`, `action_declaration`, `component_declaration`, `effect_declaration`, `error_declaration`, `context_declaration`, `provide_declaration` | +| markup | `view_block` (a reactive block: declarations, then markup), `if_block`, `else_block`, `for_block`, `loading_block`, `errored_block`, `portal_block`, `style_element` (`raw_text`) | +| statements | `write_statement` (`x := e`), `guess_statement`, `refresh_statement`, `affects_statement`, `action_statement` | +| expressions | `pending_expression`, `latest_expression`, `peek_expression`, `pre_expression`, `use_expression`, `throw_expression`, `catch_expression`, `draft_expression`, `fn_expression`, `server_fn_expression`; `await_expression` has `initial` (`-> v`) | + +A declaration's name is its `name` field, its right side `value`, a block +`body`; a write's target is `target`. Imperative code (handlers, `action` and +`effect` bodies) is TSX's `statement_block`. TSX's assignment expressions are +gone: a statement-level `x = e` is an `equation`. + +`src/scanner.c` is tree-sitter-typescript's external scanner with the changes +`.rx` needs, each marked `rx:`: a newline ends a statement before `<`, `(`, `[` +and a template (`parse.rs` reads none of them across a line) but not before +`catch`; on one line, a write (`a := 1 b := 2`) starts a new statement, as +`parse.rs` reads statements back to back; and a ``. + +## Regenerating and testing + +```sh +pnpm install +pnpm run generate # src/parser.c, src/grammar.json, src/node-types.json +pnpm test # test/corpus +pnpm run parse:repo # every tracked .rx file; none should have an ERROR or MISSING node +pnpm run query -- ../../examples/todos.rx +``` + +`src/` is generated but committed: Zed compiles `src/parser.c` and +`src/scanner.c` as they are. Regenerate after changing `grammar.js`, and +commit the result with it. + +## Queries + +`queries/highlights.scm` is tree-sitter-javascript's `highlights.scm` and +`highlights-jsx.scm` and tree-sitter-typescript's `highlights.scm`, then the +`.rx` patterns. Patterns are ordered general first, specific last: a later +pattern wins, as in Zed and Neovim. Helix (and `tree-sitter highlight`) take +the first match, so Helix needs the order reversed. `queries/injections.scm` +parses a `` (CSS-1); as an expression it is a class map (CSS-5) + style_element: $ => seq( + '<', + alias('style', $.identifier), + '>', + optional(alias($._style_text, $.raw_text)), + '', + ), + + // parse.rs reads a `` (CSS-1) +static bool scan_style_text(TSLexer *lexer) { + const char *close = ""; + bool has_content = false; + lexer->result_symbol = STYLE_TEXT; + for (;;) { + lexer->mark_end(lexer); + if (lexer->lookahead == 0) { + return false; + } + if (lexer->lookahead == '<') { + unsigned i = 0; + while (close[i] != 0 && lexer->lookahead == close[i]) { + advance(lexer); + i++; + } + if (close[i] == 0) { + return has_content; + } + has_content = true; + continue; + } + advance(lexer); + has_content = true; + } +} + +static inline bool external_scanner_scan(void *payload, TSLexer *lexer, const bool *valid_symbols) { + if (valid_symbols[STYLE_TEXT] && !valid_symbols[ERROR_RECOVERY]) { + return scan_style_text(lexer); + } + if (valid_symbols[TEMPLATE_CHARS]) { + if (valid_symbols[AUTOMATIC_SEMICOLON]) { + return false; + } + return scan_template_chars(lexer); + } + + if (valid_symbols[JSX_TEXT] && scan_jsx_text(lexer)) { + return true; + } + + if (valid_symbols[AUTOMATIC_SEMICOLON] || valid_symbols[FUNCTION_SIGNATURE_AUTOMATIC_SEMICOLON]) { + bool scanned_comment = false; + bool ret = scan_automatic_semicolon(lexer, valid_symbols, &scanned_comment); + if (!ret && !scanned_comment && valid_symbols[TERNARY_QMARK] && lexer->lookahead == '?') { + return scan_ternary_qmark(lexer); + } + return ret; + } + if (valid_symbols[TERNARY_QMARK]) { + return scan_ternary_qmark(lexer); + } + + if (valid_symbols[HTML_COMMENT] && !valid_symbols[LOGICAL_OR] && !valid_symbols[ESCAPE_SEQUENCE] && + !valid_symbols[REGEX_PATTERN]) { + return scan_closing_comment(lexer); + } + + return false; +} + +void *tree_sitter_rx_external_scanner_create() { return NULL; } + +void tree_sitter_rx_external_scanner_destroy(void *payload) {} + +unsigned tree_sitter_rx_external_scanner_serialize(void *payload, char *buffer) { return 0; } + +void tree_sitter_rx_external_scanner_deserialize(void *payload, const char *buffer, unsigned length) {} + +bool tree_sitter_rx_external_scanner_scan(void *payload, TSLexer *lexer, const bool *valid_symbols) { + return external_scanner_scan(payload, lexer, valid_symbols); +} diff --git a/tooling/tree-sitter-rx/src/tree_sitter/alloc.h b/tooling/tree-sitter-rx/src/tree_sitter/alloc.h new file mode 100644 index 00000000..1abdd120 --- /dev/null +++ b/tooling/tree-sitter-rx/src/tree_sitter/alloc.h @@ -0,0 +1,54 @@ +#ifndef TREE_SITTER_ALLOC_H_ +#define TREE_SITTER_ALLOC_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include + +// Allow clients to override allocation functions +#ifdef TREE_SITTER_REUSE_ALLOCATOR + +extern void *(*ts_current_malloc)(size_t size); +extern void *(*ts_current_calloc)(size_t count, size_t size); +extern void *(*ts_current_realloc)(void *ptr, size_t size); +extern void (*ts_current_free)(void *ptr); + +#ifndef ts_malloc +#define ts_malloc ts_current_malloc +#endif +#ifndef ts_calloc +#define ts_calloc ts_current_calloc +#endif +#ifndef ts_realloc +#define ts_realloc ts_current_realloc +#endif +#ifndef ts_free +#define ts_free ts_current_free +#endif + +#else + +#ifndef ts_malloc +#define ts_malloc malloc +#endif +#ifndef ts_calloc +#define ts_calloc calloc +#endif +#ifndef ts_realloc +#define ts_realloc realloc +#endif +#ifndef ts_free +#define ts_free free +#endif + +#endif + +#ifdef __cplusplus +} +#endif + +#endif // TREE_SITTER_ALLOC_H_ diff --git a/tooling/tree-sitter-rx/src/tree_sitter/array.h b/tooling/tree-sitter-rx/src/tree_sitter/array.h new file mode 100644 index 00000000..eba3da40 --- /dev/null +++ b/tooling/tree-sitter-rx/src/tree_sitter/array.h @@ -0,0 +1,336 @@ +#ifndef TREE_SITTER_ARRAY_H_ +#define TREE_SITTER_ARRAY_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "./alloc.h" + +#include +#include +#include +#include +#include + +#ifdef _MSC_VER +#pragma warning(push) +#pragma warning(disable : 4101) +#elif defined(__GNUC__) || defined(__clang__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-variable" +#endif + +#define Array(T) \ + struct { \ + T *contents; \ + uint32_t size; \ + uint32_t capacity; \ + } + +/// Initialize an array. +#define array_init(self) \ + ((self)->size = 0, (self)->capacity = 0, (self)->contents = NULL) + +/// Create an empty array. +#define array_new() \ + { NULL, 0, 0 } + +/// Get a pointer to the element at a given `index` in the array. +#define array_get(self, _index) \ + (assert((uint32_t)(_index) < (self)->size), &(self)->contents[_index]) + +/// Get a pointer to the first element in the array. +#define array_front(self) array_get(self, 0) + +/// Get a pointer to the last element in the array. +#define array_back(self) array_get(self, (self)->size - 1) + +/// Clear the array, setting its size to zero. Note that this does not free any +/// memory allocated for the array's contents. +#define array_clear(self) ((self)->size = 0) + +#ifdef __cplusplus +#define _array__cast(self, expr) (decltype((self)->contents))(expr) +#else +#define _array__cast(self, expr) (expr) +#endif + +/// Reserve `new_capacity` elements of space in the array. If `new_capacity` is +/// less than the array's current capacity, this function has no effect. +#define array_reserve(self, new_capacity) \ + ((self)->contents = _array__cast(self, _array__reserve( \ + (void *)(self)->contents, &(self)->capacity, \ + array_elem_size(self), new_capacity)) \ + ) + +/// Free any memory allocated for this array. Note that this does not free any +/// memory allocated for the array's contents. +#define array_delete(self) \ + do { \ + if ((self)->contents) ts_free((self)->contents); \ + (self)->contents = NULL; \ + (self)->size = 0; \ + (self)->capacity = 0; \ + } while (0) + +/// Push a new `element` onto the end of the array. +#define array_push(self, element) \ + do { \ + (self)->contents = _array__cast(self, _array__grow( \ + (void *)(self)->contents, (self)->size, &(self)->capacity, \ + 1, array_elem_size(self) \ + )); \ + (self)->contents[(self)->size++] = (element); \ + } while(0) + +/// Increase the array's size by `count` elements. +/// New elements are zero-initialized. +#define array_grow_by(self, count) \ + do { \ + if ((count) == 0) break; \ + (self)->contents = _array__cast(self, _array__grow( \ + (self)->contents, (self)->size, &(self)->capacity, \ + count, array_elem_size(self) \ + )); \ + memset((self)->contents + (self)->size, 0, (count) * array_elem_size(self)); \ + (self)->size += (count); \ + } while (0) + +/// Append all elements from one array to the end of another. +#define array_push_all(self, other) \ + array_extend((self), (other)->size, (other)->contents) + +/// Append `count` elements to the end of the array, reading their values from the +/// `contents` pointer. +#define array_extend(self, count, other_contents) \ + ((self)->contents = _array__cast(self, _array__splice( \ + (void*)(self)->contents, &(self)->size, &(self)->capacity, \ + array_elem_size(self), (self)->size, 0, count, other_contents \ + ))) + +/// Remove `old_count` elements from the array starting at the given `index`. At +/// the same index, insert `new_count` new elements, reading their values from the +/// `new_contents` pointer. +#define array_splice(self, _index, old_count, new_count, new_contents) \ + ((self)->contents = _array__cast(self, _array__splice( \ + (void *)(self)->contents, &(self)->size, &(self)->capacity, \ + array_elem_size(self), _index, old_count, new_count, new_contents \ + ))) + +/// Insert one `element` into the array at the given `index`. +#define array_insert(self, _index, element) \ + ((self)->contents = _array__cast(self, _array__splice( \ + (void *)(self)->contents, &(self)->size, &(self)->capacity, \ + array_elem_size(self), _index, 0, 1, &(element) \ + ))) + +/// Remove one element from the array at the given `index`. +#define array_erase(self, _index) \ + _array__erase((void *)(self)->contents, &(self)->size, array_elem_size(self), _index) + +/// Pop the last element off the array, returning the element by value. +#define array_pop(self) ((self)->contents[--(self)->size]) + +/// Assign the contents of one array to another, reallocating if necessary. +#define array_assign(self, other) \ + ((self)->contents = _array__cast(self, _array__assign( \ + (void *)(self)->contents, &(self)->size, &(self)->capacity, \ + (const void *)(other)->contents, (other)->size, array_elem_size(self) \ + ))) + +/// Swap one array with another +#define array_swap(self, other) \ + do { \ + void *_array_swap_tmp = (void *)(self)->contents; \ + (self)->contents = (other)->contents; \ + (other)->contents = _array__cast(other, _array_swap_tmp); \ + _array__swap(&(self)->size, &(self)->capacity, \ + &(other)->size, &(other)->capacity); \ + } while (0) + +/// Get the size of the array contents +#define array_elem_size(self) (sizeof *(self)->contents) + +/// Search a sorted array for a given `needle` value, using the given `compare` +/// callback to determine the order. +/// +/// If an existing element is found to be equal to `needle`, then the `index` +/// out-parameter is set to the existing value's index, and the `exists` +/// out-parameter is set to true. Otherwise, `index` is set to an index where +/// `needle` should be inserted in order to preserve the sorting, and `exists` +/// is set to false. +#define array_search_sorted_with(self, compare, needle, _index, _exists) \ + _array__search_sorted(self, 0, compare, , needle, _index, _exists) + +/// Search a sorted array for a given `needle` value, using integer comparisons +/// of a given struct field (specified with a leading dot) to determine the order. +/// +/// See also `array_search_sorted_with`. +#define array_search_sorted_by(self, field, needle, _index, _exists) \ + _array__search_sorted(self, 0, _compare_int, field, needle, _index, _exists) + +/// Insert a given `value` into a sorted array, using the given `compare` +/// callback to determine the order. +#define array_insert_sorted_with(self, compare, value) \ + do { \ + unsigned _index, _exists; \ + array_search_sorted_with(self, compare, &(value), &_index, &_exists); \ + if (!_exists) array_insert(self, _index, value); \ + } while (0) + +/// Insert a given `value` into a sorted array, using integer comparisons of +/// a given struct field (specified with a leading dot) to determine the order. +/// +/// See also `array_search_sorted_by`. +#define array_insert_sorted_by(self, field, value) \ + do { \ + unsigned _index, _exists; \ + array_search_sorted_by(self, field, (value) field, &_index, &_exists); \ + if (!_exists) array_insert(self, _index, value); \ + } while (0) + +// Private + +// Pointers to individual `Array` fields (rather than the entire `Array` itself) +// are passed to the various `_array__*` functions below to address strict aliasing +// violations that arises when the _entire_ `Array` struct is passed as `Array(void)*`. +// +// The `Array` type itself was not altered as a solution in order to avoid breakage +// with existing consumers (in particular, parsers with external scanners). + +/// This is not what you're looking for, see `array_erase`. +static inline void _array__erase(void* self_contents, uint32_t *size, + size_t element_size, uint32_t index) { + assert(index < *size); + char *contents = (char *)self_contents; + memmove(contents + index * element_size, contents + (index + 1) * element_size, + (*size - index - 1) * element_size); + (*size)--; +} + +/// This is not what you're looking for, see `array_reserve`. +static inline void *_array__reserve(void *contents, uint32_t *capacity, + size_t element_size, uint32_t new_capacity) { + void *new_contents = contents; + if (new_capacity > *capacity) { + if (contents) { + new_contents = ts_realloc(contents, new_capacity * element_size); + } else { + new_contents = ts_malloc(new_capacity * element_size); + } + *capacity = new_capacity; + } + return new_contents; +} + +/// This is not what you're looking for, see `array_assign`. +static inline void *_array__assign(void* self_contents, uint32_t *self_size, uint32_t *self_capacity, + const void *other_contents, uint32_t other_size, size_t element_size) { + void *new_contents = _array__reserve(self_contents, self_capacity, element_size, other_size); + *self_size = other_size; + memcpy(new_contents, other_contents, *self_size * element_size); + return new_contents; +} + +/// This is not what you're looking for, see `array_swap`. +static inline void _array__swap(uint32_t *self_size, uint32_t *self_capacity, + uint32_t *other_size, uint32_t *other_capacity) { + uint32_t tmp_size = *self_size; + uint32_t tmp_capacity = *self_capacity; + *self_size = *other_size; + *self_capacity = *other_capacity; + *other_size = tmp_size; + *other_capacity = tmp_capacity; +} + +/// This is not what you're looking for, see `array_push` or `array_grow_by`. +static inline void *_array__grow(void *contents, uint32_t size, uint32_t *capacity, + uint32_t count, size_t element_size) { + void *new_contents = contents; + uint32_t new_size = size + count; + if (new_size > *capacity) { + uint32_t new_capacity = *capacity * 2; + if (new_capacity < 8) new_capacity = 8; + if (new_capacity < new_size) new_capacity = new_size; + new_contents = _array__reserve(contents, capacity, element_size, new_capacity); + } + return new_contents; +} + +/// This is not what you're looking for, see `array_splice`. +static inline void *_array__splice(void *self_contents, uint32_t *size, uint32_t *capacity, + size_t element_size, + uint32_t index, uint32_t old_count, + uint32_t new_count, const void *elements) { + uint32_t new_size = *size + new_count - old_count; + uint32_t old_end = index + old_count; + uint32_t new_end = index + new_count; + assert(old_end <= *size); + + void *new_contents = _array__reserve(self_contents, capacity, element_size, new_size); + + char *contents = (char *)new_contents; + if (*size > old_end) { + memmove( + contents + new_end * element_size, + contents + old_end * element_size, + (*size - old_end) * element_size + ); + } + if (new_count > 0) { + if (elements) { + memcpy( + (contents + index * element_size), + elements, + new_count * element_size + ); + } else { + memset( + (contents + index * element_size), + 0, + new_count * element_size + ); + } + } + *size += new_count - old_count; + + return new_contents; +} + +/// A binary search routine, based on Rust's `std::slice::binary_search_by`. +/// This is not what you're looking for, see `array_search_sorted_with` or `array_search_sorted_by`. +#define _array__search_sorted(self, start, compare, suffix, needle, _index, _exists) \ + do { \ + *(_index) = start; \ + *(_exists) = false; \ + uint32_t size = (self)->size - *(_index); \ + if (size == 0) break; \ + int comparison; \ + while (size > 1) { \ + uint32_t half_size = size / 2; \ + uint32_t mid_index = *(_index) + half_size; \ + comparison = compare(&((self)->contents[mid_index] suffix), (needle)); \ + if (comparison <= 0) *(_index) = mid_index; \ + size -= half_size; \ + } \ + comparison = compare(&((self)->contents[*(_index)] suffix), (needle)); \ + if (comparison == 0) *(_exists) = true; \ + else if (comparison < 0) *(_index) += 1; \ + } while (0) + +/// Helper macro for the `_sorted_by` routines below. This takes the left (existing) +/// parameter by reference in order to work with the generic sorting function above. +#define _compare_int(a, b) ((int)*(a) - (int)(b)) + +#ifdef _MSC_VER +#pragma warning(pop) +#elif defined(__GNUC__) || defined(__clang__) +#pragma GCC diagnostic pop +#endif + +#ifdef __cplusplus +} +#endif + +#endif // TREE_SITTER_ARRAY_H_ diff --git a/tooling/tree-sitter-rx/src/tree_sitter/parser.h b/tooling/tree-sitter-rx/src/tree_sitter/parser.h new file mode 100644 index 00000000..858107de --- /dev/null +++ b/tooling/tree-sitter-rx/src/tree_sitter/parser.h @@ -0,0 +1,286 @@ +#ifndef TREE_SITTER_PARSER_H_ +#define TREE_SITTER_PARSER_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include + +#define ts_builtin_sym_error ((TSSymbol)-1) +#define ts_builtin_sym_end 0 +#define TREE_SITTER_SERIALIZATION_BUFFER_SIZE 1024 + +#ifndef TREE_SITTER_API_H_ +typedef uint16_t TSStateId; +typedef uint16_t TSSymbol; +typedef uint16_t TSFieldId; +typedef struct TSLanguage TSLanguage; +typedef struct TSLanguageMetadata { + uint8_t major_version; + uint8_t minor_version; + uint8_t patch_version; +} TSLanguageMetadata; +#endif + +typedef struct { + TSFieldId field_id; + uint8_t child_index; + bool inherited; +} TSFieldMapEntry; + +// Used to index the field and supertype maps. +typedef struct { + uint16_t index; + uint16_t length; +} TSMapSlice; + +typedef struct { + bool visible; + bool named; + bool supertype; +} TSSymbolMetadata; + +typedef struct TSLexer TSLexer; + +struct TSLexer { + int32_t lookahead; + TSSymbol result_symbol; + void (*advance)(TSLexer *, bool); + void (*mark_end)(TSLexer *); + uint32_t (*get_column)(TSLexer *); + bool (*is_at_included_range_start)(const TSLexer *); + bool (*eof)(const TSLexer *); + void (*log)(const TSLexer *, const char *, ...); +}; + +typedef enum { + TSParseActionTypeShift, + TSParseActionTypeReduce, + TSParseActionTypeAccept, + TSParseActionTypeRecover, +} TSParseActionType; + +typedef union { + struct { + uint8_t type; + TSStateId state; + bool extra; + bool repetition; + } shift; + struct { + uint8_t type; + uint8_t child_count; + TSSymbol symbol; + int16_t dynamic_precedence; + uint16_t production_id; + } reduce; + uint8_t type; +} TSParseAction; + +typedef struct { + uint16_t lex_state; + uint16_t external_lex_state; +} TSLexMode; + +typedef struct { + uint16_t lex_state; + uint16_t external_lex_state; + uint16_t reserved_word_set_id; +} TSLexerMode; + +typedef union { + TSParseAction action; + struct { + uint8_t count; + bool reusable; + } entry; +} TSParseActionEntry; + +typedef struct { + int32_t start; + int32_t end; +} TSCharacterRange; + +struct TSLanguage { + uint32_t abi_version; + uint32_t symbol_count; + uint32_t alias_count; + uint32_t token_count; + uint32_t external_token_count; + uint32_t state_count; + uint32_t large_state_count; + uint32_t production_id_count; + uint32_t field_count; + uint16_t max_alias_sequence_length; + const uint16_t *parse_table; + const uint16_t *small_parse_table; + const uint32_t *small_parse_table_map; + const TSParseActionEntry *parse_actions; + const char * const *symbol_names; + const char * const *field_names; + const TSMapSlice *field_map_slices; + const TSFieldMapEntry *field_map_entries; + const TSSymbolMetadata *symbol_metadata; + const TSSymbol *public_symbol_map; + const uint16_t *alias_map; + const TSSymbol *alias_sequences; + const TSLexerMode *lex_modes; + bool (*lex_fn)(TSLexer *, TSStateId); + bool (*keyword_lex_fn)(TSLexer *, TSStateId); + TSSymbol keyword_capture_token; + struct { + const bool *states; + const TSSymbol *symbol_map; + void *(*create)(void); + void (*destroy)(void *); + bool (*scan)(void *, TSLexer *, const bool *symbol_whitelist); + unsigned (*serialize)(void *, char *); + void (*deserialize)(void *, const char *, unsigned); + } external_scanner; + const TSStateId *primary_state_ids; + const char *name; + const TSSymbol *reserved_words; + uint16_t max_reserved_word_set_size; + uint32_t supertype_count; + const TSSymbol *supertype_symbols; + const TSMapSlice *supertype_map_slices; + const TSSymbol *supertype_map_entries; + TSLanguageMetadata metadata; +}; + +static inline bool set_contains(const TSCharacterRange *ranges, uint32_t len, int32_t lookahead) { + uint32_t index = 0; + uint32_t size = len - index; + while (size > 1) { + uint32_t half_size = size / 2; + uint32_t mid_index = index + half_size; + const TSCharacterRange *range = &ranges[mid_index]; + if (lookahead >= range->start && lookahead <= range->end) { + return true; + } else if (lookahead > range->end) { + index = mid_index; + } + size -= half_size; + } + const TSCharacterRange *range = &ranges[index]; + return (lookahead >= range->start && lookahead <= range->end); +} + +/* + * Lexer Macros + */ + +#ifdef _MSC_VER +#define UNUSED __pragma(warning(suppress : 4101)) +#else +#define UNUSED __attribute__((unused)) +#endif + +#define START_LEXER() \ + bool result = false; \ + bool skip = false; \ + UNUSED \ + bool eof = false; \ + int32_t lookahead; \ + goto start; \ + next_state: \ + lexer->advance(lexer, skip); \ + start: \ + skip = false; \ + lookahead = lexer->lookahead; + +#define ADVANCE(state_value) \ + { \ + state = state_value; \ + goto next_state; \ + } + +#define ADVANCE_MAP(...) \ + { \ + static const uint16_t map[] = { __VA_ARGS__ }; \ + for (uint32_t i = 0; i < sizeof(map) / sizeof(map[0]); i += 2) { \ + if (map[i] == lookahead) { \ + state = map[i + 1]; \ + goto next_state; \ + } \ + } \ + } + +#define SKIP(state_value) \ + { \ + skip = true; \ + state = state_value; \ + goto next_state; \ + } + +#define ACCEPT_TOKEN(symbol_value) \ + result = true; \ + lexer->result_symbol = symbol_value; \ + lexer->mark_end(lexer); + +#define END_STATE() return result; + +/* + * Parse Table Macros + */ + +#define SMALL_STATE(id) ((id) - LARGE_STATE_COUNT) + +#define STATE(id) id + +#define ACTIONS(id) id + +#define SHIFT(state_value) \ + {{ \ + .shift = { \ + .type = TSParseActionTypeShift, \ + .state = (state_value) \ + } \ + }} + +#define SHIFT_REPEAT(state_value) \ + {{ \ + .shift = { \ + .type = TSParseActionTypeShift, \ + .state = (state_value), \ + .repetition = true \ + } \ + }} + +#define SHIFT_EXTRA() \ + {{ \ + .shift = { \ + .type = TSParseActionTypeShift, \ + .extra = true \ + } \ + }} + +#define REDUCE(symbol_name, children, precedence, prod_id) \ + {{ \ + .reduce = { \ + .type = TSParseActionTypeReduce, \ + .symbol = symbol_name, \ + .child_count = children, \ + .dynamic_precedence = precedence, \ + .production_id = prod_id \ + }, \ + }} + +#define RECOVER() \ + {{ \ + .type = TSParseActionTypeRecover \ + }} + +#define ACCEPT_INPUT() \ + {{ \ + .type = TSParseActionTypeAccept \ + }} + +#ifdef __cplusplus +} +#endif + +#endif // TREE_SITTER_PARSER_H_ diff --git a/tooling/tree-sitter-rx/test/corpus/declarations.txt b/tooling/tree-sitter-rx/test/corpus/declarations.txt new file mode 100644 index 00000000..55c17cbc --- /dev/null +++ b/tooling/tree-sitter-rx/test/corpus/declarations.txt @@ -0,0 +1,532 @@ +======================================== +Source with a seed +======================================== + +source count := 0 + +--- + +(program + (source_declaration + name: (identifier) + value: (number))) + +======================================== +Source with a type and a question +======================================== + +source user: User = await fetchUser(id) + +--- + +(program + (source_declaration + name: (identifier) + type: (type_annotation + (type_identifier)) + value: (await_expression + argument: (call_expression + function: (identifier) + arguments: (arguments + (identifier)))))) + +======================================== +Source with a key +======================================== + +source todos: Row[] key id = server.map(row) +source xs key id := [] + +--- + +(program + (source_declaration + name: (identifier) + type: (type_annotation + (array_type + (type_identifier))) + (key_clause + name: (identifier)) + value: (call_expression + function: (member_expression + object: (identifier) + property: (property_identifier)) + arguments: (arguments + (identifier)))) + (source_declaration + name: (identifier) + (key_clause + name: (identifier)) + value: (array))) + +======================================== +Equations +======================================== + +double = count * 2 +label: string = `${user.name} (${count})` + +--- + +(program + (equation + name: (identifier) + value: (binary_expression + left: (identifier) + right: (number))) + (equation + name: (identifier) + type: (type_annotation + (predefined_type)) + value: (template_string + (template_substitution + (member_expression + object: (identifier) + property: (property_identifier))) + (string_fragment) + (template_substitution + (identifier)) + (string_fragment)))) + +======================================== +Equation named like a contextual keyword +======================================== + +server = await loadTodos() +error = latest problem + +--- + +(program + (equation + name: (identifier) + value: (await_expression + argument: (call_expression + function: (identifier) + arguments: (arguments)))) + (equation + name: (identifier) + value: (latest_expression + argument: (identifier)))) + +======================================== +Keyed and deferred equations +======================================== + +rows key id = items +deferred preview = await render(doc) + +--- + +(program + (equation + name: (identifier) + (key_clause + name: (identifier)) + value: (identifier)) + (equation + (deferred) + name: (identifier) + value: (await_expression + argument: (call_expression + function: (identifier) + arguments: (arguments + (identifier)))))) + +======================================== +Destructuring equations +======================================== + +{ name, age = 0, ...rest } = user +[first, , third] = list + +--- + +(program + (equation + name: (object_pattern + (shorthand_property_identifier_pattern) + (object_assignment_pattern + left: (shorthand_property_identifier_pattern) + right: (number)) + (rest_pattern + (identifier))) + value: (identifier)) + (equation + name: (array_pattern + (identifier) + (identifier)) + value: (identifier))) + +======================================== +Arrow fn +======================================== + +fn area(w: number, h: number): number => w * h + +--- + +(program + (fn_declaration + name: (identifier) + parameters: (formal_parameters + (required_parameter + pattern: (identifier) + type: (type_annotation + (predefined_type))) + (required_parameter + pattern: (identifier) + type: (type_annotation + (predefined_type)))) + return_type: (type_annotation + (predefined_type)) + body: (binary_expression + left: (identifier) + right: (identifier)))) + +======================================== +Block fn with ref parameter +======================================== + +fn Counter(ref start: number) { + source n := peek(start) + next = n + 1 + next +} + +--- + +(program + (fn_declaration + name: (identifier) + parameters: (formal_parameters + (required_parameter + (ref_modifier) + pattern: (identifier) + type: (type_annotation + (predefined_type)))) + body: (fn_body + (source_declaration + name: (identifier) + value: (peek_expression + argument: (parenthesized_expression + (identifier)))) + (equation + name: (identifier) + value: (binary_expression + left: (identifier) + right: (number))) + value: (identifier)))) + +======================================== +Server fn +======================================== + +server fn listNotes(q: string): Promise { + let rows = await dbNotes(q) + return rows +} + +--- + +(program + (server_function_declaration + (side) + name: (identifier) + parameters: (formal_parameters + (required_parameter + pattern: (identifier) + type: (type_annotation + (predefined_type)))) + return_type: (type_annotation + (generic_type + name: (type_identifier) + type_arguments: (type_arguments + (array_type + (type_identifier))))) + body: (statement_block + (lexical_declaration + (variable_declarator + name: (identifier) + value: (await_expression + argument: (call_expression + function: (identifier) + arguments: (arguments + (identifier)))))) + (return_statement + (identifier))))) + +======================================== +Action +======================================== + +action save(n: string) { + name := n +} + +--- + +(program + (action_declaration + name: (identifier) + parameters: (formal_parameters + (required_parameter + pattern: (identifier) + type: (type_annotation + (predefined_type)))) + body: (statement_block + (write_statement + target: (identifier) + value: (identifier))))) + +======================================== +Effect +======================================== + +effect (count, user.name) { log($0, $1) } +effect () { start() } + +--- + +(program + (effect_declaration + inputs: (effect_inputs + (identifier) + (member_expression + object: (identifier) + property: (property_identifier))) + body: (statement_block + (expression_statement + (call_expression + function: (identifier) + arguments: (arguments + (identifier) + (identifier)))))) + (effect_declaration + inputs: (effect_inputs) + body: (statement_block + (expression_statement + (call_expression + function: (identifier) + arguments: (arguments)))))) + +======================================== +Error classes +======================================== + +error NotFound(id: number) => `no user ${id}` +error Offline + +--- + +(program + (error_declaration + name: (type_identifier) + parameters: (formal_parameters + (required_parameter + pattern: (identifier) + type: (type_annotation + (predefined_type)))) + message: (template_string + (string_fragment) + (template_substitution + (identifier)))) + (error_declaration + name: (type_identifier))) + +======================================== +Context and provide +======================================== + +context theme: Theme = defaultTheme +context session: source Session +provide theme = settings.theme + +--- + +(program + (context_declaration + name: (identifier) + type: (type_identifier) + value: (identifier)) + (context_declaration + name: (identifier) + type: (type_identifier)) + (provide_declaration + name: (identifier) + value: (member_expression + object: (identifier) + property: (property_identifier)))) + +======================================== +Component with props +======================================== + +component Row(props: { item: Item }) { +
  • {props.item.name}
  • +} + +--- + +(program + (component_declaration + name: (identifier) + parameters: (formal_parameters + (required_parameter + pattern: (identifier) + type: (type_annotation + (object_type + (property_signature + name: (property_identifier) + type: (type_annotation + (type_identifier))))))) + body: (view_block + (jsx_element + open_tag: (jsx_opening_element + name: (identifier)) + (jsx_expression + (member_expression + object: (member_expression + object: (identifier) + property: (property_identifier)) + property: (property_identifier))) + close_tag: (jsx_closing_element + name: (identifier)))))) + +======================================== +Component with destructured props +======================================== + +component Card({ title, open = false }) { +

    {title}

    +} + +--- + +(program + (component_declaration + name: (identifier) + parameters: (formal_parameters + (required_parameter + pattern: (object_pattern + (shorthand_property_identifier_pattern) + (object_assignment_pattern + left: (shorthand_property_identifier_pattern) + right: (false))))) + body: (view_block + (jsx_element + open_tag: (jsx_opening_element + name: (identifier)) + (jsx_expression + (identifier)) + close_tag: (jsx_closing_element + name: (identifier)))))) + +======================================== +Component body: declarations then markup +======================================== + +component Counter() { + source n := 0 + doubled = n * 2 + +} + +--- + +(program + (component_declaration + name: (identifier) + parameters: (formal_parameters) + body: (view_block + (source_declaration + name: (identifier) + value: (number)) + (equation + name: (identifier) + value: (binary_expression + left: (identifier) + right: (number))) + (jsx_element + open_tag: (jsx_opening_element + name: (identifier) + attribute: (jsx_attribute + (property_identifier) + (jsx_expression + (arrow_function + parameters: (formal_parameters) + body: (statement_block + (write_statement + target: (identifier) + value: (binary_expression + left: (identifier) + right: (number)))))))) + (jsx_expression + (identifier)) + close_tag: (jsx_closing_element + name: (identifier)))))) + +======================================== +Declarations back to back on one line +======================================== + +source a := 1; b = a + 1 + +--- + +(program + (source_declaration + name: (identifier) + value: (number)) + (equation + name: (identifier) + value: (binary_expression + left: (identifier) + right: (number)))) + +======================================== +A fn body on the next line +======================================== + +fn f(a: number): number +{ + a + 1 +} + +server fn load(id: number): Row +{ + return db(id) +} + +--- + +(program + (fn_declaration + (identifier) + (formal_parameters + (required_parameter + (identifier) + (type_annotation + (predefined_type)))) + (type_annotation + (predefined_type)) + (fn_body + (binary_expression + (identifier) + (number)))) + (server_function_declaration + (side) + (identifier) + (formal_parameters + (required_parameter + (identifier) + (type_annotation + (predefined_type)))) + (type_annotation + (type_identifier)) + (statement_block + (return_statement + (call_expression + (identifier) + (arguments + (identifier))))))) diff --git a/tooling/tree-sitter-rx/test/corpus/expressions.txt b/tooling/tree-sitter-rx/test/corpus/expressions.txt new file mode 100644 index 00000000..ac152a1d --- /dev/null +++ b/tooling/tree-sitter-rx/test/corpus/expressions.txt @@ -0,0 +1,305 @@ +======================================== +Await with a first paint +======================================== + +xs = await fetchRows(q) -> [] + +--- + +(program + (equation + name: (identifier) + value: (await_expression + argument: (call_expression + function: (identifier) + arguments: (arguments + (identifier))) + initial: (array)))) + +======================================== +Stream +======================================== + +source messages = await* stream(room) -> [] + +--- + +(program + (source_declaration + name: (identifier) + value: (await_expression + argument: (call_expression + function: (identifier) + arguments: (arguments + (identifier))) + initial: (array)))) + +======================================== +Pending and latest are unary +======================================== + +busy = pending? a || b +next = latest x * 2 + +--- + +(program + (equation + name: (identifier) + value: (binary_expression + left: (pending_expression + argument: (identifier)) + right: (identifier))) + (equation + name: (identifier) + value: (binary_expression + left: (latest_expression + argument: (identifier)) + right: (number)))) + +======================================== +Pending in a conditional +======================================== + +label = pending? x ? "saving" : "idle" + +--- + +(program + (equation + name: (identifier) + value: (ternary_expression + condition: (pending_expression + argument: (identifier)) + consequence: (string + (string_fragment)) + alternative: (string + (string_fragment))))) + +======================================== +Peek +======================================== + +source draft := peek(props.x) + +--- + +(program + (source_declaration + name: (identifier) + value: (peek_expression + argument: (parenthesized_expression + (member_expression + object: (identifier) + property: (property_identifier)))))) + +======================================== +Pre +======================================== + +changed = x != pre x +first = pre x -> 0 + +--- + +(program + (equation + name: (identifier) + value: (binary_expression + left: (identifier) + right: (pre_expression + name: (identifier)))) + (equation + name: (identifier) + value: (pre_expression + name: (identifier) + initial: (number)))) + +======================================== +Use +======================================== + +palette = (use theme).palette + +--- + +(program + (equation + name: (identifier) + value: (member_expression + object: (parenthesized_expression + (use_expression + name: (identifier))) + property: (property_identifier)))) + +======================================== +Throw expression +======================================== + +name = user == null ? throw NotFound(id) : user.name + +--- + +(program + (equation + name: (identifier) + value: (ternary_expression + condition: (binary_expression + left: (identifier) + right: (null)) + consequence: (throw_expression + argument: (call_expression + function: (identifier) + arguments: (arguments + (identifier)))) + alternative: (member_expression + object: (identifier) + property: (property_identifier))))) + +======================================== +Catch expression +======================================== + +shown = name catch (e: NotFound | Gone) => "(nobody)" +safe = risky catch => 0 + +--- + +(program + (equation + name: (identifier) + value: (catch_expression + value: (identifier) + parameter: (identifier) + type: (type_identifier) + type: (type_identifier) + alternative: (string + (string_fragment)))) + (equation + name: (identifier) + value: (catch_expression + value: (identifier) + alternative: (number)))) + +======================================== +Catch on the next line +======================================== + +shown = load(id) + catch (e) => null + +--- + +(program + (equation + name: (identifier) + value: (catch_expression + value: (call_expression + function: (identifier) + arguments: (arguments + (identifier))) + parameter: (identifier) + alternative: (null)))) + +======================================== +Draft +======================================== + +selected = draft(pre selected -> {}) { d[id] := true } + +--- + +(program + (equation + name: (identifier) + value: (draft_expression + value: (pre_expression + name: (identifier) + initial: (object)) + body: (statement_block + (write_statement + target: (subscript_expression + object: (identifier) + index: (identifier)) + value: (true)))))) + +======================================== +Fn expression +======================================== + +add = fn (a, b) => a + b + +--- + +(program + (equation + name: (identifier) + value: (fn_expression + parameters: (formal_parameters + (required_parameter + pattern: (identifier)) + (required_parameter + pattern: (identifier))) + body: (binary_expression + left: (identifier) + right: (identifier))))) + +======================================== +Server fn expression +======================================== + +save = server fn (id: number) { await db(id) } + +--- + +(program + (equation + name: (identifier) + value: (server_fn_expression + parameters: (formal_parameters + (required_parameter + pattern: (identifier) + type: (type_annotation + (predefined_type)))) + body: (statement_block + (expression_statement + (await_expression + argument: (call_expression + function: (identifier) + arguments: (arguments + (identifier))))))))) + +======================================== +Style class map +======================================== + +styles = + +--- + +(program + (equation + name: (identifier) + value: (style_element + (identifier) + (raw_text) + (identifier)))) + +======================================== +A chain of catches folds left +======================================== + +x = a catch => b catch (e: NotFound) => c + +--- + +(program + (equation + (identifier) + (catch_expression + (catch_expression + (identifier) + (identifier)) + (identifier) + (type_identifier) + (identifier)))) diff --git a/tooling/tree-sitter-rx/test/corpus/markup.txt b/tooling/tree-sitter-rx/test/corpus/markup.txt new file mode 100644 index 00000000..225f859d --- /dev/null +++ b/tooling/tree-sitter-rx/test/corpus/markup.txt @@ -0,0 +1,431 @@ +======================================== +Braced if inside an element +======================================== + +component A() { +
    + {if open {

    open

    } else {

    closed

    }} +
    +} + +--- + +(program + (component_declaration + name: (identifier) + parameters: (formal_parameters) + body: (view_block + (jsx_element + open_tag: (jsx_opening_element + name: (identifier)) + (jsx_expression + (if_block + condition: (identifier) + consequence: (view_block + (jsx_element + open_tag: (jsx_opening_element + name: (identifier)) + (jsx_text) + close_tag: (jsx_closing_element + name: (identifier)))) + alternative: (else_block + (view_block + (jsx_element + open_tag: (jsx_opening_element + name: (identifier)) + (jsx_text) + close_tag: (jsx_closing_element + name: (identifier))))))) + close_tag: (jsx_closing_element + name: (identifier)))))) + +======================================== +Bare if with else if +======================================== + +component A() { + if a { +

    a

    + } else if b { +

    b

    + } else { +

    c

    + } +} + +--- + +(program + (component_declaration + name: (identifier) + parameters: (formal_parameters) + body: (view_block + (if_block + condition: (identifier) + consequence: (view_block + (jsx_element + open_tag: (jsx_opening_element + name: (identifier)) + (jsx_text) + close_tag: (jsx_closing_element + name: (identifier)))) + alternative: (else_block + (if_block + condition: (identifier) + consequence: (view_block + (jsx_element + open_tag: (jsx_opening_element + name: (identifier)) + (jsx_text) + close_tag: (jsx_closing_element + name: (identifier)))) + alternative: (else_block + (view_block + (jsx_element + open_tag: (jsx_opening_element + name: (identifier)) + (jsx_text) + close_tag: (jsx_closing_element + name: (identifier))))))))))) + +======================================== +For with declarations in its body +======================================== + +component A() { +
      + {for row in rows { + source hits := 0 +
    • { hits := hits + 1 }}>{row.name} {hits}
    • + }} +
    +} + +--- + +(program + (component_declaration + name: (identifier) + parameters: (formal_parameters) + body: (view_block + (jsx_element + open_tag: (jsx_opening_element + name: (identifier)) + (jsx_expression + (for_block + name: (identifier) + value: (identifier) + body: (view_block + (source_declaration + name: (identifier) + value: (number)) + (jsx_element + open_tag: (jsx_opening_element + name: (identifier) + attribute: (jsx_attribute + (property_identifier) + (jsx_expression + (arrow_function + parameters: (formal_parameters) + body: (statement_block + (write_statement + target: (identifier) + value: (binary_expression + left: (identifier) + right: (number)))))))) + (jsx_expression + (member_expression + object: (identifier) + property: (property_identifier))) + (jsx_text) + (jsx_expression + (identifier)) + close_tag: (jsx_closing_element + name: (identifier)))))) + close_tag: (jsx_closing_element + name: (identifier)))))) + +======================================== +Loading with on +======================================== + +component A() { + loading on(id) fallback { } { +
    + } +} + +--- + +(program + (component_declaration + name: (identifier) + parameters: (formal_parameters) + body: (view_block + (loading_block + trigger: (identifier) + fallback: (view_block + (jsx_self_closing_element + name: (identifier))) + body: (view_block + (jsx_self_closing_element + name: (identifier) + attribute: (jsx_attribute + (property_identifier) + (jsx_expression + (await_expression + argument: (call_expression + function: (identifier) + arguments: (arguments + (identifier)))))))))))) + +======================================== +Errored with a parameter and classes +======================================== + +component A() { + {errored fallback(e: BadId | Gone) {

    {e.message}

    } { }} + errored fallback {

    down

    } { } +} + +--- + +(program + (component_declaration + name: (identifier) + parameters: (formal_parameters) + body: (view_block + (jsx_expression + (errored_block + parameter: (identifier) + type: (type_identifier) + type: (type_identifier) + fallback: (view_block + (jsx_element + open_tag: (jsx_opening_element + name: (identifier)) + (jsx_expression + (member_expression + object: (identifier) + property: (property_identifier))) + close_tag: (jsx_closing_element + name: (identifier)))) + body: (view_block + (jsx_self_closing_element + name: (identifier))))) + (errored_block + fallback: (view_block + (jsx_element + open_tag: (jsx_opening_element + name: (identifier)) + (jsx_text) + close_tag: (jsx_closing_element + name: (identifier)))) + body: (view_block + (jsx_self_closing_element + name: (identifier))))))) + +======================================== +Portal +======================================== + +component A() { + portal to(document.body) { } +} + +--- + +(program + (component_declaration + name: (identifier) + parameters: (formal_parameters) + body: (view_block + (portal_block + target: (member_expression + object: (identifier) + property: (property_identifier)) + body: (view_block + (jsx_self_closing_element + name: (identifier))))))) + +======================================== +Reads and spreads +======================================== + +component A(props) { + {props.children}{" "} + +} + +--- + +(program + (component_declaration + name: (identifier) + parameters: (formal_parameters + (required_parameter + pattern: (identifier))) + body: (view_block + (jsx_expression + (member_expression + object: (identifier) + property: (property_identifier))) + (jsx_expression + (string + (string_fragment))) + (jsx_self_closing_element + name: (identifier) + attribute: (jsx_expression + (spread_element + (identifier))) + attribute: (jsx_attribute + (property_identifier) + (string + (string_fragment))))))) + +======================================== +Scoped style +======================================== + +component Card() { + +
    +} + +--- + +(program + (component_declaration + name: (identifier) + parameters: (formal_parameters) + body: (view_block + (style_element + (identifier) + (raw_text) + (identifier)) + (jsx_element + open_tag: (jsx_opening_element + name: (identifier) + attribute: (jsx_attribute + (property_identifier) + (string + (string_fragment)))) + close_tag: (jsx_closing_element + name: (identifier)))))) + +======================================== +Markup after an equation +======================================== + +component A() { + x = a +

    {x}

    + y = b + {y} +} + +--- + +(program + (component_declaration + name: (identifier) + parameters: (formal_parameters) + body: (view_block + (equation + name: (identifier) + value: (identifier)) + (jsx_element + open_tag: (jsx_opening_element + name: (identifier)) + (jsx_expression + (identifier)) + close_tag: (jsx_closing_element + name: (identifier))) + (equation + name: (identifier) + value: (identifier)) + (jsx_expression + (identifier))))) + +======================================== +Text with > and a bare & +======================================== + +component App() { +

    Fish & chips -> a > b & c

    +} + +--- + +(program + (component_declaration + (identifier) + (formal_parameters) + (view_block + (jsx_element + (jsx_opening_element + (identifier)) + (jsx_text) + (html_character_reference) + (jsx_text) + (jsx_closing_element + (identifier)))))) + +======================================== +A style inside an element +======================================== + +component App() { +
    + +
    +} + +--- + +(program + (component_declaration + (identifier) + (formal_parameters) + (view_block + (jsx_element + (jsx_opening_element + (identifier)) + (style_element + (identifier) + (raw_text) + (identifier)) + (jsx_closing_element + (identifier)))))) + +======================================== +An attribute value without braces +======================================== + +component App() { + +} + +--- + +(program + (component_declaration + (identifier) + (formal_parameters) + (view_block + (jsx_self_closing_element + (identifier) + (jsx_attribute + (property_identifier) + (number)) + (jsx_attribute + (property_identifier) + (identifier)) + (jsx_attribute + (property_identifier) + (true)) + (jsx_attribute + (property_identifier)))))) diff --git a/tooling/tree-sitter-rx/test/corpus/modules.txt b/tooling/tree-sitter-rx/test/corpus/modules.txt new file mode 100644 index 00000000..69b71375 --- /dev/null +++ b/tooling/tree-sitter-rx/test/corpus/modules.txt @@ -0,0 +1,325 @@ +======================================== +Import, io +======================================== + +import { saveUser } from "./api" + +--- + +(program + (import_statement + (import_clause + (named_imports + (import_specifier + name: (identifier)))) + source: (string + (string_fragment)))) + +======================================== +Import pure with alias +======================================== + +import pure { fetchUser, format as fmt } from "./api" + +--- + +(program + (import_statement + (pure) + (import_clause + (named_imports + (import_specifier + name: (identifier)) + (import_specifier + name: (identifier) + alias: (identifier)))) + source: (string + (string_fragment)))) + +======================================== +Import server and client sides +======================================== + +import server pure { db } from "./db" +import client { viewport } from "./browser" + +--- + +(program + (import_statement + side: (side) + (pure) + (import_clause + (named_imports + (import_specifier + name: (identifier)))) + source: (string + (string_fragment))) + (import_statement + side: (side) + (import_clause + (named_imports + (import_specifier + name: (identifier)))) + source: (string + (string_fragment)))) + +======================================== +Import with a fails row +======================================== + +import pure { getUser, NotFound } from "./api" fails NotFound | Gone + +--- + +(program + (import_statement + (pure) + (import_clause + (named_imports + (import_specifier + name: (identifier)) + (import_specifier + name: (identifier)))) + source: (string + (string_fragment)) + (fails_clause + (type_identifier) + (type_identifier)))) + +======================================== +Default import +======================================== + +import Chart from "./chart.js" + +--- + +(program + (import_statement + (import_clause + (identifier)) + source: (string + (string_fragment)))) + +======================================== +Export default component +======================================== + +export default component App() { +

    hi

    +} + +--- + +(program + (export_statement + declaration: (component_declaration + name: (identifier) + parameters: (formal_parameters) + body: (view_block + (jsx_element + open_tag: (jsx_opening_element + name: (identifier)) + (jsx_text) + close_tag: (jsx_closing_element + name: (identifier))))))) + +======================================== +Exported declarations +======================================== + +export source count := 0 +export total = count * 2 +export type Id = number +export action reset() { count := 0 } + +--- + +(program + (export_statement + declaration: (source_declaration + name: (identifier) + value: (number))) + (export_statement + declaration: (equation + name: (identifier) + value: (binary_expression + left: (identifier) + right: (number)))) + (export_statement + declaration: (type_alias_declaration + name: (type_identifier) + value: (predefined_type))) + (export_statement + declaration: (action_declaration + name: (identifier) + parameters: (formal_parameters) + body: (statement_block + (write_statement + target: (identifier) + value: (number)))))) + +======================================== +Declaration file rows +======================================== + +export type NoteRow = { id: number } +export error Conflict +export server pure fn dbNotes(q: string): Promise +export server fn dbSave(id: number): Promise rejects(Conflict) +export client pure fn viewport(): Promise +export pure fn counter(ref start: number): number instantiates + +--- + +(program + (export_statement + declaration: (type_alias_declaration + name: (type_identifier) + value: (object_type + (property_signature + name: (property_identifier) + type: (type_annotation + (predefined_type)))))) + (export_statement + declaration: (error_declaration + name: (type_identifier))) + (export_statement + declaration: (fn_signature + side: (side) + (pure) + name: (identifier) + parameters: (formal_parameters + (required_parameter + pattern: (identifier) + type: (type_annotation + (predefined_type)))) + return_type: (type_annotation + (generic_type + name: (type_identifier) + type_arguments: (type_arguments + (array_type + (type_identifier))))))) + (export_statement + declaration: (fn_signature + side: (side) + name: (identifier) + parameters: (formal_parameters + (required_parameter + pattern: (identifier) + type: (type_annotation + (predefined_type)))) + return_type: (type_annotation + (generic_type + name: (type_identifier) + type_arguments: (type_arguments + (type_identifier)))) + (rejects_clause + (type_identifier)))) + (export_statement + declaration: (fn_signature + side: (side) + (pure) + name: (identifier) + parameters: (formal_parameters) + return_type: (type_annotation + (generic_type + name: (type_identifier) + type_arguments: (type_arguments + (predefined_type)))))) + (export_statement + declaration: (fn_signature + (pure) + name: (identifier) + parameters: (formal_parameters + (required_parameter + (ref_modifier) + pattern: (identifier) + type: (type_annotation + (predefined_type)))) + return_type: (type_annotation + (predefined_type))))) + +======================================== +A default import named server or client +======================================== + +import server from "./x" +import client, { a } from "./api" +import server { b } from "./y" + +--- + +(program + (import_statement + (import_clause + (identifier)) + (string + (string_fragment))) + (import_statement + (import_clause + (identifier) + (named_imports + (import_specifier + (identifier)))) + (string + (string_fragment))) + (import_statement + (side) + (import_clause + (named_imports + (import_specifier + (identifier)))) + (string + (string_fragment)))) + +======================================== +A signature that returns an accessor +======================================== + +export pure fn createDebounced(ref source: string, ms: number): ref string +export pure fn createToggle(initial: boolean): [ref boolean, (v: boolean) => void] + +--- + +(program + (export_statement + (fn_signature + (pure) + (identifier) + (formal_parameters + (required_parameter + (ref_modifier) + (identifier) + (type_annotation + (predefined_type))) + (required_parameter + (identifier) + (type_annotation + (predefined_type)))) + (type_annotation + (ref_type + (ref_modifier) + (predefined_type))))) + (export_statement + (fn_signature + (pure) + (identifier) + (formal_parameters + (required_parameter + (identifier) + (type_annotation + (predefined_type)))) + (type_annotation + (tuple_type + (ref_type + (ref_modifier) + (predefined_type)) + (function_type + (formal_parameters + (required_parameter + (identifier) + (type_annotation + (predefined_type)))) + (predefined_type))))))) diff --git a/tooling/tree-sitter-rx/test/corpus/statements.txt b/tooling/tree-sitter-rx/test/corpus/statements.txt new file mode 100644 index 00000000..40753ab6 --- /dev/null +++ b/tooling/tree-sitter-rx/test/corpus/statements.txt @@ -0,0 +1,376 @@ +======================================== +Writes +======================================== + +action f() { + x := 1 + user.name := n + xs[i].done := true + count := c => c + 1 +} + +--- + +(program + (action_declaration + name: (identifier) + parameters: (formal_parameters) + body: (statement_block + (write_statement + target: (identifier) + value: (number)) + (write_statement + target: (member_expression + object: (identifier) + property: (property_identifier)) + value: (identifier)) + (write_statement + target: (member_expression + object: (subscript_expression + object: (identifier) + index: (identifier)) + property: (property_identifier)) + value: (true)) + (write_statement + target: (identifier) + value: (arrow_function + parameter: (identifier) + body: (binary_expression + left: (identifier) + right: (number))))))) + +======================================== +Write through a context +======================================== + +action f() { + use fields.x := 1 +} + +--- + +(program + (action_declaration + name: (identifier) + parameters: (formal_parameters) + body: (statement_block + (write_statement + target: (member_expression + object: (use_expression + name: (identifier)) + property: (property_identifier)) + value: (number))))) + +======================================== +Guess +======================================== + +action like() { + guess liked := true + guess todos[i].saving := true +} + +--- + +(program + (action_declaration + name: (identifier) + parameters: (formal_parameters) + body: (statement_block + (guess_statement + target: (identifier) + value: (true)) + (guess_statement + target: (member_expression + object: (subscript_expression + object: (identifier) + index: (identifier)) + property: (property_identifier)) + value: (true))))) + +======================================== +Refresh and affects +======================================== + +action reload() { + affects user + refresh user +} + +--- + +(program + (action_declaration + name: (identifier) + parameters: (formal_parameters) + body: (statement_block + (affects_statement + name: (identifier)) + (refresh_statement + name: (identifier))))) + +======================================== +Inline action +======================================== + +component A() { + +} + +--- + +(program + (component_declaration + name: (identifier) + parameters: (formal_parameters) + body: (view_block + (jsx_element + open_tag: (jsx_opening_element + name: (identifier) + attribute: (jsx_attribute + (property_identifier) + (jsx_expression + (arrow_function + parameters: (formal_parameters) + body: (statement_block + (action_statement + body: (statement_block + (refresh_statement + name: (identifier)) + (affects_statement + name: (identifier))))))))) + (jsx_text) + close_tag: (jsx_closing_element + name: (identifier)))))) + +======================================== +Let with await and catch +======================================== + +action f() { + let r = await save(x) catch (e: Offline) => null + await done() +} + +--- + +(program + (action_declaration + name: (identifier) + parameters: (formal_parameters) + body: (statement_block + (lexical_declaration + (variable_declarator + name: (identifier) + value: (catch_expression + value: (await_expression + argument: (call_expression + function: (identifier) + arguments: (arguments + (identifier)))) + parameter: (identifier) + type: (type_identifier) + alternative: (null)))) + (expression_statement + (await_expression + argument: (call_expression + function: (identifier) + arguments: (arguments))))))) + +======================================== +Try, catch with classes, finally +======================================== + +action f() { + try { + await save() + } catch (e: Conflict) { + problem := e.message + } finally { + busy := false + } +} + +--- + +(program + (action_declaration + name: (identifier) + parameters: (formal_parameters) + body: (statement_block + (try_statement + body: (statement_block + (expression_statement + (await_expression + argument: (call_expression + function: (identifier) + arguments: (arguments))))) + handler: (catch_clause + parameter: (identifier) + type: (type_annotation + (type_identifier)) + body: (statement_block + (write_statement + target: (identifier) + value: (member_expression + object: (identifier) + property: (property_identifier))))) + finalizer: (finally_clause + body: (statement_block + (write_statement + target: (identifier) + value: (false)))))))) + +======================================== +If statement with a bare return +======================================== + +action f() { + if (title == "") return + add(title) +} + +--- + +(program + (action_declaration + name: (identifier) + parameters: (formal_parameters) + body: (statement_block + (if_statement + condition: (parenthesized_expression + (binary_expression + left: (identifier) + right: (string))) + consequence: (return_statement)) + (expression_statement + (call_expression + function: (identifier) + arguments: (arguments + (identifier))))))) + +======================================== +Statements back to back on one line +======================================== + +action f() { + optimistic x := 1 +} + +--- + +(program + (action_declaration + name: (identifier) + parameters: (formal_parameters) + body: (statement_block + (expression_statement + (identifier)) + (write_statement + target: (identifier) + value: (number))))) + +======================================== +Keywords, use targets and calls back to back on one line +======================================== + +action go() { + a := 1 guess b := 2 refresh rows + a := b use theme := 2 + foo() bar(1).baz() + if (c) x := 1 else y := 2 + a := 1 xs["]"] := 2 + return foo(1) +} + +--- + +(program + (action_declaration + (identifier) + (formal_parameters) + (statement_block + (write_statement + (identifier) + (number)) + (guess_statement + (identifier) + (number)) + (refresh_statement + (identifier)) + (write_statement + (identifier) + (identifier)) + (write_statement + (use_expression + (identifier)) + (number)) + (expression_statement + (call_expression + (identifier) + (arguments))) + (expression_statement + (call_expression + (member_expression + (call_expression + (identifier) + (arguments + (number))) + (property_identifier)) + (arguments))) + (if_statement + (parenthesized_expression + (identifier)) + (write_statement + (identifier) + (number)) + (else_clause + (write_statement + (identifier) + (number)))) + (write_statement + (identifier) + (number)) + (write_statement + (subscript_expression + (identifier) + (string + (string_fragment))) + (number)) + (return_statement + (call_expression + (identifier) + (arguments + (number))))))) + +================================================================================ +Brackets in a comment on one line +================================================================================ + +action a() { + x := 1 f(/* ) { */ 2) g(2 / 1) +} + +-------------------------------------------------------------------------------- + +(program + (action_declaration + (identifier) + (formal_parameters) + (statement_block + (write_statement + (identifier) + (number)) + (expression_statement + (call_expression + (identifier) + (arguments + (comment) + (number)))) + (expression_statement + (call_expression + (identifier) + (arguments + (binary_expression + (number) + (number)))))))) diff --git a/tooling/tree-sitter-rx/tree-sitter.json b/tooling/tree-sitter-rx/tree-sitter.json new file mode 100644 index 00000000..41ef05f6 --- /dev/null +++ b/tooling/tree-sitter-rx/tree-sitter.json @@ -0,0 +1,31 @@ +{ + "$schema": "https://tree-sitter.github.io/tree-sitter/assets/schemas/config.schema.json", + "grammars": [ + { + "name": "rx", + "camelcase": "Rx", + "title": "ReactiveScript", + "scope": "source.rx", + "file-types": ["rx"], + "highlights": "queries/highlights.scm", + "injections": "queries/injections.scm", + "injection-regex": "^(rx|reactivescript)$" + } + ], + "metadata": { + "version": "0.1.0", + "license": "MIT", + "description": "ReactiveScript (.rx) grammar for tree-sitter", + "links": { + "repository": "https://github.com/solidjs/reactivescript" + } + }, + "bindings": { + "c": false, + "go": false, + "node": false, + "python": false, + "rust": false, + "swift": false + } +}