Add serde dependency and derive serialization and deserialization for relevant structs

This commit is contained in:
Moritz Hölting 2024-04-06 14:40:44 +02:00
parent 34076f9842
commit 3c3c9e5b24
10 changed files with 44 additions and 4 deletions

View File

@ -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"

View File

@ -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.

View File

@ -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.

View File

@ -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.

View File

@ -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),

View File

@ -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"]

View File

@ -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.

View File

@ -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<Element, Separator> {
/// The first element of the list.

View File

@ -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.

View File

@ -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.