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, lit_nil};
21 use codemap::{Span, respan};
22 use parse::parser::Parser;
23 use parse::token::Token;
29 /// The specific types of unsupported syntax
31 pub enum ObsoleteSyntax {
35 ObsoleteNamedExternModule,
36 ObsoleteMultipleLocalDecl,
37 ObsoleteUnsafeExternFn,
38 ObsoleteTraitFuncVisibility,
41 ObsoleteLoopAsContinue,
43 ObsoleteStructWildcard,
44 ObsoleteVecDotDotWildcard,
47 ObsoleteMultipleImport,
48 ObsoleteExternModAttributesInParens
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(&self, sp: Span, kind: ObsoleteSyntax);
61 // Reports an obsolete syntax non-fatal error, and returns
62 // a placeholder expression
63 fn obsolete_expr(&self, sp: Span, kind: ObsoleteSyntax) -> @Expr;
69 fn token_is_obsolete_ident(&self, ident: &str, token: &Token) -> bool;
70 fn is_obsolete_ident(&self, ident: &str) -> bool;
71 fn eat_obsolete_ident(&self, ident: &str) -> bool;
74 impl ParserObsoleteMethods for Parser {
75 /// Reports an obsolete syntax non-fatal error.
76 fn obsolete(&self, sp: Span, kind: ObsoleteSyntax) {
77 let (kind_str, desc) = match kind {
80 "Use std::util::{swap, replace} instead"
82 ObsoleteUnsafeBlock => (
83 "non-standalone unsafe block",
84 "use an inner `unsafe { ... }` block instead"
86 ObsoleteBareFnType => (
88 "use `|A| -> B` or `extern fn(A) -> B` instead"
90 ObsoleteNamedExternModule => (
91 "named external module",
92 "instead of `extern mod foo { ... }`, write `mod foo { \
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)`."
100 ObsoleteUnsafeExternFn => (
101 "unsafe external function",
102 "external functions are always unsafe; remove the `unsafe` \
105 ObsoleteTraitFuncVisibility => (
106 "visibility not necessary",
107 "trait functions inherit the visibility of the trait itself"
109 ObsoleteConstPointer => (
111 "instead of `&const Foo` or `@const Foo`, write `&Foo` or \
114 ObsoleteEmptyImpl => (
115 "empty implementation",
116 "instead of `impl A;`, write `impl A {}`"
118 ObsoleteLoopAsContinue => (
119 "`loop` instead of `continue`",
120 "`loop` is now only used for loops and `continue` is used for \
123 ObsoleteEnumWildcard => (
125 "use `..` instead of `*` for matching all enum fields"
127 ObsoleteStructWildcard => (
129 "use `..` instead of `_` for matching trailing struct fields"
131 ObsoleteVecDotDotWildcard => (
132 "vec slice wildcard",
133 "use `..` instead of `.._` for matching slices"
135 ObsoleteBoxedClosure => (
136 "managed or owned closure",
137 "managed closures have been removed and owned closures are \
138 now written `proc()`"
140 ObsoleteClosureType => (
142 "closures are now written `|A| -> B` rather than `&fn(A) -> \
145 ObsoleteMultipleImport => (
147 "only one import is allowed per `use` statement"
149 ObsoleteExternModAttributesInParens => (
150 "`extern mod` with linkage attribute list",
151 "use `extern mod foo = \"bar\";` instead of \
152 `extern mod foo (name = \"bar\")`"
156 self.report(sp, kind, kind_str, desc);
159 // Reports an obsolete syntax non-fatal error, and returns
160 // a placeholder expression
161 fn obsolete_expr(&self, sp: Span, kind: ObsoleteSyntax) -> @Expr {
162 self.obsolete(sp, kind);
163 self.mk_expr(sp.lo, sp.hi, ExprLit(@respan(sp, lit_nil)))
168 kind: ObsoleteSyntax,
171 self.span_err(sp, format!("obsolete syntax: {}", kind_str));
173 if !self.obsolete_set.contains(&kind) {
174 self.sess.span_diagnostic.handler().note(format!("{}", desc));
175 self.obsolete_set.insert(kind);
179 fn token_is_obsolete_ident(&self, ident: &str, token: &Token)
182 token::IDENT(sid, _) => {
183 str::eq_slice(self.id_to_str(sid), ident)
189 fn is_obsolete_ident(&self, ident: &str) -> bool {
190 self.token_is_obsolete_ident(ident, self.token)
193 fn eat_obsolete_ident(&self, ident: &str) -> bool {
194 if self.is_obsolete_ident(ident) {