]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/lib.rs
60de94821bba0ba7b87a9a634ab74c36a4d5fa22
[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
47 // A variant of 'try!' that panics on an Err. This is used as a crutch on the
48 // way towards a non-panic!-prone parser. It should be used for fatal parsing
49 // errors; eventually we plan to convert all code using panictry to just use
50 // normal try.
51 // Exported for syntax_ext, not meant for general use.
52 #[macro_export]
53 macro_rules! panictry {
54     ($e:expr) => ({
55         use std::result::Result::{Ok, Err};
56         use errors::FatalError;
57         match $e {
58             Ok(e) => e,
59             Err(mut e) => {
60                 e.emit();
61                 FatalError.raise()
62             }
63         }
64     })
65 }
66
67 #[macro_export]
68 macro_rules! unwrap_or {
69     ($opt:expr, $default:expr) => {
70         match $opt {
71             Some(x) => x,
72             None => $default,
73         }
74     }
75 }
76
77 pub struct Globals {
78     used_attrs: Lock<Vec<u64>>,
79     known_attrs: Lock<Vec<u64>>,
80     syntax_pos_globals: syntax_pos::Globals,
81 }
82
83 impl Globals {
84     fn new() -> Globals {
85         Globals {
86             used_attrs: Lock::new(Vec::new()),
87             known_attrs: Lock::new(Vec::new()),
88             syntax_pos_globals: syntax_pos::Globals::new(),
89         }
90     }
91 }
92
93 pub fn with_globals<F, R>(f: F) -> R
94     where F: FnOnce() -> R
95 {
96     let globals = Globals::new();
97     GLOBALS.set(&globals, || {
98         syntax_pos::GLOBALS.set(&globals.syntax_pos_globals, f)
99     })
100 }
101
102 scoped_thread_local!(pub static GLOBALS: Globals);
103
104 #[macro_use]
105 pub mod diagnostics {
106     #[macro_use]
107     pub mod macros;
108     pub mod plugin;
109     pub mod metadata;
110 }
111
112 // NB: This module needs to be declared first so diagnostics are
113 // registered before they are used.
114 pub mod diagnostic_list;
115
116 pub mod util {
117     pub mod lev_distance;
118     pub mod node_count;
119     pub mod parser;
120     #[cfg(test)]
121     pub mod parser_testing;
122     pub mod small_vector;
123     pub mod move_map;
124
125     mod thin_vec;
126     pub use self::thin_vec::ThinVec;
127
128     mod rc_slice;
129     pub use self::rc_slice::RcSlice;
130 }
131
132 pub mod json;
133
134 pub mod syntax {
135     pub use ext;
136     pub use parse;
137     pub use ast;
138 }
139
140 pub mod ast;
141 pub mod attr;
142 pub mod codemap;
143 #[macro_use]
144 pub mod config;
145 pub mod entry;
146 pub mod feature_gate;
147 pub mod fold;
148 pub mod parse;
149 pub mod ptr;
150 pub mod show_span;
151 pub mod std_inject;
152 pub mod str;
153 pub use syntax_pos::edition;
154 pub use syntax_pos::symbol;
155 pub mod test;
156 pub mod tokenstream;
157 pub mod visit;
158
159 pub mod print {
160     pub mod pp;
161     pub mod pprust;
162 }
163
164 pub mod ext {
165     pub use syntax_pos::hygiene;
166     pub mod base;
167     pub mod build;
168     pub mod derive;
169     pub mod expand;
170     pub mod placeholders;
171     pub mod quote;
172     pub mod source_util;
173
174     pub mod tt {
175         pub mod transcribe;
176         pub mod macro_parser;
177         pub mod macro_rules;
178         pub mod quoted;
179     }
180 }
181
182 pub mod early_buffered_lints;
183
184 #[cfg(test)]
185 mod test_snippet;
186
187 __build_diagnostic_array! { libsyntax, DIAGNOSTICS }