From 73ded3c63ca2522b7bb6ca8eb7834c5adc1a3511 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 27 Dec 2018 14:42:46 +0300 Subject: dedupe literal parsers --- crates/ra_syntax/src/validation/byte.rs | 7 ++++--- crates/ra_syntax/src/validation/byte_string.rs | 6 +++--- crates/ra_syntax/src/validation/char.rs | 7 ++++--- crates/ra_syntax/src/validation/string.rs | 17 ++++++----------- 4 files changed, 17 insertions(+), 20 deletions(-) (limited to 'crates/ra_syntax/src/validation') diff --git a/crates/ra_syntax/src/validation/byte.rs b/crates/ra_syntax/src/validation/byte.rs index 43c0d7edd..e3603e761 100644 --- a/crates/ra_syntax/src/validation/byte.rs +++ b/crates/ra_syntax/src/validation/byte.rs @@ -2,7 +2,7 @@ use crate::{ ast::{self, AstNode}, - string_lexing::{self, CharComponentKind}, + string_lexing::{self, StringComponentKind}, TextRange, validation::char, yellow::{ @@ -38,11 +38,11 @@ pub(super) fn validate_byte_node(node: ast::Byte, errors: &mut Vec) pub(super) fn validate_byte_component( text: &str, - kind: CharComponentKind, + kind: StringComponentKind, range: TextRange, errors: &mut Vec, ) { - use self::CharComponentKind::*; + use self::StringComponentKind::*; match kind { AsciiEscape => validate_byte_escape(text, range, errors), AsciiCodeEscape => validate_byte_code_escape(text, range, errors), @@ -63,6 +63,7 @@ pub(super) fn validate_byte_component( errors.push(SyntaxError::new(ByteOutOfRange, range)); } } + IgnoreNewline => { /* always valid */ } } } diff --git a/crates/ra_syntax/src/validation/byte_string.rs b/crates/ra_syntax/src/validation/byte_string.rs index 7b830e97c..2f98472f4 100644 --- a/crates/ra_syntax/src/validation/byte_string.rs +++ b/crates/ra_syntax/src/validation/byte_string.rs @@ -17,15 +17,15 @@ pub(crate) fn validate_byte_string_node(node: ast::ByteString, errors: &mut Vec< let range = component.range + literal_range.start(); match component.kind { - StringComponentKind::Char(kind) => { + StringComponentKind::IgnoreNewline => { /* always valid */ } + _ => { // Chars must escape \t, \n and \r codepoints, but strings don't let text = &literal_text[component.range]; match text { "\t" | "\n" | "\r" => { /* always valid */ } - _ => byte::validate_byte_component(text, kind, range, errors), + _ => byte::validate_byte_component(text, component.kind, range, errors), } } - StringComponentKind::IgnoreNewline => { /* always valid */ } } } diff --git a/crates/ra_syntax/src/validation/char.rs b/crates/ra_syntax/src/validation/char.rs index 4728c85e6..deb5b0a9e 100644 --- a/crates/ra_syntax/src/validation/char.rs +++ b/crates/ra_syntax/src/validation/char.rs @@ -6,7 +6,7 @@ use arrayvec::ArrayString; use crate::{ ast::{self, AstNode}, - string_lexing::{self, CharComponentKind}, + string_lexing::{self, StringComponentKind}, TextRange, yellow::{ SyntaxError, @@ -41,12 +41,12 @@ pub(super) fn validate_char_node(node: ast::Char, errors: &mut Vec) pub(super) fn validate_char_component( text: &str, - kind: CharComponentKind, + kind: StringComponentKind, range: TextRange, errors: &mut Vec, ) { // Validate escapes - use self::CharComponentKind::*; + use self::StringComponentKind::*; match kind { AsciiEscape => validate_ascii_escape(text, range, errors), AsciiCodeEscape => validate_ascii_code_escape(text, range, errors), @@ -57,6 +57,7 @@ pub(super) fn validate_char_component( errors.push(SyntaxError::new(UnescapedCodepoint, range)); } } + StringComponentKind::IgnoreNewline => { /* always valid */ } } } diff --git a/crates/ra_syntax/src/validation/string.rs b/crates/ra_syntax/src/validation/string.rs index 089879d15..456180ab6 100644 --- a/crates/ra_syntax/src/validation/string.rs +++ b/crates/ra_syntax/src/validation/string.rs @@ -1,6 +1,6 @@ use crate::{ ast::{self, AstNode}, - string_lexing::{self, StringComponentKind}, + string_lexing, yellow::{ SyntaxError, SyntaxErrorKind::*, @@ -16,16 +16,11 @@ pub(crate) fn validate_string_node(node: ast::String, errors: &mut Vec { - // Chars must escape \t, \n and \r codepoints, but strings don't - let text = &literal_text[component.range]; - match text { - "\t" | "\n" | "\r" => { /* always valid */ } - _ => char::validate_char_component(text, kind, range, errors), - } - } - StringComponentKind::IgnoreNewline => { /* always valid */ } + // Chars must escape \t, \n and \r codepoints, but strings don't + let text = &literal_text[component.range]; + match text { + "\t" | "\n" | "\r" => { /* always valid */ } + _ => char::validate_char_component(text, component.kind, range, errors), } } -- cgit v1.2.3 From 359e70d1b20402ca9cc8731909daecfab598e55d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 27 Dec 2018 15:03:18 +0300 Subject: support literal suffixes --- crates/ra_syntax/src/validation/byte.rs | 4 ++++ crates/ra_syntax/src/validation/byte_string.rs | 4 ++++ crates/ra_syntax/src/validation/char.rs | 4 ++++ crates/ra_syntax/src/validation/string.rs | 4 ++++ 4 files changed, 16 insertions(+) (limited to 'crates/ra_syntax/src/validation') diff --git a/crates/ra_syntax/src/validation/byte.rs b/crates/ra_syntax/src/validation/byte.rs index e3603e761..2f9b7fac7 100644 --- a/crates/ra_syntax/src/validation/byte.rs +++ b/crates/ra_syntax/src/validation/byte.rs @@ -27,6 +27,10 @@ pub(super) fn validate_byte_node(node: ast::Byte, errors: &mut Vec) errors.push(SyntaxError::new(UnclosedByte, literal_range)); } + if let Some(range) = components.suffix { + errors.push(SyntaxError::new(InvalidSuffix, range)); + } + if len == 0 { errors.push(SyntaxError::new(EmptyByte, literal_range)); } diff --git a/crates/ra_syntax/src/validation/byte_string.rs b/crates/ra_syntax/src/validation/byte_string.rs index 2f98472f4..bf4c934a7 100644 --- a/crates/ra_syntax/src/validation/byte_string.rs +++ b/crates/ra_syntax/src/validation/byte_string.rs @@ -32,6 +32,10 @@ pub(crate) fn validate_byte_string_node(node: ast::ByteString, errors: &mut Vec< if !components.has_closing_quote { errors.push(SyntaxError::new(UnclosedString, literal_range)); } + + if let Some(range) = components.suffix { + errors.push(SyntaxError::new(InvalidSuffix, range)); + } } #[cfg(test)] diff --git a/crates/ra_syntax/src/validation/char.rs b/crates/ra_syntax/src/validation/char.rs index deb5b0a9e..50184aaf8 100644 --- a/crates/ra_syntax/src/validation/char.rs +++ b/crates/ra_syntax/src/validation/char.rs @@ -30,6 +30,10 @@ pub(super) fn validate_char_node(node: ast::Char, errors: &mut Vec) errors.push(SyntaxError::new(UnclosedChar, literal_range)); } + if let Some(range) = components.suffix { + errors.push(SyntaxError::new(InvalidSuffix, range)); + } + if len == 0 { errors.push(SyntaxError::new(EmptyChar, literal_range)); } diff --git a/crates/ra_syntax/src/validation/string.rs b/crates/ra_syntax/src/validation/string.rs index 456180ab6..ff1fb6edc 100644 --- a/crates/ra_syntax/src/validation/string.rs +++ b/crates/ra_syntax/src/validation/string.rs @@ -27,6 +27,10 @@ pub(crate) fn validate_string_node(node: ast::String, errors: &mut Vec Date: Thu, 27 Dec 2018 15:10:30 +0300 Subject: fix suffix ranges --- crates/ra_syntax/src/validation/byte.rs | 5 ++++- crates/ra_syntax/src/validation/byte_string.rs | 5 ++++- crates/ra_syntax/src/validation/char.rs | 5 ++++- crates/ra_syntax/src/validation/string.rs | 5 ++++- 4 files changed, 16 insertions(+), 4 deletions(-) (limited to 'crates/ra_syntax/src/validation') diff --git a/crates/ra_syntax/src/validation/byte.rs b/crates/ra_syntax/src/validation/byte.rs index 2f9b7fac7..d0897eeed 100644 --- a/crates/ra_syntax/src/validation/byte.rs +++ b/crates/ra_syntax/src/validation/byte.rs @@ -28,7 +28,10 @@ pub(super) fn validate_byte_node(node: ast::Byte, errors: &mut Vec) } if let Some(range) = components.suffix { - errors.push(SyntaxError::new(InvalidSuffix, range)); + errors.push(SyntaxError::new( + InvalidSuffix, + range + literal_range.start(), + )); } if len == 0 { diff --git a/crates/ra_syntax/src/validation/byte_string.rs b/crates/ra_syntax/src/validation/byte_string.rs index bf4c934a7..f7a4fb156 100644 --- a/crates/ra_syntax/src/validation/byte_string.rs +++ b/crates/ra_syntax/src/validation/byte_string.rs @@ -34,7 +34,10 @@ pub(crate) fn validate_byte_string_node(node: ast::ByteString, errors: &mut Vec< } if let Some(range) = components.suffix { - errors.push(SyntaxError::new(InvalidSuffix, range)); + errors.push(SyntaxError::new( + InvalidSuffix, + range + literal_range.start(), + )); } } diff --git a/crates/ra_syntax/src/validation/char.rs b/crates/ra_syntax/src/validation/char.rs index 50184aaf8..19cd3830f 100644 --- a/crates/ra_syntax/src/validation/char.rs +++ b/crates/ra_syntax/src/validation/char.rs @@ -31,7 +31,10 @@ pub(super) fn validate_char_node(node: ast::Char, errors: &mut Vec) } if let Some(range) = components.suffix { - errors.push(SyntaxError::new(InvalidSuffix, range)); + errors.push(SyntaxError::new( + InvalidSuffix, + range + literal_range.start(), + )); } if len == 0 { diff --git a/crates/ra_syntax/src/validation/string.rs b/crates/ra_syntax/src/validation/string.rs index ff1fb6edc..1371bb1f0 100644 --- a/crates/ra_syntax/src/validation/string.rs +++ b/crates/ra_syntax/src/validation/string.rs @@ -29,7 +29,10 @@ pub(crate) fn validate_string_node(node: ast::String, errors: &mut Vec