Skip to content

Commit e3adf69

Browse files
committed
initialize parsing of frontmatter
1 parent bb08b6f commit e3adf69

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

grammar.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ module.exports = grammar({
7676
$._inner_block_doc_comment_marker,
7777
$._block_comment_content,
7878
$._line_doc_content,
79+
$.frontmatter,
7980
$._error_sentinel,
8081
],
8182

@@ -117,6 +118,7 @@ module.exports = grammar({
117118
rules: {
118119
source_file: $ => seq(
119120
optional($.shebang),
121+
optional($.frontmatter),
120122
repeat($._statement),
121123
),
122124

src/scanner.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ enum TokenType {
1313
BLOCK_INNER_DOC_MARKER,
1414
BLOCK_COMMENT_CONTENT,
1515
LINE_DOC_CONTENT,
16+
FRONTMATTER,
1617
ERROR_SENTINEL
1718
};
1819

@@ -331,6 +332,41 @@ static inline bool process_block_comment(TSLexer *lexer, const bool *valid_symbo
331332
return false;
332333
}
333334

335+
static inline bool process_frontmatter(TSLexer *lexer) {
336+
uint8_t opening = 0;
337+
while (lexer->lookahead == '-') {
338+
opening++;
339+
advance(lexer);
340+
}
341+
342+
if (opening < 3) {
343+
return false;
344+
}
345+
346+
for (;;) {
347+
if (lexer->eof(lexer)) {
348+
return false;
349+
}
350+
351+
if (lexer->lookahead == '\n') {
352+
advance(lexer);
353+
354+
uint8_t amount = 0;
355+
while (lexer->lookahead == '-' && amount < opening) {
356+
amount++;
357+
advance(lexer);
358+
}
359+
360+
if (amount == opening) {
361+
lexer->result_symbol = FRONTMATTER;
362+
return true;
363+
}
364+
} else {
365+
advance(lexer);
366+
}
367+
}
368+
}
369+
334370
bool tree_sitter_rust_external_scanner_scan(void *payload, TSLexer *lexer, const bool *valid_symbols) {
335371
// The documentation states that if the lexical analysis fails for some reason
336372
// they will mark every state as valid and pass it to the external scanner
@@ -389,5 +425,9 @@ bool tree_sitter_rust_external_scanner_scan(void *payload, TSLexer *lexer, const
389425
return process_float_literal(lexer);
390426
}
391427

428+
if (valid_symbols[FRONTMATTER]) {
429+
return process_frontmatter(lexer);
430+
}
431+
392432
return false;
393433
}

0 commit comments

Comments
 (0)