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 {
32 pub trait ParserObsoleteMethods {
33 /// Reports an obsolete syntax non-fatal error.
34 fn obsolete(&mut self, sp: Span, kind: ObsoleteSyntax);
35 /// Reports an obsolete syntax non-fatal error, and returns
36 /// a placeholder expression
37 fn obsolete_expr(&mut self, sp: Span, kind: ObsoleteSyntax) -> P<Expr>;
43 fn is_obsolete_ident(&mut self, ident: &str) -> bool;
44 fn eat_obsolete_ident(&mut self, ident: &str) -> bool;
47 impl<'a> ParserObsoleteMethods for parser::Parser<'a> {
48 /// Reports an obsolete syntax non-fatal error.
49 fn obsolete(&mut self, sp: Span, kind: ObsoleteSyntax) {
50 let (kind_str, desc) = match kind {
51 ObsoleteSyntax::ForSized => (
53 "no longer required. Traits (and their `Self` type) do not have the `Sized` bound \
56 ObsoleteSyntax::ProcType => (
58 "use unboxed closures instead",
60 ObsoleteSyntax::ProcExpr => (
62 "use a `move ||` expression instead",
64 ObsoleteSyntax::ClosureType => (
65 "`|usize| -> bool` closure type syntax",
66 "use unboxed closures instead, no type annotation needed"
68 ObsoleteSyntax::Sized => (
69 "`Sized? T` syntax for removing the `Sized` bound",
70 "write `T: ?Sized` instead"
74 self.report(sp, kind, kind_str, desc);
77 /// Reports an obsolete syntax non-fatal error, and returns
78 /// a placeholder expression
79 fn obsolete_expr(&mut self, sp: Span, kind: ObsoleteSyntax) -> P<Expr> {
80 self.obsolete(sp, kind);
81 self.mk_expr(sp.lo, sp.hi, ExprTup(vec![]))
90 &format!("obsolete syntax: {}", kind_str)[]);
92 if !self.obsolete_set.contains(&kind) {
96 .note(&format!("{}", desc)[]);
97 self.obsolete_set.insert(kind);
101 fn is_obsolete_ident(&mut self, ident: &str) -> bool {
103 token::Ident(sid, _) => {
104 token::get_ident(sid) == ident
110 fn eat_obsolete_ident(&mut self, ident: &str) -> bool {
111 if self.is_obsolete_ident(ident) {