2025-01-29 11:33:01 +01:00
|
|
|
use {
|
|
|
|
ariadne::{Color, Label, Report, ReportKind, Source},
|
|
|
|
chumsky::{
|
|
|
|
prelude::*, text::*
|
2025-01-31 17:56:08 +01:00
|
|
|
},
|
|
|
|
laddertypes::{
|
|
|
|
dict::TypeDict,
|
|
|
|
parser::ParseLadderType,
|
|
|
|
unparser::UnparseLadderType, BimapTypeDict
|
|
|
|
},
|
|
|
|
std::sync::{Arc, RwLock}
|
2025-01-29 11:33:01 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct Morphism {
|
|
|
|
symbol: String,
|
|
|
|
type_args: Vec<(String, String)>,
|
2025-01-31 17:56:08 +01:00
|
|
|
src_type: laddertypes::TypeTerm,
|
|
|
|
dst_type: laddertypes::TypeTerm,
|
2025-01-29 11:33:01 +01:00
|
|
|
locations: Vec<String>
|
|
|
|
}
|
|
|
|
|
|
|
|
/* morphism-base text format:
|
|
|
|
* NAME '(' [TYPE-ARG-NAME ':' KIND] ')'
|
|
|
|
* SRC-TYPE
|
|
|
|
* '-->' DST-TYPE
|
|
|
|
* '@' [ LOCATION ':' ]
|
|
|
|
*/
|
2025-01-31 17:56:08 +01:00
|
|
|
fn parser(
|
|
|
|
type_dict: Arc<RwLock< BimapTypeDict >>
|
|
|
|
) -> impl Parser<char, Vec<Morphism>, Error = Simple<char>> {
|
|
|
|
|
2025-01-29 11:33:01 +01:00
|
|
|
ident().padded()
|
|
|
|
.then(
|
|
|
|
ident().padded()
|
|
|
|
.then_ignore(just(':').padded())
|
|
|
|
.then(none_of(",)").repeated().padded())
|
|
|
|
.separated_by(just(',').padded())
|
|
|
|
.delimited_by(just('('), just(')'))
|
|
|
|
)
|
|
|
|
.then_ignore(just('\n'))
|
|
|
|
.then(
|
|
|
|
take_until(just("-->").ignored())
|
|
|
|
.then(take_until(just('@').ignored()))
|
|
|
|
)
|
|
|
|
.then(
|
|
|
|
none_of(":\n").repeated().separated_by(just(':'))
|
|
|
|
)
|
2025-01-31 17:56:08 +01:00
|
|
|
.map(
|
|
|
|
move |(((symbol, type_args), ((src_type, _), (dst_type, _))), locations)| {
|
|
|
|
let mut type_dict = type_dict.write().unwrap();
|
|
|
|
let type_args : Vec<_> = type_args.into_iter().map(|(v,k)| (v,k.into_iter().collect())).collect();
|
|
|
|
for (var, kind) in type_args.iter() {
|
|
|
|
type_dict.add_varname(var.clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
let src_type = type_dict.parse(&src_type.iter().collect::<String>()).expect("couldnt parse src type");
|
|
|
|
let dst_type = type_dict.parse(&dst_type.iter().collect::<String>()).expect("couldnt parse dst type");
|
|
|
|
|
|
|
|
Morphism {
|
|
|
|
symbol,
|
|
|
|
type_args,
|
|
|
|
src_type,
|
|
|
|
dst_type,
|
|
|
|
locations: locations.into_iter().map(|l| l.into_iter().collect()).collect()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.separated_by(text::newline())
|
2025-01-29 11:33:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2025-01-31 17:56:08 +01:00
|
|
|
let type_dict = Arc::new(RwLock::new(BimapTypeDict::new()));
|
2025-02-01 12:40:45 +01:00
|
|
|
let src = std::fs::read_to_string(
|
|
|
|
std::env::args().nth(1).expect("expected file name")
|
|
|
|
).expect("read");
|
2025-01-31 17:56:08 +01:00
|
|
|
|
2025-02-01 12:40:45 +01:00
|
|
|
let result = parser(type_dict.clone()).parse(src.clone());
|
2025-01-29 11:33:01 +01:00
|
|
|
match result {
|
|
|
|
Ok(morphisms) => {
|
2025-01-31 17:56:08 +01:00
|
|
|
println!("parse ok.");
|
|
|
|
let mut dict = type_dict.write().unwrap();
|
|
|
|
|
|
|
|
for m in morphisms {
|
|
|
|
println!("{}\n {}\n---> \n {}\n", m.symbol,
|
2025-02-01 12:40:45 +01:00
|
|
|
m.src_type.sugar(&mut *dict).pretty(&mut *dict, 1),
|
|
|
|
m.dst_type.sugar(&mut *dict).pretty(&mut *dict, 1),
|
2025-01-31 17:56:08 +01:00
|
|
|
);
|
|
|
|
}
|
2025-01-29 11:33:01 +01:00
|
|
|
}
|
|
|
|
Err(errs) => {
|
|
|
|
errs.into_iter().for_each(|e| {
|
|
|
|
Report::build(ReportKind::Error, (), e.span().start)
|
|
|
|
.with_message(e.to_string())
|
|
|
|
.with_label(
|
|
|
|
Label::new(e.span())
|
|
|
|
.with_message(e)
|
|
|
|
.with_color(Color::Red),
|
|
|
|
)
|
|
|
|
.finish()
|
|
|
|
.print(Source::from(&src))
|
|
|
|
.unwrap()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|