Skip to content

Commit

Permalink
refactor span
Browse files Browse the repository at this point in the history
  • Loading branch information
LighghtEeloo committed Oct 23, 2024
1 parent 9815d4e commit 5b48fd7
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 282 deletions.
9 changes: 5 additions & 4 deletions lang/driver/src/local/pack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ impl File {
let path = path.clone();
LocalError::SrcFileNotFound(path)
})?;
let info = FileInfo::new(source.as_str(), Arc::new(path));
let info = FileInfo::new(source.as_str(), Some(Arc::new(path)));
let s = HashLexer::new(&source)
.hash_string(&info)
.map_err(|span| LocalError::LexerError(span))?;
Expand All @@ -149,10 +149,11 @@ impl FileLoaded {
pub fn merge<'a>(selves: impl IntoIterator<Item = &'a Self>) -> Result<PackageHash> {
let mut hashes = HashMap::new();
for file in selves {
let path = file.info.path();
hashes.insert(
file.info
.canonicalize()
.map_err(|_| LocalError::CanonicalizationError(file.info.display_path()))?,
path.canonicalize().map_err(|_| {
LocalError::CanonicalizationError(format!("{}", path.display()))
})?,
file.hash.clone(),
);
}
Expand Down
149 changes: 69 additions & 80 deletions lang/driver/src/package.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! The package notation of zydeco.

use crate::{check::pack::*, interp::pack::*, prelude::*, *};
use crate::{check::pack::*, interp::pack::*, local::err::LocalError, prelude::*, *};
use derive_more::From;
use serde::{Deserialize, Serialize};
use std::{path::PathBuf, sync::Arc};
Expand Down Expand Up @@ -59,89 +59,78 @@ pub enum Dependency {
Local(PathBuf),
}

mod _impl {
use super::*;
use local::err::LocalError;

impl Package {
pub fn parse_package(&self, alloc: ArcGlobalAlloc) -> Result<PackageStew> {
match self {
| Package::Local(LocalPackage { path, name, srcs, deps: _, bins: _, std: _ }) => {
let stew = LocalPackage::parse_package(
alloc.clone(),
name.as_str(),
path,
srcs.iter(),
)?;
Ok(stew)
}
| Package::Binary(path) => {
let source = std::fs::read_to_string(path.as_path())?;
Package::parse_source(alloc.clone(), source, Some(path.to_owned()))
}
| Package::Repl(source) => {
Package::parse_source(alloc.clone(), source.clone(), None)
}
impl Package {
pub fn parse_package(&self, alloc: ArcGlobalAlloc) -> Result<PackageStew> {
match self {
| Package::Local(LocalPackage { path, name, srcs, deps: _, bins: _, std: _ }) => {
let stew =
LocalPackage::parse_package(alloc.clone(), name.as_str(), path, srcs.iter())?;
Ok(stew)
}
| Package::Binary(path) => {
let source = std::fs::read_to_string(path.as_path())?;
Package::parse_source(alloc.clone(), source, Some(path.to_owned()))
}
| Package::Repl(source) => Package::parse_source(alloc.clone(), source.clone(), None),
}
pub fn parse_source(
alloc: ArcGlobalAlloc, source: String, path: Option<PathBuf>,
) -> Result<PackageStew> {
let mut parser = t::Parser::new(alloc.alloc());
let (loc, path) = match path {
| Some(path) => {
let loc = LocationCtx::File(FileInfo::new(&source, Arc::new(path.clone())));
(loc, path)
}
| None => (LocationCtx::Plain, PathBuf::new()),
};
}
pub fn parse_source(
alloc: ArcGlobalAlloc, source: String, path: Option<PathBuf>,
) -> Result<PackageStew> {
let mut parser = t::Parser::new(alloc.alloc());
let (loc, path) = match path {
| Some(path) => {
let loc = LocationCtx::File(FileInfo::new(&source, Some(Arc::new(path.clone()))));
(loc, path)
}
| None => (LocationCtx::Plain, PathBuf::new()),
};

let top = TopLevelParser::new()
.parse(&source, &loc, &mut parser, Lexer::new(&source))
.map_err(|error| {
LocalError::ParseError(
ParseError {
error,
file_info: &FileInfo::new(&source, Arc::new(path.clone())),
}
.to_string(),
)
})?;
let top = TopLevelParser::new()
.parse(&source, &loc, &mut parser, Lexer::new(&source))
.map_err(|error| {
LocalError::ParseError(
ParseError {
error,
file_info: &FileInfo::new(&source, Some(Arc::new(path.clone()))),
}
.to_string(),
)
})?;

let t::Parser { spans, arena: textual } = parser;
let bitter = b::Arena::new_arc(alloc.clone());
let desugarer = Desugarer { spans, textual, bitter, prim: b::PrimTerms::default() };
let DesugarOut { spans, arena, prim: prim_term, top } =
desugarer.run(top).map_err(|err| LocalError::DesugarError(err.to_string()))?;
let t::Parser { spans, arena: textual } = parser;
let bitter = b::Arena::new_arc(alloc.clone());
let desugarer = Desugarer { spans, textual, bitter, prim: b::PrimTerms::default() };
let DesugarOut { spans, arena, prim: prim_term, top } =
desugarer.run(top).map_err(|err| LocalError::DesugarError(err.to_string()))?;

Ok(PackageStew {
sources: [(path, source)].into_iter().collect(),
spans,
arena,
prim_term,
top,
})
}
pub fn check_package(
alloc: ArcGlobalAlloc, name: &str, pack: PackageStew,
) -> Result<PackageChecked> {
// resolving
let pack = pack.resolve(alloc.alloc())?.self_check(name);
// tycking
let checked = pack.tyck(alloc, name)?;
Ok(checked)
}
pub fn link_interp(name: &str, pack: PackageChecked) -> Result<PackageRuntime> {
// compiling
let dynamics = pack.dynamics(name)?;
Ok(PackageRuntime { dynamics })
}
pub fn run_interp(runtime: PackageRuntime) -> ProgKont {
runtime.run()
}
pub fn test_interp(runtime: PackageRuntime, name: &str, aloud: bool) -> Result<()> {
let () = runtime.test(name, aloud)?;
Ok(())
}
Ok(PackageStew {
sources: [(path, source)].into_iter().collect(),
spans,
arena,
prim_term,
top,
})
}
pub fn check_package(
alloc: ArcGlobalAlloc, name: &str, pack: PackageStew,
) -> Result<PackageChecked> {
// resolving
let pack = pack.resolve(alloc.alloc())?.self_check(name);
// tycking
let checked = pack.tyck(alloc, name)?;
Ok(checked)
}
pub fn link_interp(name: &str, pack: PackageChecked) -> Result<PackageRuntime> {
// compiling
let dynamics = pack.dynamics(name)?;
Ok(PackageRuntime { dynamics })
}
pub fn run_interp(runtime: PackageRuntime) -> ProgKont {
runtime.run()
}
pub fn test_interp(runtime: PackageRuntime, name: &str, aloud: bool) -> Result<()> {
let () = runtime.test(name, aloud)?;
Ok(())
}
}
13 changes: 9 additions & 4 deletions lang/surface/src/textual/err.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@ impl Display for ParseError<'_> {
match error {
| User { ref error } => write!(f, "{error}"),
| InvalidToken { ref location } => {
write!(f, "Invalid token at {}:{}", gen.display_path(), gen.trans_span2(*location))
write!(
f,
"Invalid token at {}:{}",
gen.path().display(),
gen.trans_span2(*location)
)
}
| UnrecognizedEof { ref location, ref expected } => {
write!(
f,
"Unrecognized EOF found at {}:{}{}",
gen.display_path(),
gen.path().display(),
gen.trans_span2(*location),
fmt_expected(expected)
)
Expand All @@ -29,7 +34,7 @@ impl Display for ParseError<'_> {
write!(
f,
"Unrecognized token `{token}` found at {}:{} - {}{}",
gen.display_path(),
gen.path().display(),
gen.trans_span2(*start),
gen.trans_span2(*end),
fmt_expected(expected)
Expand All @@ -39,7 +44,7 @@ impl Display for ParseError<'_> {
write!(
f,
"Extra token `{token}` found at {}:{} - {}",
gen.display_path(),
gen.path().display(),
gen.trans_span2(*start),
gen.trans_span2(*end),
)
Expand Down
61 changes: 0 additions & 61 deletions lang/utils/src/interval_tree.rs

This file was deleted.

4 changes: 1 addition & 3 deletions lang/utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@ pub mod span;
pub mod never;
pub mod monoid;
pub mod wrappers;
pub mod interval_tree;

pub mod prelude {
pub use crate::{
arena::*,
cells::{MultiCell, SingCell},
interval_tree::IntervalTree,
monoid::Monoid,
never::Never,
rc,
span::{Sp, Span, SpanHolder, SpanView},
span::{Sp, Span},
};
}
Loading

0 comments on commit 5b48fd7

Please sign in to comment.