syntax: Put the main parser interface in mod parse

This commit is contained in:
Brian Anderson 2012-04-17 23:34:48 -07:00
parent 2c0cb901c8
commit 9a8a04629e
15 changed files with 214 additions and 199 deletions

View File

@ -1,7 +1,7 @@
// cargo.rs - Rust package manager
import rustc::syntax::{ast, codemap};
import rustc::syntax::parse::parser;
import rustc::syntax::parse;
import rustc::util::filesearch::{get_cargo_root, get_cargo_root_nearest,
get_cargo_sysroot, libdir};
import rustc::driver::diagnostic;
@ -119,7 +119,7 @@ fn load_pkg(filename: str) -> option<pkg> {
mut chpos: 0u,
mut byte_pos: 0u
};
let c = parser::parse_crate_from_crate_file(filename, [], sess);
let c = parse::parse_crate_from_crate_file(filename, [], sess);
let mut name = none;
let mut vers = none;

View File

@ -1,7 +1,7 @@
import io::writer_util;
import rustc::syntax::{ast, ast_util, fold, visit, codemap};
import rustc::syntax::parse::parser;
import rustc::syntax::parse;
import rustc::syntax::print::pprust;
import rustc::driver::diagnostic;
@ -429,7 +429,7 @@ fn parse_and_print(code: @str) -> str {
mut byte_pos: 0u
};
write_file(filename, *code);
let crate = parser::parse_crate_from_source_str(
let crate = parse::parse_crate_from_source_str(
filename, code, [], sess);
io::with_str_reader(*code) { |rdr|
as_str(bind pprust::print_crate(sess.cm,
@ -579,7 +579,7 @@ fn check_variants(files: [str], cx: context) {
mut byte_pos: 0u
};
let crate =
parser::parse_crate_from_source_str(
parse::parse_crate_from_source_str(
file,
s, [], sess);
io::with_str_reader(*s) { |rdr|

View File

@ -46,7 +46,7 @@ fn syntax_expander_table() -> hashmap<str, syntax_extension> {
iface ext_ctxt {
fn codemap() -> codemap;
fn parse_sess() -> parser::parse_sess;
fn parse_sess() -> parse::parse_sess;
fn cfg() -> ast::crate_cfg;
fn print_backtrace();
fn backtrace() -> expn_info;
@ -60,14 +60,14 @@ iface ext_ctxt {
fn next_id() -> ast::node_id;
}
fn mk_ctxt(parse_sess: parser::parse_sess,
fn mk_ctxt(parse_sess: parse::parse_sess,
cfg: ast::crate_cfg) -> ext_ctxt {
type ctxt_repr = {parse_sess: parser::parse_sess,
type ctxt_repr = {parse_sess: parse::parse_sess,
cfg: ast::crate_cfg,
mut backtrace: expn_info};
impl of ext_ctxt for ctxt_repr {
fn codemap() -> codemap { self.parse_sess.cm }
fn parse_sess() -> parser::parse_sess { self.parse_sess }
fn parse_sess() -> parse::parse_sess { self.parse_sess }
fn cfg() -> ast::crate_cfg { self.cfg }
fn print_backtrace() { }
fn backtrace() -> expn_info { self.backtrace }
@ -111,7 +111,7 @@ fn mk_ctxt(parse_sess: parser::parse_sess,
self.parse_sess.span_diagnostic.handler().bug(msg);
}
fn next_id() -> ast::node_id {
ret parser::next_node_id(self.parse_sess);
ret parse::next_node_id(self.parse_sess);
}
}
let imp : ctxt_repr = {

View File

@ -5,7 +5,7 @@ import fold::*;
import ext::base::*;
import ext::qquote::{qq_helper};
import parse::parser;
import parse::parser::parse_expr_from_source_str;
import parse::parse_expr_from_source_str;
import codemap::{span, expanded_from};
@ -110,7 +110,7 @@ fn core_macros() -> str {
}";
}
fn expand_crate(parse_sess: parser::parse_sess,
fn expand_crate(parse_sess: parse::parse_sess,
cfg: ast::crate_cfg, c: @crate) -> @crate {
let exts = syntax_expander_table();
let afp = default_ast_fold();

View File

@ -5,7 +5,7 @@ import visit::*;
import ext::base::*;
import ext::build::*;
import parse::parser;
import parse::parser::{parser, parse_from_source_str};
import parse::parser::parse_from_source_str;
import print::*;
import io::*;

166
src/librustsyntax/parse.rs Normal file
View File

@ -0,0 +1,166 @@
export parse_sess;
export next_node_id;
export new_parser_from_file;
export new_parser_from_source_str;
export parse_crate_from_file;
export parse_crate_from_crate_file;
export parse_crate_from_source_str;
export parse_expr_from_source_str;
export parse_from_source_str;
import parser::parser;
import ast::node_id;
import util::interner;
import lexer::reader;
type parse_sess = @{
cm: codemap::codemap,
mut next_id: node_id,
span_diagnostic: diagnostic::span_handler,
// these two must be kept up to date
mut chpos: uint,
mut byte_pos: uint
};
fn next_node_id(sess: parse_sess) -> node_id {
let rv = sess.next_id;
sess.next_id += 1;
// ID 0 is reserved for the crate and doesn't actually exist in the AST
assert rv != 0;
ret rv;
}
fn new_parser_from_file(sess: parse_sess, cfg: ast::crate_cfg, path: str,
ftype: parser::file_type) ->
parser {
let src = alt io::read_whole_file_str(path) {
result::ok(src) {
// FIXME: This copy is unfortunate
@src
}
result::err(e) {
sess.span_diagnostic.handler().fatal(e)
}
};
let filemap = codemap::new_filemap(path, src,
sess.chpos, sess.byte_pos);
sess.cm.files += [filemap];
let itr = @interner::mk(str::hash, str::eq);
let rdr = lexer::new_reader(sess.span_diagnostic, filemap, itr);
ret new_parser(sess, cfg, rdr, ftype);
}
fn new_parser_from_source_str(sess: parse_sess, cfg: ast::crate_cfg,
name: str, ss: codemap::file_substr,
source: @str) -> parser {
let ftype = parser::SOURCE_FILE;
let filemap = codemap::new_filemap_w_substr
(name, ss, source, sess.chpos, sess.byte_pos);
sess.cm.files += [filemap];
let itr = @interner::mk(str::hash, str::eq);
let rdr = lexer::new_reader(sess.span_diagnostic,
filemap, itr);
ret new_parser(sess, cfg, rdr, ftype);
}
fn new_parser(sess: parse_sess, cfg: ast::crate_cfg, rdr: lexer::reader,
ftype: parser::file_type) -> parser {
let tok0 = lexer::next_token(rdr);
let span0 = ast_util::mk_sp(tok0.chpos, rdr.chpos);
@{sess: sess,
cfg: cfg,
file_type: ftype,
mut token: tok0.tok,
mut span: span0,
mut last_span: span0,
mut buffer: [],
mut restriction: parser::UNRESTRICTED,
reader: rdr,
binop_precs: prec::binop_prec_table(),
bad_expr_words: token::bad_expr_word_table()}
}
fn parse_crate_from_crate_file(input: str, cfg: ast::crate_cfg,
sess: parse_sess) -> @ast::crate {
let p = new_parser_from_file(sess, cfg, input, parser::CRATE_FILE);
let lo = p.span.lo;
let prefix = path::dirname(p.reader.filemap.name);
let leading_attrs = parser::parse_inner_attrs_and_next(p);
let crate_attrs = leading_attrs.inner;
let first_cdir_attr = leading_attrs.next;
let cdirs = parser::parse_crate_directives(
p, token::EOF, first_cdir_attr);
sess.chpos = p.reader.chpos;
sess.byte_pos = sess.byte_pos + p.reader.pos;
let cx =
@{p: p,
sess: sess,
cfg: p.cfg};
let (companionmod, _) = path::splitext(path::basename(input));
let (m, attrs) = eval::eval_crate_directives_to_mod(
cx, cdirs, prefix, option::some(companionmod));
let mut hi = p.span.hi;
parser::expect(p, token::EOF);
ret @ast_util::respan(ast_util::mk_sp(lo, hi),
{directives: cdirs,
module: m,
attrs: crate_attrs + attrs,
config: p.cfg});
}
fn parse_crate_from_file(input: str, cfg: ast::crate_cfg, sess: parse_sess) ->
@ast::crate {
if str::ends_with(input, ".rc") {
parse_crate_from_crate_file(input, cfg, sess)
} else if str::ends_with(input, ".rs") {
parse_crate_from_source_file(input, cfg, sess)
} else {
sess.span_diagnostic.handler().fatal("unknown input file type: " +
input)
}
}
fn parse_crate_from_source_file(input: str, cfg: ast::crate_cfg,
sess: parse_sess) -> @ast::crate {
let p = new_parser_from_file(sess, cfg, input, parser::SOURCE_FILE);
let r = parser::parse_crate_mod(p, cfg);
sess.chpos = p.reader.chpos;
sess.byte_pos = sess.byte_pos + p.reader.pos;
ret r;
}
fn parse_expr_from_source_str(name: str, source: @str, cfg: ast::crate_cfg,
sess: parse_sess) -> @ast::expr {
let p = new_parser_from_source_str(
sess, cfg, name, codemap::fss_none, source);
let r = parser::parse_expr(p);
sess.chpos = p.reader.chpos;
sess.byte_pos = sess.byte_pos + p.reader.pos;
ret r;
}
fn parse_crate_from_source_str(name: str, source: @str, cfg: ast::crate_cfg,
sess: parse_sess) -> @ast::crate {
let p = new_parser_from_source_str(
sess, cfg, name, codemap::fss_none, source);
let r = parser::parse_crate_mod(p, cfg);
sess.chpos = p.reader.chpos;
sess.byte_pos = sess.byte_pos + p.reader.pos;
ret r;
}
fn parse_from_source_str<T>(f: fn (p: parser) -> T,
name: str, ss: codemap::file_substr,
source: @str, cfg: ast::crate_cfg,
sess: parse_sess)
-> T
{
let p = new_parser_from_source_str(sess, cfg, name, ss, source);
let r = f(p);
if !p.reader.is_eof() {
p.reader.fatal("expected end-of-string");
}
sess.chpos = p.reader.chpos;
sess.byte_pos = sess.byte_pos + p.reader.pos;
ret r;
}

View File

@ -1,5 +1,5 @@
import attr;
import parser::{parser, new_parser_from_file,
import parser::{parser,
parse_inner_attrs_and_next,
parse_mod_items, SOURCE_FILE};
@ -7,7 +7,7 @@ export eval_crate_directives_to_mod;
type ctx =
@{p: parser,
sess: parser::parse_sess,
sess: parse::parse_sess,
cfg: ast::crate_cfg};
fn eval_crate_directives(cx: ctx, cdirs: [@ast::crate_directive], prefix: str,

View File

@ -4,33 +4,31 @@ import std::map::{hashmap, str_hash};
import token::{can_begin_expr, is_ident, is_plain_ident};
import codemap::{span,fss_none};
import util::interner;
import ast::{node_id, spanned};
import ast::spanned;
import ast_util::{mk_sp, ident_to_path};
import lexer::reader;
import prec::{op_spec, binop_prec_table, as_prec};
import prec::{op_spec, as_prec};
export expect;
export file_type;
export mk_item;
export next_node_id;
export new_parser_from_file;
export new_parser_from_source_str;
export restriction;
export parser;
export parse_crate_from_file;
export parse_crate_from_crate_file;
export parse_crate_from_source_str;
export parse_crate_directives;
export parse_crate_mod;
export parse_expr;
export parse_expr_from_source_str;
export parse_from_source_str;
export parse_inner_attrs_and_next;
export parse_item;
export parse_mod_items;
export parse_outer_attributes;
export parse_pat;
export parse_sess;
export parse_stmt;
export parse_ty;
// FIXME: #ast expects to find this here but it's actually defined in `parse`
import parse_from_source_str;
export parse_from_source_str;
enum restriction {
UNRESTRICTED,
RESTRICT_STMT_EXPR,
@ -40,23 +38,6 @@ enum restriction {
enum file_type { CRATE_FILE, SOURCE_FILE, }
type parse_sess = @{
cm: codemap::codemap,
mut next_id: node_id,
span_diagnostic: diagnostic::span_handler,
// these two must be kept up to date
mut chpos: uint,
mut byte_pos: uint
};
fn next_node_id(sess: parse_sess) -> node_id {
let rv = sess.next_id;
sess.next_id += 1;
// ID 0 is reserved for the crate and doesn't actually exist in the AST
assert rv != 0;
ret rv;
}
type parser = @{
sess: parse_sess,
cfg: ast::crate_cfg,
@ -111,56 +92,6 @@ impl parser for parser {
fn get_id() -> node_id { next_node_id(self.sess) }
}
fn new_parser_from_file(sess: parse_sess, cfg: ast::crate_cfg, path: str,
ftype: file_type) ->
parser {
let src = alt io::read_whole_file_str(path) {
result::ok(src) {
// FIXME: This copy is unfortunate
@src
}
result::err(e) {
sess.span_diagnostic.handler().fatal(e)
}
};
let filemap = codemap::new_filemap(path, src,
sess.chpos, sess.byte_pos);
sess.cm.files += [filemap];
let itr = @interner::mk(str::hash, str::eq);
let rdr = lexer::new_reader(sess.span_diagnostic, filemap, itr);
ret new_parser(sess, cfg, rdr, ftype);
}
fn new_parser_from_source_str(sess: parse_sess, cfg: ast::crate_cfg,
name: str, ss: codemap::file_substr,
source: @str) -> parser {
let ftype = SOURCE_FILE;
let filemap = codemap::new_filemap_w_substr
(name, ss, source, sess.chpos, sess.byte_pos);
sess.cm.files += [filemap];
let itr = @interner::mk(str::hash, str::eq);
let rdr = lexer::new_reader(sess.span_diagnostic,
filemap, itr);
ret new_parser(sess, cfg, rdr, ftype);
}
fn new_parser(sess: parse_sess, cfg: ast::crate_cfg, rdr: reader,
ftype: file_type) -> parser {
let tok0 = lexer::next_token(rdr);
let span0 = ast_util::mk_sp(tok0.chpos, rdr.chpos);
@{sess: sess,
cfg: cfg,
file_type: ftype,
mut token: tok0.tok,
mut span: span0,
mut last_span: span0,
mut buffer: [],
mut restriction: UNRESTRICTED,
reader: rdr,
binop_precs: binop_prec_table(),
bad_expr_words: token::bad_expr_word_table()}
}
fn token_to_str(reader: reader, token: token::token) -> str {
token::to_str(*reader.interner, token)
}
@ -2690,50 +2621,6 @@ fn parse_native_view(p: parser) -> [@ast::view_item] {
maybe_parse_view_while(p, [], is_view_item)
}
fn parse_crate_from_source_file(input: str, cfg: ast::crate_cfg,
sess: parse_sess) -> @ast::crate {
let p = new_parser_from_file(sess, cfg, input, SOURCE_FILE);
let r = parse_crate_mod(p, cfg);
sess.chpos = p.reader.chpos;
sess.byte_pos = sess.byte_pos + p.reader.pos;
ret r;
}
fn parse_expr_from_source_str(name: str, source: @str, cfg: ast::crate_cfg,
sess: parse_sess) -> @ast::expr {
let p = new_parser_from_source_str(sess, cfg, name, fss_none, source);
let r = parse_expr(p);
sess.chpos = p.reader.chpos;
sess.byte_pos = sess.byte_pos + p.reader.pos;
ret r;
}
fn parse_from_source_str<T>(f: fn (p: parser) -> T,
name: str, ss: codemap::file_substr,
source: @str, cfg: ast::crate_cfg,
sess: parse_sess)
-> T
{
let p = new_parser_from_source_str(sess, cfg, name, ss, source);
let r = f(p);
if !p.reader.is_eof() {
p.reader.fatal("expected end-of-string");
}
sess.chpos = p.reader.chpos;
sess.byte_pos = sess.byte_pos + p.reader.pos;
ret r;
}
fn parse_crate_from_source_str(name: str, source: @str, cfg: ast::crate_cfg,
sess: parse_sess) -> @ast::crate {
let p = new_parser_from_source_str(sess, cfg, name, fss_none, source);
let r = parse_crate_mod(p, cfg);
sess.chpos = p.reader.chpos;
sess.byte_pos = sess.byte_pos + p.reader.pos;
ret r;
}
// Parses a source module as a crate
fn parse_crate_mod(p: parser, _cfg: ast::crate_cfg) -> @ast::crate {
let lo = p.span.lo;
@ -2822,45 +2709,6 @@ fn parse_crate_directives(p: parser, term: token::token,
ret cdirs;
}
fn parse_crate_from_crate_file(input: str, cfg: ast::crate_cfg,
sess: parse_sess) -> @ast::crate {
let p = new_parser_from_file(sess, cfg, input, CRATE_FILE);
let lo = p.span.lo;
let prefix = path::dirname(p.reader.filemap.name);
let leading_attrs = parse_inner_attrs_and_next(p);
let crate_attrs = leading_attrs.inner;
let first_cdir_attr = leading_attrs.next;
let cdirs = parse_crate_directives(p, token::EOF, first_cdir_attr);
sess.chpos = p.reader.chpos;
sess.byte_pos = sess.byte_pos + p.reader.pos;
let cx =
@{p: p,
sess: sess,
cfg: p.cfg};
let (companionmod, _) = path::splitext(path::basename(input));
let (m, attrs) = eval::eval_crate_directives_to_mod(
cx, cdirs, prefix, option::some(companionmod));
let mut hi = p.span.hi;
expect(p, token::EOF);
ret @spanned(lo, hi,
{directives: cdirs,
module: m,
attrs: crate_attrs + attrs,
config: p.cfg});
}
fn parse_crate_from_file(input: str, cfg: ast::crate_cfg, sess: parse_sess) ->
@ast::crate {
if str::ends_with(input, ".rc") {
parse_crate_from_crate_file(input, cfg, sess)
} else if str::ends_with(input, ".rs") {
parse_crate_from_source_file(input, cfg, sess)
} else {
sess.span_diagnostic.handler().fatal("unknown input file type: " +
input)
}
}
//
// Local Variables:
// mode: rust

View File

@ -1,7 +1,7 @@
// -*- rust -*-
import metadata::{creader, cstore};
import session::session;
import syntax::parse::{parser};
import syntax::parse;
import syntax::{ast, codemap};
import syntax::attr;
import middle::{trans, resolve, freevars, kind, ty, typeck, fn_usage,
@ -76,10 +76,10 @@ fn input_is_stdin(filename: str) -> bool { filename == "-" }
fn parse_input(sess: session, cfg: ast::crate_cfg, input: str)
-> @ast::crate {
if !input_is_stdin(input) {
parser::parse_crate_from_file(input, cfg, sess.parse_sess)
parse::parse_crate_from_file(input, cfg, sess.parse_sess)
} else {
let src = @str::from_bytes(io::stdin().read_whole_stream());
parser::parse_crate_from_source_str(input, src, cfg, sess.parse_sess)
parse::parse_crate_from_source_str(input, src, cfg, sess.parse_sess)
}
}

View File

@ -3,7 +3,7 @@ import syntax::{ast, codemap};
import syntax::ast::node_id;
import codemap::span;
import syntax::ast::{int_ty, uint_ty, float_ty};
import syntax::parse::parser::parse_sess;
import syntax::parse::parse_sess;
import util::filesearch;
import back::target_strs;
import middle::lint;
@ -106,7 +106,7 @@ impl session for session {
self.span_diagnostic.handler().unimpl(msg)
}
fn next_node_id() -> ast::node_id {
ret syntax::parse::parser::next_node_id(self.parse_sess);
ret syntax::parse::next_node_id(self.parse_sess);
}
fn diagnostic() -> diagnostic::span_handler {
self.span_diagnostic

View File

@ -34,7 +34,7 @@ import e = encoder;
// used in testing:
import driver::diagnostic;
import syntax::codemap;
import syntax::parse::parser;
import syntax::parse;
import syntax::print::pprust;
export encode_inlined_item;
@ -956,7 +956,7 @@ fn decode_item_ast(par_doc: ebml::doc) -> @ast::item {
}
#[cfg(test)]
fn new_parse_sess() -> parser::parse_sess {
fn new_parse_sess() -> parse::parse_sess {
let cm = codemap::new_codemap();
let handler = diagnostic::mk_handler(option::none);
let sess = @{
@ -972,7 +972,7 @@ fn new_parse_sess() -> parser::parse_sess {
#[cfg(test)]
iface fake_ext_ctxt {
fn cfg() -> ast::crate_cfg;
fn parse_sess() -> parser::parse_sess;
fn parse_sess() -> parse::parse_sess;
}
#[cfg(test)]
@ -981,7 +981,7 @@ type fake_session = ();
#[cfg(test)]
impl of fake_ext_ctxt for fake_session {
fn cfg() -> ast::crate_cfg { [] }
fn parse_sess() -> parser::parse_sess { new_parse_sess() }
fn parse_sess() -> parse::parse_sess { new_parse_sess() }
}
#[cfg(test)]

View File

@ -21,6 +21,7 @@ type crate_attrs = {
mod test {
fn parse_attributes(source: str) -> [ast::attribute] {
import rustc::syntax::parse;
import rustc::syntax::parse::parser;
import rustc::syntax::codemap;
import rustc::driver::diagnostic;
@ -34,7 +35,7 @@ mod test {
mut chpos: 0u,
mut byte_pos: 0u
};
let parser = parser::new_parser_from_source_str(
let parser = parse::new_parser_from_source_str(
parse_sess, [], "-", codemap::fss_none, @source);
parser::parse_outer_attributes(parser)

View File

@ -5,11 +5,11 @@ import rustc::driver::session;
import rustc::driver::diagnostic;
import rustc::syntax::ast;
import rustc::syntax::codemap;
import rustc::syntax::parse::parser;
import rustc::syntax::parse;
export from_file, from_str, from_file_sess, from_str_sess;
fn new_parse_sess() -> parser::parse_sess {
fn new_parse_sess() -> parse::parse_sess {
let cm = codemap::new_codemap();
let handler = diagnostic::mk_handler(none);
let sess = @{
@ -23,21 +23,21 @@ fn new_parse_sess() -> parser::parse_sess {
}
fn from_file(file: str) -> @ast::crate {
parser::parse_crate_from_file(
parse::parse_crate_from_file(
file, [], new_parse_sess())
}
fn from_str(source: str) -> @ast::crate {
parser::parse_crate_from_source_str(
parse::parse_crate_from_source_str(
"-", @source, [], new_parse_sess())
}
fn from_file_sess(sess: session::session, file: str) -> @ast::crate {
parser::parse_crate_from_file(file, cfg(sess), sess.parse_sess)
parse::parse_crate_from_file(file, cfg(sess), sess.parse_sess)
}
fn from_str_sess(sess: session::session, source: str) -> @ast::crate {
parser::parse_crate_from_source_str(
parse::parse_crate_from_source_str(
"-", @source, cfg(sess), sess.parse_sess)
}

View File

@ -9,10 +9,10 @@ import io::*;
import rustc::driver::diagnostic;
import rustc::syntax::ast;
import rustc::syntax::codemap;
import rustc::syntax::parse::parser;
import rustc::syntax::parse;
import rustc::syntax::print::*;
fn new_parse_sess() -> parser::parse_sess {
fn new_parse_sess() -> parse::parse_sess {
fail;
}
@ -23,7 +23,7 @@ iface fake_ext_ctxt {
type fake_options = {cfg: ast::crate_cfg};
type fake_session = {opts: @fake_options,
parse_sess: parser::parse_sess};
parse_sess: parse::parse_sess};
impl of fake_ext_ctxt for fake_session {
fn session() -> fake_session {self}

View File

@ -9,10 +9,10 @@ import io::*;
import rustc::driver::diagnostic;
import rustc::syntax::ast;
import rustc::syntax::codemap;
import rustc::syntax::parse::parser;
import rustc::syntax::parse;
import rustc::syntax::print::*;
fn new_parse_sess() -> parser::parse_sess {
fn new_parse_sess() -> parse::parse_sess {
let cm = codemap::new_codemap();
let handler = diagnostic::mk_handler(option::none);
let sess = @{
@ -27,14 +27,14 @@ fn new_parse_sess() -> parser::parse_sess {
iface fake_ext_ctxt {
fn cfg() -> ast::crate_cfg;
fn parse_sess() -> parser::parse_sess;
fn parse_sess() -> parse::parse_sess;
}
type fake_session = ();
impl of fake_ext_ctxt for fake_session {
fn cfg() -> ast::crate_cfg { [] }
fn parse_sess() -> parser::parse_sess { new_parse_sess() }
fn parse_sess() -> parse::parse_sess { new_parse_sess() }
}
fn mk_ctxt() -> fake_ext_ctxt {