]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/lib.rs
libsyntax: factor out file path resolving
[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 #![deny(rust_2018_idioms)]
11 #![deny(unused_lifetimes)]
12
13 #![feature(bind_by_move_pattern_guards)]
14 #![feature(box_syntax)]
15 #![feature(const_fn)]
16 #![feature(const_transmute)]
17 #![feature(crate_visibility_modifier)]
18 #![feature(label_break_value)]
19 #![feature(mem_take)]
20 #![feature(nll)]
21 #![feature(rustc_diagnostic_macros)]
22 #![feature(try_trait)]
23 #![feature(unicode_internals)]
24
25 #![recursion_limit="256"]
26
27 #[allow(unused_extern_crates)]
28 extern crate serialize as rustc_serialize; // used by deriving
29
30 pub use errors;
31 use rustc_data_structures::sync::Lock;
32 use rustc_data_structures::bit_set::GrowableBitSet;
33 pub use rustc_data_structures::thin_vec::ThinVec;
34 use ast::AttrId;
35 use syntax_pos::edition::Edition;
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_rules! panictry {
44     ($e:expr) => ({
45         use std::result::Result::{Ok, Err};
46         use errors::FatalError;
47         match $e {
48             Ok(e) => e,
49             Err(mut e) => {
50                 e.emit();
51                 FatalError.raise()
52             }
53         }
54     })
55 }
56
57 // A variant of 'panictry!' that works on a Vec<Diagnostic> instead of a single DiagnosticBuilder.
58 macro_rules! panictry_buffer {
59     ($handler:expr, $e:expr) => ({
60         use std::result::Result::{Ok, Err};
61         use errors::{FatalError, DiagnosticBuilder};
62         match $e {
63             Ok(e) => e,
64             Err(errs) => {
65                 for e in errs {
66                     DiagnosticBuilder::new_diagnostic($handler, e).emit();
67                 }
68                 FatalError.raise()
69             }
70         }
71     })
72 }
73
74 #[macro_export]
75 macro_rules! unwrap_or {
76     ($opt:expr, $default:expr) => {
77         match $opt {
78             Some(x) => x,
79             None => $default,
80         }
81     }
82 }
83
84 pub struct Globals {
85     used_attrs: Lock<GrowableBitSet<AttrId>>,
86     known_attrs: Lock<GrowableBitSet<AttrId>>,
87     syntax_pos_globals: syntax_pos::Globals,
88 }
89
90 impl Globals {
91     fn new(edition: Edition) -> Globals {
92         Globals {
93             // We have no idea how many attributes their will be, so just
94             // initiate the vectors with 0 bits. We'll grow them as necessary.
95             used_attrs: Lock::new(GrowableBitSet::new_empty()),
96             known_attrs: Lock::new(GrowableBitSet::new_empty()),
97             syntax_pos_globals: syntax_pos::Globals::new(edition),
98         }
99     }
100 }
101
102 pub fn with_globals<F, R>(edition: Edition, f: F) -> R
103     where F: FnOnce() -> R
104 {
105     let globals = Globals::new(edition);
106     GLOBALS.set(&globals, || {
107         syntax_pos::GLOBALS.set(&globals.syntax_pos_globals, f)
108     })
109 }
110
111 pub fn with_default_globals<F, R>(f: F) -> R
112     where F: FnOnce() -> R
113 {
114     with_globals(edition::DEFAULT_EDITION, f)
115 }
116
117 scoped_tls::scoped_thread_local!(pub static GLOBALS: Globals);
118
119 #[macro_use]
120 pub mod diagnostics {
121     #[macro_use]
122     pub mod macros;
123     pub mod plugin;
124     pub mod metadata;
125 }
126
127 // N.B., this module needs to be declared first so diagnostics are
128 // registered before they are used.
129 pub mod error_codes;
130
131 pub mod util {
132     pub mod lev_distance;
133     pub mod node_count;
134     pub mod parser;
135     #[cfg(test)]
136     pub mod parser_testing;
137     pub mod map_in_place;
138     pub mod path;
139 }
140
141 pub mod json;
142
143 pub mod ast;
144 pub mod attr;
145 pub mod source_map;
146 #[macro_use]
147 pub mod config;
148 pub mod entry;
149 pub mod feature_gate;
150 pub mod mut_visit;
151 pub mod parse;
152 pub mod ptr;
153 pub mod show_span;
154 pub mod std_inject;
155 pub use syntax_pos::edition;
156 pub use syntax_pos::symbol;
157 pub mod test;
158 pub mod tokenstream;
159 pub mod visit;
160
161 pub mod print {
162     pub mod pp;
163     pub mod pprust;
164     mod helpers;
165 }
166
167 pub mod ext {
168     pub use syntax_pos::hygiene;
169     pub mod base;
170     pub mod build;
171     pub mod derive;
172     pub mod expand;
173     pub mod placeholders;
174     pub mod source_util;
175
176     pub mod tt {
177         pub mod transcribe;
178         pub mod macro_check;
179         pub mod macro_parser;
180         pub mod macro_rules;
181         pub mod quoted;
182     }
183 }
184
185 pub mod early_buffered_lints;
186
187 #[cfg(test)]
188 mod test_snippet;
189
190 __build_diagnostic_array! { libsyntax, DIAGNOSTICS }