devalang_core/core/lexer/
mod.rs

1pub mod handler;
2pub mod token;
3
4use crate::core::{
5    lexer::{handler::driver::handle_content_lexing, token::Token},
6    utils::path::normalize_path,
7};
8use std::fs;
9use std::path::Path;
10
11pub struct Lexer {}
12
13impl Lexer {
14    pub fn new() -> Self {
15        Lexer {}
16    }
17
18    pub fn lex_from_source(&self, source: &str) -> Result<Vec<Token>, String> {
19        handle_content_lexing(source.to_string())
20    }
21
22    pub fn lex_tokens(&self, entrypoint: &str) -> Vec<Token> {
23        let path = normalize_path(entrypoint);
24        let resolved_path = Self::resolve_entry_path(&path);
25
26        let file_content =
27            fs::read_to_string(&resolved_path).expect("Failed to read the entrypoint file");
28
29        handle_content_lexing(file_content).expect("Failed to lex the content")
30    }
31
32    fn resolve_entry_path(path: &str) -> String {
33        let candidate = Path::new(path);
34
35        if candidate.is_dir() {
36            let index_path = candidate.join("index.deva");
37            if index_path.exists() {
38                return index_path.to_string_lossy().replace("\\", "/");
39            } else {
40                panic!(
41                    "Expected 'index.deva' in directory '{}', but it was not found",
42                    path
43                );
44            }
45        } else if candidate.is_file() {
46            return path.to_string();
47        } else {
48            panic!(
49                "Provided entrypoint '{}' is not a valid file or directory",
50                path
51            );
52        }
53    }
54}