2012-12-03 16:48:01 -08:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2012-09-10 15:38:28 -07:00
|
|
|
use std::map::{HashMap};
|
2012-09-04 11:37:29 -07:00
|
|
|
use ast_util::spanned;
|
2012-12-13 13:05:22 -08:00
|
|
|
use parse::parser::Parser;
|
|
|
|
use parse::lexer::reader;
|
2012-04-19 21:18:33 -07:00
|
|
|
|
2012-05-24 12:38:45 -07:00
|
|
|
type seq_sep = {
|
2012-10-15 14:56:42 -07:00
|
|
|
sep: Option<token::Token>,
|
2012-06-12 10:17:36 -07:00
|
|
|
trailing_sep_allowed: bool
|
2012-05-24 12:38:45 -07:00
|
|
|
};
|
|
|
|
|
2012-10-15 14:56:42 -07:00
|
|
|
fn seq_sep_trailing_disallowed(t: token::Token) -> seq_sep {
|
2012-08-20 12:23:37 -07:00
|
|
|
return {sep: option::Some(t), trailing_sep_allowed: false};
|
2012-05-24 12:38:45 -07:00
|
|
|
}
|
2012-10-15 14:56:42 -07:00
|
|
|
fn seq_sep_trailing_allowed(t: token::Token) -> seq_sep {
|
2012-08-20 12:23:37 -07:00
|
|
|
return {sep: option::Some(t), trailing_sep_allowed: true};
|
2012-05-24 12:38:45 -07:00
|
|
|
}
|
|
|
|
fn seq_sep_none() -> seq_sep {
|
2012-08-20 12:23:37 -07:00
|
|
|
return {sep: option::None, trailing_sep_allowed: false};
|
2012-05-24 12:38:45 -07:00
|
|
|
}
|
|
|
|
|
2012-10-15 14:56:42 -07:00
|
|
|
fn token_to_str(reader: reader, ++token: token::Token) -> ~str {
|
2012-08-14 15:27:06 -07:00
|
|
|
token::to_str(reader.interner(), token)
|
2012-04-19 21:18:33 -07:00
|
|
|
}
|
|
|
|
|
2012-11-29 18:37:33 -08:00
|
|
|
impl Parser {
|
2012-10-15 14:56:42 -07:00
|
|
|
fn unexpected_last(t: token::Token) -> ! {
|
2012-06-03 07:36:40 -07:00
|
|
|
self.span_fatal(
|
|
|
|
copy self.last_span,
|
2012-07-13 22:57:48 -07:00
|
|
|
~"unexpected token: `" + token_to_str(self.reader, t) + ~"`");
|
2012-04-19 21:18:33 -07:00
|
|
|
}
|
|
|
|
|
2012-05-24 12:38:45 -07:00
|
|
|
fn unexpected() -> ! {
|
2012-07-13 22:57:48 -07:00
|
|
|
self.fatal(~"unexpected token: `"
|
|
|
|
+ token_to_str(self.reader, self.token) + ~"`");
|
2012-04-19 21:18:33 -07:00
|
|
|
}
|
|
|
|
|
2012-10-15 14:56:42 -07:00
|
|
|
fn expect(t: token::Token) {
|
2012-05-24 12:38:45 -07:00
|
|
|
if self.token == t {
|
|
|
|
self.bump();
|
|
|
|
} else {
|
2012-07-13 22:57:48 -07:00
|
|
|
let mut s: ~str = ~"expected `";
|
2012-05-24 12:38:45 -07:00
|
|
|
s += token_to_str(self.reader, t);
|
2012-07-13 22:57:48 -07:00
|
|
|
s += ~"` but found `";
|
2012-05-24 12:38:45 -07:00
|
|
|
s += token_to_str(self.reader, self.token);
|
2012-07-13 22:57:48 -07:00
|
|
|
self.fatal(s + ~"`");
|
2012-05-24 12:38:45 -07:00
|
|
|
}
|
|
|
|
}
|
2012-04-19 21:18:33 -07:00
|
|
|
|
2012-05-24 12:38:45 -07:00
|
|
|
fn parse_ident() -> ast::ident {
|
2012-09-09 17:35:56 -07:00
|
|
|
self.check_strict_keywords();
|
2012-09-11 19:26:48 -07:00
|
|
|
self.check_reserved_keywords();
|
2012-08-06 12:34:08 -07:00
|
|
|
match copy self.token {
|
2012-07-18 16:18:02 -07:00
|
|
|
token::IDENT(i, _) => { self.bump(); return i; }
|
2012-08-03 19:59:04 -07:00
|
|
|
token::INTERPOLATED(token::nt_ident(*)) => { self.bug(
|
2012-07-13 22:57:48 -07:00
|
|
|
~"ident interpolation not converted to real token"); }
|
2012-08-03 19:59:04 -07:00
|
|
|
_ => { self.fatal(~"expected ident, found `"
|
2012-07-13 13:20:49 -07:00
|
|
|
+ token_to_str(self.reader, self.token)
|
2012-07-13 22:57:48 -07:00
|
|
|
+ ~"`"); }
|
2012-05-24 12:38:45 -07:00
|
|
|
}
|
|
|
|
}
|
2012-04-19 21:18:33 -07:00
|
|
|
|
2012-05-24 12:38:45 -07:00
|
|
|
fn parse_path_list_ident() -> ast::path_list_ident {
|
|
|
|
let lo = self.span.lo;
|
|
|
|
let ident = self.parse_ident();
|
|
|
|
let hi = self.span.hi;
|
2012-08-01 17:30:05 -07:00
|
|
|
return spanned(lo, hi, {name: ident, id: self.get_id()});
|
2012-04-19 21:18:33 -07:00
|
|
|
}
|
|
|
|
|
2012-05-24 12:38:45 -07:00
|
|
|
fn parse_value_ident() -> ast::ident {
|
2012-08-01 17:30:05 -07:00
|
|
|
return self.parse_ident();
|
2012-05-08 23:01:38 +02:00
|
|
|
}
|
|
|
|
|
2012-10-15 14:56:42 -07:00
|
|
|
fn eat(tok: token::Token) -> bool {
|
2012-08-01 17:30:05 -07:00
|
|
|
return if self.token == tok { self.bump(); true } else { false };
|
2012-05-24 12:38:45 -07:00
|
|
|
}
|
2012-04-19 21:18:33 -07:00
|
|
|
|
2012-07-18 16:18:02 -07:00
|
|
|
// Storing keywords as interned idents instead of strings would be nifty.
|
|
|
|
|
2012-05-24 12:38:45 -07:00
|
|
|
// A sanity check that the word we are asking for is a known keyword
|
2012-07-13 22:57:48 -07:00
|
|
|
fn require_keyword(word: ~str) {
|
2012-08-02 15:42:56 -07:00
|
|
|
if !self.keywords.contains_key_ref(&word) {
|
2012-08-22 17:24:52 -07:00
|
|
|
self.bug(fmt!("unknown keyword: %s", word));
|
2012-05-24 12:38:45 -07:00
|
|
|
}
|
2012-04-19 21:18:33 -07:00
|
|
|
}
|
|
|
|
|
2012-10-15 14:56:42 -07:00
|
|
|
fn token_is_word(word: ~str, ++tok: token::Token) -> bool {
|
2012-08-06 12:34:08 -07:00
|
|
|
match tok {
|
2012-07-18 16:18:02 -07:00
|
|
|
token::IDENT(sid, false) => { *self.id_to_str(sid) == word }
|
2012-07-30 16:33:02 -07:00
|
|
|
_ => { false }
|
2012-05-24 12:38:45 -07:00
|
|
|
}
|
2012-04-19 21:18:33 -07:00
|
|
|
}
|
|
|
|
|
2012-10-15 14:56:42 -07:00
|
|
|
fn token_is_keyword(word: ~str, ++tok: token::Token) -> bool {
|
2012-07-30 16:33:02 -07:00
|
|
|
self.require_keyword(word);
|
|
|
|
self.token_is_word(word, tok)
|
|
|
|
}
|
|
|
|
|
2012-07-13 22:57:48 -07:00
|
|
|
fn is_keyword(word: ~str) -> bool {
|
2012-05-24 12:38:45 -07:00
|
|
|
self.token_is_keyword(word, self.token)
|
|
|
|
}
|
2012-04-24 21:05:25 -07:00
|
|
|
|
2012-10-15 14:56:42 -07:00
|
|
|
fn is_any_keyword(tok: token::Token) -> bool {
|
2012-08-06 12:34:08 -07:00
|
|
|
match tok {
|
2012-08-03 19:59:04 -07:00
|
|
|
token::IDENT(sid, false) => {
|
2012-07-18 16:18:02 -07:00
|
|
|
self.keywords.contains_key_ref(self.id_to_str(sid))
|
2012-07-05 12:10:33 -07:00
|
|
|
}
|
2012-08-03 19:59:04 -07:00
|
|
|
_ => false
|
2012-07-05 12:10:33 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-13 22:57:48 -07:00
|
|
|
fn eat_keyword(word: ~str) -> bool {
|
2012-05-24 12:38:45 -07:00
|
|
|
self.require_keyword(word);
|
2012-08-23 14:46:59 -07:00
|
|
|
let is_kw = match self.token {
|
|
|
|
token::IDENT(sid, false) => (word == *self.id_to_str(sid)),
|
2012-08-03 19:59:04 -07:00
|
|
|
_ => false
|
2012-07-25 19:13:58 -07:00
|
|
|
};
|
2012-08-23 14:46:59 -07:00
|
|
|
if is_kw { self.bump() }
|
|
|
|
is_kw
|
2012-04-19 21:18:33 -07:00
|
|
|
}
|
|
|
|
|
2012-07-13 22:57:48 -07:00
|
|
|
fn expect_keyword(word: ~str) {
|
2012-05-24 12:38:45 -07:00
|
|
|
self.require_keyword(word);
|
|
|
|
if !self.eat_keyword(word) {
|
2012-07-13 22:57:48 -07:00
|
|
|
self.fatal(~"expected `" + word + ~"`, found `" +
|
2012-07-13 13:20:49 -07:00
|
|
|
token_to_str(self.reader, self.token) +
|
2012-07-13 22:57:48 -07:00
|
|
|
~"`");
|
2012-06-12 10:59:50 -07:00
|
|
|
}
|
2012-04-27 15:44:40 -07:00
|
|
|
}
|
|
|
|
|
2012-09-09 17:35:56 -07:00
|
|
|
fn is_strict_keyword(word: ~str) -> bool {
|
|
|
|
self.strict_keywords.contains_key_ref(&word)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_strict_keywords() {
|
|
|
|
match self.token {
|
|
|
|
token::IDENT(_, false) => {
|
|
|
|
let w = token_to_str(self.reader, self.token);
|
|
|
|
self.check_strict_keywords_(w);
|
|
|
|
}
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_strict_keywords_(w: ~str) {
|
|
|
|
if self.is_strict_keyword(w) {
|
|
|
|
self.fatal(~"found `" + w + ~"` in ident position");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-11 19:26:48 -07:00
|
|
|
fn is_reserved_keyword(word: ~str) -> bool {
|
|
|
|
self.reserved_keywords.contains_key_ref(&word)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_reserved_keywords() {
|
|
|
|
match self.token {
|
|
|
|
token::IDENT(_, false) => {
|
|
|
|
let w = token_to_str(self.reader, self.token);
|
|
|
|
self.check_reserved_keywords_(w);
|
|
|
|
}
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_reserved_keywords_(w: ~str) {
|
|
|
|
if self.is_reserved_keyword(w) {
|
|
|
|
self.fatal(~"`" + w + ~"` is a reserved keyword");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-24 12:38:45 -07:00
|
|
|
fn expect_gt() {
|
|
|
|
if self.token == token::GT {
|
|
|
|
self.bump();
|
|
|
|
} else if self.token == token::BINOP(token::SHR) {
|
2012-11-15 19:37:29 -08:00
|
|
|
self.swap(token::GT, self.span.lo + BytePos(1u), self.span.hi);
|
2012-05-24 12:38:45 -07:00
|
|
|
} else {
|
2012-07-13 22:57:48 -07:00
|
|
|
let mut s: ~str = ~"expected `";
|
2012-05-24 12:38:45 -07:00
|
|
|
s += token_to_str(self.reader, token::GT);
|
2012-07-13 22:57:48 -07:00
|
|
|
s += ~"`, found `";
|
2012-05-24 12:38:45 -07:00
|
|
|
s += token_to_str(self.reader, self.token);
|
2012-07-13 22:57:48 -07:00
|
|
|
s += ~"`";
|
2012-05-24 12:38:45 -07:00
|
|
|
self.fatal(s);
|
|
|
|
}
|
|
|
|
}
|
2012-04-19 21:18:33 -07:00
|
|
|
|
2012-10-15 14:56:42 -07:00
|
|
|
fn parse_seq_to_before_gt<T: Copy>(sep: Option<token::Token>,
|
|
|
|
f: fn(Parser) -> T) -> ~[T] {
|
2012-05-24 12:38:45 -07:00
|
|
|
let mut first = true;
|
2012-06-29 16:26:56 -07:00
|
|
|
let mut v = ~[];
|
2012-05-24 12:38:45 -07:00
|
|
|
while self.token != token::GT
|
|
|
|
&& self.token != token::BINOP(token::SHR) {
|
2012-08-06 12:34:08 -07:00
|
|
|
match sep {
|
2012-12-04 10:50:00 -08:00
|
|
|
Some(ref t) => {
|
2012-08-03 19:59:04 -07:00
|
|
|
if first { first = false; }
|
2012-12-04 10:50:00 -08:00
|
|
|
else { self.expect((*t)); }
|
2012-08-03 19:59:04 -07:00
|
|
|
}
|
|
|
|
_ => ()
|
2012-05-24 12:38:45 -07:00
|
|
|
}
|
2012-09-26 17:33:34 -07:00
|
|
|
v.push(f(self));
|
2012-05-24 12:38:45 -07:00
|
|
|
}
|
2012-04-19 21:18:33 -07:00
|
|
|
|
2012-08-01 17:30:05 -07:00
|
|
|
return v;
|
2012-05-24 12:38:45 -07:00
|
|
|
}
|
2012-04-19 21:18:33 -07:00
|
|
|
|
2012-10-15 14:56:42 -07:00
|
|
|
fn parse_seq_to_gt<T: Copy>(sep: Option<token::Token>,
|
|
|
|
f: fn(Parser) -> T) -> ~[T] {
|
2012-05-24 12:38:45 -07:00
|
|
|
let v = self.parse_seq_to_before_gt(sep, f);
|
|
|
|
self.expect_gt();
|
2012-04-19 21:18:33 -07:00
|
|
|
|
2012-08-01 17:30:05 -07:00
|
|
|
return v;
|
2012-05-24 12:38:45 -07:00
|
|
|
}
|
2012-04-19 21:18:33 -07:00
|
|
|
|
2012-10-15 14:56:42 -07:00
|
|
|
fn parse_seq_lt_gt<T: Copy>(sep: Option<token::Token>,
|
|
|
|
f: fn(Parser) -> T) -> spanned<~[T]> {
|
2012-05-24 12:38:45 -07:00
|
|
|
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();
|
2012-08-01 17:30:05 -07:00
|
|
|
return spanned(lo, hi, result);
|
2012-05-24 12:38:45 -07:00
|
|
|
}
|
2012-04-19 21:18:33 -07:00
|
|
|
|
2012-10-15 14:56:42 -07:00
|
|
|
fn parse_seq_to_end<T: Copy>(ket: token::Token, sep: seq_sep,
|
|
|
|
f: fn(Parser) -> T) -> ~[T] {
|
2012-05-24 12:38:45 -07:00
|
|
|
let val = self.parse_seq_to_before_end(ket, sep, f);
|
|
|
|
self.bump();
|
2012-08-01 17:30:05 -07:00
|
|
|
return val;
|
2012-05-24 12:38:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-15 14:56:42 -07:00
|
|
|
fn parse_seq_to_before_end<T: Copy>(ket: token::Token, sep: seq_sep,
|
|
|
|
f: fn(Parser) -> T) -> ~[T] {
|
2012-05-24 12:38:45 -07:00
|
|
|
let mut first: bool = true;
|
2012-06-29 16:26:56 -07:00
|
|
|
let mut v: ~[T] = ~[];
|
2012-05-24 12:38:45 -07:00
|
|
|
while self.token != ket {
|
2012-08-06 12:34:08 -07:00
|
|
|
match sep.sep {
|
2012-12-04 10:50:00 -08:00
|
|
|
Some(ref t) => {
|
2012-08-03 19:59:04 -07:00
|
|
|
if first { first = false; }
|
2012-12-04 10:50:00 -08:00
|
|
|
else { self.expect((*t)); }
|
2012-08-03 19:59:04 -07:00
|
|
|
}
|
|
|
|
_ => ()
|
2012-05-24 12:38:45 -07:00
|
|
|
}
|
2012-06-12 10:17:36 -07:00
|
|
|
if sep.trailing_sep_allowed && self.token == ket { break; }
|
2012-09-26 17:33:34 -07:00
|
|
|
v.push(f(self));
|
2012-04-19 21:18:33 -07:00
|
|
|
}
|
2012-08-01 17:30:05 -07:00
|
|
|
return v;
|
2012-04-19 21:18:33 -07:00
|
|
|
}
|
|
|
|
|
2012-10-15 14:56:42 -07:00
|
|
|
fn parse_unspanned_seq<T: Copy>(bra: token::Token,
|
|
|
|
ket: token::Token,
|
2012-06-25 20:00:46 -07:00
|
|
|
sep: seq_sep,
|
2012-10-15 14:56:42 -07:00
|
|
|
f: fn(Parser) -> T) -> ~[T] {
|
2012-06-07 22:09:27 -07:00
|
|
|
self.expect(bra);
|
|
|
|
let result = self.parse_seq_to_before_end::<T>(ket, sep, f);
|
|
|
|
self.bump();
|
2012-08-01 17:30:05 -07:00
|
|
|
return result;
|
2012-06-07 22:09:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// NB: Do not use this function unless you actually plan to place the
|
|
|
|
// spanned list in the AST.
|
2012-10-15 14:56:42 -07:00
|
|
|
fn parse_seq<T: Copy>(bra: token::Token, ket: token::Token, sep: seq_sep,
|
|
|
|
f: fn(Parser) -> T) -> spanned<~[T]> {
|
2012-05-24 12:38:45 -07:00
|
|
|
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();
|
2012-08-01 17:30:05 -07:00
|
|
|
return spanned(lo, hi, result);
|
2012-05-24 12:38:45 -07:00
|
|
|
}
|
2012-06-07 21:53:47 -07:00
|
|
|
}
|