]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/lib.rs
Some more cleanup in libsyntax::ext::tt::quoted
[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(internal)]
12
13 #![feature(bind_by_move_pattern_guards)]
14 #![feature(crate_visibility_modifier)]
15 #![feature(label_break_value)]
16 #![feature(nll)]
17 #![feature(rustc_attrs)]
18 #![feature(rustc_diagnostic_macros)]
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 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 use syntax_pos::edition::Edition;
34
35 const MACRO_ARGUMENTS: Option<&'static str> = Some("macro arguments");
36
37 // A variant of 'try!' that panics on an Err. This is used as a crutch on the
38 // way towards a non-panic!-prone parser. It should be used for fatal parsing
39 // errors; eventually we plan to convert all code using panictry to just use
40 // normal try.
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 }
162
163 pub mod ext {
164     pub use syntax_pos::hygiene;
165     pub mod base;
166     pub mod build;
167     pub mod derive;
168     pub mod expand;
169     pub mod placeholders;
170     pub mod source_util;
171
172     pub mod tt {
173         pub mod transcribe;
174         pub mod macro_parser;
175         pub mod macro_rules;
176         pub mod quoted;
177     }
178 }
179
180 pub mod early_buffered_lints;
181
182 #[cfg(test)]
183 mod test_snippet;
184
185 __build_diagnostic_array! { libsyntax, DIAGNOSTICS }