De-@ ParseSess uses.
This commit is contained in:
parent
555a239301
commit
90cbe0cad2
16 changed files with 90 additions and 109 deletions
|
@ -146,11 +146,10 @@ pub fn build_configuration(sess: &Session) -> ast::CrateConfig {
|
||||||
fn parse_cfgspecs(cfgspecs: Vec<~str> )
|
fn parse_cfgspecs(cfgspecs: Vec<~str> )
|
||||||
-> ast::CrateConfig {
|
-> ast::CrateConfig {
|
||||||
cfgspecs.move_iter().map(|s| {
|
cfgspecs.move_iter().map(|s| {
|
||||||
let sess = parse::new_parse_sess();
|
|
||||||
parse::parse_meta_from_source_str("cfgspec".to_str(),
|
parse::parse_meta_from_source_str("cfgspec".to_str(),
|
||||||
s,
|
s,
|
||||||
Vec::new(),
|
Vec::new(),
|
||||||
sess)
|
&parse::new_parse_sess())
|
||||||
}).collect::<ast::CrateConfig>()
|
}).collect::<ast::CrateConfig>()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -175,13 +174,13 @@ pub fn phase_1_parse_input(sess: &Session, cfg: ast::CrateConfig, input: &Input)
|
||||||
let krate = time(sess.time_passes(), "parsing", (), |_| {
|
let krate = time(sess.time_passes(), "parsing", (), |_| {
|
||||||
match *input {
|
match *input {
|
||||||
FileInput(ref file) => {
|
FileInput(ref file) => {
|
||||||
parse::parse_crate_from_file(&(*file), cfg.clone(), sess.parse_sess)
|
parse::parse_crate_from_file(&(*file), cfg.clone(), &sess.parse_sess)
|
||||||
}
|
}
|
||||||
StrInput(ref src) => {
|
StrInput(ref src) => {
|
||||||
parse::parse_crate_from_source_str(anon_src(),
|
parse::parse_crate_from_source_str(anon_src(),
|
||||||
(*src).clone(),
|
(*src).clone(),
|
||||||
cfg.clone(),
|
cfg.clone(),
|
||||||
sess.parse_sess)
|
&sess.parse_sess)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -241,7 +240,7 @@ pub fn phase_2_configure_and_expand(sess: &Session,
|
||||||
deriving_hash_type_parameter: sess.features.default_type_params.get(),
|
deriving_hash_type_parameter: sess.features.default_type_params.get(),
|
||||||
crate_id: crate_id.clone(),
|
crate_id: crate_id.clone(),
|
||||||
};
|
};
|
||||||
syntax::ext::expand::expand_crate(sess.parse_sess,
|
syntax::ext::expand::expand_crate(&sess.parse_sess,
|
||||||
cfg,
|
cfg,
|
||||||
krate)
|
krate)
|
||||||
});
|
});
|
||||||
|
|
|
@ -177,7 +177,7 @@ pub struct Session {
|
||||||
targ_cfg: @Config,
|
targ_cfg: @Config,
|
||||||
opts: @Options,
|
opts: @Options,
|
||||||
cstore: metadata::cstore::CStore,
|
cstore: metadata::cstore::CStore,
|
||||||
parse_sess: @ParseSess,
|
parse_sess: ParseSess,
|
||||||
codemap: @codemap::CodeMap,
|
codemap: @codemap::CodeMap,
|
||||||
// For a library crate, this is always none
|
// For a library crate, this is always none
|
||||||
entry_fn: RefCell<Option<(NodeId, codemap::Span)>>,
|
entry_fn: RefCell<Option<(NodeId, codemap::Span)>>,
|
||||||
|
|
|
@ -166,7 +166,7 @@ fn generate_test_harness(sess: &Session, krate: ast::Crate)
|
||||||
let loader = &mut Loader::new(sess);
|
let loader = &mut Loader::new(sess);
|
||||||
let mut cx: TestCtxt = TestCtxt {
|
let mut cx: TestCtxt = TestCtxt {
|
||||||
sess: sess,
|
sess: sess,
|
||||||
ext_cx: ExtCtxt::new(sess.parse_sess, sess.opts.cfg.clone(),
|
ext_cx: ExtCtxt::new(&sess.parse_sess, sess.opts.cfg.clone(),
|
||||||
ExpansionConfig {
|
ExpansionConfig {
|
||||||
loader: loader,
|
loader: loader,
|
||||||
deriving_hash_type_parameter: false,
|
deriving_hash_type_parameter: false,
|
||||||
|
|
|
@ -350,13 +350,13 @@ fn parse_crate_attrs(sess: &session::Session, input: &d::Input) ->
|
||||||
d::FileInput(ref ifile) => {
|
d::FileInput(ref ifile) => {
|
||||||
parse::parse_crate_attrs_from_file(ifile,
|
parse::parse_crate_attrs_from_file(ifile,
|
||||||
Vec::new(),
|
Vec::new(),
|
||||||
sess.parse_sess)
|
&sess.parse_sess)
|
||||||
}
|
}
|
||||||
d::StrInput(ref src) => {
|
d::StrInput(ref src) => {
|
||||||
parse::parse_crate_attrs_from_source_str(d::anon_src(),
|
parse::parse_crate_attrs_from_source_str(d::anon_src(),
|
||||||
(*src).clone(),
|
(*src).clone(),
|
||||||
Vec::new(),
|
Vec::new(),
|
||||||
sess.parse_sess)
|
&sess.parse_sess)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
result.move_iter().collect()
|
result.move_iter().collect()
|
||||||
|
|
|
@ -1446,17 +1446,17 @@ fn decode_item_ast(par_doc: ebml::Doc) -> @ast::Item {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
trait fake_ext_ctxt {
|
trait fake_ext_ctxt {
|
||||||
fn cfg(&self) -> ast::CrateConfig;
|
fn cfg(&self) -> ast::CrateConfig;
|
||||||
fn parse_sess(&self) -> @parse::ParseSess;
|
fn parse_sess<'a>(&'a self) -> &'a parse::ParseSess;
|
||||||
fn call_site(&self) -> Span;
|
fn call_site(&self) -> Span;
|
||||||
fn ident_of(&self, st: &str) -> ast::Ident;
|
fn ident_of(&self, st: &str) -> ast::Ident;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
impl fake_ext_ctxt for @parse::ParseSess {
|
impl fake_ext_ctxt for parse::ParseSess {
|
||||||
fn cfg(&self) -> ast::CrateConfig {
|
fn cfg(&self) -> ast::CrateConfig {
|
||||||
Vec::new()
|
Vec::new()
|
||||||
}
|
}
|
||||||
fn parse_sess(&self) -> @parse::ParseSess { *self }
|
fn parse_sess<'a>(&'a self) -> &'a parse::ParseSess { self }
|
||||||
fn call_site(&self) -> Span {
|
fn call_site(&self) -> Span {
|
||||||
codemap::Span {
|
codemap::Span {
|
||||||
lo: codemap::BytePos(0),
|
lo: codemap::BytePos(0),
|
||||||
|
@ -1470,7 +1470,7 @@ impl fake_ext_ctxt for @parse::ParseSess {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
fn mk_ctxt() -> @parse::ParseSess {
|
fn mk_ctxt() -> parse::ParseSess {
|
||||||
parse::new_parse_sess()
|
parse::new_parse_sess()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -30,10 +30,10 @@ pub fn highlight(src: &str, class: Option<&str>) -> ~str {
|
||||||
let sess = parse::new_parse_sess();
|
let sess = parse::new_parse_sess();
|
||||||
let handler = diagnostic::default_handler();
|
let handler = diagnostic::default_handler();
|
||||||
let span_handler = diagnostic::mk_span_handler(handler, sess.cm);
|
let span_handler = diagnostic::mk_span_handler(handler, sess.cm);
|
||||||
let fm = parse::string_to_filemap(sess, src.to_owned(), ~"<stdin>");
|
let fm = parse::string_to_filemap(&sess, src.to_owned(), ~"<stdin>");
|
||||||
|
|
||||||
let mut out = io::MemWriter::new();
|
let mut out = io::MemWriter::new();
|
||||||
doit(sess,
|
doit(&sess,
|
||||||
lexer::new_string_reader(span_handler, fm),
|
lexer::new_string_reader(span_handler, fm),
|
||||||
class,
|
class,
|
||||||
&mut out).unwrap();
|
&mut out).unwrap();
|
||||||
|
@ -47,7 +47,7 @@ pub fn highlight(src: &str, class: Option<&str>) -> ~str {
|
||||||
/// it's used. All source code emission is done as slices from the source map,
|
/// it's used. All source code emission is done as slices from the source map,
|
||||||
/// not from the tokens themselves, in order to stay true to the original
|
/// not from the tokens themselves, in order to stay true to the original
|
||||||
/// source.
|
/// source.
|
||||||
fn doit(sess: @parse::ParseSess, lexer: lexer::StringReader, class: Option<&str>,
|
fn doit(sess: &parse::ParseSess, lexer: lexer::StringReader, class: Option<&str>,
|
||||||
out: &mut Writer) -> io::IoResult<()> {
|
out: &mut Writer) -> io::IoResult<()> {
|
||||||
use syntax::parse::lexer::Reader;
|
use syntax::parse::lexer::Reader;
|
||||||
|
|
||||||
|
|
|
@ -289,7 +289,7 @@ pub trait CrateLoader {
|
||||||
// when a macro expansion occurs, the resulting nodes have the backtrace()
|
// when a macro expansion occurs, the resulting nodes have the backtrace()
|
||||||
// -> expn_info of their expansion context stored into their span.
|
// -> expn_info of their expansion context stored into their span.
|
||||||
pub struct ExtCtxt<'a> {
|
pub struct ExtCtxt<'a> {
|
||||||
parse_sess: @parse::ParseSess,
|
parse_sess: &'a parse::ParseSess,
|
||||||
cfg: ast::CrateConfig,
|
cfg: ast::CrateConfig,
|
||||||
backtrace: Option<@ExpnInfo>,
|
backtrace: Option<@ExpnInfo>,
|
||||||
ecfg: expand::ExpansionConfig<'a>,
|
ecfg: expand::ExpansionConfig<'a>,
|
||||||
|
@ -299,7 +299,7 @@ pub struct ExtCtxt<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> ExtCtxt<'a> {
|
impl<'a> ExtCtxt<'a> {
|
||||||
pub fn new<'a>(parse_sess: @parse::ParseSess, cfg: ast::CrateConfig,
|
pub fn new<'a>(parse_sess: &'a parse::ParseSess, cfg: ast::CrateConfig,
|
||||||
ecfg: expand::ExpansionConfig<'a>) -> ExtCtxt<'a> {
|
ecfg: expand::ExpansionConfig<'a>) -> ExtCtxt<'a> {
|
||||||
ExtCtxt {
|
ExtCtxt {
|
||||||
parse_sess: parse_sess,
|
parse_sess: parse_sess,
|
||||||
|
@ -327,7 +327,7 @@ impl<'a> ExtCtxt<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn codemap(&self) -> @CodeMap { self.parse_sess.cm }
|
pub fn codemap(&self) -> @CodeMap { self.parse_sess.cm }
|
||||||
pub fn parse_sess(&self) -> @parse::ParseSess { self.parse_sess }
|
pub fn parse_sess(&self) -> &'a parse::ParseSess { self.parse_sess }
|
||||||
pub fn cfg(&self) -> ast::CrateConfig { self.cfg.clone() }
|
pub fn cfg(&self) -> ast::CrateConfig { self.cfg.clone() }
|
||||||
pub fn call_site(&self) -> Span {
|
pub fn call_site(&self) -> Span {
|
||||||
match self.backtrace {
|
match self.backtrace {
|
||||||
|
|
|
@ -838,12 +838,12 @@ pub fn new_span(cx: &ExtCtxt, sp: Span) -> Span {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct MacroExpander<'a> {
|
pub struct MacroExpander<'a, 'b> {
|
||||||
extsbox: SyntaxEnv,
|
extsbox: SyntaxEnv,
|
||||||
cx: &'a mut ExtCtxt<'a>,
|
cx: &'a mut ExtCtxt<'b>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Folder for MacroExpander<'a> {
|
impl<'a, 'b> Folder for MacroExpander<'a, 'b> {
|
||||||
fn fold_expr(&mut self, expr: @ast::Expr) -> @ast::Expr {
|
fn fold_expr(&mut self, expr: @ast::Expr) -> @ast::Expr {
|
||||||
expand_expr(expr, self)
|
expand_expr(expr, self)
|
||||||
}
|
}
|
||||||
|
@ -875,7 +875,7 @@ pub struct ExpansionConfig<'a> {
|
||||||
crate_id: CrateId,
|
crate_id: CrateId,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn expand_crate(parse_sess: @parse::ParseSess,
|
pub fn expand_crate(parse_sess: &parse::ParseSess,
|
||||||
cfg: ExpansionConfig,
|
cfg: ExpansionConfig,
|
||||||
c: Crate) -> Crate {
|
c: Crate) -> Crate {
|
||||||
let mut cx = ExtCtxt::new(parse_sess, c.config.clone(), cfg);
|
let mut cx = ExtCtxt::new(parse_sess, c.config.clone(), cfg);
|
||||||
|
@ -974,7 +974,7 @@ mod test {
|
||||||
use ext::mtwt;
|
use ext::mtwt;
|
||||||
use parse;
|
use parse;
|
||||||
use parse::token;
|
use parse::token;
|
||||||
use util::parser_testing::{string_to_crate_and_sess};
|
use util::parser_testing::{string_to_parser};
|
||||||
use util::parser_testing::{string_to_pat, strs_to_idents};
|
use util::parser_testing::{string_to_pat, strs_to_idents};
|
||||||
use visit;
|
use visit;
|
||||||
use visit::Visitor;
|
use visit::Visitor;
|
||||||
|
@ -1126,7 +1126,8 @@ mod test {
|
||||||
//}
|
//}
|
||||||
|
|
||||||
fn expand_crate_str(crate_str: ~str) -> ast::Crate {
|
fn expand_crate_str(crate_str: ~str) -> ast::Crate {
|
||||||
let (crate_ast,ps) = string_to_crate_and_sess(crate_str);
|
let ps = parse::new_parse_sess();
|
||||||
|
let crate_ast = string_to_parser(&ps, source_str).parse_crate_mod();
|
||||||
// the cfg argument actually does matter, here...
|
// the cfg argument actually does matter, here...
|
||||||
let mut loader = ErrLoader;
|
let mut loader = ErrLoader;
|
||||||
let cfg = ::syntax::ext::expand::ExpansionConfig {
|
let cfg = ::syntax::ext::expand::ExpansionConfig {
|
||||||
|
@ -1134,7 +1135,7 @@ mod test {
|
||||||
deriving_hash_type_parameter: false,
|
deriving_hash_type_parameter: false,
|
||||||
crate_id: from_str("test").unwrap(),
|
crate_id: from_str("test").unwrap(),
|
||||||
};
|
};
|
||||||
expand_crate(ps,cfg,crate_ast)
|
expand_crate(&ps,cfg,crate_ast)
|
||||||
}
|
}
|
||||||
|
|
||||||
//fn expand_and_resolve(crate_str: @str) -> ast::crate {
|
//fn expand_and_resolve(crate_str: @str) -> ast::crate {
|
||||||
|
|
|
@ -35,8 +35,8 @@ enum Position {
|
||||||
Named(~str),
|
Named(~str),
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Context<'a> {
|
struct Context<'a, 'b> {
|
||||||
ecx: &'a mut ExtCtxt<'a>,
|
ecx: &'a mut ExtCtxt<'b>,
|
||||||
fmtsp: Span,
|
fmtsp: Span,
|
||||||
|
|
||||||
// Parsed argument expressions and the types that we've found so far for
|
// Parsed argument expressions and the types that we've found so far for
|
||||||
|
@ -142,7 +142,7 @@ fn parse_args(ecx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
|
||||||
return (extra, Some((fmtstr, args, order, names)));
|
return (extra, Some((fmtstr, args, order, names)));
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Context<'a> {
|
impl<'a, 'b> Context<'a, 'b> {
|
||||||
/// Verifies one piece of a parse string. All errors are not emitted as
|
/// Verifies one piece of a parse string. All errors are not emitted as
|
||||||
/// fatal so we can continue giving errors about this and possibly other
|
/// fatal so we can continue giving errors about this and possibly other
|
||||||
/// format strings.
|
/// format strings.
|
||||||
|
|
|
@ -170,9 +170,9 @@ pub enum NamedMatch {
|
||||||
MatchedNonterminal(Nonterminal)
|
MatchedNonterminal(Nonterminal)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn nameize(p_s: @ParseSess, ms: &[Matcher], res: &[@NamedMatch])
|
pub fn nameize(p_s: &ParseSess, ms: &[Matcher], res: &[@NamedMatch])
|
||||||
-> HashMap<Ident, @NamedMatch> {
|
-> HashMap<Ident, @NamedMatch> {
|
||||||
fn n_rec(p_s: @ParseSess, m: &Matcher, res: &[@NamedMatch],
|
fn n_rec(p_s: &ParseSess, m: &Matcher, res: &[@NamedMatch],
|
||||||
ret_val: &mut HashMap<Ident, @NamedMatch>) {
|
ret_val: &mut HashMap<Ident, @NamedMatch>) {
|
||||||
match *m {
|
match *m {
|
||||||
codemap::Spanned {node: MatchTok(_), .. } => (),
|
codemap::Spanned {node: MatchTok(_), .. } => (),
|
||||||
|
@ -205,7 +205,7 @@ pub enum ParseResult {
|
||||||
Error(codemap::Span, ~str)
|
Error(codemap::Span, ~str)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_or_else<R: Reader>(sess: @ParseSess,
|
pub fn parse_or_else<R: Reader>(sess: &ParseSess,
|
||||||
cfg: ast::CrateConfig,
|
cfg: ast::CrateConfig,
|
||||||
rdr: R,
|
rdr: R,
|
||||||
ms: Vec<Matcher> )
|
ms: Vec<Matcher> )
|
||||||
|
@ -227,7 +227,7 @@ pub fn token_name_eq(t1 : &Token, t2 : &Token) -> bool {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse<R: Reader>(sess: @ParseSess,
|
pub fn parse<R: Reader>(sess: &ParseSess,
|
||||||
cfg: ast::CrateConfig,
|
cfg: ast::CrateConfig,
|
||||||
rdr: R,
|
rdr: R,
|
||||||
ms: &[Matcher])
|
ms: &[Matcher])
|
||||||
|
|
|
@ -30,11 +30,11 @@ use util::small_vector::SmallVector;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::vec_ng::Vec;
|
use std::vec_ng::Vec;
|
||||||
|
|
||||||
struct ParserAnyMacro {
|
struct ParserAnyMacro<'a> {
|
||||||
parser: RefCell<Parser>,
|
parser: RefCell<Parser<'a>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ParserAnyMacro {
|
impl<'a> ParserAnyMacro<'a> {
|
||||||
/// Make sure we don't have any tokens left to parse, so we don't
|
/// Make sure we don't have any tokens left to parse, so we don't
|
||||||
/// silently drop anything. `allow_semi` is so that "optional"
|
/// silently drop anything. `allow_semi` is so that "optional"
|
||||||
/// semilons at the end of normal expressions aren't complained
|
/// semilons at the end of normal expressions aren't complained
|
||||||
|
@ -57,7 +57,7 @@ impl ParserAnyMacro {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AnyMacro for ParserAnyMacro {
|
impl<'a> AnyMacro for ParserAnyMacro<'a> {
|
||||||
fn make_expr(&self) -> @ast::Expr {
|
fn make_expr(&self) -> @ast::Expr {
|
||||||
let ret = {
|
let ret = {
|
||||||
let mut parser = self.parser.borrow_mut();
|
let mut parser = self.parser.borrow_mut();
|
||||||
|
|
|
@ -28,7 +28,7 @@ pub trait ParserAttr {
|
||||||
fn parse_optional_meta(&mut self) -> Vec<@ast::MetaItem> ;
|
fn parse_optional_meta(&mut self) -> Vec<@ast::MetaItem> ;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ParserAttr for Parser {
|
impl<'a> ParserAttr for Parser<'a> {
|
||||||
// Parse attributes that appear before an item
|
// Parse attributes that appear before an item
|
||||||
fn parse_outer_attributes(&mut self) -> Vec<ast::Attribute> {
|
fn parse_outer_attributes(&mut self) -> Vec<ast::Attribute> {
|
||||||
let mut attrs: Vec<ast::Attribute> = Vec::new();
|
let mut attrs: Vec<ast::Attribute> = Vec::new();
|
||||||
|
|
|
@ -46,9 +46,9 @@ pub struct ParseSess {
|
||||||
included_mod_stack: RefCell<Vec<Path> >,
|
included_mod_stack: RefCell<Vec<Path> >,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_parse_sess() -> @ParseSess {
|
pub fn new_parse_sess() -> ParseSess {
|
||||||
let cm = @CodeMap::new();
|
let cm = @CodeMap::new();
|
||||||
@ParseSess {
|
ParseSess {
|
||||||
cm: cm,
|
cm: cm,
|
||||||
span_diagnostic: mk_span_handler(default_handler(), cm),
|
span_diagnostic: mk_span_handler(default_handler(), cm),
|
||||||
included_mod_stack: RefCell::new(Vec::new()),
|
included_mod_stack: RefCell::new(Vec::new()),
|
||||||
|
@ -57,8 +57,8 @@ pub fn new_parse_sess() -> @ParseSess {
|
||||||
|
|
||||||
pub fn new_parse_sess_special_handler(sh: @SpanHandler,
|
pub fn new_parse_sess_special_handler(sh: @SpanHandler,
|
||||||
cm: @codemap::CodeMap)
|
cm: @codemap::CodeMap)
|
||||||
-> @ParseSess {
|
-> ParseSess {
|
||||||
@ParseSess {
|
ParseSess {
|
||||||
cm: cm,
|
cm: cm,
|
||||||
span_diagnostic: sh,
|
span_diagnostic: sh,
|
||||||
included_mod_stack: RefCell::new(Vec::new()),
|
included_mod_stack: RefCell::new(Vec::new()),
|
||||||
|
@ -73,7 +73,7 @@ pub fn new_parse_sess_special_handler(sh: @SpanHandler,
|
||||||
pub fn parse_crate_from_file(
|
pub fn parse_crate_from_file(
|
||||||
input: &Path,
|
input: &Path,
|
||||||
cfg: ast::CrateConfig,
|
cfg: ast::CrateConfig,
|
||||||
sess: @ParseSess
|
sess: &ParseSess
|
||||||
) -> ast::Crate {
|
) -> ast::Crate {
|
||||||
new_parser_from_file(sess, cfg, input).parse_crate_mod()
|
new_parser_from_file(sess, cfg, input).parse_crate_mod()
|
||||||
// why is there no p.abort_if_errors here?
|
// why is there no p.abort_if_errors here?
|
||||||
|
@ -82,17 +82,17 @@ pub fn parse_crate_from_file(
|
||||||
pub fn parse_crate_attrs_from_file(
|
pub fn parse_crate_attrs_from_file(
|
||||||
input: &Path,
|
input: &Path,
|
||||||
cfg: ast::CrateConfig,
|
cfg: ast::CrateConfig,
|
||||||
sess: @ParseSess
|
sess: &ParseSess
|
||||||
) -> Vec<ast::Attribute> {
|
) -> Vec<ast::Attribute> {
|
||||||
let mut 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;
|
inner
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_crate_from_source_str(name: ~str,
|
pub fn parse_crate_from_source_str(name: ~str,
|
||||||
source: ~str,
|
source: ~str,
|
||||||
cfg: ast::CrateConfig,
|
cfg: ast::CrateConfig,
|
||||||
sess: @ParseSess)
|
sess: &ParseSess)
|
||||||
-> ast::Crate {
|
-> ast::Crate {
|
||||||
let mut p = new_parser_from_source_str(sess,
|
let mut p = new_parser_from_source_str(sess,
|
||||||
cfg,
|
cfg,
|
||||||
|
@ -104,20 +104,20 @@ pub fn parse_crate_from_source_str(name: ~str,
|
||||||
pub fn parse_crate_attrs_from_source_str(name: ~str,
|
pub fn parse_crate_attrs_from_source_str(name: ~str,
|
||||||
source: ~str,
|
source: ~str,
|
||||||
cfg: ast::CrateConfig,
|
cfg: ast::CrateConfig,
|
||||||
sess: @ParseSess)
|
sess: &ParseSess)
|
||||||
-> Vec<ast::Attribute> {
|
-> Vec<ast::Attribute> {
|
||||||
let mut p = new_parser_from_source_str(sess,
|
let mut p = new_parser_from_source_str(sess,
|
||||||
cfg,
|
cfg,
|
||||||
name,
|
name,
|
||||||
source);
|
source);
|
||||||
let (inner, _) = maybe_aborted(p.parse_inner_attrs_and_next(),p);
|
let (inner, _) = maybe_aborted(p.parse_inner_attrs_and_next(),p);
|
||||||
return inner;
|
inner
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_expr_from_source_str(name: ~str,
|
pub fn parse_expr_from_source_str(name: ~str,
|
||||||
source: ~str,
|
source: ~str,
|
||||||
cfg: ast::CrateConfig,
|
cfg: ast::CrateConfig,
|
||||||
sess: @ParseSess)
|
sess: &ParseSess)
|
||||||
-> @ast::Expr {
|
-> @ast::Expr {
|
||||||
let mut p = new_parser_from_source_str(sess, cfg, name, source);
|
let mut p = new_parser_from_source_str(sess, cfg, name, source);
|
||||||
maybe_aborted(p.parse_expr(), p)
|
maybe_aborted(p.parse_expr(), p)
|
||||||
|
@ -126,7 +126,7 @@ pub fn parse_expr_from_source_str(name: ~str,
|
||||||
pub fn parse_item_from_source_str(name: ~str,
|
pub fn parse_item_from_source_str(name: ~str,
|
||||||
source: ~str,
|
source: ~str,
|
||||||
cfg: ast::CrateConfig,
|
cfg: ast::CrateConfig,
|
||||||
sess: @ParseSess)
|
sess: &ParseSess)
|
||||||
-> Option<@ast::Item> {
|
-> Option<@ast::Item> {
|
||||||
let mut p = new_parser_from_source_str(sess, cfg, name, source);
|
let mut p = new_parser_from_source_str(sess, cfg, name, source);
|
||||||
let attrs = p.parse_outer_attributes();
|
let attrs = p.parse_outer_attributes();
|
||||||
|
@ -136,7 +136,7 @@ pub fn parse_item_from_source_str(name: ~str,
|
||||||
pub fn parse_meta_from_source_str(name: ~str,
|
pub fn parse_meta_from_source_str(name: ~str,
|
||||||
source: ~str,
|
source: ~str,
|
||||||
cfg: ast::CrateConfig,
|
cfg: ast::CrateConfig,
|
||||||
sess: @ParseSess)
|
sess: &ParseSess)
|
||||||
-> @ast::MetaItem {
|
-> @ast::MetaItem {
|
||||||
let mut p = new_parser_from_source_str(sess, cfg, name, source);
|
let mut p = new_parser_from_source_str(sess, cfg, name, source);
|
||||||
maybe_aborted(p.parse_meta_item(),p)
|
maybe_aborted(p.parse_meta_item(),p)
|
||||||
|
@ -146,7 +146,7 @@ pub fn parse_stmt_from_source_str(name: ~str,
|
||||||
source: ~str,
|
source: ~str,
|
||||||
cfg: ast::CrateConfig,
|
cfg: ast::CrateConfig,
|
||||||
attrs: Vec<ast::Attribute> ,
|
attrs: Vec<ast::Attribute> ,
|
||||||
sess: @ParseSess)
|
sess: &ParseSess)
|
||||||
-> @ast::Stmt {
|
-> @ast::Stmt {
|
||||||
let mut p = new_parser_from_source_str(
|
let mut p = new_parser_from_source_str(
|
||||||
sess,
|
sess,
|
||||||
|
@ -160,7 +160,7 @@ pub fn parse_stmt_from_source_str(name: ~str,
|
||||||
pub fn parse_tts_from_source_str(name: ~str,
|
pub fn parse_tts_from_source_str(name: ~str,
|
||||||
source: ~str,
|
source: ~str,
|
||||||
cfg: ast::CrateConfig,
|
cfg: ast::CrateConfig,
|
||||||
sess: @ParseSess)
|
sess: &ParseSess)
|
||||||
-> Vec<ast::TokenTree> {
|
-> Vec<ast::TokenTree> {
|
||||||
let mut p = new_parser_from_source_str(
|
let mut p = new_parser_from_source_str(
|
||||||
sess,
|
sess,
|
||||||
|
@ -174,48 +174,48 @@ pub fn parse_tts_from_source_str(name: ~str,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a new parser from a source string
|
// Create a new parser from a source string
|
||||||
pub fn new_parser_from_source_str(sess: @ParseSess,
|
pub fn new_parser_from_source_str<'a>(sess: &'a ParseSess,
|
||||||
cfg: ast::CrateConfig,
|
cfg: ast::CrateConfig,
|
||||||
name: ~str,
|
name: ~str,
|
||||||
source: ~str)
|
source: ~str)
|
||||||
-> Parser {
|
-> Parser<'a> {
|
||||||
filemap_to_parser(sess,string_to_filemap(sess,source,name),cfg)
|
filemap_to_parser(sess,string_to_filemap(sess,source,name),cfg)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a new parser, handling errors as appropriate
|
/// Create a new parser, handling errors as appropriate
|
||||||
/// if the file doesn't exist
|
/// if the file doesn't exist
|
||||||
pub fn new_parser_from_file(
|
pub fn new_parser_from_file<'a>(
|
||||||
sess: @ParseSess,
|
sess: &'a ParseSess,
|
||||||
cfg: ast::CrateConfig,
|
cfg: ast::CrateConfig,
|
||||||
path: &Path
|
path: &Path
|
||||||
) -> Parser {
|
) -> Parser<'a> {
|
||||||
filemap_to_parser(sess,file_to_filemap(sess,path,None),cfg)
|
filemap_to_parser(sess,file_to_filemap(sess,path,None),cfg)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Given a session, a crate config, a path, and a span, add
|
/// Given a session, a crate config, a path, and a span, add
|
||||||
/// the file at the given path to the codemap, and return a parser.
|
/// the file at the given path to the codemap, and return a parser.
|
||||||
/// On an error, use the given span as the source of the problem.
|
/// On an error, use the given span as the source of the problem.
|
||||||
pub fn new_sub_parser_from_file(
|
pub fn new_sub_parser_from_file<'a>(
|
||||||
sess: @ParseSess,
|
sess: &'a ParseSess,
|
||||||
cfg: ast::CrateConfig,
|
cfg: ast::CrateConfig,
|
||||||
path: &Path,
|
path: &Path,
|
||||||
sp: Span
|
sp: Span
|
||||||
) -> Parser {
|
) -> Parser<'a> {
|
||||||
filemap_to_parser(sess,file_to_filemap(sess,path,Some(sp)),cfg)
|
filemap_to_parser(sess,file_to_filemap(sess,path,Some(sp)),cfg)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Given a filemap and config, return a parser
|
/// Given a filemap and config, return a parser
|
||||||
pub fn filemap_to_parser(sess: @ParseSess,
|
pub fn filemap_to_parser<'a>(sess: &'a ParseSess,
|
||||||
filemap: @FileMap,
|
filemap: @FileMap,
|
||||||
cfg: ast::CrateConfig) -> Parser {
|
cfg: ast::CrateConfig) -> Parser<'a> {
|
||||||
tts_to_parser(sess,filemap_to_tts(sess,filemap),cfg)
|
tts_to_parser(sess,filemap_to_tts(sess,filemap),cfg)
|
||||||
}
|
}
|
||||||
|
|
||||||
// must preserve old name for now, because quote! from the *existing*
|
// must preserve old name for now, because quote! from the *existing*
|
||||||
// compiler expands into it
|
// compiler expands into it
|
||||||
pub fn new_parser_from_tts(sess: @ParseSess,
|
pub fn new_parser_from_tts<'a>(sess: &'a ParseSess,
|
||||||
cfg: ast::CrateConfig,
|
cfg: ast::CrateConfig,
|
||||||
tts: Vec<ast::TokenTree> ) -> Parser {
|
tts: Vec<ast::TokenTree>) -> Parser<'a> {
|
||||||
tts_to_parser(sess,tts,cfg)
|
tts_to_parser(sess,tts,cfg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -224,7 +224,7 @@ pub fn new_parser_from_tts(sess: @ParseSess,
|
||||||
|
|
||||||
/// Given a session and a path and an optional span (for error reporting),
|
/// Given a session and a path and an optional span (for error reporting),
|
||||||
/// add the path to the session's codemap and return the new filemap.
|
/// add the path to the session's codemap and return the new filemap.
|
||||||
pub fn file_to_filemap(sess: @ParseSess, path: &Path, spanopt: Option<Span>)
|
pub fn file_to_filemap(sess: &ParseSess, path: &Path, spanopt: Option<Span>)
|
||||||
-> @FileMap {
|
-> @FileMap {
|
||||||
let err = |msg: &str| {
|
let err = |msg: &str| {
|
||||||
match spanopt {
|
match spanopt {
|
||||||
|
@ -250,13 +250,13 @@ pub fn file_to_filemap(sess: @ParseSess, path: &Path, spanopt: Option<Span>)
|
||||||
|
|
||||||
// given a session and a string, add the string to
|
// given a session and a string, add the string to
|
||||||
// the session's codemap and return the new filemap
|
// the session's codemap and return the new filemap
|
||||||
pub fn string_to_filemap(sess: @ParseSess, source: ~str, path: ~str)
|
pub fn string_to_filemap(sess: &ParseSess, source: ~str, path: ~str)
|
||||||
-> @FileMap {
|
-> @FileMap {
|
||||||
sess.cm.new_filemap(path, source)
|
sess.cm.new_filemap(path, source)
|
||||||
}
|
}
|
||||||
|
|
||||||
// given a filemap, produce a sequence of token-trees
|
// given a filemap, produce a sequence of token-trees
|
||||||
pub fn filemap_to_tts(sess: @ParseSess, filemap: @FileMap)
|
pub fn filemap_to_tts(sess: &ParseSess, filemap: @FileMap)
|
||||||
-> Vec<ast::TokenTree> {
|
-> Vec<ast::TokenTree> {
|
||||||
// it appears to me that the cfg doesn't matter here... indeed,
|
// it appears to me that the cfg doesn't matter here... indeed,
|
||||||
// parsing tt's probably shouldn't require a parser at all.
|
// parsing tt's probably shouldn't require a parser at all.
|
||||||
|
@ -267,9 +267,9 @@ pub fn filemap_to_tts(sess: @ParseSess, filemap: @FileMap)
|
||||||
}
|
}
|
||||||
|
|
||||||
// given tts and cfg, produce a parser
|
// given tts and cfg, produce a parser
|
||||||
pub fn tts_to_parser(sess: @ParseSess,
|
pub fn tts_to_parser<'a>(sess: &'a ParseSess,
|
||||||
tts: Vec<ast::TokenTree> ,
|
tts: Vec<ast::TokenTree>,
|
||||||
cfg: ast::CrateConfig) -> Parser {
|
cfg: ast::CrateConfig) -> Parser<'a> {
|
||||||
let trdr = lexer::new_tt_reader(sess.span_diagnostic, None, tts);
|
let trdr = lexer::new_tt_reader(sess.span_diagnostic, None, tts);
|
||||||
Parser(sess, cfg, ~trdr)
|
Parser(sess, cfg, ~trdr)
|
||||||
}
|
}
|
||||||
|
@ -594,7 +594,7 @@ mod test {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test] fn parse_ident_pat () {
|
#[test] fn parse_ident_pat () {
|
||||||
let mut parser = string_to_parser(~"b");
|
let mut parser = string_to_parser(&new_parse_sess(), ~"b");
|
||||||
assert!(parser.parse_pat() ==
|
assert!(parser.parse_pat() ==
|
||||||
@ast::Pat{id: ast::DUMMY_NODE_ID,
|
@ast::Pat{id: ast::DUMMY_NODE_ID,
|
||||||
node: ast::PatIdent(
|
node: ast::PatIdent(
|
||||||
|
|
|
@ -59,7 +59,7 @@ pub trait ParserObsoleteMethods {
|
||||||
fn eat_obsolete_ident(&mut self, ident: &str) -> bool;
|
fn eat_obsolete_ident(&mut self, ident: &str) -> bool;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ParserObsoleteMethods for Parser {
|
impl<'a> ParserObsoleteMethods for Parser<'a> {
|
||||||
/// Reports an obsolete syntax non-fatal error.
|
/// Reports an obsolete syntax non-fatal error.
|
||||||
fn obsolete(&mut 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 {
|
||||||
|
|
|
@ -284,8 +284,8 @@ struct ParsedItemsAndViewItems {
|
||||||
|
|
||||||
/* ident is handled by common.rs */
|
/* ident is handled by common.rs */
|
||||||
|
|
||||||
pub fn Parser(sess: @ParseSess, cfg: ast::CrateConfig, rdr: ~Reader:)
|
pub fn Parser<'a>(sess: &'a ParseSess, cfg: ast::CrateConfig, rdr: ~Reader:)
|
||||||
-> Parser {
|
-> Parser<'a> {
|
||||||
let tok0 = rdr.next_token();
|
let tok0 = rdr.next_token();
|
||||||
let span = tok0.sp;
|
let span = tok0.sp;
|
||||||
let placeholder = TokenAndSpan {
|
let placeholder = TokenAndSpan {
|
||||||
|
@ -320,8 +320,8 @@ pub fn Parser(sess: @ParseSess, cfg: ast::CrateConfig, rdr: ~Reader:)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Parser {
|
pub struct Parser<'a> {
|
||||||
sess: @ParseSess,
|
sess: &'a ParseSess,
|
||||||
cfg: CrateConfig,
|
cfg: CrateConfig,
|
||||||
// the current token:
|
// the current token:
|
||||||
token: token::Token,
|
token: token::Token,
|
||||||
|
@ -354,7 +354,7 @@ fn is_plain_ident_or_underscore(t: &token::Token) -> bool {
|
||||||
is_plain_ident(t) || *t == token::UNDERSCORE
|
is_plain_ident(t) || *t == token::UNDERSCORE
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Parser {
|
impl<'a> Parser<'a> {
|
||||||
// convert a token to a string using self's reader
|
// convert a token to a string using self's reader
|
||||||
pub fn token_to_str(token: &token::Token) -> ~str {
|
pub fn token_to_str(token: &token::Token) -> ~str {
|
||||||
token::to_str(token)
|
token::to_str(token)
|
||||||
|
|
|
@ -17,32 +17,19 @@ use parse::token;
|
||||||
|
|
||||||
use std::vec_ng::Vec;
|
use std::vec_ng::Vec;
|
||||||
|
|
||||||
// map a string to tts, using a made-up filename: return both the TokenTree's
|
|
||||||
// and the ParseSess
|
|
||||||
pub fn string_to_tts_and_sess (source_str : ~str) -> (Vec<ast::TokenTree> , @ParseSess) {
|
|
||||||
let ps = new_parse_sess();
|
|
||||||
(filemap_to_tts(ps,string_to_filemap(ps,source_str,~"bogofile")),ps)
|
|
||||||
}
|
|
||||||
|
|
||||||
// map a string to tts, using a made-up filename:
|
// map a string to tts, using a made-up filename:
|
||||||
pub fn string_to_tts(source_str : ~str) -> Vec<ast::TokenTree> {
|
pub fn string_to_tts(source_str: ~str) -> Vec<ast::TokenTree> {
|
||||||
let (tts,_) = string_to_tts_and_sess(source_str);
|
|
||||||
tts
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn string_to_parser_and_sess(source_str: ~str) -> (Parser,@ParseSess) {
|
|
||||||
let ps = new_parse_sess();
|
let ps = new_parse_sess();
|
||||||
(new_parser_from_source_str(ps,Vec::new(),~"bogofile",source_str),ps)
|
filemap_to_tts(&ps, string_to_filemap(&ps, source_str,~"bogofile"))
|
||||||
}
|
}
|
||||||
|
|
||||||
// map string to parser (via tts)
|
// map string to parser (via tts)
|
||||||
pub fn string_to_parser(source_str: ~str) -> Parser {
|
pub fn string_to_parser<'a>(ps: &'a ParseSess, source_str: ~str) -> Parser<'a> {
|
||||||
let (p,_) = string_to_parser_and_sess(source_str);
|
new_parser_from_source_str(ps, Vec::new(), ~"bogofile", source_str)
|
||||||
p
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn with_error_checking_parse<T>(s: ~str, f: |&mut Parser| -> T) -> T {
|
fn with_error_checking_parse<T>(s: ~str, f: |&mut Parser| -> T) -> T {
|
||||||
let mut p = string_to_parser(s);
|
let mut p = string_to_parser(&new_parse_sess(), s);
|
||||||
let x = f(&mut p);
|
let x = f(&mut p);
|
||||||
p.abort_if_errors();
|
p.abort_if_errors();
|
||||||
x
|
x
|
||||||
|
@ -55,12 +42,6 @@ pub fn string_to_crate (source_str : ~str) -> ast::Crate {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// parse a string, return a crate and the ParseSess
|
|
||||||
pub fn string_to_crate_and_sess (source_str : ~str) -> (ast::Crate,@ParseSess) {
|
|
||||||
let (mut p,ps) = string_to_parser_and_sess(source_str);
|
|
||||||
(p.parse_crate_mod(),ps)
|
|
||||||
}
|
|
||||||
|
|
||||||
// parse a string, return an expr
|
// parse a string, return an expr
|
||||||
pub fn string_to_expr (source_str : ~str) -> @ast::Expr {
|
pub fn string_to_expr (source_str : ~str) -> @ast::Expr {
|
||||||
with_error_checking_parse(source_str, |p| {
|
with_error_checking_parse(source_str, |p| {
|
||||||
|
@ -84,8 +65,8 @@ pub fn string_to_stmt(source_str : ~str) -> @ast::Stmt {
|
||||||
|
|
||||||
// parse a string, return a pat. Uses "irrefutable"... which doesn't
|
// parse a string, return a pat. Uses "irrefutable"... which doesn't
|
||||||
// (currently) affect parsing.
|
// (currently) affect parsing.
|
||||||
pub fn string_to_pat(source_str : ~str) -> @ast::Pat {
|
pub fn string_to_pat(source_str: ~str) -> @ast::Pat {
|
||||||
string_to_parser(source_str).parse_pat()
|
string_to_parser(&new_parse_sess(), source_str).parse_pat()
|
||||||
}
|
}
|
||||||
|
|
||||||
// convert a vector of strings to a vector of ast::Ident's
|
// convert a vector of strings to a vector of ast::Ident's
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue