add self.token_to_str and is_any_keyword convenience abstractions
This commit is contained in:
parent
7e4cd09e2e
commit
e7aa24de18
2 changed files with 55 additions and 25 deletions
|
@ -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 {
|
pub fn token_to_str(reader: @reader, token: &token::Token) -> ~str {
|
||||||
token::to_str(reader.interner(), token)
|
token::to_str(reader.interner(), token)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub impl Parser {
|
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) -> ! {
|
fn unexpected_last(&self, t: &token::Token) -> ! {
|
||||||
self.span_fatal(
|
self.span_fatal(
|
||||||
*self.last_span,
|
*self.last_span,
|
||||||
fmt!(
|
fmt!(
|
||||||
"unexpected token: `%s`",
|
"unexpected token: `%s`",
|
||||||
token_to_str(self.reader, t)
|
self.token_to_str(t)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -66,7 +78,7 @@ pub impl Parser {
|
||||||
self.fatal(
|
self.fatal(
|
||||||
fmt!(
|
fmt!(
|
||||||
"unexpected token: `%s`",
|
"unexpected token: `%s`",
|
||||||
token_to_str(self.reader, © *self.token)
|
self.this_token_to_str()
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -80,8 +92,8 @@ pub impl Parser {
|
||||||
self.fatal(
|
self.fatal(
|
||||||
fmt!(
|
fmt!(
|
||||||
"expected `%s` but found `%s`",
|
"expected `%s` but found `%s`",
|
||||||
token_to_str(self.reader, t),
|
self.token_to_str(t),
|
||||||
token_to_str(self.reader, © *self.token)
|
self.this_token_to_str()
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -104,7 +116,7 @@ pub impl Parser {
|
||||||
self.fatal(
|
self.fatal(
|
||||||
fmt!(
|
fmt!(
|
||||||
"expected ident, found `%s`",
|
"expected ident, found `%s`",
|
||||||
token_to_str(self.reader, © *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.
|
// 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
|
// 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) {
|
fn require_keyword(&self, word: &~str) {
|
||||||
if !self.keywords.contains(word) {
|
if !self.keywords.contains(word) {
|
||||||
self.bug(fmt!("unknown keyword: %s", *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 {
|
fn token_is_word(&self, word: &~str, tok: &token::Token) -> bool {
|
||||||
match *tok {
|
match *tok {
|
||||||
token::IDENT(sid, false) => { *self.id_to_str(sid) == *word }
|
token::IDENT(sid, false) => { *self.id_to_str(sid) == *word }
|
||||||
|
@ -150,6 +165,10 @@ pub impl Parser {
|
||||||
self.token_is_keyword(word, © *self.token)
|
self.token_is_keyword(word, © *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 {
|
fn is_any_keyword(&self, tok: &token::Token) -> bool {
|
||||||
match *tok {
|
match *tok {
|
||||||
token::IDENT(sid, false) => {
|
token::IDENT(sid, false) => {
|
||||||
|
@ -182,7 +201,7 @@ pub impl Parser {
|
||||||
fmt!(
|
fmt!(
|
||||||
"expected `%s`, found `%s`",
|
"expected `%s`, found `%s`",
|
||||||
*word,
|
*word,
|
||||||
token_to_str(self.reader, © *self.token)
|
self.this_token_to_str()
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -248,9 +267,9 @@ pub impl Parser {
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
let mut s: ~str = ~"expected `";
|
let mut s: ~str = ~"expected `";
|
||||||
s += token_to_str(self.reader, &token::GT);
|
s += self.token_to_str(&token::GT);
|
||||||
s += ~"`, found `";
|
s += ~"`, found `";
|
||||||
s += token_to_str(self.reader, © *self.token);
|
s += self.this_token_to_str();
|
||||||
s += ~"`";
|
s += ~"`";
|
||||||
self.fatal(s);
|
self.fatal(s);
|
||||||
}
|
}
|
||||||
|
|
|
@ -66,7 +66,7 @@ use codemap::{span, BytePos, spanned, mk_sp};
|
||||||
use codemap;
|
use codemap;
|
||||||
use parse::attr::parser_attr;
|
use parse::attr::parser_attr;
|
||||||
use parse::classify;
|
use parse::classify;
|
||||||
use parse::common::{seq_sep_none, token_to_str};
|
use parse::common::{seq_sep_none};
|
||||||
use parse::common::{seq_sep_trailing_disallowed, seq_sep_trailing_allowed};
|
use parse::common::{seq_sep_trailing_disallowed, seq_sep_trailing_allowed};
|
||||||
use parse::lexer::reader;
|
use parse::lexer::reader;
|
||||||
use parse::lexer::TokenAndSpan;
|
use parse::lexer::TokenAndSpan;
|
||||||
|
@ -252,8 +252,11 @@ pub fn Parser(sess: @mut ParseSess,
|
||||||
pub struct Parser {
|
pub struct Parser {
|
||||||
sess: @mut ParseSess,
|
sess: @mut ParseSess,
|
||||||
cfg: crate_cfg,
|
cfg: crate_cfg,
|
||||||
|
// the current token:
|
||||||
token: @mut token::Token,
|
token: @mut token::Token,
|
||||||
|
// the span of the current token:
|
||||||
span: @mut span,
|
span: @mut span,
|
||||||
|
// the span of the prior token:
|
||||||
last_span: @mut span,
|
last_span: @mut span,
|
||||||
buffer: @mut [TokenAndSpan, ..4],
|
buffer: @mut [TokenAndSpan, ..4],
|
||||||
buffer_start: @mut int,
|
buffer_start: @mut int,
|
||||||
|
@ -499,7 +502,7 @@ pub impl Parser {
|
||||||
let hi = p.last_span.hi;
|
let hi = p.last_span.hi;
|
||||||
debug!("parse_trait_methods(): trait method signature ends in \
|
debug!("parse_trait_methods(): trait method signature ends in \
|
||||||
`%s`",
|
`%s`",
|
||||||
token_to_str(p.reader, © *p.token));
|
self.this_token_to_str());
|
||||||
match *p.token {
|
match *p.token {
|
||||||
token::SEMI => {
|
token::SEMI => {
|
||||||
p.bump();
|
p.bump();
|
||||||
|
@ -541,7 +544,7 @@ pub impl Parser {
|
||||||
p.fatal(
|
p.fatal(
|
||||||
fmt!(
|
fmt!(
|
||||||
"expected `;` or `}` but found `%s`",
|
"expected `;` or `}` but found `%s`",
|
||||||
token_to_str(p.reader, © *p.token)
|
self.this_token_to_str()
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -1456,6 +1459,11 @@ pub impl Parser {
|
||||||
fn parse_token_tree(&self) -> token_tree {
|
fn parse_token_tree(&self) -> token_tree {
|
||||||
maybe_whole!(deref self, nt_tt);
|
maybe_whole!(deref self, nt_tt);
|
||||||
|
|
||||||
|
// this is the fall-through for the 'match' below.
|
||||||
|
// invariants: the current token is not a left-delimiter,
|
||||||
|
// not an EOF, and not the desired right-delimiter (if
|
||||||
|
// it were, parse_seq_to_before_end would have prevented
|
||||||
|
// reaching this point.
|
||||||
fn parse_non_delim_tt_tok(p: &Parser) -> token_tree {
|
fn parse_non_delim_tt_tok(p: &Parser) -> token_tree {
|
||||||
maybe_whole!(deref p, nt_tt);
|
maybe_whole!(deref p, nt_tt);
|
||||||
match *p.token {
|
match *p.token {
|
||||||
|
@ -1464,7 +1472,7 @@ pub impl Parser {
|
||||||
p.fatal(
|
p.fatal(
|
||||||
fmt!(
|
fmt!(
|
||||||
"incorrect close delimiter: `%s`",
|
"incorrect close delimiter: `%s`",
|
||||||
token_to_str(p.reader, © *p.token)
|
p.this_token_to_str()
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -1506,18 +1514,17 @@ pub impl Parser {
|
||||||
|
|
||||||
match *self.token {
|
match *self.token {
|
||||||
token::EOF => {
|
token::EOF => {
|
||||||
self.fatal(~"file ended in the middle of a macro invocation");
|
self.fatal(~"file ended with unbalanced delimiters");
|
||||||
}
|
}
|
||||||
token::LPAREN | token::LBRACE | token::LBRACKET => {
|
token::LPAREN | token::LBRACE | token::LBRACKET => {
|
||||||
// tjc: ??????
|
let close_delim = token::flip_delimiter(&*self.token);
|
||||||
let ket = token::flip_delimiter(&*self.token);
|
|
||||||
tt_delim(
|
tt_delim(
|
||||||
vec::append(
|
vec::append(
|
||||||
// the open delimiter:
|
// the open delimiter:
|
||||||
~[parse_any_tt_tok(self)],
|
~[parse_any_tt_tok(self)],
|
||||||
vec::append(
|
vec::append(
|
||||||
self.parse_seq_to_before_end(
|
self.parse_seq_to_before_end(
|
||||||
&ket,
|
&close_delim,
|
||||||
seq_sep_none(),
|
seq_sep_none(),
|
||||||
|p| p.parse_token_tree()
|
|p| p.parse_token_tree()
|
||||||
),
|
),
|
||||||
|
@ -1531,6 +1538,8 @@ pub impl Parser {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// parse a stream of tokens into a list of token_trees,
|
||||||
|
// up to EOF.
|
||||||
fn parse_all_token_trees(&self) -> ~[token_tree] {
|
fn parse_all_token_trees(&self) -> ~[token_tree] {
|
||||||
let mut tts = ~[];
|
let mut tts = ~[];
|
||||||
while *self.token != token::EOF {
|
while *self.token != token::EOF {
|
||||||
|
@ -2053,6 +2062,7 @@ pub impl Parser {
|
||||||
return e;
|
return e;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// parse the RHS of a local variable declaration (e.g. '= 14;')
|
||||||
fn parse_initializer(&self) -> Option<@expr> {
|
fn parse_initializer(&self) -> Option<@expr> {
|
||||||
match *self.token {
|
match *self.token {
|
||||||
token::EQ => {
|
token::EQ => {
|
||||||
|
@ -2139,7 +2149,7 @@ pub impl Parser {
|
||||||
self.fatal(
|
self.fatal(
|
||||||
fmt!(
|
fmt!(
|
||||||
"expected `}`, found `%s`",
|
"expected `}`, found `%s`",
|
||||||
token_to_str(self.reader, © *self.token)
|
self.this_token_to_str()
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -2407,6 +2417,7 @@ pub impl Parser {
|
||||||
pat_ident(binding_mode, name, sub)
|
pat_ident(binding_mode, name, sub)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// parse a local variable declaration
|
||||||
fn parse_local(&self, is_mutbl: bool,
|
fn parse_local(&self, is_mutbl: bool,
|
||||||
allow_init: bool) -> @local {
|
allow_init: bool) -> @local {
|
||||||
let lo = self.span.lo;
|
let lo = self.span.lo;
|
||||||
|
@ -2652,7 +2663,7 @@ pub impl Parser {
|
||||||
fmt!(
|
fmt!(
|
||||||
"expected `;` or `}` after \
|
"expected `;` or `}` after \
|
||||||
expression but found `%s`",
|
expression but found `%s`",
|
||||||
token_to_str(self.reader, &t)
|
self.token_to_str(&t)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -2867,7 +2878,7 @@ pub impl Parser {
|
||||||
self.fatal(
|
self.fatal(
|
||||||
fmt!(
|
fmt!(
|
||||||
"expected `self` but found `%s`",
|
"expected `self` but found `%s`",
|
||||||
token_to_str(self.reader, © *self.token)
|
self.this_token_to_str()
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -2991,7 +3002,7 @@ pub impl Parser {
|
||||||
self.fatal(
|
self.fatal(
|
||||||
fmt!(
|
fmt!(
|
||||||
"expected `,` or `)`, found `%s`",
|
"expected `,` or `)`, found `%s`",
|
||||||
token_to_str(self.reader, © *self.token)
|
self.this_token_to_str()
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -3271,7 +3282,7 @@ pub impl Parser {
|
||||||
fmt!(
|
fmt!(
|
||||||
"expected `{`, `(`, or `;` after struct name \
|
"expected `{`, `(`, or `;` after struct name \
|
||||||
but found `%s`",
|
but found `%s`",
|
||||||
token_to_str(self.reader, © *self.token)
|
self.this_token_to_str()
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -3321,7 +3332,7 @@ pub impl Parser {
|
||||||
copy *self.span,
|
copy *self.span,
|
||||||
fmt!(
|
fmt!(
|
||||||
"expected `;`, `,`, or '}' but found `%s`",
|
"expected `;`, `,`, or '}' but found `%s`",
|
||||||
token_to_str(self.reader, © *self.token)
|
self.this_token_to_str()
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -3423,7 +3434,7 @@ pub impl Parser {
|
||||||
self.fatal(
|
self.fatal(
|
||||||
fmt!(
|
fmt!(
|
||||||
"expected item but found `%s`",
|
"expected item but found `%s`",
|
||||||
token_to_str(self.reader, © *self.token)
|
self.this_token_to_str()
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -3683,7 +3694,7 @@ pub impl Parser {
|
||||||
copy *self.span,
|
copy *self.span,
|
||||||
fmt!(
|
fmt!(
|
||||||
"expected `{` or `mod` but found `%s`",
|
"expected `{` or `mod` but found `%s`",
|
||||||
token_to_str(self.reader, © *self.token)
|
self.this_token_to_str()
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -3696,7 +3707,7 @@ pub impl Parser {
|
||||||
copy *self.span,
|
copy *self.span,
|
||||||
fmt!(
|
fmt!(
|
||||||
"expected foreign module name but found `%s`",
|
"expected foreign module name but found `%s`",
|
||||||
token_to_str(self.reader, © *self.token)
|
self.this_token_to_str()
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue