codespan reporting

This commit is contained in:
nothendev 2023-05-05 12:15:01 +03:00
parent 7e954ca279
commit 68a58e6a53
5 changed files with 54 additions and 25 deletions

View file

@ -6,5 +6,6 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
codespan-reporting = "0.11.1"
logos = "0"
thiserror = "1"

View file

@ -4,7 +4,7 @@ Use core.Int;
Constant Hi = "WHY???/\n";
Alias Yo = Byte;
Constant Version = Make Structure Version {
Constant Version = Make Version {
major: 1,
minor: 0,
patch: 0

View file

@ -59,16 +59,11 @@ pub struct UseDecl {
pub enum Expr {
Literal(Literal),
_IdentAccess(String),
Make(ExprMake)
Make(Box<ExprMake>)
}
#[derive(Debug)]
pub enum ExprMake {
Structure(Box<MakeStructure>)
}
#[derive(Debug)]
pub struct MakeStructure {
pub struct ExprMake {
pub name: String,
pub params: HashMap<String, Expr>
}

View file

@ -1,19 +1,57 @@
#![feature(result_option_inspect)]
#![allow(non_snake_case)]
use std::path::Path;
use codespan_reporting::{
diagnostic::Label,
term::{termcolor::StandardStream, Config},
};
use crate::lexer::Spanned;
mod ast;
mod lexer;
mod parser;
const TEST: &str = include_str!("../assets/why.idl");
//const TEST: &str = include_str!("../assets/why.idl");
fn main() {
let res = parser::parse(TEST);
match res {
Ok(ast) => {
println!("{:?}", ast);
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();
}
}
Err(e) => println!("{}", e),
}
}

View file

@ -1,7 +1,7 @@
use std::collections::HashMap;
use crate::{
ast::{Expr, ExprMake, Literal, MakeStructure, NumberLiteral},
ast::{Expr, ExprMake, Literal, NumberLiteral},
lexer::{Ident, NumberSuffix, Spanned, Token},
unwrap_match,
};
@ -17,14 +17,9 @@ impl<'a> Parser<'a> {
}
Token::Ident(Ident::Make) => {
self.eat();
match self.tokens.next()?.0 {
Token::Ident(Ident::Structure) => self
._ask_struct_init()?
.map(Box::new)
.map(ExprMake::Structure)
.map(Expr::Make),
_ => return Err(self.expected("a Make expression")),
}
self._ask_struct_init()?
.map(Box::new)
.map(Expr::Make)
}
_ => return Err(self.expected("an expression")),
})
@ -66,7 +61,7 @@ impl<'a> Parser<'a> {
})
}
fn _ask_struct_init(&mut self) -> Result<Spanned<MakeStructure>, ParserError> {
fn _ask_struct_init(&mut self) -> Result<Spanned<ExprMake>, ParserError> {
let Spanned(name, nSp) = self.ask_ident()?;
let Spanned(_, _) = self.get_real(
|token| matches!(token, Token::LeftCurly),
@ -92,7 +87,7 @@ impl<'a> Parser<'a> {
}
if let Spanned(Token::RightCurly, ccSp) = self.tokens.next()? {
return Ok(Spanned(MakeStructure { name, params }, nSp + ccSp));
return Ok(Spanned(ExprMake { name, params }, nSp + ccSp));
};
Err(self.expected("something"))