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