Skip to content

Commit 70a19d5

Browse files
committed
Implement bytes! syntax extension
1 parent c18e44b commit 70a19d5

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

src/libsyntax/ext/base.rs

+2
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ pub fn syntax_expander_table() -> SyntaxEnv {
139139
@SE(ItemDecorator(ext::auto_encode::expand_auto_decode)));
140140
syntax_expanders.insert(@~"env",
141141
builtin_normal_tt(ext::env::expand_syntax_ext));
142+
syntax_expanders.insert(@~"bytes",
143+
builtin_normal_tt(ext::bytes::expand_syntax_ext));
142144
syntax_expanders.insert(@~"concat_idents",
143145
builtin_normal_tt(
144146
ext::concat_idents::expand_syntax_ext));

src/libsyntax/ext/bytes.rs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
/* The compiler code necessary to support the bytes! extension. */
12+
13+
use ast;
14+
use codemap::span;
15+
use ext::base::*;
16+
use ext::base;
17+
use ext::build::{mk_u8, mk_slice_vec_e};
18+
19+
pub fn expand_syntax_ext(cx: @ext_ctxt, sp: span, tts: &[ast::token_tree])
20+
-> base::MacResult {
21+
let var = get_single_str_from_tts(cx, sp, tts, "bytes!");
22+
let mut bytes = ~[];
23+
for var.each |byte| {
24+
bytes.push(mk_u8(cx, sp, byte));
25+
}
26+
let e = mk_slice_vec_e(cx, sp, bytes);
27+
MRExpr(e)
28+
}

src/libsyntax/syntax.rc

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ pub mod ext {
7878

7979
pub mod fmt;
8080
pub mod env;
81+
pub mod bytes;
8182
pub mod concat_idents;
8283
pub mod log_syntax;
8384
pub mod auto_encode;

0 commit comments

Comments
 (0)