From 3c3c9e5b24ab50f5872e105e2f90122cfd3f3688 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20H=C3=B6lting?= <87192362+moritz-hoelting@users.noreply.github.com> Date: Sat, 6 Apr 2024 14:40:44 +0200 Subject: [PATCH] Add serde dependency and derive serialization and deserialization for relevant structs --- Cargo.toml | 2 ++ src/base/source_file.rs | 2 ++ src/lexical/token.rs | 12 ++++++++++++ src/lexical/token_stream.rs | 5 ++++- src/syntax/syntax_tree/condition.rs | 9 ++++++++- src/syntax/syntax_tree/declaration.rs | 3 +++ src/syntax/syntax_tree/expression.rs | 7 +++++-- src/syntax/syntax_tree/mod.rs | 1 + src/syntax/syntax_tree/program.rs | 1 + src/syntax/syntax_tree/statement.rs | 6 ++++++ 10 files changed, 44 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 473f8f9..f49b4fa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ edition = "2021" [features] default = ["shulkerbox"] shulkerbox = ["dep:shulkerbox"] +serde = ["dep:serde"] [dependencies] chksum-md5 = "0.0.0" @@ -15,6 +16,7 @@ colored = "2.1.0" derive_more = { version = "0.99.17", default-features = false, features = ["deref", "from", "deref_mut"] } enum-as-inner = "0.6.0" getset = "0.1.2" +serde = { version = "1.0.197", features = ["derive", "rc"], optional = true } shulkerbox = { path = "../shulkerbox", optional = true} strum = { version = "0.26.2", features = ["derive"] } strum_macros = "0.26.2" diff --git a/src/base/source_file.rs b/src/base/source_file.rs index a85241a..a1fec39 100644 --- a/src/base/source_file.rs +++ b/src/base/source_file.rs @@ -16,6 +16,7 @@ use getset::{CopyGetters, Getters}; use super::Error; /// Represents a source file that contains the source code. +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Clone)] pub struct SourceFile { path: PathBuf, @@ -129,6 +130,7 @@ impl SourceFile { } /// Represents a range of characters in a source file. +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Clone, Getters, CopyGetters)] pub struct Span { /// Get the start byte index of the span. diff --git a/src/lexical/token.rs b/src/lexical/token.rs index 6326cba..35ba5ad 100644 --- a/src/lexical/token.rs +++ b/src/lexical/token.rs @@ -14,6 +14,7 @@ use strum_macros::EnumIter; use super::{error::UnterminatedDelimitedComment, Error}; /// Is an enumeration representing keywords in shulkerscript. +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, EnumIter)] #[allow(missing_docs)] pub enum KeywordKind { @@ -68,6 +69,7 @@ impl KeywordKind { } /// Is an enumeration containing all kinds of tokens in the Shulkerscript programming language. +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, From, EnumAsInner)] #[allow(missing_docs)] pub enum Token { @@ -117,6 +119,7 @@ impl SourceElement for Token { } /// Represents a contiguous sequence of whitespace characters. +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct WhiteSpaces { /// Is the span that makes up the token. @@ -129,6 +132,7 @@ impl SourceElement for WhiteSpaces { } } /// Represents a contiguous sequence of characters that are valid in an identifier. +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct Identifier { /// Is the span that makes up the token. @@ -142,6 +146,7 @@ impl SourceElement for Identifier { } /// Represents a contiguous sequence of characters that are reserved for a keyword. +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct Keyword { /// Is the span that makes up the token. @@ -158,6 +163,7 @@ impl SourceElement for Keyword { } /// Represents a single ASCII punctuation character. +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct Punctuation { /// Is the span that makes up the token. @@ -174,6 +180,7 @@ impl SourceElement for Punctuation { } /// Represents a hardcoded numeric literal value in the source code. +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct Numeric { /// Is the span that makes up the token. @@ -187,6 +194,7 @@ impl SourceElement for Numeric { } /// Represents a hardcoded string literal value in the source code. +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct StringLiteral { /// Is the span that makes up the token. @@ -209,6 +217,7 @@ impl SourceElement for StringLiteral { } /// Is an enumeration representing the two kinds of comments in the Shulkerscript programming language. +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum CommentKind { /// A comment that starts with `//` and ends at the end of the line. @@ -219,6 +228,7 @@ pub enum CommentKind { } /// Represents a portion of the source code that is ignored by the interpreter. +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct Comment { /// Is the span that makes up the token. @@ -235,6 +245,7 @@ impl SourceElement for Comment { } /// Represents a documentation comment in the source code. +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct DocComment { /// Is the span that makes up the token. @@ -256,6 +267,7 @@ impl DocComment { } /// Represents a hardcoded literal command in the source code. +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct CommandLiteral { /// Span that makes up the token. diff --git a/src/lexical/token_stream.rs b/src/lexical/token_stream.rs index b001b55..e8d99f5 100644 --- a/src/lexical/token_stream.rs +++ b/src/lexical/token_stream.rs @@ -15,6 +15,7 @@ use super::{ /// /// This struct is the final output of the lexical analysis phase and is meant to be used by the /// next stage of the compilation process. +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Deref)] pub struct TokenStream { #[deref] @@ -162,6 +163,7 @@ impl TokenStream { } /// Is an enumeration of either a [`Token`] or a [`Delimited`]. +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, From)] #[allow(missing_docs)] pub enum TokenTree { @@ -170,8 +172,8 @@ pub enum TokenTree { } /// Is an enumeration of the different types of delimiters in the [`Delimited`]. +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] -#[allow(missing_docs)] pub enum Delimiter { /// () Parenthesis, @@ -182,6 +184,7 @@ pub enum Delimiter { } /// Represents a list of tokens enclosed by a pair of delimiters. +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct Delimited { /// The opening delimiter. diff --git a/src/syntax/syntax_tree/condition.rs b/src/syntax/syntax_tree/condition.rs index 4aa40ba..0f4c035 100644 --- a/src/syntax/syntax_tree/condition.rs +++ b/src/syntax/syntax_tree/condition.rs @@ -29,6 +29,7 @@ use crate::{ /// | StringLiteral /// ``` #[allow(missing_docs)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, EnumAsInner)] pub enum PrimaryCondition { Prefix(ConditionalPrefix), @@ -53,6 +54,7 @@ impl SourceElement for PrimaryCondition { /// Condition ConditionalBinaryOperator Condition /// ; /// ``` +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)] pub struct BinaryCondition { /// The left operand of the binary condition. @@ -91,8 +93,9 @@ impl BinaryCondition { /// | '||' /// ; /// ``` -#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, EnumAsInner)] #[allow(missing_docs)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, EnumAsInner)] pub enum ConditionalBinaryOperator { LogicalAnd(Punctuation, Punctuation), LogicalOr(Punctuation, Punctuation), @@ -128,6 +131,7 @@ impl SourceElement for ConditionalBinaryOperator { /// ParenthesizedCondition: /// '(' Condition ')'; /// ``` +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)] pub struct ParenthesizedCondition { /// The opening parenthesis. @@ -164,6 +168,7 @@ impl SourceElement for ParenthesizedCondition { /// PrefixOperator: '!'; /// ``` #[allow(missing_docs)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, EnumAsInner)] pub enum ConditionalPrefixOperator { LogicalNot(Punctuation), @@ -184,6 +189,7 @@ impl SourceElement for ConditionalPrefixOperator { /// ConditionalPrefixOperator StringLiteral /// ; /// ``` +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)] pub struct ConditionalPrefix { /// The operator of the prefix. @@ -213,6 +219,7 @@ impl ConditionalPrefix { /// Condition: PrimaryCondition; /// ``` #[allow(missing_docs)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, EnumAsInner)] pub enum Condition { Primary(PrimaryCondition), diff --git a/src/syntax/syntax_tree/declaration.rs b/src/syntax/syntax_tree/declaration.rs index c3bba94..fc48aa7 100644 --- a/src/syntax/syntax_tree/declaration.rs +++ b/src/syntax/syntax_tree/declaration.rs @@ -21,6 +21,7 @@ use crate::{ use super::{statement::Block, ConnectedList}; +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum Declaration { Function(Function), @@ -40,6 +41,7 @@ impl SourceElement for Declaration { /// '#[' Identifier ('=' StringLiteral)? ']' /// ; /// ``` +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)] pub struct Annotation { #[get = "pub"] @@ -95,6 +97,7 @@ impl SourceElement for Annotation { /// Identifier (',' Identifier)* ','? /// ; /// ``` +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)] pub struct Function { #[get = "pub"] diff --git a/src/syntax/syntax_tree/expression.rs b/src/syntax/syntax_tree/expression.rs index 1f7ac74..59c8738 100644 --- a/src/syntax/syntax_tree/expression.rs +++ b/src/syntax/syntax_tree/expression.rs @@ -26,8 +26,9 @@ use super::ConnectedList; /// Expression: /// Primary /// ``` -#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, EnumAsInner)] #[allow(missing_docs)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, EnumAsInner)] pub enum Expression { Primary(Primary), } @@ -46,8 +47,9 @@ impl SourceElement for Expression { /// Primary: /// FunctionCall /// ``` -#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, EnumAsInner)] #[allow(missing_docs)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, EnumAsInner)] pub enum Primary { FunctionCall(FunctionCall), } @@ -67,6 +69,7 @@ impl SourceElement for Primary { /// Identifier '(' (Expression (',' Expression)*)? ')' /// ; /// ``` +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)] pub struct FunctionCall { /// The identifier of the function. diff --git a/src/syntax/syntax_tree/mod.rs b/src/syntax/syntax_tree/mod.rs index 31969ef..11f467d 100644 --- a/src/syntax/syntax_tree/mod.rs +++ b/src/syntax/syntax_tree/mod.rs @@ -27,6 +27,7 @@ pub mod statement; /// This struct is useful for representing syntax tree nodes that are separated by a separator. /// For example, a comma separated list of expressions such as `1, 2, 3` can be represented by a /// [`ConnectedList`] with the separator being a comma token and the elements being the expressions. +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)] pub struct ConnectedList { /// The first element of the list. diff --git a/src/syntax/syntax_tree/program.rs b/src/syntax/syntax_tree/program.rs index 7195845..34082ec 100644 --- a/src/syntax/syntax_tree/program.rs +++ b/src/syntax/syntax_tree/program.rs @@ -13,6 +13,7 @@ use crate::{ use super::declaration::Declaration; /// Program is a collection of declarations. +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)] pub struct Program { /// The declarations within the program. diff --git a/src/syntax/syntax_tree/statement.rs b/src/syntax/syntax_tree/statement.rs index 18b308b..21ac523 100644 --- a/src/syntax/syntax_tree/statement.rs +++ b/src/syntax/syntax_tree/statement.rs @@ -31,6 +31,7 @@ use super::{condition::ParenthesizedCondition, expression::Expression}; /// ; /// ``` #[allow(missing_docs)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum Statement { Block(Block), @@ -61,6 +62,7 @@ impl SourceElement for Statement { /// '{' Statement* '}' /// ; /// ``` +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)] pub struct Block { /// The opening brace of the block. @@ -98,6 +100,7 @@ impl SourceElement for Block { /// 'if' ParenthizedCondition Block ('else' Block)? /// ; /// ```` +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)] pub struct Conditional { /// The `if` keyword. @@ -148,6 +151,7 @@ impl SourceElement for Conditional { /// 'else' Block /// ; /// ``` +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)] pub struct Else { /// The `else` keyword. @@ -179,6 +183,7 @@ impl SourceElement for Else { /// 'group' Block /// ; /// ```` +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)] pub struct Grouping { /// The `group` keyword. @@ -212,6 +217,7 @@ impl SourceElement for Grouping { /// Expression ';' /// ; /// ``` +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)] pub struct Semicolon { /// The expression of the semicolon statement.