Skip to content

Commit 97078d4

Browse files
committed
Converted fourcc! to loadable syntax extension
1 parent c1cc7e5 commit 97078d4

12 files changed

+225
-118
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

+158
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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+
use std::ascii::AsciiCast;
74+
75+
pub fn expand_syntax_ext(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]) -> base::MacResult {
76+
let (expr, endian) = parse_tts(cx, tts);
77+
78+
let little = match endian {
79+
None => target_endian_little(cx, sp),
80+
Some(Ident{ident, span}) => match token::get_ident(ident.name).get() {
81+
"little" => true,
82+
"big" => false,
83+
_ => {
84+
cx.span_err(span, "invalid endian directive in fourcc!");
85+
target_endian_little(cx, sp)
86+
}
87+
}
88+
};
89+
90+
let s = match expr.node {
91+
// expression is a literal
92+
ast::ExprLit(lit) => match lit.node {
93+
// string literal
94+
ast::LitStr(ref s, _) => {
95+
if !s.get().is_ascii() {
96+
cx.span_err(expr.span, "non-ascii string literal in fourcc!");
97+
} else if s.get().len() != 4 {
98+
cx.span_err(expr.span, "string literal with len != 4 in fourcc!");
99+
}
100+
s
101+
}
102+
_ => {
103+
cx.span_err(expr.span, "unsupported literal in fourcc!");
104+
return MRExpr(cx.expr_lit(sp, ast::LitUint(0u64, ast::TyU32)));
105+
}
106+
},
107+
_ => {
108+
cx.span_err(expr.span, "non-literal in fourcc!");
109+
return MRExpr(cx.expr_lit(sp, ast::LitUint(0u64, ast::TyU32)));
110+
}
111+
};
112+
113+
let mut val = 0u32;
114+
if little {
115+
for byte in s.get().bytes_rev().take(4) {
116+
val = (val << 8) | (byte as u32);
117+
}
118+
} else {
119+
for byte in s.get().bytes().take(4) {
120+
val = (val << 8) | (byte as u32);
121+
}
122+
}
123+
let e = cx.expr_lit(sp, ast::LitUint(val as u64, ast::TyU32));
124+
MRExpr(e)
125+
}
126+
127+
struct Ident {
128+
ident: ast::Ident,
129+
span: Span
130+
}
131+
132+
fn parse_tts(cx: &ExtCtxt, tts: &[ast::TokenTree]) -> (@ast::Expr, Option<Ident>) {
133+
let p = &mut parse::new_parser_from_tts(cx.parse_sess(), cx.cfg(), tts.to_owned());
134+
let ex = p.parse_expr();
135+
let id = if p.token == token::EOF {
136+
None
137+
} else {
138+
p.expect(&token::COMMA);
139+
let lo = p.span.lo;
140+
let ident = p.parse_ident();
141+
let hi = p.last_span.hi;
142+
Some(Ident{ident: ident, span: mk_sp(lo, hi)})
143+
};
144+
if p.token != token::EOF {
145+
p.unexpected();
146+
}
147+
(ex, id)
148+
}
149+
150+
fn target_endian_little(cx: &ExtCtxt, sp: Span) -> bool {
151+
let meta = cx.meta_name_value(sp, InternedString::new("target_endian"),
152+
ast::LitStr(InternedString::new("little"), ast::CookedStr));
153+
contains(cx.cfg(), meta)
154+
}
155+
156+
// Fixes LLVM assert on Windows
157+
#[test]
158+
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-4
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
//
@@ -194,9 +194,6 @@ pub fn syntax_expander_table() -> SyntaxEnv {
194194
syntax_expanders.insert(intern("bytes"),
195195
builtin_normal_expander(
196196
ext::bytes::expand_syntax_ext));
197-
syntax_expanders.insert(intern("fourcc"),
198-
builtin_normal_tt_no_ctxt(
199-
ext::fourcc::expand_syntax_ext));
200197
syntax_expanders.insert(intern("concat_idents"),
201198
builtin_normal_expander(
202199
ext::concat_idents::expand_syntax_ext));

src/libsyntax/ext/fourcc.rs

-106
This file was deleted.

src/libsyntax/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ pub mod ext {
9595
pub mod bytes;
9696
pub mod concat;
9797
pub mod concat_idents;
98-
pub mod fourcc;
9998
pub mod log_syntax;
10099
pub mod source_util;
101100

src/test/compile-fail/syntax-extension-fourcc-bad-len.rs

+9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// xfail-stage1
12+
// xfail-pretty
13+
// xfail-android
14+
15+
#[feature(phase)];
16+
17+
#[phase(syntax)]
18+
extern mod fourcc;
19+
1120
fn main() {
1221
let val = fourcc!("foo"); //~ ERROR string literal with len != 4 in fourcc!
1322
let val2 = fourcc!("fooba"); //~ ERROR string literal with len != 4 in fourcc!

src/test/compile-fail/syntax-extension-fourcc-invalid-endian.rs

+9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// xfail-stage1
12+
// xfail-pretty
13+
// xfail-android
14+
15+
#[feature(phase)];
16+
17+
#[phase(syntax)]
18+
extern mod fourcc;
19+
1120
fn main() {
1221
let val = fourcc!("foo ", bork); //~ ERROR invalid endian directive in fourcc!
1322
}

src/test/compile-fail/syntax-extension-fourcc-non-ascii-str.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 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
//
@@ -8,6 +8,15 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// xfail-stage1
12+
// xfail-pretty
13+
// xfail-android
14+
15+
#[feature(phase)];
16+
17+
#[phase(syntax)]
18+
extern mod fourcc;
19+
1120
fn main() {
1221
let v = fourcc!("fooλ"); //~ ERROR non-ascii string literal in fourcc!
1322
}

src/test/compile-fail/syntax-extension-fourcc-non-literal.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 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
//
@@ -8,6 +8,15 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// xfail-stage1
12+
// xfail-pretty
13+
// xfail-android
14+
15+
#[feature(phase)];
16+
17+
#[phase(syntax)]
18+
extern mod fourcc;
19+
1120
fn main() {
1221
let val = fourcc!(foo); //~ ERROR non-literal in fourcc!
1322
}

0 commit comments

Comments
 (0)