devalang_core/core/lexer/
mod.rs1pub 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 Default for Lexer {
14 fn default() -> Self {
15 Self::new()
16 }
17}
18
19impl Lexer {
20 pub fn new() -> Self {
21 Lexer {}
22 }
23
24 pub fn lex_from_source(&self, source: &str) -> Result<Vec<Token>, String> {
25 handle_content_lexing(source.to_string())
26 }
27
28 pub fn lex_tokens(&self, entrypoint: &str) -> Result<Vec<Token>, String> {
29 let path = normalize_path(entrypoint);
30 let resolved_path = Self::resolve_entry_path(&path)?;
31
32 let file_content = fs::read_to_string(&resolved_path).map_err(|e| {
33 format!(
34 "Failed to read the entrypoint file '{}': {}",
35 resolved_path, e
36 )
37 })?;
38
39 handle_content_lexing(file_content).map_err(|e| format!("Failed to lex the content: {}", e))
40 }
41
42 fn resolve_entry_path(path: &str) -> Result<String, String> {
43 let candidate = Path::new(path);
44
45 if candidate.is_dir() {
46 let index_path = candidate.join("index.deva");
47 if index_path.exists() {
48 Ok(index_path.to_string_lossy().replace("\\", "/"))
49 } else {
50 Err(format!(
51 "Expected 'index.deva' in directory '{}', but it was not found",
52 path
53 ))
54 }
55 } else if candidate.is_file() {
56 return Ok(path.to_string());
57 } else {
58 return Err(format!(
59 "Provided entrypoint '{}' is not a valid file or directory",
60 path
61 ));
62 }
63 }
64}