syntax: Reorganize mod parse
This commit is contained in:
parent
7fa7e56341
commit
087b12ac29
@ -1,3 +1,5 @@
|
|||||||
|
#[doc = "The main parser interface"];
|
||||||
|
|
||||||
export parse_sess;
|
export parse_sess;
|
||||||
export next_node_id;
|
export next_node_id;
|
||||||
export new_parser_from_file;
|
export new_parser_from_file;
|
||||||
@ -22,63 +24,16 @@ type parse_sess = @{
|
|||||||
mut byte_pos: uint
|
mut byte_pos: uint
|
||||||
};
|
};
|
||||||
|
|
||||||
fn next_node_id(sess: parse_sess) -> node_id {
|
fn parse_crate_from_file(input: str, cfg: ast::crate_cfg, sess: parse_sess) ->
|
||||||
let rv = sess.next_id;
|
@ast::crate {
|
||||||
sess.next_id += 1;
|
if str::ends_with(input, ".rc") {
|
||||||
// ID 0 is reserved for the crate and doesn't actually exist in the AST
|
parse_crate_from_crate_file(input, cfg, sess)
|
||||||
assert rv != 0;
|
} else if str::ends_with(input, ".rs") {
|
||||||
ret rv;
|
parse_crate_from_source_file(input, cfg, sess)
|
||||||
}
|
} else {
|
||||||
|
sess.span_diagnostic.handler().fatal("unknown input file type: " +
|
||||||
fn new_parser_from_file(sess: parse_sess, cfg: ast::crate_cfg, path: str,
|
input)
|
||||||
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(),
|
|
||||||
keywords: token::keyword_table(),
|
|
||||||
bad_expr_words: token::bad_expr_word_table()}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_crate_from_crate_file(input: str, cfg: ast::crate_cfg,
|
fn parse_crate_from_crate_file(input: str, cfg: ast::crate_cfg,
|
||||||
@ -109,18 +64,6 @@ fn parse_crate_from_crate_file(input: str, cfg: ast::crate_cfg,
|
|||||||
config: p.cfg});
|
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,
|
fn parse_crate_from_source_file(input: str, cfg: ast::crate_cfg,
|
||||||
sess: parse_sess) -> @ast::crate {
|
sess: parse_sess) -> @ast::crate {
|
||||||
let p = new_parser_from_file(sess, cfg, input, parser::SOURCE_FILE);
|
let p = new_parser_from_file(sess, cfg, input, parser::SOURCE_FILE);
|
||||||
@ -130,6 +73,16 @@ fn parse_crate_from_source_file(input: str, cfg: ast::crate_cfg,
|
|||||||
ret r;
|
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_expr_from_source_str(name: str, source: @str, cfg: ast::crate_cfg,
|
fn parse_expr_from_source_str(name: str, source: @str, cfg: ast::crate_cfg,
|
||||||
sess: parse_sess) -> @ast::expr {
|
sess: parse_sess) -> @ast::expr {
|
||||||
let p = new_parser_from_source_str(
|
let p = new_parser_from_source_str(
|
||||||
@ -140,16 +93,6 @@ fn parse_expr_from_source_str(name: str, source: @str, cfg: ast::crate_cfg,
|
|||||||
ret r;
|
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,
|
fn parse_from_source_str<T>(f: fn (p: parser) -> T,
|
||||||
name: str, ss: codemap::file_substr,
|
name: str, ss: codemap::file_substr,
|
||||||
source: @str, cfg: ast::crate_cfg,
|
source: @str, cfg: ast::crate_cfg,
|
||||||
@ -165,3 +108,62 @@ fn parse_from_source_str<T>(f: fn (p: parser) -> T,
|
|||||||
sess.byte_pos = sess.byte_pos + p.reader.pos;
|
sess.byte_pos = sess.byte_pos + p.reader.pos;
|
||||||
ret r;
|
ret r;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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(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(),
|
||||||
|
keywords: token::keyword_table(),
|
||||||
|
bad_expr_words: token::bad_expr_word_table()}
|
||||||
|
}
|
||||||
|
|
||||||
|
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_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);
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user