Skip to content

Commit 71c0873

Browse files
committed
Move ExprPrecedence to libsyntax/util/parser.rs
1 parent afe8d13 commit 71c0873

File tree

3 files changed

+128
-136
lines changed

3 files changed

+128
-136
lines changed

src/librustc/hir/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use syntax::ptr::P;
4141
use syntax::symbol::{Symbol, keywords};
4242
use syntax::tokenstream::TokenStream;
4343
use syntax::util::ThinVec;
44-
use syntax::ast::ExprPrecedence;
44+
use syntax::util::parser::ExprPrecedence;
4545
use ty::AdtKind;
4646

4747
use rustc_data_structures::indexed_vec;

src/libsyntax/ast.rs

+1-135
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,7 @@ pub use self::UnsafeSource::*;
1515
pub use self::PathParameters::*;
1616
pub use symbol::{Ident, Symbol as Name};
1717
pub use util::ThinVec;
18-
pub use util::parser::{
19-
AssocOp,
20-
PREC_RESET,
21-
PREC_CLOSURE,
22-
PREC_JUMP,
23-
PREC_RANGE,
24-
PREC_PREFIX,
25-
PREC_POSTFIX,
26-
PREC_PAREN,
27-
PREC_FORCE_PAREN,
28-
};
18+
pub use util::parser::ExprPrecedence;
2919

3020
use syntax_pos::{Span, DUMMY_SP};
3121
use codemap::{respan, Spanned};
@@ -39,7 +29,6 @@ use tokenstream::{ThinTokenStream, TokenStream};
3929

4030
use serialize::{self, Encoder, Decoder};
4131
use std::collections::HashSet;
42-
use std::cmp::Ordering;
4332
use std::fmt;
4433
use std::rc::Rc;
4534
use std::u32;
@@ -917,129 +906,6 @@ pub struct Expr {
917906
pub attrs: ThinVec<Attribute>
918907
}
919908

