mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 21:05:02 +00:00
dedupe literal parsers
This commit is contained in:
parent
dbf03b674e
commit
73ded3c63c
10 changed files with 220 additions and 374 deletions
|
@ -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<SyntaxError>)
|
|||
|
||||
pub(super) fn validate_byte_component(
|
||||
text: &str,
|
||||
kind: CharComponentKind,
|
||||
kind: StringComponentKind,
|
||||
range: TextRange,
|
||||
errors: &mut Vec<SyntaxError>,
|
||||
) {
|
||||
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 */ }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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 */ }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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<SyntaxError>)
|
|||
|
||||
pub(super) fn validate_char_component(
|
||||
text: &str,
|
||||
kind: CharComponentKind,
|
||||
kind: StringComponentKind,
|
||||
range: TextRange,
|
||||
errors: &mut Vec<SyntaxError>,
|
||||
) {
|
||||
// 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 */ }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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<SyntaxErr
|
|||
for component in &mut components {
|
||||
let range = component.range + literal_range.start();
|
||||
|
||||
match component.kind {
|
||||
StringComponentKind::Char(kind) => {
|
||||
// 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),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue