Implement to_wstr() for ParseTokenType and ParseKeyword

This cleans up some messy call sites.
This commit is contained in:
ridiculousfish 2023-08-05 15:00:44 -07:00
parent cc1e4b998a
commit d2f7a3507b
3 changed files with 19 additions and 15 deletions

View File

@ -115,10 +115,10 @@ pub trait Node: Acceptor + ConcreteNode + std::fmt::Debug {
fn describe(&self) -> WString { fn describe(&self) -> WString {
let mut res = ast_type_to_string(self.typ()).to_owned(); let mut res = ast_type_to_string(self.typ()).to_owned();
if let Some(n) = self.as_token() { if let Some(n) = self.as_token() {
let token_type: &'static wstr = n.token_type().into(); let token_type = n.token_type().to_wstr();
res += &sprintf!(" '%ls'"L, token_type)[..]; res += &sprintf!(" '%ls'"L, token_type)[..];
} else if let Some(n) = self.as_keyword() { } else if let Some(n) = self.as_keyword() {
let keyword: &'static wstr = n.keyword().into(); let keyword = n.keyword().to_wstr();
res += &sprintf!(" '%ls'"L, keyword)[..]; res += &sprintf!(" '%ls'"L, keyword)[..];
} }
res res
@ -2341,7 +2341,7 @@ impl Ast {
result += &sprintf!(": '%ls'"L, argsrc)[..]; result += &sprintf!(": '%ls'"L, argsrc)[..];
} }
} else if let Some(n) = node.as_keyword() { } else if let Some(n) = node.as_keyword() {
result += &sprintf!("keyword: %ls"L, Into::<&'static wstr>::into(n.keyword()))[..]; result += &sprintf!("keyword: %ls"L, n.keyword().to_wstr())[..];
} else if let Some(n) = node.as_token() { } else if let Some(n) = node.as_token() {
let desc = match n.token_type() { let desc = match n.token_type() {
ParseTokenType::string => { ParseTokenType::string => {
@ -2754,7 +2754,9 @@ impl<'s> NodeVisitorMut for Populator<'s> {
if self.unwinding { if self.unwinding {
return; return;
} }
let VisitResult::Break(error) = flow else { return; }; let VisitResult::Break(error) = flow else {
return;
};
/// We believe the node is some sort of block statement. Attempt to find a source range /// We believe the node is some sort of block statement. Attempt to find a source range
/// for the block's keyword (for, if, etc) and a user-presentable description. This /// for the block's keyword (for, if, etc) and a user-presentable description. This

View File

@ -252,10 +252,11 @@ impl Default for ParseTokenType {
} }
} }
impl From<ParseTokenType> for &'static wstr { impl ParseTokenType {
/// Return a string describing the token type.
#[widestrs] #[widestrs]
fn from(token_type: ParseTokenType) -> Self { pub fn to_wstr(self) -> &'static wstr {
match token_type { match self {
ParseTokenType::comment => "ParseTokenType::comment"L, ParseTokenType::comment => "ParseTokenType::comment"L,
ParseTokenType::error => "ParseTokenType::error"L, ParseTokenType::error => "ParseTokenType::error"L,
ParseTokenType::tokenizer_error => "ParseTokenType::tokenizer_error"L, ParseTokenType::tokenizer_error => "ParseTokenType::tokenizer_error"L,
@ -279,10 +280,11 @@ impl Default for ParseKeyword {
} }
} }
impl From<ParseKeyword> for &'static wstr { impl ParseKeyword {
/// Return the keyword as a string.
#[widestrs] #[widestrs]
fn from(keyword: ParseKeyword) -> Self { pub fn to_wstr(self) -> &'static wstr {
match keyword { match self {
ParseKeyword::kw_exclam => "!"L, ParseKeyword::kw_exclam => "!"L,
ParseKeyword::kw_and => "and"L, ParseKeyword::kw_and => "and"L,
ParseKeyword::kw_begin => "begin"L, ParseKeyword::kw_begin => "begin"L,
@ -308,7 +310,7 @@ impl From<ParseKeyword> for &'static wstr {
impl printf_compat::args::ToArg<'static> for ParseKeyword { impl printf_compat::args::ToArg<'static> for ParseKeyword {
fn to_arg(self) -> printf_compat::args::Arg<'static> { fn to_arg(self) -> printf_compat::args::Arg<'static> {
printf_compat::args::Arg::Str(self.into()) printf_compat::args::Arg::Str(self.to_wstr())
} }
} }
@ -559,7 +561,7 @@ pub fn token_type_user_presentable_description(
keyword: ParseKeyword, keyword: ParseKeyword,
) -> WString { ) -> WString {
if keyword != ParseKeyword::none { if keyword != ParseKeyword::none {
return sprintf!("keyword: '%ls'"L, Into::<&'static wstr>::into(keyword)); return sprintf!("keyword: '%ls'"L, keyword.to_wstr());
} }
match type_ { match type_ {
ParseTokenType::string => "a string"L.to_owned(), ParseTokenType::string => "a string"L.to_owned(),
@ -573,7 +575,7 @@ pub fn token_type_user_presentable_description(
ParseTokenType::error => "a parse error"L.to_owned(), ParseTokenType::error => "a parse error"L.to_owned(),
ParseTokenType::tokenizer_error => "an incomplete token"L.to_owned(), ParseTokenType::tokenizer_error => "an incomplete token"L.to_owned(),
ParseTokenType::comment => "a comment"L.to_owned(), ParseTokenType::comment => "a comment"L.to_owned(),
_ => sprintf!("a %ls"L, Into::<&'static wstr>::into(type_)), _ => sprintf!("a %ls"L, type_.to_wstr()),
} }
} }

View File

@ -73,9 +73,9 @@ impl ParseToken {
} }
/// Returns a string description of the given parse token. /// Returns a string description of the given parse token.
pub fn describe(&self) -> WString { pub fn describe(&self) -> WString {
let mut result = Into::<&'static wstr>::into(self.typ).to_owned(); let mut result = self.typ.to_wstr().to_owned();
if self.keyword != ParseKeyword::none { if self.keyword != ParseKeyword::none {
result += &sprintf!(L!(" <%ls>"), Into::<&'static wstr>::into(self.keyword))[..] sprintf!(=> &mut result, " <%ls>", self.keyword.to_wstr())
} }
result result
} }