]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/lib.rs
3c66db082cc412d0b1a4aa62690aaf2b33c22a9c
[rust.git] / src / libsyntax / lib.rs
1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! The Rust parser and macro expander.
12 //!
13 //! # Note
14 //!
15 //! This API is completely unstable and subject to change.
16
17 #![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
18        html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
19        html_root_url = "https://doc.rust-lang.org/nightly/",
20        test(attr(deny(warnings))))]
21
22 #![feature(crate_visibility_modifier)]
23 #![feature(nll)]
24 #![feature(rustc_attrs)]
25 #![feature(rustc_diagnostic_macros)]
26 #![feature(slice_sort_by_cached_key)]
27 #![feature(str_escape)]
28 #![feature(step_trait)]
29 #![feature(try_trait)]
30 #![feature(unicode_internals)]
31
32 #![recursion_limit="256"]
33
34 #[macro_use] extern crate bitflags;
35 extern crate core;
36 extern crate serialize;
37 #[macro_use] extern crate log;
38 pub extern crate rustc_errors as errors;
39 extern crate syntax_pos;
40 #[macro_use] extern crate rustc_data_structures;
41 extern crate rustc_target;
42 #[macro_use] extern crate scoped_tls;
43 #[macro_use]
44 extern crate smallvec;
45
46 extern crate serialize as rustc_serialize; // used by deriving
47
48 use rustc_data_structures::sync::Lock;
49 use rustc_data_structures::bit_set::GrowableBitSet;
50 pub use rustc_data_structures::thin_vec::ThinVec;
51 use ast::AttrId;
52
53 // A variant of 'try!' that panics on an Err. This is used as a crutch on the
54 // way towards a non-panic!-prone parser. It should be used for fatal parsing
55 // errors; eventually we plan to convert all code using panictry to just use
56 // normal try.
57 // Exported for syntax_ext, not meant for general use.
58 #[macro_export]
59 macro_rules! panictry {
60     ($e:expr) => ({
61         use std::result::Result::{Ok, Err};
62         use errors::FatalError;
63         match $e {
64             Ok(e) => e,
65             Err(mut e) => {
66                 e.emit();
67                 FatalError.raise()
68             }
69         }
70     })
71 }
72
73 // A variant of 'panictry!' that works on a Vec<Diagnostic> instead of a single DiagnosticBuilder.
74 macro_rules! panictry_buffer {
75     ($handler:expr, $e:expr) => ({
76         use std::result::Result::{Ok, Err};
77         use errors::{FatalError, DiagnosticBuilder};
78         match $e {
79             Ok(e) => e,
80             Err(errs) => {
81                 for e in errs {
82                     DiagnosticBuilder::new_diagnostic($handler, e).emit();
83                 }
84                 FatalError.raise()
85             }
86         }
87     })
88 }
89
90 #[macro_export]
91 macro_rules! unwrap_or {
92     ($opt:expr, $default:expr) => {
93         match $opt {
94             Some(x) => x,
95             None => $default,
96         }
97     }
98 }
99
100 pub struct Globals {
101     used_attrs: Lock<GrowableBitSet<AttrId>>,
102     known_attrs: Lock<GrowableBitSet<AttrId>>,
103     syntax_pos_globals: syntax_pos::Globals,
104 }
105
106 impl Globals {
107     fn new() -> Globals {
108         Globals {
109             // We have no idea how many attributes their will be, so just
110             // initiate the vectors with 0 bits. We'll grow them as necessary.
111             used_attrs: Lock::new(GrowableBitSet::new_empty()),
112             known_attrs: Lock::new(GrowableBitSet::new_empty()),
113             syntax_pos_globals: syntax_pos::Globals::new(),
114         }
115     }
116 }
117
118 pub fn with_globals<F, R>(f: F) -> R
119     where F: FnOnce() -> R
120 {
121     let globals = Globals::new();
122     GLOBALS.set(&globals, || {
123         syntax_pos::GLOBALS.set(&globals.syntax_pos_globals, f)
124     })
125 }
126
127 scoped_thread_local!(pub static GLOBALS: Globals);
128
129 #[macro_use]
130 pub mod diagnostics {
131     #[macro_use]
132     pub mod macros;
133     pub mod plugin;
134     pub mod metadata;
135 }
136
137 // NB: This module needs to be declared first so diagnostics are
138 // registered before they are used.
139 pub mod diagnostic_list;
140
141 pub mod util {
142     pub mod lev_distance;
143     pub mod node_count;
144     pub mod parser;
145     #[cfg(test)]
146     pub mod parser_testing;
147     pub mod move_map;
148
149     mod rc_slice;
150     pub use self::rc_slice::RcSlice;
151
152     mod rc_vec;
153     pub use self::rc_vec::RcVec;
154 }
155
156 pub mod json;
157
158 pub mod syntax {
159     pub use ext;
160     pub use parse;
161     pub use ast;
162 }
163
164 pub mod ast;
165 pub mod attr;
166 pub mod source_map;
167 #[macro_use]
168 pub mod config;
169 pub mod entry;
170 pub mod feature_gate;
171 pub mod fold;
172 pub mod parse;
173 pub mod ptr;
174 pub mod show_span;
175 pub mod std_inject;
176 pub use syntax_pos::edition;
177 pub use syntax_pos::symbol;
178 pub mod test;
179 pub mod tokenstream;
180 pub mod visit;
181
182 pub mod print {
183     pub mod pp;
184     pub mod pprust;
185 }
186
187 pub mod ext {
188     pub use syntax_pos::hygiene;
189     pub mod base;
190     pub mod build;
191     pub mod derive;
192     pub mod expand;
193     pub mod placeholders;
194     pub mod quote;
195     pub mod source_util;
196
197     pub mod tt {
198         pub mod transcribe;
199         pub mod macro_parser;
200         pub mod macro_rules;
201         pub mod quoted;
202     }
203 }
204
205 pub mod early_buffered_lints;
206
207 #[cfg(test)]
208 mod test_snippet;
209
210 __build_diagnostic_array! { libsyntax, DIAGNOSTICS }