Skip to content

Commit c683964

Browse files
committed
parse optional (frontmatter_info_string)
1 parent 4a5f2e5 commit c683964

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

grammar.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ module.exports = grammar({
7777
$._block_comment_content,
7878
$._line_doc_content,
7979
$._frontmatter_start,
80+
$.frontmatter_info_string,
8081
$.frontmatter_content,
8182
$._frontmatter_end,
8283
$._error_sentinel,
@@ -1654,6 +1655,7 @@ module.exports = grammar({
16541655

16551656
frontmatter: $ => seq(
16561657
$._frontmatter_start,
1658+
optional($.frontmatter_info_string),
16571659
$.frontmatter_content,
16581660
$._frontmatter_end,
16591661
),

src/scanner.c

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ enum TokenType {
1414
BLOCK_COMMENT_CONTENT,
1515
LINE_DOC_CONTENT,
1616
FRONTMATTER_START,
17+
FRONTMATTER_INFO_STRING,
1718
FRONTMATTER_CONTENT,
1819
FRONTMATTER_END,
1920
ERROR_SENTINEL
@@ -22,6 +23,7 @@ enum TokenType {
2223
typedef struct {
2324
uint8_t opening_hash_count;
2425
uint8_t frontmatter_dashes;
26+
bool frontmatter_has_info_string;
2527
} Scanner;
2628

2729
void *tree_sitter_rust_external_scanner_create() { return ts_calloc(1, sizeof(Scanner)); }
@@ -32,16 +34,19 @@ unsigned tree_sitter_rust_external_scanner_serialize(void *payload, char *buffer
3234
Scanner *scanner = (Scanner *)payload;
3335
buffer[0] = (char)scanner->opening_hash_count;
3436
buffer[1] = (char)scanner->frontmatter_dashes;
35-
return 2;
37+
buffer[2] = (char)scanner->frontmatter_has_info_string;
38+
return 3;
3639
}
3740

3841
void tree_sitter_rust_external_scanner_deserialize(void *payload, const char *buffer, unsigned length) {
3942
Scanner *scanner = (Scanner *)payload;
4043
scanner->opening_hash_count = 0;
4144
scanner->frontmatter_dashes = 0;
42-
if (length == 2) {
45+
scanner->frontmatter_has_info_string = false;
46+
if (length == 3) {
4347
scanner->opening_hash_count = buffer[0];
4448
scanner->frontmatter_dashes = buffer[1];
49+
scanner->frontmatter_has_info_string = buffer[2];
4550
}
4651
}
4752

@@ -350,16 +355,38 @@ static inline bool process_frontmatter_start(TSLexer *lexer, Scanner *scanner) {
350355
scanner->frontmatter_dashes = amount;
351356
lexer->result_symbol = FRONTMATTER_START;
352357

358+
while (!lexer->eof(lexer) && iswspace(lexer->lookahead) && lexer->lookahead != '\n') {
359+
advance(lexer);
360+
}
361+
353362
// parse optional info string after the initial fence
354-
while (lexer->lookahead != '\n' && !lexer->eof(lexer)) {
363+
if (lexer->eof(lexer) || lexer->lookahead == '\n') {
364+
scanner->frontmatter_has_info_string = false;
355365
advance(lexer);
366+
} else {
367+
scanner->frontmatter_has_info_string = true;
356368
}
357-
advance(lexer);
358369

359370
return true;
360371
}
361372
}
362373

374+
static inline bool process_frontmatter_info_string(TSLexer *lexer, Scanner *scanner) {
375+
if (scanner->frontmatter_has_info_string) {
376+
while (!lexer->eof(lexer) && lexer->lookahead != '\n') {
377+
advance(lexer);
378+
}
379+
380+
lexer->result_symbol = FRONTMATTER_INFO_STRING;
381+
lexer->mark_end(lexer);
382+
383+
advance(lexer);
384+
return true;
385+
} else {
386+
return false;
387+
}
388+
}
389+
363390
static inline bool process_frontmatter(TSLexer *lexer, Scanner *scanner) {
364391
// seperately parse empty frontmatter, as tree-sitter strips all whitespace,
365392
// including newlines, so i can't rely on parsing only after a newline in this case.
@@ -472,6 +499,12 @@ bool tree_sitter_rust_external_scanner_scan(void *payload, TSLexer *lexer, const
472499
return process_frontmatter_start(lexer, scanner);
473500
}
474501

502+
if (valid_symbols[FRONTMATTER_INFO_STRING]) {
503+
if (process_frontmatter_info_string(lexer, scanner)) {
504+
return true;
505+
}
506+
}
507+
475508
if (valid_symbols[FRONTMATTER_CONTENT]) {
476509
return process_frontmatter(lexer, scanner);
477510
}

test/corpus/source_files.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ fn main() {}
287287

288288
(source_file
289289
(frontmatter
290+
(frontmatter_info_string)
290291
(frontmatter_content))
291292
(function_item
292293
(identifier)

0 commit comments

Comments
 (0)