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.
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.
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.
16 Obsolete syntax that becomes too hard to parse can be
20 use ast::{Expr, ExprLit, LitNil};
21 use codemap::{Span, respan};
22 use parse::parser::Parser;
28 /// The specific types of unsupported syntax
30 pub enum ObsoleteSyntax {
34 ObsoleteNamedExternModule,
35 ObsoleteMultipleLocalDecl,
36 ObsoleteUnsafeExternFn,
37 ObsoleteTraitFuncVisibility,
40 ObsoleteLoopAsContinue,
42 ObsoleteStructWildcard,
43 ObsoleteVecDotDotWildcard,
46 ObsoleteMultipleImport,
47 ObsoleteExternModAttributesInParens,
48 ObsoleteManagedPattern,
51 impl to_bytes::IterBytes for ObsoleteSyntax {
53 fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) -> bool {
54 (*self as uint).iter_bytes(lsb0, f)
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;
69 fn is_obsolete_ident(&mut self, ident: &str) -> bool;
70 fn eat_obsolete_ident(&mut self, ident: &str) -> bool;
73 impl ParserObsoleteMethods for Parser {
74 /// Reports an obsolete syntax non-fatal error.
75 fn obsolete(&mut self, sp: Span, kind: ObsoleteSyntax) {
76 let (kind_str, desc) = match kind {
79 "Use std::util::{swap, replace} instead"
81 ObsoleteUnsafeBlock => (
82 "non-standalone unsafe block",
83 "use an inner `unsafe { ... }` block instead"
85 ObsoleteBareFnType => (
87 "use `|A| -> B` or `extern fn(A) -> B` instead"
89 ObsoleteNamedExternModule => (
90 "named external module",
91 "instead of `extern mod foo { ... }`, write `mod foo { \
94 ObsoleteMultipleLocalDecl => (
95 "declaration of multiple locals at once",
96 "instead of e.g. `let a = 1, b = 2`, write \
97 `let (a, b) = (1, 2)`."
99 ObsoleteUnsafeExternFn => (
100 "unsafe external function",
101 "external functions are always unsafe; remove the `unsafe` \
104 ObsoleteTraitFuncVisibility => (
105 "visibility not necessary",
106 "trait functions inherit the visibility of the trait itself"
108 ObsoleteConstPointer => (
110 "instead of `&const Foo` or `@const Foo`, write `&Foo` or \
113 ObsoleteEmptyImpl => (
114 "empty implementation",
115 "instead of `impl A;`, write `impl A {}`"
117 ObsoleteLoopAsContinue => (
118 "`loop` instead of `continue`",
119 "`loop` is now only used for loops and `continue` is used for \
122 ObsoleteEnumWildcard => (
124 "use `..` instead of `*` for matching all enum fields"
126 ObsoleteStructWildcard => (
128 "use `..` instead of `_` for matching trailing struct fields"
130 ObsoleteVecDotDotWildcard => (
131 "vec slice wildcard",
132 "use `..` instead of `.._` for matching slices"
134 ObsoleteBoxedClosure => (
135 "managed or owned closure",
136 "managed closures have been removed and owned closures are \
137 now written `proc()`"
139 ObsoleteClosureType => (
141 "closures are now written `|A| -> B` rather than `&fn(A) -> \
144 ObsoleteMultipleImport => (
146 "only one import is allowed per `use` statement"
148 ObsoleteExternModAttributesInParens => (
149 "`extern mod` with linkage attribute list",
150 "use `extern mod foo = \"bar\";` instead of \
151 `extern mod foo (name = \"bar\")`"
153 ObsoleteManagedPattern => (
154 "managed pointer pattern",
155 "use a nested `match` expression instead of a managed box \
160 self.report(sp, kind, kind_str, desc);
163 // Reports an obsolete syntax non-fatal error, and returns
164 // a placeholder expression
165 fn obsolete_expr(&mut self, sp: Span, kind: ObsoleteSyntax) -> @Expr {
166 self.obsolete(sp, kind);
167 self.mk_expr(sp.lo, sp.hi, ExprLit(@respan(sp, LitNil)))
172 kind: ObsoleteSyntax,
175 self.span_err(sp, format!("obsolete syntax: {}", kind_str));
177 if !self.obsolete_set.contains(&kind) {
178 self.sess.span_diagnostic.handler().note(format!("{}", desc));
179 self.obsolete_set.insert(kind);
183 fn is_obsolete_ident(&mut self, ident: &str) -> bool {
185 token::IDENT(sid, _) => {
186 str::eq_slice(self.id_to_str(sid), ident)
192 fn eat_obsolete_ident(&mut self, ident: &str) -> bool {
193 if self.is_obsolete_ident(ident) {