]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/lib.rs
9077eca18215c0010d3e57a52570f97b3e44a1bd
[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(macro_at_most_once_rep)]
24 #![feature(nll)]
25 #![feature(rustc_attrs)]
26 #![feature(rustc_diagnostic_macros)]
27 #![feature(slice_sort_by_cached_key)]
28 #![feature(str_escape)]
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 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 #[macro_export]
74 macro_rules! unwrap_or {
75     ($opt:expr, $default:expr) => {
76         match $opt {
77             Some(x) => x,
78             None => $default,
79         }
80     }
81 }
82
83 pub struct Globals {
84     used_attrs: Lock<GrowableBitSet<AttrId>>,
85     known_attrs: Lock<GrowableBitSet<AttrId>>,
86     syntax_pos_globals: syntax_pos::Globals,
87 }
88
89 impl Globals {
90     fn new() -> Globals {
91         Globals {
92             // We have no idea how many attributes their will be, so just
93             // initiate the vectors with 0 bits. We'll grow them as necessary.
94             used_attrs: Lock::new(GrowableBitSet::new_empty()),
95             known_attrs: Lock::new(GrowableBitSet::new_empty()),
96             syntax_pos_globals: syntax_pos::Globals::new(),
97         }
98     }
99 }
100
101 pub fn with_globals<F, R>(f: F) -> R
102     where F: FnOnce() -> R
103 {
104     let globals = Globals::new();
105     GLOBALS.set(&globals, || {
106         syntax_pos::GLOBALS.set(&globals.syntax_pos_globals, f)
107     })
108 }
109
110 scoped_thread_local!(pub static GLOBALS: Globals);
111
112 #[macro_use]
113 pub mod diagnostics {
114     #[macro_use]
115     pub mod macros;
116     pub mod plugin;
117     pub mod metadata;
118 }
119
120 // NB: This module needs to be declared first so diagnostics are
121 // registered before they are used.
122 pub mod diagnostic_list;
123
124 pub mod util {
125     pub mod lev_distance;
126     pub mod node_count;
127     pub mod parser;
128     #[cfg(test)]
129     pub mod parser_testing;
130     pub mod move_map;
131
132     mod rc_slice;
133     pub use self::rc_slice::RcSlice;
134
135     mod rc_vec;
136     pub use self::rc_vec::RcVec;
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 mod str;
160 pub use syntax_pos::edition;
161 pub use syntax_pos::symbol;
162 pub mod test;
163 pub mod tokenstream;
164 pub mod visit;
165
166 pub mod print {
167     pub mod pp;
168     pub mod pprust;
169 }
170
171 pub mod ext {
172     pub use syntax_pos::hygiene;
173     pub mod base;
174     pub mod build;
175     pub mod derive;
176     pub mod expand;
177     pub mod placeholders;
178     pub mod quote;
179     pub mod source_util;
180
181     pub mod tt {
182         pub mod transcribe;
183         pub mod macro_parser;
184         pub mod macro_rules;
185         pub mod quoted;
186     }
187 }
188
189 pub mod early_buffered_lints;
190
191 #[cfg(test)]
192 mod test_snippet;
193
194 __build_diagnostic_array! { libsyntax, DIAGNOSTICS }