]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/lib.rs
Rollup merge of #58273 - taiki-e:rename-dependency, r=matthewjasper
[rust.git] / src / libsyntax / lib.rs
1 //! The Rust parser and macro expander.
2 //!
3 //! # Note
4 //!
5 //! This API is completely unstable and subject to change.
6
7 #![doc(html_root_url = "https://doc.rust-lang.org/nightly/",
8        test(attr(deny(warnings))))]
9
10 #![deny(rust_2018_idioms)]
11
12 #![feature(crate_visibility_modifier)]
13 #![feature(label_break_value)]
14 #![feature(nll)]
15 #![feature(rustc_attrs)]
16 #![feature(rustc_diagnostic_macros)]
17 #![feature(slice_sort_by_cached_key)]
18 #![feature(step_trait)]
19 #![feature(try_trait)]
20 #![feature(unicode_internals)]
21
22 #![recursion_limit="256"]
23
24 #[allow(unused_extern_crates)]
25 extern crate serialize as rustc_serialize; // used by deriving
26
27 pub use errors;
28 use rustc_data_structures::sync::Lock;
29 use rustc_data_structures::bit_set::GrowableBitSet;
30 pub use rustc_data_structures::thin_vec::ThinVec;
31 use ast::AttrId;
32
33 // A variant of 'try!' that panics on an Err. This is used as a crutch on the
34 // way towards a non-panic!-prone parser. It should be used for fatal parsing
35 // errors; eventually we plan to convert all code using panictry to just use
36 // normal try.
37 macro_rules! panictry {
38     ($e:expr) => ({
39         use std::result::Result::{Ok, Err};
40         use errors::FatalError;
41         match $e {
42             Ok(e) => e,
43             Err(mut e) => {
44                 e.emit();
45                 FatalError.raise()
46             }
47         }
48     })
49 }
50
51 // A variant of 'panictry!' that works on a Vec<Diagnostic> instead of a single DiagnosticBuilder.
52 macro_rules! panictry_buffer {
53     ($handler:expr, $e:expr) => ({
54         use std::result::Result::{Ok, Err};
55         use errors::{FatalError, DiagnosticBuilder};
56         match $e {
57             Ok(e) => e,
58             Err(errs) => {
59                 for e in errs {
60                     DiagnosticBuilder::new_diagnostic($handler, e).emit();
61                 }
62                 FatalError.raise()
63             }
64         }
65     })
66 }
67
68 #[macro_export]
69 macro_rules! unwrap_or {
70     ($opt:expr, $default:expr) => {
71         match $opt {
72             Some(x) => x,
73             None => $default,
74         }
75     }
76 }
77
78 pub struct Globals {
79     used_attrs: Lock<GrowableBitSet<AttrId>>,
80     known_attrs: Lock<GrowableBitSet<AttrId>>,
81     syntax_pos_globals: syntax_pos::Globals,
82 }
83
84 impl Globals {
85     fn new() -> Globals {
86         Globals {
87             // We have no idea how many attributes their will be, so just
88             // initiate the vectors with 0 bits. We'll grow them as necessary.
89             used_attrs: Lock::new(GrowableBitSet::new_empty()),
90             known_attrs: Lock::new(GrowableBitSet::new_empty()),
91             syntax_pos_globals: syntax_pos::Globals::new(),
92         }
93     }
94 }
95
96 pub fn with_globals<F, R>(f: F) -> R
97     where F: FnOnce() -> R
98 {
99     let globals = Globals::new();
100     GLOBALS.set(&globals, || {
101         syntax_pos::GLOBALS.set(&globals.syntax_pos_globals, f)
102     })
103 }
104
105 scoped_tls::scoped_thread_local!(pub static GLOBALS: Globals);
106
107 #[macro_use]
108 pub mod diagnostics {
109     #[macro_use]
110     pub mod macros;
111     pub mod plugin;
112     pub mod metadata;
113 }
114
115 // N.B., this module needs to be declared first so diagnostics are
116 // registered before they are used.
117 pub mod diagnostic_list;
118
119 pub mod util {
120     pub mod lev_distance;
121     pub mod node_count;
122     pub mod parser;
123     #[cfg(test)]
124     pub mod parser_testing;
125     pub mod map_in_place;
126 }
127
128 pub mod json;
129
130 pub mod syntax {
131     pub use crate::ext;
132     pub use crate::parse;
133     pub use crate::ast;
134 }
135
136 pub mod ast;
137 pub mod attr;
138 pub mod source_map;
139 #[macro_use]
140 pub mod config;
141 pub mod entry;
142 pub mod feature_gate;
143 pub mod mut_visit;
144 pub mod parse;
145 pub mod ptr;
146 pub mod show_span;
147 pub mod std_inject;
148 pub use syntax_pos::edition;
149 pub use syntax_pos::symbol;
150 pub mod test;
151 pub mod tokenstream;
152 pub mod visit;
153
154 pub mod print {
155     pub mod pp;
156     pub mod pprust;
157 }
158
159 pub mod ext {
160     pub use syntax_pos::hygiene;
161     pub mod base;
162     pub mod build;
163     pub mod derive;
164     pub mod expand;
165     pub mod placeholders;
166     pub mod source_util;
167
168     pub mod tt {
169         pub mod transcribe;
170         pub mod macro_parser;
171         pub mod macro_rules;
172         pub mod quoted;
173     }
174 }
175
176 pub mod early_buffered_lints;
177
178 #[cfg(test)]
179 mod test_snippet;
180
181 __build_diagnostic_array! { libsyntax, DIAGNOSTICS }