fix(src/parsing): Remove the extra `error` attribute keyword

This keyword marked an enum as being an error, which is exactly what
`derive(Error)` already does. Thus this extra attribute keyword has been
removed and the `error` name has been used to rename the `msg` attribute
keyword, to be in-line with what `thiserror` does.
This commit is contained in:
Benedikt Peetz 2024-05-19 14:03:10 +02:00
parent d27eea711a
commit 5a3f337057
Signed by: bpeetz
GPG Key ID: B6139BCB07CE946D
8 changed files with 31 additions and 46 deletions

View File

@ -47,13 +47,14 @@ DocNamedType = {DocComment} {Attribute} NamedType;
# (* This is syntax sugar for a `DocAttribute` *)
DocComment = "///" {ANYTHING} LineEnding;
DocAttribute = "doc" "=" StringLiteral;
Attribute = "#" "[" AttributeValue "]" LineEnding;
AttributeValue = DeriveAttribute | DocAttribute | ErrorAttribute | MsgAttribute;
ErrorAttribute = "error";
MsgAttribute = "msg" "(" StringLiteral ")";
AttributeValue = DeriveAttribute | DocAttribute | ErrorAttribute;
DeriveAttribute = "derive" "(" "Error" ")";
DocAttribute = "doc" "=" StringLiteral;
ErrorAttribute = "error" "=" StringLiteral;
Comment = "//" [ NOT ("/" {ANYTHING} LineEnding) | "//"] {ANYTHING} LineEnding;
LineEnding = "\\n" | "\\r" | "\\r\\n";

View File

@ -23,7 +23,7 @@
use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
use crate::parser::command_spec::{Attribute, Identifier};
use crate::parser::command_spec::{unchecked::DeriveValue, Attribute, Identifier};
impl Attribute {
pub fn to_rust(&self, _target: &Identifier) -> TokenStream2 {
@ -31,14 +31,17 @@ impl Attribute {
Attribute::doc(comment) => quote! {
#[doc = #comment]
},
Attribute::error => quote! {
// We simply use thiserror here
#[derive(trixy::__private::thiserror::Error)]
},
Attribute::msg(msg) => quote! {
Attribute::error(msg) => quote! {
#[error(#msg)]
},
Attribute::derive(_) => unimplemented!("Derive is not used as of now"),
Attribute::derive(attr) => {
match attr {
DeriveValue::Error => quote! {
// We simply use `thiserror` here
#[derive(trixy::__private::thiserror::Error)]
},
}
}
}
}
}

View File

@ -246,20 +246,19 @@ impl TokenKind {
pub enum Attribute {
#[allow(non_camel_case_types)]
doc(String),
#[allow(non_camel_case_types)]
derive(DeriveValue),
#[allow(non_camel_case_types)]
error,
#[allow(non_camel_case_types)]
msg(String),
error(String),
}
impl From<unchecked::Attribute> for Attribute {
fn from(value: unchecked::Attribute) -> Self {
match value {
unchecked::Attribute::doc { content: name, .. } => Self::doc(name.content),
unchecked::Attribute::derive { value, .. } => Self::derive(value),
unchecked::Attribute::error { .. } => Self::error,
unchecked::Attribute::msg { content, .. } => Self::msg(content.content),
unchecked::Attribute::error { content, .. } => Self::error(content.content),
}
}
}

View File

@ -76,12 +76,12 @@ pub enum Attribute {
content: StringLiteral,
span: TokenSpan,
},
#[allow(non_camel_case_types)]
derive { value: DeriveValue, span: TokenSpan },
#[allow(non_camel_case_types)]
error { span: TokenSpan },
#[allow(non_camel_case_types)]
msg {
error {
content: StringLiteral,
span: TokenSpan,
},
@ -93,7 +93,6 @@ impl Display for Attribute {
Attribute::doc { .. } => f.write_str("doc"),
Attribute::derive { .. } => f.write_str("derive"),
Attribute::error { .. } => f.write_str("error"),
Attribute::msg { .. } => f.write_str("msg"),
}
}
}
@ -104,7 +103,6 @@ impl Attribute {
Attribute::doc { span, .. } => *span,
Attribute::derive { span, .. } => *span,
Attribute::error { span, .. } => *span,
Attribute::msg { span, .. } => *span,
}
}
}

