Skip to content

Commit 9e7eaa9

Browse files
author
Veetaha
committed
ra_syntax: refactored the lexer design as per @matklad and @kiljacken PR review
1 parent bf60661 commit 9e7eaa9

File tree

9 files changed

+199
-178
lines changed

9 files changed

+199
-178
lines changed

crates/ra_ide/src/references/rename.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
33
use hir::ModuleSource;
44
use ra_db::{RelativePath, RelativePathBuf, SourceDatabase, SourceDatabaseExt};
5-
use ra_syntax::{algo::find_node_at_offset, ast, single_token, AstNode, SyntaxKind, SyntaxNode};
5+
use ra_syntax::{
6+
algo::find_node_at_offset, ast, lex_single_valid_syntax_kind, AstNode, SyntaxKind, SyntaxNode,
7+
};
68
use ra_text_edit::TextEdit;
79

810
use crate::{
@@ -17,7 +19,7 @@ pub(crate) fn rename(
1719
position: FilePosition,
1820
new_name: &str,
1921
) -> Option<RangeInfo<SourceChange>> {
20-
match single_token(new_name)?.token.kind {
22+
match lex_single_valid_syntax_kind(new_name)? {
2123
SyntaxKind::IDENT | SyntaxKind::UNDERSCORE => (),
2224
_ => return None,
2325
}

crates/ra_mbe/src/subtree_source.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! FIXME: write short doc here
22
33
use ra_parser::{Token, TokenSource};
4-
use ra_syntax::{single_token, SmolStr, SyntaxKind, SyntaxKind::*, T};
4+
use ra_syntax::{lex_single_valid_syntax_kind, SmolStr, SyntaxKind, SyntaxKind::*, T};
55
use std::cell::{Cell, Ref, RefCell};
66
use tt::buffer::{Cursor, TokenBuffer};
77

@@ -129,8 +129,7 @@ fn convert_delim(d: Option<tt::DelimiterKind>, closing: bool) -> TtToken {
129129
}
130130

131131
fn convert_literal(l: &tt::Literal) -> TtToken {
132-
let kind = single_token(&l.text)
133-
.map(|parsed| parsed.token.kind)
132+
let kind = lex_single_valid_syntax_kind(&l.text)
134133
.filter(|kind| kind.is_literal())
135134
.unwrap_or_else(|| match l.text.as_ref() {
136135
"true" => T![true],

crates/ra_syntax/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ use crate::syntax_node::GreenNode;
4141
pub use crate::{
4242
algo::InsertPosition,
4343
ast::{AstNode, AstToken},
44-
parsing::{first_token, single_token, tokenize, tokenize_append, Token, TokenizeError},
44+
parsing::{
45+
lex_single_syntax_kind, lex_single_valid_syntax_kind, tokenize, Token, TokenizeError,
46+
},
4547
ptr::{AstPtr, SyntaxNodePtr},
4648
syntax_error::{Location, SyntaxError, SyntaxErrorKind},
4749
syntax_node::{

crates/ra_syntax/src/parsing.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@ pub use lexer::*;
1515
pub(crate) use self::reparsing::incremental_reparse;
1616

1717
pub(crate) fn parse_text(text: &str) -> (GreenNode, Vec<SyntaxError>) {
18-
let ParsedTokens { tokens, errors } = tokenize(&text);
18+
let (tokens, lexer_errors) = tokenize(&text);
19+
1920
let mut token_source = TextTokenSource::new(text, &tokens);
20-
let mut tree_sink = TextTreeSink::new(text, &tokens, errors);
21+
let mut tree_sink = TextTreeSink::new(text, &tokens);
22+
2123
ra_parser::parse(&mut token_source, &mut tree_sink);
22-
tree_sink.finish()
24+
25+
let (tree, mut parser_errors) = tree_sink.finish();
26+
parser_errors.extend(lexer_errors);
27+
28+
(tree, parser_errors)
2329
}

0 commit comments

Comments
 (0)