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.
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};
26 /// The specific types of unsupported syntax
27 #[deriving(PartialEq, Eq, Hash)]
28 pub enum ObsoleteSyntax {
34 ObsoleteImportRenaming,
35 ObsoleteSubsliceMatch,
36 ObsoleteExternCrateRenaming,
39 pub trait ParserObsoleteMethods {
40 /// Reports an obsolete syntax non-fatal error.
41 fn obsolete(&mut self, sp: Span, kind: ObsoleteSyntax);
42 /// Reports an obsolete syntax non-fatal error, and returns
43 /// a placeholder expression
44 fn obsolete_expr(&mut self, sp: Span, kind: ObsoleteSyntax) -> P<Expr>;
50 fn is_obsolete_ident(&mut self, ident: &str) -> bool;
51 fn eat_obsolete_ident(&mut self, ident: &str) -> bool;
54 impl<'a> ParserObsoleteMethods for parser::Parser<'a> {
55 /// Reports an obsolete syntax non-fatal error.
56 fn obsolete(&mut self, sp: Span, kind: ObsoleteSyntax) {
57 let (kind_str, desc) = match kind {
58 ObsoleteOwnedType => (
59 "`~` notation for owned pointers",
60 "use `Box<T>` in `std::owned` instead"
62 ObsoleteOwnedExpr => (
63 "`~` notation for owned pointer allocation",
64 "use the `box` operator instead of `~`"
66 ObsoleteOwnedPattern => (
67 "`~` notation for owned pointer patterns",
68 "use the `box` operator instead of `~`"
70 ObsoleteOwnedVector => (
71 "`~[T]` is no longer a type",
72 "use the `Vec` type instead"
74 ObsoleteOwnedSelf => (
75 "`~self` is no longer supported",
76 "write `self: Box<Self>` instead"
78 ObsoleteImportRenaming => (
79 "`use foo = bar` syntax",
80 "write `use bar as foo` instead"
82 ObsoleteSubsliceMatch => (
83 "subslice match syntax",
84 "instead of `..xs`, write `xs..` in a pattern"
86 ObsoleteExternCrateRenaming => (
87 "`extern crate foo = bar` syntax",
88 "write `extern crate bar as foo` instead"
92 self.report(sp, kind, kind_str, desc);
95 /// Reports an obsolete syntax non-fatal error, and returns
96 /// a placeholder expression
97 fn obsolete_expr(&mut self, sp: Span, kind: ObsoleteSyntax) -> P<Expr> {
98 self.obsolete(sp, kind);
99 self.mk_expr(sp.lo, sp.hi, ExprLit(P(respan(sp, LitNil))))
104 kind: ObsoleteSyntax,
108 format!("obsolete syntax: {}", kind_str).as_slice());
110 if !self.obsolete_set.contains(&kind) {
114 .note(format!("{}", desc).as_slice());
115 self.obsolete_set.insert(kind);
119 fn is_obsolete_ident(&mut self, ident: &str) -> bool {
121 token::Ident(sid, _) => {
122 token::get_ident(sid).equiv(&ident)
128 fn eat_obsolete_ident(&mut self, ident: &str) -> bool {
129 if self.is_obsolete_ident(ident) {