rust/src/libsyntax/parse/common.rs

363 lines
10 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.
use core::prelude::*;
use ast;
2013-01-30 09:56:33 -08:00
use codemap::{BytePos, spanned};
use parse::lexer::reader;
use parse::parser::Parser;
use parse::token;
use core::option::{None, Option, Some};
use core::option;
use std::oldmap::HashMap;
// SeqSep : a sequence separator (token)
2013-02-04 13:15:17 -08:00
// and whether a trailing separator is allowed.
pub struct SeqSep {
sep: Option<token::Token>,
trailing_sep_allowed: bool
}
2012-05-24 12:38:45 -07:00
2013-02-24 18:32:02 -08:00
pub fn seq_sep_trailing_disallowed(+t: token::Token) -> SeqSep {
SeqSep {
2013-02-24 15:41:54 -08:00
sep: Some(t),
trailing_sep_allowed: false,
}
2012-05-24 12:38:45 -07:00
}
2013-02-24 18:32:02 -08:00
pub fn seq_sep_trailing_allowed(+t: token::Token) -> SeqSep {
SeqSep {
2013-02-24 15:41:54 -08:00
sep: Some(t),
trailing_sep_allowed: true,
}
2012-05-24 12:38:45 -07:00
}
pub fn seq_sep_none() -> SeqSep {
SeqSep {
2013-02-24 15:41:54 -08:00
sep: None,
trailing_sep_allowed: false,
}
2012-05-24 12:38:45 -07:00
}
pub fn token_to_str(reader: reader, token: &token::Token) -> ~str {
token::to_str(reader.interner(), token)
}
pub impl Parser {
fn unexpected_last(t: token::Token) -> ! {
self.span_fatal(
*self.last_span,
2013-02-24 15:41:54 -08:00
fmt!(
"unexpected token: `%s`",
token_to_str(self.reader, &t)
2013-02-24 15:41:54 -08:00
)
);
}
2012-05-24 12:38:45 -07:00
fn unexpected() -> ! {
2013-02-24 15:41:54 -08:00
self.fatal(
fmt!(
"unexpected token: `%s`",
token_to_str(self.reader, &copy *self.token)
2013-02-24 15:41:54 -08: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.
fn expect(t: &token::Token) {
if *self.token == *t {
2012-05-24 12:38:45 -07:00
self.bump();
} else {
self.fatal(
fmt!(
"expected `%s` but found `%s`",
token_to_str(self.reader, t),
token_to_str(self.reader, &copy *self.token)
)
)
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();
match *self.token {
2013-02-24 15:41:54 -08:00
token::IDENT(i, _) => {
self.bump();
i
}
token::INTERPOLATED(token::nt_ident(*)) => {
self.bug(
~"ident interpolation not converted to real token"
);
}
_ => {
self.fatal(
fmt!(
"expected ident, found `%s`",
token_to_str(self.reader, &copy *self.token)
2013-02-24 15:41:54 -08:00
)
);
}
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;
spanned(lo, hi, ast::path_list_ident_ { 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();
}
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-02-24 09:54:41 -08:00
fn eat(tok: &token::Token) -> bool {
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(word) {
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 {
match *tok {
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 {
self.token_is_keyword(word, &copy *self.token)
2012-05-24 12:38:45 -07:00
}
fn is_any_keyword(tok: &token::Token) -> bool {
match *tok {
2012-08-03 19:59:04 -07:00
token::IDENT(sid, false) => {
self.keywords.contains_key(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) {
2013-02-24 15:41:54 -08:00
self.fatal(
fmt!(
"expected `%s`, found `%s`",
*word,
token_to_str(self.reader, &copy *self.token)
2013-02-24 15:41:54 -08:00
)
);
2012-06-12 10:59:50 -07:00
}
}
fn is_strict_keyword(word: &~str) -> bool {
self.strict_keywords.contains_key(word)
}
fn check_strict_keywords() {
match *self.token {
token::IDENT(_, false) => {
let w = token_to_str(self.reader, &copy *self.token);
self.check_strict_keywords_(&w);
}
_ => ()
}
}
fn check_strict_keywords_(w: &~str) {
if self.is_strict_keyword(w) {
2013-02-24 15:41:54 -08:00
self.fatal(fmt!("found `%s` in ident position", *w));
}
}
fn is_reserved_keyword(word: &~str) -> bool {
self.reserved_keywords.contains_key(word)
2012-09-11 19:26:48 -07:00
}
fn check_reserved_keywords() {
match *self.token {
token::IDENT(_, false) => {
let w = token_to_str(self.reader, &copy *self.token);
self.check_reserved_keywords_(&w);
}
_ => ()
2012-09-11 19:26:48 -07:00
}
}
fn check_reserved_keywords_(w: &~str) {
2012-09-11 19:26:48 -07:00
if self.is_reserved_keyword(w) {
2013-02-24 15:41:54 -08:00
self.fatal(fmt!("`%s` is a reserved keyword", *w));
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
// with a single > and continue.
2012-05-24 12:38:45 -07:00
fn expect_gt() {
if *self.token == token::GT {
2012-05-24 12:38:45 -07:00
self.bump();
} 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 {
let mut s: ~str = ~"expected `";
s += token_to_str(self.reader, &token::GT);
s += ~"`, found `";
s += token_to_str(self.reader, &copy *self.token);
s += ~"`";
2012-05-24 12:38:45 -07:00
self.fatal(s);
}
}
2013-02-04 13:15:17 -08:00
// parse a sequence bracketed by '<' and '>', stopping
// before the '>'.
2013-02-24 15:41:54 -08: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;
let mut v = ~[];
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
}
2013-02-24 15:41:54 -08: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
}
2013-02-04 13:15:17 -08:00
// parse a sequence bracketed by '<' and '>'
2013-02-24 15:41:54 -08: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);
2012-05-24 12:38:45 -07:00
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
}
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-02-24 15:41:54 -08:00
fn parse_seq_to_end<T: Copy>(
ket: &token::Token,
2013-02-24 15:41:54 -08:00
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-02-24 15:41:54 -08:00
fn parse_seq_to_before_end<T: Copy>(
ket: &token::Token,
2013-02-24 15:41:54 -08:00
sep: SeqSep,
f: fn(Parser) -> T
) -> ~[T] {
2012-05-24 12:38:45 -07:00
let mut first: bool = true;
let mut v: ~[T] = ~[];
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));
}
return v;
}
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-02-24 15:41:54 -08:00
fn parse_unspanned_seq<T: Copy>(
bra: &token::Token,
ket: &token::Token,
2013-02-24 15:41:54 -08:00
sep: SeqSep,
f: fn(Parser) -> T
) -> ~[T] {
self.expect(bra);
2013-02-24 15:41:54 -08:00
let result = self.parse_seq_to_before_end(ket, sep, f);
self.bump();
2013-02-24 15:41:54 -08:00
result
}
// NB: Do not use this function unless you actually plan to place the
// spanned list in the AST.
2013-02-24 15:41:54 -08:00
fn parse_seq<T: Copy>(
bra: &token::Token,
ket: &token::Token,
2013-02-24 15:41:54 -08:00
sep: SeqSep,
f: fn(Parser) -> T
) -> spanned<~[T]> {
2012-05-24 12:38:45 -07:00
let lo = self.span.lo;
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
}