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