Skip to content

Commit 49ac48d

Browse files
committedFeb 9, 2014
auto merge of #12034 : dguenther/rust/fourcc, r=alexcrichton
I was looking into #9303 and was curious if this would still be valuable. @kballard had already done 99% of the work, so I brought the branch up to date and added a feature gate. Any feedback would be appreciated; I wasn't sure if this should be set up as a syntax extension with `#[macro_registrar]`, and if so, where it should be located. Original PR is here: #9255 TODO: * [x] Convert to loadable syntax extension * [x] Default to big endian * [x] Add `target` identifier * [x] Expand to include code points 128-255
2 parents 58985e1 + 337e62e commit 49ac48d

10 files changed

+323
-3
lines changed
 

‎mk/crates.mk

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
################################################################################
5151

5252
TARGET_CRATES := std extra green rustuv native flate arena glob term semver \
53-
uuid serialize sync getopts collections
53+
uuid serialize sync getopts collections fourcc
5454
HOST_CRATES := syntax rustc rustdoc
5555
CRATES := $(TARGET_CRATES) $(HOST_CRATES)
5656
TOOLS := compiletest rustdoc rustc
@@ -74,6 +74,7 @@ DEPS_uuid := std serialize
7474
DEPS_sync := std
7575
DEPS_getopts := std
7676
DEPS_collections := std serialize
77+
DEPS_fourcc := syntax std
7778

7879
TOOL_DEPS_compiletest := extra green rustuv getopts
7980
TOOL_DEPS_rustdoc := rustdoc green rustuv

‎src/libfourcc/lib.rs

+160
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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 generate FourCCs.
13+
14+
Once loaded, fourcc!() is called with a single 4-character string,
15+
and an optional ident that is either `big`, `little`, or `target`.
16+
The ident represents endianness, and specifies in which direction
17+
the characters should be read. If the ident is omitted, it is assumed
18+
to be `big`, i.e. left-to-right order. It returns a u32.
19+
20+
# Examples
21+
22+
To load the extension and use it:
23+
24+
```rust,ignore
25+
#[phase(syntax)]
26+
extern mod fourcc;
27+
28+
fn main() {
29+
let val = fourcc!("\xC0\xFF\xEE!")
30+
// val is 0xC0FFEE21
31+
let big_val = fourcc!("foo ", big);
32+
// big_val is 0x21EEFFC0
33+
}
34+
```
35+
36+
# References
37+
38+
* [Wikipedia: FourCC](http://en.wikipedia.org/wiki/FourCC)
39+
40+
*/
41+
42+
#[crate_id = "fourcc#0.10-pre"];
43+
#[crate_type = "rlib"];
44+
#[crate_type = "dylib"];
45+
#[license = "MIT/ASL2"];
46+
47+
#[feature(macro_registrar, managed_boxes)];
48+
49+
extern mod syntax;
50+
51+
use syntax::ast;
52+
use syntax::ast::Name;
53+
use syntax::attr::contains;
54+
use syntax::codemap::{Span, mk_sp};
55+
use syntax::ext::base;
56+
use syntax::ext::base::{SyntaxExtension, BasicMacroExpander, NormalTT, ExtCtxt, MRExpr};
57+
use syntax::ext::build::AstBuilder;
58+
use syntax::parse;
59+
use syntax::parse::token;
60+
use syntax::parse::token::InternedString;
61+
62+
#[macro_registrar]
63+
#[cfg(not(test))]
64+
pub fn macro_registrar(register: |Name, SyntaxExtension|) {
65+
register(token::intern("fourcc"),
66+
NormalTT(~BasicMacroExpander {
67+
expander: expand_syntax_ext,
68+
span: None,
69+
},
70+
None));
71+
}
72+
73+
pub fn expand_syntax_ext(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]) -> base::MacResult {
74+
let (expr, endian) = parse_tts(cx, tts);
75+
76+
let little = match endian {
77+
None => false,
78+
Some(Ident{ident, span}) => match token::get_ident(ident.name).get() {
79+
"little" => true,
80+
"big" => false,
81+
"target" => target_endian_little(cx, sp),
82+
_ => {
83+
cx.span_err(span, "invalid endian directive in fourcc!");
84+
target_endian_little(cx, sp)
85+
}
86+
}
87+
};
88+
89+
let s = match expr.node {
90+
// expression is a literal
91+
ast::ExprLit(lit) => match lit.node {
92+
// string literal
93+
ast::LitStr(ref s, _) => {
94+
if s.get().char_len() != 4 {
95+
cx.span_err(expr.span, "string literal with len != 4 in fourcc!");
96+
}
97+
s
98+
}
99+
_ => {
100+
cx.span_err(expr.span, "unsupported literal in fourcc!");
101+
return MRExpr(cx.expr_lit(sp, ast::LitUint(0u64, ast::TyU32)));
102+
}
103+
},
104+
_ => {
105+
cx.span_err(expr.span, "non-literal in fourcc!");
106+
return MRExpr(cx.expr_lit(sp, ast::LitUint(0u64, ast::TyU32)));
107+
}
108+
};
109+
110+
let mut val = 0u32;
111+
for codepoint in s.get().chars().take(4) {
112+
let byte = if codepoint as u32 > 0xFF {
113+
cx.span_err(expr.span, "fourcc! literal character out of range 0-255");
114+
0u8
115+
} else {
116+
codepoint as u8
117+
};
118+
119+
val = if little {
120+
(val >> 8) | ((byte as u32) << 24)
121+
} else {
122+
(val << 8) | (byte as u32)
123+
};
124+
}
125+
let e = cx.expr_lit(sp, ast::LitUint(val as u64, ast::TyU32));
126+
MRExpr(e)
127+
}
128+
129+
struct Ident {
130+
ident: ast::Ident,
131+
span: Span
132+
}
133+
134+
fn parse_tts(cx: &ExtCtxt, tts: &[ast::TokenTree]) -> (@ast::Expr, Option<Ident>) {
135+
let p = &mut parse::new_parser_from_tts(cx.parse_sess(), cx.cfg(), tts.to_owned());
136+
let ex = p.parse_expr();
137+
let id = if p.token == token::EOF {
138+
None
139+
} else {
140+
p.expect(&token::COMMA);
141+
let lo = p.span.lo;
142+
let ident = p.parse_ident();
143+
let hi = p.last_span.hi;
144+
Some(Ident{ident: ident, span: mk_sp(lo, hi)})
145+
};
146+
if p.token != token::EOF {
147+
p.unexpected();
148+
}
149+
(ex, id)
150+
}
151+
152+
fn target_endian_little(cx: &ExtCtxt, sp: Span) -> bool {
153+
let meta = cx.meta_name_value(sp, InternedString::new("target_endian"),
154+
ast::LitStr(InternedString::new("little"), ast::CookedStr));
155+
contains(cx.cfg(), meta)
156+
}
157+
158+
// Fixes LLVM assert on Windows
159+
#[test]
160+
fn dummy_test() { }

