add self.token_to_str and is_any_keyword convenience abstractions

This commit is contained in:
John Clements
2013-04-15 16:13:42 -07:00
parent 7e4cd09e2e
commit e7aa24de18
2 changed files with 55 additions and 25 deletions

View File

@@ -47,17 +47,29 @@ pub fn seq_sep_none() -> SeqSep {
}
}
// maps any token back to a string. not necessary if you know it's
// an identifier....
pub fn token_to_str(reader: @reader, token: &token::Token) -> ~str {
token::to_str(reader.interner(), token)
}
pub impl Parser {
// convert a token to a string using self's reader
fn token_to_str(&self, token: &token::Token) -> ~str {
token::to_str(self.reader.interner(), token)
}
// convert the current token to a string using self's reader
fn this_token_to_str(&self) -> ~str {
self.token_to_str(self.token)
}
fn unexpected_last(&self, t: &token::Token) -> ! {
self.span_fatal(
*self.last_span,
fmt!(
"unexpected token: `%s`",
token_to_str(self.reader, t)
self.token_to_str(t)
)
);
}
@@ -66,7 +78,7 @@ pub impl Parser {
self.fatal(
fmt!(
"unexpected token: `%s`",
token_to_str(self.reader, &copy *self.token)
self.this_token_to_str()
)
);
}
@@ -80,8 +92,8 @@ pub impl Parser {
self.fatal(
fmt!(
"expected `%s` but found `%s`",
token_to_str(self.reader, t),
token_to_str(self.reader, &copy *self.token)
self.token_to_str(t),
self.this_token_to_str()
)
)
}
@@ -104,7 +116,7 @@ pub impl Parser {
self.fatal(
fmt!(
"expected ident, found `%s`",
token_to_str(self.reader, &copy *self.token)
self.this_token_to_str()
)
);
}
@@ -128,12 +140,15 @@ pub impl Parser {
// Storing keywords as interned idents instead of strings would be nifty.
// A sanity check that the word we are asking for is a known keyword
// NOTE: this could be done statically....
fn require_keyword(&self, word: &~str) {
if !self.keywords.contains(word) {
self.bug(fmt!("unknown keyword: %s", *word));
}
}
// return true when this token represents the given string, and is not
// followed immediately by :: .
fn token_is_word(&self, word: &~str, tok: &token::Token) -> bool {
match *tok {
token::IDENT(sid, false) => { *self.id_to_str(sid) == *word }
@@ -150,6 +165,10 @@ pub impl Parser {
self.token_is_keyword(word, &copy *self.token)
}
fn id_is_any_keyword(&self, id: ast::ident) -> bool {
self.keywords.contains(self.id_to_str(id))
}
fn is_any_keyword(&self, tok: &token::Token) -> bool {
match *tok {
token::IDENT(sid, false) => {
@@ -182,7 +201,7 @@ pub impl Parser {
fmt!(
"expected `%s`, found `%s`",
*word,
token_to_str(self.reader, &copy *self.token)
self.this_token_to_str()
)
);
}
@@ -248,9 +267,9 @@ pub impl Parser {
);
} else {
let mut s: ~str = ~"expected `";
s += token_to_str(self.reader, &token::GT);
s += self.token_to_str(&token::GT);
s += ~"`, found `";
s += token_to_str(self.reader, &copy *self.token);
s += self.this_token_to_str();
s += ~"`";
self.fatal(s);
}