]> git.proxmox.com Git - rustc.git/blame - src/libsyntax/parse/classify.rs
Imported Upstream version 1.7.0+dfsg1
[rustc.git] / src / libsyntax / parse / classify.rs
CommitLineData
223e47cc
LB
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
1a4d82fc
JJ
11//! Routines the parser uses to classify AST nodes
12
13// Predicates on exprs and stmts that the pretty-printer and parser use
223e47cc
LB
14
15use ast;
223e47cc 16
1a4d82fc
JJ
17/// Does this expression require a semicolon to be treated
18/// as a statement? The negation of this: 'can this expression
19/// be used as a statement without a semicolon' -- is used
20/// as an early-bail-out in the parser so that, for instance,
21/// if true {...} else {...}
22/// |x| 5
23/// isn't parsed as (if true {...} else {...} | x) | 5
24pub fn expr_requires_semi_to_be_stmt(e: &ast::Expr) -> bool {
223e47cc 25 match e.node {
1a4d82fc
JJ
26 ast::ExprIf(..)
27 | ast::ExprIfLet(..)
28 | ast::ExprMatch(..)
29 | ast::ExprBlock(_)
30 | ast::ExprWhile(..)
31 | ast::ExprWhileLet(..)
32 | ast::ExprLoop(..)
33 | ast::ExprForLoop(..) => false,
34 _ => true
223e47cc
LB
35 }
36}
37
1a4d82fc 38pub fn expr_is_simple_block(e: &ast::Expr) -> bool {
223e47cc 39 match e.node {
1a4d82fc
JJ
40 ast::ExprBlock(ref block) => block.rules == ast::DefaultBlock,
41 _ => false
223e47cc
LB
42 }
43}
44
1a4d82fc
JJ
45/// this statement requires a semicolon after it.
46/// note that in one case (stmt_semi), we've already
47/// seen the semicolon, and thus don't need another.
48pub fn stmt_ends_with_semi(stmt: &ast::Stmt_) -> bool {
49 match *stmt {
50 ast::StmtDecl(ref d, _) => {
223e47cc 51 match d.node {
1a4d82fc
JJ
52 ast::DeclLocal(_) => true,
53 ast::DeclItem(_) => false
223e47cc
LB
54 }
55 }
92a42be0 56 ast::StmtExpr(ref e, _) => { expr_requires_semi_to_be_stmt(e) }
1a4d82fc
JJ
57 ast::StmtSemi(..) => { false }
58 ast::StmtMac(..) => { false }
223e47cc
LB
59 }
60}