]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/parse/obsolete.rs
syntax: Clean out obsolete syntax parsing
[rust.git] / src / libsyntax / parse / obsolete.rs
1 // Copyright 2012 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::Parser;
23 use parse::token;
24
25 /// The specific types of unsupported syntax
26 #[deriving(Eq, TotalEq, Hash)]
27 pub enum ObsoleteSyntax {
28     ObsoleteOwnedType,
29     ObsoleteOwnedExpr,
30     ObsoleteOwnedPattern,
31 }
32
33 pub trait ParserObsoleteMethods {
34     /// Reports an obsolete syntax non-fatal error.
35     fn obsolete(&mut self, sp: Span, kind: ObsoleteSyntax);
36     // Reports an obsolete syntax non-fatal error, and returns
37     // a placeholder expression
38     fn obsolete_expr(&mut self, sp: Span, kind: ObsoleteSyntax) -> @Expr;
39     fn report(&mut self,
40               sp: Span,
41               kind: ObsoleteSyntax,
42               kind_str: &str,
43               desc: &str);
44     fn is_obsolete_ident(&mut self, ident: &str) -> bool;
45     fn eat_obsolete_ident(&mut self, ident: &str) -> bool;
46 }
47
48 impl<'a> ParserObsoleteMethods for Parser<'a> {
49     /// Reports an obsolete syntax non-fatal error.
50     fn obsolete(&mut self, sp: Span, kind: ObsoleteSyntax) {
51         let (kind_str, desc) = match kind {
52             ObsoleteOwnedType => (
53                 "`~` notation for owned pointers",
54                 "use `Box<T>` in `std::owned` instead"
55             ),
56             ObsoleteOwnedExpr => (
57                 "`~` notation for owned pointer allocation",
58                 "use the `box` operator instead of `~`"
59             ),
60             ObsoleteOwnedPattern => (
61                 "`~` notation for owned pointer patterns",
62                 "use the `box` operator instead of `~`"
63             ),
64         };
65
66         self.report(sp, kind, kind_str, desc);
67     }
68
69     // Reports an obsolete syntax non-fatal error, and returns
70     // a placeholder expression
71     fn obsolete_expr(&mut self, sp: Span, kind: ObsoleteSyntax) -> @Expr {
72         self.obsolete(sp, kind);
73         self.mk_expr(sp.lo, sp.hi, ExprLit(@respan(sp, LitNil)))
74     }
75
76     fn report(&mut self,
77               sp: Span,
78               kind: ObsoleteSyntax,
79               kind_str: &str,
80               desc: &str) {
81         self.span_err(sp, format!("obsolete syntax: {}", kind_str));
82
83         if !self.obsolete_set.contains(&kind) {
84             self.sess.span_diagnostic.handler().note(format!("{}", desc));
85             self.obsolete_set.insert(kind);
86         }
87     }
88
89     fn is_obsolete_ident(&mut self, ident: &str) -> bool {
90         match self.token {
91             token::IDENT(sid, _) => {
92                 token::get_ident(sid).equiv(&ident)
93             }
94             _ => false
95         }
96     }
97
98     fn eat_obsolete_ident(&mut self, ident: &str) -> bool {
99         if self.is_obsolete_ident(ident) {
100             self.bump();
101             true
102         } else {
103             false
104         }
105     }
106 }