]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/lib.rs
Auto merge of #61212 - alexcrichton:skip-rustc, r=pietroalbini
[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(crate_visibility_modifier)]
14 #![feature(label_break_value)]
15 #![feature(nll)]
16 #![feature(rustc_attrs)]
17 #![feature(rustc_diagnostic_macros)]
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 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_rules! panictry {
41     ($e:expr) => ({
42         use std::result::Result::{Ok, Err};
43         use errors::FatalError;
44         match $e {
45             Ok(e) => e,
46             Err(mut e) => {
47                 e.emit();
48                 FatalError.raise()
49             }
50         }
51     })
52 }
53
54 // A variant of 'panictry!' that works on a Vec<Diagnostic> instead of a single DiagnosticBuilder.
55 macro_rules! panictry_buffer {
56     ($handler:expr, $e:expr) => ({
57         use std::result::Result::{Ok, Err};
58         use errors::{FatalError, DiagnosticBuilder};
59         match $e {
60             Ok(e) => e,
61             Err(errs) => {
62                 for e in errs {
63                     DiagnosticBuilder::new_diagnostic($handler, e).emit();
64                 }
65                 FatalError.raise()
66             }
67         }
68     })
69 }
70
71 #[macro_export]
72 macro_rules! unwrap_or {
73     ($opt:expr, $default:expr) => {
74         match $opt {
75             Some(x) => x,
76             None => $default,
77         }
78     }
79 }
80
81 pub struct Globals {
82     used_attrs: Lock<GrowableBitSet<AttrId>>,
83     known_attrs: Lock<GrowableBitSet<AttrId>>,
84     syntax_pos_globals: syntax_pos::Globals,
85 }
86
87 impl Globals {
88     fn new(edition: Edition) -> Globals {
89         Globals {
90             // We have no idea how many attributes their will be, so just
91             // initiate the vectors with 0 bits. We'll grow them as necessary.
92             used_attrs: Lock::new(GrowableBitSet::new_empty()),
93             known_attrs: Lock::new(GrowableBitSet::new_empty()),
94             syntax_pos_globals: syntax_pos::Globals::new(edition),
95         }
96     }
97 }
98
99 pub fn with_globals<F, R>(edition: Edition, f: F) -> R
100     where F: FnOnce() -> R
101 {
102     let globals = Globals::new(edition);
103     GLOBALS.set(&globals, || {
104         syntax_pos::GLOBALS.set(&globals.syntax_pos_globals, f)
105     })
106 }
107
108 pub fn with_default_globals<F, R>(f: F) -> R
109     where F: FnOnce() -> R
110 {
111     with_globals(edition::DEFAULT_EDITION, f)
112 }
113
114 scoped_tls::scoped_thread_local!(pub static GLOBALS: Globals);
115
116 #[macro_use]
117 pub mod diagnostics {
118     #[macro_use]
119     pub mod macros;
120     pub mod plugin;
121     pub mod metadata;
122 }
123
124 // N.B., this module needs to be declared first so diagnostics are
125 // registered before they are used.
126 pub mod error_codes;
127
128 pub mod util {
129     pub mod lev_distance;
130     pub mod node_count;
131     pub mod parser;
132     #[cfg(test)]
133     pub mod parser_testing;
134     pub mod map_in_place;
135 }
136
137 pub mod json;
138
139 pub mod syntax {
140     pub use crate::ext;
141     pub use crate::parse;
142     pub use crate::ast;
143 }
144
145 pub mod ast;
146 pub mod attr;
147 pub mod source_map;
148 #[macro_use]
149 pub mod config;
150 pub mod entry;
151 pub mod feature_gate;
152 pub mod mut_visit;
153 pub mod parse;
154 pub mod ptr;
155 pub mod show_span;
156 pub mod std_inject;
157 pub use syntax_pos::edition;
158 pub use syntax_pos::symbol;
159 pub mod test;
160 pub mod tokenstream;
161 pub mod visit;
162
163 pub mod print {
164     pub mod pp;
165     pub mod pprust;
166 }
167
168 pub mod ext {
169     pub use syntax_pos::hygiene;
170     pub mod base;
171     pub mod build;
172     pub mod derive;
173     pub mod expand;
174     pub mod placeholders;
175     pub mod source_util;
176
177     pub mod tt {
178         pub mod transcribe;
179         pub mod macro_parser;
180         pub mod macro_rules;
181         pub mod quoted;
182     }
183 }
184
185 pub mod early_buffered_lints;
186
187 #[cfg(test)]
188 mod test_snippet;
189
190 __build_diagnostic_array! { libsyntax, DIAGNOSTICS }