libsyntax: add &self to parser methods

This commit is contained in:
Erick Tryzelaar 2013-03-02 13:02:27 -08:00
parent 3c23589b08
commit 4a73426aa2
5 changed files with 303 additions and 261 deletions

View File

@ -21,21 +21,24 @@ use core::either::{Either, Left, Right};
// a parser that can parse attributes.
pub trait parser_attr {
fn parse_outer_attributes() -> ~[ast::attribute];
fn parse_attribute(style: ast::attr_style) -> ast::attribute;
fn parse_attribute_naked(style: ast::attr_style, lo: BytePos) ->
ast::attribute;
fn parse_inner_attrs_and_next() ->
fn parse_outer_attributes(&self) -> ~[ast::attribute];
fn parse_attribute(&self, style: ast::attr_style) -> ast::attribute;
fn parse_attribute_naked(
&self,
style: ast::attr_style,
lo: BytePos
) -> ast::attribute;
fn parse_inner_attrs_and_next(&self) ->
(~[ast::attribute], ~[ast::attribute]);
fn parse_meta_item() -> @ast::meta_item;
fn parse_meta_seq() -> ~[@ast::meta_item];
fn parse_optional_meta() -> ~[@ast::meta_item];
fn parse_meta_item(&self) -> @ast::meta_item;
fn parse_meta_seq(&self) -> ~[@ast::meta_item];
fn parse_optional_meta(&self) -> ~[@ast::meta_item];
}
impl parser_attr for Parser {
// Parse attributes that appear before an item
fn parse_outer_attributes() -> ~[ast::attribute] {
fn parse_outer_attributes(&self) -> ~[ast::attribute] {
let mut attrs: ~[ast::attribute] = ~[];
loop {
match *self.token {
@ -63,13 +66,13 @@ impl parser_attr for Parser {
return attrs;
}
fn parse_attribute(style: ast::attr_style) -> ast::attribute {
fn parse_attribute(&self, style: ast::attr_style) -> ast::attribute {
let lo = self.span.lo;
self.expect(&token::POUND);
return self.parse_attribute_naked(style, lo);
}
fn parse_attribute_naked(style: ast::attr_style, lo: BytePos) ->
fn parse_attribute_naked(&self, style: ast::attr_style, lo: BytePos) ->
ast::attribute {
self.expect(&token::LBRACKET);
let meta_item = self.parse_meta_item();
@ -89,7 +92,7 @@ impl parser_attr for Parser {
// you can make the 'next' field an Option, but the result is going to be
// more useful as a vector.
fn parse_inner_attrs_and_next() ->
fn parse_inner_attrs_and_next(&self) ->
(~[ast::attribute], ~[ast::attribute]) {
let mut inner_attrs: ~[ast::attribute] = ~[];
let mut next_outer_attrs: ~[ast::attribute] = ~[];
@ -135,7 +138,7 @@ impl parser_attr for Parser {
(inner_attrs, next_outer_attrs)
}
fn parse_meta_item() -> @ast::meta_item {
fn parse_meta_item(&self) -> @ast::meta_item {
let lo = self.span.lo;
let name = self.id_to_str(self.parse_ident());
match *self.token {
@ -157,7 +160,7 @@ impl parser_attr for Parser {
}
}
fn parse_meta_seq() -> ~[@ast::meta_item] {
fn parse_meta_seq(&self) -> ~[@ast::meta_item] {
copy self.parse_seq(
&token::LPAREN,
&token::RPAREN,
@ -166,7 +169,7 @@ impl parser_attr for Parser {
).node
}
fn parse_optional_meta() -> ~[@ast::meta_item] {
fn parse_optional_meta(&self) -> ~[@ast::meta_item] {
match *self.token {
token::LPAREN => self.parse_meta_seq(),
_ => ~[]

View File

@ -54,7 +54,7 @@ pub fn token_to_str(reader: reader, token: &token::Token) -> ~str {
}
pub impl Parser {
fn unexpected_last(t: &token::Token) -> ! {
fn unexpected_last(&self, t: &token::Token) -> ! {
self.span_fatal(
*self.last_span,
fmt!(
@ -64,7 +64,7 @@ pub impl Parser {
);
}
fn unexpected() -> ! {
fn unexpected(&self) -> ! {
self.fatal(
fmt!(
"unexpected token: `%s`",
@ -75,7 +75,7 @@ pub impl Parser {
// expect and consume the token t. Signal an error if
// the next token is not t.
fn expect(t: &token::Token) {
fn expect(&self, t: &token::Token) {
if *self.token == *t {
self.bump();
} else {
@ -89,7 +89,7 @@ pub impl Parser {
}
}
fn parse_ident() -> ast::ident {
fn parse_ident(&self) -> ast::ident {
self.check_strict_keywords();
self.check_reserved_keywords();
match *self.token {
@ -113,7 +113,7 @@ pub impl Parser {
}
}
fn parse_path_list_ident() -> ast::path_list_ident {
fn parse_path_list_ident(&self) -> ast::path_list_ident {
let lo = self.span.lo;
let ident = self.parse_ident();
let hi = self.span.hi;
@ -121,42 +121,42 @@ pub impl Parser {
id: self.get_id() })
}
fn parse_value_ident() -> ast::ident {
fn parse_value_ident(&self) -> ast::ident {
return self.parse_ident();
}
// consume token 'tok' if it exists. Returns true if the given
// token was present, false otherwise.
fn eat(tok: &token::Token) -> bool {
fn eat(&self, tok: &token::Token) -> bool {
return if *self.token == *tok { self.bump(); true } else { false };
}
// 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
fn require_keyword(word: &~str) {
fn require_keyword(&self, word: &~str) {
if !self.keywords.contains_key(word) {
self.bug(fmt!("unknown keyword: %s", *word));
}
}
pure fn token_is_word(word: &~str, tok: &token::Token) -> bool {
pure fn token_is_word(&self, word: &~str, tok: &token::Token) -> bool {
match *tok {
token::IDENT(sid, false) => { *self.id_to_str(sid) == *word }
_ => { false }
}
}
fn token_is_keyword(word: &~str, tok: &token::Token) -> bool {
fn token_is_keyword(&self, word: &~str, tok: &token::Token) -> bool {
self.require_keyword(word);
self.token_is_word(word, tok)
}
fn is_keyword(word: &~str) -> bool {
fn is_keyword(&self, word: &~str) -> bool {
self.token_is_keyword(word, &copy *self.token)
}
fn is_any_keyword(tok: &token::Token) -> bool {
fn is_any_keyword(&self, tok: &token::Token) -> bool {
match *tok {
token::IDENT(sid, false) => {
self.keywords.contains_key(self.id_to_str(sid))
@ -165,7 +165,7 @@ pub impl Parser {
}
}
fn eat_keyword(word: &~str) -> bool {
fn eat_keyword(&self, word: &~str) -> bool {
self.require_keyword(word);
let is_kw = match *self.token {
token::IDENT(sid, false) => *word == *self.id_to_str(sid),
@ -175,7 +175,7 @@ pub impl Parser {
is_kw
}
fn expect_keyword(word: &~str) {
fn expect_keyword(&self, word: &~str) {
self.require_keyword(word);
if !self.eat_keyword(word) {
self.fatal(
@ -188,11 +188,11 @@ pub impl Parser {
}
}
fn is_strict_keyword(word: &~str) -> bool {
fn is_strict_keyword(&self, word: &~str) -> bool {
self.strict_keywords.contains_key(word)
}
fn check_strict_keywords() {
fn check_strict_keywords(&self) {
match *self.token {
token::IDENT(_, false) => {
let w = token_to_str(self.reader, &copy *self.token);
@ -202,17 +202,17 @@ pub impl Parser {
}
}
fn check_strict_keywords_(w: &~str) {
fn check_strict_keywords_(&self, w: &~str) {
if self.is_strict_keyword(w) {
self.fatal(fmt!("found `%s` in ident position", *w));
}
}
fn is_reserved_keyword(word: &~str) -> bool {
fn is_reserved_keyword(&self, word: &~str) -> bool {
self.reserved_keywords.contains_key(word)
}
fn check_reserved_keywords() {
fn check_reserved_keywords(&self) {
match *self.token {
token::IDENT(_, false) => {
let w = token_to_str(self.reader, &copy *self.token);
@ -222,7 +222,7 @@ pub impl Parser {
}
}
fn check_reserved_keywords_(w: &~str) {
fn check_reserved_keywords_(&self, w: &~str) {
if self.is_reserved_keyword(w) {
self.fatal(fmt!("`%s` is a reserved keyword", *w));
}
@ -230,7 +230,7 @@ pub impl Parser {
// expect and consume a GT. if a >> is seen, replace it
// with a single > and continue.
fn expect_gt() {
fn expect_gt(&self) {
if *self.token == token::GT {
self.bump();
} else if *self.token == token::BINOP(token::SHR) {
@ -252,6 +252,7 @@ pub impl Parser {
// parse a sequence bracketed by '<' and '>', stopping
// before the '>'.
fn parse_seq_to_before_gt<T: Copy>(
&self,
sep: Option<token::Token>,
f: fn(&Parser) -> T
) -> OptVec<T> {
@ -266,12 +267,13 @@ pub impl Parser {
}
_ => ()
}
v.push(f(&self));
v.push(f(self));
}
return v;
}
fn parse_seq_to_gt<T: Copy>(
&self,
sep: Option<token::Token>,
f: fn(&Parser) -> T
) -> OptVec<T> {
@ -284,6 +286,7 @@ pub impl Parser {
// f must consume tokens until reaching the next separator or
// closing bracket.
fn parse_seq_to_end<T: Copy>(
&self,
ket: &token::Token,
sep: SeqSep,
f: fn(&Parser) -> T
@ -297,6 +300,7 @@ pub impl Parser {
// f must consume tokens until reaching the next separator or
// closing bracket.
fn parse_seq_to_before_end<T: Copy>(
&self,
ket: &token::Token,
sep: SeqSep,
f: fn(&Parser) -> T
@ -312,7 +316,7 @@ pub impl Parser {
_ => ()
}
if sep.trailing_sep_allowed && *self.token == *ket { break; }
v.push(f(&self));
v.push(f(self));
}
return v;
}
@ -321,6 +325,7 @@ pub impl Parser {
// f must consume tokens until reaching the next separator or
// closing bracket.
fn parse_unspanned_seq<T: Copy>(
&self,
bra: &token::Token,
ket: &token::Token,
sep: SeqSep,
@ -335,6 +340,7 @@ pub impl Parser {
// NB: Do not use this function unless you actually plan to place the
// spanned list in the AST.
fn parse_seq<T: Copy>(
&self,
bra: &token::Token,
ket: &token::Token,
sep: SeqSep,

View File

@ -62,7 +62,7 @@ impl to_bytes::IterBytes for ObsoleteSyntax {
pub impl Parser {
/// Reports an obsolete syntax non-fatal error.
fn obsolete(sp: span, kind: ObsoleteSyntax) {
fn obsolete(&self, sp: span, kind: ObsoleteSyntax) {
let (kind_str, desc) = match kind {
ObsoleteLowerCaseKindBounds => (
"lower-case kind bounds",
@ -154,12 +154,12 @@ pub impl Parser {
// Reports an obsolete syntax non-fatal error, and returns
// a placeholder expression
fn obsolete_expr(sp: span, kind: ObsoleteSyntax) -> @expr {
fn obsolete_expr(&self, sp: span, kind: ObsoleteSyntax) -> @expr {
self.obsolete(sp, kind);
self.mk_expr(sp.lo, sp.hi, expr_lit(@respan(sp, lit_nil)))
}
priv fn report(sp: span, kind: ObsoleteSyntax, kind_str: &str,
priv fn report(&self, sp: span, kind: ObsoleteSyntax, kind_str: &str,
desc: &str) {
self.span_err(sp, fmt!("obsolete syntax: %s", kind_str));
@ -169,7 +169,7 @@ pub impl Parser {
}
}
fn token_is_obsolete_ident(ident: &str, token: Token) -> bool {
fn token_is_obsolete_ident(&self, ident: &str, token: Token) -> bool {
match token {
token::IDENT(copy sid, _) => {
str::eq_slice(*self.id_to_str(sid), ident)
@ -178,11 +178,11 @@ pub impl Parser {
}
}
fn is_obsolete_ident(ident: &str) -> bool {
fn is_obsolete_ident(&self, ident: &str) -> bool {
self.token_is_obsolete_ident(ident, *self.token)
}
fn eat_obsolete_ident(ident: &str) -> bool {
fn eat_obsolete_ident(&self, ident: &str) -> bool {
if self.is_obsolete_ident(ident) {
self.bump();
true
@ -191,7 +191,7 @@ pub impl Parser {
}
}
fn try_parse_obsolete_struct_ctor() -> bool {
fn try_parse_obsolete_struct_ctor(&self) -> bool {
if self.eat_obsolete_ident("new") {
self.obsolete(*self.last_span, ObsoleteStructCtor);
self.parse_fn_decl(|p| p.parse_arg());
@ -202,7 +202,7 @@ pub impl Parser {
}
}
fn try_parse_obsolete_with() -> bool {
fn try_parse_obsolete_with(&self) -> bool {
if *self.token == token::COMMA
&& self.token_is_obsolete_ident("with",
self.look_ahead(1u)) {
@ -217,7 +217,7 @@ pub impl Parser {
}
}
fn try_parse_obsolete_priv_section() -> bool {
fn try_parse_obsolete_priv_section(&self) -> bool {
if self.is_keyword(&~"priv") && self.look_ahead(1) == token::LBRACE {
self.obsolete(copy *self.span, ObsoletePrivSection);
self.eat_keyword(&~"priv");

File diff suppressed because it is too large Load Diff

View File

@ -359,16 +359,16 @@ pub struct ident_interner {
}
pub impl ident_interner {
fn intern(val: @~str) -> ast::ident {
fn intern(&self, val: @~str) -> ast::ident {
ast::ident { repr: self.interner.intern(val) }
}
fn gensym(val: @~str) -> ast::ident {
fn gensym(&self, val: @~str) -> ast::ident {
ast::ident { repr: self.interner.gensym(val) }
}
pure fn get(idx: ast::ident) -> @~str {
pure fn get(&self, idx: ast::ident) -> @~str {
self.interner.get(idx.repr)
}
fn len() -> uint {
fn len(&self) -> uint {
self.interner.len()
}
}