]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/parse/obsolete.rs
rollup merge of #20518: nagisa/weighted-bool
[rust.git] / src / libsyntax / parse / obsolete.rs
1 // Copyright 2012-2014 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 //! Support for parsing unsupported, old syntaxes, for the purpose of reporting errors. Parsing of
12 //! these syntaxes is tested by compile-test/obsolete-syntax.rs.
13 //!
14 //! Obsolete syntax that becomes too hard to parse can be removed.
15
16 pub use self::ObsoleteSyntax::*;
17
18 use ast::{Expr, ExprTup};
19 use codemap::Span;
20 use parse::parser;
21 use parse::token;
22 use ptr::P;
23
24 /// The specific types of unsupported syntax
25 #[derive(Copy, PartialEq, Eq, Hash)]
26 pub enum ObsoleteSyntax {
27     ObsoleteOwnedType,
28     ObsoleteOwnedExpr,
29     ObsoleteOwnedPattern,
30     ObsoleteOwnedVector,
31     ObsoleteOwnedSelf,
32     ObsoleteImportRenaming,
33     ObsoleteSubsliceMatch,
34     ObsoleteExternCrateRenaming,
35     ObsoleteProcType,
36     ObsoleteProcExpr,
37     ObsoleteClosureType,
38 }
39
40 pub trait ParserObsoleteMethods {
41     /// Reports an obsolete syntax non-fatal error.
42     fn obsolete(&mut self, sp: Span, kind: ObsoleteSyntax);
43     /// Reports an obsolete syntax non-fatal error, and returns
44     /// a placeholder expression
45     fn obsolete_expr(&mut self, sp: Span, kind: ObsoleteSyntax) -> P<Expr>;
46     fn report(&mut self,
47               sp: Span,
48               kind: ObsoleteSyntax,
49               kind_str: &str,
50               desc: &str);
51     fn is_obsolete_ident(&mut self, ident: &str) -> bool;
52     fn eat_obsolete_ident(&mut self, ident: &str) -> bool;
53 }
54
55 impl<'a> ParserObsoleteMethods for parser::Parser<'a> {
56     /// Reports an obsolete syntax non-fatal error.
57     fn obsolete(&mut self, sp: Span, kind: ObsoleteSyntax) {
58         let (kind_str, desc) = match kind {
59             ObsoleteProcType => (
60                 "the `proc` type",
61                 "use unboxed closures instead",
62             ),
63             ObsoleteProcExpr => (
64                 "`proc` expression",
65                 "use a `move ||` expression instead",
66             ),
67             ObsoleteOwnedType => (
68                 "`~` notation for owned pointers",
69                 "use `Box<T>` in `std::owned` instead"
70             ),
71             ObsoleteOwnedExpr => (
72                 "`~` notation for owned pointer allocation",
73                 "use the `box` operator instead of `~`"
74             ),
75             ObsoleteOwnedPattern => (
76                 "`~` notation for owned pointer patterns",
77                 "use the `box` operator instead of `~`"
78             ),
79             ObsoleteOwnedVector => (
80                 "`~[T]` is no longer a type",
81                 "use the `Vec` type instead"
82             ),
83             ObsoleteOwnedSelf => (
84                 "`~self` is no longer supported",
85                 "write `self: Box<Self>` instead"
86             ),
87             ObsoleteImportRenaming => (
88                 "`use foo = bar` syntax",
89                 "write `use bar as foo` instead"
90             ),
91             ObsoleteSubsliceMatch => (
92                 "subslice match syntax",
93                 "instead of `..xs`, write `xs..` in a pattern"
94             ),
95             ObsoleteExternCrateRenaming => (
96                 "`extern crate foo = bar` syntax",
97                 "write `extern crate bar as foo` instead"
98             ),
99             ObsoleteClosureType => (
100                 "`|uint| -> bool` closure type syntax",
101                 "use unboxed closures instead, no type annotation needed"
102             )
103         };
104
105         self.report(sp, kind, kind_str, desc);
106     }
107
108     /// Reports an obsolete syntax non-fatal error, and returns
109     /// a placeholder expression
110     fn obsolete_expr(&mut self, sp: Span, kind: ObsoleteSyntax) -> P<Expr> {
111         self.obsolete(sp, kind);
112         self.mk_expr(sp.lo, sp.hi, ExprTup(vec![]))
113     }
114
115     fn report(&mut self,
116               sp: Span,
117               kind: ObsoleteSyntax,
118               kind_str: &str,
119               desc: &str) {
120         self.span_err(sp,
121                       format!("obsolete syntax: {}", kind_str)[]);
122
123         if !self.obsolete_set.contains(&kind) {
124             self.sess
125                 .span_diagnostic
126                 .handler()
127                 .note(format!("{}", desc)[]);
128             self.obsolete_set.insert(kind);
129         }
130     }
131
132     fn is_obsolete_ident(&mut self, ident: &str) -> bool {
133         match self.token {
134             token::Ident(sid, _) => {
135                 token::get_ident(sid) == ident
136             }
137             _ => false
138         }
139     }
140
141     fn eat_obsolete_ident(&mut self, ident: &str) -> bool {
142         if self.is_obsolete_ident(ident) {
143             self.bump();
144             true
145         } else {
146             false
147         }
148     }
149 }