mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-22 23:04:33 +00:00
syntax: Extract prec mod from parser mod
This commit is contained in:
parent
aa4278a3f5
commit
476d5a099d
@ -7,8 +7,8 @@ import util::interner;
|
||||
import ast::{node_id, spanned};
|
||||
import ast_util::{mk_sp, ident_to_path};
|
||||
import lexer::reader;
|
||||
import prec::{op_spec, binop_prec_table, as_prec};
|
||||
|
||||
export as_prec;
|
||||
export expr_requires_semi_to_be_stmt;
|
||||
export file_type;
|
||||
export mk_item;
|
||||
@ -31,9 +31,7 @@ export parse_pat;
|
||||
export parse_sess;
|
||||
export parse_stmt;
|
||||
export parse_ty;
|
||||
export prec_table;
|
||||
export stmt_ends_with_semi;
|
||||
export unop_prec;
|
||||
|
||||
enum restriction {
|
||||
UNRESTRICTED,
|
||||
@ -71,7 +69,7 @@ type parser = @{
|
||||
mut buffer: [{tok: token::token, span: span}],
|
||||
mut restriction: restriction,
|
||||
reader: reader,
|
||||
precs: @[op_spec],
|
||||
binop_precs: @[op_spec],
|
||||
bad_expr_words: hashmap<str, ()>
|
||||
};
|
||||
|
||||
@ -161,7 +159,7 @@ fn new_parser(sess: parse_sess, cfg: ast::crate_cfg, rdr: reader,
|
||||
mut buffer: [],
|
||||
mut restriction: UNRESTRICTED,
|
||||
reader: rdr,
|
||||
precs: prec_table(),
|
||||
binop_precs: binop_prec_table(),
|
||||
bad_expr_words: bad_expr_word_table()}
|
||||
}
|
||||
|
||||
@ -1301,41 +1299,11 @@ fn parse_prefix_expr(p: parser) -> pexpr {
|
||||
ret mk_pexpr(p, lo, hi, ex);
|
||||
}
|
||||
|
||||
type op_spec = {tok: token::token, op: ast::binop, prec: int};
|
||||
|
||||
|
||||
// FIXME make this a const, don't store it in parser state
|
||||
fn prec_table() -> @[op_spec] {
|
||||
ret @[{tok: token::BINOP(token::STAR), op: ast::mul, prec: 12},
|
||||
{tok: token::BINOP(token::SLASH), op: ast::div, prec: 12},
|
||||
{tok: token::BINOP(token::PERCENT), op: ast::rem, prec: 12},
|
||||
// 'as' sits between here with 11
|
||||
{tok: token::BINOP(token::PLUS), op: ast::add, prec: 10},
|
||||
{tok: token::BINOP(token::MINUS), op: ast::subtract, prec: 10},
|
||||
{tok: token::BINOP(token::LSL), op: ast::lsl, prec: 9},
|
||||
{tok: token::BINOP(token::LSR), op: ast::lsr, prec: 9},
|
||||
{tok: token::BINOP(token::ASR), op: ast::asr, prec: 9},
|
||||
{tok: token::BINOP(token::AND), op: ast::bitand, prec: 8},
|
||||
{tok: token::BINOP(token::CARET), op: ast::bitxor, prec: 7},
|
||||
{tok: token::BINOP(token::OR), op: ast::bitor, prec: 6},
|
||||
{tok: token::LT, op: ast::lt, prec: 4},
|
||||
{tok: token::LE, op: ast::le, prec: 4},
|
||||
{tok: token::GE, op: ast::ge, prec: 4},
|
||||
{tok: token::GT, op: ast::gt, prec: 4},
|
||||
{tok: token::EQEQ, op: ast::eq, prec: 3},
|
||||
{tok: token::NE, op: ast::ne, prec: 3},
|
||||
{tok: token::ANDAND, op: ast::and, prec: 2},
|
||||
{tok: token::OROR, op: ast::or, prec: 1}];
|
||||
}
|
||||
|
||||
fn parse_binops(p: parser) -> @ast::expr {
|
||||
ret parse_more_binops(p, parse_prefix_expr(p), 0);
|
||||
}
|
||||
|
||||
const unop_prec: int = 100;
|
||||
|
||||
const as_prec: int = 11;
|
||||
|
||||
fn parse_more_binops(p: parser, plhs: pexpr, min_prec: int) ->
|
||||
@ast::expr {
|
||||
let lhs = to_expr(plhs);
|
||||
@ -1343,7 +1311,7 @@ fn parse_more_binops(p: parser, plhs: pexpr, min_prec: int) ->
|
||||
let peeked = p.token;
|
||||
if peeked == token::BINOP(token::OR) &&
|
||||
p.restriction == RESTRICT_NO_BAR_OP { ret lhs; }
|
||||
for vec::each(*p.precs) {|cur|
|
||||
for vec::each(*p.binop_precs) {|cur|
|
||||
if cur.prec > min_prec && cur.tok == peeked {
|
||||
p.bump();
|
||||
let expr = parse_prefix_expr(p);
|
||||
|
40
src/librustsyntax/parse/prec.rs
Normal file
40
src/librustsyntax/parse/prec.rs
Normal file
@ -0,0 +1,40 @@
|
||||
export as_prec;
|
||||
export unop_prec;
|
||||
export binop_prec_table;
|
||||
export op_spec;
|
||||
|
||||
#[doc = "Unary operators have higher precedence than binary"]
|
||||
const unop_prec: int = 100;
|
||||
|
||||
#[doc = "
|
||||
Precedence of the `as` operator, which is a binary operator
|
||||
but is not represented in the precedence table.
|
||||
"]
|
||||
const as_prec: int = 11;
|
||||
|
||||
type op_spec = {tok: token::token, op: ast::binop, prec: int};
|
||||
|
||||
// FIXME make this a const, don't store it in parser state
|
||||
#[doc = "The precedence of binary operators"]
|
||||
fn binop_prec_table() -> @[op_spec] {
|
||||
ret @[{tok: token::BINOP(token::STAR), op: ast::mul, prec: 12},
|
||||
{tok: token::BINOP(token::SLASH), op: ast::div, prec: 12},
|
||||
{tok: token::BINOP(token::PERCENT), op: ast::rem, prec: 12},
|
||||
// 'as' sits between here with 11
|
||||
{tok: token::BINOP(token::PLUS), op: ast::add, prec: 10},
|
||||
{tok: token::BINOP(token::MINUS), op: ast::subtract, prec: 10},
|
||||
{tok: token::BINOP(token::LSL), op: ast::lsl, prec: 9},
|
||||
{tok: token::BINOP(token::LSR), op: ast::lsr, prec: 9},
|
||||
{tok: token::BINOP(token::ASR), op: ast::asr, prec: 9},
|
||||
{tok: token::BINOP(token::AND), op: ast::bitand, prec: 8},
|
||||
{tok: token::BINOP(token::CARET), op: ast::bitxor, prec: 7},
|
||||
{tok: token::BINOP(token::OR), op: ast::bitor, prec: 6},
|
||||
{tok: token::LT, op: ast::lt, prec: 4},
|
||||
{tok: token::LE, op: ast::le, prec: 4},
|
||||
{tok: token::GE, op: ast::ge, prec: 4},
|
||||
{tok: token::GT, op: ast::gt, prec: 4},
|
||||
{tok: token::EQEQ, op: ast::eq, prec: 3},
|
||||
{tok: token::NE, op: ast::ne, prec: 3},
|
||||
{tok: token::ANDAND, op: ast::and, prec: 2},
|
||||
{tok: token::OROR, op: ast::or, prec: 1}];
|
||||
}
|
@ -934,7 +934,7 @@ fn print_expr(s: ps, &&expr: @ast::expr) {
|
||||
}
|
||||
ast::expr_unary(op, expr) {
|
||||
word(s.s, ast_util::unop_to_str(op));
|
||||
print_op_maybe_parens(s, expr, parse::parser::unop_prec);
|
||||
print_op_maybe_parens(s, expr, parse::prec::unop_prec);
|
||||
}
|
||||
ast::expr_addr_of(m, expr) {
|
||||
word(s.s, "&");
|
||||
@ -943,7 +943,7 @@ fn print_expr(s: ps, &&expr: @ast::expr) {
|
||||
}
|
||||
ast::expr_lit(lit) { print_literal(s, lit); }
|
||||
ast::expr_cast(expr, ty) {
|
||||
print_op_maybe_parens(s, expr, parse::parser::as_prec);
|
||||
print_op_maybe_parens(s, expr, parse::prec::as_prec);
|
||||
space(s.s);
|
||||
word_space(s, "as");
|
||||
print_type(s, ty);
|
||||
@ -1488,7 +1488,7 @@ fn print_view_item(s: ps, item: @ast::view_item) {
|
||||
// FIXME: The fact that this builds up the table anew for every call is
|
||||
// not good. Eventually, table should be a const.
|
||||
fn operator_prec(op: ast::binop) -> int {
|
||||
for vec::each(*parse::parser::prec_table()) {|spec|
|
||||
for vec::each(*parse::prec::binop_prec_table()) {|spec|
|
||||
if spec.op == op { ret spec.prec; }
|
||||
}
|
||||
core::unreachable();
|
||||
@ -1497,7 +1497,7 @@ fn operator_prec(op: ast::binop) -> int {
|
||||
fn need_parens(expr: @ast::expr, outer_prec: int) -> bool {
|
||||
alt expr.node {
|
||||
ast::expr_binary(op, _, _) { operator_prec(op) < outer_prec }
|
||||
ast::expr_cast(_, _) { parse::parser::as_prec < outer_prec }
|
||||
ast::expr_cast(_, _) { parse::prec::as_prec < outer_prec }
|
||||
// This may be too conservative in some cases
|
||||
ast::expr_assign(_, _) { true }
|
||||
ast::expr_move(_, _) { true }
|
||||
|
@ -26,12 +26,16 @@ mod parse {
|
||||
export parser;
|
||||
export lexer;
|
||||
export comments;
|
||||
export prec;
|
||||
|
||||
mod eval;
|
||||
mod lexer;
|
||||
mod parser;
|
||||
mod token;
|
||||
mod comments;
|
||||
|
||||
#[doc = "Functions dealing with operator precedence"]
|
||||
mod prec;
|
||||
}
|
||||
|
||||
mod print {
|
||||
|
Loading…
Reference in New Issue
Block a user