fix some bugs

This commit is contained in:
Mark
2024-06-27 18:11:37 +02:00
parent d01da83866
commit 7acaafaa2f
5 changed files with 23 additions and 4 deletions

View File

@@ -112,6 +112,7 @@ pub struct Source {
comments: Vec<(usize, String)>,
i: usize,
sections: Vec<SectionMarker>,
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

View File

@@ -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();

View File

@@ -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);
}
}