From 68a58e6a53f576a62d015b5c15243036f135c81e Mon Sep 17 00:00:00 2001 From: nothendev Date: Fri, 5 May 2023 12:15:01 +0300 Subject: [PATCH] codespan reporting --- programs/aidl/Cargo.toml | 1 + programs/aidl/assets/why.idl | 2 +- programs/aidl/src/ast.rs | 9 ++---- programs/aidl/src/main.rs | 50 ++++++++++++++++++++++++++++---- programs/aidl/src/parser/expr.rs | 17 ++++------- 5 files changed, 54 insertions(+), 25 deletions(-) diff --git a/programs/aidl/Cargo.toml b/programs/aidl/Cargo.toml index abea912..7616c2c 100644 --- a/programs/aidl/Cargo.toml +++ b/programs/aidl/Cargo.toml @@ -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" diff --git a/programs/aidl/assets/why.idl b/programs/aidl/assets/why.idl index c1e82c9..e1ca115 100644 --- a/programs/aidl/assets/why.idl +++ b/programs/aidl/assets/why.idl @@ -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 diff --git a/programs/aidl/src/ast.rs b/programs/aidl/src/ast.rs index 3d4b14d..9c18491 100644 --- a/programs/aidl/src/ast.rs +++ b/programs/aidl/src/ast.rs @@ -59,16 +59,11 @@ pub struct UseDecl { pub enum Expr { Literal(Literal), _IdentAccess(String), - Make(ExprMake) + Make(Box) } #[derive(Debug)] -pub enum ExprMake { - Structure(Box) -} - -#[derive(Debug)] -pub struct MakeStructure { +pub struct ExprMake { pub name: String, pub params: HashMap } diff --git a/programs/aidl/src/main.rs b/programs/aidl/src/main.rs index 6cce604..179bd3e 100644 --- a/programs/aidl/src/main.rs +++ b/programs/aidl/src/main.rs @@ -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), } } diff --git a/programs/aidl/src/parser/expr.rs b/programs/aidl/src/parser/expr.rs index bc2b55b..c793206 100644 --- a/programs/aidl/src/parser/expr.rs +++ b/programs/aidl/src/parser/expr.rs @@ -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, ParserError> { + fn _ask_struct_init(&mut self) -> Result, 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"))