Wrap float literals in their own node
This commit is contained in:
parent
2d5d16f18c
commit
502c519e7d
@ -972,7 +972,7 @@ fn from(ast_lit_kind: ast::LiteralKind) -> Self {
|
||||
}
|
||||
}
|
||||
LiteralKind::FloatNumber(lit) => {
|
||||
let ty = lit.suffix().and_then(BuiltinFloat::from_suffix);
|
||||
let ty = lit.suffix().and_then(|s| BuiltinFloat::from_suffix(&s));
|
||||
Literal::Float(Default::default(), ty)
|
||||
}
|
||||
LiteralKind::ByteString(bs) => {
|
||||
|
@ -4,10 +4,7 @@
|
||||
use cfg::CfgExpr;
|
||||
use either::Either;
|
||||
use mbe::{parse_exprs_with_sep, parse_to_token_tree};
|
||||
use syntax::{
|
||||
ast::{self, AstToken},
|
||||
SmolStr,
|
||||
};
|
||||
use syntax::{ast, SmolStr};
|
||||
|
||||
use crate::{db::AstDatabase, name, quote, ExpandError, ExpandResult, MacroCallId, MacroCallLoc};
|
||||
|
||||
@ -358,14 +355,7 @@ fn unreachable_expand(
|
||||
}
|
||||
|
||||
fn unquote_str(lit: &tt::Literal) -> Option<String> {
|
||||
let lit = ast::make::tokens::literal(&lit.to_string());
|
||||
let token = ast::String::cast(lit)?;
|
||||
token.value().map(|it| it.into_owned())
|
||||
}
|
||||
|
||||
fn unquote_byte_string(lit: &tt::Literal) -> Option<Vec<u8>> {
|
||||
let lit = ast::make::tokens::literal(&lit.to_string());
|
||||
let token = ast::ByteString::cast(lit)?;
|
||||
let token = ast::make::literal(&lit.to_string()).as_string()?;
|
||||
token.value().map(|it| it.into_owned())
|
||||
}
|
||||
|
||||
@ -442,12 +432,16 @@ fn concat_bytes_expand(
|
||||
for (i, t) in tt.token_trees.iter().enumerate() {
|
||||
match t {
|
||||
tt::TokenTree::Leaf(tt::Leaf::Literal(lit)) => {
|
||||
let token = ast::make::tokens::literal(&lit.to_string());
|
||||
match token.kind() {
|
||||
syntax::SyntaxKind::BYTE => bytes.push(token.text().to_string()),
|
||||
syntax::SyntaxKind::BYTE_STRING => {
|
||||
let components = unquote_byte_string(lit).unwrap_or_else(Vec::new);
|
||||
components.into_iter().for_each(|x| bytes.push(x.to_string()));
|
||||
let lit = ast::make::literal(&lit.to_string());
|
||||
match lit.kind() {
|
||||
ast::LiteralKind::ByteString(s) => {
|
||||
s.value()
|
||||
.unwrap_or_default()
|
||||
.into_iter()
|
||||
.for_each(|x| bytes.push(x.to_string()));
|
||||
}
|
||||
ast::LiteralKind::Byte => {
|
||||
bytes.push(lit.to_string());
|
||||
}
|
||||
_ => {
|
||||
err.get_or_insert(mbe::ExpandError::UnexpectedToken.into());
|
||||
@ -481,10 +475,10 @@ fn concat_bytes_expand_subtree(
|
||||
for (ti, tt) in tree.token_trees.iter().enumerate() {
|
||||
match tt {
|
||||
tt::TokenTree::Leaf(tt::Leaf::Literal(lit)) => {
|
||||
let lit = ast::make::tokens::literal(&lit.to_string());
|
||||
let lit = ast::make::literal(&lit.to_string());
|
||||
match lit.kind() {
|
||||
syntax::SyntaxKind::BYTE | syntax::SyntaxKind::INT_NUMBER => {
|
||||
bytes.push(lit.text().to_string())
|
||||
ast::LiteralKind::IntNumber(_) | ast::LiteralKind::Byte => {
|
||||
bytes.push(lit.to_string());
|
||||
}
|
||||
_ => {
|
||||
return Err(mbe::ExpandError::UnexpectedToken.into());
|
||||
|
@ -29,7 +29,13 @@ pub(crate) fn literal(p: &mut Parser) -> Option<CompletedMarker> {
|
||||
return None;
|
||||
}
|
||||
let m = p.start();
|
||||
p.bump_any();
|
||||
if p.at(FLOAT_NUMBER) {
|
||||
let f = p.start();
|
||||
p.bump(FLOAT_NUMBER);
|
||||
f.complete(p, FLOAT_LITERAL);
|
||||
} else {
|
||||
p.bump_any();
|
||||
}
|
||||
Some(m.complete(p, LITERAL))
|
||||
}
|
||||
|
||||
|
File diff suppressed because one or more lines are too long
@ -57,7 +57,8 @@ SOURCE_FILE
|
||||
EQ "="
|
||||
WHITESPACE " "
|
||||
LITERAL
|
||||
FLOAT_NUMBER "2.0"
|
||||
FLOAT_LITERAL
|
||||
FLOAT_NUMBER "2.0"
|
||||
SEMICOLON ";"
|
||||
WHITESPACE "\n "
|
||||
LET_STMT
|
||||
|
@ -19,7 +19,8 @@ SOURCE_FILE
|
||||
CAST_EXPR
|
||||
METHOD_CALL_EXPR
|
||||
LITERAL
|
||||
FLOAT_NUMBER "1.0f32"
|
||||
FLOAT_LITERAL
|
||||
FLOAT_NUMBER "1.0f32"
|
||||
DOT "."
|
||||
NAME_REF
|
||||
IDENT "floor"
|
||||
@ -40,7 +41,8 @@ SOURCE_FILE
|
||||
CAST_EXPR
|
||||
METHOD_CALL_EXPR
|
||||
LITERAL
|
||||
FLOAT_NUMBER "1.0f32"
|
||||
FLOAT_LITERAL
|
||||
FLOAT_NUMBER "1.0f32"
|
||||
DOT "."
|
||||
NAME_REF
|
||||
IDENT "floor"
|
||||
|
@ -365,13 +365,16 @@ MacroExpr =
|
||||
|
||||
Literal =
|
||||
Attr* value:(
|
||||
'int_number' | 'float_number'
|
||||
'int_number' | FloatLiteral
|
||||
| 'string' | 'raw_string'
|
||||
| 'byte_string' | 'raw_byte_string'
|
||||
| 'true' | 'false'
|
||||
| 'char' | 'byte'
|
||||
)
|
||||
|
||||
FloatLiteral =
|
||||
'float_number'
|
||||
|
||||
PathExpr =
|
||||
Attr* Path
|
||||
|
||||
|
@ -282,7 +282,7 @@ pub enum LiteralKind {
|
||||
String(ast::String),
|
||||
ByteString(ast::ByteString),
|
||||
IntNumber(ast::IntNumber),
|
||||
FloatNumber(ast::FloatNumber),
|
||||
FloatNumber(ast::FloatLiteral),
|
||||
Char(ast::Char),
|
||||
Byte(ast::Byte),
|
||||
Bool(bool),
|
||||
@ -297,16 +297,17 @@ pub fn value(&self) -> SyntaxElement {
|
||||
}
|
||||
pub fn kind(&self) -> LiteralKind {
|
||||
let token = match self.value() {
|
||||
rowan::NodeOrToken::Node(_node) => unreachable!(),
|
||||
rowan::NodeOrToken::Node(node) => {
|
||||
return LiteralKind::FloatNumber(
|
||||
ast::FloatLiteral::cast(node).expect("unreachable"),
|
||||
);
|
||||
}
|
||||
rowan::NodeOrToken::Token(token) => token,
|
||||
};
|
||||
|
||||
if let Some(t) = ast::IntNumber::cast(token.clone()) {
|
||||
return LiteralKind::IntNumber(t);
|
||||
}
|
||||
if let Some(t) = ast::FloatNumber::cast(token.clone()) {
|
||||
return LiteralKind::FloatNumber(t);
|
||||
}
|
||||
if let Some(t) = ast::String::cast(token.clone()) {
|
||||
return LiteralKind::String(t);
|
||||
}
|
||||
@ -326,6 +327,26 @@ pub fn kind(&self) -> LiteralKind {
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_string(&self) -> Option<ast::String> {
|
||||
match self.kind() {
|
||||
LiteralKind::String(it) => Some(it),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_byte_string(&self) -> Option<ast::ByteString> {
|
||||
match self.kind() {
|
||||
LiteralKind::ByteString(it) => Some(it),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ast::FloatLiteral {
|
||||
pub fn suffix(&self) -> Option<String> {
|
||||
ast::FloatNumber::cast(self.syntax().last_token()?)?.suffix().map(|s| s.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
pub enum BlockModifier {
|
||||
|
@ -1085,6 +1085,16 @@ impl UnderscoreExpr {
|
||||
pub fn underscore_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![_]) }
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct FloatLiteral {
|
||||
pub(crate) syntax: SyntaxNode,
|
||||
}
|
||||
impl FloatLiteral {
|
||||
pub fn float_number_token(&self) -> Option<SyntaxToken> {
|
||||
support::token(&self.syntax, T![float_number])
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct StmtList {
|
||||
pub(crate) syntax: SyntaxNode,
|
||||
@ -2719,6 +2729,17 @@ fn cast(syntax: SyntaxNode) -> Option<Self> {
|
||||
}
|
||||
fn syntax(&self) -> &SyntaxNode { &self.syntax }
|
||||
}
|
||||
impl AstNode for FloatLiteral {
|
||||
fn can_cast(kind: SyntaxKind) -> bool { kind == FLOAT_LITERAL }
|
||||
fn cast(syntax: SyntaxNode) -> Option<Self> {
|
||||
if Self::can_cast(syntax.kind()) {
|
||||
Some(Self { syntax })
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
fn syntax(&self) -> &SyntaxNode { &self.syntax }
|
||||
}
|
||||
impl AstNode for StmtList {
|
||||
fn can_cast(kind: SyntaxKind) -> bool { kind == STMT_LIST }
|
||||
fn cast(syntax: SyntaxNode) -> Option<Self> {
|
||||
@ -4608,6 +4629,11 @@ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
std::fmt::Display::fmt(self.syntax(), f)
|
||||
}
|
||||
}
|
||||
impl std::fmt::Display for FloatLiteral {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
std::fmt::Display::fmt(self.syntax(), f)
|
||||
}
|
||||
}
|
||||
impl std::fmt::Display for StmtList {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
std::fmt::Display::fmt(self.syntax(), f)
|
||||
|
@ -799,6 +799,11 @@ pub fn struct_(
|
||||
))
|
||||
}
|
||||
|
||||
pub fn literal(text: &str) -> ast::Literal {
|
||||
assert_eq!(text.trim(), text);
|
||||
ast_from_text(&format!("fn f() {{ let _ = {}; }}", text))
|
||||
}
|
||||
|
||||
#[track_caller]
|
||||
fn ast_from_text<N: AstNode>(text: &str) -> N {
|
||||
let parse = SourceFile::parse(text);
|
||||
@ -827,7 +832,7 @@ pub fn token(kind: SyntaxKind) -> SyntaxToken {
|
||||
pub mod tokens {
|
||||
use once_cell::sync::Lazy;
|
||||
|
||||
use crate::{ast, AstNode, Parse, SourceFile, SyntaxKind::*, SyntaxToken};
|
||||
use crate::{AstNode, Parse, SourceFile, SyntaxKind::*, SyntaxToken};
|
||||
|
||||
pub(super) static SOURCE_FILE: Lazy<Parse<SourceFile>> = Lazy::new(|| {
|
||||
SourceFile::parse(
|
||||
@ -858,12 +863,6 @@ pub fn doc_comment(text: &str) -> SyntaxToken {
|
||||
sf.syntax().first_child_or_token().unwrap().into_token().unwrap()
|
||||
}
|
||||
|
||||
pub fn literal(text: &str) -> SyntaxToken {
|
||||
assert_eq!(text.trim(), text);
|
||||
let lit: ast::Literal = super::ast_from_text(&format!("fn f() {{ let _ = {}; }}", text));
|
||||
lit.syntax().first_child_or_token().unwrap().into_token().unwrap()
|
||||
}
|
||||
|
||||
pub fn single_newline() -> SyntaxToken {
|
||||
let res = SOURCE_FILE
|
||||
.tree()
|
||||
|
@ -355,14 +355,24 @@ const fn prefix_len(self) -> usize {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::ast::{self, make, FloatNumber, IntNumber};
|
||||
use crate::ast::{self, make};
|
||||
|
||||
fn check_float_suffix<'a>(lit: &str, expected: impl Into<Option<&'a str>>) {
|
||||
assert_eq!(FloatNumber { syntax: make::tokens::literal(lit) }.suffix(), expected.into());
|
||||
let suffix = match make::literal(lit).kind() {
|
||||
ast::LiteralKind::FloatNumber(f) => f.suffix(),
|
||||
// `1f32` lexes as an INT_NUMBER
|
||||
ast::LiteralKind::IntNumber(i) => i.suffix().map(|s| s.to_string()),
|
||||
e => unreachable!("{e:?}"),
|
||||
};
|
||||
assert_eq!(suffix.as_deref(), expected.into());
|
||||
}
|
||||
|
||||
fn check_int_suffix<'a>(lit: &str, expected: impl Into<Option<&'a str>>) {
|
||||
assert_eq!(IntNumber { syntax: make::tokens::literal(lit) }.suffix(), expected.into());
|
||||
let i = match make::literal(lit).kind() {
|
||||
ast::LiteralKind::IntNumber(i) => i,
|
||||
_ => unreachable!(),
|
||||
};
|
||||
assert_eq!(i.suffix(), expected.into());
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -390,12 +400,11 @@ fn test_int_number_suffix() {
|
||||
}
|
||||
|
||||
fn check_string_value<'a>(lit: &str, expected: impl Into<Option<&'a str>>) {
|
||||
assert_eq!(
|
||||
ast::String { syntax: make::tokens::literal(&format!("\"{}\"", lit)) }
|
||||
.value()
|
||||
.as_deref(),
|
||||
expected.into()
|
||||
);
|
||||
let s = match make::literal(&format!("\"{}\"", lit)).kind() {
|
||||
ast::LiteralKind::String(s) => s,
|
||||
_ => unreachable!(),
|
||||
};
|
||||
assert_eq!(s.value().as_deref(), expected.into());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -183,6 +183,7 @@ pub(crate) struct KindsSrc<'a> {
|
||||
"PATH",
|
||||
"PATH_SEGMENT",
|
||||
"LITERAL",
|
||||
"FLOAT_LITERAL",
|
||||
"RENAME",
|
||||
"VISIBILITY",
|
||||
"WHERE_CLAUSE",
|
||||
|
@ -462,6 +462,7 @@ macro_rules! T {
|
||||
[lifetime_ident] => { $crate::SyntaxKind::LIFETIME_IDENT };
|
||||
[ident] => { $crate::SyntaxKind::IDENT };
|
||||
[shebang] => { $crate::SyntaxKind::SHEBANG };
|
||||
[float_number] => { $crate::SyntaxKind::FLOAT_NUMBER };
|
||||
}
|
||||
pub use T;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user