Skip to content

Commit ac674e0

Browse files
committed
---
yaml --- r: 44425 b: refs/heads/master c: 25c4676 h: refs/heads/master i: 44423: 23079d8 v: v3
1 parent c7ea141 commit ac674e0

File tree

9 files changed

+120
-30
lines changed

9 files changed

+120
-30
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 6d09fc2cd80231aab527a27e8112e6167a15f042
2+
refs/heads/master: 25c4676dfa805e027564116b9c37fee7aeaf1cc4
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: a6d9689399d091c3265f00434a69c551a61c28dc
55
refs/heads/try: ef355f6332f83371e4acf04fc4eb940ab41d78d3

trunk/src/libcore/dvec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ impl<A> DVec<A> {
229229
230230
impl<A: Copy> DVec<A> {
231231
/**
232-
* Append all elements of a vector to the end of the list
232+
* Append all elements of a vector to the end of the list.
233233
*
234234
* Equivalent to `append_iter()` but potentially more efficient.
235235
*/

trunk/src/libsyntax/ast.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -779,10 +779,19 @@ pub enum expr_ {
779779
#[auto_decode]
780780
#[doc="For macro invocations; parsing is delegated to the macro"]
781781
pub enum token_tree {
782+
// a single token
782783
tt_tok(span, ::parse::token::Token),
784+
// a delimited sequence (the delimiters appear as the first
785+
// and last elements of the vector)
783786
tt_delim(~[token_tree]),
784-
// These only make sense for right-hand-sides of MBE macros
787+
// These only make sense for right-hand-sides of MBE macros:
788+
789+
// a kleene-style repetition sequence with a span, a tt_forest,
790+
// an optional separator (?), and a boolean where true indicates
791+
// zero or more (*), and false indicates one or more (+).
785792
tt_seq(span, ~[token_tree], Option<::parse::token::Token>, bool),
793+
794+
// a syntactic variable that will be filled in by macro expansion.
786795
tt_nonterminal(span, ident)
787796
}
788797

trunk/src/libsyntax/ext/base.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ pub struct SyntaxExpanderTT {
4444
span: Option<span>
4545
}
4646

47-
pub type SyntaxExpanderTTFun = fn@(ext_ctxt, span, ~[ast::token_tree])
48-
-> MacResult;
47+
pub type SyntaxExpanderTTFun
48+
= fn@(ext_ctxt, span, ~[ast::token_tree]) -> MacResult;
4949

5050
pub struct SyntaxExpanderTTItem {
5151
expander: SyntaxExpanderTTItemFun,
@@ -59,7 +59,7 @@ pub enum MacResult {
5959
MRExpr(@ast::expr),
6060
MRItem(@ast::item),
6161
MRAny(fn@()-> @ast::expr, fn@()-> Option<@ast::item>, fn@()->@ast::stmt),
62-
MRDef(MacroDef)
62+
MRDef(MacroDef),
6363
}
6464

6565
pub enum SyntaxExtension {
@@ -78,9 +78,11 @@ pub enum SyntaxExtension {
7878
// A temporary hard-coded map of methods for expanding syntax extension
7979
// AST nodes into full ASTs
8080
pub fn syntax_expander_table() -> HashMap<~str, SyntaxExtension> {
81+
// utility function to simplify creating NormalTT syntax extensions
8182
fn builtin_normal_tt(f: SyntaxExpanderTTFun) -> SyntaxExtension {
8283
NormalTT(SyntaxExpanderTT{expander: f, span: None})
8384
}
85+
// utility function to simplify creating ItemTT syntax extensions
8486
fn builtin_item_tt(f: SyntaxExpanderTTItemFun) -> SyntaxExtension {
8587
ItemTT(SyntaxExpanderTTItem{expander: f, span: None})
8688
}
@@ -112,8 +114,8 @@ pub fn syntax_expander_table() -> HashMap<~str, SyntaxExtension> {
112114
ext::deriving::expand_deriving_iter_bytes));
113115

114116
// Quasi-quoting expanders
115-
syntax_expanders.insert(
116-
~"quote_tokens", builtin_normal_tt(ext::quote::expand_quote_tokens));
117+
syntax_expanders.insert(~"quote_tokens",
118+
builtin_normal_tt(ext::quote::expand_quote_tokens));
117119
syntax_expanders.insert(~"quote_expr",
118120
builtin_normal_tt(ext::quote::expand_quote_expr));
119121
syntax_expanders.insert(~"quote_ty",

trunk/src/libsyntax/ext/tt/transcribe.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,15 @@ pure fn lookup_cur_matched_by_matched(r: @mut TtReader,
121121
vec::foldl(start, r.repeat_idx, red)
122122
}
123123

124-
fn lookup_cur_matched(r: @mut TtReader, name: ident) -> @named_match {
124+
fn lookup_cur_matched(r: &TtReader, name: ident) -> @named_match {
125125
lookup_cur_matched_by_matched(r, r.interpolations.get(&name))
126126
}
127127
enum lis {
128128
lis_unconstrained, lis_constraint(uint, ident), lis_contradiction(~str)
129129
}
130130

131-
fn lockstep_iter_size(t: token_tree, r: @mut TtReader) -> lis {
132-
fn lis_merge(lhs: lis, rhs: lis, r: @mut TtReader) -> lis {
131+
fn lockstep_iter_size(t: token_tree, r: &TtReader) -> lis {
132+
fn lis_merge(lhs: lis, rhs: lis, r: &TtReader) -> lis {
133133
match lhs {
134134
lis_unconstrained => rhs,
135135
lis_contradiction(_) => lhs,

trunk/src/libsyntax/parse/common.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ use core::option::{None, Option, Some};
2020
use core::option;
2121
use std::oldmap::HashMap;
2222

23+
// seq_sep : a sequence separator (token)
24+
// and whether a trailing separator is allowed.
2325
pub type seq_sep = {
2426
sep: Option<token::Token>,
2527
trailing_sep_allowed: bool
@@ -51,6 +53,8 @@ pub impl Parser {
5153
+ token_to_str(self.reader, self.token) + ~"`");
5254
}
5355

56+
// expect and consume the token t. Signal an error if
57+
// the next token is not t.
5458
fn expect(t: token::Token) {
5559
if self.token == t {
5660
self.bump();
@@ -88,6 +92,8 @@ pub impl Parser {
8892
return self.parse_ident();
8993
}
9094

95+
// consume token 'tok' if it exists. Returns true if the given
96+
// token was present, false otherwise.
9197
fn eat(tok: token::Token) -> bool {
9298
return if self.token == tok { self.bump(); true } else { false };
9399
}
@@ -185,6 +191,8 @@ pub impl Parser {
185191
}
186192
}
187193

194+
// expect and consume a GT. if a >> is seen, replace it
195+
// with a single > and continue.
188196
fn expect_gt() {
189197
if self.token == token::GT {
190198
self.bump();
@@ -202,16 +210,19 @@ pub impl Parser {
202210
}
203211
}
204212

213+
// parse a sequence bracketed by '<' and '>', stopping
214+
// before the '>'.
205215
fn parse_seq_to_before_gt<T: Copy>(sep: Option<token::Token>,
206216
f: fn(Parser) -> T) -> ~[T] {
207217
let mut first = true;
208218
let mut v = ~[];
209219
while self.token != token::GT
220+
// wait... isn't this going to eat a whole '>>' ?
210221
&& self.token != token::BINOP(token::SHR) {
211222
match sep {
212223
Some(ref t) => {
213224
if first { first = false; }
214-
else { self.expect((*t)); }
225+
else { self.expect(*t); }
215226
}
216227
_ => ()
217228
}
@@ -229,6 +240,7 @@ pub impl Parser {
229240
return v;
230241
}
231242

243+
// parse a sequence bracketed by '<' and '>'
232244
fn parse_seq_lt_gt<T: Copy>(sep: Option<token::Token>,
233245
f: fn(Parser) -> T) -> spanned<~[T]> {
234246
let lo = self.span.lo;
@@ -239,14 +251,19 @@ pub impl Parser {
239251
return spanned(lo, hi, result);
240252
}
241253

254+
// parse a sequence, including the closing delimiter. The function
255+
// f must consume tokens until reaching the next separator or
256+
// closing bracket.
242257
fn parse_seq_to_end<T: Copy>(ket: token::Token, sep: seq_sep,
243258
f: fn(Parser) -> T) -> ~[T] {
244259
let val = self.parse_seq_to_before_end(ket, sep, f);
245260
self.bump();
246261
return val;
247262
}
248263

249-
264+
// parse a sequence, not including the closing delimiter. The function
265+
// f must consume tokens until reaching the next separator or
266+
// closing bracket.
250267
fn parse_seq_to_before_end<T: Copy>(ket: token::Token, sep: seq_sep,
251268
f: fn(Parser) -> T) -> ~[T] {
252269
let mut first: bool = true;
@@ -255,7 +272,7 @@ pub impl Parser {
255272
match sep.sep {
256273
Some(ref t) => {
257274
if first { first = false; }
258-
else { self.expect((*t)); }
275+
else { self.expect(*t); }
259276
}
260277
_ => ()
261278
}
@@ -265,6 +282,9 @@ pub impl Parser {
265282
return v;
266283
}
267284

285+
// parse a sequence, including the closing delimiter. The function
286+
// f must consume tokens until reaching the next separator or
287+
// closing bracket.
268288
fn parse_unspanned_seq<T: Copy>(bra: token::Token,
269289
ket: token::Token,
270290
sep: seq_sep,

trunk/src/libsyntax/parse/mod.rs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,6 @@ pub fn new_parser_from_file(sess: parse_sess,
183183
let srdr = lexer::new_string_reader(sess.span_diagnostic,
184184
filemap,
185185
sess.interner);
186-
187186
Ok(Parser(sess, cfg, srdr as reader))
188187

189188
}
@@ -222,3 +221,49 @@ pub fn new_parser_from_tts(sess: parse_sess, cfg: ast::crate_cfg,
222221
return Parser(sess, cfg, trdr as reader)
223222
}
224223

224+
225+
#[cfg(test)]
226+
mod test {
227+
use super::*;
228+
use std::serialize::Encodable;
229+
use std;
230+
use core::dvec;
231+
use core::str;
232+
use util::testing::*;
233+
234+
#[test] fn to_json_str (val: Encodable<std::json::Encoder>) -> ~str {
235+
let bw = @io::BytesWriter {bytes: dvec::DVec(), pos: 0};
236+
val.encode(~std::json::Encoder(bw as io::Writer));
237+
str::from_bytes(bw.bytes.data)
238+
}
239+
240+
#[test] fn alltts () {
241+
let tts = parse_tts_from_source_str(
242+
~"bogofile",
243+
@~"fn foo (x : int) { x; }",
244+
~[],
245+
new_parse_sess(None));
246+
check_equal(to_json_str(tts as Encodable::<std::json::Encoder>),
247+
//[["tt_tok",["IDENT","fn"]]]
248+
~"abc"
249+
);
250+
let ast1 = new_parser_from_tts(new_parse_sess(None),~[],tts)
251+
.parse_item(~[]);
252+
let ast2 = parse_item_from_source_str(
253+
~"bogofile",
254+
@~"fn foo (x : int) { x; }",
255+
~[],~[],
256+
new_parse_sess(None));
257+
check_equal(ast1,ast2);
258+
}
259+
}
260+
261+
//
262+
// Local Variables:
263+
// mode: rust
264+
// fill-column: 78;
265+
// indent-tabs-mode: nil
266+
// c-basic-offset: 4
267+
// buffer-file-coding-system: utf-8-unix
268+
// End:
269+
//

trunk/src/libsyntax/parse/parser.rs

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ pure fn maybe_append(+lhs: ~[attribute], rhs: Option<~[attribute]>)
182182

183183
/* ident is handled by common.rs */
184184

185-
pub fn Parser(sess: parse_sess,
185+
pub fn Parser(sess: parse_sess
186+
,
186187
cfg: ast::crate_cfg,
187188
+rdr: reader) -> Parser {
188189

@@ -1238,6 +1239,8 @@ pub impl Parser {
12381239
return e;
12391240
}
12401241

1242+
// parse an optional separator followed by a kleene-style
1243+
// repetition token (+ or *).
12411244
fn parse_sep_and_zerok() -> (Option<token::Token>, bool) {
12421245
if self.token == token::BINOP(token::STAR)
12431246
|| self.token == token::BINOP(token::PLUS) {
@@ -1258,20 +1261,18 @@ pub impl Parser {
12581261
}
12591262
}
12601263

1264+
// parse a single token tree from the input.
12611265
fn parse_token_tree() -> token_tree {
12621266
maybe_whole!(deref self, nt_tt);
12631267

1264-
fn parse_tt_tok(p: Parser, delim_ok: bool) -> token_tree {
1268+
fn parse_non_delim_tt_tok(p: Parser) -> token_tree {
12651269
maybe_whole!(deref p, nt_tt);
12661270
match p.token {
12671271
token::RPAREN | token::RBRACE | token::RBRACKET
1268-
if !delim_ok => {
1272+
=> {
12691273
p.fatal(~"incorrect close delimiter: `"
12701274
+ token_to_str(p.reader, p.token) + ~"`");
12711275
}
1272-
token::EOF => {
1273-
p.fatal(~"file ended in the middle of a macro invocation");
1274-
}
12751276
/* we ought to allow different depths of unquotation */
12761277
token::DOLLAR if p.quote_depth > 0u => {
12771278
p.bump();
@@ -1282,32 +1283,43 @@ pub impl Parser {
12821283
seq_sep_none(),
12831284
|p| p.parse_token_tree());
12841285
let (s, z) = p.parse_sep_and_zerok();
1285-
return tt_seq(mk_sp(sp.lo ,p.span.hi), seq.node, s, z);
1286+
tt_seq(mk_sp(sp.lo ,p.span.hi), seq.node, s, z)
12861287
} else {
1287-
return tt_nonterminal(sp, p.parse_ident());
1288+
tt_nonterminal(sp, p.parse_ident())
12881289
}
12891290
}
1290-
_ => { /* ok */ }
1291+
_ => {
1292+
parse_any_tt_tok(p)
1293+
}
12911294
}
1295+
}
1296+
1297+
// turn the next token into a tt_tok:
1298+
fn parse_any_tt_tok(p: Parser) -> token_tree{
12921299
let res = tt_tok(p.span, p.token);
12931300
p.bump();
1294-
return res;
1301+
res
12951302
}
12961303

1297-
return match self.token {
1304+
match self.token {
1305+
token::EOF => {
1306+
self.fatal(~"file ended in the middle of a macro invocation");
1307+
}
12981308
token::LPAREN | token::LBRACE | token::LBRACKET => {
12991309
// tjc: ??????
13001310
let ket = token::flip_delimiter(copy self.token);
13011311
tt_delim(vec::append(
1302-
~[parse_tt_tok(self, true)],
1312+
// the open delimiter:
1313+
~[parse_any_tt_tok(self)],
13031314
vec::append(
13041315
self.parse_seq_to_before_end(
13051316
ket, seq_sep_none(),
13061317
|p| p.parse_token_tree()),
1307-
~[parse_tt_tok(self, true)])))
1318+
// the close delimiter:
1319+
~[parse_any_tt_tok(self)])))
13081320
}
1309-
_ => parse_tt_tok(self, false)
1310-
};
1321+
_ => parse_non_delim_tt_tok(self)
1322+
}
13111323
}
13121324

13131325
fn parse_all_token_trees() -> ~[token_tree] {
@@ -3999,6 +4011,7 @@ pub impl Parser {
39994011
}
40004012
}
40014013

4014+
40024015
//
40034016
// Local Variables:
40044017
// mode: rust

trunk/src/libsyntax/parse/token.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ pub enum Token {
8686
LIT_STR(ast::ident),
8787

8888
/* Name components */
89+
// an identifier contains an "is_mod_name" boolean.
8990
IDENT(ast::ident, bool),
9091
UNDERSCORE,
9192
LIFETIME(ast::ident),

0 commit comments

Comments
 (0)