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, LitNil};
21 use codemap::{Span, respan};
22 use parse::parser::Parser;
25 /// The specific types of unsupported syntax
26 #[deriving(Eq, TotalEq, Hash)]
27 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) -> @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<'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 ObsoleteOwnedType => (
53 "`~` notation for owned pointers",
54 "use `Box<T>` in `std::owned` instead"
56 ObsoleteOwnedExpr => (
57 "`~` notation for owned pointer allocation",
58 "use the `box` operator instead of `~`"
60 ObsoleteOwnedPattern => (
61 "`~` notation for owned pointer patterns",
62 "use the `box` operator instead of `~`"
66 self.report(sp, kind, kind_str, desc);
69 // Reports an obsolete syntax non-fatal error, and returns
70 // a placeholder expression
71 fn obsolete_expr(&mut self, sp: Span, kind: ObsoleteSyntax) -> @Expr {
72 self.obsolete(sp, kind);
73 self.mk_expr(sp.lo, sp.hi, ExprLit(@respan(sp, LitNil)))
81 self.span_err(sp, format!("obsolete syntax: {}", kind_str));
83 if !self.obsolete_set.contains(&kind) {
84 self.sess.span_diagnostic.handler().note(format!("{}", desc));
85 self.obsolete_set.insert(kind);
89 fn is_obsolete_ident(&mut self, ident: &str) -> bool {
91 token::IDENT(sid, _) => {
92 token::get_ident(sid).equiv(&ident)
98 fn eat_obsolete_ident(&mut self, ident: &str) -> bool {
99 if self.is_obsolete_ident(ident) {