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` *) # (* This is syntax sugar for a `DocAttribute` *)
DocComment = "///" {ANYTHING} LineEnding; DocComment = "///" {ANYTHING} LineEnding;
DocAttribute = "doc" "=" StringLiteral;
Attribute = "#" "[" AttributeValue "]" LineEnding; Attribute = "#" "[" AttributeValue "]" LineEnding;
AttributeValue = DeriveAttribute | DocAttribute | ErrorAttribute | MsgAttribute; AttributeValue = DeriveAttribute | DocAttribute | ErrorAttribute;
ErrorAttribute = "error";
MsgAttribute = "msg" "(" StringLiteral ")";
DeriveAttribute = "derive" "(" "Error" ")"; DeriveAttribute = "derive" "(" "Error" ")";
DocAttribute = "doc" "=" StringLiteral; ErrorAttribute = "error" "=" StringLiteral;
Comment = "//" [ NOT ("/" {ANYTHING} LineEnding) | "//"] {ANYTHING} LineEnding; Comment = "//" [ NOT ("/" {ANYTHING} LineEnding) | "//"] {ANYTHING} LineEnding;
LineEnding = "\\n" | "\\r" | "\\r\\n"; LineEnding = "\\n" | "\\r" | "\\r\\n";

View File

@ -23,7 +23,7 @@
use proc_macro2::TokenStream as TokenStream2; use proc_macro2::TokenStream as TokenStream2;
use quote::quote; use quote::quote;
use crate::parser::command_spec::{Attribute, Identifier}; use crate::parser::command_spec::{unchecked::DeriveValue, Attribute, Identifier};
impl Attribute { impl Attribute {
pub fn to_rust(&self, _target: &Identifier) -> TokenStream2 { pub fn to_rust(&self, _target: &Identifier) -> TokenStream2 {
@ -31,14 +31,17 @@ impl Attribute {
Attribute::doc(comment) => quote! { Attribute::doc(comment) => quote! {
#[doc = #comment] #[doc = #comment]
}, },
Attribute::error => quote! { Attribute::error(msg) => quote! {
// We simply use thiserror here
#[derive(trixy::__private::thiserror::Error)]
},
Attribute::msg(msg) => quote! {
#[error(#msg)] #[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 { pub enum Attribute {
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]
doc(String), doc(String),
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]
derive(DeriveValue), derive(DeriveValue),
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]
error, error(String),
#[allow(non_camel_case_types)]
msg(String),
} }
impl From<unchecked::Attribute> for Attribute { impl From<unchecked::Attribute> for Attribute {
fn from(value: unchecked::Attribute) -> Self { fn from(value: unchecked::Attribute) -> Self {
match value { match value {
unchecked::Attribute::doc { content: name, .. } => Self::doc(name.content), unchecked::Attribute::doc { content: name, .. } => Self::doc(name.content),
unchecked::Attribute::derive { value, .. } => Self::derive(value), unchecked::Attribute::derive { value, .. } => Self::derive(value),
unchecked::Attribute::error { .. } => Self::error, unchecked::Attribute::error { content, .. } => Self::error(content.content),
unchecked::Attribute::msg { content, .. } => Self::msg(content.content),
} }
} }
} }

View File

@ -76,12 +76,12 @@ pub enum Attribute {
content: StringLiteral, content: StringLiteral,
span: TokenSpan, span: TokenSpan,
}, },
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]
derive { value: DeriveValue, span: TokenSpan }, derive { value: DeriveValue, span: TokenSpan },
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]
error { span: TokenSpan }, error {
#[allow(non_camel_case_types)]
msg {
content: StringLiteral, content: StringLiteral,
span: TokenSpan, span: TokenSpan,
}, },
@ -93,7 +93,6 @@ impl Display for Attribute {
Attribute::doc { .. } => f.write_str("doc"), Attribute::doc { .. } => f.write_str("doc"),
Attribute::derive { .. } => f.write_str("derive"), Attribute::derive { .. } => f.write_str("derive"),
Attribute::error { .. } => f.write_str("error"), Attribute::error { .. } => f.write_str("error"),
Attribute::msg { .. } => f.write_str("msg"),
} }
} }
} }
@ -104,7 +103,6 @@ impl Attribute {
Attribute::doc { span, .. } => *span, Attribute::doc { span, .. } => *span,
Attribute::derive { span, .. } => *span, Attribute::derive { span, .. } => *span,
Attribute::error { 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>)]) /// Keywords used in attributes: (#[<keyword>(<value>)])
#[derive(Debug, PartialEq, PartialOrd, Ord, Eq, Clone, Copy)] #[derive(Debug, PartialEq, PartialOrd, Ord, Eq, Clone, Copy)]
pub enum AttributeKeyword { pub enum AttributeKeyword {
/// Derive a trait /// Derive a trait (only `Error` for now)
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]
derive, derive,
@ -272,13 +272,9 @@ pub enum AttributeKeyword {
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]
doc, doc,
/// Mark the beginning of an error /// Add an error message
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]
error, error,
/// Encompass an error message
#[allow(non_camel_case_types)]
msg,
} }
impl Display for Keyword { impl Display for Keyword {
@ -298,7 +294,6 @@ impl Display for AttributeKeyword {
AttributeKeyword::derive => f.write_str("derive"), AttributeKeyword::derive => f.write_str("derive"),
AttributeKeyword::doc => f.write_str("doc"), AttributeKeyword::doc => f.write_str("doc"),
AttributeKeyword::error => f.write_str("error"), 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), "derive" => TokenKind::AttributeKeyword(AttributeKeyword::derive),
"doc" => TokenKind::AttributeKeyword(AttributeKeyword::doc), "doc" => TokenKind::AttributeKeyword(AttributeKeyword::doc),
"error" => TokenKind::AttributeKeyword(AttributeKeyword::error), "error" => TokenKind::AttributeKeyword(AttributeKeyword::error),
"msg" => TokenKind::AttributeKeyword(AttributeKeyword::msg),
other => TokenKind::Identifier(other.to_string()), other => TokenKind::Identifier(other.to_string()),
}; };

View File

@ -243,7 +243,7 @@ impl Parser {
mem::take(&mut state.token.kind).to_identifier(Variant::DocNamedType); mem::take(&mut state.token.kind).to_identifier(Variant::DocNamedType);
DocIdentifier { DocIdentifier {
name: ident.name, name: ident.name,
attributes: take_attrs! {state, doc, msg}, attributes: take_attrs! {state, doc, error},
variant: Variant::DocNamedType, variant: Variant::DocNamedType,
} }
}) })
@ -252,7 +252,7 @@ impl Parser {
Ok(Enumeration { Ok(Enumeration {
identifier, identifier,
states, 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> { fn parse_attribute_value(&mut self) -> Result<Attribute, ParsingError> {
let ident = self.expect(token![AttributeKeyword])?; let ident = self.expect(token![AttributeKeyword])?;
let span = *ident.span(); let span = *ident.span();
@ -220,18 +212,16 @@ impl Parser {
self.expect(token![=])?; self.expect(token![=])?;
let string_literal = self.expect(token![StringLiteral])?; let string_literal = self.expect(token![StringLiteral])?;
let string_literal = Into::<StringLiteral>::into(string_literal); let string_literal = Into::<StringLiteral>::into(string_literal);
if self.expect_peek(token![PoundSign]) {
dbg!(&self.token_stream);
}
Ok(Attribute::doc { Ok(Attribute::doc {
content: string_literal, content: string_literal,
span, span,
}) })
} }
AttributeKeyword::error => Ok(Attribute::error { span }), AttributeKeyword::error => {
AttributeKeyword::msg => { self.expect(token![=])?;
let string_literal = self.parse_bracket_string_literal()?; let string_literal = self.expect(token![StringLiteral])?;
Ok(Attribute::msg { let string_literal = Into::<StringLiteral>::into(string_literal);
Ok(Attribute::error {
content: string_literal, content: string_literal,
span, span,
}) })