From 5c1b40de7498c742bf6553382d9f8f6510fc0ebb Mon Sep 17 00:00:00 2001 From: Kieran Klukas Date: Mon, 17 Aug 2026 13:04:31 -0400 Subject: [PATCH] feat: add cedarlogic tree sitter --- modules/home/apps/helix.nix | 26 + packages/tree-sitter-cdl/.gitignore | 4 + packages/tree-sitter-cdl/grammar.js | 55 +++ packages/tree-sitter-cdl/package-lock.json | 54 +++ packages/tree-sitter-cdl/package.json | 25 + .../tree-sitter-cdl/queries/highlights.scm | 67 +++ packages/tree-sitter-cdl/src/grammar.json | 92 ++++ packages/tree-sitter-cdl/src/node-types.json | 76 +++ packages/tree-sitter-cdl/src/parser.c | 450 ++++++++++++++++++ .../tree-sitter-cdl/src/tree_sitter/alloc.h | 54 +++ .../tree-sitter-cdl/src/tree_sitter/array.h | 336 +++++++++++++ .../tree-sitter-cdl/src/tree_sitter/parser.h | 286 +++++++++++ packages/tree-sitter-cdl/test/corpus/cdl.txt | 130 +++++ packages/tree-sitter-cdl/tree-sitter.json | 30 ++ 14 files changed, 1685 insertions(+) create mode 100644 packages/tree-sitter-cdl/.gitignore create mode 100644 packages/tree-sitter-cdl/grammar.js create mode 100644 packages/tree-sitter-cdl/package-lock.json create mode 100644 packages/tree-sitter-cdl/package.json create mode 100644 packages/tree-sitter-cdl/queries/highlights.scm create mode 100644 packages/tree-sitter-cdl/src/grammar.json create mode 100644 packages/tree-sitter-cdl/src/node-types.json create mode 100644 packages/tree-sitter-cdl/src/parser.c create mode 100644 packages/tree-sitter-cdl/src/tree_sitter/alloc.h create mode 100644 packages/tree-sitter-cdl/src/tree_sitter/array.h create mode 100644 packages/tree-sitter-cdl/src/tree_sitter/parser.h create mode 100644 packages/tree-sitter-cdl/test/corpus/cdl.txt create mode 100644 packages/tree-sitter-cdl/tree-sitter.json diff --git a/modules/home/apps/helix.nix b/modules/home/apps/helix.nix index c126b57..f69dcef 100644 --- a/modules/home/apps/helix.nix +++ b/modules/home/apps/helix.nix @@ -12,6 +12,26 @@ }; config = lib.mkIf config.atelier.apps.helix.enable { + # Build tree-sitter-cdl grammar as a nix derivation so helix doesn't need + # `hx --grammar build` after every rebuild. + xdg.configFile = let + tree-sitter-cdl-grammar = pkgs.stdenv.mkDerivation { + name = "tree-sitter-cdl"; + src = ../../../packages/tree-sitter-cdl; + buildPhase = '' + $CC -shared -fPIC -O2 -o cdl.so src/parser.c -I src + ''; + installPhase = '' + mkdir -p $out + cp cdl.so $out/ + ''; + }; + in { + "helix/runtime/grammars/cdl.so".source = "${tree-sitter-cdl-grammar}/cdl.so"; + "helix/runtime/queries/cdl/highlights.scm".source = + ../../../packages/tree-sitter-cdl/queries/highlights.scm; + }; + programs.helix = { enable = true; package = pkgs.evil-helix; @@ -307,6 +327,12 @@ ]; auto-format = true; } + { + name = "cdl"; + scope = "source.cdl"; + file-types = [ "cdl" ]; + grammar = "cdl"; + } ] ++ lib.optionals config.atelier.apps.helix.swift [ { diff --git a/packages/tree-sitter-cdl/.gitignore b/packages/tree-sitter-cdl/.gitignore new file mode 100644 index 0000000..bdead24 --- /dev/null +++ b/packages/tree-sitter-cdl/.gitignore @@ -0,0 +1,4 @@ +node_modules/ +bindings/ +build/ +*.node diff --git a/packages/tree-sitter-cdl/grammar.js b/packages/tree-sitter-cdl/grammar.js new file mode 100644 index 0000000..8bf05cb --- /dev/null +++ b/packages/tree-sitter-cdl/grammar.js @@ -0,0 +1,55 @@ +/// +// @ts-check + +/** + * Tree-sitter grammar for CedarLogic CDL v3 (S-expression format). + * + * CDL v3 structure: + * (cedarlogic + * (version 3) + * (generator "CedarLogic") + * (page INDEX + * (gate TYPE (uuid "...") (at X Y) (angle N) param*) + * (wire (ids "...") segment*))) + * + * Symbols are any non-delimiter chars (not whitespace, parens, or quotes). + * Strings use \" and \\ escapes only. + * No comments exist in the format. + */ +module.exports = grammar({ + name: "cdl", + + extras: ($) => [/\s/], + + rules: { + // Root: a single top-level list + source_file: ($) => $.list, + + // List: ( item* ) + list: ($) => + seq( + "(", + repeat(choice($.list, $.string, $.symbol)), + ")" + ), + + // String: "..." with \" and \\ escapes + string: ($) => + seq( + '"', + repeat( + choice( + $.escape_sequence, + /[^"\\]+/ + ) + ), + '"' + ), + + escape_sequence: (_) => /\\["\\]/, + + // Symbol: any run of non-delimiter characters + // Delimiters are: whitespace, (, ), " + symbol: (_) => /[^\s()"]+/, + }, +}); diff --git a/packages/tree-sitter-cdl/package-lock.json b/packages/tree-sitter-cdl/package-lock.json new file mode 100644 index 0000000..1352fc2 --- /dev/null +++ b/packages/tree-sitter-cdl/package-lock.json @@ -0,0 +1,54 @@ +{ + "name": "tree-sitter-cdl", + "version": "0.1.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "tree-sitter-cdl", + "version": "0.1.0", + "license": "MIT", + "dependencies": { + "node-addon-api": "^8.0.0", + "node-gyp-build": "^4.8.0" + }, + "devDependencies": { + "tree-sitter-cli": "^0.26.0" + } + }, + "node_modules/node-addon-api": { + "version": "8.9.2", + "resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-8.9.2.tgz", + "integrity": "sha512-VijLXbi3UACN69I0JVXJsX4tjACjNoQDgv2gTF6sx2wWEi8tkSg2eX8p5gSIFi8z2+DL3oHmY6OyKce38SDolg==", + "license": "MIT", + "engines": { + "node": "^18 || ^20 || >= 21" + } + }, + "node_modules/node-gyp-build": { + "version": "4.8.4", + "resolved": "https://registry.npmjs.org/node-gyp-build/-/node-gyp-build-4.8.4.tgz", + "integrity": "sha512-LA4ZjwlnUblHVgq0oBF3Jl/6h/Nvs5fzBLwdEF4nuxnFdsfajde4WfxtJr3CaiH+F6ewcIB/q4jQ4UzPyid+CQ==", + "license": "MIT", + "bin": { + "node-gyp-build": "bin.js", + "node-gyp-build-optional": "optional.js", + "node-gyp-build-test": "build-test.js" + } + }, + "node_modules/tree-sitter-cli": { + "version": "0.26.12", + "resolved": "https://registry.npmjs.org/tree-sitter-cli/-/tree-sitter-cli-0.26.12.tgz", + "integrity": "sha512-rIdMAm6nv75MVFoZ7UTaLVB1OG5h3ZSlCZEp5pne4gOUTalJIkL0A1NlYBg0oEl/d7gvIyc2Y5Jz8m5Xj+NOEw==", + "dev": true, + "hasInstallScript": true, + "license": "MIT", + "bin": { + "tree-sitter": "cli.js" + }, + "engines": { + "node": ">=12.0.0" + } + } + } +} diff --git a/packages/tree-sitter-cdl/package.json b/packages/tree-sitter-cdl/package.json new file mode 100644 index 0000000..4a4bfaf --- /dev/null +++ b/packages/tree-sitter-cdl/package.json @@ -0,0 +1,25 @@ +{ + "name": "tree-sitter-cdl", + "version": "0.1.0", + "description": "Tree-sitter grammar for CedarLogic CDL v3 circuit description files", + "main": "bindings/node", + "types": "bindings/node", + "keywords": ["tree-sitter", "cedarlogic", "cdl"], + "files": [ + "grammar.js", + "src/**" + ], + "dependencies": { + "node-addon-api": "^8.0.0", + "node-gyp-build": "^4.8.0" + }, + "devDependencies": { + "tree-sitter-cli": "^0.26.0" + }, + "scripts": { + "generate": "tree-sitter generate", + "test": "tree-sitter test", + "parse": "tree-sitter parse" + }, + "license": "MIT" +} diff --git a/packages/tree-sitter-cdl/queries/highlights.scm b/packages/tree-sitter-cdl/queries/highlights.scm new file mode 100644 index 0000000..585a6b9 --- /dev/null +++ b/packages/tree-sitter-cdl/queries/highlights.scm @@ -0,0 +1,67 @@ +;; CedarLogic CDL v3 highlights + +;; Strings (default catch-all) +(string) @string +(escape_sequence) @constant.character.escape + +;; Known CDL keywords (first element of a list) +(list + . + (symbol) @keyword + (#match? @keyword "^(cedarlogic|version|generator|page|gate|wire|uuid|at|angle|gparam|lparam|ids|seg|pts|connect|cross)$")) + +;; Gate type names: (gate "AA_AND2" ...) +(list + (symbol) @_kw + (#eq? @_kw "gate") + (string) @type + (#match? @type "^\"")) + +;; Segment orientation: (seg "id" h|v ...) +(list + (symbol) @_kw + (#eq? @_kw "seg") + (string) @string.special + (symbol) @variable.builtin + (#match? @variable.builtin "^[hv]$")) + +;; Parameter names: (gparam "NAME" "value") / (lparam "NAME" "value") +(list + (symbol) @_kw + (#match? @_kw "^(gparam|lparam)$") + (string) @variable.parameter) + +;; UUIDs: (uuid "...") +(list + (symbol) @_kw + (#eq? @_kw "uuid") + (string) @string.special) + +;; Wire IDs: (ids "a" "b" ...) +(list + (symbol) @_kw + (#eq? @_kw "ids") + (string) @string.special) + +;; Pin names in connect: (connect "gateUuid" "PIN") +(list + (symbol) @_kw + (#eq? @_kw "connect") + (string) @string.special) + +;; Cross references: (cross at "segId") +(list + (symbol) @_kw + (#eq? @_kw "cross") + (string) @string.special) + +;; Numbers: integers, decimals, and scientific notation (ยง5.2) +(symbol) @number +(#match? @number "^-?([0-9]+(\\.[0-9]*)?|\\.[0-9]+)([eE][+-]?[0-9]+)?$") + +;; Special float values +(symbol) @constant.builtin +(#match? @constant.builtin "^(nan|inf|-inf)$") + +;; Parentheses +["(" ")"] @punctuation.bracket diff --git a/packages/tree-sitter-cdl/src/grammar.json b/packages/tree-sitter-cdl/src/grammar.json new file mode 100644 index 0000000..2103d90 --- /dev/null +++ b/packages/tree-sitter-cdl/src/grammar.json @@ -0,0 +1,92 @@ +{ + "$schema": "https://tree-sitter.github.io/tree-sitter/assets/schemas/grammar.schema.json", + "name": "cdl", + "rules": { + "source_file": { + "type": "SYMBOL", + "name": "list" + }, + "list": { + "type": "SEQ", + "members": [ + { + "type": "STRING", + "value": "(" + }, + { + "type": "REPEAT", + "content": { + "type": "CHOICE", + "members": [ + { + "type": "SYMBOL", + "name": "list" + }, + { + "type": "SYMBOL", + "name": "string" + }, + { + "type": "SYMBOL", + "name": "symbol" + } + ] + } + }, + { + "type": "STRING", + "value": ")" + } + ] + }, + "string": { + "type": "SEQ", + "members": [ + { + "type": "STRING", + "value": "\"" + }, + { + "type": "REPEAT", + "content": { + "type": "CHOICE", + "members": [ + { + "type": "SYMBOL", + "name": "escape_sequence" + }, + { + "type": "PATTERN", + "value": "[^\"\\\\]+" + } + ] + } + }, + { + "type": "STRING", + "value": "\"" + } + ] + }, + "escape_sequence": { + "type": "PATTERN", + "value": "\\\\[\"\\\\]" + }, + "symbol": { + "type": "PATTERN", + "value": "[^\\s()\"]+" + } + }, + "extras": [ + { + "type": "PATTERN", + "value": "\\s" + } + ], + "conflicts": [], + "precedences": [], + "externals": [], + "inline": [], + "supertypes": [], + "reserved": {} +} \ No newline at end of file diff --git a/packages/tree-sitter-cdl/src/node-types.json b/packages/tree-sitter-cdl/src/node-types.json new file mode 100644 index 0000000..8265c86 --- /dev/null +++ b/packages/tree-sitter-cdl/src/node-types.json @@ -0,0 +1,76 @@ +[ + { + "type": "list", + "named": true, + "fields": {}, + "children": { + "multiple": true, + "required": false, + "types": [ + { + "type": "list", + "named": true + }, + { + "type": "string", + "named": true + }, + { + "type": "symbol", + "named": true + } + ] + } + }, + { + "type": "source_file", + "named": true, + "root": true, + "fields": {}, + "children": { + "multiple": false, + "required": true, + "types": [ + { + "type": "list", + "named": true + } + ] + } + }, + { + "type": "string", + "named": true, + "fields": {}, + "children": { + "multiple": true, + "required": false, + "types": [ + { + "type": "escape_sequence", + "named": true + } + ] + } + }, + { + "type": "\"", + "named": false + }, + { + "type": "(", + "named": false + }, + { + "type": ")", + "named": false + }, + { + "type": "escape_sequence", + "named": true + }, + { + "type": "symbol", + "named": true + } +] \ No newline at end of file diff --git a/packages/tree-sitter-cdl/src/parser.c b/packages/tree-sitter-cdl/src/parser.c new file mode 100644 index 0000000..50dec18 --- /dev/null +++ b/packages/tree-sitter-cdl/src/parser.c @@ -0,0 +1,450 @@ +/* Automatically @generated by tree-sitter */ + +#include "tree_sitter/parser.h" + +#if defined(__GNUC__) || defined(__clang__) +#pragma GCC diagnostic ignored "-Wmissing-field-initializers" +#endif + +#define LANGUAGE_VERSION 15 +#define STATE_COUNT 14 +#define LARGE_STATE_COUNT 5 +#define SYMBOL_COUNT 12 +#define ALIAS_COUNT 0 +#define TOKEN_COUNT 7 +#define EXTERNAL_TOKEN_COUNT 0 +#define FIELD_COUNT 0 +#define MAX_ALIAS_SEQUENCE_LENGTH 3 +#define MAX_RESERVED_WORD_SET_SIZE 0 +#define PRODUCTION_ID_COUNT 1 +#define SUPERTYPE_COUNT 0 + +enum ts_symbol_identifiers { + anon_sym_LPAREN = 1, + anon_sym_RPAREN = 2, + anon_sym_DQUOTE = 3, + aux_sym_string_token1 = 4, + sym_escape_sequence = 5, + sym_symbol = 6, + sym_source_file = 7, + sym_list = 8, + sym_string = 9, + aux_sym_list_repeat1 = 10, + aux_sym_string_repeat1 = 11, +}; + +static const char * const ts_symbol_names[] = { + [ts_builtin_sym_end] = "end", + [anon_sym_LPAREN] = "(", + [anon_sym_RPAREN] = ")", + [anon_sym_DQUOTE] = "\"", + [aux_sym_string_token1] = "string_token1", + [sym_escape_sequence] = "escape_sequence", + [sym_symbol] = "symbol", + [sym_source_file] = "source_file", + [sym_list] = "list", + [sym_string] = "string", + [aux_sym_list_repeat1] = "list_repeat1", + [aux_sym_string_repeat1] = "string_repeat1", +}; + +static const TSSymbol ts_symbol_map[] = { + [ts_builtin_sym_end] = ts_builtin_sym_end, + [anon_sym_LPAREN] = anon_sym_LPAREN, + [anon_sym_RPAREN] = anon_sym_RPAREN, + [anon_sym_DQUOTE] = anon_sym_DQUOTE, + [aux_sym_string_token1] = aux_sym_string_token1, + [sym_escape_sequence] = sym_escape_sequence, + [sym_symbol] = sym_symbol, + [sym_source_file] = sym_source_file, + [sym_list] = sym_list, + [sym_string] = sym_string, + [aux_sym_list_repeat1] = aux_sym_list_repeat1, + [aux_sym_string_repeat1] = aux_sym_string_repeat1, +}; + +static const TSSymbolMetadata ts_symbol_metadata[] = { + [ts_builtin_sym_end] = { + .visible = false, + .named = true, + }, + [anon_sym_LPAREN] = { + .visible = true, + .named = false, + }, + [anon_sym_RPAREN] = { + .visible = true, + .named = false, + }, + [anon_sym_DQUOTE] = { + .visible = true, + .named = false, + }, + [aux_sym_string_token1] = { + .visible = false, + .named = false, + }, + [sym_escape_sequence] = { + .visible = true, + .named = true, + }, + [sym_symbol] = { + .visible = true, + .named = true, + }, + [sym_source_file] = { + .visible = true, + .named = true, + }, + [sym_list] = { + .visible = true, + .named = true, + }, + [sym_string] = { + .visible = true, + .named = true, + }, + [aux_sym_list_repeat1] = { + .visible = false, + .named = false, + }, + [aux_sym_string_repeat1] = { + .visible = false, + .named = false, + }, +}; + +static const TSSymbol ts_alias_sequences[PRODUCTION_ID_COUNT][MAX_ALIAS_SEQUENCE_LENGTH] = { + [0] = {0}, +}; + +static const uint16_t ts_non_terminal_alias_map[] = { + 0, +}; + +static const TSStateId ts_primary_state_ids[STATE_COUNT] = { + [0] = 0, + [1] = 1, + [2] = 2, + [3] = 3, + [4] = 4, + [5] = 5, + [6] = 6, + [7] = 7, + [8] = 8, + [9] = 9, + [10] = 10, + [11] = 11, + [12] = 12, + [13] = 13, +}; + +static bool ts_lex(TSLexer *lexer, TSStateId state) { + START_LEXER(); + eof = lexer->eof(lexer); + switch (state) { + case 0: + if (eof) ADVANCE(4); + if (lookahead == '"') ADVANCE(7); + if (lookahead == '(') ADVANCE(5); + if (lookahead == ')') ADVANCE(6); + if (lookahead == '\\') ADVANCE(12); + if (('\t' <= lookahead && lookahead <= '\r') || + lookahead == ' ') SKIP(0); + if (lookahead != 0) ADVANCE(13); + END_STATE(); + case 1: + if (lookahead == '"') ADVANCE(7); + if (lookahead == '\\') ADVANCE(2); + if (('\t' <= lookahead && lookahead <= '\r') || + lookahead == ' ') ADVANCE(8); + if (lookahead != 0) ADVANCE(9); + END_STATE(); + case 2: + if (lookahead == '"' || + lookahead == '\\') ADVANCE(10); + END_STATE(); + case 3: + if (eof) ADVANCE(4); + if (lookahead == '"') ADVANCE(7); + if (lookahead == '(') ADVANCE(5); + if (lookahead == ')') ADVANCE(6); + if (('\t' <= lookahead && lookahead <= '\r') || + lookahead == ' ') SKIP(3); + if (lookahead != 0) ADVANCE(13); + END_STATE(); + case 4: + ACCEPT_TOKEN(ts_builtin_sym_end); + END_STATE(); + case 5: + ACCEPT_TOKEN(anon_sym_LPAREN); + END_STATE(); + case 6: + ACCEPT_TOKEN(anon_sym_RPAREN); + END_STATE(); + case 7: + ACCEPT_TOKEN(anon_sym_DQUOTE); + END_STATE(); + case 8: + ACCEPT_TOKEN(aux_sym_string_token1); + if (('\t' <= lookahead && lookahead <= '\r') || + lookahead == ' ') ADVANCE(8); + if (lookahead != 0 && + lookahead != '"' && + lookahead != '\\') ADVANCE(9); + END_STATE(); + case 9: + ACCEPT_TOKEN(aux_sym_string_token1); + if (lookahead != 0 && + lookahead != '"' && + lookahead != '\\') ADVANCE(9); + END_STATE(); + case 10: + ACCEPT_TOKEN(sym_escape_sequence); + END_STATE(); + case 11: + ACCEPT_TOKEN(sym_escape_sequence); + if (lookahead != 0 && + (lookahead < '\t' || '\r' < lookahead) && + lookahead != ' ' && + lookahead != '"' && + lookahead != '(' && + lookahead != ')') ADVANCE(13); + END_STATE(); + case 12: + ACCEPT_TOKEN(sym_symbol); + if (lookahead == '"') ADVANCE(10); + if (lookahead == '\\') ADVANCE(11); + if (lookahead != 0 && + (lookahead < '\t' || '\r' < lookahead) && + lookahead != ' ' && + lookahead != '(' && + lookahead != ')') ADVANCE(13); + END_STATE(); + case 13: + ACCEPT_TOKEN(sym_symbol); + if (lookahead != 0 && + (lookahead < '\t' || '\r' < lookahead) && + lookahead != ' ' && + lookahead != '"' && + lookahead != '(' && + lookahead != ')') ADVANCE(13); + END_STATE(); + default: + return false; + } +} + +static const TSLexerMode ts_lex_modes[STATE_COUNT] = { + [0] = {.lex_state = 0}, + [1] = {.lex_state = 0}, + [2] = {.lex_state = 3}, + [3] = {.lex_state = 3}, + [4] = {.lex_state = 3}, + [5] = {.lex_state = 3}, + [6] = {.lex_state = 3}, + [7] = {.lex_state = 1}, + [8] = {.lex_state = 3}, + [9] = {.lex_state = 1}, + [10] = {.lex_state = 3}, + [11] = {.lex_state = 1}, + [12] = {.lex_state = 0}, + [13] = {.lex_state = 0}, +}; + +static const uint16_t ts_parse_table[LARGE_STATE_COUNT][SYMBOL_COUNT] = { + [STATE(0)] = { + [ts_builtin_sym_end] = ACTIONS(1), + [anon_sym_LPAREN] = ACTIONS(1), + [anon_sym_RPAREN] = ACTIONS(1), + [anon_sym_DQUOTE] = ACTIONS(1), + [sym_escape_sequence] = ACTIONS(1), + [sym_symbol] = ACTIONS(1), + }, + [STATE(1)] = { + [sym_source_file] = STATE(12), + [sym_list] = STATE(13), + [anon_sym_LPAREN] = ACTIONS(3), + }, + [STATE(2)] = { + [sym_list] = STATE(3), + [sym_string] = STATE(3), + [aux_sym_list_repeat1] = STATE(3), + [anon_sym_LPAREN] = ACTIONS(3), + [anon_sym_RPAREN] = ACTIONS(5), + [anon_sym_DQUOTE] = ACTIONS(7), + [sym_symbol] = ACTIONS(9), + }, + [STATE(3)] = { + [sym_list] = STATE(4), + [sym_string] = STATE(4), + [aux_sym_list_repeat1] = STATE(4), + [anon_sym_LPAREN] = ACTIONS(3), + [anon_sym_RPAREN] = ACTIONS(11), + [anon_sym_DQUOTE] = ACTIONS(7), + [sym_symbol] = ACTIONS(13), + }, + [STATE(4)] = { + [sym_list] = STATE(4), + [sym_string] = STATE(4), + [aux_sym_list_repeat1] = STATE(4), + [anon_sym_LPAREN] = ACTIONS(15), + [anon_sym_RPAREN] = ACTIONS(18), + [anon_sym_DQUOTE] = ACTIONS(20), + [sym_symbol] = ACTIONS(23), + }, +}; + +static const uint16_t ts_small_parse_table[] = { + [0] = 1, + ACTIONS(26), 5, + ts_builtin_sym_end, + anon_sym_LPAREN, + anon_sym_RPAREN, + anon_sym_DQUOTE, + sym_symbol, + [8] = 1, + ACTIONS(28), 5, + ts_builtin_sym_end, + anon_sym_LPAREN, + anon_sym_RPAREN, + anon_sym_DQUOTE, + sym_symbol, + [16] = 4, + ACTIONS(30), 1, + anon_sym_DQUOTE, + ACTIONS(32), 1, + aux_sym_string_token1, + ACTIONS(34), 1, + sym_escape_sequence, + STATE(9), 1, + aux_sym_string_repeat1, + [29] = 1, + ACTIONS(36), 4, + anon_sym_LPAREN, + anon_sym_RPAREN, + anon_sym_DQUOTE, + sym_symbol, + [36] = 4, + ACTIONS(38), 1, + anon_sym_DQUOTE, + ACTIONS(40), 1, + aux_sym_string_token1, + ACTIONS(42), 1, + sym_escape_sequence, + STATE(11), 1, + aux_sym_string_repeat1, + [49] = 1, + ACTIONS(44), 4, + anon_sym_LPAREN, + anon_sym_RPAREN, + anon_sym_DQUOTE, + sym_symbol, + [56] = 4, + ACTIONS(46), 1, + anon_sym_DQUOTE, + ACTIONS(48), 1, + aux_sym_string_token1, + ACTIONS(51), 1, + sym_escape_sequence, + STATE(11), 1, + aux_sym_string_repeat1, + [69] = 1, + ACTIONS(54), 1, + ts_builtin_sym_end, + [73] = 1, + ACTIONS(56), 1, + ts_builtin_sym_end, +}; + +static const uint32_t ts_small_parse_table_map[] = { + [SMALL_STATE(5)] = 0, + [SMALL_STATE(6)] = 8, + [SMALL_STATE(7)] = 16, + [SMALL_STATE(8)] = 29, + [SMALL_STATE(9)] = 36, + [SMALL_STATE(10)] = 49, + [SMALL_STATE(11)] = 56, + [SMALL_STATE(12)] = 69, + [SMALL_STATE(13)] = 73, +}; + +static const TSParseActionEntry ts_parse_actions[] = { + [0] = {.entry = {.count = 0, .reusable = false}}, + [1] = {.entry = {.count = 1, .reusable = false}}, RECOVER(), + [3] = {.entry = {.count = 1, .reusable = true}}, SHIFT(2), + [5] = {.entry = {.count = 1, .reusable = true}}, SHIFT(5), + [7] = {.entry = {.count = 1, .reusable = true}}, SHIFT(7), + [9] = {.entry = {.count = 1, .reusable = true}}, SHIFT(3), + [11] = {.entry = {.count = 1, .reusable = true}}, SHIFT(6), + [13] = {.entry = {.count = 1, .reusable = true}}, SHIFT(4), + [15] = {.entry = {.count = 2, .reusable = true}}, REDUCE(aux_sym_list_repeat1, 2, 0, 0), SHIFT_REPEAT(2), + [18] = {.entry = {.count = 1, .reusable = true}}, REDUCE(aux_sym_list_repeat1, 2, 0, 0), + [20] = {.entry = {.count = 2, .reusable = true}}, REDUCE(aux_sym_list_repeat1, 2, 0, 0), SHIFT_REPEAT(7), + [23] = {.entry = {.count = 2, .reusable = true}}, REDUCE(aux_sym_list_repeat1, 2, 0, 0), SHIFT_REPEAT(4), + [26] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_list, 2, 0, 0), + [28] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_list, 3, 0, 0), + [30] = {.entry = {.count = 1, .reusable = false}}, SHIFT(8), + [32] = {.entry = {.count = 1, .reusable = true}}, SHIFT(9), + [34] = {.entry = {.count = 1, .reusable = false}}, SHIFT(9), + [36] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_string, 2, 0, 0), + [38] = {.entry = {.count = 1, .reusable = false}}, SHIFT(10), + [40] = {.entry = {.count = 1, .reusable = true}}, SHIFT(11), + [42] = {.entry = {.count = 1, .reusable = false}}, SHIFT(11), + [44] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_string, 3, 0, 0), + [46] = {.entry = {.count = 1, .reusable = false}}, REDUCE(aux_sym_string_repeat1, 2, 0, 0), + [48] = {.entry = {.count = 2, .reusable = true}}, REDUCE(aux_sym_string_repeat1, 2, 0, 0), SHIFT_REPEAT(11), + [51] = {.entry = {.count = 2, .reusable = false}}, REDUCE(aux_sym_string_repeat1, 2, 0, 0), SHIFT_REPEAT(11), + [54] = {.entry = {.count = 1, .reusable = true}}, ACCEPT_INPUT(), + [56] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_source_file, 1, 0, 0), +}; + +#ifdef __cplusplus +extern "C" { +#endif +#ifdef TREE_SITTER_HIDE_SYMBOLS +#define TS_PUBLIC +#elif defined(_WIN32) +#define TS_PUBLIC __declspec(dllexport) +#else +#define TS_PUBLIC __attribute__((visibility("default"))) +#endif + +TS_PUBLIC const TSLanguage *tree_sitter_cdl(void) { + static const TSLanguage language = { + .abi_version = LANGUAGE_VERSION, + .symbol_count = SYMBOL_COUNT, + .alias_count = ALIAS_COUNT, + .token_count = TOKEN_COUNT, + .external_token_count = EXTERNAL_TOKEN_COUNT, + .state_count = STATE_COUNT, + .large_state_count = LARGE_STATE_COUNT, + .production_id_count = PRODUCTION_ID_COUNT, + .supertype_count = SUPERTYPE_COUNT, + .field_count = FIELD_COUNT, + .max_alias_sequence_length = MAX_ALIAS_SEQUENCE_LENGTH, + .parse_table = &ts_parse_table[0][0], + .small_parse_table = ts_small_parse_table, + .small_parse_table_map = ts_small_parse_table_map, + .parse_actions = ts_parse_actions, + .symbol_names = ts_symbol_names, + .symbol_metadata = ts_symbol_metadata, + .public_symbol_map = ts_symbol_map, + .alias_map = ts_non_terminal_alias_map, + .alias_sequences = &ts_alias_sequences[0][0], + .lex_modes = (const void*)ts_lex_modes, + .lex_fn = ts_lex, + .primary_state_ids = ts_primary_state_ids, + .name = "cdl", + .max_reserved_word_set_size = 0, + .metadata = { + .major_version = 0, + .minor_version = 1, + .patch_version = 0, + }, + }; + return &language; +} +#ifdef __cplusplus +} +#endif diff --git a/packages/tree-sitter-cdl/src/tree_sitter/alloc.h b/packages/tree-sitter-cdl/src/tree_sitter/alloc.h new file mode 100644 index 0000000..1abdd12 --- /dev/null +++ b/packages/tree-sitter-cdl/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/packages/tree-sitter-cdl/src/tree_sitter/array.h b/packages/tree-sitter-cdl/src/tree_sitter/array.h new file mode 100644 index 0000000..eba3da4 --- /dev/null +++ b/packages/tree-sitter-cdl/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/packages/tree-sitter-cdl/src/tree_sitter/parser.h b/packages/tree-sitter-cdl/src/tree_sitter/parser.h new file mode 100644 index 0000000..858107d --- /dev/null +++ b/packages/tree-sitter-cdl/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/packages/tree-sitter-cdl/test/corpus/cdl.txt b/packages/tree-sitter-cdl/test/corpus/cdl.txt new file mode 100644 index 0000000..e8f9bcc --- /dev/null +++ b/packages/tree-sitter-cdl/test/corpus/cdl.txt @@ -0,0 +1,130 @@ +================================================================================ +Minimal CDL v3 document +================================================================================ + +(cedarlogic + (version 3) + (generator "CedarLogic test")) + +-------------------------------------------------------------------------------- + +(source_file + (list + (symbol) + (list + (symbol) + (symbol)) + (list + (symbol) + (string)))) + +================================================================================ +Gate with parameters +================================================================================ + +(gate "AA_AND2" + (uuid "b21c") + (at 12 -4) + (angle 90) + (lparam "INPUT_BITS" "2")) + +-------------------------------------------------------------------------------- + +(source_file + (list + (symbol) + (string) + (list + (symbol) + (string)) + (list + (symbol) + (symbol) + (symbol)) + (list + (symbol) + (symbol)) + (list + (symbol) + (string) + (string)))) + +================================================================================ +Wire with segments +================================================================================ + +(wire + (ids "77e0") + (seg "0" h + (pts -5 0 3 0) + (connect "3f9a" "OUT_0") + (cross 3 "1"))) + +-------------------------------------------------------------------------------- + +(source_file + (list + (symbol) + (list + (symbol) + (string)) + (list + (symbol) + (string) + (symbol) + (list + (symbol) + (symbol) + (symbol) + (symbol) + (symbol)) + (list + (symbol) + (string) + (string)) + (list + (symbol) + (symbol) + (string))))) + +================================================================================ +String escapes +================================================================================ + +(generator "hello \"world\" and \\backslash") + +-------------------------------------------------------------------------------- + +(source_file + (list + (symbol) + (string + (escape_sequence) + (escape_sequence) + (escape_sequence)))) + +================================================================================ +Empty list +================================================================================ + +(empty) + +-------------------------------------------------------------------------------- + +(source_file + (list + (symbol))) + +================================================================================ +Negative numbers as symbols +================================================================================ + +(at -8 -3.5) + +-------------------------------------------------------------------------------- + +(source_file + (list + (symbol) + (symbol) + (symbol))) diff --git a/packages/tree-sitter-cdl/tree-sitter.json b/packages/tree-sitter-cdl/tree-sitter.json new file mode 100644 index 0000000..f4c1b40 --- /dev/null +++ b/packages/tree-sitter-cdl/tree-sitter.json @@ -0,0 +1,30 @@ +{ + "grammars": [ + { + "name": "cdl", + "camelcase": "Cdl", + "scope": "source.cdl", + "path": ".", + "file-types": ["cdl"], + "highlights": "queries/highlights.scm" + } + ], + "metadata": { + "version": "0.1.0", + "license": "MIT", + "description": "Tree-sitter grammar for CedarLogic CDL v3 circuit description files", + "authors": [ + { + "name": "Kieran Klukas" + } + ] + }, + "bindings": { + "c": true, + "go": false, + "node": false, + "python": false, + "rust": false, + "swift": false + } +} -- 2.51.2