]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/parse/obsolete.rs
Merge pull request #20510 from tshepang/patch-6
[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 }
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             ObsoleteProcType => (
59                 "the `proc` type",
60                 "use unboxed closures instead",
61             ),
62             ObsoleteProcExpr => (
63                 "`proc` expression",
64                 "use a `move ||` expression instead",
65             ),
66             ObsoleteOwnedType => (
67                 "`~` notation for owned pointers",
68                 "use `Box<T>` in `std::owned` instead"
69             ),
70             ObsoleteOwnedExpr => (
71                 "`~` notation for owned pointer allocation",
72                 "use the `box` operator instead of `~`"
73             ),
74             ObsoleteOwnedPattern => (
75                 "`~` notation for owned pointer patterns",
76                 "use the `box` operator instead of `~`"
77             ),
78             ObsoleteOwnedVector => (
79                 "`~[T]` is no longer a type",
80                 "use the `Vec` type instead"
81             ),
82             ObsoleteOwnedSelf => (
83                 "`~self` is no longer supported",
84                 "write `self: Box<Self>` instead"
85             ),
86             ObsoleteImportRenaming => (
87                 "`use foo = bar` syntax",
88                 "write `use bar as foo` instead"
89             ),
90             ObsoleteSubsliceMatch => (
91                 "subslice match syntax",
92                 "instead of `..xs`, write `xs..` in a pattern"
93             ),
94             ObsoleteExternCrateRenaming => (
95                 "`extern crate foo = bar` syntax",
96                 "write `extern crate bar as foo` instead"
97             )
98         };
99
100         self.report(sp, kind, kind_str, desc);
101     }
102
103     /// Reports an obsolete syntax non-fatal error, and returns
104     /// a placeholder expression
105     fn obsolete_expr(&mut self, sp: Span, kind: ObsoleteSyntax) -> P<Expr> {
106         self.obsolete(sp, kind);
107         self.mk_expr(sp.lo, sp.hi, ExprTup(vec![]))
108     }
109
110     fn report(&mut self,
111               sp: Span,
112               kind: ObsoleteSyntax,
113               kind_str: &str,
114               desc: &str) {
115         self.span_err(sp,
116                       format!("obsolete syntax: {}", kind_str)[]);
117
118         if !self.obsolete_set.contains(&kind) {
119             self.sess
120                 .span_diagnostic
121                 .handler()
122                 .note(format!("{}", desc)[]);
123             self.obsolete_set.insert(kind);
124         }
125     }
126
127     fn is_obsolete_ident(&mut self, ident: &str) -> bool {
128         match self.token {
129             token::Ident(sid, _) => {
130                 token::get_ident(sid) == ident
131             }
132             _ => false
133         }
134     }
135
136     fn eat_obsolete_ident(&mut self, ident: &str) -> bool {
137         if self.is_obsolete_ident(ident) {
138             self.bump();
139             true
140         } else {
141             false
142         }
143     }
144 }