Skip to content

Commit

Permalink
Allow soft failure of the macro parser.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulstansifer committed Jul 6, 2012
1 parent 7f9b1fb commit a8112f3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
29 changes: 18 additions & 11 deletions src/libsyntax/ext/tt/earley_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,13 @@ fn nameize(&&p_s: parse_sess, ms: ~[matcher], &&res: ~[@arb_depth])
ret ret_val;
}

enum parse_result {
success(hashmap<ident, @arb_depth>),
failure(codemap::span, str)
}

fn parse(sess: parse_sess, cfg: ast::crate_cfg, rdr: reader, ms: ~[matcher])
-> hashmap<ident,@arb_depth> {
-> parse_result {
let mut cur_eis = ~[];
vec::push(cur_eis, new_matcher_pos(ms, none, rdr.peek().sp.lo));

Expand Down Expand Up @@ -195,13 +200,14 @@ fn parse(sess: parse_sess, cfg: ast::crate_cfg, rdr: reader, ms: ~[matcher])

/* error messages here could be improved with links to orig. rules */
if tok == EOF {
if eof_eis.len() == 1u { /* success */
ret nameize(sess, ms,
vec::map(eof_eis[0u].matches, |dv| dv.pop()));
if eof_eis.len() == 1u {
ret success(
nameize(sess, ms,
vec::map(eof_eis[0u].matches, |dv| dv.pop())));
} else if eof_eis.len() > 1u {
rdr.fatal("Ambiguity: multiple successful parses");
ret failure(sp, "Ambiguity: multiple successful parses");
} else {
rdr.fatal("Unexpected end of macro invocation");
ret failure(sp, "Unexpected end of macro invocation");
}
} else {
if (bb_eis.len() > 0u && next_eis.len() > 0u)
Expand All @@ -210,12 +216,13 @@ fn parse(sess: parse_sess, cfg: ast::crate_cfg, rdr: reader, ms: ~[matcher])
alt ei.elts[ei.idx].node
{ mtc_bb(_,name,_) { *name } _ { fail; } }
}), " or ");
rdr.fatal(#fmt["Local ambiguity: multiple parsing options: \
built-in NTs %s or %u other options.",
nts, next_eis.len()]);
ret failure(sp, #fmt[
"Local ambiguity: multiple parsing options: \
built-in NTs %s or %u other options.",
nts, next_eis.len()]);
} else if (bb_eis.len() == 0u && next_eis.len() == 0u) {
rdr.fatal("No rules expected the token "
+ to_str(*rdr.interner(), tok));
failure(sp, "No rules expected the token "
+ to_str(*rdr.interner(), tok));
} else if (next_eis.len() > 0u) {
/* Now process the next token */
while(next_eis.len() > 0u) {
Expand Down
8 changes: 5 additions & 3 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1210,7 +1210,7 @@ class parser {

/* temporary */
fn parse_tt_mac_demo() -> @expr {

import ext::tt::earley_parser::{parse,success,failure};
let name_idx = @mut 0u;
let ms = self.parse_seq(token::LBRACE, token::RBRACE,
common::seq_sep_none(),
Expand All @@ -1225,8 +1225,10 @@ class parser {
self.reader.interner(), none, tts)
as reader;

let matches = ext::tt::earley_parser::parse
(self.sess, self.cfg, rdr, ms);
let matches = alt parse(self.sess, self.cfg, rdr, ms) {
success(m) { m }
failure(sp, msg) { self.span_fatal(sp,msg); }
};

let transcriber = ext::tt::transcribe::new_tt_reader
(self.reader.span_diag(), self.reader.interner(),
Expand Down

0 comments on commit a8112f3

Please sign in to comment.