]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/parse/classify.rs
doc/guide-ffi: A few minor typo/language fixes
[rust.git] / src / libsyntax / parse / classify.rs
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.
4 //
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.
10
11 //! Routines the parser uses to classify AST nodes
12
13 // Predicates on exprs and stmts that the pretty-printer and parser use
14
15 use ast;
16 use std::gc::Gc;
17
18 // does this expression require a semicolon to be treated
19 // as a statement? The negation of this: 'can this expression
20 // be used as a statement without a semicolon' -- is used
21 // as an early-bail-out in the parser so that, for instance,
22 // 'if true {...} else {...}
23 //  |x| 5 '
24 // isn't parsed as (if true {...} else {...} | x) | 5
25 pub fn expr_requires_semi_to_be_stmt(e: Gc<ast::Expr>) -> bool {
26     match e.node {
27         ast::ExprIf(..)
28         | ast::ExprMatch(..)
29         | ast::ExprBlock(_)
30         | ast::ExprWhile(..)
31         | ast::ExprLoop(..)
32         | ast::ExprForLoop(..) => false,
33         _ => true
34     }
35 }
36
37 pub fn expr_is_simple_block(e: Gc<ast::Expr>) -> bool {
38     match e.node {
39         ast::ExprBlock(block) => block.rules == ast::DefaultBlock,
40       _ => false
41     }
42 }
43
44 // this statement requires a semicolon after it.
45 // note that in one case (stmt_semi), we've already
46 // seen the semicolon, and thus don't need another.
47 pub fn stmt_ends_with_semi(stmt: &ast::Stmt) -> bool {
48     return match stmt.node {
49         ast::StmtDecl(d, _) => {
50             match d.node {
51                 ast::DeclLocal(_) => true,
52                 ast::DeclItem(_) => false
53             }
54         }
55         ast::StmtExpr(e, _) => { expr_requires_semi_to_be_stmt(e) }
56         ast::StmtSemi(..) => { false }
57         ast::StmtMac(..) => { false }
58     }
59 }