Skip to content

Commit 8fff47d

Browse files
committed
Don't use the word "parse" for lexing operations.
Lexing converts source text into a token stream. Parsing converts a token stream into AST fragments. This commit renames several lexing operations that have "parse" in the name. I think these names have been subtly confusing me for years. This is just a `s/parse/lex/` on function names, with one exception: `parse_stream_from_source_str` becomes `source_str_to_stream`, to make it consistent with the existing `source_file_to_stream`. The commit also moves that function's location in the file to be just above `source_file_to_stream`. The commit also cleans up a few comments along the way.
1 parent 990632f commit 8fff47d

File tree

5 files changed

+41
-49
lines changed

5 files changed

+41
-49
lines changed

compiler/rustc_expand/src/proc_macro_server.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_data_structures::fx::FxHashMap;
1313
use rustc_data_structures::sync::Lrc;
1414
use rustc_errors::{Diag, ErrorGuaranteed, MultiSpan, PResult};
1515
use rustc_parse::lexer::nfc_normalize;
16-
use rustc_parse::parse_stream_from_source_str;
16+
use rustc_parse::source_str_to_stream;
1717
use rustc_session::parse::ParseSess;
1818
use rustc_span::def_id::CrateNum;
1919
use rustc_span::symbol::{self, sym, Symbol};
@@ -539,7 +539,7 @@ impl server::TokenStream for Rustc<'_, '_> {
539539
}
540540

