diff options
Diffstat (limited to 'crates/ra_syntax/src')
-rw-r--r-- | crates/ra_syntax/src/validation.rs | 25 | ||||
-rw-r--r-- | crates/ra_syntax/src/validation/field_expr.rs | 13 |
2 files changed, 20 insertions, 18 deletions
diff --git a/crates/ra_syntax/src/validation.rs b/crates/ra_syntax/src/validation.rs index 1f904434e..2bb3c0a03 100644 --- a/crates/ra_syntax/src/validation.rs +++ b/crates/ra_syntax/src/validation.rs | |||
@@ -1,13 +1,12 @@ | |||
1 | mod block; | 1 | mod block; |
2 | mod field_expr; | ||
3 | 2 | ||
4 | use ra_rustc_lexer::unescape; | 3 | use ra_rustc_lexer::unescape; |
5 | 4 | ||
6 | use crate::{ | 5 | use crate::{ |
7 | algo::visit::{visitor_ctx, VisitorCtx}, | 6 | algo::visit::{visitor_ctx, VisitorCtx}, |
8 | ast, SyntaxError, SyntaxErrorKind, | 7 | ast, AstNode, SyntaxError, SyntaxErrorKind, |
9 | SyntaxKind::{BYTE, BYTE_STRING, CHAR, STRING}, | 8 | SyntaxKind::{BYTE, BYTE_STRING, CHAR, INT_NUMBER, STRING}, |
10 | SyntaxNode, TextUnit, T, | 9 | SyntaxNode, SyntaxToken, TextUnit, T, |
11 | }; | 10 | }; |
12 | 11 | ||
13 | #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] | 12 | #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] |
@@ -101,7 +100,8 @@ pub(crate) fn validate(root: &SyntaxNode) -> Vec<SyntaxError> { | |||
101 | let _ = visitor_ctx(&mut errors) | 100 | let _ = visitor_ctx(&mut errors) |
102 | .visit::<ast::Literal, _>(validate_literal) | 101 | .visit::<ast::Literal, _>(validate_literal) |
103 | .visit::<ast::Block, _>(block::validate_block_node) | 102 | .visit::<ast::Block, _>(block::validate_block_node) |
104 | .visit::<ast::FieldExpr, _>(field_expr::validate_field_expr_node) | 103 | .visit::<ast::FieldExpr, _>(|it, errors| validate_numeric_name(it.name_ref(), errors)) |
104 | .visit::<ast::NamedField, _>(|it, errors| validate_numeric_name(it.name_ref(), errors)) | ||
105 | .accept(&node); | 105 | .accept(&node); |
106 | } | 106 | } |
107 | errors | 107 | errors |
@@ -189,3 +189,18 @@ pub(crate) fn validate_block_structure(root: &SyntaxNode) { | |||
189 | } | 189 | } |
190 | } | 190 | } |
191 | } | 191 | } |
192 | |||
193 | fn validate_numeric_name(name_ref: Option<ast::NameRef>, errors: &mut Vec<SyntaxError>) { | ||
194 | if let Some(int_token) = int_token(name_ref) { | ||
195 | if int_token.text().chars().any(|c| !c.is_digit(10)) { | ||
196 | errors.push(SyntaxError::new( | ||
197 | SyntaxErrorKind::InvalidTupleIndexFormat, | ||
198 | int_token.text_range(), | ||
199 | )); | ||
200 | } | ||
201 | } | ||
202 | |||
203 | fn int_token(name_ref: Option<ast::NameRef>) -> Option<SyntaxToken> { | ||
204 | name_ref?.syntax().first_child_or_token()?.into_token().filter(|it| it.kind() == INT_NUMBER) | ||
205 | } | ||
206 | } | ||
diff --git a/crates/ra_syntax/src/validation/field_expr.rs b/crates/ra_syntax/src/validation/field_expr.rs deleted file mode 100644 index 004f199fd..000000000 --- a/crates/ra_syntax/src/validation/field_expr.rs +++ /dev/null | |||
@@ -1,13 +0,0 @@ | |||
1 | use crate::{ | ||
2 | ast::{self, FieldKind}, | ||
3 | SyntaxError, | ||
4 | SyntaxErrorKind::*, | ||
5 | }; | ||
6 | |||
7 | pub(crate) fn validate_field_expr_node(node: ast::FieldExpr, errors: &mut Vec<SyntaxError>) { | ||
8 | if let Some(FieldKind::Index(idx)) = node.field_access() { | ||
9 | if idx.text().chars().any(|c| c < '0' || c > '9') { | ||
10 | errors.push(SyntaxError::new(InvalidTupleIndexFormat, idx.text_range())); | ||
11 | } | ||
12 | } | ||
13 | } | ||