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