1 // Copyright 2012-2014 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.
11 //! Support for parsing unsupported, old syntaxes, for the purpose of reporting errors. Parsing of
12 //! these syntaxes is tested by compile-test/obsolete-syntax.rs.
14 //! Obsolete syntax that becomes too hard to parse can be removed.
16 use ast::{Expr, ExprTup};
22 /// The specific types of unsupported syntax
23 #[derive(Copy, PartialEq, Eq, Hash)]
24 pub enum ObsoleteSyntax {
40 pub trait ParserObsoleteMethods {
41 /// Reports an obsolete syntax non-fatal error.
42 fn obsolete(&mut self, sp: Span, kind: ObsoleteSyntax);
43 /// Reports an obsolete syntax non-fatal error, and returns
44 /// a placeholder expression
45 fn obsolete_expr(&mut self, sp: Span, kind: ObsoleteSyntax) -> P<Expr>;
51 fn is_obsolete_ident(&mut self, ident: &str) -> bool;
52 fn eat_obsolete_ident(&mut self, ident: &str) -> bool;
55 impl<'a> ParserObsoleteMethods for parser::Parser<'a> {
56 /// Reports an obsolete syntax non-fatal error.
57 fn obsolete(&mut self, sp: Span, kind: ObsoleteSyntax) {
58 let (kind_str, desc) = match kind {
59 ObsoleteSyntax::ForSized => (
61 "no longer required. Traits (and their `Self` type) do not have the `Sized` bound \
64 ObsoleteSyntax::ProcType => (
66 "use unboxed closures instead",
68 ObsoleteSyntax::ProcExpr => (
70 "use a `move ||` expression instead",
72 ObsoleteSyntax::OwnedType => (
73 "`~` notation for owned pointers",
74 "use `Box<T>` in `std::owned` instead"
76 ObsoleteSyntax::OwnedExpr => (
77 "`~` notation for owned pointer allocation",
78 "use the `box` operator instead of `~`"
80 ObsoleteSyntax::OwnedPattern => (
81 "`~` notation for owned pointer patterns",
82 "use the `box` operator instead of `~`"
84 ObsoleteSyntax::OwnedVector => (
85 "`~[T]` is no longer a type",
86 "use the `Vec` type instead"
88 ObsoleteSyntax::OwnedSelf => (
89 "`~self` is no longer supported",
90 "write `self: Box<Self>` instead"
92 ObsoleteSyntax::ImportRenaming => (
93 "`use foo = bar` syntax",
94 "write `use bar as foo` instead"
96 ObsoleteSyntax::SubsliceMatch => (
97 "subslice match syntax",
98 "instead of `..xs`, write `xs..` in a pattern"
100 ObsoleteSyntax::ExternCrateRenaming => (
101 "`extern crate foo = bar` syntax",
102 "write `extern crate bar as foo` instead"
104 ObsoleteSyntax::ClosureType => (
105 "`|uint| -> bool` closure type syntax",
106 "use unboxed closures instead, no type annotation needed"
108 ObsoleteSyntax::Sized => (
109 "`Sized? T` syntax for removing the `Sized` bound",
110 "write `T: ?Sized` instead"
114 self.report(sp, kind, kind_str, desc);
117 /// Reports an obsolete syntax non-fatal error, and returns
118 /// a placeholder expression
119 fn obsolete_expr(&mut self, sp: Span, kind: ObsoleteSyntax) -> P<Expr> {
120 self.obsolete(sp, kind);
121 self.mk_expr(sp.lo, sp.hi, ExprTup(vec![]))
126 kind: ObsoleteSyntax,
130 format!("obsolete syntax: {}", kind_str)[]);
132 if !self.obsolete_set.contains(&kind) {
136 .note(format!("{}", desc)[]);
137 self.obsolete_set.insert(kind);
141 fn is_obsolete_ident(&mut self, ident: &str) -> bool {
143 token::Ident(sid, _) => {
144 token::get_ident(sid) == ident
150 fn eat_obsolete_ident(&mut self, ident: &str) -> bool {
151 if self.is_obsolete_ident(ident) {