diff --git a/Cargo.lock b/Cargo.lock index c77ab98..bf906f1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,17 +2,6 @@ # It is not intended for manual editing. version = 3 -[[package]] -name = "atty" -version = "0.2.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" -dependencies = [ - "hermit-abi", - "libc", - "winapi", -] - [[package]] name = "capitalize" version = "0.3.4" @@ -21,13 +10,12 @@ checksum = "6b5271031022835ee8c7582fe67403bd6cb3d962095787af7921027234bab5bf" [[package]] name = "colored" -version = "2.0.0" +version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b3616f750b84d8f0de8a58bda93e08e2a81ad3f523089b05f1dffecab48c6cbd" +checksum = "cbf2150cce219b664a8a70df7a1f933836724b503f8a413af9365b4dcc4d90b8" dependencies = [ - "atty", "lazy_static", - "winapi", + "windows-sys", ] [[package]] @@ -37,62 +25,147 @@ dependencies = [ "capitalize", "colored", "pad", + "serde", ] [[package]] -name = "hermit-abi" -version = "0.1.19" +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + +[[package]] +name = "pad" +version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +checksum = "d2ad9b889f1b12e0b9ee24db044b5129150d5eada288edc800f789928dc8c0e3" dependencies = [ - "libc", + "unicode-width", ] [[package]] -name = "lazy_static" -version = "1.4.0" +name = "proc-macro2" +version = "1.0.87" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" +checksum = "b3e4daa0dcf6feba26f985457cdf104d4b4256fc5a09547140f3631bb076b19a" +dependencies = [ + "unicode-ident", +] [[package]] -name = "libc" -version = "0.2.126" +name = "quote" +version = "1.0.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836" +checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" +dependencies = [ + "proc-macro2", +] [[package]] -name = "pad" -version = "0.1.6" +name = "serde" +version = "1.0.210" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d2ad9b889f1b12e0b9ee24db044b5129150d5eada288edc800f789928dc8c0e3" +checksum = "c8e3592472072e6e22e0a54d5904d9febf8508f65fb8552499a1abc7d1078c3a" dependencies = [ - "unicode-width", + "serde_derive", ] +[[package]] +name = "serde_derive" +version = "1.0.210" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "243902eda00fad750862fc144cea25caca5e20d615af0a81bee94ca738f1df1f" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "syn" +version = "2.0.79" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89132cd0bf050864e1d38dc3bbc07a0eb8e7530af26344d3d2bbbef83499f590" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" + [[package]] name = "unicode-width" -version = "0.1.9" +version = "0.1.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973" +checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" [[package]] -name = "winapi" -version = "0.3.9" +name = "windows-sys" +version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" dependencies = [ - "winapi-i686-pc-windows-gnu", - "winapi-x86_64-pc-windows-gnu", + "windows-targets", ] [[package]] -name = "winapi-i686-pc-windows-gnu" -version = "0.4.0" +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" [[package]] -name = "winapi-x86_64-pc-windows-gnu" -version = "0.4.0" +name = "windows_x86_64_msvc" +version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" diff --git a/Cargo.toml b/Cargo.toml index 0f8cc85..c167f09 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,3 +14,7 @@ keywords = ["heraclitus", "compiler", "parser"] colored = "2.0.0" pad = "0.1.6" capitalize = "0.3.4" +serde = { version = "1.0.210", default-features = false, optional = true, features = [ "derive" ] } + +[features] +serde = [ "dep:serde" ] diff --git a/src/compiling/compiler.rs b/src/compiling/compiler.rs index f531156..806d9dd 100644 --- a/src/compiling/compiler.rs +++ b/src/compiling/compiler.rs @@ -9,10 +9,14 @@ use crate::error_pos; use super::lexer::Lexer; +#[cfg(feature = "serde")] +use serde::{Serialize, Deserialize}; + /// How do you want to separate expressions? /// /// Separator mode determines how do you want to handle separators (in many languages the semicolon) #[derive(Debug, Clone, PartialEq, Eq)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum SeparatorMode { /// Manual separators require user to manually write them all Manual, @@ -32,6 +36,7 @@ pub enum SeparatorMode { /// For instance do you want to use blocks like `{ ... }` or `if ... fi` /// or do you want to use intents like in languages such as Python or Yaml. #[derive(Debug, Clone, PartialEq, Eq)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum ScopingMode { /// Scopes are going to be contained between two specified tokens Block, @@ -62,6 +67,7 @@ pub enum ScopingMode { /// # } /// ``` #[derive(Debug, Clone, PartialEq)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Compiler { /// Name of your language pub name: String, diff --git a/src/compiling/failing/failure.rs b/src/compiling/failing/failure.rs index 383842b..9a9159d 100644 --- a/src/compiling/failing/failure.rs +++ b/src/compiling/failing/failure.rs @@ -3,6 +3,9 @@ use crate::compiling::failing::message::Message; use crate::compiling::failing::position_info::PositionInfo; +#[cfg(feature = "serde")] +use serde::{Serialize, Deserialize}; + /// Failure enum /// /// This enum returns two types of errors - `Quiet` and `Loud`. @@ -15,6 +18,7 @@ use crate::compiling::failing::position_info::PositionInfo; /// The Loud failure is used when the parser cannot continue. It contains detailed information /// about the error such as token position and length, but also a message, comment and a full traceback. #[derive(Debug, Clone)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum Failure { /// Failure that is not important Quiet(PositionInfo), diff --git a/src/compiling/failing/logger.rs b/src/compiling/failing/logger.rs index 4faf1e0..324724f 100644 --- a/src/compiling/failing/logger.rs +++ b/src/compiling/failing/logger.rs @@ -1,15 +1,18 @@ //! This is a logger module which is used by compiler to log errors, warnings and info messages -#![allow(dead_code)] use colored::{Colorize, Color}; use pad::PadStr; use crate::compiling::failing::position_info::PositionInfo; use crate::compiling::failing::message::MessageType; use crate::prelude::Position; +#[cfg(feature = "serde")] +use serde::{Serialize, Deserialize}; + /// This is a logger that is used to log messages to the user /// The logger is being used internally by the Message struct /// when invoking the `show` method +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Logger { kind: MessageType, trace: Vec diff --git a/src/compiling/failing/message.rs b/src/compiling/failing/message.rs index b64a332..bd0a5f5 100644 --- a/src/compiling/failing/message.rs +++ b/src/compiling/failing/message.rs @@ -5,13 +5,16 @@ //! However, if you need more specific functionality - it is encouraged to create your //! own implementation of such mechanism. -#![allow(dead_code)] use crate::compiling::{Metadata, Token}; use crate::compiling::failing::logger::Logger; use crate::compiling::failing::position_info::PositionInfo; +#[cfg(feature = "serde")] +use serde::{Serialize, Deserialize}; + /// Type of the message that logger shall display #[derive(Clone, Debug)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum MessageType { /// Error message Error, @@ -38,6 +41,7 @@ pub enum MessageType { /// .show(); /// ``` #[derive(Debug, Clone)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Message { /// Type of the message pub kind: MessageType, diff --git a/src/compiling/failing/position_info.rs b/src/compiling/failing/position_info.rs index 13ed4fe..b1135dc 100644 --- a/src/compiling/failing/position_info.rs +++ b/src/compiling/failing/position_info.rs @@ -7,8 +7,12 @@ use std::fs::File; use std::io::*; use crate::compiling::{Metadata, Token}; +#[cfg(feature = "serde")] +use serde::{Serialize, Deserialize}; + /// Store position of some error #[derive(Debug, Clone)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum Position { /// Explicit row and column Pos(usize, usize), @@ -18,6 +22,7 @@ pub enum Position { /// Struct that is used to return a simple error #[derive(Debug, Clone)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct PositionInfo { /// Path of the file pub path: Option, diff --git a/src/compiling/lexing/compound_handler.rs b/src/compiling/lexing/compound_handler.rs index bf57019..45d3134 100644 --- a/src/compiling/lexing/compound_handler.rs +++ b/src/compiling/lexing/compound_handler.rs @@ -2,7 +2,11 @@ use std::collections::HashMap; use crate::compiling_rules::Rules; use super::reader::Reader; +#[cfg(feature = "serde")] +use serde::{Serialize, Deserialize}; + #[derive(Debug, PartialEq, Eq)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum CompoundReaction { Begin, Keep, @@ -10,6 +14,7 @@ pub enum CompoundReaction { Pass } +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct CompoundHandler { compound_tree: HashMap>, is_triggered: bool diff --git a/src/compiling/lexing/lexer.rs b/src/compiling/lexing/lexer.rs index 14ffea0..8836ef9 100644 --- a/src/compiling/lexing/lexer.rs +++ b/src/compiling/lexing/lexer.rs @@ -14,8 +14,12 @@ use super::{ LexerError, LexerErrorType, }; +#[cfg(feature = "serde")] +use serde::{Serialize, Deserialize}; + /// Lexer #[derive(Debug, Clone, PartialEq)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Lexer { rules: Rules, /// Path to the lexed file @@ -28,6 +32,7 @@ pub struct Lexer { pub scoping_mode: ScopingMode, } +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] struct LexState<'a> { word: String, is_indenting: bool, diff --git a/src/compiling/lexing/mod.rs b/src/compiling/lexing/mod.rs index 704573b..b60c915 100644 --- a/src/compiling/lexing/mod.rs +++ b/src/compiling/lexing/mod.rs @@ -9,8 +9,12 @@ pub mod lexer; mod reader; mod region_handler; +#[cfg(feature = "serde")] +use serde::{Serialize, Deserialize}; + /// Lexer's error type #[derive(Debug)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum LexerErrorType { /// Unspillable region has been spilled Singleline, diff --git a/src/compiling/lexing/reader.rs b/src/compiling/lexing/reader.rs index c87ac4d..438fae9 100644 --- a/src/compiling/lexing/reader.rs +++ b/src/compiling/lexing/reader.rs @@ -1,11 +1,15 @@ -#![allow(dead_code)] +#[cfg(feature = "serde")] +use serde::{Serialize, Deserialize}; + const BEGINNING: (usize, usize) = (0, 1); +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum ReadMode { History, Future } +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Reader<'a> { pub code: &'a String, pub row: usize, diff --git a/src/compiling/lexing/region_handler.rs b/src/compiling/lexing/region_handler.rs index 591aa06..831e878 100644 --- a/src/compiling/lexing/region_handler.rs +++ b/src/compiling/lexing/region_handler.rs @@ -2,13 +2,18 @@ use crate::compiling_rules::{Region, Rules, RegionMap}; use super::reader::Reader; use super::reader::ReadMode; +#[cfg(feature = "serde")] +use serde::{Serialize, Deserialize}; + #[derive(PartialEq, Eq)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum RegionReaction { Begin(bool), End(bool), Pass } +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct RegionHandler { region_stack: Vec, region_map: RegionMap diff --git a/src/compiling/parser/metadata.rs b/src/compiling/parser/metadata.rs index 64b95b4..674af98 100644 --- a/src/compiling/parser/metadata.rs +++ b/src/compiling/parser/metadata.rs @@ -1,8 +1,12 @@ use crate::compiling::Token; use crate::compiling::failing::position_info::PositionInfo; +#[cfg(feature = "serde")] +use serde::{Serialize, Deserialize}; + /// Default implementation of metadata. /// This is useful for debuging or languages that are not too demanding. +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct DefaultMetadata { /// Current index in the token stream pub index: usize, diff --git a/src/compiling/token.rs b/src/compiling/token.rs index df74162..8242482 100644 --- a/src/compiling/token.rs +++ b/src/compiling/token.rs @@ -1,7 +1,11 @@ use std::fmt::{Formatter, Display, Result, Debug}; +#[cfg(feature = "serde")] +use serde::{Serialize, Deserialize}; + /// The building block of the AST #[derive(Clone, PartialEq, Eq, Default)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Token { /// Value of the token pub word: String, diff --git a/src/compiling_rules/region.rs b/src/compiling_rules/region.rs index 66cf259..34aea54 100644 --- a/src/compiling_rules/region.rs +++ b/src/compiling_rules/region.rs @@ -1,5 +1,8 @@ use std::collections::HashMap; +#[cfg(feature = "serde")] +use serde::{Serialize, Deserialize}; + /// This is a type of a map that is generated by `generate_region_map` method of region's pub type RegionMap = HashMap; @@ -110,6 +113,7 @@ macro_rules! reg { /// - `allow_left_open` /// - `singleline` #[derive(Debug, PartialEq, Clone)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Region { /// identifier that will be used to reference this region in an interpolation pub id: String, diff --git a/src/compiling_rules/rules.rs b/src/compiling_rules/rules.rs index fd9f412..41bd845 100644 --- a/src/compiling_rules/rules.rs +++ b/src/compiling_rules/rules.rs @@ -1,5 +1,8 @@ use super::region::Region; +#[cfg(feature = "serde")] +use serde::{Serialize, Deserialize}; + /// Determine lexing rules for the parser /// /// Rules struct that contains list of symbols as well as region tree @@ -25,6 +28,7 @@ use super::region::Region; /// ``` #[derive(Debug, Clone, PartialEq)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Rules { /// Symbols that should be separated (most commonly: (, ), +, -, ...) /// This handles situations like for instance if we want to parse `1+1` as