Skip to content

Commit 52d0e86

Browse files
committed
syntax: extract sess.rs for ParseSess
1 parent c29fe81 commit 52d0e86

File tree

3 files changed

+132
-119
lines changed

3 files changed

+132
-119
lines changed

src/libsyntax/parse/mod.rs

Lines changed: 7 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,19 @@
11
//! The main parser interface.
22
3-
use crate::ast::{self, CrateConfig, NodeId};
4-
use crate::early_buffered_lints::{BufferedEarlyLint, BufferedEarlyLintId};
5-
use crate::source_map::{SourceMap, FilePathMapping};
6-
use crate::feature_gate::UnstableFeatures;
3+
use crate::ast;
74
use crate::parse::parser::{Parser, emit_unclosed_delims};
85
use crate::parse::token::{Nonterminal, TokenKind};
96
use crate::tokenstream::{self, TokenStream, TokenTree};
107
use crate::print::pprust;
11-
use crate::symbol::Symbol;
128

13-
use errors::{Applicability, FatalError, Level, Handler, ColorConfig, Diagnostic, DiagnosticBuilder};
14-
use rustc_data_structures::fx::{FxHashSet, FxHashMap};
9+
use errors::{FatalError, Level, Diagnostic, DiagnosticBuilder};
1510
#[cfg(target_arch = "x86_64")]
1611
use rustc_data_structures::static_assert_size;
17-
use rustc_data_structures::sync::{Lrc, Lock, Once};
18-
use syntax_pos::{Span, SourceFile, FileName, MultiSpan};
19-
use syntax_pos::edition::Edition;
20-
use syntax_pos::hygiene::ExpnId;
12+
use rustc_data_structures::sync::Lrc;
13+
use syntax_pos::{Span, SourceFile, FileName};
2114

2215
use std::borrow::Cow;
23-
use std::path::{Path, PathBuf};
16+
use std::path::Path;
2417
use std::str;
2518

2619
use log::info;
@@ -33,6 +26,8 @@ pub mod parser;
3326
pub mod attr;
3427
pub mod lexer;
3528
pub mod token;
29+
mod sess;
30+
pub use sess::ParseSess;
3631

