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;
25 /// The specific types of unsupported syntax
26 #[deriving(Eq, TotalEq, Hash)]
27 pub enum ObsoleteSyntax {
31 ObsoleteMultipleLocalDecl,
32 ObsoleteUnsafeExternFn,
33 ObsoleteTraitFuncVisibility,
35 ObsoleteLoopAsContinue,
37 ObsoleteStructWildcard,
38 ObsoleteVecDotDotWildcard,
41 ObsoleteMultipleImport,
42 ObsoleteManagedPattern,
43 ObsoleteManagedString,
47 pub trait ParserObsoleteMethods {
48 /// Reports an obsolete syntax non-fatal error.
49 fn obsolete(&mut self, sp: Span, kind: ObsoleteSyntax);
50 // Reports an obsolete syntax non-fatal error, and returns
51 // a placeholder expression
52 fn obsolete_expr(&mut self, sp: Span, kind: ObsoleteSyntax) -> @Expr;
58 fn is_obsolete_ident(&mut self, ident: &str) -> bool;
59 fn eat_obsolete_ident(&mut self, ident: &str) -> bool;
62 impl<'a> ParserObsoleteMethods for Parser<'a> {
63 /// Reports an obsolete syntax non-fatal error.
64 fn obsolete(&mut self, sp: Span, kind: ObsoleteSyntax) {
65 let (kind_str, desc) = match kind {
68 "use std::mem::{swap, replace} instead"
70 ObsoleteUnsafeBlock => (
71 "non-standalone unsafe block",
72 "use an inner `unsafe { ... }` block instead"
74 ObsoleteBareFnType => (
76 "use `|A| -> B` or `extern fn(A) -> B` instead"
78 ObsoleteMultipleLocalDecl => (
79 "declaration of multiple locals at once",
80 "instead of e.g. `let a = 1, b = 2`, write \
81 `let (a, b) = (1, 2)`."
83 ObsoleteUnsafeExternFn => (
84 "unsafe external function",
85 "external functions are always unsafe; remove the `unsafe` \
88 ObsoleteTraitFuncVisibility => (
89 "visibility not necessary",
90 "trait functions inherit the visibility of the trait itself"
92 ObsoleteConstPointer => (
94 "instead of `&const Foo` or `@const Foo`, write `&Foo` or \
97 ObsoleteLoopAsContinue => (
98 "`loop` instead of `continue`",
99 "`loop` is now only used for loops and `continue` is used for \
102 ObsoleteEnumWildcard => (
104 "use `..` instead of `*` for matching all enum fields"
106 ObsoleteStructWildcard => (
108 "use `..` instead of `_` for matching trailing struct fields"
110 ObsoleteVecDotDotWildcard => (
111 "vec slice wildcard",
112 "use `..` instead of `.._` for matching slices"
114 ObsoleteBoxedClosure => (
115 "managed or owned closure",
116 "managed closures have been removed and owned closures are \
117 now written `proc()`"
119 ObsoleteClosureType => (
121 "closures are now written `|A| -> B` rather than `&fn(A) -> \
124 ObsoleteMultipleImport => (
126 "only one import is allowed per `use` statement"
128 ObsoleteManagedPattern => (
129 "managed pointer pattern",
130 "use a nested `match` expression instead of a managed box \
133 ObsoleteManagedString => (
135 "use `Rc<~str>` instead of a managed string"
137 ObsoleteManagedVec => (
139 "use `Rc<~[T]>` instead of a managed vector"
143 self.report(sp, kind, kind_str, desc);
146 // Reports an obsolete syntax non-fatal error, and returns
147 // a placeholder expression
148 fn obsolete_expr(&mut self, sp: Span, kind: ObsoleteSyntax) -> @Expr {
149 self.obsolete(sp, kind);
150 self.mk_expr(sp.lo, sp.hi, ExprLit(@respan(sp, LitNil)))
155 kind: ObsoleteSyntax,
158 self.span_err(sp, format!("obsolete syntax: {}", kind_str));
160 if !self.obsolete_set.contains(&kind) {
161 self.sess.span_diagnostic.handler().note(format!("{}", desc));
162 self.obsolete_set.insert(kind);
166 fn is_obsolete_ident(&mut self, ident: &str) -> bool {
168 token::IDENT(sid, _) => {
169 token::get_ident(sid).equiv(&ident)
175 fn eat_obsolete_ident(&mut self, ident: &str) -> bool {
176 if self.is_obsolete_ident(ident) {