diff options
Diffstat (limited to 'src/libsyntax')
| -rw-r--r-- | src/libsyntax/ast.rs | 38 | ||||
| -rw-r--r-- | src/libsyntax/ext/build.rs | 6 | ||||
| -rw-r--r-- | src/libsyntax/ext/bytes.rs | 11 | ||||
| -rw-r--r-- | src/libsyntax/ext/concat.rs | 9 | ||||
| -rw-r--r-- | src/libsyntax/ext/deriving/generic/mod.rs | 2 | ||||
| -rw-r--r-- | src/libsyntax/ext/quote.rs | 9 | ||||
| -rw-r--r-- | src/libsyntax/parse/mod.rs | 57 | ||||
| -rw-r--r-- | src/libsyntax/parse/parser.rs | 4 | ||||
| -rw-r--r-- | src/libsyntax/print/pprust.rs | 28 | 
9 files changed, 97 insertions, 67 deletions
| diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index e19d39a4dc5..19882fecaa9 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -18,6 +18,7 @@ use parse::token::{InternedString, str_to_ident}; use parse::token; use std::fmt; +use std::num::Zero; use std::fmt::Show; use std::option::Option; use std::rc::Rc; @@ -657,14 +658,45 @@ pub enum StrStyle { pub type Lit = Spanned<Lit_>; #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +pub enum Sign { + Minus, + Plus +} + +impl<T: PartialOrd+Zero> Sign { + pub fn new(n: T) -> Sign { + if n < Zero::zero() { + Minus + } else { + Plus + } + } +} + +#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +pub enum LitIntType { + SignedIntLit(IntTy, Sign), + UnsignedIntLit(UintTy), + UnsuffixedIntLit(Sign) +} + +impl LitIntType { + pub fn suffix_len(&self) -> uint { + match *self { + UnsuffixedIntLit(_) => 0, + SignedIntLit(s, _) => s.suffix_len(), + UnsignedIntLit(u) => u.suffix_len() + } + } +} + +#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] pub enum Lit_ { LitStr(InternedString, StrStyle), LitBinary(Rc<Vec<u8> >), LitByte(u8), LitChar(char), - LitInt(i64, IntTy), - LitUint(u64, UintTy), - LitIntUnsuffixed(i64), + LitInt(u64, LitIntType), LitFloat(InternedString, FloatTy), LitFloatUnsuffixed(InternedString), LitNil, diff --git a/src/libsyntax/ext/build.rs b/src/libsyntax/ext/build.rs index 33daefa3e06..5acb84cf852 100644 --- a/src/libsyntax/ext/build.rs +++ b/src/libsyntax/ext/build.rs @@ -626,13 +626,13 @@ impl<'a> AstBuilder for ExtCtxt<'a> { self.expr(sp, ast::ExprLit(box(GC) respan(sp, lit))) } fn expr_uint(&self, span: Span, i: uint) -> Gc<ast::Expr> { - self.expr_lit(span, ast::LitUint(i as u64, ast::TyU)) + self.expr_lit(span, ast::LitInt(i as u64, ast::UnsignedIntLit(ast::TyU))) } fn expr_int(&self, sp: Span, i: int) -> Gc<ast::Expr> { - self.expr_lit(sp, ast::LitInt(i as i64, ast::TyI)) + self.expr_lit(sp, ast::LitInt(i as u64, ast::SignedIntLit(ast::TyI, ast::Sign::new(i)))) } fn expr_u8(&self, sp: Span, u: u8) -> Gc<ast::Expr> { - self.expr_lit(sp, ast::LitUint(u as u64, ast::TyU8)) + self.expr_lit(sp, ast::LitInt(u as u64, ast::UnsignedIntLit(ast::TyU8))) } fn expr_bool(&self, sp: Span, value: bool) -> Gc<ast::Expr> { self.expr_lit(sp, ast::LitBool(value)) diff --git a/src/libsyntax/ext/bytes.rs b/src/libsyntax/ext/bytes.rs index ce13fa2a7c6..6ea55096348 100644 --- a/src/libsyntax/ext/bytes.rs +++ b/src/libsyntax/ext/bytes.rs @@ -47,7 +47,7 @@ pub fn expand_syntax_ext(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]) } // u8 literal, push to vector expression - ast::LitUint(v, ast::TyU8) => { + ast::LitInt(v, ast::UnsignedIntLit(ast::TyU8)) => { if v > 0xFF { cx.span_err(expr.span, "too large u8 literal in bytes!"); err = true; @@ -57,13 +57,14 @@ pub fn expand_syntax_ext(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]) } // integer literal, push to vector expression - ast::LitIntUnsuffixed(v) => { + ast::LitInt(_, ast::UnsuffixedIntLit(ast::Minus)) => { + cx.span_err(expr.span, "negative integer literal in bytes!"); + err = true; + } + ast::LitInt(v, ast::UnsuffixedIntLit(ast::Plus)) => { if v > 0xFF { cx.span_err(expr.span, "too large integer literal in bytes!"); err = true; - } else if v < 0 { - cx.span_err(expr.span, "negative integer literal in bytes!"); - err = true; } else { bytes.push(cx.expr_u8(expr.span, v as u8)); } diff --git a/src/libsyntax/ext/concat.rs b/src/libsyntax/ext/concat.rs index 670e38327d6..dd1153bf666 100644 --- a/src/libsyntax/ext/concat.rs +++ b/src/libsyntax/ext/concat.rs @@ -37,11 +37,14 @@ pub fn expand_syntax_ext(cx: &mut base::ExtCtxt, ast::LitChar(c) => { accumulator.push_char(c); } - ast::LitInt(i, _) | ast::LitIntUnsuffixed(i) => { + ast::LitInt(i, ast::UnsignedIntLit(_)) | + ast::LitInt(i, ast::SignedIntLit(_, ast::Plus)) | + ast::LitInt(i, ast::UnsuffixedIntLit(ast::Plus)) => { accumulator.push_str(format!("{}", i).as_slice()); } - ast::LitUint(u, _) => { - accumulator.push_str(format!("{}", u).as_slice()); + ast::LitInt(i, ast::SignedIntLit(_, ast::Minus)) | + ast::LitInt(i, ast::UnsuffixedIntLit(ast::Minus)) => { + accumulator.push_str(format!("-{}", i).as_slice()); } ast::LitNil => {} ast::LitBool(b) => { diff --git a/src/libsyntax/ext/deriving/generic/mod.rs b/src/libsyntax/ext/deriving/generic/mod.rs index 871f277a2da..9225e4414c4 100644 --- a/src/libsyntax/ext/deriving/generic/mod.rs +++ b/src/libsyntax/ext/deriving/generic/mod.rs @@ -996,7 +996,7 @@ impl<'a> MethodDef<'a> { let arms : Vec<ast::Arm> = variants.iter().enumerate() .map(|(index, &variant)| { let pat = variant_to_pat(cx, sp, &*variant); - let lit = ast::LitUint(index as u64, ast::TyU); + let lit = ast::LitInt(index as u64, ast::UnsignedIntLit(ast::TyU)); cx.arm(sp, vec![pat], cx.expr_lit(sp, lit)) }).collect(); diff --git a/src/libsyntax/ext/quote.rs b/src/libsyntax/ext/quote.rs index dcfb0198127..cc07b531258 100644 --- a/src/libsyntax/ext/quote.rs +++ b/src/libsyntax/ext/quote.rs @@ -189,16 +189,17 @@ pub mod rt { (signed, $t:ty, $tag:ident) => ( impl ToSource for $t { fn to_source(&self) -> String { - let lit = dummy_spanned(ast::LitInt(*self as i64, ast::$tag)); - pprust::lit_to_string(&lit) + let lit = ast::LitInt(*self as u64, ast::SignedIntLit(ast::$tag, + ast::Sign::new(*self))); + pprust::lit_to_string(&dummy_spanned(lit)) } } ); (unsigned, $t:ty, $tag:ident) => ( impl ToSource for $t { fn to_source(&self) -> String { - let lit = dummy_spanned(ast::LitUint(*self as u64, ast::$tag)); - pprust::lit_to_string(&lit) + let lit = ast::LitInt(*self as u64, ast::UnsignedIntLit(ast::$tag)); + pprust::lit_to_string(&dummy_spanned(lit)) } } ); diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs index a3e169cd511..8f960e37de2 100644 --- a/src/libsyntax/parse/mod.rs +++ b/src/libsyntax/parse/mod.rs @@ -515,31 +515,13 @@ pub fn integer_lit(s: &str, sd: &SpanHandler, sp: Span) -> ast::Lit_ { debug!("parse_integer_lit: {}", s); if s.len() == 1 { - return ast::LitIntUnsuffixed((s.char_at(0)).to_digit(10).unwrap() as i64); + let n = (s.char_at(0)).to_digit(10).unwrap(); + return ast::LitInt(n as u64, ast::UnsuffixedIntLit(ast::Sign::new(n))); } let mut base = 10; let orig = s; - - #[deriving(Show)] - enum Result { - Nothing, - Signed(ast::IntTy), - Unsigned(ast::UintTy) - } - - impl Result { - fn suffix_len(&self) -> uint { - match *self { - Nothing => 0, - Signed(s) => s.suffix_len(), - Unsigned(u) => u.suffix_len() - } - } - } - - let mut ty = Nothing; - + let mut ty = ast::UnsuffixedIntLit(ast::Plus); if s.char_at(0) == '0' { match s.char_at(1) { @@ -556,13 +538,13 @@ pub fn integer_lit(s: &str, sd: &SpanHandler, sp: Span) -> ast::Lit_ { let last = s.len() - 1; match s.char_at(last) { - 'i' => ty = Signed(ast::TyI), - 'u' => ty = Unsigned(ast::TyU), + 'i' => ty = ast::SignedIntLit(ast::TyI, ast::Plus), + 'u' => ty = ast::UnsignedIntLit(ast::TyU), '8' => { if s.len() > 2 { match s.char_at(last - 1) { - 'i' => ty = Signed(ast::TyI8), - 'u' => ty = Unsigned(ast::TyU8), + 'i' => ty = ast::SignedIntLit(ast::TyI8, ast::Plus), + 'u' => ty = ast::UnsignedIntLit(ast::TyU8), _ => { } } } @@ -570,8 +552,8 @@ pub fn integer_lit(s: &str, sd: &SpanHandler, sp: Span) -> ast::Lit_ { '6' => { if s.len() > 3 && s.char_at(last - 1) == '1' { match s.char_at(last - 2) { - 'i' => ty = Signed(ast::TyI16), - 'u' => ty = Unsigned(ast::TyU16), + 'i' => ty = ast::SignedIntLit(ast::TyI16, ast::Plus), + 'u' => ty = ast::UnsignedIntLit(ast::TyU16), _ => { } } } @@ -579,8 +561,8 @@ pub fn integer_lit(s: &str, sd: &SpanHandler, sp: Span) -> ast::Lit_ { '2' => { if s.len() > 3 && s.char_at(last - 1) == '3' { match s.char_at(last - 2) { - 'i' => ty = Signed(ast::TyI32), - 'u' => ty = Unsigned(ast::TyU32), + 'i' => ty = ast::SignedIntLit(ast::TyI32, ast::Plus), + 'u' => ty = ast::UnsignedIntLit(ast::TyU32), _ => { } } } @@ -588,8 +570,8 @@ pub fn integer_lit(s: &str, sd: &SpanHandler, sp: Span) -> ast::Lit_ { '4' => { if s.len() > 3 && s.char_at(last - 1) == '6' { match s.char_at(last - 2) { - 'i' => ty = Signed(ast::TyI64), - 'u' => ty = Unsigned(ast::TyU64), + 'i' => ty = ast::SignedIntLit(ast::TyI64, ast::Plus), + 'u' => ty = ast::UnsignedIntLit(ast::TyU64), _ => { } } } @@ -597,21 +579,22 @@ pub fn integer_lit(s: &str, sd: &SpanHandler, sp: Span) -> ast::Lit_ { _ => { } } - - s = s.slice_to(s.len() - ty.suffix_len()); - debug!("The suffix is {}, base {}, the new string is {}, the original \ string was {}", ty, base, s, orig); + s = s.slice_to(s.len() - ty.suffix_len()); + let res: u64 = match ::std::num::from_str_radix(s, base) { Some(r) => r, None => { sd.span_err(sp, "int literal is too large"); 0 } }; + // adjust the sign + let sign = ast::Sign::new(res); match ty { - Nothing => ast::LitIntUnsuffixed(res as i64), - Signed(t) => ast::LitInt(res as i64, t), - Unsigned(t) => ast::LitUint(res, t) + ast::SignedIntLit(t, _) => ast::LitInt(res, ast::SignedIntLit(t, sign)), + ast::UnsuffixedIntLit(_) => ast::LitInt(res, ast::UnsuffixedIntLit(sign)), + us@ast::UnsignedIntLit(_) => ast::LitInt(res, us) } } diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index de3be4f8f38..18b1d60d4e9 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -34,7 +34,7 @@ use ast::{Ident, NormalFn, Inherited, Item, Item_, ItemStatic}; use ast::{ItemEnum, ItemFn, ItemForeignMod, ItemImpl}; use ast::{ItemMac, ItemMod, ItemStruct, ItemTrait, ItemTy, Lit, Lit_}; use ast::{LitBool, LitChar, LitByte, LitBinary}; -use ast::{LitNil, LitStr, LitUint, Local, LocalLet}; +use ast::{LitNil, LitStr, LitInt, Local, LocalLet}; use ast::{MutImmutable, MutMutable, Mac_, MacInvocTT, Matcher, MatchNonterminal}; use ast::{MatchSeq, MatchTok, Method, MutTy, BiMul, Mutability}; use ast::{NamedField, UnNeg, NoReturn, UnNot, P, Pat, PatEnum}; @@ -1889,7 +1889,7 @@ impl<'a> Parser<'a> { pub fn mk_lit_u32(&mut self, i: u32) -> Gc<Expr> { let span = &self.span; let lv_lit = box(GC) codemap::Spanned { - node: LitUint(i as u64, TyU32), + node: LitInt(i as u64, ast::UnsignedIntLit(TyU32)), span: *span }; diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 675588a4460..095dca66164 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -2415,15 +2415,25 @@ impl<'a> State<'a> { word(&mut self.s, res.as_slice()) } ast::LitInt(i, t) => { - word(&mut self.s, - ast_util::int_ty_to_string(t, Some(i)).as_slice()) - } - ast::LitUint(u, t) => { - word(&mut self.s, - ast_util::uint_ty_to_string(t, Some(u)).as_slice()) - } - ast::LitIntUnsuffixed(i) => { - word(&mut self.s, format!("{}", i).as_slice()) + match t { + ast::SignedIntLit(st, ast::Plus) => { + word(&mut self.s, + ast_util::int_ty_to_string(st, Some(i as i64)).as_slice()) + } + ast::SignedIntLit(st, ast::Minus) => { + word(&mut self.s, + ast_util::int_ty_to_string(st, Some(-(i as i64))).as_slice()) + } + ast::UnsignedIntLit(ut) => { + word(&mut self.s, ast_util::uint_ty_to_string(ut, Some(i)).as_slice()) + } + ast::UnsuffixedIntLit(ast::Plus) => { + word(&mut self.s, format!("{}", i).as_slice()) + } + ast::UnsuffixedIntLit(ast::Minus) => { + word(&mut self.s, format!("-{}", i).as_slice()) + } + } } ast::LitFloat(ref f, t) => { word(&mut self.s, | 
