add language injections

Closes #6
This commit is contained in:
Dmitriy Pleshevskiy 2022-12-09 18:21:55 +03:00
parent d230155d48
commit 99d9fbbf63
Signed by: pleshevskiy
GPG key ID: 1B59187B161C0215
14 changed files with 142 additions and 41 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 26 KiB

After

Width:  |  Height:  |  Size: 98 KiB

View file

@ -9,7 +9,7 @@
"sources": [ "sources": [
"bindings/node/binding.cc", "bindings/node/binding.cc",
"src/parser.c", "src/parser.c",
# If your language uses an external scanner, add it here. "src/scanner.c",
], ],
"cflags_c": [ "cflags_c": [
"-std=c99", "-std=c99",

View file

@ -13,11 +13,9 @@ fn main() {
// If your language uses an external scanner written in C, // If your language uses an external scanner written in C,
// then include this block of code: // then include this block of code:
/*
let scanner_path = src_dir.join("scanner.c"); let scanner_path = src_dir.join("scanner.c");
c_config.file(&scanner_path); c_config.file(&scanner_path);
println!("cargo:rerun-if-changed={}", scanner_path.to_str().unwrap()); println!("cargo:rerun-if-changed={}", scanner_path.to_str().unwrap());
*/
c_config.compile("parser"); c_config.compile("parser");
println!("cargo:rerun-if-changed={}", parser_path.to_str().unwrap()); println!("cargo:rerun-if-changed={}", parser_path.to_str().unwrap());

View file

@ -10,7 +10,7 @@ let
}; };
tree-sitter = (pkgs.tree-sitter.override { inherit extraGrammars; }); tree-sitter = (pkgs.tree-sitter.override { inherit extraGrammars; });
grammars = tree-sitter.withPlugins (g: [ g.tree-sitter-d2 g.tree-sitter-javascript ]); grammars = tree-sitter.withPlugins (g: tree-sitter.allGrammars);
nvim-treesitter = pkgs.vimPlugins.nvim-treesitter.overrideAttrs (oldAttrs: { nvim-treesitter = pkgs.vimPlugins.nvim-treesitter.overrideAttrs (oldAttrs: {
postPatch = '' postPatch = ''

View file

@ -1,7 +1,8 @@
timeline mixer: "" { timeline mixer: "" {
explanation: |md explanation: |md
## **Timeline mixer** ## **Timeline mixer**
- Inject ads, who-to-follow, onboarding
- **Inject ads**, who-to-follow, onboarding
- Conversation module - Conversation module
- Cursoring,pagination - Cursoring,pagination
- Tweat deduplication - Tweat deduplication

View file

@ -15,6 +15,8 @@ const PREC = {
module.exports = grammar({ module.exports = grammar({
name: "d2", name: "d2",
externals: ($) => [$._text_block_raw],
extras: ($) => [ extras: ($) => [
/[ \f\t\v\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]/, /[ \f\t\v\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]/,
$.line_comment, $.line_comment,
@ -132,10 +134,11 @@ module.exports = grammar({
), ),
_text_block_definition: ($) => _text_block_definition: ($) =>
seq(optional($.language), $._eol, optional($.text_block_content)), seq(
optional($.language),
text_block_content: ($) => $._eol,
repeat1(token(prec(PREC.TEXT_BLOCK_CONTENT, /.*?[^`|]/))), optional(alias($._text_block_raw, $.raw_text))
),
language: ($) => /\w+/, language: ($) => /\w+/,

View file

@ -8,6 +8,7 @@
; Literals ; Literals
;------------------------------------------------------------------------------- ;-------------------------------------------------------------------------------
(language) @string.special
(container_key (string) @string.special) (container_key (string) @string.special)
(shape_key (string) @string) (shape_key (string) @string)
(label) @string (label) @string

35
queries/injections.scm Normal file
View file

@ -0,0 +1,35 @@
; for tree-sitter
(
(text_block (raw_text) @injection.content)
(#set! injection.language "markdown")
)
(text_block
(language) @injection.language
(raw_text) @injection.content
)
;; overwrite for nvim-treesitter
; use markdown as default
(text_block . (raw_text) @markdown)
; add alias for markdown
(text_block
(language) @_language
(raw_text) @markdown
(#eq? @_language "md")
)
; add alias for javascript
(text_block
(language) @_language
(raw_text) @javascript
(#eq? @_language "js")
)
(text_block
(language) @language
(raw_text) @content
)

View file

@ -569,8 +569,13 @@
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{ {
"type": "ALIAS",
"content": {
"type": "SYMBOL", "type": "SYMBOL",
"name": "text_block_content" "name": "_text_block_raw"
},
"named": true,
"value": "raw_text"
}, },
{ {
"type": "BLANK" "type": "BLANK"
@ -579,20 +584,6 @@
} }
] ]
}, },
"text_block_content": {
"type": "REPEAT1",
"content": {
"type": "TOKEN",
"content": {
"type": "PREC",
"value": -1,
"content": {
"type": "PATTERN",
"value": ".*?[^`|]"
}
}
}
},
"language": { "language": {
"type": "PATTERN", "type": "PATTERN",
"value": "\\w+" "value": "\\w+"
@ -1339,7 +1330,12 @@
] ]
], ],
"precedences": [], "precedences": [],
"externals": [], "externals": [
{
"type": "SYMBOL",
"name": "_text_block_raw"
}
],
"inline": [], "inline": [],
"supertypes": [] "supertypes": []
} }

View file

@ -287,17 +287,12 @@
"named": true "named": true
}, },
{ {
"type": "text_block_content", "type": "raw_text",
"named": true "named": true
} }
] ]
} }
}, },
{
"type": "text_block_content",
"named": true,
"fields": {}
},
{ {
"type": "\u0000", "type": "\u0000",
"named": false "named": false
@ -394,6 +389,10 @@
"type": "opacity", "type": "opacity",
"named": false "named": false
}, },
{
"type": "raw_text",
"named": true
},
{ {
"type": "shadow", "type": "shadow",
"named": false "named": false

Binary file not shown.

68
src/scanner.c Normal file
View file

@ -0,0 +1,68 @@
#include <tree_sitter/parser.h>
#include <wctype.h>
enum TokenType {
RAW_TEXT
};
void *tree_sitter_d2_external_scanner_create() { return NULL; }
void tree_sitter_d2_external_scanner_destroy(void *payload) { }
unsigned tree_sitter_d2_external_scanner_serialize(void *p, char *buffer) { return 0; }
void tree_sitter_d2_external_scanner_deserialize(void *p, const char *b, unsigned n) {}
static void advance(TSLexer *lexer) { lexer->advance(lexer, false); }
static void skip(TSLexer *lexer) { lexer->advance(lexer, true); }
static bool scan_raw_text(TSLexer *lexer) {
lexer->result_symbol = RAW_TEXT;
if (lexer->lookahead == '`') {
advance(lexer);
}
if (lexer->lookahead == '|') {
return false;
}
for (bool has_content = false;; has_content = true) {
lexer->mark_end(lexer);
if (lexer->lookahead == '\0') {
return has_content;
}
if (lexer->lookahead == '\n') {
advance(lexer);
lexer->mark_end(lexer);
while (iswspace(lexer->lookahead)) {
advance(lexer);
lexer->mark_end(lexer);
}
if (lexer->lookahead == '`') {
advance(lexer);
}
if (lexer->lookahead == '|') {
return has_content;
}
}
advance(lexer);
}
return false;
}
bool tree_sitter_d2_external_scanner_scan(
void *payload,
TSLexer *lexer,
const bool *valid_symbols
) {
if (valid_symbols[RAW_TEXT]) {
return scan_raw_text(lexer);
}
return false;
}

View file

@ -122,7 +122,7 @@ foo: |
(shape (shape
(shape_key) (shape_key)
(text_block (text_block
(text_block_content) (raw_text)
) )
) )
) )
@ -149,7 +149,7 @@ foo: |go
(shape_key) (shape_key)
(text_block (text_block
(language) (language)
(text_block_content) (raw_text)
) )
) )
) )
@ -176,7 +176,7 @@ foo: |`go
(shape_key) (shape_key)
(text_block (text_block
(language) (language)
(text_block_content) (raw_text)
) )
) )
) )

Binary file not shown.