add highlights; test on simplified grammar

This commit is contained in:
Michael Sippel 2025-06-13 10:13:35 +02:00
parent 824f590b03
commit 21646a9209
Signed by: senvas
GPG key ID: F96CF119C34B64A6
6 changed files with 136 additions and 133 deletions

View file

@ -15,11 +15,11 @@ module.exports = grammar({
morphbase: ($) => seq($.include, repeat($.stmnt)),
include: ($) => seq("(", $.ident, ")"),
stmnt: ($) => choice($.comment, seq($.def, ";")),
stmnt: ($) => choice($.comment, $.def),
comment: (_) => seq("/*", /(.*)/, "*/"),
def: ($) => choice($.typedef, $.morphdef),
typedef: ($) => seq("type", $.ident, "=", $.type),
morphdef: ($) => seq("morph", $.ident, ":", $.type, "=", $.impl),
typedef: ($) => seq("type", $.ident, "=", $.type, ";"),
morphdef: ($) => seq("morph", $.ident, ":", $.type, "=", $.impl, ";"),
ident: ($) => /([a-zA-Z$][0-9a-zA-Z_\-]*)/,
type: ($) => /([a-zA-Z$][0-9a-zA-Z_\-\.]*)/,
impl: ($) => seq("'", /(.)/, "'"),

View file

@ -0,0 +1,4 @@
"type" @keyword
"morph" @keyword
(type) @type
(impl) @string

21
src/grammar.json generated
View file

@ -47,17 +47,8 @@
"name": "comment"
},
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "def"
},
{
"type": "STRING",
"value": ";"
}
]
"type": "SYMBOL",
"name": "def"
}
]
},
@ -109,6 +100,10 @@
{
"type": "SYMBOL",
"name": "type"
},
{
"type": "STRING",
"value": ";"
}
]
},
@ -138,6 +133,10 @@
{
"type": "SYMBOL",
"name": "impl"
},
{
"type": "STRING",
"value": ";"
}
]
},

221
src/parser.c generated
View file

