rust/crates/ra_syntax/src/validation.rs

240 lines
9.0 KiB
Rust
Raw Normal View History

//! FIXME: write short doc here
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::{
2019-10-05 09:03:03 -05:00
ast, match_ast, AstNode, SyntaxError, SyntaxErrorKind,
2019-11-11 02:26:57 -06:00
SyntaxKind::{BYTE, BYTE_STRING, CHAR, CONST_DEF, FN_DEF, INT_NUMBER, STRING, TYPE_ALIAS_DEF},
2019-08-09 05:16:47 -05:00
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-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() {
2019-10-05 09:03:03 -05:00
match_ast! {
match node {
ast::Literal(it) => { validate_literal(it, &mut errors) },
ast::BlockExpr(it) => { block::validate_block_expr(it, &mut errors) },
ast::FieldExpr(it) => { validate_numeric_name(it.name_ref(), &mut errors) },
ast::RecordField(it) => { validate_numeric_name(it.name_ref(), &mut errors) },
2019-11-11 02:26:57 -06:00
ast::Visibility(it) => { validate_visibility(it, &mut errors) },
ast::RangeExpr(it) => { validate_range_expr(it, &mut errors) },
2019-10-05 09:03:03 -05:00
_ => (),
}
}
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>) {
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);
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);
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);
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);
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)
}
}
2019-11-11 02:26:57 -06:00
fn validate_visibility(vis: ast::Visibility, errors: &mut Vec<SyntaxError>) {
let parent = match vis.syntax().parent() {
Some(it) => it,
None => return,
};
match parent.kind() {
FN_DEF | CONST_DEF | TYPE_ALIAS_DEF => (),
_ => return,
}
let impl_block = match parent.parent().and_then(|it| it.parent()).and_then(ast::ImplBlock::cast)
{
Some(it) => it,
None => return,
};
if impl_block.target_trait().is_some() {
errors
.push(SyntaxError::new(SyntaxErrorKind::VisibilityNotAllowed, vis.syntax.text_range()))
}
}
fn validate_range_expr(expr: ast::RangeExpr, errors: &mut Vec<SyntaxError>) {
2019-11-15 14:05:29 -06:00
if expr.op_kind() == Some(ast::RangeOp::Inclusive) && expr.end().is_none() {
errors.push(SyntaxError::new(
SyntaxErrorKind::InclusiveRangeMissingEnd,
2019-11-15 14:05:29 -06:00
expr.syntax().text_range(),
));
}
}