2012-04-19 21:18:33 -07:00
|
|
|
import std::map::{hashmap};
|
2012-04-19 21:24:42 -07:00
|
|
|
import ast_util::spanned;
|
2012-04-19 21:18:33 -07:00
|
|
|
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 {
|
2012-04-24 22:33:49 -07:00
|
|
|
check_restricted_keywords(p);
|
2012-04-19 21:18:33 -07:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-08 23:01:38 +02:00
|
|
|
fn token_is_keyword(p: parser, word: str, tok: token::token) -> bool {
|
2012-04-19 21:18:33 -07:00
|
|
|
require_keyword(p, word);
|
2012-05-08 23:01:38 +02:00
|
|
|
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)
|
2012-04-19 21:18:33 -07:00
|
|
|
}
|
|
|
|
|
2012-04-24 20:16:53 -07:00
|
|
|
fn eat_keyword(p: parser, word: str) -> bool {
|
2012-04-19 21:18:33 -07:00
|
|
|
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; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-24 20:16:53 -07:00
|
|
|
fn expect_keyword(p: parser, word: str) {
|
2012-04-19 21:18:33 -07:00
|
|
|
require_keyword(p, word);
|
2012-04-24 20:16:53 -07:00
|
|
|
if !eat_keyword(p, word) {
|
2012-04-19 21:18:33 -07:00
|
|
|
p.fatal("expecting " + word + ", found " +
|
|
|
|
token_to_str(p.reader, p.token));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-24 22:33:49 -07:00
|
|
|
fn is_restricted_keyword(p: parser, word: str) -> bool {
|
|
|
|
p.restricted_keywords.contains_key(word)
|
2012-04-24 21:05:25 -07:00
|
|
|
}
|
|
|
|
|
2012-04-24 22:33:49 -07:00
|
|
|
fn check_restricted_keywords(p: parser) {
|
2012-04-24 21:05:25 -07:00
|
|
|
alt p.token {
|
|
|
|
token::IDENT(_, false) {
|
2012-04-19 21:18:33 -07:00
|
|
|
let w = token_to_str(p.reader, p.token);
|
2012-04-27 15:44:40 -07:00
|
|
|
check_restricted_keywords_(p, w);
|
2012-04-24 21:05:25 -07:00
|
|
|
}
|
|
|
|
_ { }
|
2012-04-19 21:18:33 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-27 15:44:40 -07:00
|
|
|
fn check_restricted_keywords_(p: parser, w: ast::ident) {
|
|
|
|
if is_restricted_keyword(p, w) {
|
|
|
|
p.fatal("found `" + w + "` in restricted position");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-19 21:18:33 -07:00
|
|
|
fn expect_gt(p: parser) {
|
|
|
|
if p.token == token::GT {
|
|
|
|
p.bump();
|
2012-05-22 14:59:15 -07:00
|
|
|
} else if p.token == token::BINOP(token::SHR) {
|
2012-04-19 21:18:33 -07:00
|
|
|
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>,
|
2012-05-23 15:06:11 -07:00
|
|
|
p: parser, f: fn(parser) -> T) -> [T] {
|
2012-04-19 21:18:33 -07:00
|
|
|
let mut first = true;
|
|
|
|
let mut v = [];
|
2012-05-22 14:59:15 -07:00
|
|
|
while p.token != token::GT && p.token != token::BINOP(token::SHR) {
|
2012-04-19 21:18:33 -07:00
|
|
|
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>,
|
2012-05-23 15:06:11 -07:00
|
|
|
p: parser, f: fn(parser) -> T) -> [T] {
|
|
|
|
let v = parse_seq_to_before_gt(sep, p, f);
|
2012-04-19 21:18:33 -07:00
|
|
|
expect_gt(p);
|
|
|
|
|
|
|
|
ret v;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_seq_lt_gt<T: copy>(sep: option<token::token>,
|
2012-05-23 15:06:11 -07:00
|
|
|
p: parser, f: fn(parser) -> T) -> spanned<[T]> {
|
2012-04-19 21:18:33 -07:00
|
|
|
let lo = p.span.lo;
|
|
|
|
expect(p, token::LT);
|
2012-05-23 15:06:11 -07:00
|
|
|
let result = parse_seq_to_before_gt::<T>(sep, p, f);
|
2012-04-19 21:18:33 -07:00
|
|
|
let hi = p.span.hi;
|
|
|
|
expect_gt(p);
|
|
|
|
ret spanned(lo, hi, result);
|
|
|
|
}
|
|
|
|
|
2012-05-23 15:06:11 -07:00
|
|
|
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);
|
2012-04-19 21:18:33 -07:00
|
|
|
p.bump();
|
|
|
|
ret val;
|
|
|
|
}
|
|
|
|
|
|
|
|
type seq_sep = {
|
|
|
|
sep: option<token::token>,
|
|
|
|
trailing_opt: bool // is trailing separator optional?
|
|
|
|
};
|
|
|
|
|
|
|
|
fn seq_sep(t: token::token) -> seq_sep {
|
|
|
|
ret {sep: option::some(t), trailing_opt: false};
|
|
|
|
}
|
|
|
|
fn seq_sep_opt(t: token::token) -> seq_sep {
|
|
|
|
ret {sep: option::some(t), trailing_opt: true};
|
|
|
|
}
|
|
|
|
fn seq_sep_none() -> seq_sep {
|
|
|
|
ret {sep: option::none, trailing_opt: false};
|
|
|
|
}
|
|
|
|
|
2012-05-23 15:06:11 -07:00
|
|
|
fn parse_seq_to_before_end<T: copy>(ket: token::token, sep: seq_sep,
|
|
|
|
p: parser, f: fn(parser) -> T) -> [T] {
|
2012-04-19 21:18:33 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-05-23 15:06:11 -07:00
|
|
|
fn parse_seq<T: copy>(bra: token::token, ket: token::token, sep: seq_sep,
|
|
|
|
p: parser, f: fn(parser) -> T) -> spanned<[T]> {
|
2012-04-19 21:18:33 -07:00
|
|
|
let lo = p.span.lo;
|
|
|
|
expect(p, bra);
|
2012-05-23 15:06:11 -07:00
|
|
|
let result = parse_seq_to_before_end::<T>(ket, sep, p, f);
|
2012-04-19 21:18:33 -07:00
|
|
|
let hi = p.span.hi;
|
|
|
|
p.bump();
|
|
|
|
ret spanned(lo, hi, result);
|
|
|
|
}
|