From 849ea21b030d88c7470fc02dc2ceec2641b91e37 Mon Sep 17 00:00:00 2001 From: Benedikt Peetz Date: Sun, 19 May 2024 14:03:10 +0200 Subject: [PATCH] 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. --- docs/grammar.ebnf | 9 +++++---- .../convert/host/rust/attribute/mod.rs | 17 +++++++++------- src/parser/command_spec/checked.rs | 9 ++++----- src/parser/command_spec/unchecked.rs | 8 +++----- src/parser/lexing/mod.rs | 9 ++------- src/parser/lexing/tokenizer.rs | 1 - src/parser/parsing/checked/mod.rs | 4 ++-- src/parser/parsing/unchecked/mod.rs | 20 +++++-------------- 8 files changed, 31 insertions(+), 46 deletions(-) diff --git a/docs/grammar.ebnf b/docs/grammar.ebnf index 681c55c..626794e 100644 --- a/docs/grammar.ebnf +++ b/docs/grammar.ebnf @@ -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"; diff --git a/src/macros/generate/convert/host/rust/attribute/mod.rs b/src/macros/generate/convert/host/rust/attribute/mod.rs index 457ddd0..fb008e4 100644 --- a/src/macros/generate/convert/host/rust/attribute/mod.rs +++ b/src/macros/generate/convert/host/rust/attribute/mod.rs @@ -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)] + }, + } + } } } } diff --git a/src/parser/command_spec/checked.rs b/src/parser/command_spec/checked.rs index 900ba3e..221fdf6 100644 --- a/src/parser/command_spec/checked.rs +++ b/src/parser/command_spec/checked.rs @@ -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 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), } } } diff --git a/src/parser/command_spec/unchecked.rs b/src/parser/command_spec/unchecked.rs index 4423a4e..fbddba2 100644 --- a/src/parser/command_spec/unchecked.rs +++ b/src/parser/command_spec/unchecked.rs @@ -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, } } } diff --git a/src/parser/lexing/mod.rs b/src/parser/lexing/mod.rs index a612499..9d64b97 100644 --- a/src/parser/lexing/mod.rs +++ b/src/parser/lexing/mod.rs @@ -264,7 +264,7 @@ pub enum Keyword { /// Keywords used in attributes: (#[()]) #[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"), } } } diff --git a/src/parser/lexing/tokenizer.rs b/src/parser/lexing/tokenizer.rs index 12f152a..4f6f089 100644 --- a/src/parser/lexing/tokenizer.rs +++ b/src/parser/lexing/tokenizer.rs @@ -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()), }; diff --git a/src/parser/parsing/checked/mod.rs b/src/parser/parsing/checked/mod.rs index e169f1c..04ca1a7 100644 --- a/src/parser/parsing/checked/mod.rs +++ b/src/parser/parsing/checked/mod.rs @@ -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}, }) } diff --git a/src/parser/parsing/unchecked/mod.rs b/src/parser/parsing/unchecked/mod.rs index 7869a43..abc7b98 100644 --- a/src/parser/parsing/unchecked/mod.rs +++ b/src/parser/parsing/unchecked/mod.rs @@ -176,14 +176,6 @@ impl Parser { } } - fn parse_bracket_string_literal(&mut self) -> Result { - self.expect(token![CurvedBracketOpen])?; - let string_literal = self.expect(token![StringLiteral])?; - self.expect(token![CurvedBracketClose])?; - let string_literal = Into::::into(string_literal); - Ok(string_literal) - } - fn parse_attribute_value(&mut self) -> Result { 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::::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::::into(string_literal); + Ok(Attribute::error { content: string_literal, span, })