Skip to content

Commit

Permalink
introduce Source that includes the file unit and lines map
Browse files Browse the repository at this point in the history
  • Loading branch information
vincenthz committed Jan 26, 2024
1 parent 3f571e6 commit 233650c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
16 changes: 16 additions & 0 deletions werbolg-lang-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@ pub use filemap::{Line, LineCol, LinesMap};
pub use fileunit::FileUnit;
pub use report::{Report, ReportKind};

pub struct Source {
pub file_unit: FileUnit,
pub lines_map: LinesMap,
}

impl Source {
pub fn from_string(filename: String, content: String) -> Self {
let file_unit = FileUnit::from_string(filename, content);
let lines_map = LinesMap::new(&file_unit.content);
Self {
file_unit,
lines_map,
}
}
}

#[derive(Debug, Clone)]
pub struct ParseError {
pub context: Option<ir::Span>,
Expand Down
17 changes: 6 additions & 11 deletions werbolg-lang-common/src/report.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::filemap::Line;

use super::filemap::LinesMap;
use super::fileunit::FileUnit;
use super::span::Span;
use super::Source;

use alloc::format;
use alloc::string::String;
Expand Down Expand Up @@ -127,12 +127,7 @@ impl Report {
Ok(file_map.lines_iterator(context_start_line, context_end_line))
}

pub fn write<W: Write>(
self,
file_unit: &FileUnit,
file_map: &LinesMap,
writer: &mut W,
) -> Result<(), core::fmt::Error> {
pub fn write<W: Write>(self, source: &Source, writer: &mut W) -> Result<(), core::fmt::Error> {
// write the first line
let code_format = if let Some(code) = &self.code {
format!("[{}] ", code)
Expand All @@ -146,7 +141,7 @@ impl Report {
};
writeln!(writer, "{}{}: {}", code_format, hd, self.header)?;

let context_lines = match self.context_lines(file_map) {
let context_lines = match self.context_lines(&source.lines_map) {
Ok(o) => o,
Err(None) => return Ok(()),
Err(Some(_)) => return Ok(()),
Expand All @@ -155,7 +150,7 @@ impl Report {
let Some(highlight) = self.highlight else {
unreachable!();
};
let (start_highlight, end_highlight) = file_map.resolve_span(&highlight.0).unwrap();
let (start_highlight, end_highlight) = source.lines_map.resolve_span(&highlight.0).unwrap();
let multiline = !(start_highlight.line() == end_highlight.line());

writeln!(
Expand All @@ -164,11 +159,11 @@ impl Report {
line_format(None),
BOXING[TL],
BOXING[H],
file_unit.filename
source.file_unit.filename
)?;

for line in context_lines {
let line_text = file_map.get_line_trim(file_unit, line);
let line_text = source.lines_map.get_line_trim(&source.file_unit, line);
writeln!(
writer,
"{} {} {}",
Expand Down
21 changes: 5 additions & 16 deletions werbolg-tales/src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@ use hashbrown::HashSet;
use werbolg_compile::{code_dump, compile, Environment, InstructionAddress};
use werbolg_core::{id::IdF, AbsPath, Ident, Module, Namespace};
use werbolg_exec::{ExecutionEnviron, ExecutionMachine, ExecutionParams, WAllocator, NIF};
use werbolg_lang_common::{FileUnit, LinesMap, Report, ReportKind};

pub struct Source {
file_unit: FileUnit,
file_map: LinesMap,
}
use werbolg_lang_common::{Report, ReportKind, Source};

pub fn run_frontend(
params: &TalesParams,
Expand All @@ -24,12 +19,7 @@ pub fn run_frontend(
}

let path = std::path::PathBuf::from(&args[0]);
let file_unit = get_file(&path)?;
let file_map = LinesMap::new(&file_unit.content);
let source = Source {
file_unit,
file_map,
};
let source = get_file(&path)?;

let parsing_res = match params.frontend {
Frontend::Rusty => werbolg_lang_rusty::module(&source.file_unit),
Expand All @@ -44,7 +34,6 @@ pub fn run_frontend(

report_print(&source, report)?;
return Err(format!("parse error").into());
//return Err(format!("parse error \"{}\" : {:?}", path.to_string_lossy(), e).into());
}
Ok(module) => module,
};
Expand All @@ -57,7 +46,7 @@ pub fn run_frontend(

pub fn report_print(source: &Source, report: Report) -> Result<(), Box<dyn Error>> {
let mut s = String::new();
report.write(&source.file_unit, &source.file_map, &mut s)?;
report.write(&source, &mut s)?;
println!("{}", s);
Ok(())
}
Expand Down Expand Up @@ -160,9 +149,9 @@ pub fn run_exec<'m, 'e>(
}
}

fn get_file(path: &std::path::Path) -> std::io::Result<FileUnit> {
fn get_file(path: &std::path::Path) -> std::io::Result<Source> {
let path = std::path::PathBuf::from(&path);
let content = std::fs::read_to_string(&path).expect("file read");
let fileunit = FileUnit::from_string(path.to_string_lossy().to_string(), content);
let fileunit = Source::from_string(path.to_string_lossy().to_string(), content);
Ok(fileunit)
}

0 comments on commit 233650c

Please sign in to comment.