Skip to content

Commit 1ea0238

Browse files
committed
Add classify_literal and undo expose next_token
1 parent 1ab78d6 commit 1ea0238

File tree

5 files changed

+15
-8
lines changed

5 files changed

+15
-8
lines changed

crates/ra_mbe/src/lib.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -343,16 +343,14 @@ SOURCE_FILE@[0; 40)
343343
if let tt::TokenTree::Subtree(subtree) = tt {
344344
return &subtree;
345345
}
346-
assert!(false, "It is not a subtree");
347-
unreachable!();
346+
unreachable!("It is not a subtree");
348347
}
349348

350349
fn to_literal(tt: &tt::TokenTree) -> &tt::Literal {
351350
if let tt::TokenTree::Leaf(tt::Leaf::Literal(lit)) = tt {
352351
return lit;
353352
}
354-
assert!(false, "It is not a literal");
355-
unreachable!();
353+
unreachable!("It is not a literal");
356354
}
357355

358356
let rules = create_rules(

crates/ra_mbe/src/syntax_bridge.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use ra_parser::{TokenSource, TreeSink, ParseError};
22
use ra_syntax::{
33
AstNode, SyntaxNode, TextRange, SyntaxKind, SmolStr, SyntaxTreeBuilder, TreeArc, SyntaxElement,
4-
ast, SyntaxKind::*, TextUnit, next_token
4+
ast, SyntaxKind::*, TextUnit, classify_literal
55
};
66

77
/// Maps `tt::TokenId` to the relative range of the original token.
@@ -189,7 +189,7 @@ impl TtTokenSource {
189189
{
190190
let tok = match token {
191191
tt::Leaf::Literal(l) => TtToken {
192-
kind: next_token(&l.text).kind,
192+
kind: classify_literal(&l.text).unwrap().kind,
193193
is_joint_to_next: false,
194194
text: l.text.clone(),
195195
},

crates/ra_syntax/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub use crate::{
4040
syntax_text::SyntaxText,
4141
syntax_node::{Direction, SyntaxNode, WalkEvent, TreeArc, SyntaxTreeBuilder, SyntaxElement, SyntaxToken},
4242
ptr::{SyntaxNodePtr, AstPtr},
43-
parsing::{tokenize, next_token, Token},
43+
parsing::{tokenize, classify_literal, Token},
4444
};
4545

4646
use ra_text_edit::AtomTextEdit;

crates/ra_syntax/src/parsing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{
1111
syntax_node::GreenNode,
1212
};
1313

14-
pub use self::lexer::{tokenize, next_token, Token};
14+
pub use self::lexer::{tokenize, classify_literal, Token};
1515

1616
pub(crate) use self::reparsing::incremental_reparse;
1717

crates/ra_syntax/src/parsing/lexer.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,12 @@ fn scan_literal_suffix(ptr: &mut Ptr) {
214214
}
215215
ptr.bump_while(is_ident_continue);
216216
}
217+
218+
pub fn classify_literal(text: &str) -> Option<Token> {
219+
let tkn = next_token(text);
220+
if tkn.kind.is_literal() || tkn.len.to_usize() != text.len() {
221+
return None;
222+
}
223+
224+
Some(tkn)
225+
}

0 commit comments

Comments
 (0)