diff options
author | Aleksey Kladov <[email protected]> | 2019-08-09 11:16:47 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-08-09 11:16:47 +0100 |
commit | f3ee5a15090d8ba6ec220e1f907ed3af27e57734 (patch) | |
tree | d4042c35679ca1e4db06fd9976d90d025fca0966 /crates/ra_syntax | |
parent | 5f82012779c374d9f6b518634aefb14ce28e17e6 (diff) |
Move numeric names inside of `NameRef`
Diffstat (limited to 'crates/ra_syntax')
5 files changed, 32 insertions, 25 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 | } | ||
diff --git a/crates/ra_syntax/test_data/parser/inline/err/0010_bad_tuple_index_expr.txt b/crates/ra_syntax/test_data/parser/inline/err/0010_bad_tuple_index_expr.txt index a21b29c80..465e79e7b 100644 --- a/crates/ra_syntax/test_data/parser/inline/err/0010_bad_tuple_index_expr.txt +++ b/crates/ra_syntax/test_data/parser/inline/err/0010_bad_tuple_index_expr.txt | |||
@@ -30,7 +30,8 @@ SOURCE_FILE@[0; 47) | |||
30 | NAME_REF@[25; 26) | 30 | NAME_REF@[25; 26) |
31 | IDENT@[25; 26) "x" | 31 | IDENT@[25; 26) "x" |
32 | DOT@[26; 27) "." | 32 | DOT@[26; 27) "." |
33 | INT_NUMBER@[27; 31) "1i32" | 33 | NAME_REF@[27; 31) |
34 | INT_NUMBER@[27; 31) "1i32" | ||
34 | SEMI@[31; 32) ";" | 35 | SEMI@[31; 32) ";" |
35 | WHITESPACE@[32; 37) "\n " | 36 | WHITESPACE@[32; 37) "\n " |
36 | EXPR_STMT@[37; 44) | 37 | EXPR_STMT@[37; 44) |
@@ -41,11 +42,11 @@ SOURCE_FILE@[0; 47) | |||
41 | NAME_REF@[37; 38) | 42 | NAME_REF@[37; 38) |
42 | IDENT@[37; 38) "x" | 43 | IDENT@[37; 38) "x" |
43 | DOT@[38; 39) "." | 44 | DOT@[38; 39) "." |
44 | INT_NUMBER@[39; 43) "0x01" | 45 | NAME_REF@[39; 43) |
46 | INT_NUMBER@[39; 43) "0x01" | ||
45 | SEMI@[43; 44) ";" | 47 | SEMI@[43; 44) ";" |
46 | WHITESPACE@[44; 45) "\n" | 48 | WHITESPACE@[44; 45) "\n" |
47 | R_CURLY@[45; 46) "}" | 49 | R_CURLY@[45; 46) "}" |
48 | WHITESPACE@[46; 47) "\n" | 50 | WHITESPACE@[46; 47) "\n" |
49 | error [17; 19): Tuple (struct) field access is only allowed through decimal integers with no underscores or suffix | ||
50 | error [27; 31): Tuple (struct) field access is only allowed through decimal integers with no underscores or suffix | 51 | error [27; 31): Tuple (struct) field access is only allowed through decimal integers with no underscores or suffix |
51 | error [39; 43): Tuple (struct) field access is only allowed through decimal integers with no underscores or suffix | 52 | error [39; 43): Tuple (struct) field access is only allowed through decimal integers with no underscores or suffix |
diff --git a/crates/ra_syntax/test_data/parser/inline/ok/0011_field_expr.txt b/crates/ra_syntax/test_data/parser/inline/ok/0011_field_expr.txt index 78054ec5a..1d2cf2761 100644 --- a/crates/ra_syntax/test_data/parser/inline/ok/0011_field_expr.txt +++ b/crates/ra_syntax/test_data/parser/inline/ok/0011_field_expr.txt | |||
@@ -32,7 +32,8 @@ SOURCE_FILE@[0; 48) | |||
32 | NAME_REF@[26; 27) | 32 | NAME_REF@[26; 27) |
33 | IDENT@[26; 27) "x" | 33 | IDENT@[26; 27) "x" |
34 | DOT@[27; 28) "." | 34 | DOT@[27; 28) "." |
35 | INT_NUMBER@[28; 29) "0" | 35 | NAME_REF@[28; 29) |
36 | INT_NUMBER@[28; 29) "0" | ||
36 | DOT@[29; 30) "." | 37 | DOT@[29; 30) "." |
37 | NAME_REF@[30; 33) | 38 | NAME_REF@[30; 33) |
38 | IDENT@[30; 33) "bar" | 39 | IDENT@[30; 33) "bar" |
@@ -47,7 +48,8 @@ SOURCE_FILE@[0; 48) | |||
47 | NAME_REF@[39; 40) | 48 | NAME_REF@[39; 40) |
48 | IDENT@[39; 40) "x" | 49 | IDENT@[39; 40) "x" |
49 | DOT@[40; 41) "." | 50 | DOT@[40; 41) "." |
50 | INT_NUMBER@[41; 42) "0" | 51 | NAME_REF@[41; 42) |
52 | INT_NUMBER@[41; 42) "0" | ||
51 | ARG_LIST@[42; 44) | 53 | ARG_LIST@[42; 44) |
52 | L_PAREN@[42; 43) "(" | 54 | L_PAREN@[42; 43) "(" |
53 | R_PAREN@[43; 44) ")" | 55 | R_PAREN@[43; 44) ")" |
diff --git a/crates/ra_syntax/test_data/parser/inline/ok/0137_await_expr.txt b/crates/ra_syntax/test_data/parser/inline/ok/0137_await_expr.txt index 99bd76ace..7adb662de 100644 --- a/crates/ra_syntax/test_data/parser/inline/ok/0137_await_expr.txt +++ b/crates/ra_syntax/test_data/parser/inline/ok/0137_await_expr.txt | |||
@@ -31,7 +31,8 @@ SOURCE_FILE@[0; 67) | |||
31 | NAME_REF@[28; 29) | 31 | NAME_REF@[28; 29) |
32 | IDENT@[28; 29) "x" | 32 | IDENT@[28; 29) "x" |
33 | DOT@[29; 30) "." | 33 | DOT@[29; 30) "." |
34 | INT_NUMBER@[30; 31) "0" | 34 | NAME_REF@[30; 31) |
35 | INT_NUMBER@[30; 31) "0" | ||
35 | DOT@[31; 32) "." | 36 | DOT@[31; 32) "." |
36 | AWAIT_KW@[32; 37) "await" | 37 | AWAIT_KW@[32; 37) "await" |
37 | SEMI@[37; 38) ";" | 38 | SEMI@[37; 38) ";" |
@@ -48,7 +49,8 @@ SOURCE_FILE@[0; 67) | |||
48 | NAME_REF@[43; 44) | 49 | NAME_REF@[43; 44) |
49 | IDENT@[43; 44) "x" | 50 | IDENT@[43; 44) "x" |
50 | DOT@[44; 45) "." | 51 | DOT@[44; 45) "." |
51 | INT_NUMBER@[45; 46) "0" | 52 | NAME_REF@[45; 46) |
53 | INT_NUMBER@[45; 46) "0" | ||
52 | ARG_LIST@[46; 48) | 54 | ARG_LIST@[46; 48) |
53 | L_PAREN@[46; 47) "(" | 55 | L_PAREN@[46; 47) "(" |
54 | R_PAREN@[47; 48) ")" | 56 | R_PAREN@[47; 48) ")" |