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