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