external files can now be includes via @include

This commit is contained in:
Sven Vogel 2023-09-14 12:52:39 +02:00
parent 4c335ec850
commit 2cec9f1df4
4 changed files with 28 additions and 16 deletions

View File

@ -1,6 +1,6 @@
use std::{collections::VecDeque}; use std::{collections::VecDeque};
use crate::{token::{Token}, builtin::modules::Module}; use crate::{token::{Token, MessageType}, builtin::modules::Module};
#[derive(Default)] #[derive(Default)]
pub struct LangSpecs { pub struct LangSpecs {
@ -8,7 +8,7 @@ pub struct LangSpecs {
builtin_features: Vec<crate::builtin::modules::Module>, builtin_features: Vec<crate::builtin::modules::Module>,
lang_version: u32, lang_version: u32,
authors: Vec<String>, authors: Vec<String>,
embedded_files: Vec<String>, embedded_files: Vec<(usize, String)>,
} }
impl LangSpecs { impl LangSpecs {
@ -16,24 +16,22 @@ impl LangSpecs {
pub fn features(&self) -> &[crate::builtin::modules::Module] { pub fn features(&self) -> &[crate::builtin::modules::Module] {
&self.builtin_features &self.builtin_features
} }
pub fn embedded_files(&self) -> &[(usize, String)] {
&self.embedded_files
}
} }
pub fn resolve_directives(tokens: &mut VecDeque<Token>) -> LangSpecs { pub fn resolve_directives(tokens: &mut VecDeque<Token>) -> LangSpecs {
let mut specs = LangSpecs::default(); let mut specs = LangSpecs::default();
for token in tokens.iter() { for (idx, token) in tokens.iter().enumerate() {
match token { match token {
Token::CompilerDirective(text, _) => parse_directive(text, &mut specs), Token::CompilerDirective(text, _) => parse_directive(text, idx, &mut specs),
_ => () _ => ()
} }
} }
// remove compiler directives from source
tokens.retain(|token| match token {
Token::CompilerDirective(_, _) => false,
_ => true
});
specs specs
} }
@ -41,7 +39,7 @@ static DIRECTIVE_REGEX_SRC: &'static str = concat!(
r"@feature\(((?:\s*[\w]+\s*,?)*)\)", r"@feature\(((?:\s*[\w]+\s*,?)*)\)",
r"|@version\(\s*([0-9]{3})\s*\)", r"|@version\(\s*([0-9]{3})\s*\)",
r"|@author\((.*)\)", r"|@author\((.*)\)",
r"|@embed\((.*)\)" r"|@include\((.*)\)"
); );
lazy_static::lazy_static! { lazy_static::lazy_static! {
@ -58,7 +56,7 @@ pub fn from_list(text: &str) -> Vec<String> {
vec vec
} }
fn parse_directive(text: &str, specs: &mut LangSpecs) { fn parse_directive(text: &str, token_idx: usize, specs: &mut LangSpecs) {
for cap in DIRECTIVE_REGEX.captures_iter(text) { for cap in DIRECTIVE_REGEX.captures_iter(text) {
let mut enumerator = cap.iter().enumerate(); let mut enumerator = cap.iter().enumerate();
@ -91,8 +89,13 @@ fn parse_directive(text: &str, specs: &mut LangSpecs) {
return; return;
}, },
4 => { 4 => {
specs.embedded_files.append(&mut from_list(mat.as_str())); for path in from_list(mat.as_str()).iter() {
crate::message(crate::token::MessageType::Warning, "Embed directive not working at current state"); if let Ok(str) = std::fs::read_to_string(path) {
specs.embedded_files.push((token_idx, str));
} else {
crate::message(MessageType::Warning, format!("Unable to read embedded file: {path}"));
}
}
return; return;
}, },
_ => crate::message(crate::token::MessageType::Warning, format!("unknown directive: `{}`", text)), _ => crate::message(crate::token::MessageType::Warning, format!("unknown directive: `{}`", text)),

View File

@ -36,6 +36,15 @@ fn compile(settings: &Settings) -> Option<(Vec<Func>, Vec<Declr>, Vec<BuiltinFun
if let Ok(mut tokens) = tokenize(code, &mut diagnostics) { if let Ok(mut tokens) = tokenize(code, &mut diagnostics) {
let specs = crate::direct::resolve_directives(&mut tokens); let specs = crate::direct::resolve_directives(&mut tokens);
// read source of every embedded file and tokenize
for (idx, src) in specs.embedded_files() {
if let Ok(em_tokens) = tokenize(src, &mut diagnostics) {
for (idy, em_token) in em_tokens.into_iter().enumerate() {
tokens.insert(idx + idy, em_token.to_owned());
}
}
}
let mut parser = Parser::new(&specs); let mut parser = Parser::new(&specs);
if let Ok((funcs, declrs, builtin)) = parser.parse(&mut tokens, &mut diagnostics, &settings) { if let Ok((funcs, declrs, builtin)) = parser.parse(&mut tokens, &mut diagnostics, &settings) {

View File

@ -285,7 +285,7 @@ fn discover_functions(
// if we have anything left it might be an error // if we have anything left it might be an error
match &top { match &top {
Token::LineBreak(_) | Token::Terminator(_) => (), // valid whitespace Token::LineBreak(_) | Token::Terminator(_) | Token::CompilerDirective(_,_) => (), // valid whitespace
_ => { _ => {
diagnostics.set_err(&top, crate::msg::ERR22, ""); diagnostics.set_err(&top, crate::msg::ERR22, "");
return Err(()) ; return Err(()) ;

View File

@ -472,7 +472,7 @@ pub struct DebugInfo {
/// index in source string where the token ends in the current line /// index in source string where the token ends in the current line
pub end: usize, pub end: usize,
/// line number where the line in which the token is begins /// line number where the line in which the token is begins
pub line: usize, pub line: usize
} }
#[derive(Debug)] #[derive(Debug)]