Skip to content

Commit

Permalink
Use sml-file, warn on .sig/.fun not containing 1
Browse files Browse the repository at this point in the history
  • Loading branch information
azdavis committed Nov 30, 2023
1 parent 9ce1e4a commit a2cf7eb
Show file tree
Hide file tree
Showing 35 changed files with 368 additions and 57 deletions.
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/cm-syntax/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ text-size-util.workspace = true

lex-util.path = "../lex-util"
slash-var-path.path = "../slash-var-path"
sml-file.path = "../sml-file"
2 changes: 1 addition & 1 deletion crates/cm-syntax/src/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub(crate) fn get(root: ParseRoot) -> Result<CmFile> {
};
let kind = match cls {
Some(class) => match class.val {
Class::Sml => PathKind::Sml,
Class::Sml(k) => PathKind::Sml(k),
Class::Cm => PathKind::Cm,
Class::Other(s) => {
return Err(Error::new(ErrorKind::UnsupportedClass(path, s), class.range))
Expand Down
15 changes: 6 additions & 9 deletions crates/cm-syntax/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ pub enum CmFileKind {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PathKind {
/// SML files.
Sml,
Sml(sml_file::Kind),
/// CM files.
Cm,
}
Expand Down Expand Up @@ -220,7 +220,7 @@ impl Member {
#[derive(Debug, Clone)]
pub enum Class {
/// SML files.
Sml,
Sml(sml_file::Kind),
/// CM files.
Cm,
/// Some other class.
Expand All @@ -229,11 +229,8 @@ pub enum Class {

impl Class {
fn from_path(path: &Path) -> Option<Self> {
let ret = match path.extension()?.to_str()? {
"sig" | "sml" | "fun" => Self::Sml,
"cm" => Self::Cm,
_ => return None,
};
let ext = path.extension()?.to_str()?;
let ret = if ext == "cm" { Self::Cm } else { Self::Sml(ext.parse().ok()?) };
Some(ret)
}
}
Expand All @@ -243,7 +240,7 @@ impl FromStr for Class {

fn from_str(s: &str) -> Result<Self, Self::Err> {
let ret = match s.to_ascii_lowercase().as_str() {
"sml" => Self::Sml,
"sml" => Self::Sml(sml_file::Kind::Sml),
"cm" | "cmfile" => Self::Cm,
s => Self::Other(s.to_owned()),
};
Expand All @@ -254,7 +251,7 @@ impl FromStr for Class {
impl fmt::Display for Class {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Class::Sml => f.write_str("sml"),
Class::Sml(_) => f.write_str("sml"),
Class::Cm => f.write_str("cm"),
Class::Other(s) => f.write_str(s),
}
Expand Down
1 change: 1 addition & 0 deletions crates/input/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ mlb-hir.path = "../mlb-hir"
mlb-syntax.path = "../mlb-syntax"
slash-var-path.path = "../slash-var-path"
sml-file-syntax.path = "../sml-file-syntax"
sml-file.path = "../sml-file"
sml-fixity.path = "../sml-fixity"
sml-namespace.path = "../sml-namespace"
sml-path.path = "../sml-path"
Expand Down
10 changes: 5 additions & 5 deletions crates/input/src/lower_cm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ struct CmFile {
/// only optional so this can derive default.
pos_db: Option<text_pos::PositionDb>,
cm_paths: Vec<paths::PathId>,
sml_paths: FxHashSet<paths::PathId>,
sml_paths: FxHashSet<(paths::PathId, sml_file::Kind)>,
exports: NameExports,
}

Expand Down Expand Up @@ -125,7 +125,7 @@ fn get_one_cm_file<F>(
}
};
match pp.val.kind() {
cm_syntax::PathKind::Sml => {
cm_syntax::PathKind::Sml(kind) => {
let contents = match read_file(st.fs, source, path.as_path()) {
Ok(x) => x,
Err(e) => {
Expand All @@ -134,7 +134,7 @@ fn get_one_cm_file<F>(
}
};
st.sources.insert(path_id, contents);
ret.sml_paths.insert(path_id);
ret.sml_paths.insert((path_id, kind));
}
cm_syntax::PathKind::Cm => {
let cur = GroupPathToProcess { parent: cur_path_id, range: source.range, path: path_id };
Expand Down Expand Up @@ -179,7 +179,7 @@ fn get_one_cm_file<F>(
struct ExportCx<'a> {
group: &'a StartedGroup,
cm_paths: &'a [paths::PathId],
sml_paths: &'a FxHashSet<paths::PathId>,
sml_paths: &'a FxHashSet<(paths::PathId, sml_file::Kind)>,
cur_path_id: paths::PathId,
}

Expand Down Expand Up @@ -273,7 +273,7 @@ fn get_all_sources<F>(st: &mut St<'_, F>, cx: ExportCx<'_>, range: TextRange, ac
where
F: paths::FileSystem,
{
for path_id in cx.sml_paths {
for (path_id, _) in cx.sml_paths {
let contents = st.sources.get(path_id).expect("sml file should be set").as_str();
get_top_defs(contents, ac, range);
}
Expand Down
4 changes: 2 additions & 2 deletions crates/input/src/lower_mlb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ where
}
};
let kind = match pp.val.kind() {
mlb_syntax::PathKind::Sml => {
mlb_syntax::PathKind::Sml(file_kind) => {
let contents = match read_file(st.fs, source, path.as_path()) {
Ok(x) => x,
Err(e) => {
Expand All @@ -131,7 +131,7 @@ where
}
};
st.sources.insert(path_id, contents);
mlb_hir::PathKind::Source
mlb_hir::PathKind::Source(file_kind)
}
mlb_syntax::PathKind::Mlb => {
st.stack.push(GroupPathToProcess {
Expand Down
2 changes: 1 addition & 1 deletion crates/input/src/topo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn bas_dec_paths(ac: &mut BTreeSet<PathId>, dec: &mlb_hir::BasDec) {
mlb_hir::BasDec::Path(p, _) => {
ac.insert(*p);
}
mlb_hir::BasDec::SourcePathSet(paths) => ac.extend(paths.iter().copied()),
mlb_hir::BasDec::SourcePathSet(paths) => ac.extend(paths.iter().map(|&(x, _)| x)),
mlb_hir::BasDec::Basis(_, exp) => bas_exp_paths(ac, exp),
mlb_hir::BasDec::Local(local_dec, in_dec) => {
bas_dec_paths(ac, local_dec);
Expand Down
1 change: 1 addition & 0 deletions crates/mlb-hir/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ paths.workspace = true
str-util.workspace = true
text-size-util.workspace = true

sml-file.path = "../sml-file"
sml-namespace.path = "../sml-namespace"
4 changes: 2 additions & 2 deletions crates/mlb-hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub enum BasDec {
/// A file path.
Path(paths::PathId, PathKind),
/// Used by CM only.
SourcePathSet(FxHashSet<paths::PathId>),
SourcePathSet(FxHashSet<(paths::PathId, sml_file::Kind)>),
/// A sequence of declarations.
Seq(Vec<BasDec>),
}
Expand Down Expand Up @@ -58,7 +58,7 @@ pub enum BasExp {
#[derive(Debug, Clone, Copy)]
pub enum PathKind {
/// An SML source path.
Source,
Source(sml_file::Kind),
/// A group path, like MLB or CM.
Group,
}
Expand Down
1 change: 1 addition & 0 deletions crates/mlb-statics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ config.path = "../config"
mlb-hir.path = "../mlb-hir"
sml-comment.path = "../sml-comment"
sml-file-syntax.path = "../sml-file-syntax"
sml-file.path = "../sml-file"
sml-fixity.path = "../sml-fixity"
sml-hir-lower.path = "../sml-hir-lower"
sml-hir.path = "../sml-hir"
Expand Down
14 changes: 7 additions & 7 deletions crates/mlb-statics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,10 @@ fn get_bas_dec(
}
},
mlb_hir::BasDec::Path(path, kind) => match kind {
mlb_hir::PathKind::Source => {
mlb_hir::PathKind::Source(file_kind) => {
let contents = cx.source_file_contents.get(path).expect("no source file");
let mut fix_env = scope.fix_env.clone();
let syntax = SourceFileSyntax::new(&mut fix_env, cx.lang, contents);
let syntax = SourceFileSyntax::new(&mut fix_env, cx.lang, *file_kind, contents);
get_source_file(st, cx.lang, *path, scope, ac, fix_env, syntax);
}
mlb_hir::PathKind::Group => match st.bases.get(path) {
Expand All @@ -259,11 +259,11 @@ fn get_bas_dec(
mlb_hir::BasDec::SourcePathSet(paths) => {
let mut syntaxes: paths::PathMap<_> = paths
.iter()
.map(|path| {
let contents = cx.source_file_contents.get(path).expect("no source file");
.map(|&(path, kind)| {
let contents = cx.source_file_contents.get(&path).expect("no source file");
let mut fix_env = scope.fix_env.clone();
let syntax = SourceFileSyntax::new(&mut fix_env, cx.lang, contents);
(*path, (fix_env, syntax))
let syntax = SourceFileSyntax::new(&mut fix_env, cx.lang, kind, contents);
(path, (fix_env, syntax))
})
.collect();
let hir_roots: paths::PathMap<_> = syntaxes
Expand Down Expand Up @@ -386,7 +386,7 @@ pub fn update_one(
contents: &str,
) {
let mut fix_env = sml_fixity::STD_BASIS.clone();
sf.syntax = sml_file_syntax::SourceFileSyntax::new(&mut fix_env, lang, contents);
sf.syntax = sml_file_syntax::SourceFileSyntax::new(&mut fix_env, lang, sf.syntax.kind, contents);
let mode = sml_statics_types::mode::Mode::Regular(Some(path));
let checked =
sml_statics::get(syms_tys, &sf.scope, mode, &sf.syntax.lower.arenas, &sf.syntax.lower.root);
Expand Down
4 changes: 3 additions & 1 deletion crates/mlb-statics/src/std_basis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ where
contents = &owned_contents;
}
let mut fix_env = sml_fixity::STD_BASIS.clone();
let started = SourceFileSyntax::new(&mut fix_env, &lang, contents);
let ext = std::path::Path::new(name).extension().unwrap().to_str().unwrap();
let file_kind: sml_file::Kind = ext.parse().unwrap();
let started = SourceFileSyntax::new(&mut fix_env, &lang, file_kind, contents);
if let Some(e) = started.lex_errors.first() {
panic!("{name}: lex error: {e}");
}
Expand Down
1 change: 1 addition & 0 deletions crates/mlb-syntax/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ text-size-util.workspace = true

lex-util.path = "../lex-util"
slash-var-path.path = "../slash-var-path"
sml-file.path = "../sml-file"
sml-namespace.path = "../sml-namespace"
7 changes: 2 additions & 5 deletions crates/mlb-syntax/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,8 @@ fn bas_dec_one(p: &mut Parser<'_>) -> Result<BasDecOne> {
}

fn path_kind(path: &Path) -> Option<PathKind> {
let ret = match path.extension()?.to_str()? {
"sml" | "sig" | "fun" => PathKind::Sml,
"mlb" => PathKind::Mlb,
_ => return None,
};
let ext = path.extension()?.to_str()?;
let ret = if ext == "mlb" { PathKind::Mlb } else { PathKind::Sml(ext.parse().ok()?) };
Some(ret)
}

Expand Down
2 changes: 1 addition & 1 deletion crates/mlb-syntax/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ pub enum BasExp {
#[derive(Debug, Clone, Copy)]
pub enum PathKind {
/// SML paths.
Sml,
Sml(sml_file::Kind),
/// ML Basis paths.
Mlb,
}
Expand Down
1 change: 1 addition & 0 deletions crates/sml-dynamics-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ str-util.workspace = true
config.path = "../config"
sml-dynamics.path = "../sml-dynamics"
sml-file-syntax.path = "../sml-file-syntax"
sml-file.path = "../sml-file"
sml-fixity.path = "../sml-fixity"
sml-hir.path = "../sml-hir"
sml-path.path = "../sml-path"
Expand Down
2 changes: 1 addition & 1 deletion crates/sml-dynamics-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn check(s: &str, steps: &[&str]) {
let check_steps = !env_var_enabled("MILLET_NO_CHECK_STEPS");
let mut fix_env = sml_fixity::STD_BASIS.clone();
let lang = config::lang::Language::default();
let sf = sml_file_syntax::SourceFileSyntax::new(&mut fix_env, &lang, s);
let sf = sml_file_syntax::SourceFileSyntax::new(&mut fix_env, &lang, sml_file::Kind::Sml, s);
if let Some(e) = sf.lex_errors.first() {
panic!("lex error: {e}");
}
Expand Down
1 change: 1 addition & 0 deletions crates/sml-file-syntax/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ elapsed.workspace = true
text-pos.workspace = true

config.path = "../config"
sml-file.path = "../sml-file"
sml-fixity.path = "../sml-fixity"
sml-hir-lower.path = "../sml-hir-lower"
sml-lex.path = "../sml-lex"
Expand Down
13 changes: 10 additions & 3 deletions crates/sml-file-syntax/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,23 @@ pub struct SourceFileSyntax {
pub parse: sml_parse::Parse,
/// The lowered HIR.
pub lower: sml_hir_lower::Lower,
/// The kind of source file this is.
pub kind: sml_file::Kind,
}

impl SourceFileSyntax {
/// Starts processing a single source file.
pub fn new(fix_env: &mut sml_fixity::Env, lang: &config::lang::Language, contents: &str) -> Self {
pub fn new(
fix_env: &mut sml_fixity::Env,
lang: &config::lang::Language,
kind: sml_file::Kind,
contents: &str,
) -> Self {
elapsed::log("SourceFileSyntax::new", || {
let (lex_errors, parse) = Self::lex_and_parse(fix_env, contents);
let mut lower = sml_hir_lower::get(lang, &parse.root);
let mut lower = sml_hir_lower::get(lang, kind, &parse.root);
sml_ty_var_scope::get(&mut lower.arenas, &lower.root);
Self { pos_db: text_pos::PositionDb::new(contents), lex_errors, parse, lower }
Self { pos_db: text_pos::PositionDb::new(contents), lex_errors, parse, lower, kind }
})
}

Expand Down
10 changes: 10 additions & 0 deletions crates/sml-file/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "sml-file"
version.workspace = true
edition.workspace = true
license.workspace = true
publish.workspace = true

[lib]
doctest = false
test = false
Loading

0 comments on commit a2cf7eb

Please sign in to comment.