|
| 1 | +// Copyright 2014 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 | + |
| 11 | +/*! |
| 12 | +Syntax extension to create floating point literals from hexadecimal strings |
| 13 | +
|
| 14 | +Once loaded, hexfloat!() is called with a string containing the hexadecimal |
| 15 | +floating-point literal, and an optional type (f32 or f64). |
| 16 | +If the type is omitted, the literal is treated the same as a normal unsuffixed |
| 17 | +literal. |
| 18 | +
|
| 19 | +# Examples |
| 20 | +
|
| 21 | +To load the extension and use it: |
| 22 | +
|
| 23 | +```rust,ignore |
| 24 | +#[phase(syntax)] |
| 25 | +extern crate hexfloat; |
| 26 | +
|
| 27 | +fn main() { |
| 28 | + let val = hexfloat!("0x1.ffffb4", f32); |
| 29 | +} |
| 30 | + ``` |
| 31 | +
|
| 32 | +# References |
| 33 | +
|
| 34 | +* [ExploringBinary: hexadecimal floating point constants] |
| 35 | + (http://www.exploringbinary.com/hexadecimal-floating-point-constants/) |
| 36 | +
|
| 37 | +*/ |
| 38 | + |
| 39 | +#[crate_id = "hexfloat#0.10-pre"]; |
| 40 | +#[crate_type = "rlib"]; |
| 41 | +#[crate_type = "dylib"]; |
| 42 | +#[license = "MIT/ASL2"]; |
| 43 | + |
| 44 | +#[feature(macro_registrar, managed_boxes)]; |
| 45 | + |
| 46 | +extern crate syntax; |
| 47 | + |
| 48 | +use syntax::ast; |
| 49 | +use syntax::ast::Name; |
| 50 | +use syntax::codemap::{Span, mk_sp}; |
| 51 | +use syntax::ext::base; |
| 52 | +use syntax::ext::base::{SyntaxExtension, BasicMacroExpander, NormalTT, ExtCtxt, MRExpr}; |
| 53 | +use syntax::ext::build::AstBuilder; |
| 54 | +use syntax::parse; |
| 55 | +use syntax::parse::token; |
| 56 | + |
| 57 | +#[macro_registrar] |
| 58 | +pub fn macro_registrar(register: |Name, SyntaxExtension|) { |
| 59 | + register(token::intern("hexfloat"), |
| 60 | + NormalTT(~BasicMacroExpander { |
| 61 | + expander: expand_syntax_ext, |
| 62 | + span: None, |
| 63 | + }, |
| 64 | + None)); |
| 65 | +} |
| 66 | + |
| 67 | +//Check if the literal is valid (as LLVM expects), |
| 68 | +//and return a descriptive error if not. |
| 69 | +fn hex_float_lit_err(s: &str) -> Option<(uint, ~str)> { |
| 70 | + let mut chars = s.chars().peekable(); |
| 71 | + let mut i = 0; |
| 72 | + if chars.peek() == Some(&'-') { chars.next(); i+= 1 } |
| 73 | + if chars.next() != Some('0') { return Some((i, ~"Expected '0'")); } i+=1; |
| 74 | + if chars.next() != Some('x') { return Some((i, ~"Expected 'x'")); } i+=1; |
| 75 | + let mut d_len = 0; |
| 76 | + for _ in chars.take_while(|c| c.is_digit_radix(16)) { chars.next(); i+=1; d_len += 1;} |
| 77 | + if chars.next() != Some('.') { return Some((i, ~"Expected '.'")); } i+=1; |
| 78 | + let mut f_len = 0; |
| 79 | + for _ in chars.take_while(|c| c.is_digit_radix(16)) { chars.next(); i+=1; f_len += 1;} |
| 80 | + if d_len == 0 && f_len == 0 { |
| 81 | + return Some((i, ~"Expected digits before or after decimal point")); |
| 82 | + } |
| 83 | + if chars.next() != Some('p') { return Some((i, ~"Expected 'p'")); } i+=1; |
| 84 | + if chars.peek() == Some(&'-') { chars.next(); i+= 1 } |
| 85 | + let mut e_len = 0; |
| 86 | + for _ in chars.take_while(|c| c.is_digit()) { chars.next(); i+=1; e_len += 1} |
| 87 | + if e_len == 0 { |
| 88 | + return Some((i, ~"Expected exponent digits")); |
| 89 | + } |
| 90 | + match chars.next() { |
| 91 | + None => None, |
| 92 | + Some(_) => Some((i, ~"Expected end of string")) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +pub fn expand_syntax_ext(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]) -> base::MacResult { |
| 97 | + let (expr, ty_lit) = parse_tts(cx, tts); |
| 98 | + |
| 99 | + let ty = match ty_lit { |
| 100 | + None => None, |
| 101 | + Some(Ident{ident, span}) => match token::get_ident(ident).get() { |
| 102 | + "f32" => Some(ast::TyF32), |
| 103 | + "f64" => Some(ast::TyF64), |
| 104 | + _ => { |
| 105 | + cx.span_err(span, "invalid floating point type in hexfloat!"); |
| 106 | + None |
| 107 | + } |
| 108 | + } |
| 109 | + }; |
| 110 | + |
| 111 | + let s = match expr.node { |
| 112 | + // expression is a literal |
| 113 | + ast::ExprLit(lit) => match lit.node { |
| 114 | + // string literal |
| 115 | + ast::LitStr(ref s, _) => { |
| 116 | + s.clone() |
| 117 | + } |
| 118 | + _ => { |
| 119 | + cx.span_err(expr.span, "unsupported literal in hexfloat!"); |
| 120 | + return base::MacResult::dummy_expr(sp); |
| 121 | + } |
| 122 | + }, |
| 123 | + _ => { |
| 124 | + cx.span_err(expr.span, "non-literal in hexfloat!"); |
| 125 | + return base::MacResult::dummy_expr(sp); |
| 126 | + } |
| 127 | + }; |
| 128 | + |
| 129 | + { |
| 130 | + let err = hex_float_lit_err(s.get()); |
| 131 | + match err { |
| 132 | + Some((err_pos, err_str)) => { |
| 133 | + let pos = expr.span.lo + syntax::codemap::Pos::from_uint(err_pos + 1); |
| 134 | + let span = syntax::codemap::mk_sp(pos,pos); |
| 135 | + cx.span_err(span, format!("invalid hex float literal in hexfloat!: {}", err_str)); |
| 136 | + return base::MacResult::dummy_expr(sp); |
| 137 | + } |
| 138 | + _ => () |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + let lit = match ty { |
| 143 | + None => ast::LitFloatUnsuffixed(s), |
| 144 | + Some (ty) => ast::LitFloat(s, ty) |
| 145 | + }; |
| 146 | + MRExpr(cx.expr_lit(sp, lit)) |
| 147 | +} |
| 148 | + |
| 149 | +struct Ident { |
| 150 | + ident: ast::Ident, |
| 151 | + span: Span |
| 152 | +} |
| 153 | + |
| 154 | +fn parse_tts(cx: &ExtCtxt, tts: &[ast::TokenTree]) -> (@ast::Expr, Option<Ident>) { |
| 155 | + let p = &mut parse::new_parser_from_tts(cx.parse_sess(), |
| 156 | + cx.cfg(), |
| 157 | + tts.iter() |
| 158 | + .map(|x| (*x).clone()) |
| 159 | + .collect()); |
| 160 | + let ex = p.parse_expr(); |
| 161 | + let id = if p.token == token::EOF { |
| 162 | + None |
| 163 | + } else { |
| 164 | + p.expect(&token::COMMA); |
| 165 | + let lo = p.span.lo; |
| 166 | + let ident = p.parse_ident(); |
| 167 | + let hi = p.last_span.hi; |
| 168 | + Some(Ident{ident: ident, span: mk_sp(lo, hi)}) |
| 169 | + }; |
| 170 | + if p.token != token::EOF { |
| 171 | + p.unexpected(); |
| 172 | + } |
| 173 | + (ex, id) |
| 174 | +} |
| 175 | + |
| 176 | +// FIXME (10872): This is required to prevent an LLVM assert on Windows |
| 177 | +#[test] |
| 178 | +fn dummy_test() { } |
0 commit comments