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