diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2018-11-04 14:47:20 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2018-11-04 14:47:20 +0000 |
commit | 576b9a0727ebbf00521bc1131cda808145696d06 (patch) | |
tree | a1ef0fa5dbfd431e8a58afc6542c32c9ecefed04 /crates/ra_syntax/src/ast | |
parent | 19c6cbd9540ef87850161cad7e108b380eceea24 (diff) | |
parent | 9b5bbab104d8ba445143f6f3a9e4149b40c29ae5 (diff) |
Merge #184
184: Basic validation for character literals r=aochagavia a=aochagavia
As part of #27 I would like to add a validator for characters that detects missing quotes and too long characters. I set up a dummy implementation to get my feet wet, which generates errors whenever it finds a character.
Right now I have the following questions:
1. The `SyntaxError` type seems too basic to me. I think it would make sense to have a `SyntaxErrorKind` instead of a `msg` field (we can implement `Display` for it so you can generate the string if desired). It should also have a `TextRange` instead of a `TextUnit`, so you can support errors that are longer than one character. Do you agree?
1. I am manually checking whether the literal is a character (see the `is_char` method). Ideally, I would like to have a `LiteralKind` enum with variants like `Int`, `Float`, `Char`, `String`, etc. but it seems cumbersome to write all that by hand. Is there a way to specify this in `grammar.ron` so that the code is generated (the same way the `Expr` enum is generated)?
By the way, there seems to be no error reporting of panics inside the language server. When I was developing this PR I accidentally introduced a panic, which resulted in no syntax errors being shown. I knew something was wrong, because normally the vscode highlights syntax errors, but I didn't know it was caused by a panic.
Co-authored-by: Adolfo OchagavĂa <[email protected]>
Diffstat (limited to 'crates/ra_syntax/src/ast')
-rw-r--r-- | crates/ra_syntax/src/ast/generated.rs | 34 | ||||
-rw-r--r-- | crates/ra_syntax/src/ast/mod.rs | 6 |
2 files changed, 40 insertions, 0 deletions
diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs index f77795d05..75769a4e9 100644 --- a/crates/ra_syntax/src/ast/generated.rs +++ b/crates/ra_syntax/src/ast/generated.rs | |||
@@ -409,6 +409,40 @@ impl<'a> AstNode<'a> for CastExpr<'a> { | |||
409 | 409 | ||
410 | impl<'a> CastExpr<'a> {} | 410 | impl<'a> CastExpr<'a> {} |
411 | 411 | ||
412 | // Char | ||
413 | |||
414 | #[derive(Debug, Clone)] | ||
415 | pub struct CharNode(SyntaxNode); | ||
416 | |||
417 | impl CharNode { | ||
418 | pub fn ast(&self) -> Char { | ||
419 | Char::cast(self.0.borrowed()).unwrap() | ||
420 | } | ||
421 | } | ||
422 | |||
423 | impl<'a> From<Char<'a>> for CharNode { | ||
424 | fn from(ast: Char<'a>) -> CharNode { | ||
425 | let syntax = ast.syntax().owned(); | ||
426 | CharNode(syntax) | ||
427 | } | ||
428 | } | ||
429 | #[derive(Debug, Clone, Copy)] | ||
430 | pub struct Char<'a> { | ||
431 | syntax: SyntaxNodeRef<'a>, | ||
432 | } | ||
433 | |||
434 | impl<'a> AstNode<'a> for Char<'a> { | ||
435 | fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> { | ||
436 | match syntax.kind() { | ||
437 | CHAR => Some(Char { syntax }), | ||
438 | _ => None, | ||
439 | } | ||
440 | } | ||
441 | fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax } | ||
442 | } | ||
443 | |||
444 | impl<'a> Char<'a> {} | ||
445 | |||
412 | // Comment | 446 | // Comment |
413 | 447 | ||
414 | #[derive(Debug, Clone)] | 448 | #[derive(Debug, Clone)] |
diff --git a/crates/ra_syntax/src/ast/mod.rs b/crates/ra_syntax/src/ast/mod.rs index 688ffff47..4355531d0 100644 --- a/crates/ra_syntax/src/ast/mod.rs +++ b/crates/ra_syntax/src/ast/mod.rs | |||
@@ -123,6 +123,12 @@ impl<'a> Lifetime<'a> { | |||
123 | } | 123 | } |
124 | } | 124 | } |
125 | 125 | ||
126 | impl<'a> Char<'a> { | ||
127 | pub fn text(&self) -> &SmolStr { | ||
128 | &self.syntax().leaf_text().unwrap() | ||
129 | } | ||
130 | } | ||
131 | |||
126 | impl<'a> Comment<'a> { | 132 | impl<'a> Comment<'a> { |
127 | pub fn text(&self) -> &SmolStr { | 133 | pub fn text(&self) -> &SmolStr { |
128 | self.syntax().leaf_text().unwrap() | 134 | self.syntax().leaf_text().unwrap() |