libsyntax: Make the parser mutable
This commit is contained in:
parent
0df9b850ac
commit
f499d365ad
13 changed files with 518 additions and 487 deletions
|
@ -39,7 +39,7 @@ fn next_state(s: State) -> Option<State> {
|
||||||
|
|
||||||
pub fn expand_asm(cx: &mut ExtCtxt, sp: Span, tts: &[ast::token_tree])
|
pub fn expand_asm(cx: &mut ExtCtxt, sp: Span, tts: &[ast::token_tree])
|
||||||
-> base::MacResult {
|
-> base::MacResult {
|
||||||
let p = parse::new_parser_from_tts(cx.parse_sess(),
|
let mut p = parse::new_parser_from_tts(cx.parse_sess(),
|
||||||
cx.cfg(),
|
cx.cfg(),
|
||||||
tts.to_owned());
|
tts.to_owned());
|
||||||
|
|
||||||
|
|
|
@ -442,7 +442,7 @@ pub fn get_single_str_from_tts(cx: &ExtCtxt,
|
||||||
pub fn get_exprs_from_tts(cx: &ExtCtxt,
|
pub fn get_exprs_from_tts(cx: &ExtCtxt,
|
||||||
sp: Span,
|
sp: Span,
|
||||||
tts: &[ast::token_tree]) -> ~[@ast::Expr] {
|
tts: &[ast::token_tree]) -> ~[@ast::Expr] {
|
||||||
let p = parse::new_parser_from_tts(cx.parse_sess(),
|
let mut p = parse::new_parser_from_tts(cx.parse_sess(),
|
||||||
cx.cfg(),
|
cx.cfg(),
|
||||||
tts.to_owned());
|
tts.to_owned());
|
||||||
let mut es = ~[];
|
let mut es = ~[];
|
||||||
|
|
|
@ -26,7 +26,9 @@ use parse::token;
|
||||||
use parse::attr::parser_attr;
|
use parse::attr::parser_attr;
|
||||||
|
|
||||||
pub fn expand_cfg(cx: &mut ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult {
|
pub fn expand_cfg(cx: &mut ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult {
|
||||||
let p = parse::new_parser_from_tts(cx.parse_sess(), cx.cfg(), tts.to_owned());
|
let mut p = parse::new_parser_from_tts(cx.parse_sess(),
|
||||||
|
cx.cfg(),
|
||||||
|
tts.to_owned());
|
||||||
|
|
||||||
let mut cfgs = ~[];
|
let mut cfgs = ~[];
|
||||||
// parse `cfg!(meta_item, meta_item(x,y), meta_item="foo", ...)`
|
// parse `cfg!(meta_item, meta_item(x,y), meta_item="foo", ...)`
|
||||||
|
|
|
@ -53,9 +53,9 @@ struct Context<'a> {
|
||||||
impl<'a> Context<'a> {
|
impl<'a> Context<'a> {
|
||||||
/// Parses the arguments from the given list of tokens, returning None if
|
/// Parses the arguments from the given list of tokens, returning None if
|
||||||
/// there's a parse error so we can continue parsing other format! expressions.
|
/// there's a parse error so we can continue parsing other format! expressions.
|
||||||
fn parse_args(&mut self, sp: Span,
|
fn parse_args(&mut self, sp: Span, tts: &[ast::token_tree])
|
||||||
tts: &[ast::token_tree]) -> (@ast::Expr, Option<@ast::Expr>) {
|
-> (@ast::Expr, Option<@ast::Expr>) {
|
||||||
let p = rsparse::new_parser_from_tts(self.ecx.parse_sess(),
|
let mut p = rsparse::new_parser_from_tts(self.ecx.parse_sess(),
|
||||||
self.ecx.cfg(),
|
self.ecx.cfg(),
|
||||||
tts.to_owned());
|
tts.to_owned());
|
||||||
// Parse the leading function expression (maybe a block, maybe a path)
|
// Parse the leading function expression (maybe a block, maybe a path)
|
||||||
|
|
|
@ -579,21 +579,17 @@ fn mk_tts(cx: &ExtCtxt, sp: Span, tts: &[ast::token_tree])
|
||||||
ss
|
ss
|
||||||
}
|
}
|
||||||
|
|
||||||
fn expand_tts(cx: &ExtCtxt,
|
fn expand_tts(cx: &ExtCtxt, sp: Span, tts: &[ast::token_tree])
|
||||||
sp: Span,
|
-> (@ast::Expr, @ast::Expr) {
|
||||||
tts: &[ast::token_tree]) -> (@ast::Expr, @ast::Expr) {
|
|
||||||
|
|
||||||
// NB: It appears that the main parser loses its mind if we consider
|
// NB: It appears that the main parser loses its mind if we consider
|
||||||
// $foo as a tt_nonterminal during the main parse, so we have to re-parse
|
// $foo as a tt_nonterminal during the main parse, so we have to re-parse
|
||||||
// under quote_depth > 0. This is silly and should go away; the _guess_ is
|
// under quote_depth > 0. This is silly and should go away; the _guess_ is
|
||||||
// it has to do with transition away from supporting old-style macros, so
|
// it has to do with transition away from supporting old-style macros, so
|
||||||
// try removing it when enough of them are gone.
|
// try removing it when enough of them are gone.
|
||||||
|
|
||||||
let p = parse::new_parser_from_tts(
|
let mut p = parse::new_parser_from_tts(cx.parse_sess(),
|
||||||
cx.parse_sess(),
|
|
||||||
cx.cfg(),
|
cx.cfg(),
|
||||||
tts.to_owned()
|
tts.to_owned());
|
||||||
);
|
|
||||||
*p.quote_depth += 1u;
|
*p.quote_depth += 1u;
|
||||||
|
|
||||||
let cx_expr = p.parse_expr();
|
let cx_expr = p.parse_expr();
|
||||||
|
|
|
@ -81,9 +81,13 @@ pub fn expand_include(cx: &mut ExtCtxt, sp: Span, tts: &[ast::token_tree])
|
||||||
-> base::MacResult {
|
-> base::MacResult {
|
||||||
let file = get_single_str_from_tts(cx, sp, tts, "include!");
|
let file = get_single_str_from_tts(cx, sp, tts, "include!");
|
||||||
// The file will be added to the code map by the parser
|
// The file will be added to the code map by the parser
|
||||||
let p = parse::new_sub_parser_from_file(
|
let mut p =
|
||||||
cx.parse_sess(), cx.cfg(),
|
parse::new_sub_parser_from_file(cx.parse_sess(),
|
||||||
&res_rel_file(cx, sp, &Path::new(file)), sp);
|
cx.cfg(),
|
||||||
|
&res_rel_file(cx,
|
||||||
|
sp,
|
||||||
|
&Path::new(file)),
|
||||||
|
sp);
|
||||||
base::MRExpr(p.parse_expr())
|
base::MRExpr(p.parse_expr())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -26,7 +26,7 @@ pub fn expand_trace_macros(cx: &mut ExtCtxt,
|
||||||
None,
|
None,
|
||||||
tt.to_owned());
|
tt.to_owned());
|
||||||
let rdr = tt_rdr as @mut reader;
|
let rdr = tt_rdr as @mut reader;
|
||||||
let rust_parser = Parser(sess, cfg.clone(), rdr.dup());
|
let mut rust_parser = Parser(sess, cfg.clone(), rdr.dup());
|
||||||
|
|
||||||
if rust_parser.is_keyword(keywords::True) {
|
if rust_parser.is_keyword(keywords::True) {
|
||||||
cx.set_trace_macros(true);
|
cx.set_trace_macros(true);
|
||||||
|
@ -38,7 +38,7 @@ pub fn expand_trace_macros(cx: &mut ExtCtxt,
|
||||||
|
|
||||||
rust_parser.bump();
|
rust_parser.bump();
|
||||||
|
|
||||||
let rust_parser = Parser(sess, cfg, rdr.dup());
|
let mut rust_parser = Parser(sess, cfg, rdr.dup());
|
||||||
let result = rust_parser.parse_expr();
|
let result = rust_parser.parse_expr();
|
||||||
base::MRExpr(result)
|
base::MRExpr(result)
|
||||||
}
|
}
|
||||||
|
|
|
@ -403,13 +403,13 @@ pub fn parse(
|
||||||
}
|
}
|
||||||
rdr.next_token();
|
rdr.next_token();
|
||||||
} else /* bb_eis.len() == 1 */ {
|
} else /* bb_eis.len() == 1 */ {
|
||||||
let rust_parser = Parser(sess, cfg.clone(), rdr.dup());
|
let mut rust_parser = Parser(sess, cfg.clone(), rdr.dup());
|
||||||
|
|
||||||
let mut ei = bb_eis.pop();
|
let mut ei = bb_eis.pop();
|
||||||
match ei.elts[ei.idx].node {
|
match ei.elts[ei.idx].node {
|
||||||
match_nonterminal(_, ref name, idx) => {
|
match_nonterminal(_, ref name, idx) => {
|
||||||
ei.matches[idx].push(@matched_nonterminal(
|
ei.matches[idx].push(@matched_nonterminal(
|
||||||
parse_nt(&rust_parser, ident_to_str(name))));
|
parse_nt(&mut rust_parser, ident_to_str(name))));
|
||||||
ei.idx += 1u;
|
ei.idx += 1u;
|
||||||
}
|
}
|
||||||
_ => fail!()
|
_ => fail!()
|
||||||
|
@ -426,7 +426,7 @@ pub fn parse(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_nt(p: &Parser, name: &str) -> nonterminal {
|
pub fn parse_nt(p: &mut Parser, name: &str) -> nonterminal {
|
||||||
match name {
|
match name {
|
||||||
"item" => match p.parse_item(~[]) {
|
"item" => match p.parse_item(~[]) {
|
||||||
Some(i) => token::nt_item(i),
|
Some(i) => token::nt_item(i),
|
||||||
|
|
|
@ -24,10 +24,11 @@ use parse::attr::parser_attr;
|
||||||
use parse::token::{get_ident_interner, special_idents, gensym_ident, ident_to_str};
|
use parse::token::{get_ident_interner, special_idents, gensym_ident, ident_to_str};
|
||||||
use parse::token::{FAT_ARROW, SEMI, nt_matchers, nt_tt, EOF};
|
use parse::token::{FAT_ARROW, SEMI, nt_matchers, nt_tt, EOF};
|
||||||
use print;
|
use print;
|
||||||
|
use std::cell::RefCell;
|
||||||
use util::small_vector::SmallVector;
|
use util::small_vector::SmallVector;
|
||||||
|
|
||||||
struct ParserAnyMacro {
|
struct ParserAnyMacro {
|
||||||
parser: @Parser,
|
parser: RefCell<Parser>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ParserAnyMacro {
|
impl ParserAnyMacro {
|
||||||
|
@ -38,28 +39,36 @@ impl ParserAnyMacro {
|
||||||
/// fail!(); } )` doesn't get picked up by .parse_expr(), but it's
|
/// fail!(); } )` doesn't get picked up by .parse_expr(), but it's
|
||||||
/// allowed to be there.
|
/// allowed to be there.
|
||||||
fn ensure_complete_parse(&self, allow_semi: bool) {
|
fn ensure_complete_parse(&self, allow_semi: bool) {
|
||||||
if allow_semi && *self.parser.token == SEMI {
|
let mut parser = self.parser.borrow_mut();
|
||||||
self.parser.bump()
|
if allow_semi && *parser.get().token == SEMI {
|
||||||
|
parser.get().bump()
|
||||||
}
|
}
|
||||||
if *self.parser.token != EOF {
|
if *parser.get().token != EOF {
|
||||||
let msg = format!("macro expansion ignores token `{}` and any following",
|
let token_str = parser.get().this_token_to_str();
|
||||||
self.parser.this_token_to_str());
|
let msg = format!("macro expansion ignores token `{}` and any \
|
||||||
self.parser.span_err(*self.parser.span, msg);
|
following",
|
||||||
|
token_str);
|
||||||
|
let span = *parser.get().span;
|
||||||
|
parser.get().span_err(span, msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AnyMacro for ParserAnyMacro {
|
impl AnyMacro for ParserAnyMacro {
|
||||||
fn make_expr(&self) -> @ast::Expr {
|
fn make_expr(&self) -> @ast::Expr {
|
||||||
let ret = self.parser.parse_expr();
|
let ret = {
|
||||||
|
let mut parser = self.parser.borrow_mut();
|
||||||
|
parser.get().parse_expr()
|
||||||
|
};
|
||||||
self.ensure_complete_parse(true);
|
self.ensure_complete_parse(true);
|
||||||
ret
|
ret
|
||||||
}
|
}
|
||||||
fn make_items(&self) -> SmallVector<@ast::item> {
|
fn make_items(&self) -> SmallVector<@ast::item> {
|
||||||
let mut ret = SmallVector::zero();
|
let mut ret = SmallVector::zero();
|
||||||
loop {
|
loop {
|
||||||
let attrs = self.parser.parse_outer_attributes();
|
let mut parser = self.parser.borrow_mut();
|
||||||
match self.parser.parse_item(attrs) {
|
let attrs = parser.get().parse_outer_attributes();
|
||||||
|
match parser.get().parse_item(attrs) {
|
||||||
Some(item) => ret.push(item),
|
Some(item) => ret.push(item),
|
||||||
None => break
|
None => break
|
||||||
}
|
}
|
||||||
|
@ -68,8 +77,11 @@ impl AnyMacro for ParserAnyMacro {
|
||||||
ret
|
ret
|
||||||
}
|
}
|
||||||
fn make_stmt(&self) -> @ast::Stmt {
|
fn make_stmt(&self) -> @ast::Stmt {
|
||||||
let attrs = self.parser.parse_outer_attributes();
|
let ret = {
|
||||||
let ret = self.parser.parse_stmt(attrs);
|
let mut parser = self.parser.borrow_mut();
|
||||||
|
let attrs = parser.get().parse_outer_attributes();
|
||||||
|
parser.get().parse_stmt(attrs)
|
||||||
|
};
|
||||||
self.ensure_complete_parse(true);
|
self.ensure_complete_parse(true);
|
||||||
ret
|
ret
|
||||||
}
|
}
|
||||||
|
@ -142,14 +154,14 @@ fn generic_extension(cx: &ExtCtxt,
|
||||||
// rhs has holes ( `$id` and `$(...)` that need filled)
|
// rhs has holes ( `$id` and `$(...)` that need filled)
|
||||||
let trncbr = new_tt_reader(s_d, Some(named_matches),
|
let trncbr = new_tt_reader(s_d, Some(named_matches),
|
||||||
rhs);
|
rhs);
|
||||||
let p = @Parser(cx.parse_sess(),
|
let p = Parser(cx.parse_sess(),
|
||||||
cx.cfg(),
|
cx.cfg(),
|
||||||
trncbr as @mut reader);
|
trncbr as @mut reader);
|
||||||
|
|
||||||
// Let the context choose how to interpret the result.
|
// Let the context choose how to interpret the result.
|
||||||
// Weird, but useful for X-macros.
|
// Weird, but useful for X-macros.
|
||||||
return MRAny(@ParserAnyMacro {
|
return MRAny(@ParserAnyMacro {
|
||||||
parser: p,
|
parser: RefCell::new(p),
|
||||||
} as @AnyMacro)
|
} as @AnyMacro)
|
||||||
}
|
}
|
||||||
failure(sp, ref msg) => if sp.lo >= best_fail_spot.lo {
|
failure(sp, ref msg) => if sp.lo >= best_fail_spot.lo {
|
||||||
|
|
|
@ -17,19 +17,18 @@ use parse::token::INTERPOLATED;
|
||||||
|
|
||||||
// a parser that can parse attributes.
|
// a parser that can parse attributes.
|
||||||
pub trait parser_attr {
|
pub trait parser_attr {
|
||||||
fn parse_outer_attributes(&self) -> ~[ast::Attribute];
|
fn parse_outer_attributes(&mut self) -> ~[ast::Attribute];
|
||||||
fn parse_attribute(&self, permit_inner: bool) -> ast::Attribute;
|
fn parse_attribute(&mut self, permit_inner: bool) -> ast::Attribute;
|
||||||
fn parse_inner_attrs_and_next(&self) ->
|
fn parse_inner_attrs_and_next(&mut self)
|
||||||
(~[ast::Attribute], ~[ast::Attribute]);
|
-> (~[ast::Attribute], ~[ast::Attribute]);
|
||||||
fn parse_meta_item(&self) -> @ast::MetaItem;
|
fn parse_meta_item(&mut self) -> @ast::MetaItem;
|
||||||
fn parse_meta_seq(&self) -> ~[@ast::MetaItem];
|
fn parse_meta_seq(&mut self) -> ~[@ast::MetaItem];
|
||||||
fn parse_optional_meta(&self) -> ~[@ast::MetaItem];
|
fn parse_optional_meta(&mut self) -> ~[@ast::MetaItem];
|
||||||
}
|
}
|
||||||
|
|
||||||
impl parser_attr for Parser {
|
impl parser_attr for Parser {
|
||||||
|
|
||||||
// Parse attributes that appear before an item
|
// Parse attributes that appear before an item
|
||||||
fn parse_outer_attributes(&self) -> ~[ast::Attribute] {
|
fn parse_outer_attributes(&mut self) -> ~[ast::Attribute] {
|
||||||
let mut attrs: ~[ast::Attribute] = ~[];
|
let mut attrs: ~[ast::Attribute] = ~[];
|
||||||
loop {
|
loop {
|
||||||
debug!("parse_outer_attributes: self.token={:?}",
|
debug!("parse_outer_attributes: self.token={:?}",
|
||||||
|
@ -66,7 +65,7 @@ impl parser_attr for Parser {
|
||||||
//
|
//
|
||||||
// if permit_inner is true, then a trailing `;` indicates an inner
|
// if permit_inner is true, then a trailing `;` indicates an inner
|
||||||
// attribute
|
// attribute
|
||||||
fn parse_attribute(&self, permit_inner: bool) -> ast::Attribute {
|
fn parse_attribute(&mut self, permit_inner: bool) -> ast::Attribute {
|
||||||
debug!("parse_attributes: permit_inner={:?} self.token={:?}",
|
debug!("parse_attributes: permit_inner={:?} self.token={:?}",
|
||||||
permit_inner, self.token);
|
permit_inner, self.token);
|
||||||
let (span, value) = match *self.token {
|
let (span, value) = match *self.token {
|
||||||
|
@ -85,8 +84,9 @@ impl parser_attr for Parser {
|
||||||
(mk_sp(lo, hi), meta_item)
|
(mk_sp(lo, hi), meta_item)
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
|
let token_str = self.this_token_to_str();
|
||||||
self.fatal(format!("expected `\\#` but found `{}`",
|
self.fatal(format!("expected `\\#` but found `{}`",
|
||||||
self.this_token_to_str()));
|
token_str));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let style = if permit_inner && *self.token == token::SEMI {
|
let style = if permit_inner && *self.token == token::SEMI {
|
||||||
|
@ -115,7 +115,7 @@ impl parser_attr for Parser {
|
||||||
// matches inner_attrs* outer_attr?
|
// matches inner_attrs* outer_attr?
|
||||||
// you can make the 'next' field an Option, but the result is going to be
|
// you can make the 'next' field an Option, but the result is going to be
|
||||||
// more useful as a vector.
|
// more useful as a vector.
|
||||||
fn parse_inner_attrs_and_next(&self)
|
fn parse_inner_attrs_and_next(&mut self)
|
||||||
-> (~[ast::Attribute], ~[ast::Attribute]) {
|
-> (~[ast::Attribute], ~[ast::Attribute]) {
|
||||||
let mut inner_attrs: ~[ast::Attribute] = ~[];
|
let mut inner_attrs: ~[ast::Attribute] = ~[];
|
||||||
let mut next_outer_attrs: ~[ast::Attribute] = ~[];
|
let mut next_outer_attrs: ~[ast::Attribute] = ~[];
|
||||||
|
@ -154,9 +154,10 @@ impl parser_attr for Parser {
|
||||||
// matches meta_item = IDENT
|
// matches meta_item = IDENT
|
||||||
// | IDENT = lit
|
// | IDENT = lit
|
||||||
// | IDENT meta_seq
|
// | IDENT meta_seq
|
||||||
fn parse_meta_item(&self) -> @ast::MetaItem {
|
fn parse_meta_item(&mut self) -> @ast::MetaItem {
|
||||||
let lo = self.span.lo;
|
let lo = self.span.lo;
|
||||||
let name = self.id_to_str(self.parse_ident());
|
let ident = self.parse_ident();
|
||||||
|
let name = self.id_to_str(ident);
|
||||||
match *self.token {
|
match *self.token {
|
||||||
token::EQ => {
|
token::EQ => {
|
||||||
self.bump();
|
self.bump();
|
||||||
|
@ -187,14 +188,14 @@ impl parser_attr for Parser {
|
||||||
}
|
}
|
||||||
|
|
||||||
// matches meta_seq = ( COMMASEP(meta_item) )
|
// matches meta_seq = ( COMMASEP(meta_item) )
|
||||||
fn parse_meta_seq(&self) -> ~[@ast::MetaItem] {
|
fn parse_meta_seq(&mut self) -> ~[@ast::MetaItem] {
|
||||||
self.parse_seq(&token::LPAREN,
|
self.parse_seq(&token::LPAREN,
|
||||||
&token::RPAREN,
|
&token::RPAREN,
|
||||||
seq_sep_trailing_disallowed(token::COMMA),
|
seq_sep_trailing_disallowed(token::COMMA),
|
||||||
|p| p.parse_meta_item()).node
|
|p| p.parse_meta_item()).node
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_optional_meta(&self) -> ~[@ast::MetaItem] {
|
fn parse_optional_meta(&mut self) -> ~[@ast::MetaItem] {
|
||||||
match *self.token {
|
match *self.token {
|
||||||
token::LPAREN => self.parse_meta_seq(),
|
token::LPAREN => self.parse_meta_seq(),
|
||||||
_ => ~[]
|
_ => ~[]
|
||||||
|
|
|
@ -84,7 +84,7 @@ pub fn parse_crate_attrs_from_file(
|
||||||
cfg: ast::CrateConfig,
|
cfg: ast::CrateConfig,
|
||||||
sess: @mut ParseSess
|
sess: @mut ParseSess
|
||||||
) -> ~[ast::Attribute] {
|
) -> ~[ast::Attribute] {
|
||||||
let parser = new_parser_from_file(sess, cfg, input);
|
let mut parser = new_parser_from_file(sess, cfg, input);
|
||||||
let (inner, _) = parser.parse_inner_attrs_and_next();
|
let (inner, _) = parser.parse_inner_attrs_and_next();
|
||||||
return inner;
|
return inner;
|
||||||
}
|
}
|
||||||
|
@ -95,7 +95,7 @@ pub fn parse_crate_from_source_str(
|
||||||
cfg: ast::CrateConfig,
|
cfg: ast::CrateConfig,
|
||||||
sess: @mut ParseSess
|
sess: @mut ParseSess
|
||||||
) -> ast::Crate {
|
) -> ast::Crate {
|
||||||
let p = new_parser_from_source_str(sess,
|
let mut p = new_parser_from_source_str(sess,
|
||||||
/*bad*/ cfg.clone(),
|
/*bad*/ cfg.clone(),
|
||||||
name,
|
name,
|
||||||
source);
|
source);
|
||||||
|
@ -108,7 +108,7 @@ pub fn parse_crate_attrs_from_source_str(
|
||||||
cfg: ast::CrateConfig,
|
cfg: ast::CrateConfig,
|
||||||
sess: @mut ParseSess
|
sess: @mut ParseSess
|
||||||
) -> ~[ast::Attribute] {
|
) -> ~[ast::Attribute] {
|
||||||
let p = new_parser_from_source_str(sess,
|
let mut p = new_parser_from_source_str(sess,
|
||||||
/*bad*/ cfg.clone(),
|
/*bad*/ cfg.clone(),
|
||||||
name,
|
name,
|
||||||
source);
|
source);
|
||||||
|
@ -122,12 +122,7 @@ pub fn parse_expr_from_source_str(
|
||||||
cfg: ast::CrateConfig,
|
cfg: ast::CrateConfig,
|
||||||
sess: @mut ParseSess
|
sess: @mut ParseSess
|
||||||
) -> @ast::Expr {
|
) -> @ast::Expr {
|
||||||
let p = new_parser_from_source_str(
|
let mut p = new_parser_from_source_str(sess, cfg, name, source);
|
||||||
sess,
|
|
||||||
cfg,
|
|
||||||
name,
|
|
||||||
source
|
|
||||||
);
|
|
||||||
maybe_aborted(p.parse_expr(), p)
|
maybe_aborted(p.parse_expr(), p)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -138,12 +133,7 @@ pub fn parse_item_from_source_str(
|
||||||
attrs: ~[ast::Attribute],
|
attrs: ~[ast::Attribute],
|
||||||
sess: @mut ParseSess
|
sess: @mut ParseSess
|
||||||
) -> Option<@ast::item> {
|
) -> Option<@ast::item> {
|
||||||
let p = new_parser_from_source_str(
|
let mut p = new_parser_from_source_str(sess, cfg, name, source);
|
||||||
sess,
|
|
||||||
cfg,
|
|
||||||
name,
|
|
||||||
source
|
|
||||||
);
|
|
||||||
maybe_aborted(p.parse_item(attrs),p)
|
maybe_aborted(p.parse_item(attrs),p)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -153,12 +143,7 @@ pub fn parse_meta_from_source_str(
|
||||||
cfg: ast::CrateConfig,
|
cfg: ast::CrateConfig,
|
||||||
sess: @mut ParseSess
|
sess: @mut ParseSess
|
||||||
) -> @ast::MetaItem {
|
) -> @ast::MetaItem {
|
||||||
let p = new_parser_from_source_str(
|
let mut p = new_parser_from_source_str(sess, cfg, name, source);
|
||||||
sess,
|
|
||||||
cfg,
|
|
||||||
name,
|
|
||||||
source
|
|
||||||
);
|
|
||||||
maybe_aborted(p.parse_meta_item(),p)
|
maybe_aborted(p.parse_meta_item(),p)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -169,7 +154,7 @@ pub fn parse_stmt_from_source_str(
|
||||||
attrs: ~[ast::Attribute],
|
attrs: ~[ast::Attribute],
|
||||||
sess: @mut ParseSess
|
sess: @mut ParseSess
|
||||||
) -> @ast::Stmt {
|
) -> @ast::Stmt {
|
||||||
let p = new_parser_from_source_str(
|
let mut p = new_parser_from_source_str(
|
||||||
sess,
|
sess,
|
||||||
cfg,
|
cfg,
|
||||||
name,
|
name,
|
||||||
|
@ -184,7 +169,7 @@ pub fn parse_tts_from_source_str(
|
||||||
cfg: ast::CrateConfig,
|
cfg: ast::CrateConfig,
|
||||||
sess: @mut ParseSess
|
sess: @mut ParseSess
|
||||||
) -> ~[ast::token_tree] {
|
) -> ~[ast::token_tree] {
|
||||||
let p = new_parser_from_source_str(
|
let mut p = new_parser_from_source_str(
|
||||||
sess,
|
sess,
|
||||||
cfg,
|
cfg,
|
||||||
name,
|
name,
|
||||||
|
@ -201,15 +186,15 @@ pub fn parse_tts_from_source_str(
|
||||||
// consumed all of the input before returning the function's
|
// consumed all of the input before returning the function's
|
||||||
// result.
|
// result.
|
||||||
pub fn parse_from_source_str<T>(
|
pub fn parse_from_source_str<T>(
|
||||||
f: |&Parser| -> T,
|
f: |&mut Parser| -> T,
|
||||||
name: @str,
|
name: @str,
|
||||||
ss: codemap::FileSubstr,
|
ss: codemap::FileSubstr,
|
||||||
source: @str,
|
source: @str,
|
||||||
cfg: ast::CrateConfig,
|
cfg: ast::CrateConfig,
|
||||||
sess: @mut ParseSess)
|
sess: @mut ParseSess)
|
||||||
-> T {
|
-> T {
|
||||||
let p = new_parser_from_source_substr(sess, cfg, name, ss, source);
|
let mut p = new_parser_from_source_substr(sess, cfg, name, ss, source);
|
||||||
let r = f(&p);
|
let r = f(&mut p);
|
||||||
if !p.reader.is_eof() {
|
if !p.reader.is_eof() {
|
||||||
p.reader.fatal(~"expected end-of-string");
|
p.reader.fatal(~"expected end-of-string");
|
||||||
}
|
}
|
||||||
|
@ -326,7 +311,7 @@ pub fn filemap_to_tts(sess: @mut ParseSess, filemap: @FileMap)
|
||||||
// parsing tt's probably shouldn't require a parser at all.
|
// parsing tt's probably shouldn't require a parser at all.
|
||||||
let cfg = ~[];
|
let cfg = ~[];
|
||||||
let srdr = lexer::new_string_reader(sess.span_diagnostic, filemap);
|
let srdr = lexer::new_string_reader(sess.span_diagnostic, filemap);
|
||||||
let p1 = Parser(sess, cfg, srdr as @mut reader);
|
let mut p1 = Parser(sess, cfg, srdr as @mut reader);
|
||||||
p1.parse_all_token_trees()
|
p1.parse_all_token_trees()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -339,7 +324,7 @@ pub fn tts_to_parser(sess: @mut ParseSess,
|
||||||
}
|
}
|
||||||
|
|
||||||
// abort if necessary
|
// abort if necessary
|
||||||
pub fn maybe_aborted<T>(result : T, p: Parser) -> T {
|
pub fn maybe_aborted<T>(result: T, mut p: Parser) -> T {
|
||||||
p.abort_if_errors();
|
p.abort_if_errors();
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,23 +57,23 @@ impl to_bytes::IterBytes for ObsoleteSyntax {
|
||||||
|
|
||||||
pub trait ParserObsoleteMethods {
|
pub trait ParserObsoleteMethods {
|
||||||
/// Reports an obsolete syntax non-fatal error.
|
/// Reports an obsolete syntax non-fatal error.
|
||||||
fn obsolete(&self, sp: Span, kind: ObsoleteSyntax);
|
fn obsolete(&mut self, sp: Span, kind: ObsoleteSyntax);
|
||||||
// Reports an obsolete syntax non-fatal error, and returns
|
// Reports an obsolete syntax non-fatal error, and returns
|
||||||
// a placeholder expression
|
// a placeholder expression
|
||||||
fn obsolete_expr(&self, sp: Span, kind: ObsoleteSyntax) -> @Expr;
|
fn obsolete_expr(&mut self, sp: Span, kind: ObsoleteSyntax) -> @Expr;
|
||||||
fn report(&self,
|
fn report(&mut self,
|
||||||
sp: Span,
|
sp: Span,
|
||||||
kind: ObsoleteSyntax,
|
kind: ObsoleteSyntax,
|
||||||
kind_str: &str,
|
kind_str: &str,
|
||||||
desc: &str);
|
desc: &str);
|
||||||
fn token_is_obsolete_ident(&self, ident: &str, token: &Token) -> bool;
|
fn token_is_obsolete_ident(&mut self, ident: &str, token: &Token) -> bool;
|
||||||
fn is_obsolete_ident(&self, ident: &str) -> bool;
|
fn is_obsolete_ident(&mut self, ident: &str) -> bool;
|
||||||
fn eat_obsolete_ident(&self, ident: &str) -> bool;
|
fn eat_obsolete_ident(&mut self, ident: &str) -> bool;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ParserObsoleteMethods for Parser {
|
impl ParserObsoleteMethods for Parser {
|
||||||
/// Reports an obsolete syntax non-fatal error.
|
/// Reports an obsolete syntax non-fatal error.
|
||||||
fn obsolete(&self, sp: Span, kind: ObsoleteSyntax) {
|
fn obsolete(&mut self, sp: Span, kind: ObsoleteSyntax) {
|
||||||
let (kind_str, desc) = match kind {
|
let (kind_str, desc) = match kind {
|
||||||
ObsoleteSwap => (
|
ObsoleteSwap => (
|
||||||
"swap",
|
"swap",
|
||||||
|
@ -158,12 +158,12 @@ impl ParserObsoleteMethods for Parser {
|
||||||
|
|
||||||
// Reports an obsolete syntax non-fatal error, and returns
|
// Reports an obsolete syntax non-fatal error, and returns
|
||||||
// a placeholder expression
|
// a placeholder expression
|
||||||
fn obsolete_expr(&self, sp: Span, kind: ObsoleteSyntax) -> @Expr {
|
fn obsolete_expr(&mut self, sp: Span, kind: ObsoleteSyntax) -> @Expr {
|
||||||
self.obsolete(sp, kind);
|
self.obsolete(sp, kind);
|
||||||
self.mk_expr(sp.lo, sp.hi, ExprLit(@respan(sp, lit_nil)))
|
self.mk_expr(sp.lo, sp.hi, ExprLit(@respan(sp, lit_nil)))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn report(&self,
|
fn report(&mut self,
|
||||||
sp: Span,
|
sp: Span,
|
||||||
kind: ObsoleteSyntax,
|
kind: ObsoleteSyntax,
|
||||||
kind_str: &str,
|
kind_str: &str,
|
||||||
|
@ -176,7 +176,7 @@ impl ParserObsoleteMethods for Parser {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn token_is_obsolete_ident(&self, ident: &str, token: &Token)
|
fn token_is_obsolete_ident(&mut self, ident: &str, token: &Token)
|
||||||
-> bool {
|
-> bool {
|
||||||
match *token {
|
match *token {
|
||||||
token::IDENT(sid, _) => {
|
token::IDENT(sid, _) => {
|
||||||
|
@ -186,11 +186,11 @@ impl ParserObsoleteMethods for Parser {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_obsolete_ident(&self, ident: &str) -> bool {
|
fn is_obsolete_ident(&mut self, ident: &str) -> bool {
|
||||||
self.token_is_obsolete_ident(ident, self.token)
|
self.token_is_obsolete_ident(ident, self.token)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn eat_obsolete_ident(&self, ident: &str) -> bool {
|
fn eat_obsolete_ident(&mut self, ident: &str) -> bool {
|
||||||
if self.is_obsolete_ident(ident) {
|
if self.is_obsolete_ident(ident) {
|
||||||
self.bump();
|
self.bump();
|
||||||
true
|
true
|
||||||
|
|
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue