2019-01-28 14:03:56 -06:00
|
|
|
mod block;
|
2018-11-08 08:42:00 -06:00
|
|
|
|
2019-08-20 11:16:57 -05:00
|
|
|
use rustc_lexer::unescape;
|
2019-07-24 03:47:28 -05:00
|
|
|
|
2019-01-07 07:15:47 -06:00
|
|
|
use crate::{
|
|
|
|
algo::visit::{visitor_ctx, VisitorCtx},
|
2019-08-09 05:16:47 -05:00
|
|
|
ast, AstNode, SyntaxError, SyntaxErrorKind,
|
|
|
|
SyntaxKind::{BYTE, BYTE_STRING, CHAR, INT_NUMBER, STRING},
|
|
|
|
SyntaxNode, SyntaxToken, TextUnit, T,
|
2019-01-07 07:15:47 -06:00
|
|
|
};
|
|
|
|
|
2019-07-24 03:47:28 -05:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
|
|
pub enum EscapeError {
|
|
|
|
ZeroChars,
|
|
|
|
MoreThanOneChar,
|
|
|
|
LoneSlash,
|
|
|
|
InvalidEscape,
|
|
|
|
BareCarriageReturn,
|
|
|
|
EscapeOnlyChar,
|
|
|
|
TooShortHexEscape,
|
|
|
|
InvalidCharInHexEscape,
|
|
|
|
OutOfRangeHexEscape,
|
|
|
|
NoBraceInUnicodeEscape,
|
|
|
|
InvalidCharInUnicodeEscape,
|
|
|
|
EmptyUnicodeEscape,
|
|
|
|
UnclosedUnicodeEscape,
|
|
|
|
LeadingUnderscoreUnicodeEscape,
|
|
|
|
OverlongUnicodeEscape,
|
|
|
|
LoneSurrogateUnicodeEscape,
|
|
|
|
OutOfRangeUnicodeEscape,
|
|
|
|
UnicodeEscapeInByte,
|
|
|
|
NonAsciiCharInByte,
|
|
|
|
}
|
|
|
|
|
2019-08-20 11:16:57 -05:00
|
|
|
impl From<rustc_lexer::unescape::EscapeError> for EscapeError {
|
|
|
|
fn from(err: rustc_lexer::unescape::EscapeError) -> Self {
|
2019-07-24 03:47:28 -05:00
|
|
|
match err {
|
2019-08-20 11:16:57 -05:00
|
|
|
rustc_lexer::unescape::EscapeError::ZeroChars => EscapeError::ZeroChars,
|
|
|
|
rustc_lexer::unescape::EscapeError::MoreThanOneChar => EscapeError::MoreThanOneChar,
|
|
|
|
rustc_lexer::unescape::EscapeError::LoneSlash => EscapeError::LoneSlash,
|
|
|
|
rustc_lexer::unescape::EscapeError::InvalidEscape => EscapeError::InvalidEscape,
|
|
|
|
rustc_lexer::unescape::EscapeError::BareCarriageReturn
|
|
|
|
| rustc_lexer::unescape::EscapeError::BareCarriageReturnInRawString => {
|
2019-07-24 03:47:28 -05:00
|
|
|
EscapeError::BareCarriageReturn
|
|
|
|
}
|
2019-08-20 11:16:57 -05:00
|
|
|
rustc_lexer::unescape::EscapeError::EscapeOnlyChar => EscapeError::EscapeOnlyChar,
|
|
|
|
rustc_lexer::unescape::EscapeError::TooShortHexEscape => EscapeError::TooShortHexEscape,
|
|
|
|
rustc_lexer::unescape::EscapeError::InvalidCharInHexEscape => {
|
2019-07-24 03:47:28 -05:00
|
|
|
EscapeError::InvalidCharInHexEscape
|
|
|
|
}
|
2019-08-20 11:16:57 -05:00
|
|
|
rustc_lexer::unescape::EscapeError::OutOfRangeHexEscape => {
|
2019-07-24 03:47:28 -05:00
|
|
|
EscapeError::OutOfRangeHexEscape
|
|
|
|
}
|
2019-08-20 11:16:57 -05:00
|
|
|
rustc_lexer::unescape::EscapeError::NoBraceInUnicodeEscape => {
|
2019-07-24 03:47:28 -05:00
|
|
|
EscapeError::NoBraceInUnicodeEscape
|
|
|
|
}
|
2019-08-20 11:16:57 -05:00
|
|
|
rustc_lexer::unescape::EscapeError::InvalidCharInUnicodeEscape => {
|
2019-07-24 03:47:28 -05:00
|
|
|
EscapeError::InvalidCharInUnicodeEscape
|
|
|
|
}
|
2019-08-20 11:16:57 -05:00
|
|
|
rustc_lexer::unescape::EscapeError::EmptyUnicodeEscape => {
|
2019-07-24 03:47:28 -05:00
|
|
|
EscapeError::EmptyUnicodeEscape
|
|
|
|
}
|
2019-08-20 11:16:57 -05:00
|
|
|
rustc_lexer::unescape::EscapeError::UnclosedUnicodeEscape => {
|
2019-07-24 03:47:28 -05:00
|
|
|
EscapeError::UnclosedUnicodeEscape
|
|
|
|
}
|
2019-08-20 11:16:57 -05:00
|
|
|
rustc_lexer::unescape::EscapeError::LeadingUnderscoreUnicodeEscape => {
|
2019-07-24 03:47:28 -05:00
|
|
|
EscapeError::LeadingUnderscoreUnicodeEscape
|
|
|
|
}
|
2019-08-20 11:16:57 -05:00
|
|
|
rustc_lexer::unescape::EscapeError::OverlongUnicodeEscape => {
|
2019-07-24 03:47:28 -05:00
|
|
|
EscapeError::OverlongUnicodeEscape
|
|
|
|
}
|
2019-08-20 11:16:57 -05:00
|
|
|
rustc_lexer::unescape::EscapeError::LoneSurrogateUnicodeEscape => {
|
2019-07-24 03:47:28 -05:00
|
|
|
EscapeError::LoneSurrogateUnicodeEscape
|
|
|
|
}
|
2019-08-20 11:16:57 -05:00
|
|
|
rustc_lexer::unescape::EscapeError::OutOfRangeUnicodeEscape => {
|
2019-07-24 03:47:28 -05:00
|
|
|
EscapeError::OutOfRangeUnicodeEscape
|
|
|
|
}
|
2019-08-20 11:16:57 -05:00
|
|
|
rustc_lexer::unescape::EscapeError::UnicodeEscapeInByte => {
|
2019-07-24 03:47:28 -05:00
|
|
|
EscapeError::UnicodeEscapeInByte
|
|
|
|
}
|
2019-08-20 11:16:57 -05:00
|
|
|
rustc_lexer::unescape::EscapeError::NonAsciiCharInByte
|
|
|
|
| rustc_lexer::unescape::EscapeError::NonAsciiCharInByteString => {
|
2019-07-24 03:47:28 -05:00
|
|
|
EscapeError::NonAsciiCharInByte
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-20 11:16:57 -05:00
|
|
|
impl From<rustc_lexer::unescape::EscapeError> for SyntaxErrorKind {
|
|
|
|
fn from(err: rustc_lexer::unescape::EscapeError) -> Self {
|
2019-07-24 03:47:28 -05:00
|
|
|
SyntaxErrorKind::EscapeError(err.into())
|
|
|
|
}
|
|
|
|
}
|
2019-05-07 11:38:26 -05:00
|
|
|
|
2019-07-21 05:34:15 -05:00
|
|
|
pub(crate) fn validate(root: &SyntaxNode) -> Vec<SyntaxError> {
|
2018-11-08 08:42:00 -06:00
|
|
|
let mut errors = Vec::new();
|
2019-07-21 05:34:15 -05:00
|
|
|
for node in root.descendants() {
|
2018-11-08 08:42:00 -06:00
|
|
|
let _ = visitor_ctx(&mut errors)
|
2019-03-30 05:25:53 -05:00
|
|
|
.visit::<ast::Literal, _>(validate_literal)
|
2019-09-02 11:33:02 -05:00
|
|
|
.visit::<ast::BlockExpr, _>(block::validate_block_expr)
|
2019-08-09 05:16:47 -05:00
|
|
|
.visit::<ast::FieldExpr, _>(|it, errors| validate_numeric_name(it.name_ref(), errors))
|
2019-08-23 07:55:21 -05:00
|
|
|
.visit::<ast::RecordField, _>(|it, errors| validate_numeric_name(it.name_ref(), errors))
|
2019-07-18 11:23:05 -05:00
|
|
|
.accept(&node);
|
2018-11-08 08:42:00 -06:00
|
|
|
}
|
|
|
|
errors
|
|
|
|
}
|
2019-02-21 06:51:22 -06:00
|
|
|
|
2019-03-30 05:25:53 -05:00
|
|
|
// FIXME: kill duplication
|
2019-07-18 11:23:05 -05:00
|
|
|
fn validate_literal(literal: ast::Literal, acc: &mut Vec<SyntaxError>) {
|
2019-05-07 11:38:26 -05:00
|
|
|
let token = literal.token();
|
|
|
|
let text = token.text().as_str();
|
|
|
|
match token.kind() {
|
|
|
|
BYTE => {
|
|
|
|
if let Some(end) = text.rfind('\'') {
|
|
|
|
if let Some(without_quotes) = text.get(2..end) {
|
|
|
|
if let Err((off, err)) = unescape::unescape_byte(without_quotes) {
|
2019-07-20 04:58:27 -05:00
|
|
|
let off = token.text_range().start() + TextUnit::from_usize(off + 2);
|
2019-05-07 11:38:26 -05:00
|
|
|
acc.push(SyntaxError::new(err.into(), off))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CHAR => {
|
|
|
|
if let Some(end) = text.rfind('\'') {
|
|
|
|
if let Some(without_quotes) = text.get(1..end) {
|
|
|
|
if let Err((off, err)) = unescape::unescape_char(without_quotes) {
|
2019-07-20 04:58:27 -05:00
|
|
|
let off = token.text_range().start() + TextUnit::from_usize(off + 1);
|
2019-05-07 11:38:26 -05:00
|
|
|
acc.push(SyntaxError::new(err.into(), off))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
BYTE_STRING => {
|
|
|
|
if let Some(end) = text.rfind('\"') {
|
|
|
|
if let Some(without_quotes) = text.get(2..end) {
|
|
|
|
unescape::unescape_byte_str(without_quotes, &mut |range, char| {
|
|
|
|
if let Err(err) = char {
|
|
|
|
let off = range.start;
|
2019-07-20 04:58:27 -05:00
|
|
|
let off = token.text_range().start() + TextUnit::from_usize(off + 2);
|
2019-05-07 11:38:26 -05:00
|
|
|
acc.push(SyntaxError::new(err.into(), off))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
STRING => {
|
|
|
|
if let Some(end) = text.rfind('\"') {
|
|
|
|
if let Some(without_quotes) = text.get(1..end) {
|
|
|
|
unescape::unescape_str(without_quotes, &mut |range, char| {
|
|
|
|
if let Err(err) = char {
|
|
|
|
let off = range.start;
|
2019-07-20 04:58:27 -05:00
|
|
|
let off = token.text_range().start() + TextUnit::from_usize(off + 1);
|
2019-05-07 11:38:26 -05:00
|
|
|
acc.push(SyntaxError::new(err.into(), off))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-30 05:25:53 -05:00
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 06:51:22 -06:00
|
|
|
pub(crate) fn validate_block_structure(root: &SyntaxNode) {
|
|
|
|
let mut stack = Vec::new();
|
|
|
|
for node in root.descendants() {
|
|
|
|
match node.kind() {
|
2019-05-15 07:35:47 -05:00
|
|
|
T!['{'] => stack.push(node),
|
|
|
|
T!['}'] => {
|
2019-02-21 06:51:22 -06:00
|
|
|
if let Some(pair) = stack.pop() {
|
|
|
|
assert_eq!(
|
|
|
|
node.parent(),
|
|
|
|
pair.parent(),
|
2019-07-20 04:48:24 -05:00
|
|
|
"\nunpaired curleys:\n{}\n{:#?}\n",
|
2019-02-21 06:51:22 -06:00
|
|
|
root.text(),
|
2019-07-20 04:48:24 -05:00
|
|
|
root,
|
2019-02-21 06:51:22 -06:00
|
|
|
);
|
|
|
|
assert!(
|
|
|
|
node.next_sibling().is_none() && pair.prev_sibling().is_none(),
|
|
|
|
"\nfloating curlys at {:?}\nfile:\n{}\nerror:\n{}\n",
|
|
|
|
node,
|
|
|
|
root.text(),
|
|
|
|
node.text(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-09 05:16:47 -05:00
|
|
|
|
|
|
|
fn validate_numeric_name(name_ref: Option<ast::NameRef>, errors: &mut Vec<SyntaxError>) {
|
|
|
|
if let Some(int_token) = int_token(name_ref) {
|
|
|
|
if int_token.text().chars().any(|c| !c.is_digit(10)) {
|
|
|
|
errors.push(SyntaxError::new(
|
|
|
|
SyntaxErrorKind::InvalidTupleIndexFormat,
|
|
|
|
int_token.text_range(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn int_token(name_ref: Option<ast::NameRef>) -> Option<SyntaxToken> {
|
|
|
|
name_ref?.syntax().first_child_or_token()?.into_token().filter(|it| it.kind() == INT_NUMBER)
|
|
|
|
}
|
|
|
|
}
|