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;
27 /// The specific types of unsupported syntax
29 pub enum ObsoleteSyntax {
33 ObsoleteMultipleLocalDecl,
34 ObsoleteUnsafeExternFn,
35 ObsoleteTraitFuncVisibility,
37 ObsoleteLoopAsContinue,
39 ObsoleteStructWildcard,
40 ObsoleteVecDotDotWildcard,
43 ObsoleteMultipleImport,
44 ObsoleteManagedPattern,
45 ObsoleteManagedString,
49 impl to_bytes::IterBytes for ObsoleteSyntax {
51 fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) -> bool {
52 (*self as uint).iter_bytes(lsb0, f)
56 pub trait ParserObsoleteMethods {
57 /// Reports an obsolete syntax non-fatal error.
58 fn obsolete(&mut self, sp: Span, kind: ObsoleteSyntax);
59 // Reports an obsolete syntax non-fatal error, and returns
60 // a placeholder expression
61 fn obsolete_expr(&mut self, sp: Span, kind: ObsoleteSyntax) -> @Expr;
67 fn is_obsolete_ident(&mut self, ident: &str) -> bool;
68 fn eat_obsolete_ident(&mut self, ident: &str) -> bool;
71 impl ParserObsoleteMethods for Parser {
72 /// Reports an obsolete syntax non-fatal error.
73 fn obsolete(&mut self, sp: Span, kind: ObsoleteSyntax) {
74 let (kind_str, desc) = match kind {
77 "use std::mem::{swap, replace} instead"
79 ObsoleteUnsafeBlock => (
80 "non-standalone unsafe block",
81 "use an inner `unsafe { ... }` block instead"
83 ObsoleteBareFnType => (
85 "use `|A| -> B` or `extern fn(A) -> B` instead"
87 ObsoleteMultipleLocalDecl => (
88 "declaration of multiple locals at once",
89 "instead of e.g. `let a = 1, b = 2`, write \
90 `let (a, b) = (1, 2)`."
92 ObsoleteUnsafeExternFn => (
93 "unsafe external function",
94 "external functions are always unsafe; remove the `unsafe` \
97 ObsoleteTraitFuncVisibility => (
98 "visibility not necessary",
99 "trait functions inherit the visibility of the trait itself"
101 ObsoleteConstPointer => (
103 "instead of `&const Foo` or `@const Foo`, write `&Foo` or \
106 ObsoleteLoopAsContinue => (
107 "`loop` instead of `continue`",
108 "`loop` is now only used for loops and `continue` is used for \
111 ObsoleteEnumWildcard => (
113 "use `..` instead of `*` for matching all enum fields"
115 ObsoleteStructWildcard => (
117 "use `..` instead of `_` for matching trailing struct fields"
119 ObsoleteVecDotDotWildcard => (
120 "vec slice wildcard",
121 "use `..` instead of `.._` for matching slices"
123 ObsoleteBoxedClosure => (
124 "managed or owned closure",
125 "managed closures have been removed and owned closures are \
126 now written `proc()`"
128 ObsoleteClosureType => (
130 "closures are now written `|A| -> B` rather than `&fn(A) -> \
133 ObsoleteMultipleImport => (
135 "only one import is allowed per `use` statement"
137 ObsoleteManagedPattern => (
138 "managed pointer pattern",
139 "use a nested `match` expression instead of a managed box \
142 ObsoleteManagedString => (
144 "use `Rc<~str>` instead of a managed string"
146 ObsoleteManagedVec => (
148 "use `Rc<~[T]>` instead of a managed vector"
152 self.report(sp, kind, kind_str, desc);
155 // Reports an obsolete syntax non-fatal error, and returns
156 // a placeholder expression
157 fn obsolete_expr(&mut self, sp: Span, kind: ObsoleteSyntax) -> @Expr {
158 self.obsolete(sp, kind);
159 self.mk_expr(sp.lo, sp.hi, ExprLit(@respan(sp, LitNil)))
164 kind: ObsoleteSyntax,
167 self.span_err(sp, format!("obsolete syntax: {}", kind_str));
169 if !self.obsolete_set.contains(&kind) {
170 self.sess.span_diagnostic.handler().note(format!("{}", desc));
171 self.obsolete_set.insert(kind);
175 fn is_obsolete_ident(&mut self, ident: &str) -> bool {
177 token::IDENT(sid, _) => {
178 token::get_ident(sid).equiv(&ident)
184 fn eat_obsolete_ident(&mut self, ident: &str) -> bool {
185 if self.is_obsolete_ident(ident) {