diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-05-07 17:43:10 +0100 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-05-07 17:43:10 +0100 |
commit | d3efedb752bb2198796603d8a479a5e3ee472a97 (patch) | |
tree | ca6a4aee6ad4077a869a932a18c6c8d134406f8c /crates/ra_syntax/src/syntax_error.rs | |
parent | ef782adc293deb287128f005dbab2038ba3ccdc1 (diff) | |
parent | 313314e14b629ebf50389dbd2d440bda922f6ae7 (diff) |
Merge #1253
1253: Share literal validation logic with compiler r=matklad a=matklad
This is neat: the unescape module is literary what compiler is using right now:
https://github.com/rust-lang/rust/blob/c6ac57564852cb6e2d0db60f7b46d9eb98d4b449/src/libsyntax/parse/unescape.rs
So, yeah, code sharing via copy-paste!
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_syntax/src/syntax_error.rs')
-rw-r--r-- | crates/ra_syntax/src/syntax_error.rs | 104 |
1 files changed, 45 insertions, 59 deletions
diff --git a/crates/ra_syntax/src/syntax_error.rs b/crates/ra_syntax/src/syntax_error.rs index 4198eefdb..27e12293b 100644 --- a/crates/ra_syntax/src/syntax_error.rs +++ b/crates/ra_syntax/src/syntax_error.rs | |||
@@ -2,7 +2,10 @@ use std::fmt; | |||
2 | 2 | ||
3 | use ra_parser::ParseError; | 3 | use ra_parser::ParseError; |
4 | 4 | ||
5 | use crate::{TextRange, TextUnit}; | 5 | use crate::{ |
6 | TextRange, TextUnit, | ||
7 | validation::EscapeError, | ||
8 | }; | ||
6 | 9 | ||
7 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 10 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
8 | pub struct SyntaxError { | 11 | pub struct SyntaxError { |
@@ -67,32 +70,7 @@ impl fmt::Display for SyntaxError { | |||
67 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 70 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
68 | pub enum SyntaxErrorKind { | 71 | pub enum SyntaxErrorKind { |
69 | ParseError(ParseError), | 72 | ParseError(ParseError), |
70 | UnescapedCodepoint, | 73 | EscapeError(EscapeError), |
71 | EmptyChar, | ||
72 | UnclosedChar, | ||
73 | OverlongChar, | ||
74 | EmptyByte, | ||
75 | UnclosedByte, | ||
76 | OverlongByte, | ||
77 | ByteOutOfRange, | ||
78 | UnescapedByte, | ||
79 | EmptyByteEscape, | ||
80 | InvalidByteEscape, | ||
81 | TooShortByteCodeEscape, | ||
82 | MalformedByteCodeEscape, | ||
83 | UnicodeEscapeForbidden, | ||
84 | EmptyAsciiEscape, | ||
85 | InvalidAsciiEscape, | ||
86 | TooShortAsciiCodeEscape, | ||
87 | AsciiCodeEscapeOutOfRange, | ||
88 | MalformedAsciiCodeEscape, | ||
89 | UnclosedUnicodeEscape, | ||
90 | MalformedUnicodeEscape, | ||
91 | EmptyUnicodeEcape, | ||
92 | OverlongUnicodeEscape, | ||
93 | UnicodeEscapeOutOfRange, | ||
94 | UnclosedString, | ||
95 | InvalidSuffix, | ||
96 | InvalidBlockAttr, | 74 | InvalidBlockAttr, |
97 | InvalidMatchInnerAttr, | 75 | InvalidMatchInnerAttr, |
98 | InvalidTupleIndexFormat, | 76 | InvalidTupleIndexFormat, |
@@ -102,38 +80,6 @@ impl fmt::Display for SyntaxErrorKind { | |||
102 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | 80 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
103 | use self::SyntaxErrorKind::*; | 81 | use self::SyntaxErrorKind::*; |
104 | match self { | 82 | match self { |
105 | UnescapedCodepoint => write!(f, "This codepoint should always be escaped"), | ||
106 | EmptyAsciiEscape => write!(f, "Empty escape sequence"), | ||
107 | InvalidAsciiEscape => write!(f, "Invalid escape sequence"), | ||
108 | EmptyChar => write!(f, "Empty char literal"), | ||
109 | UnclosedChar => write!(f, "Unclosed char literal"), | ||
110 | OverlongChar => write!(f, "Char literal should be one character long"), | ||
111 | EmptyByte => write!(f, "Empty byte literal"), | ||
112 | UnclosedByte => write!(f, "Unclosed byte literal"), | ||
113 | OverlongByte => write!(f, "Byte literal should be one character long"), | ||
114 | ByteOutOfRange => write!(f, "Byte should be a valid ASCII character"), | ||
115 | UnescapedByte => write!(f, "This byte should always be escaped"), | ||
116 | EmptyByteEscape => write!(f, "Empty escape sequence"), | ||
117 | InvalidByteEscape => write!(f, "Invalid escape sequence"), | ||
118 | TooShortByteCodeEscape => write!(f, "Escape sequence should have two digits"), | ||
119 | MalformedByteCodeEscape => write!(f, "Escape sequence should be a hexadecimal number"), | ||
120 | UnicodeEscapeForbidden => { | ||
121 | write!(f, "Unicode escapes are not allowed in byte literals or byte strings") | ||
122 | } | ||
123 | TooShortAsciiCodeEscape => write!(f, "Escape sequence should have two digits"), | ||
124 | AsciiCodeEscapeOutOfRange => { | ||
125 | write!(f, "Escape sequence should be between \\x00 and \\x7F") | ||
126 | } | ||
127 | MalformedAsciiCodeEscape => write!(f, "Escape sequence should be a hexadecimal number"), | ||
128 | UnclosedUnicodeEscape => write!(f, "Missing `}}`"), | ||
129 | MalformedUnicodeEscape => write!(f, "Malformed unicode escape sequence"), | ||
130 | EmptyUnicodeEcape => write!(f, "Empty unicode escape sequence"), | ||
131 | OverlongUnicodeEscape => { | ||
132 | write!(f, "Unicode escape sequence should have at most 6 digits") | ||
133 | } | ||
134 | UnicodeEscapeOutOfRange => write!(f, "Unicode escape code should be at most 0x10FFFF"), | ||
135 | UnclosedString => write!(f, "Unclosed string literal"), | ||
136 | InvalidSuffix => write!(f, "Invalid literal suffix"), | ||
137 | InvalidBlockAttr => { | 83 | InvalidBlockAttr => { |
138 | write!(f, "A block in this position cannot accept inner attributes") | 84 | write!(f, "A block in this position cannot accept inner attributes") |
139 | } | 85 | } |
@@ -144,6 +90,46 @@ impl fmt::Display for SyntaxErrorKind { | |||
144 | write!(f, "Tuple (struct) field access is only allowed through decimal integers with no underscores or suffix") | 90 | write!(f, "Tuple (struct) field access is only allowed through decimal integers with no underscores or suffix") |
145 | } | 91 | } |
146 | ParseError(msg) => write!(f, "{}", msg.0), | 92 | ParseError(msg) => write!(f, "{}", msg.0), |
93 | EscapeError(err) => write!(f, "{}", err), | ||
147 | } | 94 | } |
148 | } | 95 | } |
149 | } | 96 | } |
97 | |||
98 | impl fmt::Display for EscapeError { | ||
99 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
100 | let msg = match self { | ||
101 | EscapeError::ZeroChars => "Empty literal", | ||
102 | EscapeError::MoreThanOneChar => "Literal should be one character long", | ||
103 | EscapeError::LoneSlash => "Character must be escaped: '\\'", | ||
104 | EscapeError::InvalidEscape => "Invalid escape sequence", | ||
105 | EscapeError::BareCarriageReturn => "Character must be escaped: '\r'", | ||
106 | EscapeError::EscapeOnlyChar => "Character must be escaped", | ||
107 | EscapeError::TooShortHexEscape => "Escape sequence should have two digits", | ||
108 | EscapeError::InvalidCharInHexEscape => "Escape sequence should be a hexadecimal number", | ||
109 | EscapeError::OutOfRangeHexEscape => "Escape sequence should be ASCII", | ||
110 | EscapeError::NoBraceInUnicodeEscape => "Invalid escape sequence", | ||
111 | EscapeError::InvalidCharInUnicodeEscape => "Invalid escape sequence", | ||
112 | EscapeError::EmptyUnicodeEscape => "Invalid escape sequence", | ||
113 | EscapeError::UnclosedUnicodeEscape => "Missing '}'", | ||
114 | EscapeError::LeadingUnderscoreUnicodeEscape => "Invalid escape sequence", | ||
115 | EscapeError::OverlongUnicodeEscape => { | ||
116 | "Unicode escape sequence should have at most 6 digits" | ||
117 | } | ||
118 | EscapeError::LoneSurrogateUnicodeEscape => { | ||
119 | "Unicode escape code should not be a surrogate" | ||
120 | } | ||
121 | EscapeError::OutOfRangeUnicodeEscape => { | ||
122 | "Unicode escape code should be at most 0x10FFFF" | ||
123 | } | ||
124 | EscapeError::UnicodeEscapeInByte => "Unicode escapes are not allowed in bytes", | ||
125 | EscapeError::NonAsciiCharInByte => "Non ASCII characters are not allowed in bytes", | ||
126 | }; | ||
127 | write!(f, "{}", msg) | ||
128 | } | ||
129 | } | ||
130 | |||
131 | impl From<EscapeError> for SyntaxErrorKind { | ||
132 | fn from(err: EscapeError) -> Self { | ||
133 | SyntaxErrorKind::EscapeError(err) | ||
134 | } | ||
135 | } | ||