Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

init: serde feature #66

Merged
merged 4 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 115 additions & 42 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" ]
6 changes: 6 additions & 0 deletions src/compiling/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions src/compiling/failing/failure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -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),
Expand Down
5 changes: 4 additions & 1 deletion src/compiling/failing/logger.rs
Original file line number Diff line number Diff line change
@@ -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<PositionInfo>
Expand Down
6 changes: 5 additions & 1 deletion src/compiling/failing/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions src/compiling/failing/position_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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<String>,
Expand Down
5 changes: 5 additions & 0 deletions src/compiling/lexing/compound_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ 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,
End,
Pass
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct CompoundHandler {
compound_tree: HashMap<char, Vec<char>>,
is_triggered: bool
Expand Down
5 changes: 5 additions & 0 deletions src/compiling/lexing/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions src/compiling/lexing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading
Loading