rust/src/libsyntax/parse/common.rs

280 lines
8.4 KiB
Rust
Raw Normal View History

// 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;
use parse::parser::Parser;
use parse::lexer::reader;
2012-05-24 12:38:45 -07:00
type seq_sep = {
sep: Option<token::Token>,
trailing_sep_allowed: bool
2012-05-24 12:38:45 -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
}
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
}
fn token_to_str(reader: reader, ++token: token::Token) -> ~str {
token::to_str(reader.interner(), token)
}
impl Parser {
fn unexpected_last(t: token::Token) -> ! {
self.span_fatal(
copy self.last_span,
~"unexpected token: `" + token_to_str(self.reader, t) + ~"`");
}
2012-05-24 12:38:45 -07:00
fn unexpected() -> ! {
self.fatal(~"unexpected token: `"
+ token_to_str(self.reader, self.token) + ~"`");
}
fn expect(t: token::Token) {
2012-05-24 12:38:45 -07:00
if self.token == t {
self.bump();
} else {
let mut s: ~str = ~"expected `";
2012-05-24 12:38:45 -07:00
s += token_to_str(self.reader, t);
s += ~"` but found `";
2012-05-24 12:38:45 -07:00
s += token_to_str(self.reader, self.token);
self.fatal(s + ~"`");
2012-05-24 12:38:45 -07:00
}
}
2012-05-24 12:38:45 -07:00
fn parse_ident() -> ast::ident {
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(
~"ident interpolation not converted to real token"); }
2012-08-03 19:59:04 -07:00
_ => { self.fatal(~"expected ident, found `"
+ token_to_str(self.reader, self.token)
+ ~"`"); }
2012-05-24 12:38:45 -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-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();
}
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-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
fn require_keyword(word: ~str) {
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
}
}
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
}
}
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)
}
fn is_keyword(word: ~str) -> bool {
2012-05-24 12:38:45 -07:00
self.token_is_keyword(word, self.token)
}
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-08-03 19:59:04 -07:00
_ => false
}
}
fn eat_keyword(word: ~str) -> bool {
2012-05-24 12:38:45 -07:00
self.require_keyword(word);
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
};
if is_kw { self.bump() }
is_kw
}
fn expect_keyword(word: ~str) {
2012-05-24 12:38:45 -07:00
self.require_keyword(word);
if !self.eat_keyword(word) {
self.fatal(~"expected `" + word + ~"`, found `" +
token_to_str(self.reader, self.token) +
~"`");
2012-06-12 10:59:50 -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) {
self.swap(token::GT, self.span.lo + BytePos(1u), self.span.hi);
2012-05-24 12:38:45 -07:00
} else {
let mut s: ~str = ~"expected `";
2012-05-24 12:38:45 -07:00
s += token_to_str(self.reader, token::GT);
s += ~"`, found `";
2012-05-24 12:38:45 -07:00
s += token_to_str(self.reader, self.token);
s += ~"`";
2012-05-24 12:38:45 -07:00
self.fatal(s);
}
}
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;
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 {
Some(ref t) => {
2012-08-03 19:59:04 -07:00
if first { first = false; }
else { self.expect((*t)); }
2012-08-03 19:59:04 -07:00
}
_ => ()
2012-05-24 12:38:45 -07: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
}
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-08-01 17:30:05 -07:00
return v;
2012-05-24 12:38:45 -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
}
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
}
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;
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 {
Some(ref t) => {
2012-08-03 19:59:04 -07:00
if first { first = false; }
else { self.expect((*t)); }
2012-08-03 19:59:04 -07:00
}
_ => ()
2012-05-24 12:38:45 -07:00
}
if sep.trailing_sep_allowed && self.token == ket { break; }
v.push(f(self));
}
2012-08-01 17:30:05 -07:00
return v;
}
fn parse_unspanned_seq<T: Copy>(bra: token::Token,
ket: token::Token,
sep: seq_sep,
f: fn(Parser) -> T) -> ~[T] {
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;
}
// NB: Do not use this function unless you actually plan to place the
// spanned list in the AST.
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
}