]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/parse/obsolete.rs
Fix misspelled comments.
[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 use ast::{Expr, ExprTup};
17 use codemap::Span;
18 use parse::parser;
19 use parse::token;
20 use ptr::P;
21
22 /// The specific types of unsupported syntax
23 #[derive(Copy, PartialEq, Eq, Hash)]
24 pub enum ObsoleteSyntax {
25     Sized,
26     ForSized,
27     OwnedType,
28     OwnedExpr,
29     OwnedPattern,
30     OwnedVector,
31     OwnedSelf,
32     ImportRenaming,
33     SubsliceMatch,
34     ExternCrateRenaming,
35     ProcType,
36     ProcExpr,
37     ClosureType,
38 }
39
40 pub trait ParserObsoleteMethods {
41     /// Reports an obsolete syntax non-fatal error.
42     fn obsolete(&mut self, sp: Span, kind: ObsoleteSyntax);
43     /// Reports an obsolete syntax non-fatal error, and returns
44     /// a placeholder expression
45     fn obsolete_expr(&mut self, sp: Span, kind: ObsoleteSyntax) -> P<Expr>;
46     fn report(&mut self,
47               sp: Span,
48               kind: ObsoleteSyntax,
49               kind_str: &str,
50               desc: &str);
51     fn is_obsolete_ident(&mut self, ident: &str) -> bool;
52     fn eat_obsolete_ident(&mut self, ident: &str) -> bool;
53 }
54
55 impl<'a> ParserObsoleteMethods for parser::Parser<'a> {
56     /// Reports an obsolete syntax non-fatal error.
57     fn obsolete(&mut self, sp: Span, kind: ObsoleteSyntax) {
58         let (kind_str, desc) = match kind {
59             ObsoleteSyntax::ForSized => (
60                 "for Sized?",
61                 "no longer required. Traits (and their `Self` type) do not have the `Sized` bound \
62                  by default",
63             ),
64             ObsoleteSyntax::ProcType => (
65                 "the `proc` type",
66                 "use unboxed closures instead",
67             ),
68             ObsoleteSyntax::ProcExpr => (
69                 "`proc` expression",
70                 "use a `move ||` expression instead",
71             ),
72             ObsoleteSyntax::OwnedType => (
73                 "`~` notation for owned pointers",
74                 "use `Box<T>` in `std::owned` instead"
75             ),
76             ObsoleteSyntax::OwnedExpr => (
77                 "`~` notation for owned pointer allocation",
78                 "use the `box` operator instead of `~`"
79             ),
80             ObsoleteSyntax::OwnedPattern => (
81                 "`~` notation for owned pointer patterns",
82                 "use the `box` operator instead of `~`"
83             ),
84             ObsoleteSyntax::OwnedVector => (
85                 "`~[T]` is no longer a type",
86                 "use the `Vec` type instead"
87             ),
88             ObsoleteSyntax::OwnedSelf => (
89                 "`~self` is no longer supported",
90                 "write `self: Box<Self>` instead"
91             ),
92             ObsoleteSyntax::ImportRenaming => (
93                 "`use foo = bar` syntax",
94                 "write `use bar as foo` instead"
95             ),
96             ObsoleteSyntax::SubsliceMatch => (
97                 "subslice match syntax",
98                 "instead of `..xs`, write `xs..` in a pattern"
99             ),
100             ObsoleteSyntax::ExternCrateRenaming => (
101                 "`extern crate foo = bar` syntax",
102                 "write `extern crate bar as foo` instead"
103             ),
104             ObsoleteSyntax::ClosureType => (
105                 "`|uint| -> bool` closure type syntax",
106                 "use unboxed closures instead, no type annotation needed"
107             ),
108             ObsoleteSyntax::Sized => (
109                 "`Sized? T` syntax for removing the `Sized` bound",
110                 "write `T: ?Sized` instead"
111             ),
112         };
113
114         self.report(sp, kind, kind_str, desc);
115     }
116
117     /// Reports an obsolete syntax non-fatal error, and returns
118     /// a placeholder expression
119     fn obsolete_expr(&mut self, sp: Span, kind: ObsoleteSyntax) -> P<Expr> {
120         self.obsolete(sp, kind);
121         self.mk_expr(sp.lo, sp.hi, ExprTup(vec![]))
122     }
123
124     fn report(&mut self,
125               sp: Span,
126               kind: ObsoleteSyntax,
127               kind_str: &str,
128               desc: &str) {
129         self.span_err(sp,
130                       format!("obsolete syntax: {}", kind_str)[]);
131
132         if !self.obsolete_set.contains(&kind) {
133             self.sess
134                 .span_diagnostic
135                 .handler()
136                 .note(format!("{}", desc)[]);
137             self.obsolete_set.insert(kind);
138         }
139     }
140
141     fn is_obsolete_ident(&mut self, ident: &str) -> bool {
142         match self.token {
143             token::Ident(sid, _) => {
144                 token::get_ident(sid) == ident
145             }
146             _ => false
147         }
148     }
149
150     fn eat_obsolete_ident(&mut self, ident: &str) -> bool {
151         if self.is_obsolete_ident(ident) {
152             self.bump();
153             true
154         } else {
155             false
156         }
157     }
158 }