aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/syntax_error.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_syntax/src/syntax_error.rs')
-rw-r--r--crates/ra_syntax/src/syntax_error.rs104
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
3use ra_parser::ParseError; 3use ra_parser::ParseError;
4 4
5use crate::{TextRange, TextUnit}; 5use crate::{
6 TextRange, TextUnit,
7 validation::EscapeError,
8};
6 9
7#[derive(Debug, Clone, PartialEq, Eq, Hash)] 10#[derive(Debug, Clone, PartialEq, Eq, Hash)]
8pub struct SyntaxError { 11pub 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)]
68pub enum SyntaxErrorKind { 71pub 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
98impl 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
131impl From<EscapeError> for SyntaxErrorKind {
132 fn from(err: EscapeError) -> Self {
133 SyntaxErrorKind::EscapeError(err)
134 }
135}