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