From 7acaafaa2f06669919d3fbdd3606a1c72dd9769d Mon Sep 17 00:00:00 2001 From: Mark <> Date: Thu, 27 Jun 2024 18:11:37 +0200 Subject: [PATCH] fix some bugs --- mers/Cargo.toml | 4 ++-- mers_lib/Cargo.toml | 4 ++-- mers_lib/src/parsing/mod.rs | 9 +++++++++ mers_lib/src/parsing/statements.rs | 8 ++++++++ mers_lib/src/pretty_print.rs | 2 ++ 5 files changed, 23 insertions(+), 4 deletions(-) diff --git a/mers/Cargo.toml b/mers/Cargo.toml index 83945ef..2dddd58 100644 --- a/mers/Cargo.toml +++ b/mers/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mers" -version = "0.8.20" +version = "0.8.25" edition = "2021" license = "MIT OR Apache-2.0" description = "dynamically typed but type-checked programming language" @@ -15,7 +15,7 @@ default = ["colored-output"] colored-output = ["mers_lib/ecolor-term", "mers_lib/pretty-print", "dep:colored"] [dependencies] -mers_lib = "0.8.20" +mers_lib = "0.8.25" # mers_lib = { path = "../mers_lib" } clap = { version = "4.3.19", features = ["derive"] } colored = { version = "2.1.0", optional = true } diff --git a/mers_lib/Cargo.toml b/mers_lib/Cargo.toml index dfe44dc..01fb1b6 100755 --- a/mers_lib/Cargo.toml +++ b/mers_lib/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mers_lib" -version = "0.8.24" +version = "0.8.25" edition = "2021" license = "MIT OR Apache-2.0" description = "library to use the mers language in other projects" @@ -9,7 +9,7 @@ readme = "README.md" repository = "https://github.com/Dummi26/mers" [features] -default = ["parse", "pretty-print", "ecolor-html"] +default = ["parse"] # for parsing and running mers code (for most situations: just enable parse) parse = ["run"] diff --git a/mers_lib/src/parsing/mod.rs b/mers_lib/src/parsing/mod.rs index 6805be8..539a8e9 100755 --- a/mers_lib/src/parsing/mod.rs +++ b/mers_lib/src/parsing/mod.rs @@ -112,6 +112,7 @@ pub struct Source { comments: Vec<(usize, String)>, i: usize, sections: Vec, + allow_includes: bool, } impl Clone for Source { fn clone(&self) -> Self { @@ -123,6 +124,7 @@ impl Clone for Source { comments: self.comments.clone(), i: self.i, sections: vec![], + allow_includes: self.allow_includes, } } } @@ -150,12 +152,14 @@ impl Source { comments: vec![], i: 0, sections: vec![], + allow_includes: false, } } pub fn new_from_string(source: String) -> Self { Self::new(SourceFrom::Unspecified, source) } pub fn new(src_from: SourceFrom, source: String) -> Self { + let allow_includes = matches!(src_from, SourceFrom::File(_)); let mut src = String::with_capacity(source.len()); let mut comment = (0, String::new()); let mut comments = Vec::new(); @@ -222,8 +226,13 @@ impl Source { comments, i: 0, sections: vec![], + allow_includes, } } + pub fn allow_includes(mut self, allow_includes: bool) -> Self { + self.allow_includes = allow_includes; + self + } pub fn src(&self) -> &String { &self.src diff --git a/mers_lib/src/parsing/statements.rs b/mers_lib/src/parsing/statements.rs index 3c57b5c..bca8a10 100755 --- a/mers_lib/src/parsing/statements.rs +++ b/mers_lib/src/parsing/statements.rs @@ -281,6 +281,14 @@ pub fn parse_no_chain( } match src.next_word() { "include" => { + if !src.allow_includes { + return Err(CheckError::new() + .src(vec![( + (pos_in_src, src.get_pos(), srca).into(), + Some(EColor::HashIncludeCantLoadFile), + )]) + .msg_str(format!("not allowed to use #include (only allowed when source code is read from a file, or if allow_includes is explicitly set)"))); + } let end_in_src = src.get_pos(); src.skip_whitespace(); let string_in_src = src.get_pos(); diff --git a/mers_lib/src/pretty_print.rs b/mers_lib/src/pretty_print.rs index 50e62ed..96199e2 100644 --- a/mers_lib/src/pretty_print.rs +++ b/mers_lib/src/pretty_print.rs @@ -75,6 +75,7 @@ impl ThemeGen for DefaultTheme { type T = std::io::Stdout; fn color(&self, text: &str, color: Self::C, t: &mut Self::T) { use colored::{Color, Colorize}; + use std::io::Write; if let Some(color) = map_color(color) { let _ = write!( t, @@ -92,6 +93,7 @@ impl ThemeGen for DefaultTheme { } } fn nocolor(&self, text: &str, t: &mut Self::T) { + use std::io::Write; let _ = write!(t, "{}", text); } }