920-
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
921-
pub enum ExprPrecedence {
922-
Closure,
923-
Break,
924-
Continue,
925-
Ret,
926-
Yield,
927-
928-
Range,
929-
930-
Binary(BinOpKind),
931-
932-
InPlace,
933-
Cast,
934-
Type,
935-
936-
Assign,
937-
AssignOp,
938-
939-
Box,
940-
AddrOf,
941-
Unary,
942-
943-
Call,
944-
MethodCall,
945-
Field,
946-
TupField,
947-
Index,
948-
Try,
949-
InlineAsm,
950-
Mac,
951-
952-
Array,
953-
Repeat,
954-
Tup,
955-
Lit,
956-
Path,
957-
Paren,
958-
If,
959-
IfLet,
960-
While,
961-
WhileLet,
962-
ForLoop,
963-
Loop,
964-
Match,
965-
Block,
966-
Catch,
967-
Struct,
968-
}
969-
970-
impl PartialOrd for ExprPrecedence {
971-
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
972-
Some(self.order().cmp(&other.order()))
973-
}
974-
}
975-
976-
impl Ord for ExprPrecedence {
977-
fn cmp(&self, other: &Self) -> Ordering {
978-
self.order().cmp(&other.order())
979-
}
980-
}
981-
982-
impl ExprPrecedence {
983-
pub fn order(self) -> i8 {
984-
match self {
985-
ExprPrecedence::Closure => PREC_CLOSURE,
986-
987-
ExprPrecedence::Break |
988-
ExprPrecedence::Continue |
989-
ExprPrecedence::Ret |
990-
ExprPrecedence::Yield => PREC_JUMP,
991-
992-
// `Range` claims to have higher precedence than `Assign`, but `x .. x = x` fails to
993-
// parse, instead of parsing as `(x .. x) = x`. Giving `Range` a lower precedence
994-
// ensures that `pprust` will add parentheses in the right places to get the desired
995-
// parse.
996-
ExprPrecedence::Range => PREC_RANGE,
997-
998-
// Binop-like expr kinds, handled by `AssocOp`.
999-
ExprPrecedence::Binary(op) => AssocOp::from_ast_binop(op).precedence() as i8,
1000-
ExprPrecedence::InPlace => AssocOp::Inplace.precedence() as i8,
1001-
ExprPrecedence::Cast => AssocOp::As.precedence() as i8,
1002-
ExprPrecedence::Type => AssocOp::Colon.precedence() as i8,
1003-
1004-
ExprPrecedence::Assign |
1005-
ExprPrecedence::AssignOp => AssocOp::Assign.precedence() as i8,
1006-
1007-
// Unary, prefix
1008-
ExprPrecedence::Box |
1009-
ExprPrecedence::AddrOf |
1010-
ExprPrecedence::Unary => PREC_PREFIX,
1011-
1012-
// Unary, postfix
1013-
ExprPrecedence::Call |
1014-
ExprPrecedence::MethodCall |
1015-
ExprPrecedence::Field |
1016-
ExprPrecedence::TupField |
1017-
ExprPrecedence::Index |
1018-
ExprPrecedence::Try |
1019-
ExprPrecedence::InlineAsm |
1020-
ExprPrecedence::Mac => PREC_POSTFIX,
1021-
1022-
// Never need parens
1023-
ExprPrecedence::Array |
1024-
ExprPrecedence::Repeat |
1025-
ExprPrecedence::Tup |
1026-
ExprPrecedence::Lit |
1027-
ExprPrecedence::Path |
1028-
ExprPrecedence::Paren |
1029-
ExprPrecedence::If |
1030-
ExprPrecedence::IfLet |
1031-
ExprPrecedence::While |
1032-
ExprPrecedence::WhileLet |
1033-
ExprPrecedence::ForLoop |
1034-
ExprPrecedence::Loop |
1035-
ExprPrecedence::Match |
1036-
ExprPrecedence::Block |
1037-
ExprPrecedence::Catch |
1038-
ExprPrecedence::Struct => PREC_PAREN,
1039-
}
1040-
}
1041-
}
1042-
1043909
impl Expr {
1044910
/// Wether this expression would be valid somewhere that expects a value, for example, an `if`
1045911
/// condition.

src/libsyntax/util/parser.rs

+126
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use parse::token::{Token, BinOpToken};
1111
use symbol::keywords;
1212
use ast::{self, BinOpKind};
1313

14+
use std::cmp::Ordering;
15+
1416
/// Associative operator with precedence.
1517
///
1618
/// This is the enum which specifies operator precedence and fixity to the parser.
@@ -228,6 +230,130 @@ pub const PREC_POSTFIX: i8 = 60;
228230
pub const PREC_PAREN: i8 = 99;
229231
pub const PREC_FORCE_PAREN: i8 = 100;
230232

233+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
234+
pub enum ExprPrecedence {
235+
Closure,
236+
Break,
237+
Continue,
238+
Ret,
239+
Yield,
240+
241+
Range,
242+
243+
Binary(BinOpKind),
244+
245+
InPlace,
246+
Cast,
247+
Type,
248+
249+
Assign,
250+
AssignOp,
251+
252+
Box,
253+
AddrOf,
254+
Unary,
255+
256+
Call,
257+
MethodCall,
258+
Field,
259+
TupField,
260+
Index,
261+
Try,
262+
InlineAsm,
263+
Mac,
264+
265+
Array,
266+
Repeat,
267+
Tup,
268+
Lit,
269+
Path,
270+
Paren,
271+
If,
272+
IfLet,
273+
While,
274+
WhileLet,
275+
ForLoop,
276+
Loop,
277+
Match,
278+
Block,
279+
Catch,
280+
Struct,
281+
}
282+
283+
impl PartialOrd for ExprPrecedence {
284+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
285+
Some(self.order().cmp(&other.order()))
286+
}
287+
}
288+
289+
impl Ord for ExprPrecedence {
290+
fn cmp(&self, other: &Self) -> Ordering {
291+
self.order().cmp(&other.order())
292+
}
293+
}
294+
295+
impl ExprPrecedence {
296+
pub fn order(self) -> i8 {
297+
match self {
298+
ExprPrecedence::Closure => PREC_CLOSURE,
299+
300+
ExprPrecedence::Break |
301+
ExprPrecedence::Continue |
302+
ExprPrecedence::Ret |
303+
ExprPrecedence::Yield => PREC_JUMP,
304+
305+
// `Range` claims to have higher precedence than `Assign`, but `x .. x = x` fails to
306+
// parse, instead of parsing as `(x .. x) = x`. Giving `Range` a lower precedence
307+
// ensures that `pprust` will add parentheses in the right places to get the desired
308+
// parse.
309+
ExprPrecedence::Range => PREC_RANGE,
310+
311+
// Binop-like expr kinds, handled by `AssocOp`.
312+
ExprPrecedence::Binary(op) => AssocOp::from_ast_binop(op).precedence() as i8,
313+
ExprPrecedence::InPlace => AssocOp::Inplace.precedence() as i8,
314+
ExprPrecedence::Cast => AssocOp::As.precedence() as i8,
315+
ExprPrecedence::Type => AssocOp::Colon.precedence() as i8,
316+
317+
ExprPrecedence::Assign |
318+
ExprPrecedence::AssignOp => AssocOp::Assign.precedence() as i8,
319+
320+
// Unary, prefix
321+
ExprPrecedence::Box |
322+
ExprPrecedence::AddrOf |
323+
ExprPrecedence::Unary => PREC_PREFIX,
324+
325+
// Unary, postfix
326+
ExprPrecedence::Call |
327+
ExprPrecedence::MethodCall |
328+
ExprPrecedence::Field |
329+
ExprPrecedence::TupField |
330+
ExprPrecedence::Index |
331+
ExprPrecedence::Try |
332+
ExprPrecedence::InlineAsm |
333+
ExprPrecedence::Mac => PREC_POSTFIX,
334+
335+
// Never need parens
336+
ExprPrecedence::Array |
337+
ExprPrecedence::Repeat |
338+
ExprPrecedence::Tup |
339+
ExprPrecedence::Lit |
340+
ExprPrecedence::Path |
341+
ExprPrecedence::Paren |
342+
ExprPrecedence::If |
343+
ExprPrecedence::IfLet |
344+
ExprPrecedence::While |
345+
ExprPrecedence::WhileLet |
346+
ExprPrecedence::ForLoop |
347+
ExprPrecedence::Loop |
348+
ExprPrecedence::Match |
349+
ExprPrecedence::Block |
350+
ExprPrecedence::Catch |
351+
ExprPrecedence::Struct => PREC_PAREN,
352+
}
353+
}
354+
}
355+
356+
231357
/// Expressions that syntactically contain an "exterior" struct literal i.e. not surrounded by any
232358
/// parens or other delimiters, e.g. `X { y: 1 }`, `X { y: 1 }.method()`, `foo == X { y: 1 }` and
233359
/// `X { y: 1 } == foo` all do, but `(X { y: 1 }) == foo` does not.

0 commit comments

Comments
 (0)