De-@ NamedMatch.

This commit is contained in:
Eduard Burtescu 2014-03-27 16:52:27 +02:00
parent 8f226e5694
commit 83c4e25d93
3 changed files with 27 additions and 26 deletions

View File

@ -21,6 +21,7 @@ use parse::parser::{LifetimeAndTypesWithoutColons, Parser};
use parse::token::{Token, EOF, Nonterminal};
use parse::token;
use std::rc::Rc;
use collections::HashMap;
/* This is an Earley-like parser, without support for in-grammar nonterminals,
@ -102,7 +103,7 @@ pub struct MatcherPos {
sep: Option<Token>,
idx: uint,
up: Option<~MatcherPos>,
matches: Vec<Vec<@NamedMatch>>,
matches: Vec<Vec<Rc<NamedMatch>>>,
match_lo: uint, match_hi: uint,
sp_lo: BytePos,
}
@ -165,14 +166,14 @@ pub fn initial_matcher_pos(ms: Vec<Matcher> , sep: Option<Token>, lo: BytePos)
// ast::Matcher it was derived from.
pub enum NamedMatch {
MatchedSeq(Vec<@NamedMatch> , codemap::Span),
MatchedSeq(Vec<Rc<NamedMatch>>, codemap::Span),
MatchedNonterminal(Nonterminal)
}
pub fn nameize(p_s: &ParseSess, ms: &[Matcher], res: &[@NamedMatch])
-> HashMap<Ident, @NamedMatch> {
fn n_rec(p_s: &ParseSess, m: &Matcher, res: &[@NamedMatch],
ret_val: &mut HashMap<Ident, @NamedMatch>) {
pub fn nameize(p_s: &ParseSess, ms: &[Matcher], res: &[Rc<NamedMatch>])
-> HashMap<Ident, Rc<NamedMatch>> {
fn n_rec(p_s: &ParseSess, m: &Matcher, res: &[Rc<NamedMatch>],
ret_val: &mut HashMap<Ident, Rc<NamedMatch>>) {
match *m {
codemap::Spanned {node: MatchTok(_), .. } => (),
codemap::Spanned {node: MatchSeq(ref more_ms, _, _, _, _), .. } => {
@ -189,7 +190,7 @@ pub fn nameize(p_s: &ParseSess, ms: &[Matcher], res: &[@NamedMatch])
p_s.span_diagnostic
.span_fatal(span, "duplicated bind name: " + string.get())
}
ret_val.insert(bind_name, res[idx]);
ret_val.insert(bind_name, res[idx].clone());
}
}
}
@ -199,7 +200,7 @@ pub fn nameize(p_s: &ParseSess, ms: &[Matcher], res: &[@NamedMatch])
}
pub enum ParseResult {
Success(HashMap<Ident, @NamedMatch>),
Success(HashMap<Ident, Rc<NamedMatch>>),
Failure(codemap::Span, ~str),
Error(codemap::Span, ~str)
}
@ -208,7 +209,7 @@ pub fn parse_or_else(sess: &ParseSess,
cfg: ast::CrateConfig,
rdr: TtReader,
ms: Vec<Matcher> )
-> HashMap<Ident, @NamedMatch> {
-> HashMap<Ident, Rc<NamedMatch>> {
match parse(sess, cfg, rdr, ms.as_slice()) {
Success(m) => m,
Failure(sp, str) => sess.span_diagnostic.span_fatal(sp, str),
@ -282,8 +283,8 @@ pub fn parse(sess: &ParseSess,
let sub = (*ei.matches.get(idx)).clone();
new_pos.matches
.get_mut(idx)
.push(@MatchedSeq(sub, mk_sp(ei.sp_lo,
sp.hi)));
.push(Rc::new(MatchedSeq(sub, mk_sp(ei.sp_lo,
sp.hi))));
}
new_pos.idx += 1;
@ -325,7 +326,7 @@ pub fn parse(sess: &ParseSess,
for idx in range(match_idx_lo, match_idx_hi) {
new_ei.matches
.get_mut(idx)
.push(@MatchedSeq(Vec::new(), sp));
.push(Rc::new(MatchedSeq(Vec::new(), sp)));
}
cur_eis.push(new_ei);
@ -401,8 +402,8 @@ pub fn parse(sess: &ParseSess,
match ei.elts.get(ei.idx).node {
MatchNonterminal(_, name, idx) => {
let name_string = token::get_ident(name);
ei.matches.get_mut(idx).push(@MatchedNonterminal(
parse_nt(&mut rust_parser, name_string.get())));
ei.matches.get_mut(idx).push(Rc::new(MatchedNonterminal(
parse_nt(&mut rust_parser, name_string.get()))));
ei.idx += 1u;
}
_ => fail!()

View File

@ -86,8 +86,8 @@ impl<'a> AnyMacro for ParserAnyMacro<'a> {
struct MacroRulesMacroExpander {
name: Ident,
lhses: @Vec<@NamedMatch> ,
rhses: @Vec<@NamedMatch> ,
lhses: Vec<Rc<NamedMatch>>,
rhses: Vec<Rc<NamedMatch>>,
}
impl MacroExpander for MacroRulesMacroExpander {
@ -110,8 +110,8 @@ fn generic_extension(cx: &ExtCtxt,
sp: Span,
name: Ident,
arg: &[ast::TokenTree],
lhses: &[@NamedMatch],
rhses: &[@NamedMatch])
lhses: &[Rc<NamedMatch>],
rhses: &[Rc<NamedMatch>])
-> MacResult {
if cx.trace_macros() {
println!("{}! \\{ {} \\}",
@ -221,12 +221,12 @@ pub fn add_new_extension(cx: &mut ExtCtxt,
// Extract the arguments:
let lhses = match **argument_map.get(&lhs_nm) {
MatchedSeq(ref s, _) => /* FIXME (#2543) */ @(*s).clone(),
MatchedSeq(ref s, _) => /* FIXME (#2543) */ (*s).clone(),
_ => cx.span_bug(sp, "wrong-structured lhs")
};
let rhses = match **argument_map.get(&rhs_nm) {
MatchedSeq(ref s, _) => /* FIXME (#2543) */ @(*s).clone(),
MatchedSeq(ref s, _) => /* FIXME (#2543) */ (*s).clone(),
_ => cx.span_bug(sp, "wrong-structured rhs")
};