‎src/librustc/front/feature_gate.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,13 @@ impl Visitor<()> for Context {
210210
self.gate_feature("log_syntax", path.span, "`log_syntax!` is not \
211211
stable enough for use and is subject to change");
212212
}
213+
213214
else if id == self.sess.ident_of("trace_macros") {
214215
self.gate_feature("trace_macros", path.span, "`trace_macros` is not \
215216
stable enough for use and is subject to change");
216-
} else {
217+
}
218+
219+
else {
217220
for &quote in quotes.iter() {
218221
if id == self.sess.ident_of(quote) {
219222
self.gate_feature("quote", path.span, quote + msg);

‎src/libsyntax/ext/base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2013 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+
// xfail-stage1
12+
// xfail-pretty
13+
// xfail-android
14+
15+
#[feature(phase)];
16+
17+
#[phase(syntax)]
18+
extern mod fourcc;
19+
20+
fn main() {
21+
let val = fourcc!("foo"); //~ ERROR string literal with len != 4 in fourcc!
22+
let val2 = fourcc!("fooba"); //~ ERROR string literal with len != 4 in fourcc!
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2013 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+
// xfail-stage1
12+
// xfail-pretty
13+
// xfail-android
14+
15+
#[feature(phase)];
16+
17+
#[phase(syntax)]
18+
extern mod fourcc;
19+
20+
fn main() {
21+
let val = fourcc!("foo ", bork); //~ ERROR invalid endian directive in fourcc!
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
// xfail-stage1
12+
// xfail-pretty
13+
// xfail-android
14+
15+
#[feature(phase)];
16+
17+
#[phase(syntax)]
18+
extern mod fourcc;
19+
20+
fn main() {
21+
let v = fourcc!("fooλ"); //~ ERROR fourcc! literal character out of range 0-255
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
// xfail-stage1
12+
// xfail-pretty
13+
// xfail-android
14+
15+
#[feature(phase)];
16+
17+
#[phase(syntax)]
18+
extern mod fourcc;
19+
20+
fn main() {
21+
let val = fourcc!(foo); //~ ERROR non-literal in fourcc!
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
// xfail-stage1
12+
// xfail-pretty
13+
// xfail-android
14+
15+
#[feature(phase)];
16+
17+
#[phase(syntax)]
18+
extern mod fourcc;
19+
20+
fn main() {
21+
let val = fourcc!(45f32); //~ ERROR unsupported literal in fourcc!
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
// xfail-fast Feature gating doesn't work
12+
// xfail-stage1
13+
// xfail-pretty
14+
// xfail-android
15+
16+
#[feature(phase)];
17+
18+
#[phase(syntax)]
19+
extern mod fourcc;
20+
21+
static static_val: u32 = fourcc!("foo ");
22+
static static_val_be: u32 = fourcc!("foo ", big);
23+
static static_val_le: u32 = fourcc!("foo ", little);
24+
static static_val_target: u32 = fourcc!("foo ", target);
25+
26+
fn main() {
27+
let val = fourcc!("foo ", big);
28+
assert_eq!(val, 0x666f6f20u32);
29+
assert_eq!(val, fourcc!("foo "));
30+
31+
let val = fourcc!("foo ", little);
32+
assert_eq!(val, 0x206f6f66u32);
33+
34+
let val = fourcc!("foo ", target);
35+
let exp = if cfg!(target_endian = "big") { 0x666f6f20u32 } else { 0x206f6f66u32 };
36+
assert_eq!(val, exp);
37+
38+
assert_eq!(static_val_be, 0x666f6f20u32);
39+
assert_eq!(static_val, static_val_be);
40+
assert_eq!(static_val_le, 0x206f6f66u32);
41+
let exp = if cfg!(target_endian = "big") { 0x666f6f20u32 } else { 0x206f6f66u32 };
42+
assert_eq!(static_val_target, exp);
43+
44+
assert_eq!(fourcc!("\xC0\xFF\xEE!"), 0xC0FFEE21);
45+
}

0 commit comments

Comments
 (0)