forked from AbleOS/ableos_userland
codespan reporting
This commit is contained in:
parent
7e954ca279
commit
68a58e6a53
|
@ -6,5 +6,6 @@ edition = "2021"
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
codespan-reporting = "0.11.1"
|
||||||
logos = "0"
|
logos = "0"
|
||||||
thiserror = "1"
|
thiserror = "1"
|
||||||
|
|
|
@ -4,7 +4,7 @@ Use core.Int;
|
||||||
Constant Hi = "WHY???/\n";
|
Constant Hi = "WHY???/\n";
|
||||||
Alias Yo = Byte;
|
Alias Yo = Byte;
|
||||||
|
|
||||||
Constant Version = Make Structure Version {
|
Constant Version = Make Version {
|
||||||
major: 1,
|
major: 1,
|
||||||
minor: 0,
|
minor: 0,
|
||||||
patch: 0
|
patch: 0
|
||||||
|
|
|
@ -59,16 +59,11 @@ pub struct UseDecl {
|
||||||
pub enum Expr {
|
pub enum Expr {
|
||||||
Literal(Literal),
|
Literal(Literal),
|
||||||
_IdentAccess(String),
|
_IdentAccess(String),
|
||||||
Make(ExprMake)
|
Make(Box<ExprMake>)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum ExprMake {
|
pub struct ExprMake {
|
||||||
Structure(Box<MakeStructure>)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct MakeStructure {
|
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub params: HashMap<String, Expr>
|
pub params: HashMap<String, Expr>
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,19 +1,57 @@
|
||||||
#![feature(result_option_inspect)]
|
#![feature(result_option_inspect)]
|
||||||
#![allow(non_snake_case)]
|
#![allow(non_snake_case)]
|
||||||
|
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
use codespan_reporting::{
|
||||||
|
diagnostic::Label,
|
||||||
|
term::{termcolor::StandardStream, Config},
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::lexer::Spanned;
|
||||||
|
|
||||||
mod ast;
|
mod ast;
|
||||||
mod lexer;
|
mod lexer;
|
||||||
mod parser;
|
mod parser;
|
||||||
|
|
||||||
const TEST: &str = include_str!("../assets/why.idl");
|
//const TEST: &str = include_str!("../assets/why.idl");
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let res = parser::parse(TEST);
|
let mut args = std::env::args();
|
||||||
match res {
|
args.next().unwrap();
|
||||||
Ok(ast) => {
|
if let Some(file) = args.next() {
|
||||||
println!("{:?}", ast);
|
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),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
ast::{Expr, ExprMake, Literal, MakeStructure, NumberLiteral},
|
ast::{Expr, ExprMake, Literal, NumberLiteral},
|
||||||
lexer::{Ident, NumberSuffix, Spanned, Token},
|
lexer::{Ident, NumberSuffix, Spanned, Token},
|
||||||
unwrap_match,
|
unwrap_match,
|
||||||
};
|
};
|
||||||
|
@ -17,14 +17,9 @@ impl<'a> Parser<'a> {
|
||||||
}
|
}
|
||||||
Token::Ident(Ident::Make) => {
|
Token::Ident(Ident::Make) => {
|
||||||
self.eat();
|
self.eat();
|
||||||
match self.tokens.next()?.0 {
|
self._ask_struct_init()?
|
||||||
Token::Ident(Ident::Structure) => self
|
.map(Box::new)
|
||||||
._ask_struct_init()?
|
.map(Expr::Make)
|
||||||
.map(Box::new)
|
|
||||||
.map(ExprMake::Structure)
|
|
||||||
.map(Expr::Make),
|
|
||||||
_ => return Err(self.expected("a Make expression")),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
_ => return Err(self.expected("an expression")),
|
_ => 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(name, nSp) = self.ask_ident()?;
|
||||||
let Spanned(_, _) = self.get_real(
|
let Spanned(_, _) = self.get_real(
|
||||||
|token| matches!(token, Token::LeftCurly),
|
|token| matches!(token, Token::LeftCurly),
|
||||||
|
@ -92,7 +87,7 @@ impl<'a> Parser<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Spanned(Token::RightCurly, ccSp) = self.tokens.next()? {
|
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"))
|
Err(self.expected("something"))
|
||||||
|
|
Loading…
Reference in a new issue