541541
fn from_str(&mut self, src: &str) -> Self::TokenStream {
542-
parse_stream_from_source_str(
542+
source_str_to_stream(
543543
FileName::proc_macro_source_code(src),
544544
src.to_string(),
545545
self.psess(),

compiler/rustc_parse/src/lexer/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub(crate) struct UnmatchedDelim {
4242
pub candidate_span: Option<Span>,
4343
}
4444

45-
pub(crate) fn parse_token_trees<'psess, 'src>(
45+
pub(crate) fn lex_token_trees<'psess, 'src>(
4646
psess: &'psess ParseSess,
4747
mut src: &'src str,
4848
mut start_pos: BytePos,
@@ -66,7 +66,7 @@ pub(crate) fn parse_token_trees<'psess, 'src>(
6666
last_lifetime: None,
6767
};
6868
let (stream, res, unmatched_delims) =
69-
tokentrees::TokenTreesReader::parse_all_token_trees(string_reader);
69+
tokentrees::TokenTreesReader::lex_all_token_trees(string_reader);
7070
match res {
7171
Ok(()) if unmatched_delims.is_empty() => Ok(stream),
7272
_ => {

compiler/rustc_parse/src/lexer/tokentrees.rs

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,21 @@ pub(super) struct TokenTreesReader<'psess, 'src> {
1717
}
1818

1919
impl<'psess, 'src> TokenTreesReader<'psess, 'src> {
20-
pub(super) fn parse_all_token_trees(
20+
pub(super) fn lex_all_token_trees(
2121
string_reader: StringReader<'psess, 'src>,
2222
) -> (TokenStream, Result<(), Vec<PErr<'psess>>>, Vec<UnmatchedDelim>) {
2323
let mut tt_reader = TokenTreesReader {
2424
string_reader,
2525
token: Token::dummy(),
2626
diag_info: TokenTreeDiagInfo::default(),
2727
};
28-
let (_open_spacing, stream, res) =
29-
tt_reader.parse_token_trees(/* is_delimited */ false);
28+
let (_open_spacing, stream, res) = tt_reader.lex_token_trees(/* is_delimited */ false);
3029
(stream, res, tt_reader.diag_info.unmatched_delims)
3130
}
3231

33-
// Parse a stream of tokens into a list of `TokenTree`s. The `Spacing` in
34-
// the result is that of the opening delimiter.
35-
fn parse_token_trees(
32+
// Lex into a token stream. The `Spacing` in the result is that of the
33+
// opening delimiter.
34+
fn lex_token_trees(
3635
&mut self,
3736
is_delimited: bool,
3837
) -> (Spacing, TokenStream, Result<(), Vec<PErr<'psess>>>) {
@@ -42,12 +41,10 @@ impl<'psess, 'src> TokenTreesReader<'psess, 'src> {
4241
let mut buf = Vec::new();
4342
loop {
4443
match self.token.kind {
45-
token::OpenDelim(delim) => {
46-
buf.push(match self.parse_token_tree_open_delim(delim) {
47-
Ok(val) => val,
48-
Err(errs) => return (open_spacing, TokenStream::new(buf), Err(errs)),
49-
})
50-
}
44+
token::OpenDelim(delim) => buf.push(match self.lex_token_tree_open_delim(delim) {
45+
Ok(val) => val,
46+
Err(errs) => return (open_spacing, TokenStream::new(buf), Err(errs)),
47+
}),
5148
token::CloseDelim(delim) => {
5249
return (
5350
open_spacing,
@@ -95,24 +92,24 @@ impl<'psess, 'src> TokenTreesReader<'psess, 'src> {
9592
err
9693
}
9794

98-
fn parse_token_tree_open_delim(
95+
fn lex_token_tree_open_delim(
9996
&mut self,
10097
open_delim: Delimiter,
10198
) -> Result<TokenTree, Vec<PErr<'psess>>> {
102-
// The span for beginning of the delimited section
99+
// The span for beginning of the delimited section.
103100
let pre_span = self.token.span;
104101

105102
self.diag_info.open_braces.push((open_delim, self.token.span));
106103

107-
// Parse the token trees within the delimiters.
104+
// Lex the token trees within the delimiters.
108105
// We stop at any delimiter so we can try to recover if the user
109106
// uses an incorrect delimiter.
110-
let (open_spacing, tts, res) = self.parse_token_trees(/* is_delimited */ true);
107+
let (open_spacing, tts, res) = self.lex_token_trees(/* is_delimited */ true);
111108
if let Err(errs) = res {
112109
return Err(self.unclosed_delim_err(tts, errs));
113110
}
114111

115-
// Expand to cover the entire delimited token tree
112+
// Expand to cover the entire delimited token tree.
116113
let delim_span = DelimSpan::from_pair(pre_span, self.token.span);
117114
let sm = self.string_reader.psess.source_map();
118115

@@ -150,7 +147,7 @@ impl<'psess, 'src> TokenTreesReader<'psess, 'src> {
150147
self.diag_info.last_unclosed_found_span = Some(self.token.span);
151148
// This is a conservative error: only report the last unclosed
152149
// delimiter. The previous unclosed delimiters could actually be
153-
// closed! The parser just hasn't gotten to them yet.
150+
// closed! The lexer just hasn't gotten to them yet.
154151
if let Some(&(_, sp)) = self.diag_info.open_braces.last() {
155152
unclosed_delimiter = Some(sp);
156153
};
@@ -236,9 +233,9 @@ impl<'psess, 'src> TokenTreesReader<'psess, 'src> {
236233
// out instead of complaining about the unclosed delims.
237234
let mut parser = crate::stream_to_parser(self.string_reader.psess, tts, None);
238235
let mut diff_errs = vec![];
239-
// Suggest removing a `{` we think appears in an `if`/`while` condition
240-
// We want to suggest removing a `{` only if we think we're in an `if`/`while` condition, but
241-
// we have no way of tracking this in the lexer itself, so we piggyback on the parser
236+
// Suggest removing a `{` we think appears in an `if`/`while` condition.
237+
// We want to suggest removing a `{` only if we think we're in an `if`/`while` condition,
238+
// but we have no way of tracking this in the lexer itself, so we piggyback on the parser.
242239
let mut in_cond = false;
243240
while parser.token != token::Eof {
244241
if let Err(diff_err) = parser.err_vcs_conflict_marker() {
@@ -249,14 +246,15 @@ impl<'psess, 'src> TokenTreesReader<'psess, 'src> {
249246
parser.token.kind,
250247
token::CloseDelim(Delimiter::Brace) | token::FatArrow
251248
) {
252-
// end of the `if`/`while` body, or the end of a `match` guard
249+
// End of the `if`/`while` body, or the end of a `match` guard.
253250
in_cond = false;
254251
} else if in_cond && parser.token == token::OpenDelim(Delimiter::Brace) {
255252
// Store the `&&` and `let` to use their spans later when creating the diagnostic
256253
let maybe_andand = parser.look_ahead(1, |t| t.clone());
257254
let maybe_let = parser.look_ahead(2, |t| t.clone());
258255
if maybe_andand == token::OpenDelim(Delimiter::Brace) {
259-
// This might be the beginning of the `if`/`while` body (i.e., the end of the condition)
256+
// This might be the beginning of the `if`/`while` body (i.e., the end of the
257+
// condition).
260258
in_cond = false;
261259
} else if maybe_andand == token::AndAnd && maybe_let.is_keyword(kw::Let) {
262260
let mut err = parser.dcx().struct_span_err(
@@ -288,8 +286,7 @@ impl<'psess, 'src> TokenTreesReader<'psess, 'src> {
288286
}
289287

290288
fn close_delim_err(&mut self, delim: Delimiter) -> PErr<'psess> {
291-
// An unexpected closing delimiter (i.e., there is no
292-
// matching opening delimiter).
289+
// An unexpected closing delimiter (i.e., there is no matching opening delimiter).
293290
let token_str = token_to_string(&self.token);
294291
let msg = format!("unexpected closing delimiter: `{token_str}`");
295292
let mut err = self.string_reader.psess.dcx.struct_span_err(self.token.span, msg);

compiler/rustc_parse/src/lib.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,6 @@ pub fn parse_crate_attrs_from_source_str(
8383
new_parser_from_source_str(psess, name, source).parse_inner_attributes()
8484
}
8585

86-
pub fn parse_stream_from_source_str(
87-
name: FileName,
88-
source: String,
89-
psess: &ParseSess,
90-
override_span: Option<Span>,
91-
) -> TokenStream {
92-
source_file_to_stream(psess, psess.source_map().new_source_file(name, source), override_span)
93-
}
94-
9586
/// Creates a new parser from a source string.
9687
pub fn new_parser_from_source_str(psess: &ParseSess, name: FileName, source: String) -> Parser<'_> {
9788
panictry_buffer!(maybe_new_parser_from_source_str(psess, name, source))
@@ -141,6 +132,15 @@ fn maybe_source_file_to_parser(
141132

142133
// Base abstractions
143134

135+
pub fn source_str_to_stream(
136+
name: FileName,
137+
source: String,
138+
psess: &ParseSess,
139+
override_span: Option<Span>,
140+
) -> TokenStream {
141+
source_file_to_stream(psess, psess.source_map().new_source_file(name, source), override_span)
142+
}
143+
144144
/// Given a `source_file`, produces a sequence of token trees.
145145
pub fn source_file_to_stream(
146146
psess: &ParseSess,
@@ -164,7 +164,7 @@ fn maybe_file_to_stream<'psess>(
164164
));
165165
});
166166

167-
lexer::parse_token_trees(psess, src.as_str(), source_file.start_pos, override_span)
167+
lexer::lex_token_trees(psess, src.as_str(), source_file.start_pos, override_span)
168168
}
169169

170170
/// Given a stream and the `ParseSess`, produces a parser.
@@ -194,13 +194,13 @@ pub fn parse_in<'a, T>(
194194
pub fn fake_token_stream_for_item(psess: &ParseSess, item: &ast::Item) -> TokenStream {
195195
let source = pprust::item_to_string(item);
196196
let filename = FileName::macro_expansion_source_code(&source);
197-
parse_stream_from_source_str(filename, source, psess, Some(item.span))
197+
source_str_to_stream(filename, source, psess, Some(item.span))
198198
}
199199

200200
pub fn fake_token_stream_for_crate(psess: &ParseSess, krate: &ast::Crate) -> TokenStream {
201201
let source = pprust::crate_to_string_for_macros(krate);
202202
let filename = FileName::macro_expansion_source_code(&source);
203-
parse_stream_from_source_str(filename, source, psess, Some(krate.spans.inner_span))
203+
source_str_to_stream(filename, source, psess, Some(krate.spans.inner_span))
204204
}
205205

206206
pub fn parse_cfg_attr(

src/librustdoc/passes/lint/check_code_block_syntax.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_errors::{
55
translation::{to_fluent_args, Translate},
66
Applicability, DiagCtxt, DiagInner, LazyFallbackBundle,
77
};
8-
use rustc_parse::parse_stream_from_source_str;
8+
use rustc_parse::source_str_to_stream;
99
use rustc_resolve::rustdoc::source_span_for_markdown_range;
1010
use rustc_session::parse::ParseSess;
1111
use rustc_span::hygiene::{AstPass, ExpnData, ExpnKind, LocalExpnId, Transparency};
@@ -53,13 +53,8 @@ fn check_rust_syntax(
5353
let span = DUMMY_SP.apply_mark(expn_id.to_expn_id(), Transparency::Transparent);
5454

5555
let is_empty = rustc_driver::catch_fatal_errors(|| {
56-
parse_stream_from_source_str(
57-
FileName::Custom(String::from("doctest")),
58-
source,
59-
&psess,
60-
Some(span),
61-
)
62-
.is_empty()
56+
source_str_to_stream(FileName::Custom(String::from("doctest")), source, &psess, Some(span))
57+
.is_empty()
6358
})
6459
.unwrap_or(false);
6560
let buffer = buffer.borrow();

0 commit comments

Comments
 (0)