Skip to content
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
3 changes: 3 additions & 0 deletions crates/plotnik-lib/src/diagnostics/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub enum DiagnosticKind {

// User wrote something that doesn't belong
EmptyTree,
EmptyAnonymousNode,
BareIdentifier,
InvalidSeparator,
AnchorInAlternation,
Expand Down Expand Up @@ -144,6 +145,7 @@ impl DiagnosticKind {
Self::ExpectedTypeName => Some("e.g., `::MyType` or `::string`"),
Self::ExpectedFieldName => Some("e.g., `-value`"),
Self::EmptyTree => Some("use `(_)` to match any named node, or `_` for any node"),
Self::EmptyAnonymousNode => Some("use a valid anonymous node or remove it"),
Self::TreeSitterSequenceSyntax => Some("use `{...}` for sequences"),
Self::NegationSyntaxDeprecated => Some("use `-field` instead of `!field`"),
Self::MixedAltBranches => {
Expand Down Expand Up @@ -182,6 +184,7 @@ impl DiagnosticKind {

// Invalid syntax
Self::EmptyTree => "empty `()` is not allowed",
Self::EmptyAnonymousNode => "empty anonymous node",
Self::BareIdentifier => "bare identifier is not valid",
Self::InvalidSeparator => "unexpected separator",
Self::AnchorInAlternation => "anchors cannot appear directly in alternations",
Expand Down
34 changes: 33 additions & 1 deletion crates/plotnik-lib/src/parser/grammar/atoms.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use rowan::TextRange;

use crate::diagnostics::DiagnosticKind;
use crate::parser::Parser;
use crate::parser::cst::SyntaxKind;

Expand All @@ -10,9 +13,38 @@ impl Parser<'_, '_> {

/// `"if"` | `'+'`
pub(crate) fn parse_str(&mut self) {
let start = self.current_span().start();
self.start_node(SyntaxKind::Str);
self.bump_string_tokens();

let open_quote = self.current();
self.bump(); // opening quote

let has_content = self.currently_is(SyntaxKind::StrVal);
if has_content {
self.bump();
}

let closing = self.current();
assert_eq!(
closing, open_quote,
"parse_str: expected closing {:?} but found {:?} \
(lexer should only produce quote tokens from complete strings)",
open_quote, closing
);
let end = self.current_span().end();
self.bump(); // closing quote

self.finish_node();

if !has_content {
self.diagnostics
.report(
self.source_id,
DiagnosticKind::EmptyAnonymousNode,
TextRange::new(start, end),
)
.emit();
}
}

/// Consume string tokens (quote + optional content + quote) without creating a node.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ fn empty_double_quote_string() {
Q = (a "")
"#};

let res = Query::expect_valid_cst(input);
// Empty anonymous nodes are now invalid, but CST structure is still correct
let res = Query::expect(input).dump_cst();

insta::assert_snapshot!(res, @r#"
Root
Expand All @@ -223,7 +224,8 @@ fn empty_single_quote_string() {
Q = (a '')
"#};

let res = Query::expect_valid_cst(input);
// Empty anonymous nodes are now invalid, but CST structure is still correct
let res = Query::expect(input).dump_cst();

insta::assert_snapshot!(res, @r#"
Root
Expand Down
32 changes: 32 additions & 0 deletions crates/plotnik-lib/src/parser/tests/recovery/validation_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1407,3 +1407,35 @@ fn negation_syntax_deprecated_warning() {
help: use `-field` instead of `!field`
");
}

#[test]
fn empty_anonymous_node_double_quotes() {
let input = r#"(node "")"#;

let res = Query::expect_invalid(input);

insta::assert_snapshot!(res, @r#"
error: empty anonymous node
|
1 | (node "")
| ^^
|
help: use a valid anonymous node or remove it
"#);
}

#[test]
fn empty_anonymous_node_single_quotes() {
let input = "(node '')";

let res = Query::expect_invalid(input);

insta::assert_snapshot!(res, @r"
error: empty anonymous node
|
1 | (node '')
| ^^
|
help: use a valid anonymous node or remove it
");
}