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