]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/lib.rs
16507ec5a5dd53856af13c0aac665948527ec2cb
[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(const_atomic_usize_new)]
23 #![feature(crate_visibility_modifier)]
24 #![feature(macro_at_most_once_rep)]
25 #![feature(rustc_attrs)]
26 #![feature(rustc_diagnostic_macros)]
27 #![feature(slice_sort_by_cached_key)]
28 #![feature(str_escape)]
29 #![feature(unicode_internals)]
30
31 #![recursion_limit="256"]
32
33 #[macro_use] extern crate bitflags;
34 extern crate core;
35 extern crate serialize;
36 #[macro_use] extern crate log;
37 pub extern crate rustc_errors as errors;
38 extern crate syntax_pos;
39 extern crate rustc_data_structures;
40 extern crate rustc_target;
41 #[macro_use] extern crate scoped_tls;
42
43 extern crate serialize as rustc_serialize; // used by deriving
44
45 use rustc_data_structures::sync::Lock;
46 use rustc_data_structures::bitvec::BitVector;
47 use ast::AttrId;
48
49 // A variant of 'try!' that panics on an Err. This is used as a crutch on the
50 // way towards a non-panic!-prone parser. It should be used for fatal parsing
51 // errors; eventually we plan to convert all code using panictry to just use
52 // normal try.
53 // Exported for syntax_ext, not meant for general use.
54 #[macro_export]
55 macro_rules! panictry {
56     ($e:expr) => ({
57         use std::result::Result::{Ok, Err};
58         use errors::FatalError;
59         match $e {
60             Ok(e) => e,
61             Err(mut e) => {
62                 e.emit();
63                 FatalError.raise()
64             }
65         }
66     })
67 }
68
69 #[macro_export]
70 macro_rules! unwrap_or {
71     ($opt:expr, $default:expr) => {
72         match $opt {
73             Some(x) => x,
74             None => $default,
75         }
76     }
77 }
78
79 pub struct Globals {
80     used_attrs: Lock<BitVector<AttrId>>,
81     known_attrs: Lock<BitVector<AttrId>>,
82     syntax_pos_globals: syntax_pos::Globals,
83 }
84
85 impl Globals {
86     fn new() -> Globals {
87         Globals {
88             // We have no idea how many attributes their will be, so just
89             // initiate the vectors with 0 bits. We'll grow them as necessary.
90             used_attrs: Lock::new(BitVector::new(0)),
91             known_attrs: Lock::new(BitVector::new(0)),
92             syntax_pos_globals: syntax_pos::Globals::new(),
93         }
94     }
95 }
96
97 pub fn with_globals<F, R>(f: F) -> R
98     where F: FnOnce() -> R
99 {
100     let globals = Globals::new();
101     GLOBALS.set(&globals, || {
102         syntax_pos::GLOBALS.set(&globals.syntax_pos_globals, f)
103     })
104 }
105
106 scoped_thread_local!(pub static GLOBALS: Globals);
107
108 #[macro_use]
109 pub mod diagnostics {
110     #[macro_use]
111     pub mod macros;
112     pub mod plugin;
113     pub mod metadata;
114 }
115
116 // NB: This module needs to be declared first so diagnostics are
117 // registered before they are used.
118 pub mod diagnostic_list;
119
120 pub mod util {
121     pub mod lev_distance;
122     pub mod node_count;
123     pub mod parser;
124     #[cfg(test)]
125     pub mod parser_testing;
126     pub mod small_vector;
127     pub mod move_map;
128
129     mod thin_vec;
130     pub use self::thin_vec::ThinVec;
131
132     mod rc_slice;
133     pub use self::rc_slice::RcSlice;
134 }
135
136 pub mod json;
137
138 pub mod syntax {
139     pub use ext;
140     pub use parse;
141     pub use ast;
142 }
143
144 pub mod ast;
145 pub mod attr;
146 pub mod codemap;
147 #[macro_use]
148 pub mod config;
149 pub mod entry;
150 pub mod feature_gate;
151 pub mod fold;
152 pub mod parse;
153 pub mod ptr;
154 pub mod show_span;
155 pub mod std_inject;
156 pub mod str;
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 quote;
176     pub mod source_util;
177
178     pub mod tt {
179         pub mod transcribe;
180         pub mod macro_parser;
181         pub mod macro_rules;
182         pub mod quoted;
183     }
184 }
185
186 pub mod early_buffered_lints;
187
188 #[cfg(test)]
189 mod test_snippet;
190
191 __build_diagnostic_array! { libsyntax, DIAGNOSTICS }