2023-05-04 11:50:17 -05:00
|
|
|
#![feature(result_option_inspect)]
|
2023-05-04 06:19:32 -05:00
|
|
|
#![allow(non_snake_case)]
|
2023-05-04 02:27:04 -05:00
|
|
|
|
2023-05-05 04:15:01 -05:00
|
|
|
use std::path::Path;
|
|
|
|
|
|
|
|
use codespan_reporting::{
|
|
|
|
diagnostic::Label,
|
|
|
|
term::{termcolor::StandardStream, Config},
|
|
|
|
};
|
|
|
|
|
|
|
|
use crate::lexer::Spanned;
|
|
|
|
|
2023-05-04 06:19:32 -05:00
|
|
|
mod ast;
|
|
|
|
mod lexer;
|
|
|
|
mod parser;
|
2023-05-04 02:27:04 -05:00
|
|
|
|
2023-05-05 04:15:01 -05:00
|
|
|
//const TEST: &str = include_str!("../assets/why.idl");
|
2023-05-04 02:27:04 -05:00
|
|
|
|
|
|
|
fn main() {
|
2023-05-05 04:15:01 -05:00
|
|
|
let mut args = std::env::args();
|
|
|
|
args.next().unwrap();
|
|
|
|
if let Some(file) = args.next() {
|
|
|
|
let path = Path::new(&file);
|
|
|
|
dbg!(path);
|
|
|
|
let codespan_file = codespan_reporting::files::SimpleFile::new(
|
|
|
|
&file,
|
|
|
|
std::fs::read_to_string(path).unwrap(),
|
|
|
|
);
|
|
|
|
let writer = StandardStream::stdout(codespan_reporting::term::termcolor::ColorChoice::Auto);
|
|
|
|
let config = Config::default();
|
|
|
|
match parser::parse(codespan_file.source()) {
|
|
|
|
Ok(ast) => println!("SUCCESS: \n{:#?}", ast),
|
|
|
|
Err(e) => {
|
|
|
|
let msg = e.to_string();
|
|
|
|
let label = match e {
|
|
|
|
parser::ParserError::UnexpectedEOF => Label::primary(
|
|
|
|
(),
|
|
|
|
(codespan_file.source().len() - 1)..codespan_file.source().len(),
|
|
|
|
).with_message("Unexpected end of file here"),
|
|
|
|
parser::ParserError::Unexpected(expected, Spanned(got, span)) => Label::primary((), span.0).with_message(format!("Unexpected `{got}`, expected {expected}")),
|
|
|
|
parser::ParserError::PleaseStopParsingUse => unreachable!()
|
|
|
|
};
|
|
|
|
let diagnostic = codespan_reporting::diagnostic::Diagnostic::error()
|
|
|
|
.with_message(msg)
|
|
|
|
.with_labels(vec![label]);
|
|
|
|
codespan_reporting::term::emit(
|
|
|
|
&mut writer.lock(),
|
|
|
|
&config,
|
|
|
|
&codespan_file,
|
|
|
|
&diagnostic,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
}
|
2023-05-04 08:51:31 -05:00
|
|
|
}
|
2023-05-04 07:44:49 -05:00
|
|
|
}
|
2023-05-04 06:19:32 -05:00
|
|
|
}
|
2023-05-04 02:27:04 -05:00
|
|
|
|
2023-05-04 06:19:32 -05:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! unwrap_match {
|
|
|
|
($x:expr, $m:pat => $a:expr) => {
|
|
|
|
match $x {
|
|
|
|
$m => $a,
|
2023-05-04 08:51:31 -05:00
|
|
|
_ => unreachable!(),
|
2023-05-04 06:19:32 -05:00
|
|
|
}
|
|
|
|
};
|
2023-05-04 02:27:04 -05:00
|
|
|
}
|