From d8b1fa0ae0c27e54d3539190683c01e194d36fbd Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Mon, 27 Oct 2014 19:22:52 +1100 Subject: Use PascalCase for token variants --- src/libsyntax/ext/tt/macro_parser.rs | 16 ++++++++-------- src/libsyntax/ext/tt/macro_rules.rs | 17 +++++++++-------- src/libsyntax/ext/tt/transcribe.rs | 12 ++++++------ 3 files changed, 23 insertions(+), 22 deletions(-) (limited to 'src/libsyntax/ext/tt') diff --git a/src/libsyntax/ext/tt/macro_parser.rs b/src/libsyntax/ext/tt/macro_parser.rs index cea8cab5265..6d30de96a3c 100644 --- a/src/libsyntax/ext/tt/macro_parser.rs +++ b/src/libsyntax/ext/tt/macro_parser.rs @@ -85,7 +85,7 @@ use parse::lexer::*; //resolve bug? use parse::ParseSess; use parse::attr::ParserAttr; use parse::parser::{LifetimeAndTypesWithoutColons, Parser}; -use parse::token::{Token, EOF, Nonterminal}; +use parse::token::{Token, Nonterminal}; use parse::token; use ptr::P; @@ -226,8 +226,8 @@ pub fn parse_or_else(sess: &ParseSess, /// unhygienic comparison) pub fn token_name_eq(t1 : &Token, t2 : &Token) -> bool { match (t1,t2) { - (&token::IDENT(id1,_),&token::IDENT(id2,_)) - | (&token::LIFETIME(id1),&token::LIFETIME(id2)) => + (&token::Ident(id1,_),&token::Ident(id2,_)) + | (&token::Lifetime(id1),&token::Lifetime(id2)) => id1.name == id2.name, _ => *t1 == *t2 } @@ -354,9 +354,9 @@ pub fn parse(sess: &ParseSess, // Built-in nonterminals never start with these tokens, // so we can eliminate them from consideration. match tok { - token::RPAREN | - token::RBRACE | - token::RBRACKET => {}, + token::RParen | + token::RBrace | + token::RBracket => {}, _ => bb_eis.push(ei) } } @@ -372,7 +372,7 @@ pub fn parse(sess: &ParseSess, } /* error messages here could be improved with links to orig. rules */ - if token_name_eq(&tok, &EOF) { + if token_name_eq(&tok, &token::Eof) { if eof_eis.len() == 1u { let mut v = Vec::new(); for dv in eof_eis.get_mut(0).matches.iter_mut() { @@ -447,7 +447,7 @@ pub fn parse_nt(p: &mut Parser, name: &str) -> Nonterminal { "ty" => token::NtTy(p.parse_ty(false /* no need to disambiguate*/)), // this could be handled like a token, since it is one "ident" => match p.token { - token::IDENT(sn,b) => { p.bump(); token::NtIdent(box sn,b) } + token::Ident(sn,b) => { p.bump(); token::NtIdent(box sn,b) } _ => { let token_str = token::to_string(&p.token); p.fatal((format!("expected ident, found {}", diff --git a/src/libsyntax/ext/tt/macro_rules.rs b/src/libsyntax/ext/tt/macro_rules.rs index 3b51fb380b8..20428e50c7f 100644 --- a/src/libsyntax/ext/tt/macro_rules.rs +++ b/src/libsyntax/ext/tt/macro_rules.rs @@ -20,7 +20,7 @@ use parse::lexer::new_tt_reader; use parse::parser::Parser; use parse::attr::ParserAttr; use parse::token::{special_idents, gensym_ident}; -use parse::token::{FAT_ARROW, SEMI, NtMatchers, NtTT, EOF}; +use parse::token::{NtMatchers, NtTT}; use parse::token; use print; use ptr::P; @@ -43,10 +43,10 @@ impl<'a> ParserAnyMacro<'a> { /// allowed to be there. fn ensure_complete_parse(&self, allow_semi: bool) { let mut parser = self.parser.borrow_mut(); - if allow_semi && parser.token == SEMI { + if allow_semi && parser.token == token::Semi { parser.bump() } - if parser.token != EOF { + if parser.token != token::Eof { let token_str = parser.this_token_to_string(); let msg = format!("macro expansion ignores token `{}` and any \ following", @@ -89,7 +89,7 @@ impl<'a> MacResult for ParserAnyMacro<'a> { loop { let mut parser = self.parser.borrow_mut(); match parser.token { - EOF => break, + token::Eof => break, _ => { let attrs = parser.parse_outer_attributes(); ret.push(parser.parse_method(attrs, ast::Inherited)) @@ -231,12 +231,13 @@ pub fn add_new_extension<'cx>(cx: &'cx mut ExtCtxt, let argument_gram = vec!( ms(MatchSeq(vec!( ms(MatchNonterminal(lhs_nm, special_idents::matchers, 0u)), - ms(MatchTok(FAT_ARROW)), - ms(MatchNonterminal(rhs_nm, special_idents::tt, 1u))), Some(SEMI), - ast::OneOrMore, 0u, 2u)), + ms(MatchTok(token::FatArrow)), + ms(MatchNonterminal(rhs_nm, special_idents::tt, 1u))), + Some(token::Semi), ast::OneOrMore, 0u, 2u)), //to phase into semicolon-termination instead of //semicolon-separation - ms(MatchSeq(vec!(ms(MatchTok(SEMI))), None, ast::ZeroOrMore, 2u, 2u))); + ms(MatchSeq(vec!(ms(MatchTok(token::Semi))), None, + ast::ZeroOrMore, 2u, 2u))); // Parse the macro_rules! invocation (`none` is for no interpolations): diff --git a/src/libsyntax/ext/tt/transcribe.rs b/src/libsyntax/ext/tt/transcribe.rs index 1bb519f66cd..2c7b583d460 100644 --- a/src/libsyntax/ext/tt/transcribe.rs +++ b/src/libsyntax/ext/tt/transcribe.rs @@ -13,7 +13,7 @@ use ast::{TokenTree, TtDelimited, TtToken, TtSequence, TtNonterminal, Ident}; use codemap::{Span, DUMMY_SP}; use diagnostic::SpanHandler; use ext::tt::macro_parser::{NamedMatch, MatchedSeq, MatchedNonterminal}; -use parse::token::{EOF, INTERPOLATED, IDENT, Token, NtIdent}; +use parse::token::{Token, NtIdent}; use parse::token; use parse::lexer::TokenAndSpan; @@ -66,7 +66,7 @@ pub fn new_tt_reader<'a>(sp_diag: &'a SpanHandler, repeat_idx: Vec::new(), repeat_len: Vec::new(), /* dummy values, never read: */ - cur_tok: EOF, + cur_tok: token::Eof, cur_span: DUMMY_SP, }; tt_next_token(&mut r); /* get cur_tok and cur_span set up */ @@ -158,7 +158,7 @@ pub fn tt_next_token(r: &mut TtReader) -> TokenAndSpan { loop { let should_pop = match r.stack.last() { None => { - assert_eq!(ret_val.tok, EOF); + assert_eq!(ret_val.tok, token::Eof); return ret_val; } Some(frame) => { @@ -175,7 +175,7 @@ pub fn tt_next_token(r: &mut TtReader) -> TokenAndSpan { let prev = r.stack.pop().unwrap(); match r.stack.last_mut() { None => { - r.cur_tok = EOF; + r.cur_tok = token::Eof; return ret_val; } Some(frame) => { @@ -272,13 +272,13 @@ pub fn tt_next_token(r: &mut TtReader) -> TokenAndSpan { (b) we actually can, since it's a token. */ MatchedNonterminal(NtIdent(box sn, b)) => { r.cur_span = sp; - r.cur_tok = IDENT(sn,b); + r.cur_tok = token::Ident(sn,b); return ret_val; } MatchedNonterminal(ref other_whole_nt) => { // FIXME(pcwalton): Bad copy. r.cur_span = sp; - r.cur_tok = INTERPOLATED((*other_whole_nt).clone()); + r.cur_tok = token::Interpolated((*other_whole_nt).clone()); return ret_val; } MatchedSeq(..) => { -- cgit 1.4.1-3-g733a5 From 665ad9c175f746b78c7eae81432b543d2e16c3c9 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Tue, 28 Oct 2014 11:05:28 +1100 Subject: Move token-to-string functions into print::pprust --- src/libsyntax/ext/tt/macro_parser.rs | 5 +- src/libsyntax/parse/lexer/comments.rs | 4 +- src/libsyntax/parse/parser.rs | 3 +- src/libsyntax/parse/token.rs | 95 ------------------------------ src/libsyntax/print/pprust.rs | 107 ++++++++++++++++++++++++++++++++-- 5 files changed, 108 insertions(+), 106 deletions(-) (limited to 'src/libsyntax/ext/tt') diff --git a/src/libsyntax/ext/tt/macro_parser.rs b/src/libsyntax/ext/tt/macro_parser.rs index 6d30de96a3c..073bebcb3f6 100644 --- a/src/libsyntax/ext/tt/macro_parser.rs +++ b/src/libsyntax/ext/tt/macro_parser.rs @@ -87,6 +87,7 @@ use parse::attr::ParserAttr; use parse::parser::{LifetimeAndTypesWithoutColons, Parser}; use parse::token::{Token, Nonterminal}; use parse::token; +use print::pprust; use ptr::P; use std::rc::Rc; @@ -402,7 +403,7 @@ pub fn parse(sess: &ParseSess, nts, next_eis.len()).to_string()); } else if bb_eis.len() == 0u && next_eis.len() == 0u { return Failure(sp, format!("no rules expected the token `{}`", - token::to_string(&tok)).to_string()); + pprust::token_to_string(&tok)).to_string()); } else if next_eis.len() > 0u { /* Now process the next token */ while next_eis.len() > 0u { @@ -449,7 +450,7 @@ pub fn parse_nt(p: &mut Parser, name: &str) -> Nonterminal { "ident" => match p.token { token::Ident(sn,b) => { p.bump(); token::NtIdent(box sn,b) } _ => { - let token_str = token::to_string(&p.token); + let token_str = pprust::token_to_string(&p.token); p.fatal((format!("expected ident, found {}", token_str.as_slice())).as_slice()) } diff --git a/src/libsyntax/parse/lexer/comments.rs b/src/libsyntax/parse/lexer/comments.rs index 66b21aed552..3814ecfbe5b 100644 --- a/src/libsyntax/parse/lexer/comments.rs +++ b/src/libsyntax/parse/lexer/comments.rs @@ -15,7 +15,7 @@ use parse::lexer::{is_whitespace, Reader}; use parse::lexer::{StringReader, TokenAndSpan}; use parse::lexer::is_block_doc_comment; use parse::lexer; -use parse::token; +use print::pprust; use std::io; use std::str; @@ -373,7 +373,7 @@ pub fn gather_comments_and_literals(span_diagnostic: &diagnostic::SpanHandler, literals.push(Literal {lit: s.to_string(), pos: sp.lo}); }) } else { - debug!("tok: {}", token::to_string(&tok)); + debug!("tok: {}", pprust::token_to_string(&tok)); } first_read = false; } diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 54b1cc2dbad..654de709566 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -78,6 +78,7 @@ use parse::token::InternedString; use parse::token::{keywords, special_idents}; use parse::token; use parse::{new_sub_parser_from_file, ParseSess}; +use print::pprust; use ptr::P; use owned_slice::OwnedSlice; @@ -394,7 +395,7 @@ impl<'a> Parser<'a> { /// Convert a token to a string using self's reader pub fn token_to_string(token: &token::Token) -> String { - token::to_string(token) + pprust::token_to_string(token) } /// Convert the current token to a string using self's reader diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index 1a69944bffa..9ed8e4bc3a7 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -431,101 +431,6 @@ impl fmt::Show for Nonterminal { } } -pub fn binop_to_string(o: BinOpToken) -> &'static str { - match o { - Plus => "+", - Minus => "-", - Star => "*", - Slash => "/", - Percent => "%", - Caret => "^", - And => "&", - Or => "|", - Shl => "<<", - Shr => ">>", - } -} - -pub fn to_string(t: &Token) -> String { - match *t { - Eq => "=".into_string(), - Lt => "<".into_string(), - Le => "<=".into_string(), - EqEq => "==".into_string(), - Ne => "!=".into_string(), - Ge => ">=".into_string(), - Gt => ">".into_string(), - Not => "!".into_string(), - Tilde => "~".into_string(), - OrOr => "||".into_string(), - AndAnd => "&&".into_string(), - BinOp(op) => binop_to_string(op).into_string(), - BinOpEq(op) => format!("{}=", binop_to_string(op)), - - /* Structural symbols */ - At => "@".into_string(), - Dot => ".".into_string(), - DotDot => "..".into_string(), - DotDotDot => "...".into_string(), - Comma => ",".into_string(), - Semi => ";".into_string(), - Colon => ":".into_string(), - ModSep => "::".into_string(), - RArrow => "->".into_string(), - LArrow => "<-".into_string(), - FatArrow => "=>".into_string(), - LParen => "(".into_string(), - RParen => ")".into_string(), - LBracket => "[".into_string(), - RBracket => "]".into_string(), - LBrace => "{".into_string(), - RBrace => "}".into_string(), - Pound => "#".into_string(), - Dollar => "$".into_string(), - Question => "?".into_string(), - - /* Literals */ - LitByte(b) => format!("b'{}'", b.as_str()), - LitChar(c) => format!("'{}'", c.as_str()), - LitFloat(c) => c.as_str().into_string(), - LitInteger(c) => c.as_str().into_string(), - LitStr(s) => format!("\"{}\"", s.as_str()), - LitStrRaw(s, n) => format!("r{delim}\"{string}\"{delim}", - delim="#".repeat(n), - string=s.as_str()), - LitBinary(v) => format!("b\"{}\"", v.as_str()), - LitBinaryRaw(s, n) => format!("br{delim}\"{string}\"{delim}", - delim="#".repeat(n), - string=s.as_str()), - - /* Name components */ - Ident(s, _) => get_ident(s).get().into_string(), - Lifetime(s) => format!("{}", get_ident(s)), - Underscore => "_".into_string(), - - /* Other */ - DocComment(s) => s.as_str().into_string(), - Eof => "".into_string(), - Whitespace => " ".into_string(), - Comment => "/* */".into_string(), - Shebang(s) => format!("/* shebang: {}*/", s.as_str()), - - Interpolated(ref nt) => match *nt { - NtExpr(ref e) => ::print::pprust::expr_to_string(&**e), - NtMeta(ref e) => ::print::pprust::meta_item_to_string(&**e), - NtTy(ref e) => ::print::pprust::ty_to_string(&**e), - NtPath(ref e) => ::print::pprust::path_to_string(&**e), - NtItem(..) => "an interpolated item".into_string(), - NtBlock(..) => "an interpolated block".into_string(), - NtStmt(..) => "an interpolated statement".into_string(), - NtPat(..) => "an interpolated pattern".into_string(), - NtIdent(..) => "an interpolated identifier".into_string(), - NtTT(..) => "an interpolated tt".into_string(), - NtMatchers(..) => "an interpolated matcher sequence".into_string(), - } - } -} - // Get the first "argument" macro_rules! first { ( $first:expr, $( $remainder:expr, )* ) => ( $first ) diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 25ef8700ed0..a52987f5bd1 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -21,6 +21,7 @@ use attr::{AttrMetaMethods, AttributeMethods}; use codemap::{CodeMap, BytePos}; use codemap; use diagnostic; +use parse::token::{BinOpToken, Token}; use parse::token; use parse::lexer::comments; use parse; @@ -181,6 +182,101 @@ pub fn to_string(f: |&mut State| -> IoResult<()>) -> String { } } +pub fn binop_to_string(op: BinOpToken) -> &'static str { + match op { + token::Plus => "+", + token::Minus => "-", + token::Star => "*", + token::Slash => "/", + token::Percent => "%", + token::Caret => "^", + token::And => "&", + token::Or => "|", + token::Shl => "<<", + token::Shr => ">>", + } +} + +pub fn token_to_string(tok: &Token) -> String { + match *tok { + token::Eq => "=".into_string(), + token::Lt => "<".into_string(), + token::Le => "<=".into_string(), + token::EqEq => "==".into_string(), + token::Ne => "!=".into_string(), + token::Ge => ">=".into_string(), + token::Gt => ">".into_string(), + token::Not => "!".into_string(), + token::Tilde => "~".into_string(), + token::OrOr => "||".into_string(), + token::AndAnd => "&&".into_string(), + token::BinOp(op) => binop_to_string(op).into_string(), + token::BinOpEq(op) => format!("{}=", binop_to_string(op)), + + /* Structural symbols */ + token::At => "@".into_string(), + token::Dot => ".".into_string(), + token::DotDot => "..".into_string(), + token::DotDotDot => "...".into_string(), + token::Comma => ",".into_string(), + token::Semi => ";".into_string(), + token::Colon => ":".into_string(), + token::ModSep => "::".into_string(), + token::RArrow => "->".into_string(), + token::LArrow => "<-".into_string(), + token::FatArrow => "=>".into_string(), + token::LParen => "(".into_string(), + token::RParen => ")".into_string(), + token::LBracket => "[".into_string(), + token::RBracket => "]".into_string(), + token::LBrace => "{".into_string(), + token::RBrace => "}".into_string(), + token::Pound => "#".into_string(), + token::Dollar => "$".into_string(), + token::Question => "?".into_string(), + + /* Literals */ + token::LitByte(b) => format!("b'{}'", b.as_str()), + token::LitChar(c) => format!("'{}'", c.as_str()), + token::LitFloat(c) => c.as_str().into_string(), + token::LitInteger(c) => c.as_str().into_string(), + token::LitStr(s) => format!("\"{}\"", s.as_str()), + token::LitStrRaw(s, n) => format!("r{delim}\"{string}\"{delim}", + delim="#".repeat(n), + string=s.as_str()), + token::LitBinary(v) => format!("b\"{}\"", v.as_str()), + token::LitBinaryRaw(s, n) => format!("br{delim}\"{string}\"{delim}", + delim="#".repeat(n), + string=s.as_str()), + + /* Name components */ + token::Ident(s, _) => token::get_ident(s).get().into_string(), + token::Lifetime(s) => format!("{}", token::get_ident(s)), + token::Underscore => "_".into_string(), + + /* Other */ + token::DocComment(s) => s.as_str().into_string(), + token::Eof => "".into_string(), + token::Whitespace => " ".into_string(), + token::Comment => "/* */".into_string(), + token::Shebang(s) => format!("/* shebang: {}*/", s.as_str()), + + token::Interpolated(ref nt) => match *nt { + token::NtExpr(ref e) => expr_to_string(&**e), + token::NtMeta(ref e) => meta_item_to_string(&**e), + token::NtTy(ref e) => ty_to_string(&**e), + token::NtPath(ref e) => path_to_string(&**e), + token::NtItem(..) => "an interpolated item".into_string(), + token::NtBlock(..) => "an interpolated block".into_string(), + token::NtStmt(..) => "an interpolated statement".into_string(), + token::NtPat(..) => "an interpolated pattern".into_string(), + token::NtIdent(..) => "an interpolated identifier".into_string(), + token::NtTT(..) => "an interpolated tt".into_string(), + token::NtMatchers(..) => "an interpolated matcher sequence".into_string(), + } + } +} + // FIXME (Issue #16472): the thing_to_string_impls macro should go away // after we revise the syntax::ext::quote::ToToken impls to go directly // to token-trees instead of thing -> string -> token-trees. @@ -1026,14 +1122,14 @@ impl<'a> State<'a> { match *tt { ast::TtDelimited(_, ref delimed) => { let (ref open, ref tts, ref close) = **delimed; - try!(word(&mut self.s, parse::token::to_string(&open.token).as_slice())); + try!(word(&mut self.s, token_to_string(&open.token).as_slice())); try!(space(&mut self.s)); try!(self.print_tts(tts.as_slice())); try!(space(&mut self.s)); - word(&mut self.s, parse::token::to_string(&close.token).as_slice()) + word(&mut self.s, token_to_string(&close.token).as_slice()) }, ast::TtToken(_, ref tk) => { - try!(word(&mut self.s, parse::token::to_string(tk).as_slice())); + try!(word(&mut self.s, token_to_string(tk).as_slice())); match *tk { parse::token::DocComment(..) => { hardbreak(&mut self.s) @@ -1049,10 +1145,9 @@ impl<'a> State<'a> { try!(word(&mut self.s, ")")); match *separator { Some(ref tk) => { - try!(word(&mut self.s, - parse::token::to_string(tk).as_slice())); + try!(word(&mut self.s, token_to_string(tk).as_slice())); } - None => () + None => {}, } match kleene_op { ast::ZeroOrMore => word(&mut self.s, "*"), -- cgit 1.4.1-3-g733a5