Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't over-parse text nodes #50

Merged
merged 2 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified docs/js/tree-sitter-rst.wasm
Binary file not shown.
30 changes: 15 additions & 15 deletions src/tree_sitter_rst/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ static bool parse_overline(RSTScanner* scanner)
return false;
}

bool is_word = is_start_char(adornment);
int overline_length = 0;
scanner->advance(scanner);
lexer->mark_end(lexer);
int overline_length = 1;

while (true) {
if (scanner->lookahead != adornment) {
Expand All @@ -91,12 +92,9 @@ static bool parse_overline(RSTScanner* scanner)
if (is_space(scanner->lookahead)) {
break;
}
return parse_text(scanner, !is_word);
return parse_text(scanner, false);
}
scanner->advance(scanner);
if (is_word && overline_length == 0) {
lexer->mark_end(lexer);
}
overline_length++;
}

Expand Down Expand Up @@ -175,8 +173,9 @@ static bool parse_underline(RSTScanner* scanner)
return false;
}

bool is_word = is_start_char(adornment);
int underline_length = 0;
scanner->advance(scanner);
lexer->mark_end(lexer);
int underline_length = 1;

while (!is_newline(scanner->lookahead)) {
if (scanner->lookahead != adornment) {
Expand All @@ -187,12 +186,9 @@ static bool parse_underline(RSTScanner* scanner)
if (is_space(scanner->lookahead)) {
break;
}
return parse_text(scanner, !is_word);
return parse_text(scanner, false);
}
scanner->advance(scanner);
if (is_word && underline_length == 0) {
lexer->mark_end(lexer);
}
underline_length++;
}

Expand Down Expand Up @@ -252,12 +248,14 @@ static bool fallback_adornment(RSTScanner* scanner, int32_t adornment, int adorn
if (ok) {
return true;
}
return parse_text(scanner, false);
}
if (adornment == ':' && valid_symbols[T_FIELD_MARK]) {
bool ok = parse_inner_field_mark(scanner);
if (ok) {
return true;
}
return parse_text(scanner, false);
}
if (adornment == '`' && (valid_symbols[T_INTERPRETED_TEXT] || valid_symbols[T_INTERPRETED_TEXT_PREFIX] || valid_symbols[T_REFERENCE])) {
return parse_inner_inline_markup(scanner, IM_INTERPRETED_TEXT | IM_INTERPRETED_TEXT_PREFIX | IM_REFERENCE);
Expand Down Expand Up @@ -609,10 +607,11 @@ static bool parse_field_mark_end(RSTScanner* scanner)
}

scanner->advance(scanner);
lexer->mark_end(lexer);

if (is_space(scanner->lookahead)) {
// Consume all whitespaces.
get_indent_level(scanner);
lexer->mark_end(lexer);
// Go to the next line.
while (!is_newline(scanner->lookahead)) {
scanner->advance(scanner);
Expand All @@ -639,7 +638,7 @@ static bool parse_field_mark_end(RSTScanner* scanner)
return true;
}

return parse_text(scanner, true);
return parse_text(scanner, false);
}

static bool parse_label(RSTScanner* scanner)
Expand Down Expand Up @@ -1091,6 +1090,7 @@ static bool parse_inline_markup(RSTScanner* scanner)
const bool* valid_symbols = scanner->valid_symbols;
TSLexer* lexer = scanner->lexer;
scanner->advance(scanner);
lexer->mark_end(lexer);

unsigned type = 0;

Expand Down Expand Up @@ -1161,7 +1161,7 @@ static bool parse_inner_inline_markup(RSTScanner* scanner, unsigned type)
}
}
}
return parse_text(scanner, true);
return parse_text(scanner, false);
}

while (scanner->lookahead != CHAR_EOF) {
Expand Down
30 changes: 30 additions & 0 deletions test/corpus/inline_markup.txt
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ interpreted_text`

I'm in (`parenthesis`)

I'm in [`brackets`, `too`].

[`Brackets`, `again`].

:`start with colon` and :`end with colon`

---

(document
Expand All @@ -146,6 +152,15 @@ I'm in (`parenthesis`)
(paragraph
(interpreted_text))
(paragraph
(interpreted_text))
(paragraph
(interpreted_text)
(interpreted_text))
(paragraph
(interpreted_text)
(interpreted_text))
(paragraph
(interpreted_text)
(interpreted_text)))

===========================
Expand Down Expand Up @@ -222,6 +237,12 @@ here``

I'm in (``parenthesis``)

I'm in [``brackets``, ``too``].

[``Brackets``, ``again``].

:``start with colon`` and :``end with colon``

---

(document
Expand Down Expand Up @@ -249,6 +270,15 @@ I'm in (``parenthesis``)
(paragraph
(literal))
(paragraph
(literal))
(paragraph
(literal)
(literal))
(paragraph
(literal)
(literal))
(paragraph
(literal)
(literal)))

=======================
Expand Down
Binary file modified tree-sitter-rst.wasm
Binary file not shown.
Loading