]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/lib.rs
Rename `Item.node` to `Item.kind`
[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_root_url = "https://doc.rust-lang.org/nightly/",
8        test(attr(deny(warnings))))]
9
10 #![feature(box_syntax)]
11 #![feature(const_fn)]
12 #![feature(const_transmute)]
13 #![feature(crate_visibility_modifier)]
14 #![feature(label_break_value)]
15 #![feature(mem_take)]
16 #![feature(nll)]
17 #![feature(proc_macro_diagnostic)]
18 #![feature(proc_macro_internals)]
19 #![feature(proc_macro_span)]
20 #![feature(try_trait)]
21 #![feature(unicode_internals)]
22
23 #![recursion_limit="256"]
24
25 extern crate proc_macro;
26
27 pub use errors;
28 use rustc_data_structures::sync::Lock;
29 use rustc_data_structures::bit_set::GrowableBitSet;
30 pub use rustc_data_structures::thin_vec::ThinVec;
31 use ast::AttrId;
32 use syntax_pos::edition::Edition;
33
34 #[cfg(test)]
35 mod tests;
36
37 const MACRO_ARGUMENTS: Option<&'static str> = Some("macro arguments");
38
39 // A variant of 'try!' that panics on an Err. This is used as a crutch on the
40 // way towards a non-panic!-prone parser. It should be used for fatal parsing
41 // errors; eventually we plan to convert all code using panictry to just use
42 // normal try.
43 #[macro_export]
44 macro_rules! panictry {
45     ($e:expr) => ({
46         use std::result::Result::{Ok, Err};
47         use errors::FatalError;
48         match $e {
49             Ok(e) => e,
50             Err(mut e) => {
51                 e.emit();
52                 FatalError.raise()
53             }
54         }
55     })
56 }
57
58 // A variant of 'panictry!' that works on a Vec<Diagnostic> instead of a single DiagnosticBuilder.
59 macro_rules! panictry_buffer {
60     ($handler:expr, $e:expr) => ({
61         use std::result::Result::{Ok, Err};
62         use errors::FatalError;
63         match $e {
64             Ok(e) => e,
65             Err(errs) => {
66                 for e in errs {
67                     $handler.emit_diagnostic(&e);
68                 }
69                 FatalError.raise()
70             }
71         }
72     })
73 }
74
75 #[macro_export]
76 macro_rules! unwrap_or {
77     ($opt:expr, $default:expr) => {
78         match $opt {
79             Some(x) => x,
80             None => $default,
81         }
82     }
83 }
84
85 pub struct Globals {
86     used_attrs: Lock<GrowableBitSet<AttrId>>,
87     known_attrs: Lock<GrowableBitSet<AttrId>>,
88     syntax_pos_globals: syntax_pos::Globals,
89 }
90
91 impl Globals {
92     fn new(edition: Edition) -> Globals {
93         Globals {
94             // We have no idea how many attributes their will be, so just
95             // initiate the vectors with 0 bits. We'll grow them as necessary.
96             used_attrs: Lock::new(GrowableBitSet::new_empty()),
97             known_attrs: Lock::new(GrowableBitSet::new_empty()),
98             syntax_pos_globals: syntax_pos::Globals::new(edition),
99         }
100     }
101 }
102
103 pub fn with_globals<F, R>(edition: Edition, f: F) -> R
104     where F: FnOnce() -> R
105 {
106     let globals = Globals::new(edition);
107     GLOBALS.set(&globals, || {
108         syntax_pos::GLOBALS.set(&globals.syntax_pos_globals, f)
109     })
110 }
111
112 pub fn with_default_globals<F, R>(f: F) -> R
113     where F: FnOnce() -> R
114 {
115     with_globals(edition::DEFAULT_EDITION, f)
116 }
117
118 scoped_tls::scoped_thread_local!(pub static GLOBALS: Globals);
119
120 #[macro_use]
121 pub mod diagnostics {
122     #[macro_use]
123     pub mod macros;
124 }
125
126 pub mod error_codes;
127
128 pub mod util {
129     pub mod lev_distance;
130     pub mod node_count;
131     pub mod parser;
132     pub mod map_in_place;
133 }
134
135 pub mod json;
136
137 pub mod ast;
138 pub mod attr;
139 pub mod source_map;
140 #[macro_use]
141 pub mod config;
142 pub mod entry;
143 pub mod feature_gate;
144 pub mod mut_visit;
145 pub mod parse;
146 pub mod ptr;
147 pub mod show_span;
148 pub use syntax_pos::edition;
149 pub use syntax_pos::symbol;
150 pub mod tokenstream;
151 pub mod visit;
152
153 pub mod print {
154     pub mod pp;
155     pub mod pprust;
156     mod helpers;
157 }
158
159 pub mod ext {
160     mod placeholders;
161     mod proc_macro_server;
162
163     pub use syntax_pos::hygiene;
164     pub use mbe::macro_rules::compile_declarative_macro;
165     pub mod allocator;
166     pub mod base;
167     pub mod build;
168     pub mod expand;
169     pub mod proc_macro;
170
171     crate mod mbe;
172 }
173
174 pub mod early_buffered_lints;