From b881e6ad62500c271f6c9e1b130a09fefaf00666 Mon Sep 17 00:00:00 2001 From: mclaughlin_connor Date: Tue, 20 Dec 2022 23:27:03 +0000 Subject: [PATCH 1/3] Chore: add keyword alias to all keywords to prepare for highlighting --- grammar.js | 43 ++++--- src/grammar.json | 229 ++++++++++++++++++++++++----------- src/node-types.json | 122 ++++++++----------- src/parser.c | 100 +++++++-------- test/corpus/block.txt | 42 ++++--- test/corpus/case.txt | 68 +++++++---- test/corpus/conditionals.txt | 46 ++++--- test/corpus/include.txt | 15 +-- test/corpus/iteration.txt | 50 +++++--- test/corpus/mixin.txt | 28 ++--- 10 files changed, 436 insertions(+), 307 deletions(-) diff --git a/grammar.js b/grammar.js index 06930ba..dadb15a 100644 --- a/grammar.js +++ b/grammar.js @@ -37,14 +37,14 @@ module.exports = grammar({ include: ($) => seq( - 'include', + alias('include', $.keyword), optional($.filter), alias(/[^\n]+/, $.filename), ), while: ($) => seq( - 'while', + alias('while', $.keyword), $.iteration_iterator, $._newline, $.children, @@ -69,7 +69,7 @@ module.exports = grammar({ _each_else: ($) => seq( - 'else', + alias('else', $.keyword), $._newline, $.children, ), @@ -77,9 +77,9 @@ module.exports = grammar({ each: ($) => prec.right( seq( - choice('each', 'for'), + alias(choice('each', 'for'), $.keyword), $.iteration_variable, - 'in', + alias('in', $.keyword), $.iteration_iterator, $._newline, $.children, @@ -116,7 +116,7 @@ module.exports = grammar({ ), mixin_definition: ($) => seq( - 'mixin', + alias('mixin', $.keyword), alias($.tag_name, $.mixin_name), optional($.mixin_attributes), $._newline, @@ -153,24 +153,24 @@ module.exports = grammar({ ), block_definition: ($) => seq( - 'block', + alias('block', $.keyword), $._block_content, ), block_append: ($) => seq( - optional('block'), - 'append', + alias(optional('block'), $.keyword), + alias('append', $.keyword), $._block_content, ), block_prepend: ($) => seq( - optional('block'), - 'prepend', + alias(optional('block'), $.keyword), + alias('prepend', $.keyword), $._block_content, ), extends: ($) => seq( - 'extends', + alias('extends', $.keyword), alias(/[^\n]+/, $.filename), ), @@ -210,14 +210,17 @@ module.exports = grammar({ seq( choice( seq( - choice( - 'unless', - 'if', - 'else if', + alias( + choice( + 'unless', + 'if', + 'else if', + ), + $.keyword, ), alias($._un_delimited_javascript, $.javascript), ), - 'else', + alias('else', $.keyword), ), $._newline, $.children, @@ -225,7 +228,7 @@ module.exports = grammar({ case: ($) => prec.right( seq( - 'case', + alias('case', $.keyword), alias($._un_delimited_javascript_line, $.javascript), $._newline, $._indent, @@ -253,11 +256,11 @@ module.exports = grammar({ _when_keyword: ($) => choice( seq( - 'when', + alias('when', $.keyword), // `when`s don't work with properly with objects, so removing : from regex is fine. alias(/[^:\n]+?/, $.javascript), ), - 'default', + alias('default', $.keyword), ), when: ($) => prec.left( diff --git a/src/grammar.json b/src/grammar.json index eafb675..a04cbe0 100644 --- a/src/grammar.json +++ b/src/grammar.json @@ -149,8 +149,13 @@ "type": "SEQ", "members": [ { - "type": "STRING", - "value": "include" + "type": "ALIAS", + "content": { + "type": "STRING", + "value": "include" + }, + "named": true, + "value": "keyword" }, { "type": "CHOICE", @@ -179,8 +184,13 @@ "type": "SEQ", "members": [ { - "type": "STRING", - "value": "while" + "type": "ALIAS", + "content": { + "type": "STRING", + "value": "while" + }, + "named": true, + "value": "keyword" }, { "type": "SYMBOL", @@ -248,8 +258,13 @@ "type": "SEQ", "members": [ { - "type": "STRING", - "value": "else" + "type": "ALIAS", + "content": { + "type": "STRING", + "value": "else" + }, + "named": true, + "value": "keyword" }, { "type": "SYMBOL", @@ -268,25 +283,35 @@ "type": "SEQ", "members": [ { - "type": "CHOICE", - "members": [ - { - "type": "STRING", - "value": "each" - }, - { - "type": "STRING", - "value": "for" - } - ] + "type": "ALIAS", + "content": { + "type": "CHOICE", + "members": [ + { + "type": "STRING", + "value": "each" + }, + { + "type": "STRING", + "value": "for" + } + ] + }, + "named": true, + "value": "keyword" }, { "type": "SYMBOL", "name": "iteration_variable" }, { - "type": "STRING", - "value": "in" + "type": "ALIAS", + "content": { + "type": "STRING", + "value": "in" + }, + "named": true, + "value": "keyword" }, { "type": "SYMBOL", @@ -406,8 +431,13 @@ "type": "SEQ", "members": [ { - "type": "STRING", - "value": "mixin" + "type": "ALIAS", + "content": { + "type": "STRING", + "value": "mixin" + }, + "named": true, + "value": "keyword" }, { "type": "ALIAS", @@ -539,8 +569,13 @@ "type": "SEQ", "members": [ { - "type": "STRING", - "value": "block" + "type": "ALIAS", + "content": { + "type": "STRING", + "value": "block" + }, + "named": true, + "value": "keyword" }, { "type": "SYMBOL", @@ -552,20 +587,30 @@ "type": "SEQ", "members": [ { - "type": "CHOICE", - "members": [ - { - "type": "STRING", - "value": "block" - }, - { - "type": "BLANK" - } - ] + "type": "ALIAS", + "content": { + "type": "CHOICE", + "members": [ + { + "type": "STRING", + "value": "block" + }, + { + "type": "BLANK" + } + ] + }, + "named": true, + "value": "keyword" }, { - "type": "STRING", - "value": "append" + "type": "ALIAS", + "content": { + "type": "STRING", + "value": "append" + }, + "named": true, + "value": "keyword" }, { "type": "SYMBOL", @@ -577,20 +622,30 @@ "type": "SEQ", "members": [ { - "type": "CHOICE", - "members": [ - { - "type": "STRING", - "value": "block" - }, - { - "type": "BLANK" - } - ] + "type": "ALIAS", + "content": { + "type": "CHOICE", + "members": [ + { + "type": "STRING", + "value": "block" + }, + { + "type": "BLANK" + } + ] + }, + "named": true, + "value": "keyword" }, { - "type": "STRING", - "value": "prepend" + "type": "ALIAS", + "content": { + "type": "STRING", + "value": "prepend" + }, + "named": true, + "value": "keyword" }, { "type": "SYMBOL", @@ -602,8 +657,13 @@ "type": "SEQ", "members": [ { - "type": "STRING", - "value": "extends" + "type": "ALIAS", + "content": { + "type": "STRING", + "value": "extends" + }, + "named": true, + "value": "keyword" }, { "type": "ALIAS", @@ -731,21 +791,26 @@ "type": "SEQ", "members": [ { - "type": "CHOICE", - "members": [ - { - "type": "STRING", - "value": "unless" - }, - { - "type": "STRING", - "value": "if" - }, - { - "type": "STRING", - "value": "else if" - } - ] + "type": "ALIAS", + "content": { + "type": "CHOICE", + "members": [ + { + "type": "STRING", + "value": "unless" + }, + { + "type": "STRING", + "value": "if" + }, + { + "type": "STRING", + "value": "else if" + } + ] + }, + "named": true, + "value": "keyword" }, { "type": "ALIAS", @@ -759,8 +824,13 @@ ] }, { - "type": "STRING", - "value": "else" + "type": "ALIAS", + "content": { + "type": "STRING", + "value": "else" + }, + "named": true, + "value": "keyword" } ] }, @@ -781,8 +851,13 @@ "type": "SEQ", "members": [ { - "type": "STRING", - "value": "case" + "type": "ALIAS", + "content": { + "type": "STRING", + "value": "case" + }, + "named": true, + "value": "keyword" }, { "type": "ALIAS", @@ -863,8 +938,13 @@ "type": "SEQ", "members": [ { - "type": "STRING", - "value": "when" + "type": "ALIAS", + "content": { + "type": "STRING", + "value": "when" + }, + "named": true, + "value": "keyword" }, { "type": "ALIAS", @@ -878,8 +958,13 @@ ] }, { - "type": "STRING", - "value": "default" + "type": "ALIAS", + "content": { + "type": "STRING", + "value": "default" + }, + "named": true, + "value": "keyword" } ] }, diff --git a/src/node-types.json b/src/node-types.json index a60b3d5..2abf722 100644 --- a/src/node-types.json +++ b/src/node-types.json @@ -65,6 +65,10 @@ { "type": "children", "named": true + }, + { + "type": "keyword", + "named": true } ] } @@ -84,6 +88,10 @@ { "type": "children", "named": true + }, + { + "type": "keyword", + "named": true } ] } @@ -103,6 +111,10 @@ { "type": "children", "named": true + }, + { + "type": "keyword", + "named": true } ] } @@ -138,6 +150,10 @@ "type": "javascript", "named": true }, + { + "type": "keyword", + "named": true + }, { "type": "when", "named": true @@ -256,6 +272,10 @@ { "type": "javascript", "named": true + }, + { + "type": "keyword", + "named": true } ] } @@ -313,6 +333,10 @@ { "type": "iteration_variable", "named": true + }, + { + "type": "keyword", + "named": true } ] } @@ -322,12 +346,16 @@ "named": true, "fields": {}, "children": { - "multiple": false, + "multiple": true, "required": true, "types": [ { "type": "children", "named": true + }, + { + "type": "keyword", + "named": true } ] } @@ -337,12 +365,16 @@ "named": true, "fields": {}, "children": { - "multiple": false, + "multiple": true, "required": true, "types": [ { "type": "filename", "named": true + }, + { + "type": "keyword", + "named": true } ] } @@ -385,6 +417,10 @@ { "type": "filter", "named": true + }, + { + "type": "keyword", + "named": true } ] } @@ -451,6 +487,10 @@ "type": "children", "named": true }, + { + "type": "keyword", + "named": true + }, { "type": "mixin_attributes", "named": true @@ -735,7 +775,7 @@ "fields": {}, "children": { "multiple": true, - "required": false, + "required": true, "types": [ { "type": "children", @@ -744,6 +784,10 @@ { "type": "javascript", "named": true + }, + { + "type": "keyword", + "named": true } ] } @@ -763,6 +807,10 @@ { "type": "iteration_iterator", "named": true + }, + { + "type": "keyword", + "named": true } ] } @@ -831,10 +879,6 @@ "type": "=", "named": false }, - { - "type": "append", - "named": false - }, { "type": "attribute_modifier", "named": true @@ -843,26 +887,14 @@ "type": "attribute_value", "named": true }, - { - "type": "block", - "named": false - }, { "type": "block_name", "named": true }, - { - "type": "case", - "named": false - }, { "type": "class", "named": true }, - { - "type": "default", - "named": false - }, { "type": "doctype", "named": false @@ -871,22 +903,6 @@ "type": "doctype_name", "named": true }, - { - "type": "each", - "named": false - }, - { - "type": "else", - "named": false - }, - { - "type": "else if", - "named": false - }, - { - "type": "extends", - "named": false - }, { "type": "filename", "named": true @@ -895,38 +911,18 @@ "type": "filter_name", "named": true }, - { - "type": "for", - "named": false - }, { "type": "id", "named": true }, { - "type": "if", - "named": false - }, - { - "type": "in", - "named": false - }, - { - "type": "include", - "named": false - }, - { - "type": "mixin", - "named": false + "type": "keyword", + "named": true }, { "type": "mixin_name", "named": true }, - { - "type": "prepend", - "named": false - }, { "type": "script.", "named": false @@ -939,18 +935,6 @@ "type": "tag_name", "named": true }, - { - "type": "unless", - "named": false - }, - { - "type": "when", - "named": false - }, - { - "type": "while", - "named": false - }, { "type": "{", "named": false diff --git a/src/parser.c b/src/parser.c index 6ba539d..72b47c7 100644 --- a/src/parser.c +++ b/src/parser.c @@ -183,35 +183,35 @@ static const char * const ts_symbol_names[] = { [anon_sym_strict] = "doctype_name", [anon_sym_xml] = "doctype_name", [anon_sym_PIPE] = "|", - [anon_sym_include] = "include", + [anon_sym_include] = "keyword", [aux_sym_include_token1] = "include_token1", - [anon_sym_while] = "while", + [anon_sym_while] = "keyword", [aux_sym__each_js_token1] = "javascript", [anon_sym_COMMA] = ",", - [anon_sym_else] = "else", - [anon_sym_each] = "each", - [anon_sym_for] = "for", - [anon_sym_in] = "in", + [anon_sym_else] = "keyword", + [anon_sym_each] = "keyword", + [anon_sym_for] = "keyword", + [anon_sym_in] = "keyword", [anon_sym_PLUS] = "+", [anon_sym_LPAREN] = "(", [anon_sym_RPAREN] = ")", - [anon_sym_mixin] = "mixin", + [anon_sym_mixin] = "keyword", [aux_sym_mixin_attributes_token1] = "attribute_name", - [anon_sym_block] = "block", - [anon_sym_append] = "append", - [anon_sym_prepend] = "prepend", - [anon_sym_extends] = "extends", + [anon_sym_block] = "keyword", + [anon_sym_append] = "keyword", + [anon_sym_prepend] = "keyword", + [anon_sym_extends] = "keyword", [anon_sym_COLON] = ":", [sym_filter_name] = "filter_name", [aux_sym_filter_content_token1] = "filter_content_token1", [aux_sym_filter_content_token2] = "filter_content_token2", - [anon_sym_unless] = "unless", - [anon_sym_if] = "if", - [anon_sym_elseif] = "else if", - [anon_sym_case] = "case", - [anon_sym_when] = "when", + [anon_sym_unless] = "keyword", + [anon_sym_if] = "keyword", + [anon_sym_elseif] = "keyword", + [anon_sym_case] = "keyword", + [anon_sym_when] = "keyword", [aux_sym__when_keyword_token1] = "javascript", - [anon_sym_default] = "default", + [anon_sym_default] = "keyword", [anon_sym_BANG_EQ] = "!=", [anon_sym_EQ] = "=", [anon_sym_script_DOT] = "script.", @@ -338,33 +338,33 @@ static const TSSymbol ts_symbol_map[] = { [anon_sym_PIPE] = anon_sym_PIPE, [anon_sym_include] = anon_sym_include, [aux_sym_include_token1] = aux_sym_include_token1, - [anon_sym_while] = anon_sym_while, + [anon_sym_while] = anon_sym_include, [aux_sym__each_js_token1] = aux_sym__each_js_token1, [anon_sym_COMMA] = anon_sym_COMMA, - [anon_sym_else] = anon_sym_else, - [anon_sym_each] = anon_sym_each, - [anon_sym_for] = anon_sym_for, - [anon_sym_in] = anon_sym_in, + [anon_sym_else] = anon_sym_include, + [anon_sym_each] = anon_sym_include, + [anon_sym_for] = anon_sym_include, + [anon_sym_in] = anon_sym_include, [anon_sym_PLUS] = anon_sym_PLUS, [anon_sym_LPAREN] = anon_sym_LPAREN, [anon_sym_RPAREN] = anon_sym_RPAREN, - [anon_sym_mixin] = anon_sym_mixin, + [anon_sym_mixin] = anon_sym_include, [aux_sym_mixin_attributes_token1] = sym_attribute_name, - [anon_sym_block] = anon_sym_block, - [anon_sym_append] = anon_sym_append, - [anon_sym_prepend] = anon_sym_prepend, - [anon_sym_extends] = anon_sym_extends, + [anon_sym_block] = anon_sym_include, + [anon_sym_append] = anon_sym_include, + [anon_sym_prepend] = anon_sym_include, + [anon_sym_extends] = anon_sym_include, [anon_sym_COLON] = anon_sym_COLON, [sym_filter_name] = sym_filter_name, [aux_sym_filter_content_token1] = aux_sym_filter_content_token1, [aux_sym_filter_content_token2] = aux_sym_filter_content_token2, - [anon_sym_unless] = anon_sym_unless, - [anon_sym_if] = anon_sym_if, - [anon_sym_elseif] = anon_sym_elseif, - [anon_sym_case] = anon_sym_case, - [anon_sym_when] = anon_sym_when, + [anon_sym_unless] = anon_sym_include, + [anon_sym_if] = anon_sym_include, + [anon_sym_elseif] = anon_sym_include, + [anon_sym_case] = anon_sym_include, + [anon_sym_when] = anon_sym_include, [aux_sym__when_keyword_token1] = aux_sym__each_js_token1, - [anon_sym_default] = anon_sym_default, + [anon_sym_default] = anon_sym_include, [anon_sym_BANG_EQ] = anon_sym_BANG_EQ, [anon_sym_EQ] = anon_sym_EQ, [anon_sym_script_DOT] = anon_sym_script_DOT, @@ -509,7 +509,7 @@ static const TSSymbolMetadata ts_symbol_metadata[] = { }, [anon_sym_include] = { .visible = true, - .named = false, + .named = true, }, [aux_sym_include_token1] = { .visible = false, @@ -517,7 +517,7 @@ static const TSSymbolMetadata ts_symbol_metadata[] = { }, [anon_sym_while] = { .visible = true, - .named = false, + .named = true, }, [aux_sym__each_js_token1] = { .visible = true, @@ -529,19 +529,19 @@ static const TSSymbolMetadata ts_symbol_metadata[] = { }, [anon_sym_else] = { .visible = true, - .named = false, + .named = true, }, [anon_sym_each] = { .visible = true, - .named = false, + .named = true, }, [anon_sym_for] = { .visible = true, - .named = false, + .named = true, }, [anon_sym_in] = { .visible = true, - .named = false, + .named = true, }, [anon_sym_PLUS] = { .visible = true, @@ -557,7 +557,7 @@ static const TSSymbolMetadata ts_symbol_metadata[] = { }, [anon_sym_mixin] = { .visible = true, - .named = false, + .named = true, }, [aux_sym_mixin_attributes_token1] = { .visible = true, @@ -565,19 +565,19 @@ static const TSSymbolMetadata ts_symbol_metadata[] = { }, [anon_sym_block] = { .visible = true, - .named = false, + .named = true, }, [anon_sym_append] = { .visible = true, - .named = false, + .named = true, }, [anon_sym_prepend] = { .visible = true, - .named = false, + .named = true, }, [anon_sym_extends] = { .visible = true, - .named = false, + .named = true, }, [anon_sym_COLON] = { .visible = true, @@ -597,23 +597,23 @@ static const TSSymbolMetadata ts_symbol_metadata[] = { }, [anon_sym_unless] = { .visible = true, - .named = false, + .named = true, }, [anon_sym_if] = { .visible = true, - .named = false, + .named = true, }, [anon_sym_elseif] = { .visible = true, - .named = false, + .named = true, }, [anon_sym_case] = { .visible = true, - .named = false, + .named = true, }, [anon_sym_when] = { .visible = true, - .named = false, + .named = true, }, [aux_sym__when_keyword_token1] = { .visible = true, @@ -621,7 +621,7 @@ static const TSSymbolMetadata ts_symbol_metadata[] = { }, [anon_sym_default] = { .visible = true, - .named = false, + .named = true, }, [anon_sym_BANG_EQ] = { .visible = true, diff --git a/test/corpus/block.txt b/test/corpus/block.txt index 8f497e8..7a7546a 100644 --- a/test/corpus/block.txt +++ b/test/corpus/block.txt @@ -1,6 +1,6 @@ -======================== +================================================================================ Extends basic definition -========================= +================================================================================ extends file.pug @@ -10,12 +10,14 @@ block name tag ---- +-------------------------------------------------------------------------------- (source_file (extends + (keyword) (filename)) (block_definition + (keyword) (block_name) (children (tag @@ -26,9 +28,9 @@ tag (tag (tag_name))) -======================== +================================================================================ Extends block nesting -========================= +================================================================================ extends file.pug @@ -39,12 +41,14 @@ block name tag tag ---- +-------------------------------------------------------------------------------- (source_file (extends + (keyword) (filename)) (block_definition + (keyword) (block_name) (children (tag @@ -53,16 +57,16 @@ tag (tag (tag_name)))) (block_definition + (keyword) (block_name)) (tag (tag_name)))) (tag (tag_name))) - -======================== +================================================================================ Extends block append -========================= +================================================================================ extends file.pug @@ -77,17 +81,21 @@ block append name tag ---- +-------------------------------------------------------------------------------- (source_file (extends + (keyword) (filename)) (block_definition + (keyword) (block_name) (children (tag (tag_name)))) (block_append + (keyword) + (keyword) (block_name) (children (tag @@ -96,6 +104,7 @@ tag (tag (tag_name)))) (block_append + (keyword) (block_name)) (tag (tag_name) @@ -105,10 +114,9 @@ tag (tag (tag_name))) - -======================== +================================================================================ Extends block prepend -========================= +================================================================================ extends file.pug @@ -123,17 +131,21 @@ block prepend name tag ---- +-------------------------------------------------------------------------------- (source_file (extends + (keyword) (filename)) (block_definition + (keyword) (block_name) (children (tag (tag_name)))) (block_prepend + (keyword) + (keyword) (block_name) (children (tag @@ -142,6 +154,7 @@ tag (tag (tag_name)))) (block_prepend + (keyword) (block_name)) (tag (tag_name) @@ -150,4 +163,3 @@ tag (tag_name)))))) (tag (tag_name))) - diff --git a/test/corpus/case.txt b/test/corpus/case.txt index 942c139..7f50682 100644 --- a/test/corpus/case.txt +++ b/test/corpus/case.txt @@ -1,61 +1,68 @@ -============ +================================================================================ Basic case statement -============ +================================================================================ case variable when 4 when 2 p tag content ---- +-------------------------------------------------------------------------------- (source_file (case + (keyword) (javascript) (when + (keyword) (javascript)) (when + (keyword) (javascript) (children (tag (tag_name) (content)))))) -============ +================================================================================ Basic case statement with block expansion -============ +================================================================================ case variable when 4: tag content when 2: p tag content default: other content ---- +-------------------------------------------------------------------------------- (source_file (case + (keyword) (javascript) (when + (keyword) (javascript) (children (tag (tag_name) (content)))) (when + (keyword) (javascript) (children (tag (tag_name) (content)))) (when + (keyword) (children (tag (tag_name) (content)))))) -============ +================================================================================ Basic case statement with default -============ +================================================================================ case variable when 4 @@ -64,28 +71,32 @@ case variable default tag other content ---- +-------------------------------------------------------------------------------- (source_file (case + (keyword) (javascript) (when + (keyword) (javascript)) (when + (keyword) (javascript) (children (tag (tag_name) (content)))) (when + (keyword) (children (tag (tag_name) (content)))))) -============ +================================================================================ Basic case statement with default and break -============ +================================================================================ case variable when 4 @@ -95,54 +106,61 @@ case variable default tag other content ---- +-------------------------------------------------------------------------------- (source_file (case + (keyword) (javascript) (when + (keyword) (javascript) (children (unbuffered_code (javascript)))) (when + (keyword) (javascript) (children (tag (tag_name) (content)))) (when + (keyword) (children (tag (tag_name) (content)))))) -============ +================================================================================ Case statement with javascript -============ +================================================================================ case variable + another when 1 + 4 when 'string' p tag content ---- +-------------------------------------------------------------------------------- (source_file (case + (keyword) (javascript) (when + (keyword) (javascript)) (when + (keyword) (javascript) (children (tag (tag_name) (content)))))) -============ +================================================================================ Case statement followed by tag -============ +================================================================================ case variable when 4 @@ -151,14 +169,17 @@ case variable tag ---- +-------------------------------------------------------------------------------- (source_file (case + (keyword) (javascript) (when + (keyword) (javascript)) (when + (keyword) (javascript) (children (tag @@ -167,9 +188,9 @@ tag (tag (tag_name))) -============ +================================================================================ Complex case statement followed by tag -============ +================================================================================ case variable when 4 @@ -180,24 +201,27 @@ case variable tag ---- +-------------------------------------------------------------------------------- (source_file (case + (keyword) (javascript) (when + (keyword) (javascript)) (when + (keyword) (javascript) (children (tag (tag_name) (content)))) (when + (keyword) (children (tag (tag_name) (content))))) (tag (tag_name))) - diff --git a/test/corpus/conditionals.txt b/test/corpus/conditionals.txt index 96a3a36..0454235 100644 --- a/test/corpus/conditionals.txt +++ b/test/corpus/conditionals.txt @@ -1,6 +1,6 @@ -============ +================================================================================ Conditional -============ +================================================================================ if user.description tag @@ -9,27 +9,30 @@ else if authorised else tag ---- +-------------------------------------------------------------------------------- (source_file (conditional + (keyword) (javascript) (children (tag (tag_name)))) (conditional + (keyword) (javascript) (children (tag (tag_name)))) (conditional + (keyword) (children (tag (tag_name))))) -============ +================================================================================ Conditional in tag -============ +================================================================================ span if user.description @@ -39,30 +42,33 @@ span else tag ---- +-------------------------------------------------------------------------------- (source_file (tag (tag_name) (children (conditional + (keyword) (javascript) (children (tag (tag_name)))) (conditional + (keyword) (javascript) (children (tag (tag_name)))) (conditional + (keyword) (children (tag (tag_name))))))) -============ +================================================================================ Conditional in tag real example -============ +================================================================================ #user if user.description @@ -73,13 +79,14 @@ Conditional in tag real example else span content ---- +-------------------------------------------------------------------------------- (source_file (tag (id) (children (conditional + (keyword) (javascript) (children (tag @@ -91,21 +98,22 @@ Conditional in tag real example (buffered_code (javascript))))) (conditional + (keyword) (javascript) (children (tag (tag_name) (content)))) (conditional + (keyword) (children (tag (tag_name) (content))))))) - -============ +================================================================================ Conditional in tag real example with multiple indents and blank lines -============ +================================================================================ #user if user.description @@ -120,13 +128,14 @@ unless user.isAnonymous p ---- +-------------------------------------------------------------------------------- (source_file (tag (id) (children (conditional + (keyword) (javascript) (children (tag @@ -136,6 +145,7 @@ p (tag_name) (content)))))) (conditional + (keyword) (javascript) (children (tag @@ -147,10 +157,9 @@ p (tag (tag_name))) - -============ +================================================================================ Conditional else following multi-level a element -============ +================================================================================ tag if user.description @@ -162,13 +171,14 @@ tag p ---- +-------------------------------------------------------------------------------- (source_file (tag (tag_name) (children (conditional + (keyword) (javascript) (children (tag @@ -179,6 +189,7 @@ p (tag_name) (content)))))) (conditional + (keyword) (javascript) (children (tag @@ -186,4 +197,3 @@ p (content)))))) (tag (tag_name))) - diff --git a/test/corpus/include.txt b/test/corpus/include.txt index a15b3b8..9a8c727 100644 --- a/test/corpus/include.txt +++ b/test/corpus/include.txt @@ -1,32 +1,33 @@ -======================== +================================================================================ Basic include -======================== +================================================================================ include folder/file.pug tag ---- +-------------------------------------------------------------------------------- (source_file (include + (keyword) (filename)) (tag (tag_name))) -======================== +================================================================================ Include with filter -======================== +================================================================================ include:filter:filter folder/file.pug tag ---- +-------------------------------------------------------------------------------- (source_file (include + (keyword) (filename)) (tag (tag_name))) - diff --git a/test/corpus/iteration.txt b/test/corpus/iteration.txt index 8a74639..f89d6f0 100644 --- a/test/corpus/iteration.txt +++ b/test/corpus/iteration.txt @@ -1,18 +1,20 @@ -======================== +================================================================================ Basic each -========================= +================================================================================ each val in [1, 2, 3, 4, 5] li= val tag ---- +-------------------------------------------------------------------------------- (source_file (each + (keyword) (iteration_variable (javascript)) + (keyword) (iteration_iterator (javascript)) (children @@ -23,22 +25,24 @@ tag (tag (tag_name))) -======================== +================================================================================ Each with key and value -========================= +================================================================================ for key, val in [1, 2, 3, 4, 5] li= val tag ---- +-------------------------------------------------------------------------------- (source_file (each + (keyword) (iteration_variable (javascript) (javascript)) + (keyword) (iteration_iterator (javascript)) (children @@ -49,22 +53,24 @@ tag (tag (tag_name))) -======================== +================================================================================ Each with object -========================= +================================================================================ each val, key in {1: 'one', 2: 'two', 3: 'three'} tag tag ---- +-------------------------------------------------------------------------------- (source_file (each + (keyword) (iteration_variable (javascript) (javascript)) + (keyword) (iteration_iterator (javascript)) (children @@ -73,9 +79,9 @@ tag (tag (tag_name))) -======================== +================================================================================ Each with complex iterator -======================== +================================================================================ - var values = []; ul @@ -84,7 +90,7 @@ ul tag ---- +-------------------------------------------------------------------------------- (source_file (unbuffered_code @@ -93,8 +99,10 @@ tag (tag_name) (children (each + (keyword) (iteration_variable (javascript)) + (keyword) (iteration_iterator (javascript)) (children @@ -105,10 +113,9 @@ tag (tag (tag_name))) - -======================== +================================================================================ Complex each with else -======================== +================================================================================ - var values = []; ul @@ -117,7 +124,7 @@ ul else li There are no values ---- +-------------------------------------------------------------------------------- (source_file (unbuffered_code @@ -126,8 +133,10 @@ ul (tag_name) (children (each + (keyword) (iteration_variable (javascript)) + (keyword) (iteration_iterator (javascript)) (children @@ -136,21 +145,22 @@ ul (buffered_code (javascript)))) (else + (keyword) (children (tag (tag_name) (content)))))))) -======================== +================================================================================ Complex while -======================== +================================================================================ - var n = 0; ul while n < 4 li= n++ ---- +-------------------------------------------------------------------------------- (source_file (unbuffered_code @@ -159,6 +169,7 @@ ul (tag_name) (children (while + (keyword) (iteration_iterator (javascript)) (children @@ -166,4 +177,3 @@ ul (tag_name) (buffered_code (javascript)))))))) - diff --git a/test/corpus/mixin.txt b/test/corpus/mixin.txt index 261190c..296ac23 100644 --- a/test/corpus/mixin.txt +++ b/test/corpus/mixin.txt @@ -1,6 +1,6 @@ -============ +================================================================================ Basic mixin declaration -============ +================================================================================ mixin name tag @@ -9,10 +9,11 @@ mixin name tag ---- +-------------------------------------------------------------------------------- (source_file (mixin_definition + (keyword) (mixin_name) (children (tag @@ -25,9 +26,9 @@ tag (tag (tag_name))) -============ +================================================================================ Mixin declaration with attributes -============ +================================================================================ mixin name(attr, attr, attr) tag @@ -36,10 +37,11 @@ mixin name(attr, attr, attr) tag ---- +-------------------------------------------------------------------------------- (source_file (mixin_definition + (keyword) (mixin_name) (mixin_attributes (attribute_name) @@ -56,16 +58,16 @@ tag (tag (tag_name))) -============ +================================================================================ Mixin use with attributes -============ +================================================================================ +mixin('attr') tag ---- +-------------------------------------------------------------------------------- (source_file (mixin_use @@ -76,17 +78,16 @@ tag (tag (tag_name))) - -============ +================================================================================ Mixin use with complex attributes -============ +================================================================================ +mixin('attr', 2, ['fdsa']) tag ---- +-------------------------------------------------------------------------------- (source_file (mixin_use @@ -100,4 +101,3 @@ tag (javascript))) (tag (tag_name))) - From d3914c5aac6122bae1b3d7c6d72eb1bcdd273a9f Mon Sep 17 00:00:00 2001 From: mclaughlin_connor Date: Wed, 21 Dec 2022 16:42:14 +0000 Subject: [PATCH 2/3] Chore: bump tree-sitter-cli to 0.20.7 --- package.json | 2 +- yarn.lock | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index db4200b..8b7c9d5 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,6 @@ }, "devDependencies": { "prettier": "^2.3.2", - "tree-sitter-cli": "^0.20.0" + "tree-sitter-cli": "^0.20.7" } } diff --git a/yarn.lock b/yarn.lock index 33561d4..d1091b4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -12,7 +12,7 @@ prettier@^2.3.2: resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.3.2.tgz#ef280a05ec253712e486233db5c6f23441e7342d" integrity sha512-lnJzDfJ66zkMy58OL5/NY5zp70S7Nz6KqcKkXYzn2tMVrNxvbqaBpg7H3qHaLxCJ5lNMsGuM8+ohS7cZrthdLQ== -tree-sitter-cli@^0.20.0: - version "0.20.0" - resolved "https://registry.yarnpkg.com/tree-sitter-cli/-/tree-sitter-cli-0.20.0.tgz#feaaa11c7ecf44a6e236aa1e2963b85d045d33cc" - integrity sha512-4D1qapWbJXZ5rrSUGM5rcw5Vuq/smzn9KbiFRhlON6KeuuXjra+KAtDYVrDgAoLIG4ku+jbEEGrJxCptUGi3dg== +tree-sitter-cli@^0.20.7: + version "0.20.7" + resolved "https://registry.yarnpkg.com/tree-sitter-cli/-/tree-sitter-cli-0.20.7.tgz#e3e1b5af22677e70337f5c8b201f9447fd4a144f" + integrity sha512-MHABT8oCPr4D0fatsPo6ATQ9H4h9vHpPRjlxkxJs80tpfAEKGn6A1zU3eqfCKBcgmfZDe9CiL3rKOGMzYHwA3w== From b8c5b521a190ba2da651d6515bac034c2acb9603 Mon Sep 17 00:00:00 2001 From: mclaughlin_connor Date: Wed, 21 Dec 2022 16:51:12 +0000 Subject: [PATCH 3/3] Feat: add syntax highlighting queries --- bindings/rust/lib.rs | 2 +- package.json | 10 ++++++++- queries/highlights.scm | 50 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 queries/highlights.scm diff --git a/bindings/rust/lib.rs b/bindings/rust/lib.rs index d045f06..9be1af7 100644 --- a/bindings/rust/lib.rs +++ b/bindings/rust/lib.rs @@ -35,7 +35,7 @@ pub const NODE_TYPES: &'static str = include_str!("../../src/node-types.json"); // Uncomment these to include any queries that this grammar contains -// pub const HIGHLIGHTS_QUERY: &'static str = include_str!("../../queries/highlights.scm"); +pub const HIGHLIGHTS_QUERY: &'static str = include_str!("../../queries/highlights.scm"); // pub const INJECTIONS_QUERY: &'static str = include_str!("../../queries/injections.scm"); // pub const LOCALS_QUERY: &'static str = include_str!("../../queries/locals.scm"); // pub const TAGS_QUERY: &'static str = include_str!("../../queries/tags.scm"); diff --git a/package.json b/package.json index 8b7c9d5..738a352 100644 --- a/package.json +++ b/package.json @@ -16,5 +16,13 @@ "devDependencies": { "prettier": "^2.3.2", "tree-sitter-cli": "^0.20.7" - } + }, + "tree-sitter": [ + { + "scope": "source.pug", + "file-types": [ + "pug" + ] + } + ] } diff --git a/queries/highlights.scm b/queries/highlights.scm new file mode 100644 index 0000000..65220ba --- /dev/null +++ b/queries/highlights.scm @@ -0,0 +1,50 @@ +(comment) @comment + +(tag_name) @constant +( + (tag_name) @constant.builtin + ; https://www.script-example.com/html-tag-liste + (#any-of? @constant.builtin + "head" "title" "base" "link" "meta" "style" + "body" "article" "section" "nav" "aside" "h1" "h2" "h3" "h4" "h5" "h6" "hgroup" "header" "footer" "address" + "p" "hr" "pre" "blockquote" "ol" "ul" "menu" "li" "dl" "dt" "dd" "figure" "figcaption" "main" "div" + "a" "em" "strong" "small" "s" "cite" "q" "dfn" "abbr" "ruby" "rt" "rp" "data" "time" "code" "var" "samp" "kbd" "sub" "sup" "i" "b" "u" "mark" "bdi" "bdo" "span" "br" "wbr" + "ins" "del" + "picture" "source" "img" "iframe" "embed" "object" "param" "video" "audio" "track" "map" "area" + "table" "caption" "colgroup" "col" "tbody" "thead" "tfoot" "tr" "td" "th" + "form" "label" "input" "button" "select" "datalist" "optgroup" "option" "textarea" "output" "progress" "meter" "fieldset" "legend" + "details" "summary" "dialog" + "script" "noscript" "template" "slot" "canvas") +) + +(content) @none + +(id) @attribute +(class) @attribute + +(quoted_attribute_value) @string +(attribute_name) @symbol +( + (attribute_name) @keyword + (#match? @keyword "^(\\(.*\\)|\\[.*\\]|\\*.*)$") +) @keyword + +[ + ":" + "{{" + "}}" + "+" + "|" +] @punctuation.delimiter + +(keyword) @keyword +((keyword) @include (#eq? @include "include")) +((keyword) @repeat (#any-of? @repeat "for" "each" "of" "in" "while")) +((keyword) @conditional (#any-of? @conditional "if" "else" "else if" "unless")) +((keyword) @keyword.function (#any-of? @keyword.function "block" "mixin")) + +(filter_name) @method.call + +(mixin_use (mixin_name) @method.call) +(mixin_definition (mixin_name) @function) +