View File

@ -264,7 +264,7 @@ pub enum Keyword {
/// Keywords used in attributes: (#[<keyword>(<value>)])
#[derive(Debug, PartialEq, PartialOrd, Ord, Eq, Clone, Copy)]
pub enum AttributeKeyword {
/// Derive a trait
/// Derive a trait (only `Error` for now)
#[allow(non_camel_case_types)]
derive,
@ -272,13 +272,9 @@ pub enum AttributeKeyword {
#[allow(non_camel_case_types)]
doc,
/// Mark the beginning of an error
/// Add an error message
#[allow(non_camel_case_types)]
error,
/// Encompass an error message
#[allow(non_camel_case_types)]
msg,
}
impl Display for Keyword {
@ -298,7 +294,6 @@ impl Display for AttributeKeyword {
AttributeKeyword::derive => f.write_str("derive"),
AttributeKeyword::doc => f.write_str("doc"),
AttributeKeyword::error => f.write_str("error"),
AttributeKeyword::msg => f.write_str("msg"),
}
}
}

View File

@ -272,7 +272,6 @@ fn tokenize_ident(text: &str) -> Result<(TokenKind, usize), LexingError> {
"derive" => TokenKind::AttributeKeyword(AttributeKeyword::derive),
"doc" => TokenKind::AttributeKeyword(AttributeKeyword::doc),
"error" => TokenKind::AttributeKeyword(AttributeKeyword::error),
"msg" => TokenKind::AttributeKeyword(AttributeKeyword::msg),
other => TokenKind::Identifier(other.to_string()),
};

View File

@ -243,7 +243,7 @@ impl Parser {
mem::take(&mut state.token.kind).to_identifier(Variant::DocNamedType);
DocIdentifier {
name: ident.name,
attributes: take_attrs! {state, doc, msg},
attributes: take_attrs! {state, doc, error},
variant: Variant::DocNamedType,
}
})
@ -252,7 +252,7 @@ impl Parser {
Ok(Enumeration {
identifier,
states,
attributes: take_attrs! {enumeration, doc, derive, error},
attributes: take_attrs! {enumeration, doc, derive},
})
}

View File

@ -176,14 +176,6 @@ impl Parser {
}
}
fn parse_bracket_string_literal(&mut self) -> Result<StringLiteral, ParsingError> {
self.expect(token![CurvedBracketOpen])?;
let string_literal = self.expect(token![StringLiteral])?;
self.expect(token![CurvedBracketClose])?;
let string_literal = Into::<StringLiteral>::into(string_literal);
Ok(string_literal)
}
fn parse_attribute_value(&mut self) -> Result<Attribute, ParsingError> {
let ident = self.expect(token![AttributeKeyword])?;
let span = *ident.span();
@ -220,18 +212,16 @@ impl Parser {
self.expect(token![=])?;
let string_literal = self.expect(token![StringLiteral])?;
let string_literal = Into::<StringLiteral>::into(string_literal);
if self.expect_peek(token![PoundSign]) {
dbg!(&self.token_stream);
}
Ok(Attribute::doc {
content: string_literal,
span,
})
}
AttributeKeyword::error => Ok(Attribute::error { span }),
AttributeKeyword::msg => {
let string_literal = self.parse_bracket_string_literal()?;
Ok(Attribute::msg {
AttributeKeyword::error => {
self.expect(token![=])?;
let string_literal = self.expect(token![StringLiteral])?;
let string_literal = Into::<StringLiteral>::into(string_literal);
Ok(Attribute::error {
content: string_literal,
span,
})