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-12-23 17:41:37 -05:00
|
|
|
use ast;
|
2013-01-30 09:56:33 -08:00
|
|
|
use codemap::{BytePos, spanned};
|
2012-12-13 13:05:22 -08:00
|
|
|
use parse::lexer::reader;
|
2012-12-23 17:41:37 -05:00
|
|
|
use parse::parser::Parser;
|
2013-05-25 17:45:45 +02:00
|
|
|
use parse::token::keywords;
|
2012-12-23 17:41:37 -05:00
|
|
|
use parse::token;
|
2013-05-17 10:18:35 -07:00
|
|
|
use parse::token::{get_ident_interner};
|
2012-12-23 17:41:37 -05:00
|
|
|
|
2013-02-14 21:50:03 -08:00
|
|
|
use opt_vec;
|
|
|
|
use opt_vec::OptVec;
|
|
|
|
|
2013-02-21 00:16:31 -08:00
|
|
|
// SeqSep : a sequence separator (token)
|
2013-02-04 13:15:17 -08:00
|
|
|
// and whether a trailing separator is allowed.
|
2013-02-21 00:16:31 -08:00
|
|
|
pub struct SeqSep {
|
2012-10-15 14:56:42 -07:00
|
|
|
sep: Option<token::Token>,
|
2012-06-12 10:17:36 -07:00
|
|
|
trailing_sep_allowed: bool
|
2013-02-21 00:16:31 -08:00
|
|
|
}
|
2012-05-24 12:38:45 -07:00
|
|
|
|
2013-04-17 12:15:08 -04:00
|
|
|
pub fn seq_sep_trailing_disallowed(t: token::Token) -> SeqSep {
|
2013-02-21 00:16:31 -08:00
|
|
|
SeqSep {
|
2013-02-24 15:41:54 -08:00
|
|
|
sep: Some(t),
|
|
|
|
trailing_sep_allowed: false,
|
2013-02-21 00:16:31 -08:00
|
|
|
}
|
2012-05-24 12:38:45 -07:00
|
|
|
}
|
2013-04-17 12:15:08 -04:00
|
|
|
pub fn seq_sep_trailing_allowed(t: token::Token) -> SeqSep {
|
2013-02-21 00:16:31 -08:00
|
|
|
SeqSep {
|
2013-02-24 15:41:54 -08:00
|
|
|
sep: Some(t),
|
|
|
|
trailing_sep_allowed: true,
|
2013-02-21 00:16:31 -08:00
|
|
|
}
|
2012-05-24 12:38:45 -07:00
|
|
|
}
|
2013-02-21 00:16:31 -08:00
|
|
|
pub fn seq_sep_none() -> SeqSep {
|
|
|
|
SeqSep {
|
2013-02-24 15:41:54 -08:00
|
|
|
sep: None,
|
|
|
|
trailing_sep_allowed: false,
|
2013-02-21 00:16:31 -08:00
|
|
|
}
|
2012-05-24 12:38:45 -07:00
|
|
|
}
|
|
|
|
|
2013-04-15 16:13:42 -07:00
|
|
|
// maps any token back to a string. not necessary if you know it's
|
|
|
|
// an identifier....
|
2013-06-04 15:14:56 -07:00
|
|
|
pub fn token_to_str(token: &token::Token) -> ~str {
|
2013-05-17 10:18:35 -07:00
|
|
|
token::to_str(get_ident_interner(), token)
|
2012-04-19 21:18:33 -07:00
|
|
|
}
|
|
|
|
|
2013-05-31 15:17:22 -07:00
|
|
|
impl Parser {
|
2013-04-15 16:13:42 -07:00
|
|
|
// convert a token to a string using self's reader
|
2013-05-31 15:17:22 -07:00
|
|
|
pub fn token_to_str(&self, token: &token::Token) -> ~str {
|
2013-05-17 10:18:35 -07:00
|
|
|
token::to_str(get_ident_interner(), token)
|
2013-04-15 16:13:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// convert the current token to a string using self's reader
|
2013-05-31 15:17:22 -07:00
|
|
|
pub fn this_token_to_str(&self) -> ~str {
|
2013-04-15 16:13:42 -07:00
|
|
|
self.token_to_str(self.token)
|
|
|
|
}
|
|
|
|
|
2013-05-31 15:17:22 -07:00
|
|
|
pub fn unexpected_last(&self, t: &token::Token) -> ! {
|
2012-06-03 07:36:40 -07:00
|
|
|
self.span_fatal(
|
2013-02-21 18:12:13 -08:00
|
|
|
*self.last_span,
|
2013-02-24 15:41:54 -08:00
|
|
|
fmt!(
|
|
|
|
"unexpected token: `%s`",
|
2013-04-15 16:13:42 -07:00
|
|
|
self.token_to_str(t)
|
2013-02-24 15:41:54 -08:00
|
|
|
)
|
|
|
|
);
|
2012-04-19 21:18:33 -07:00
|
|
|
}
|
|
|
|
|
2013-05-31 15:17:22 -07:00
|
|
|
pub fn unexpected(&self) -> ! {
|
2013-02-24 15:41:54 -08:00
|
|
|
self.fatal(
|
|
|
|
fmt!(
|
|
|
|
"unexpected token: `%s`",
|
2013-04-15 16:13:42 -07:00
|
|
|
self.this_token_to_str()
|
2013-02-24 15:41:54 -08:00
|
|
|
)
|
|
|
|
);
|
2012-04-19 21:18:33 -07:00
|
|
|
}
|
|
|
|
|
2013-02-04 13:15:17 -08:00
|
|
|
// expect and consume the token t. Signal an error if
|
|
|
|
// the next token is not t.
|
2013-05-31 15:17:22 -07:00
|
|
|
pub fn expect(&self, t: &token::Token) {
|
2013-02-24 10:20:24 -08:00
|
|
|
if *self.token == *t {
|
2012-05-24 12:38:45 -07:00
|
|
|
self.bump();
|
|
|
|
} else {
|
2013-02-24 10:20:24 -08:00
|
|
|
self.fatal(
|
2013-02-24 19:54:37 -08:00
|
|
|
fmt!(
|
|
|
|
"expected `%s` but found `%s`",
|
2013-04-15 16:13:42 -07:00
|
|
|
self.token_to_str(t),
|
|
|
|
self.this_token_to_str()
|
2013-02-24 10:20:24 -08:00
|
|
|
)
|
|
|
|
)
|
2012-05-24 12:38:45 -07:00
|
|
|
}
|
|
|
|
}
|
2012-04-19 21:18:33 -07:00
|
|
|
|
2013-05-31 15:17:22 -07:00
|
|
|
pub fn parse_ident(&self) -> 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();
|
2013-02-21 18:12:13 -08:00
|
|
|
match *self.token {
|
2013-02-24 15:41:54 -08:00
|
|
|
token::IDENT(i, _) => {
|
|
|
|
self.bump();
|
|
|
|
i
|
|
|
|
}
|
|
|
|
token::INTERPOLATED(token::nt_ident(*)) => {
|
2013-05-19 01:07:44 -04:00
|
|
|
self.bug("ident interpolation not converted to real token");
|
2013-02-24 15:41:54 -08:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
self.fatal(
|
|
|
|
fmt!(
|
|
|
|
"expected ident, found `%s`",
|
2013-04-15 16:13:42 -07:00
|
|
|
self.this_token_to_str()
|
2013-02-24 15:41:54 -08:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2012-05-24 12:38:45 -07:00
|
|
|
}
|
|
|
|
}
|
2012-04-19 21:18:33 -07:00
|
|
|
|
2013-05-31 15:17:22 -07:00
|
|
|
pub fn parse_path_list_ident(&self) -> ast::path_list_ident {
|
2012-05-24 12:38:45 -07:00
|
|
|
let lo = self.span.lo;
|
|
|
|
let ident = self.parse_ident();
|
2013-05-03 02:36:24 +09:00
|
|
|
let hi = self.last_span.hi;
|
2013-01-13 17:27:57 -08:00
|
|
|
spanned(lo, hi, ast::path_list_ident_ { name: ident,
|
|
|
|
id: self.get_id() })
|
2012-04-19 21:18:33 -07:00
|
|
|
}
|
|
|
|
|
2013-02-04 13:15:17 -08:00
|
|
|
// consume token 'tok' if it exists. Returns true if the given
|
|
|
|
// token was present, false otherwise.
|
2013-05-31 15:17:22 -07:00
|
|
|
pub fn eat(&self, tok: &token::Token) -> bool {
|
2013-02-24 09:54:41 -08: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
|
|
|
|
2013-05-31 15:17:22 -07:00
|
|
|
pub fn is_keyword(&self, kw: keywords::Keyword) -> bool {
|
2013-05-25 17:45:45 +02:00
|
|
|
token::is_keyword(kw, self.token)
|
2012-07-05 12:10:33 -07:00
|
|
|
}
|
|
|
|
|
2013-03-08 10:19:19 -08:00
|
|
|
// if the next token is the given keyword, eat it and return
|
|
|
|
// true. Otherwise, return false.
|
2013-05-31 15:17:22 -07:00
|
|
|
pub fn eat_keyword(&self, kw: keywords::Keyword) -> bool {
|
2013-02-21 18:12:13 -08:00
|
|
|
let is_kw = match *self.token {
|
2013-05-17 10:18:09 -07:00
|
|
|
token::IDENT(sid, false) => kw.to_ident().name == sid.name,
|
2013-02-24 20:51:56 -08: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
|
|
|
}
|
|
|
|
|
2013-03-08 10:19:19 -08:00
|
|
|
// if the given word is not a keyword, signal an error.
|
|
|
|
// if the next token is not the given word, signal an error.
|
|
|
|
// otherwise, eat it.
|
2013-05-31 15:17:22 -07:00
|
|
|
pub fn expect_keyword(&self, kw: keywords::Keyword) {
|
2013-05-25 17:45:45 +02:00
|
|
|
if !self.eat_keyword(kw) {
|
2013-02-24 15:41:54 -08:00
|
|
|
self.fatal(
|
|
|
|
fmt!(
|
|
|
|
"expected `%s`, found `%s`",
|
2013-06-13 03:02:55 +10:00
|
|
|
self.id_to_str(kw.to_ident()),
|
2013-04-15 16:13:42 -07:00
|
|
|
self.this_token_to_str()
|
2013-02-24 15:41:54 -08:00
|
|
|
)
|
|
|
|
);
|
2012-06-12 10:59:50 -07:00
|
|
|
}
|
2012-04-27 15:44:40 -07:00
|
|
|
}
|
|
|
|
|
2013-03-08 10:19:19 -08:00
|
|
|
// signal an error if the given string is a strict keyword
|
2013-05-31 15:17:22 -07:00
|
|
|
pub fn check_strict_keywords(&self) {
|
2013-05-25 17:45:45 +02:00
|
|
|
if token::is_strict_keyword(self.token) {
|
2013-05-10 15:15:06 -07:00
|
|
|
self.span_err(*self.last_span,
|
2013-05-25 17:45:45 +02:00
|
|
|
fmt!("found `%s` in ident position", self.this_token_to_str()));
|
2012-09-09 17:35:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-08 10:19:19 -08:00
|
|
|
// signal an error if the current token is a reserved keyword
|
2013-05-31 15:17:22 -07:00
|
|
|
pub fn check_reserved_keywords(&self) {
|
2013-05-25 17:45:45 +02:00
|
|
|
if token::is_reserved_keyword(self.token) {
|
|
|
|
self.fatal(fmt!("`%s` is a reserved keyword", self.this_token_to_str()));
|
2012-09-11 19:26:48 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-04 13:15:17 -08:00
|
|
|
// expect and consume a GT. if a >> is seen, replace it
|
2013-03-08 10:19:19 -08:00
|
|
|
// with a single > and continue. If a GT is not seen,
|
|
|
|
// signal an error.
|
2013-05-31 15:17:22 -07:00
|
|
|
pub fn expect_gt(&self) {
|
2013-02-21 18:12:13 -08:00
|
|
|
if *self.token == token::GT {
|
2012-05-24 12:38:45 -07:00
|
|
|
self.bump();
|
2013-02-21 18:12:13 -08:00
|
|
|
} else if *self.token == token::BINOP(token::SHR) {
|
2013-02-24 15:41:54 -08:00
|
|
|
self.replace_token(
|
|
|
|
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 `";
|
2013-06-11 19:13:42 -07:00
|
|
|
s.push_str(self.token_to_str(&token::GT));
|
|
|
|
s.push_str("`, found `");
|
|
|
|
s.push_str(self.this_token_to_str());
|
|
|
|
s.push_str("`");
|
2012-05-24 12:38:45 -07:00
|
|
|
self.fatal(s);
|
|
|
|
}
|
|
|
|
}
|
2012-04-19 21:18:33 -07:00
|
|
|
|
2013-02-04 13:15:17 -08:00
|
|
|
// parse a sequence bracketed by '<' and '>', stopping
|
|
|
|
// before the '>'.
|
2013-05-31 15:17:22 -07:00
|
|
|
pub fn parse_seq_to_before_gt<T: Copy>(&self,
|
|
|
|
sep: Option<token::Token>,
|
|
|
|
f: &fn(&Parser) -> T)
|
|
|
|
-> OptVec<T> {
|
2012-05-24 12:38:45 -07:00
|
|
|
let mut first = true;
|
2013-02-14 21:50:03 -08:00
|
|
|
let mut v = opt_vec::Empty;
|
2013-02-21 18:12:13 -08: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; }
|
2013-02-24 10:20:24 -08:00
|
|
|
else { self.expect(t); }
|
2012-08-03 19:59:04 -07:00
|
|
|
}
|
|
|
|
_ => ()
|
2012-05-24 12:38:45 -07:00
|
|
|
}
|
2013-03-02 13:02:27 -08:00
|
|
|
v.push(f(self));
|
2012-05-24 12:38:45 -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
|
|
|
|
2013-05-31 15:17:22 -07:00
|
|
|
pub fn parse_seq_to_gt<T: Copy>(&self,
|
|
|
|
sep: Option<token::Token>,
|
|
|
|
f: &fn(&Parser) -> T)
|
|
|
|
-> OptVec<T> {
|
2012-05-24 12:38:45 -07:00
|
|
|
let v = self.parse_seq_to_before_gt(sep, f);
|
|
|
|
self.expect_gt();
|
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
|
|
|
|
2013-02-04 13:15:17 -08:00
|
|
|
// parse a sequence, including the closing delimiter. The function
|
|
|
|
// f must consume tokens until reaching the next separator or
|
|
|
|
// closing bracket.
|
2013-05-31 15:17:22 -07:00
|
|
|
pub fn parse_seq_to_end<T: Copy>(&self,
|
|
|
|
ket: &token::Token,
|
|
|
|
sep: SeqSep,
|
|
|
|
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();
|
2013-02-24 15:41:54 -08:00
|
|
|
val
|
2012-05-24 12:38:45 -07:00
|
|
|
}
|
|
|
|
|
2013-02-04 13:15:17 -08:00
|
|
|
// parse a sequence, not including the closing delimiter. The function
|
|
|
|
// f must consume tokens until reaching the next separator or
|
|
|
|
// closing bracket.
|
2013-05-31 15:17:22 -07:00
|
|
|
pub fn parse_seq_to_before_end<T: Copy>(&self,
|
|
|
|
ket: &token::Token,
|
|
|
|
sep: SeqSep,
|
|
|
|
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] = ~[];
|
2013-02-24 18:55:24 -08: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; }
|
2013-02-24 10:20:24 -08:00
|
|
|
else { self.expect(t); }
|
2012-08-03 19:59:04 -07:00
|
|
|
}
|
|
|
|
_ => ()
|
2012-05-24 12:38:45 -07:00
|
|
|
}
|
2013-02-24 18:55:24 -08:00
|
|
|
if sep.trailing_sep_allowed && *self.token == *ket { break; }
|
2013-03-02 13:02:27 -08:00
|
|
|
v.push(f(self));
|
2012-04-19 21:18:33 -07:00
|
|
|
}
|
2013-02-24 18:55:24 -08:00
|
|
|
return v;
|
2012-04-19 21:18:33 -07:00
|
|
|
}
|
|
|
|
|
2013-02-04 13:15:17 -08:00
|
|
|
// parse a sequence, including the closing delimiter. The function
|
|
|
|
// f must consume tokens until reaching the next separator or
|
|
|
|
// closing bracket.
|
2013-05-31 15:17:22 -07:00
|
|
|
pub fn parse_unspanned_seq<T: Copy>(&self,
|
|
|
|
bra: &token::Token,
|
|
|
|
ket: &token::Token,
|
|
|
|
sep: SeqSep,
|
|
|
|
f: &fn(&Parser) -> T)
|
|
|
|
-> ~[T] {
|
2013-02-24 18:55:24 -08:00
|
|
|
self.expect(bra);
|
2013-02-24 15:41:54 -08:00
|
|
|
let result = self.parse_seq_to_before_end(ket, sep, f);
|
2012-06-07 22:09:27 -07:00
|
|
|
self.bump();
|
2013-02-24 15:41:54 -08:00
|
|
|
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.
|
2013-05-31 15:17:22 -07:00
|
|
|
pub fn parse_seq<T: Copy>(&self,
|
|
|
|
bra: &token::Token,
|
|
|
|
ket: &token::Token,
|
|
|
|
sep: SeqSep,
|
|
|
|
f: &fn(&Parser) -> T)
|
|
|
|
-> spanned<~[T]> {
|
2012-05-24 12:38:45 -07:00
|
|
|
let lo = self.span.lo;
|
2013-02-24 18:55:24 -08:00
|
|
|
self.expect(bra);
|
2013-02-24 15:41:54 -08:00
|
|
|
let result = self.parse_seq_to_before_end(ket, sep, f);
|
2012-05-24 12:38:45 -07:00
|
|
|
let hi = self.span.hi;
|
|
|
|
self.bump();
|
2013-02-24 15:41:54 -08:00
|
|
|
spanned(lo, hi, result)
|
2012-05-24 12:38:45 -07:00
|
|
|
}
|
2012-06-07 21:53:47 -07:00
|
|
|
}
|