Skip to content

Commit cc42134

Browse files
committed
syntax: don't parse numeric literals in the lexer
This removes a bunch of token types. Tokens now store the original, unaltered numeric literal (that is still checked for correctness), which is parsed into an actual number later, as needed, when creating the AST. This can change how syntax extensions work, but otherwise poses no visible changes. [breaking-change]
1 parent 9f5e21d commit cc42134

File tree

9 files changed

+325
-266
lines changed

9 files changed

+325
-266
lines changed

src/librustc/middle/trans/consts.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ use syntax::{ast, ast_util};
4242
pub fn const_lit(cx: &CrateContext, e: &ast::Expr, lit: ast::Lit)
4343
-> ValueRef {
4444
let _icx = push_ctxt("trans_lit");
45+
debug!("const_lit: {}", lit);
4546
match lit.node {
4647
ast::LitByte(b) => C_integral(Type::uint_from_ty(cx, ast::TyU8), b as u64, false),
4748
ast::LitChar(i) => C_integral(Type::char(cx), i as u64, false),

src/librustdoc/html/highlight.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,7 @@ fn doit(sess: &parse::ParseSess, mut lexer: lexer::StringReader,
144144
t::LIT_CHAR(..) | t::LIT_STR(..) | t::LIT_STR_RAW(..) => "string",
145145

146146
// number literals
147-
t::LIT_INT(..) | t::LIT_UINT(..) | t::LIT_INT_UNSUFFIXED(..) |
148-
t::LIT_FLOAT(..) | t::LIT_FLOAT_UNSUFFIXED(..) => "number",
147+
t::LIT_INTEGER(..) | t::LIT_FLOAT(..) => "number",
149148

150149
// keywords are also included in the identifier set
151150
t::IDENT(ident, _is_mod_sep) => {

src/libsyntax/ast.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -619,15 +619,15 @@ pub enum Mac_ {
619619
MacInvocTT(Path, Vec<TokenTree> , SyntaxContext), // new macro-invocation
620620
}
621621

622-
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
622+
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
623623
pub enum StrStyle {
624624
CookedStr,
625625
RawStr(uint)
626626
}
627627

628628
pub type Lit = Spanned<Lit_>;
629629

630-
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
630+
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
631631
pub enum Lit_ {
632632
LitStr(InternedString, StrStyle),
633633
LitBinary(Rc<Vec<u8> >),
@@ -697,6 +697,16 @@ impl fmt::Show for IntTy {
697697
}
698698
}
699699

700+
impl IntTy {
701+
pub fn suffix_len(&self) -> uint {
702+
match *self {
703+
TyI => 1,
704+
TyI8 => 2,
705+
TyI16 | TyI32 | TyI64 => 3,
706+
}
707+
}
708+
}
709+
700710
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
701711
pub enum UintTy {
702712
TyU,
@@ -706,6 +716,16 @@ pub enum UintTy {
706716
TyU64,
707717
}
708718

719+
impl UintTy {
720+
pub fn suffix_len(&self) -> uint {
721+
match *self {
722+
TyU => 1,
723+
TyU8 => 2,
724+
TyU16 | TyU32 | TyU64 => 3,
725+
}
726+
}
727+
}
728+
709729
impl fmt::Show for UintTy {
710730
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
711731
write!(f, "{}", ast_util::uint_ty_to_string(*self, None))
@@ -724,6 +744,14 @@ impl fmt::Show for FloatTy {
724744
}
725745
}
726746

747+
impl FloatTy {
748+
pub fn suffix_len(&self) -> uint {
749+
match *self {
750+
TyF32 | TyF64 => 3, // add F128 handling here
751+
}
752+
}
753+
}
754+
727755
// NB PartialEq method appears below.
728756
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
729757
pub struct Ty {

src/libsyntax/codemap.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ pub struct Span {
9696

9797
pub static DUMMY_SP: Span = Span { lo: BytePos(0), hi: BytePos(0), expn_info: None };
9898

99-
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
99+
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
100100
pub struct Spanned<T> {
101101
pub node: T,
102102
pub span: Span,

src/libsyntax/ext/quote.rs

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -412,45 +412,14 @@ fn mk_token(cx: &ExtCtxt, sp: Span, tok: &token::Token) -> Gc<ast::Expr> {
412412
return cx.expr_call(sp, mk_token_path(cx, sp, "LIT_CHAR"), vec!(e_char));
413413
}
414414

415-
LIT_INT(i, ity) => {
416-
let s_ity = match ity {
417-
ast::TyI => "TyI",
418-
ast::TyI8 => "TyI8",
419-
ast::TyI16 => "TyI16",
420-
ast::TyI32 => "TyI32",
421-
ast::TyI64 => "TyI64"
422-
};
423-
let e_ity = mk_ast_path(cx, sp, s_ity);
424-
let e_i64 = cx.expr_lit(sp, ast::LitInt(i, ast::TyI64));
425-
return cx.expr_call(sp, mk_token_path(cx, sp, "LIT_INT"), vec!(e_i64, e_ity));
415+
LIT_INTEGER(i) => {
416+
let e_int = mk_ident(cx, sp, i);
417+
return cx.expr_call(sp, mk_token_path(cx, sp, "LIT_INTEGER"), vec!(e_int));
426418
}
427419

428-
LIT_UINT(u, uty) => {
429-
let s_uty = match uty {
430-
ast::TyU => "TyU",
431-
ast::TyU8 => "TyU8",
432-
ast::TyU16 => "TyU16",
433-
ast::TyU32 => "TyU32",
434-
ast::TyU64 => "TyU64"
435-
};
436-
let e_uty = mk_ast_path(cx, sp, s_uty);
437-
let e_u64 = cx.expr_lit(sp, ast::LitUint(u, ast::TyU64));
438-
return cx.expr_call(sp, mk_token_path(cx, sp, "LIT_UINT"), vec!(e_u64, e_uty));
439-
}
440-
441-
LIT_INT_UNSUFFIXED(i) => {
442-
let e_i64 = cx.expr_lit(sp, ast::LitInt(i, ast::TyI64));
443-
return cx.expr_call(sp, mk_token_path(cx, sp, "LIT_INT_UNSUFFIXED"), vec!(e_i64));
444-
}
445-
446-
LIT_FLOAT(fident, fty) => {
447-
let s_fty = match fty {
448-
ast::TyF32 => "TyF32",
449-
ast::TyF64 => "TyF64",
450-
};
451-
let e_fty = mk_ast_path(cx, sp, s_fty);
420+
LIT_FLOAT(fident) => {
452421
let e_fident = mk_ident(cx, sp, fident);
453-
return cx.expr_call(sp, mk_token_path(cx, sp, "LIT_FLOAT"), vec!(e_fident, e_fty));
422+
return cx.expr_call(sp, mk_token_path(cx, sp, "LIT_FLOAT"), vec!(e_fident));
454423
}
455424

456425
LIT_STR(ident) => {

0 commit comments

Comments
 (0)