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 {
33 pub trait ParserObsoleteMethods {
34 /// Reports an obsolete syntax non-fatal error.
35 fn obsolete(&mut self, sp: Span, kind: ObsoleteSyntax);
36 /// Reports an obsolete syntax non-fatal error, and returns
37 /// a placeholder expression
38 fn obsolete_expr(&mut self, sp: Span, kind: ObsoleteSyntax) -> P<Expr>;
44 fn is_obsolete_ident(&mut self, ident: &str) -> bool;
45 fn eat_obsolete_ident(&mut self, ident: &str) -> bool;
48 impl<'a> ParserObsoleteMethods for parser::Parser<'a> {
49 /// Reports an obsolete syntax non-fatal error.
50 fn obsolete(&mut self, sp: Span, kind: ObsoleteSyntax) {
51 let (kind_str, desc) = match kind {
52 ObsoleteSyntax::ForSized => (
54 "no longer required. Traits (and their `Self` type) do not have the `Sized` bound \
57 ObsoleteSyntax::ProcType => (
59 "use unboxed closures instead",
61 ObsoleteSyntax::ProcExpr => (
63 "use a `move ||` expression instead",
65 ObsoleteSyntax::ClosureType => (
66 "`|usize| -> bool` closure type",
67 "use unboxed closures instead, no type annotation needed"
69 ObsoleteSyntax::ClosureKind => (
70 "`:`, `&mut:`, or `&:`",
71 "rely on inference instead"
73 ObsoleteSyntax::Sized => (
74 "`Sized? T` for removing the `Sized` bound",
75 "write `T: ?Sized` instead"
79 self.report(sp, kind, kind_str, desc);
82 /// Reports an obsolete syntax non-fatal error, and returns
83 /// a placeholder expression
84 fn obsolete_expr(&mut self, sp: Span, kind: ObsoleteSyntax) -> P<Expr> {
85 self.obsolete(sp, kind);
86 self.mk_expr(sp.lo, sp.hi, ExprTup(vec![]))
95 &format!("obsolete syntax: {}", kind_str)[]);
97 if !self.obsolete_set.contains(&kind) {
101 .note(&format!("{}", desc)[]);
102 self.obsolete_set.insert(kind);
106 fn is_obsolete_ident(&mut self, ident: &str) -> bool {
108 token::Ident(sid, _) => {
109 token::get_ident(sid) == ident
115 fn eat_obsolete_ident(&mut self, ident: &str) -> bool {
116 if self.is_obsolete_ident(ident) {