rust-analyzer/crates/ra_syntax/src/ast/tokens.rs

189 lines
4.7 KiB
Rust
Raw Normal View History

2019-04-02 10:02:23 +00:00
//! There are many AstNodes, but only a few tokens, so we hand-write them here.
2019-04-02 07:23:18 +00:00
use crate::{
ast::AstToken,
SyntaxKind::{COMMENT, RAW_STRING, STRING, WHITESPACE},
SyntaxToken, TextRange, TextUnit,
2019-04-02 07:23:18 +00:00
};
2019-07-18 16:23:05 +00:00
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Comment(SyntaxToken);
2019-04-02 07:23:18 +00:00
2019-07-18 16:23:05 +00:00
impl AstToken for Comment {
fn cast(token: SyntaxToken) -> Option<Self> {
match token.kind() {
COMMENT => Some(Comment(token)),
_ => None,
2019-04-02 07:23:18 +00:00
}
}
2019-07-18 16:23:05 +00:00
fn syntax(&self) -> &SyntaxToken {
&self.0
2019-04-02 07:23:18 +00:00
}
}
2019-07-18 16:23:05 +00:00
impl Comment {
2019-04-02 09:18:52 +00:00
pub fn kind(&self) -> CommentKind {
kind_by_prefix(self.text())
2019-04-02 07:23:18 +00:00
}
pub fn prefix(&self) -> &'static str {
2019-04-02 09:18:52 +00:00
prefix_by_kind(self.kind())
2019-04-02 07:23:18 +00:00
}
}
2019-04-02 09:18:52 +00:00
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct CommentKind {
pub shape: CommentShape,
pub doc: Option<CommentPlacement>,
}
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum CommentShape {
2019-04-02 07:23:18 +00:00
Line,
2019-04-02 09:18:52 +00:00
Block,
2019-04-02 07:23:18 +00:00
}
2019-04-02 09:18:52 +00:00
impl CommentShape {
pub fn is_line(self) -> bool {
self == CommentShape::Line
}
pub fn is_block(self) -> bool {
self == CommentShape::Block
}
}
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum CommentPlacement {
Inner,
Outer,
}
const COMMENT_PREFIX_TO_KIND: &[(&str, CommentKind)] = {
use {CommentPlacement::*, CommentShape::*};
2019-04-02 09:18:52 +00:00
&[
("///", CommentKind { shape: Line, doc: Some(Outer) }),
("//!", CommentKind { shape: Line, doc: Some(Inner) }),
("/**", CommentKind { shape: Block, doc: Some(Outer) }),
2019-04-02 11:42:47 +00:00
("/*!", CommentKind { shape: Block, doc: Some(Inner) }),
2019-04-02 09:18:52 +00:00
("//", CommentKind { shape: Line, doc: None }),
("/*", CommentKind { shape: Block, doc: None }),
]
};
fn kind_by_prefix(text: &str) -> CommentKind {
for (prefix, kind) in COMMENT_PREFIX_TO_KIND.iter() {
if text.starts_with(prefix) {
return *kind;
2019-04-02 07:23:18 +00:00
}
}
2019-04-02 09:18:52 +00:00
panic!("bad comment text: {:?}", text)
}
2019-04-02 07:23:18 +00:00
2019-04-02 09:18:52 +00:00
fn prefix_by_kind(kind: CommentKind) -> &'static str {
for (prefix, k) in COMMENT_PREFIX_TO_KIND.iter() {
if *k == kind {
return prefix;
2019-04-02 07:23:18 +00:00
}
}
2019-04-02 09:18:52 +00:00
unreachable!()
2019-04-02 07:23:18 +00:00
}
2019-07-18 16:23:05 +00:00
pub struct Whitespace(SyntaxToken);
2019-04-02 07:23:18 +00:00
2019-07-18 16:23:05 +00:00
impl AstToken for Whitespace {
fn cast(token: SyntaxToken) -> Option<Self> {
match token.kind() {
WHITESPACE => Some(Whitespace(token)),
_ => None,
2019-04-02 07:23:18 +00:00
}
}
2019-07-18 16:23:05 +00:00
fn syntax(&self) -> &SyntaxToken {
&self.0
2019-04-02 07:23:18 +00:00
}
}
2019-07-18 16:23:05 +00:00
impl Whitespace {
2019-04-02 07:23:18 +00:00
pub fn spans_multiple_lines(&self) -> bool {
let text = self.text();
text.find('\n').map_or(false, |idx| text[idx + 1..].contains('\n'))
}
}
pub struct String(SyntaxToken);
impl AstToken for String {
fn cast(token: SyntaxToken) -> Option<Self> {
match token.kind() {
STRING => Some(String(token)),
_ => None,
}
}
fn syntax(&self) -> &SyntaxToken {
&self.0
}
}
impl String {
pub fn value(&self) -> Option<std::string::String> {
let text = self.text().as_str();
let usual_string_range = find_usual_string_range(text)?;
let start_of_inside = usual_string_range.start().to_usize() + 1;
let end_of_inside = usual_string_range.end().to_usize();
let inside_str = &text[start_of_inside..end_of_inside];
let mut buf = std::string::String::with_capacity(inside_str.len());
let mut has_error = false;
rustc_lexer::unescape::unescape_str(inside_str, &mut |_, unescaped_char| {
match unescaped_char {
Ok(c) => buf.push(c),
Err(_) => has_error = true,
}
});
if has_error {
return None;
}
Some(buf)
}
}
pub struct RawString(SyntaxToken);
impl AstToken for RawString {
fn cast(token: SyntaxToken) -> Option<Self> {
match token.kind() {
RAW_STRING => Some(RawString(token)),
_ => None,
}
}
fn syntax(&self) -> &SyntaxToken {
&self.0
}
}
impl RawString {
pub fn value(&self) -> Option<std::string::String> {
let text = self.text().as_str();
let usual_string_range = find_usual_string_range(text)?;
let start_of_inside = usual_string_range.start().to_usize() + 1;
let end_of_inside = usual_string_range.end().to_usize();
let inside_str = &text[start_of_inside..end_of_inside];
Some(inside_str.to_string())
}
}
fn find_usual_string_range(s: &str) -> Option<TextRange> {
let left_quote = s.find('"')?;
let right_quote = s.rfind('"')?;
if left_quote == right_quote {
// `s` only contains one quote
None
} else {
Some(TextRange::from_to(
TextUnit::from(left_quote as u32),
TextUnit::from(right_quote as u32),
))
}
}