Use an impl to access parse/common.rs

This commit is contained in:
Paul Stansifer 2012-05-24 12:38:45 -07:00
parent 829c983479
commit 09652c8f5f
4 changed files with 472 additions and 462 deletions

View File

@ -13,6 +13,7 @@ export parse_item_from_source_str;
export parse_from_source_str;
import parser::parser;
import common::parser_common;
import ast::node_id;
import util::interner;
import lexer::reader;
@ -56,7 +57,7 @@ fn parse_crate_from_crate_file(input: str, cfg: ast::crate_cfg,
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);
p.expect(token::EOF);
ret @ast_util::respan(ast_util::mk_sp(lo, hi),
{directives: cdirs,
module: m,

View File

@ -1,9 +1,6 @@
import either::{either, left, right};
import ast_util::spanned;
import common::{parse_seq,
seq_sep,
expect,
parse_ident};
import common::{parser_common, seq_sep};
export attr_or_ext;
export parse_outer_attributes;
@ -45,15 +42,15 @@ fn parse_outer_attributes(p: parser) -> [ast::attribute] {
fn parse_attribute(p: parser, style: ast::attr_style) -> ast::attribute {
let lo = p.span.lo;
expect(p, token::POUND);
p.expect(token::POUND);
ret parse_attribute_naked(p, style, lo);
}
fn parse_attribute_naked(p: parser, style: ast::attr_style, lo: uint) ->
ast::attribute {
expect(p, token::LBRACKET);
p.expect(token::LBRACKET);
let meta_item = parse_meta_item(p);
expect(p, token::RBRACKET);
p.expect(token::RBRACKET);
let mut hi = p.span.hi;
ret spanned(lo, hi, {style: style, value: *meta_item});
}
@ -91,7 +88,7 @@ fn parse_inner_attrs_and_next(p: parser) ->
fn parse_meta_item(p: parser) -> @ast::meta_item {
let lo = p.span.lo;
let ident = parse_ident(p);
let ident = p.parse_ident();
alt p.token {
token::EQ {
p.bump();
@ -112,8 +109,8 @@ fn parse_meta_item(p: parser) -> @ast::meta_item {
}
fn parse_meta_seq(p: parser) -> [@ast::meta_item] {
ret parse_seq(token::LPAREN, token::RPAREN, seq_sep(token::COMMA),
p, {|p| parse_meta_item(p)}).node;
ret p.parse_seq(token::LPAREN, token::RPAREN, seq_sep(token::COMMA),
{|p| parse_meta_item(p)}).node;
}
fn parse_optional_meta(p: parser) -> [@ast::meta_item] {

View File

@ -2,169 +2,6 @@ import std::map::{hashmap};
import ast_util::spanned;
import parser::parser;
fn token_to_str(reader: reader, token: token::token) -> str {
token::to_str(*reader.interner, token)
}
fn unexpected_last(p: parser, t: token::token) -> ! {
p.span_fatal(p.last_span,
"unexpected token: '" + token_to_str(p.reader, t) + "'");
}
fn unexpected(p: parser) -> ! {
p.fatal("unexpected token: '" + token_to_str(p.reader, p.token) + "'");
}
fn expect(p: parser, t: token::token) {
if p.token == t {
p.bump();
} else {
let mut s: str = "expecting '";
s += token_to_str(p.reader, t);
s += "' but found '";
s += token_to_str(p.reader, p.token);
p.fatal(s + "'");
}
}
fn parse_ident(p: parser) -> ast::ident {
alt p.token {
token::IDENT(i, _) { p.bump(); ret p.get_str(i); }
_ { p.fatal("expecting ident, found "
+ token_to_str(p.reader, p.token)); }
}
}
fn parse_path_list_ident(p: parser) -> ast::path_list_ident {
let lo = p.span.lo;
let ident = parse_ident(p);
let hi = p.span.hi;
ret spanned(lo, hi, {name: ident, id: p.get_id()});
}
fn parse_value_ident(p: parser) -> ast::ident {
check_restricted_keywords(p);
ret parse_ident(p);
}
fn eat(p: parser, tok: token::token) -> bool {
ret if p.token == tok { p.bump(); true } else { false };
}
// A sanity check that the word we are asking for is a known keyword
fn require_keyword(p: parser, word: str) {
if !p.keywords.contains_key(word) {
p.bug(#fmt("unknown keyword: %s", word));
}
}
fn token_is_keyword(p: parser, word: str, tok: token::token) -> bool {
require_keyword(p, word);
alt tok {
token::IDENT(sid, false) { str::eq(word, p.get_str(sid)) }
_ { false }
}
}
fn is_keyword(p: parser, word: str) -> bool {
token_is_keyword(p, word, p.token)
}
fn eat_keyword(p: parser, word: str) -> bool {
require_keyword(p, word);
alt p.token {
token::IDENT(sid, false) {
if str::eq(word, p.get_str(sid)) {
p.bump();
ret true;
} else { ret false; }
}
_ { ret false; }
}
}
fn expect_keyword(p: parser, word: str) {
require_keyword(p, word);
if !eat_keyword(p, word) {
p.fatal("expecting " + word + ", found " +
token_to_str(p.reader, p.token));
}
}
fn is_restricted_keyword(p: parser, word: str) -> bool {
p.restricted_keywords.contains_key(word)
}
fn check_restricted_keywords(p: parser) {
alt p.token {
token::IDENT(_, false) {
let w = token_to_str(p.reader, p.token);
check_restricted_keywords_(p, w);
}
_ { }
}
}
fn check_restricted_keywords_(p: parser, w: ast::ident) {
if is_restricted_keyword(p, w) {
p.fatal("found `" + w + "` in restricted position");
}
}
fn expect_gt(p: parser) {
if p.token == token::GT {
p.bump();
} else if p.token == token::BINOP(token::SHR) {
p.swap(token::GT, p.span.lo + 1u, p.span.hi);
} else {
let mut s: str = "expecting ";
s += token_to_str(p.reader, token::GT);
s += ", found ";
s += token_to_str(p.reader, p.token);
p.fatal(s);
}
}
fn parse_seq_to_before_gt<T: copy>(sep: option<token::token>,
p: parser, f: fn(parser) -> T) -> [T] {
let mut first = true;
let mut v = [];
while p.token != token::GT && p.token != token::BINOP(token::SHR) {
alt sep {
some(t) { if first { first = false; } else { expect(p, t); } }
_ { }
}
v += [f(p)];
}
ret v;
}
fn parse_seq_to_gt<T: copy>(sep: option<token::token>,
p: parser, f: fn(parser) -> T) -> [T] {
let v = parse_seq_to_before_gt(sep, p, f);
expect_gt(p);
ret v;
}
fn parse_seq_lt_gt<T: copy>(sep: option<token::token>,
p: parser, f: fn(parser) -> T) -> spanned<[T]> {
let lo = p.span.lo;
expect(p, token::LT);
let result = parse_seq_to_before_gt::<T>(sep, p, f);
let hi = p.span.hi;
expect_gt(p);
ret spanned(lo, hi, result);
}
fn parse_seq_to_end<T: copy>(ket: token::token, sep: seq_sep, p: parser,
f: fn(parser) -> T) -> [T] {
let val = parse_seq_to_before_end(ket, sep, p, f);
p.bump();
ret val;
}
type seq_sep = {
sep: option<token::token>,
trailing_opt: bool // is trailing separator optional?
@ -180,27 +17,201 @@ fn seq_sep_none() -> seq_sep {
ret {sep: option::none, trailing_opt: false};
}
fn parse_seq_to_before_end<T: copy>(ket: token::token, sep: seq_sep,
p: parser, f: fn(parser) -> T) -> [T] {
let mut first: bool = true;
let mut v: [T] = [];
while p.token != ket {
alt sep.sep {
some(t) { if first { first = false; } else { expect(p, t); } }
_ { }
}
if sep.trailing_opt && p.token == ket { break; }
v += [f(p)];
}
ret v;
fn token_to_str(reader: reader, token: token::token) -> str {
token::to_str(*reader.interner, token)
}
fn parse_seq<T: copy>(bra: token::token, ket: token::token, sep: seq_sep,
p: parser, f: fn(parser) -> T) -> spanned<[T]> {
let lo = p.span.lo;
expect(p, bra);
let result = parse_seq_to_before_end::<T>(ket, sep, p, f);
let hi = p.span.hi;
p.bump();
ret spanned(lo, hi, result);
// This should be done with traits, once traits work
impl parser_common for parser {
fn unexpected_last(t: token::token) -> ! {
self.span_fatal(self.last_span, "unexpected token: '"
+ token_to_str(self.reader, t) + "'");
}
fn unexpected() -> ! {
self.fatal("unexpected token: '"
+ token_to_str(self.reader, self.token) + "'");
}
fn expect(t: token::token) {
if self.token == t {
self.bump();
} else {
let mut s: str = "expecting '";
s += token_to_str(self.reader, t);
s += "' but found '";
s += token_to_str(self.reader, self.token);
self.fatal(s + "'");
}
}
fn parse_ident() -> ast::ident {
alt self.token {
token::IDENT(i, _) { self.bump(); ret self.get_str(i); }
_ { self.fatal("expecting ident, found "
+ token_to_str(self.reader, self.token)); }
}
}
fn parse_path_list_ident() -> ast::path_list_ident {
let lo = self.span.lo;
let ident = self.parse_ident();
let hi = self.span.hi;
ret spanned(lo, hi, {name: ident, id: self.get_id()});
}
fn parse_value_ident() -> ast::ident {
self.check_restricted_keywords();
ret self.parse_ident();
}
fn eat(tok: token::token) -> bool {
ret if self.token == tok { self.bump(); true } else { false };
}
// A sanity check that the word we are asking for is a known keyword
fn require_keyword(word: str) {
if !self.keywords.contains_key(word) {
self.bug(#fmt("unknown keyword: %s", word));
}
}
fn token_is_keyword(word: str, tok: token::token) -> bool {
self.require_keyword(word);
alt tok {
token::IDENT(sid, false) { str::eq(word, self.get_str(sid)) }
_ { false }
}
}
fn is_keyword(word: str) -> bool {
self.token_is_keyword(word, self.token)
}
fn eat_keyword(word: str) -> bool {
self.require_keyword(word);
alt self.token {
token::IDENT(sid, false) {
if str::eq(word, self.get_str(sid)) {
self.bump();
ret true;
} else { ret false; }
}
_ { ret false; }
}
}
fn expect_keyword(word: str) {
self.require_keyword(word);
if !self.eat_keyword(word) {
self.fatal("expecting " + word + ", found " +
token_to_str(self.reader, self.token));
}
}
fn is_restricted_keyword(word: str) -> bool {
self.restricted_keywords.contains_key(word)
}
fn check_restricted_keywords() {
alt self.token {
token::IDENT(_, false) {
let w = token_to_str(self.reader, self.token);
self.check_restricted_keywords_(w);
}
_ { }
}
}
fn check_restricted_keywords_(w: ast::ident) {
if self.is_restricted_keyword(w) {
self.fatal("found `" + w + "` in restricted position");
}
}
fn expect_gt() {
if self.token == token::GT {
self.bump();
} else if self.token == token::BINOP(token::SHR) {
self.swap(token::GT, self.span.lo + 1u, self.span.hi);
} else {
let mut s: str = "expecting ";
s += token_to_str(self.reader, token::GT);
s += ", found ";
s += token_to_str(self.reader, self.token);
self.fatal(s);
}
}
fn parse_seq_to_before_gt<T: copy>(sep: option<token::token>,
f: fn(parser) -> T) -> [T] {
let mut first = true;
let mut v = [];
while self.token != token::GT
&& self.token != token::BINOP(token::SHR) {
alt sep {
some(t) { if first { first = false; }
else { self.expect(t); } }
_ { }
}
v += [f(self)];
}
ret v;
}
fn parse_seq_to_gt<T: copy>(sep: option<token::token>,
f: fn(parser) -> T) -> [T] {
let v = self.parse_seq_to_before_gt(sep, f);
self.expect_gt();
ret v;
}
fn parse_seq_lt_gt<T: copy>(sep: option<token::token>,
f: fn(parser) -> T) -> spanned<[T]> {
let lo = self.span.lo;
self.expect(token::LT);
let result = self.parse_seq_to_before_gt::<T>(sep, f);
let hi = self.span.hi;
self.expect_gt();
ret spanned(lo, hi, result);
}
fn parse_seq_to_end<T: copy>(ket: token::token, sep: seq_sep,
f: fn(parser) -> T) -> [T] {
let val = self.parse_seq_to_before_end(ket, sep, f);
self.bump();
ret val;
}
fn parse_seq_to_before_end<T: copy>(ket: token::token, sep: seq_sep,
f: fn(parser) -> T) -> [T] {
let mut first: bool = true;
let mut v: [T] = [];
while self.token != ket {
alt sep.sep {
some(t) { if first { first = false; }
else { self.expect(t); } }
_ { }
}
if sep.trailing_opt && self.token == ket { break; }
v += [f(self)];
}
ret v;
}
fn parse_seq<T: copy>(bra: token::token, ket: token::token, sep: seq_sep,
f: fn(parser) -> T) -> spanned<[T]> {
let lo = self.span.lo;
self.expect(bra);
let result = self.parse_seq_to_before_end::<T>(ket, sep, f);
let hi = self.span.hi;
self.bump();
ret spanned(lo, hi, result);
}
}

File diff suppressed because it is too large Load Diff