View File

@ -35,7 +35,7 @@ pub struct TtReader<'a> {
// the unzipped tree:
priv stack: Vec<TtFrame>,
/* for MBE-style macro transcription */
priv interpolations: HashMap<Ident, @NamedMatch>,
priv interpolations: HashMap<Ident, Rc<NamedMatch>>,
priv repeat_idx: Vec<uint>,
priv repeat_len: Vec<uint>,
/* cached: */
@ -47,7 +47,7 @@ pub struct TtReader<'a> {
* `src` contains no `TTSeq`s and `TTNonterminal`s, `interp` can (and
* should) be none. */
pub fn new_tt_reader<'a>(sp_diag: &'a SpanHandler,
interp: Option<HashMap<Ident, @NamedMatch>>,
interp: Option<HashMap<Ident, Rc<NamedMatch>>>,
src: Vec<ast::TokenTree> )
-> TtReader<'a> {
let mut r = TtReader {
@ -72,19 +72,19 @@ pub fn new_tt_reader<'a>(sp_diag: &'a SpanHandler,
r
}
fn lookup_cur_matched_by_matched(r: &TtReader, start: @NamedMatch) -> @NamedMatch {
fn lookup_cur_matched_by_matched(r: &TtReader, start: Rc<NamedMatch>) -> Rc<NamedMatch> {
r.repeat_idx.iter().fold(start, |ad, idx| {
match *ad {
MatchedNonterminal(_) => {
// end of the line; duplicate henceforth
ad
ad.clone()
}
MatchedSeq(ref ads, _) => *ads.get(*idx)
MatchedSeq(ref ads, _) => ads.get(*idx).clone()
}
})
}
fn lookup_cur_matched(r: &TtReader, name: Ident) -> @NamedMatch {
fn lookup_cur_matched(r: &TtReader, name: Ident) -> Rc<NamedMatch> {
let matched_opt = r.interpolations.find_copy(&name);
match matched_opt {
Some(s) => lookup_cur_matched_by_matched(r, s),