]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/parse/obsolete.rs
04c73ce71d0207777a34d044057c501a2a6b885d
[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 use std::to_bytes;
26
27 /// The specific types of unsupported syntax
28 #[deriving(Eq)]
29 pub enum ObsoleteSyntax {
30     ObsoleteSwap,
31     ObsoleteUnsafeBlock,
32     ObsoleteBareFnType,
33     ObsoleteNamedExternModule,
34     ObsoleteMultipleLocalDecl,
35     ObsoleteUnsafeExternFn,
36     ObsoleteTraitFuncVisibility,
37     ObsoleteConstPointer,
38     ObsoleteLoopAsContinue,
39     ObsoleteEnumWildcard,
40     ObsoleteStructWildcard,
41     ObsoleteVecDotDotWildcard,
42     ObsoleteBoxedClosure,
43     ObsoleteClosureType,
44     ObsoleteMultipleImport,
45     ObsoleteExternModAttributesInParens,
46     ObsoleteManagedPattern,
47 }
48
49 impl to_bytes::IterBytes for ObsoleteSyntax {
50     #[inline]
51     fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) -> bool {
52         (*self as uint).iter_bytes(lsb0, f)
53     }
54 }
55
56 pub trait ParserObsoleteMethods {
57     /// Reports an obsolete syntax non-fatal error.
58     fn obsolete(&mut self, sp: Span, kind: ObsoleteSyntax);
59     // Reports an obsolete syntax non-fatal error, and returns
60     // a placeholder expression
61     fn obsolete_expr(&mut self, sp: Span, kind: ObsoleteSyntax) -> @Expr;
62     fn report(&mut self,
63               sp: Span,
64               kind: ObsoleteSyntax,
65               kind_str: &str,
66               desc: &str);
67     fn is_obsolete_ident(&mut self, ident: &str) -> bool;
68     fn eat_obsolete_ident(&mut self, ident: &str) -> bool;
69 }
70
71 impl ParserObsoleteMethods for Parser {
72     /// Reports an obsolete syntax non-fatal error.
73     fn obsolete(&mut self, sp: Span, kind: ObsoleteSyntax) {
74         let (kind_str, desc) = match kind {
75             ObsoleteSwap => (
76                 "swap",
77                 "Use std::util::{swap, replace} instead"
78             ),
79             ObsoleteUnsafeBlock => (
80                 "non-standalone unsafe block",
81                 "use an inner `unsafe { ... }` block instead"
82             ),
83             ObsoleteBareFnType => (
84                 "bare function type",
85                 "use `|A| -> B` or `extern fn(A) -> B` instead"
86             ),
87             ObsoleteNamedExternModule => (
88                 "named external module",
89                 "instead of `extern mod foo { ... }`, write `mod foo { \
90                  extern { ... } }`"
91             ),
92             ObsoleteMultipleLocalDecl => (
93                 "declaration of multiple locals at once",
94                 "instead of e.g. `let a = 1, b = 2`, write \
95                  `let (a, b) = (1, 2)`."
96             ),
97             ObsoleteUnsafeExternFn => (
98                 "unsafe external function",
99                 "external functions are always unsafe; remove the `unsafe` \
100                  keyword"
101             ),
102             ObsoleteTraitFuncVisibility => (
103                 "visibility not necessary",
104                 "trait functions inherit the visibility of the trait itself"
105             ),
106             ObsoleteConstPointer => (
107                 "const pointer",
108                 "instead of `&const Foo` or `@const Foo`, write `&Foo` or \
109                  `@Foo`"
110             ),
111             ObsoleteLoopAsContinue => (
112                 "`loop` instead of `continue`",
113                 "`loop` is now only used for loops and `continue` is used for \
114                  skipping iterations"
115             ),
116             ObsoleteEnumWildcard => (
117                 "enum wildcard",
118                 "use `..` instead of `*` for matching all enum fields"
119             ),
120             ObsoleteStructWildcard => (
121                 "struct wildcard",
122                 "use `..` instead of `_` for matching trailing struct fields"
123             ),
124             ObsoleteVecDotDotWildcard => (
125                 "vec slice wildcard",
126                 "use `..` instead of `.._` for matching slices"
127             ),
128             ObsoleteBoxedClosure => (
129                 "managed or owned closure",
130                 "managed closures have been removed and owned closures are \
131                  now written `proc()`"
132             ),
133             ObsoleteClosureType => (
134                 "closure type",
135                 "closures are now written `|A| -> B` rather than `&fn(A) -> \
136                  B`."
137             ),
138             ObsoleteMultipleImport => (
139                 "multiple imports",
140                 "only one import is allowed per `use` statement"
141             ),
142             ObsoleteExternModAttributesInParens => (
143                 "`extern mod` with linkage attribute list",
144                 "use `extern mod foo = \"bar\";` instead of \
145                 `extern mod foo (name = \"bar\")`"
146             ),
147             ObsoleteManagedPattern => (
148                 "managed pointer pattern",
149                 "use a nested `match` expression instead of a managed box \
150                  pattern"
151             ),
152         };
153
154         self.report(sp, kind, kind_str, desc);
155     }
156
157     // Reports an obsolete syntax non-fatal error, and returns
158     // a placeholder expression
159     fn obsolete_expr(&mut self, sp: Span, kind: ObsoleteSyntax) -> @Expr {
160         self.obsolete(sp, kind);
161         self.mk_expr(sp.lo, sp.hi, ExprLit(@respan(sp, LitNil)))
162     }
163
164     fn report(&mut self,
165               sp: Span,
166               kind: ObsoleteSyntax,
167               kind_str: &str,
168               desc: &str) {
169         self.span_err(sp, format!("obsolete syntax: {}", kind_str));
170
171         if !self.obsolete_set.contains(&kind) {
172             self.sess.span_diagnostic.handler().note(format!("{}", desc));
173             self.obsolete_set.insert(kind);
174         }
175     }
176
177     fn is_obsolete_ident(&mut self, ident: &str) -> bool {
178         match self.token {
179             token::IDENT(sid, _) => {
180                 let interned_string = token::get_ident(sid.name);
181                 interned_string.equiv(&ident)
182             }
183             _ => false
184         }
185     }
186
187     fn eat_obsolete_ident(&mut self, ident: &str) -> bool {
188         if self.is_obsolete_ident(ident) {
189             self.bump();
190             true
191         } else {
192             false
193         }
194     }
195 }