@ -14,7 +14,7 @@
#define TOKEN_COUNT 15
#define EXTERNAL_TOKEN_COUNT 0
#define FIELD_COUNT 0
#define MAX_ALIAS_SEQUENCE_LENGTH 6
#define MAX_ALIAS_SEQUENCE_LENGTH 7
#define MAX_RESERVED_WORD_SET_SIZE 0
#define PRODUCTION_ID_COUNT 1
#define SUPERTYPE_COUNT 0
@ -22,12 +22,12 @@
enum ts_symbol_identifiers {
anon_sym_LPAREN = 1,
anon_sym_RPAREN = 2,
anon_sym_SEMI = 3,
anon_sym_SLASH_STAR = 4,
aux_sym_comment_token1 = 5,
anon_sym_STAR_SLASH = 6,
anon_sym_type = 7,
anon_sym_EQ = 8,
anon_sym_SLASH_STAR = 3,
aux_sym_comment_token1 = 4,
anon_sym_STAR_SLASH = 5,
anon_sym_type = 6,
anon_sym_EQ = 7,
anon_sym_SEMI = 8,
anon_sym_morph = 9,
anon_sym_COLON = 10,
sym_ident = 11,
@ -50,12 +50,12 @@ static const char * const ts_symbol_names[] = {
[ts_builtin_sym_end] = "end",
[anon_sym_LPAREN] = "(",
[anon_sym_RPAREN] = ")",
[anon_sym_SEMI] = ";",
[anon_sym_SLASH_STAR] = "/*",
[aux_sym_comment_token1] = "comment_token1",
[anon_sym_STAR_SLASH] = "*/",
[anon_sym_type] = "type",
[anon_sym_EQ] = "=",
[anon_sym_SEMI] = ";",
[anon_sym_morph] = "morph",
[anon_sym_COLON] = ":",
[sym_ident] = "ident",
@ -78,12 +78,12 @@ 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_SEMI] = anon_sym_SEMI,
[anon_sym_SLASH_STAR] = anon_sym_SLASH_STAR,
[aux_sym_comment_token1] = aux_sym_comment_token1,
[anon_sym_STAR_SLASH] = anon_sym_STAR_SLASH,
[anon_sym_type] = anon_sym_type,
[anon_sym_EQ] = anon_sym_EQ,
[anon_sym_SEMI] = anon_sym_SEMI,
[anon_sym_morph] = anon_sym_morph,
[anon_sym_COLON] = anon_sym_COLON,
[sym_ident] = sym_ident,
@ -115,10 +115,6 @@ static const TSSymbolMetadata ts_symbol_metadata[] = {
.visible = true,
.named = false,
},
[anon_sym_SEMI] = {
.visible = true,
.named = false,
},
[anon_sym_SLASH_STAR] = {
.visible = true,
.named = false,
@ -139,6 +135,10 @@ static const TSSymbolMetadata ts_symbol_metadata[] = {
.visible = true,
.named = false,
},
[anon_sym_SEMI] = {
.visible = true,
.named = false,
},
[anon_sym_morph] = {
.visible = true,
.named = false,
@ -259,8 +259,8 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) {
'*', 3,
'/', 2,
':', 24,
';', 16,
'=', 22,
';', 22,
'=', 21,
'm', 6,
't', 10,
);
@ -274,13 +274,13 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) {
if (lookahead != 0) ADVANCE(28);
END_STATE();
case 2:
if (lookahead == '*') ADVANCE(17);
if (lookahead == '*') ADVANCE(16);
END_STATE();
case 3:
if (lookahead == '/') ADVANCE(20);
if (lookahead == '/') ADVANCE(19);
END_STATE();
case 4:
if (lookahead == 'e') ADVANCE(21);
if (lookahead == 'e') ADVANCE(20);
END_STATE();
case 5:
if (lookahead == 'h') ADVANCE(23);
@ -324,33 +324,33 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) {
ACCEPT_TOKEN(anon_sym_RPAREN);
END_STATE();
case 16:
ACCEPT_TOKEN(anon_sym_SEMI);
END_STATE();
case 17:
ACCEPT_TOKEN(anon_sym_SLASH_STAR);
END_STATE();
case 18:
case 17:
ACCEPT_TOKEN(aux_sym_comment_token1);
if (lookahead == '\t' ||
(0x0b <= lookahead && lookahead <= '\r') ||
lookahead == ' ') ADVANCE(18);
lookahead == ' ') ADVANCE(17);
if (lookahead != 0 &&
(lookahead < '\t' || '\r' < lookahead)) ADVANCE(19);
(lookahead < '\t' || '\r' < lookahead)) ADVANCE(18);
END_STATE();
case 19:
case 18:
ACCEPT_TOKEN(aux_sym_comment_token1);
if (lookahead != 0 &&
lookahead != '\n') ADVANCE(19);
lookahead != '\n') ADVANCE(18);
END_STATE();
case 20:
case 19:
ACCEPT_TOKEN(anon_sym_STAR_SLASH);
END_STATE();
case 21:
case 20:
ACCEPT_TOKEN(anon_sym_type);
END_STATE();
case 22:
case 21:
ACCEPT_TOKEN(anon_sym_EQ);
END_STATE();
case 22:
ACCEPT_TOKEN(anon_sym_SEMI);
END_STATE();
case 23:
ACCEPT_TOKEN(anon_sym_morph);
END_STATE();
@ -405,19 +405,19 @@ static const TSLexerMode ts_lex_modes[STATE_COUNT] = {
[8] = {.lex_state = 0},
[9] = {.lex_state = 0},
[10] = {.lex_state = 0},
[11] = {.lex_state = 11},
[11] = {.lex_state = 0},
[12] = {.lex_state = 0},
[13] = {.lex_state = 0},
[14] = {.lex_state = 18},
[13] = {.lex_state = 11},
[14] = {.lex_state = 0},
[15] = {.lex_state = 11},
[16] = {.lex_state = 11},
[17] = {.lex_state = 0},
[17] = {.lex_state = 17},
[18] = {.lex_state = 0},
[19] = {.lex_state = 0},
[20] = {.lex_state = 0},
[21] = {.lex_state = 12},
[22] = {.lex_state = 12},
[23] = {.lex_state = 0},
[22] = {.lex_state = 0},
[23] = {.lex_state = 12},
[24] = {.lex_state = 0},
[25] = {.lex_state = 0},
[26] = {.lex_state = 1},
@ -431,25 +431,25 @@ static const uint16_t ts_parse_table[LARGE_STATE_COUNT][SYMBOL_COUNT] = {
[ts_builtin_sym_end] = ACTIONS(1),
[anon_sym_LPAREN] = ACTIONS(1),
[anon_sym_RPAREN] = ACTIONS(1),
[anon_sym_SEMI] = ACTIONS(1),
[anon_sym_SLASH_STAR] = ACTIONS(1),
[anon_sym_STAR_SLASH] = ACTIONS(1),
[anon_sym_type] = ACTIONS(1),
[anon_sym_EQ] = ACTIONS(1),
[anon_sym_SEMI] = ACTIONS(1),
[anon_sym_morph] = ACTIONS(1),
[anon_sym_COLON] = ACTIONS(1),
[anon_sym_SQUOTE] = ACTIONS(1),
},
[STATE(1)] = {
[sym_source_file] = STATE(10),
[sym_morphbase] = STATE(13),
[sym_source_file] = STATE(14),
[sym_morphbase] = STATE(18),
[sym_include] = STATE(2),
[anon_sym_LPAREN] = ACTIONS(3),
},
};
static const uint16_t ts_small_parse_table[] = {
[0] = 8,
[0] = 7,
ACTIONS(5), 1,
ts_builtin_sym_end,
ACTIONS(7), 1,
@ -458,17 +458,16 @@ static const uint16_t ts_small_parse_table[] = {
anon_sym_type,
ACTIONS(11), 1,
anon_sym_morph,
STATE(5), 1,
sym_comment,
STATE(17), 1,
sym_def,
STATE(3), 2,
sym_stmnt,
aux_sym_morphbase_repeat1,
STATE(18), 2,
STATE(5), 2,
sym_typedef,
sym_morphdef,
[27] = 8,
STATE(6), 2,
sym_comment,
sym_def,
[25] = 7,
ACTIONS(7), 1,
anon_sym_SLASH_STAR,
ACTIONS(9), 1,
@ -477,17 +476,16 @@ static const uint16_t ts_small_parse_table[] = {
anon_sym_morph,
ACTIONS(13), 1,
ts_builtin_sym_end,
STATE(5), 1,
sym_comment,
STATE(17), 1,
sym_def,
STATE(4), 2,
sym_stmnt,
aux_sym_morphbase_repeat1,
STATE(18), 2,
STATE(5), 2,
sym_typedef,
sym_morphdef,
[54] = 8,
STATE(6), 2,
sym_comment,
sym_def,
[50] = 7,
ACTIONS(15), 1,
ts_builtin_sym_end,
ACTIONS(17), 1,
@ -496,60 +494,65 @@ static const uint16_t ts_small_parse_table[] = {
anon_sym_type,
ACTIONS(23), 1,
anon_sym_morph,
STATE(5), 1,
sym_comment,
STATE(17), 1,
sym_def,
STATE(4), 2,
sym_stmnt,
aux_sym_morphbase_repeat1,
STATE(18), 2,
STATE(5), 2,
sym_typedef,
sym_morphdef,
[81] = 1,
STATE(6), 2,
sym_comment,
sym_def,
[75] = 1,
ACTIONS(26), 4,
ts_builtin_sym_end,
anon_sym_SLASH_STAR,
anon_sym_type,
anon_sym_morph,
[88] = 1,
[82] = 1,
ACTIONS(28), 4,
ts_builtin_sym_end,
anon_sym_SLASH_STAR,
anon_sym_type,
anon_sym_morph,
[95] = 1,
[89] = 1,
ACTIONS(30), 4,
ts_builtin_sym_end,
anon_sym_SLASH_STAR,
anon_sym_type,
anon_sym_morph,
[102] = 1,
[96] = 1,
ACTIONS(32), 4,
ts_builtin_sym_end,
anon_sym_SLASH_STAR,
anon_sym_type,
anon_sym_morph,
[109] = 2,
ACTIONS(34), 1,
[103] = 1,
ACTIONS(34), 4,
ts_builtin_sym_end,
anon_sym_SLASH_STAR,
anon_sym_type,
anon_sym_morph,
[110] = 1,
ACTIONS(36), 4,
ts_builtin_sym_end,
anon_sym_SLASH_STAR,
anon_sym_type,
anon_sym_morph,
[117] = 2,
ACTIONS(38), 1,
anon_sym_SQUOTE,
STATE(27), 1,
sym_impl,
[116] = 1,
ACTIONS(36), 1,
ts_builtin_sym_end,
[120] = 1,
ACTIONS(38), 1,
sym_ident,
[124] = 1,
ACTIONS(40), 1,
anon_sym_RPAREN,
[128] = 1,
ACTIONS(42), 1,
ts_builtin_sym_end,
sym_ident,
[132] = 1,
ACTIONS(44), 1,
aux_sym_comment_token1,
ts_builtin_sym_end,
[136] = 1,
ACTIONS(46), 1,
sym_ident,
@ -558,28 +561,28 @@ static const uint16_t ts_small_parse_table[] = {
sym_ident,
[144] = 1,
ACTIONS(50), 1,
anon_sym_SEMI,
aux_sym_comment_token1,
[148] = 1,
ACTIONS(52), 1,
anon_sym_SEMI,
ts_builtin_sym_end,
[152] = 1,
ACTIONS(54), 1,
anon_sym_COLON,
anon_sym_STAR_SLASH,
[156] = 1,
ACTIONS(56), 1,
anon_sym_STAR_SLASH,
anon_sym_EQ,
[160] = 1,
ACTIONS(58), 1,
sym_type,
[164] = 1,
ACTIONS(60), 1,
sym_type,
anon_sym_COLON,
[168] = 1,
ACTIONS(62), 1,
anon_sym_SEMI,
sym_type,
[172] = 1,
ACTIONS(64), 1,
anon_sym_EQ,
anon_sym_SEMI,
[176] = 1,
ACTIONS(66), 1,
anon_sym_EQ,
@ -599,15 +602,15 @@ static const uint16_t ts_small_parse_table[] = {
static const uint32_t ts_small_parse_table_map[] = {
[SMALL_STATE(2)] = 0,
[SMALL_STATE(3)] = 27,
[SMALL_STATE(4)] = 54,
[SMALL_STATE(5)] = 81,
[SMALL_STATE(6)] = 88,
[SMALL_STATE(7)] = 95,
[SMALL_STATE(8)] = 102,
[SMALL_STATE(9)] = 109,
[SMALL_STATE(10)] = 116,
[SMALL_STATE(11)] = 120,
[SMALL_STATE(3)] = 25,
[SMALL_STATE(4)] = 50,
[SMALL_STATE(5)] = 75,
[SMALL_STATE(6)] = 82,
[SMALL_STATE(7)] = 89,
[SMALL_STATE(8)] = 96,
[SMALL_STATE(9)] = 103,
[SMALL_STATE(10)] = 110,
[SMALL_STATE(11)] = 117,
[SMALL_STATE(12)] = 124,
[SMALL_STATE(13)] = 128,
[SMALL_STATE(14)] = 132,
@ -631,39 +634,39 @@ static const uint32_t ts_small_parse_table_map[] = {
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(11),
[3] = {.entry = {.count = 1, .reusable = true}}, SHIFT(13),
[5] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_morphbase, 1, 0, 0),
[7] = {.entry = {.count = 1, .reusable = true}}, SHIFT(14),
[7] = {.entry = {.count = 1, .reusable = true}}, SHIFT(17),
[9] = {.entry = {.count = 1, .reusable = true}}, SHIFT(16),
[11] = {.entry = {.count = 1, .reusable = true}}, SHIFT(15),
[13] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_morphbase, 2, 0, 0),
[15] = {.entry = {.count = 1, .reusable = true}}, REDUCE(aux_sym_morphbase_repeat1, 2, 0, 0),
[17] = {.entry = {.count = 2, .reusable = true}}, REDUCE(aux_sym_morphbase_repeat1, 2, 0, 0), SHIFT_REPEAT(14),
[17] = {.entry = {.count = 2, .reusable = true}}, REDUCE(aux_sym_morphbase_repeat1, 2, 0, 0), SHIFT_REPEAT(17),
[20] = {.entry = {.count = 2, .reusable = true}}, REDUCE(aux_sym_morphbase_repeat1, 2, 0, 0), SHIFT_REPEAT(16),
[23] = {.entry = {.count = 2, .reusable = true}}, REDUCE(aux_sym_morphbase_repeat1, 2, 0, 0), SHIFT_REPEAT(15),
[26] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_stmnt, 1, 0, 0),
[28] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_include, 3, 0, 0),
[30] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_stmnt, 2, 0, 0),
[26] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_def, 1, 0, 0),
[28] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_stmnt, 1, 0, 0),
[30] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_include, 3, 0, 0),
[32] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_comment, 3, 0, 0),
[34] = {.entry = {.count = 1, .reusable = true}}, SHIFT(26),
[36] = {.entry = {.count = 1, .reusable = true}}, ACCEPT_INPUT(),
[38] = {.entry = {.count = 1, .reusable = true}}, SHIFT(12),
[40] = {.entry = {.count = 1, .reusable = true}}, SHIFT(6),
[42] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_source_file, 1, 0, 0),
[44] = {.entry = {.count = 1, .reusable = true}}, SHIFT(20),
[46] = {.entry = {.count = 1, .reusable = true}}, SHIFT(19),
[48] = {.entry = {.count = 1, .reusable = true}}, SHIFT(25),
[50] = {.entry = {.count = 1, .reusable = true}}, SHIFT(7),
[52] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_def, 1, 0, 0),
[54] = {.entry = {.count = 1, .reusable = true}}, SHIFT(22),
[56] = {.entry = {.count = 1, .reusable = true}}, SHIFT(8),
[58] = {.entry = {.count = 1, .reusable = true}}, SHIFT(23),
[60] = {.entry = {.count = 1, .reusable = true}}, SHIFT(24),
[62] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_typedef, 4, 0, 0),
[34] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_typedef, 5, 0, 0),
[36] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_morphdef, 7, 0, 0),
[38] = {.entry = {.count = 1, .reusable = true}}, SHIFT(26),
[40] = {.entry = {.count = 1, .reusable = true}}, SHIFT(7),
[42] = {.entry = {.count = 1, .reusable = true}}, SHIFT(12),
[44] = {.entry = {.count = 1, .reusable = true}}, ACCEPT_INPUT(),
[46] = {.entry = {.count = 1, .reusable = true}}, SHIFT(22),
[48] = {.entry = {.count = 1, .reusable = true}}, SHIFT(20),
[50] = {.entry = {.count = 1, .reusable = true}}, SHIFT(19),
[52] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_source_file, 1, 0, 0),
[54] = {.entry = {.count = 1, .reusable = true}}, SHIFT(8),
[56] = {.entry = {.count = 1, .reusable = true}}, SHIFT(23),
[58] = {.entry = {.count = 1, .reusable = true}}, SHIFT(25),
[60] = {.entry = {.count = 1, .reusable = true}}, SHIFT(21),
[62] = {.entry = {.count = 1, .reusable = true}}, SHIFT(24),
[64] = {.entry = {.count = 1, .reusable = true}}, SHIFT(9),
[66] = {.entry = {.count = 1, .reusable = true}}, SHIFT(21),
[66] = {.entry = {.count = 1, .reusable = true}}, SHIFT(11),
[68] = {.entry = {.count = 1, .reusable = true}}, SHIFT(28),
[70] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_morphdef, 6, 0, 0),
[70] = {.entry = {.count = 1, .reusable = true}}, SHIFT(10),
[72] = {.entry = {.count = 1, .reusable = true}}, SHIFT(29),
[74] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_impl, 3, 0, 0),
};

View file

@ -1,10 +1,5 @@
```
#include <stdio.h>
```
(stdio)
type PosInt : ∀(Radix:).∀(Endianness:Type);
morph name : ty = 'l';
morph_name ()
srctype
--> dsttype
```
// do something
```

View file

@ -8,9 +8,11 @@
"scope": "source.morphismbase",
"file-types": ["morphism-base"],
"injection-regex": "^morphismbase$",
"class-name": "TreeSitterMorphismbase"
"class-name": "TreeSitterMorphismbase",
"highlights": "query/highlights.scm"
}
],
"metadata": {
"version": "0.1.0",
"license": "GPLv3",