3732
crate mod classify;
3833
crate mod diagnostics;
@@ -46,112 +41,6 @@ pub type PResult<'a, T> = Result<T, DiagnosticBuilder<'a>>;
4641
#[cfg(target_arch = "x86_64")]
4742
static_assert_size!(PResult<'_, bool>, 16);
4843

49-
/// Collected spans during parsing for places where a certain feature was
50-
/// used and should be feature gated accordingly in `check_crate`.
51-
#[derive(Default)]
52-
pub struct GatedSpans {
53-
/// Spans collected for gating `let_chains`, e.g. `if a && let b = c {}`.
54-
pub let_chains: Lock<Vec<Span>>,
55-
/// Spans collected for gating `async_closure`, e.g. `async || ..`.
56-
pub async_closure: Lock<Vec<Span>>,
57-
/// Spans collected for gating `yield e?` expressions (`generators` gate).
58-
pub yields: Lock<Vec<Span>>,
59-
/// Spans collected for gating `or_patterns`, e.g. `Some(Foo | Bar)`.
60-
pub or_patterns: Lock<Vec<Span>>,
61-
/// Spans collected for gating `const_extern_fn`, e.g. `const extern fn foo`.
62-
pub const_extern_fn: Lock<Vec<Span>>,
63-
}
64-
65-
/// Info about a parsing session.
66-
pub struct ParseSess {
67-
pub span_diagnostic: Handler,
68-
pub unstable_features: UnstableFeatures,
69-
pub config: CrateConfig,
70-
pub edition: Edition,
71-
pub missing_fragment_specifiers: Lock<FxHashSet<Span>>,
72-
/// Places where raw identifiers were used. This is used for feature-gating raw identifiers.
73-
pub raw_identifier_spans: Lock<Vec<Span>>,
74-
/// Used to determine and report recursive module inclusions.
75-
included_mod_stack: Lock<Vec<PathBuf>>,
76-
source_map: Lrc<SourceMap>,
77-
pub buffered_lints: Lock<Vec<BufferedEarlyLint>>,
78-
/// Contains the spans of block expressions that could have been incomplete based on the
79-
/// operation token that followed it, but that the parser cannot identify without further
80-
/// analysis.
81-
pub ambiguous_block_expr_parse: Lock<FxHashMap<Span, Span>>,
82-
pub injected_crate_name: Once<Symbol>,
83-
pub gated_spans: GatedSpans,
84-
}
85-
86-
impl ParseSess {
87-
pub fn new(file_path_mapping: FilePathMapping) -> Self {
88-
let cm = Lrc::new(SourceMap::new(file_path_mapping));
89-
let handler = Handler::with_tty_emitter(
90-
ColorConfig::Auto,
91-
true,
92-
None,
93-
Some(cm.clone()),
94-
);
95-
ParseSess::with_span_handler(handler, cm)
96-
}
97-
98-
pub fn with_span_handler(handler: Handler, source_map: Lrc<SourceMap>) -> Self {
99-
Self {
100-
span_diagnostic: handler,
101-
unstable_features: UnstableFeatures::from_environment(),
102-
config: FxHashSet::default(),
103-
edition: ExpnId::root().expn_data().edition,
104-
missing_fragment_specifiers: Lock::new(FxHashSet::default()),
105-
raw_identifier_spans: Lock::new(Vec::new()),
106-
included_mod_stack: Lock::new(vec![]),
107-
source_map,
108-
buffered_lints: Lock::new(vec![]),
109-
ambiguous_block_expr_parse: Lock::new(FxHashMap::default()),
110-
injected_crate_name: Once::new(),
111-
gated_spans: GatedSpans::default(),
112-
}
113-
}
114-
115-
#[inline]
116-
pub fn source_map(&self) -> &SourceMap {
117-
&self.source_map
118-
}
119-
120-
pub fn buffer_lint<S: Into<MultiSpan>>(&self,
121-
lint_id: BufferedEarlyLintId,
122-
span: S,
123-
id: NodeId,
124-
msg: &str,
125-
) {
126-
self.buffered_lints.with_lock(|buffered_lints| {
127-
buffered_lints.push(BufferedEarlyLint{
128-
span: span.into(),
129-
id,
130-
msg: msg.into(),
131-
lint_id,
132-
});
133-
});
134-
}
135-
136-
/// Extend an error with a suggestion to wrap an expression with parentheses to allow the
137-
/// parser to continue parsing the following operation as part of the same expression.
138-
pub fn expr_parentheses_needed(
139-
&self,
140-
err: &mut DiagnosticBuilder<'_>,
141-
span: Span,
142-
alt_snippet: Option<String>,
143-
) {
144-
if let Some(snippet) = self.source_map().span_to_snippet(span).ok().or(alt_snippet) {
145-
err.span_suggestion(
146-
span,
147-
"parentheses are required to parse this as an expression",
148-
format!("({})", snippet),
149-
Applicability::MachineApplicable,
150-
);
151-
}
152-
}
153-
}
154-
15544
#[derive(Clone)]
15645
pub struct Directory<'a> {
15746
pub path: Cow<'a, Path>,

src/libsyntax/parse/parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1366,7 +1366,7 @@ impl<'a> Parser<'a> {
13661366
],
13671367
Applicability::MaybeIncorrect,
13681368
).span_suggestion(
1369-
self.sess.source_map.next_point(self.prev_span),
1369+
self.sess.source_map().next_point(self.prev_span),
13701370
"add a semicolon",
13711371
';'.to_string(),
13721372
Applicability::MaybeIncorrect,

src/libsyntax/parse/sess.rs

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
//! Contains `ParseSess` which holds state living beyond what one `Parser` might.
2+
//! It also serves as an input to the parser itself.
3+
4+
use crate::ast::{CrateConfig, NodeId};
5+
use crate::early_buffered_lints::{BufferedEarlyLint, BufferedEarlyLintId};
6+
use crate::source_map::{SourceMap, FilePathMapping};
7+
use crate::feature_gate::UnstableFeatures;
8+
9+
use errors::{Applicability, Handler, ColorConfig, DiagnosticBuilder};
10+
use rustc_data_structures::fx::{FxHashSet, FxHashMap};
11+
use rustc_data_structures::sync::{Lrc, Lock, Once};
12+
use syntax_pos::{Symbol, Span, MultiSpan};
13+
use syntax_pos::edition::Edition;
14+
use syntax_pos::hygiene::ExpnId;
15+
16+
use std::path::PathBuf;
17+
use std::str;
18+
19+
/// Collected spans during parsing for places where a certain feature was
20+
/// used and should be feature gated accordingly in `check_crate`.
21+
#[derive(Default)]
22+
crate struct GatedSpans {
23+
/// Spans collected for gating `let_chains`, e.g. `if a && let b = c {}`.
24+
crate let_chains: Lock<Vec<Span>>,
25+
/// Spans collected for gating `async_closure`, e.g. `async || ..`.
26+
crate async_closure: Lock<Vec<Span>>,
27+
/// Spans collected for gating `yield e?` expressions (`generators` gate).
28+
crate yields: Lock<Vec<Span>>,
29+
/// Spans collected for gating `or_patterns`, e.g. `Some(Foo | Bar)`.
30+
crate or_patterns: Lock<Vec<Span>>,
31+
/// Spans collected for gating `const_extern_fn`, e.g. `const extern fn foo`.
32+
crate const_extern_fn: Lock<Vec<Span>>,
33+
}
34+
35+
/// Info about a parsing session.
36+
pub struct ParseSess {
37+
pub span_diagnostic: Handler,
38+
crate unstable_features: UnstableFeatures,
39+
pub config: CrateConfig,
40+
pub edition: Edition,
41+
pub missing_fragment_specifiers: Lock<FxHashSet<Span>>,
42+
/// Places where raw identifiers were used. This is used for feature-gating raw identifiers.
43+
pub raw_identifier_spans: Lock<Vec<Span>>,
44+
/// Used to determine and report recursive module inclusions.
45+
pub(super) included_mod_stack: Lock<Vec<PathBuf>>,
46+
source_map: Lrc<SourceMap>,
47+
pub buffered_lints: Lock<Vec<BufferedEarlyLint>>,
48+
/// Contains the spans of block expressions that could have been incomplete based on the
49+
/// operation token that followed it, but that the parser cannot identify without further
50+
/// analysis.
51+
pub ambiguous_block_expr_parse: Lock<FxHashMap<Span, Span>>,
52+
pub injected_crate_name: Once<Symbol>,
53+
crate gated_spans: GatedSpans,
54+
}
55+
56+
impl ParseSess {
57+
pub fn new(file_path_mapping: FilePathMapping) -> Self {
58+
let cm = Lrc::new(SourceMap::new(file_path_mapping));
59+
let handler = Handler::with_tty_emitter(
60+
ColorConfig::Auto,
61+
true,
62+
None,
63+
Some(cm.clone()),
64+
);
65+
ParseSess::with_span_handler(handler, cm)
66+
}
67+
68+
pub fn with_span_handler(handler: Handler, source_map: Lrc<SourceMap>) -> Self {
69+
Self {
70+
span_diagnostic: handler,
71+
unstable_features: UnstableFeatures::from_environment(),
72+
config: FxHashSet::default(),
73+
edition: ExpnId::root().expn_data().edition,
74+
missing_fragment_specifiers: Lock::new(FxHashSet::default()),
75+
raw_identifier_spans: Lock::new(Vec::new()),
76+
included_mod_stack: Lock::new(vec![]),
77+
source_map,
78+
buffered_lints: Lock::new(vec![]),
79+
ambiguous_block_expr_parse: Lock::new(FxHashMap::default()),
80+
injected_crate_name: Once::new(),
81+
gated_spans: GatedSpans::default(),
82+
}
83+
}
84+
85+
#[inline]
86+
pub fn source_map(&self) -> &SourceMap {
87+
&self.source_map
88+
}
89+
90+
pub fn buffer_lint(
91+
&self,
92+
lint_id: BufferedEarlyLintId,
93+
span: impl Into<MultiSpan>,
94+
id: NodeId,
95+
msg: &str,
96+
) {
97+
self.buffered_lints.with_lock(|buffered_lints| {
98+
buffered_lints.push(BufferedEarlyLint{
99+
span: span.into(),
100+
id,
101+
msg: msg.into(),
102+
lint_id,
103+
});
104+
});
105+
}
106+
107+
/// Extend an error with a suggestion to wrap an expression with parentheses to allow the
108+
/// parser to continue parsing the following operation as part of the same expression.
109+
pub fn expr_parentheses_needed(
110+
&self,
111+
err: &mut DiagnosticBuilder<'_>,
112+
span: Span,
113+
alt_snippet: Option<String>,
114+
) {
115+
if let Some(snippet) = self.source_map().span_to_snippet(span).ok().or(alt_snippet) {
116+
err.span_suggestion(
117+
span,
118+
"parentheses are required to parse this as an expression",
119+
format!("({})", snippet),
120+
Applicability::MachineApplicable,
121+
);
122+
}
123+
}
124+
}

0 commit comments

Comments
 (0)