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,
39 ObsoleteMultipleImport,
40 ObsoleteManagedPattern,
41 ObsoleteManagedString,
48 pub trait ParserObsoleteMethods {
49 /// Reports an obsolete syntax non-fatal error.
50 fn obsolete(&mut self, sp: Span, kind: ObsoleteSyntax);
51 // Reports an obsolete syntax non-fatal error, and returns
52 // a placeholder expression
53 fn obsolete_expr(&mut self, sp: Span, kind: ObsoleteSyntax) -> @Expr;
59 fn is_obsolete_ident(&mut self, ident: &str) -> bool;
60 fn eat_obsolete_ident(&mut self, ident: &str) -> bool;
63 impl<'a> ParserObsoleteMethods for Parser<'a> {
64 /// Reports an obsolete syntax non-fatal error.
65 fn obsolete(&mut self, sp: Span, kind: ObsoleteSyntax) {
66 let (kind_str, desc) = match kind {
69 "use std::mem::{swap, replace} instead"
71 ObsoleteUnsafeBlock => (
72 "non-standalone unsafe block",
73 "use an inner `unsafe { ... }` block instead"
75 ObsoleteBareFnType => (
77 "use `|A| -> B` or `extern fn(A) -> B` instead"
79 ObsoleteMultipleLocalDecl => (
80 "declaration of multiple locals at once",
81 "instead of e.g. `let a = 1, b = 2`, write \
82 `let (a, b) = (1, 2)`."
84 ObsoleteUnsafeExternFn => (
85 "unsafe external function",
86 "external functions are always unsafe; remove the `unsafe` \
89 ObsoleteTraitFuncVisibility => (
90 "visibility not necessary",
91 "trait functions inherit the visibility of the trait itself"
93 ObsoleteConstPointer => (
95 "instead of `&const Foo` or `@const Foo`, write `&Foo` or \
98 ObsoleteLoopAsContinue => (
99 "`loop` instead of `continue`",
100 "`loop` is now only used for loops and `continue` is used for \
103 ObsoleteEnumWildcard => (
105 "use `..` instead of `*` for matching all enum fields"
107 ObsoleteStructWildcard => (
109 "use `..` instead of `_` for matching trailing struct fields"
111 ObsoleteVecDotDotWildcard => (
112 "vec slice wildcard",
113 "use `..` instead of `.._` for matching slices"
115 ObsoleteMultipleImport => (
117 "only one import is allowed per `use` statement"
119 ObsoleteManagedPattern => (
120 "managed pointer pattern",
121 "use a nested `match` expression instead of a managed box \
124 ObsoleteManagedString => (
126 "use `Rc<~str>` instead of a managed string"
128 ObsoleteManagedVec => (
130 "use `Rc<~[T]>` instead of a managed vector"
132 ObsoleteOwnedType => (
133 "`~` notation for owned pointers",
134 "use `Box<T>` in `std::owned` instead"
136 ObsoleteOwnedExpr => (
137 "`~` notation for owned pointer allocation",
138 "use the `box` operator instead of `~`"
140 ObsoleteOwnedPattern => (
141 "`~` notation for owned pointer patterns",
142 "use the `box` operator instead of `~`"
146 self.report(sp, kind, kind_str, desc);
149 // Reports an obsolete syntax non-fatal error, and returns
150 // a placeholder expression
151 fn obsolete_expr(&mut self, sp: Span, kind: ObsoleteSyntax) -> @Expr {
152 self.obsolete(sp, kind);
153 self.mk_expr(sp.lo, sp.hi, ExprLit(@respan(sp, LitNil)))
158 kind: ObsoleteSyntax,
161 self.span_err(sp, format!("obsolete syntax: {}", kind_str));
163 if !self.obsolete_set.contains(&kind) {
164 self.sess.span_diagnostic.handler().note(format!("{}", desc));
165 self.obsolete_set.insert(kind);
169 fn is_obsolete_ident(&mut self, ident: &str) -> bool {
171 token::IDENT(sid, _) => {
172 token::get_ident(sid).equiv(&ident)
178 fn eat_obsolete_ident(&mut self, ident: &str) -> bool {
179 if self.is_obsolete_ident(ident) {