cl-ast: Add filename to File

- Better error reporting
- Better pizza
- Papa Cow's
This commit is contained in:
John 2025-03-14 04:11:22 -05:00
parent cdb9ec49fe
commit a4176c710e
13 changed files with 80 additions and 49 deletions

View File

@ -34,6 +34,7 @@ pub enum Visibility {
/// A list of [Item]s /// A list of [Item]s
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)] #[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
pub struct File { pub struct File {
pub name: &'static str,
pub items: Vec<Item>, pub items: Vec<Item>,
} }

View File

@ -13,8 +13,8 @@ pub trait WeightOf {
impl WeightOf for File { impl WeightOf for File {
fn weight_of(&self) -> usize { fn weight_of(&self) -> usize {
let Self { items } = self; let Self { name, items } = self;
items.weight_of() name.weight_of() + items.weight_of()
} }
} }

View File

@ -44,8 +44,8 @@ pub trait Fold {
s s
} }
fn fold_file(&mut self, f: File) -> File { fn fold_file(&mut self, f: File) -> File {
let File { items } = f; let File { name, items } = f;
File { items: items.into_iter().map(|i| self.fold_item(i)).collect() } File { name, items: items.into_iter().map(|i| self.fold_item(i)).collect() }
} }
fn fold_attrs(&mut self, a: Attrs) -> Attrs { fn fold_attrs(&mut self, a: Attrs) -> Attrs {
let Attrs { meta } = a; let Attrs { meta } = a;

View File

@ -26,7 +26,7 @@ pub trait Visit<'a>: Sized {
fn visit_smuggled_float(&mut self, _f: &'a u64) {} fn visit_smuggled_float(&mut self, _f: &'a u64) {}
fn visit_string(&mut self, _s: &'a str) {} fn visit_string(&mut self, _s: &'a str) {}
fn visit_file(&mut self, f: &'a File) { fn visit_file(&mut self, f: &'a File) {
let File { items } = f; let File { name: _, items } = f;
items.iter().for_each(|i| self.visit_item(i)); items.iter().for_each(|i| self.visit_item(i));
} }
fn visit_attrs(&mut self, a: &'a Attrs) { fn visit_attrs(&mut self, a: &'a Attrs) {

View File

@ -19,7 +19,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let parent = path.parent().unwrap_or("".as_ref()); let parent = path.parent().unwrap_or("".as_ref());
let code = std::fs::read_to_string(&path)?; let code = std::fs::read_to_string(&path)?;
let code = Parser::new(Lexer::new(&code)).parse()?; let code = Parser::new(path.display().to_string(), Lexer::new(&code)).parse()?;
let code = match ModuleInliner::new(parent).inline(code) { let code = match ModuleInliner::new(parent).inline(code) {
Ok(code) => code, Ok(code) => code,
Err((code, ioerrs, perrs)) => { Err((code, ioerrs, perrs)) => {
@ -40,7 +40,7 @@ fn main() -> Result<(), Box<dyn Error>> {
if env.get(main).is_ok() { if env.get(main).is_ok() {
let args = args let args = args
.flat_map(|arg| { .flat_map(|arg| {
Parser::new(Lexer::new(&arg)) Parser::new(&arg, Lexer::new(&arg))
.parse::<Expr>() .parse::<Expr>()
.map(|arg| env.eval(&arg)) .map(|arg| env.eval(&arg))
}) })

View File

@ -71,7 +71,7 @@ mod macros {
/// ///
/// Returns a `Result<`[`Block`]`, ParseError>` /// Returns a `Result<`[`Block`]`, ParseError>`
pub macro block($($t:tt)*) { pub macro block($($t:tt)*) {
Block::parse(&mut Parser::new(Lexer::new(stringify!({ $($t)* })))) Block::parse(&mut Parser::new("test", Lexer::new(stringify!({ $($t)* }))))
} }
/// Evaluates a block of code in the given environment /// Evaluates a block of code in the given environment

View File

@ -1,6 +1,6 @@
use super::*; use super::*;
use cl_ast::Expr; use cl_ast::{Expr, Sym};
use cl_lexer::error::{Error as LexError, Reason}; use cl_lexer::error::{Error as LexError, Reason};
use std::fmt::Display; use std::fmt::Display;
pub type PResult<T> = Result<T, Error>; pub type PResult<T> = Result<T, Error>;
@ -8,6 +8,7 @@ pub type PResult<T> = Result<T, Error>;
/// Contains information about [Parser] errors /// Contains information about [Parser] errors
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub struct Error { pub struct Error {
pub in_file: Sym,
pub reason: ErrorKind, pub reason: ErrorKind,
pub while_parsing: Parsing, pub while_parsing: Parsing,
pub loc: Loc, pub loc: Loc,
@ -129,13 +130,18 @@ pub enum Parsing {
impl Display for Error { impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { reason, while_parsing, loc } = self; let Self { in_file, reason, while_parsing, loc } = self;
match reason { match reason {
// TODO entries are debug-printed // TODO entries are debug-printed
ErrorKind::Todo(_) => write!(f, "{loc} {reason} {while_parsing:?}"), ErrorKind::Todo(_) => write!(f, "{in_file}:{loc} {reason} {while_parsing:?}"),
// lexical errors print their own higher-resolution loc info // lexical errors print their own higher-resolution loc info
ErrorKind::Lexical(e) => write!(f, "{e} (while parsing {while_parsing})"), ErrorKind::Lexical(e) => write!(f, "{e} (while parsing {while_parsing})"),
_ => write!(f, "{loc}: {reason} while parsing {while_parsing}"), _ => {
if !in_file.is_empty() {
write!(f, "{in_file}:")?
}
write!(f, "{loc}: {reason} while parsing {while_parsing}")
}
} }
} }
} }

View File

@ -76,25 +76,29 @@ impl Fold for ModuleInliner {
impl ModuleInliner { impl ModuleInliner {
/// Attempts to read and parse a file for every module in the tree /// Attempts to read and parse a file for every module in the tree
fn fold_module_kind(&mut self, m: Option<File>) -> Option<File> { fn fold_module_kind(&mut self, m: Option<File>) -> Option<File> {
use std::borrow::Cow;
if let Some(f) = m { if let Some(f) = m {
return Some(self.fold_file(f)); return Some(self.fold_file(f));
} }
// cd path/mod.cl // cd path/mod.cl
self.path.set_extension("cl"); self.path.set_extension("cl");
let mut used_path: Cow<Path> = Cow::Borrowed(&self.path);
let file = match std::fs::read_to_string(&self.path) { let file = match std::fs::read_to_string(&self.path) {
Err(error) => { Err(error) => {
let Some(basename) = self.path.file_name() else { let Some(basename) = self.path.file_name() else {
return self.handle_io_error(error); return self.handle_io_error(error);
}; };
let path = self used_path = Cow::Owned(
.path self.path
.parent() .parent()
.and_then(Path::parent) .and_then(Path::parent)
.map(|path| path.join(basename)) .map(|path| path.join(basename))
.unwrap_or_default(); .unwrap_or_default(),
);
match std::fs::read_to_string(&path) { match std::fs::read_to_string(&used_path) {
Err(error) => return self.handle_io_error(error), Err(error) => return self.handle_io_error(error),
Ok(file) => file, Ok(file) => file,
} }
@ -102,7 +106,7 @@ impl ModuleInliner {
Ok(file) => file, Ok(file) => file,
}; };
match Parser::new(Lexer::new(&file)).parse() { match Parser::new(used_path.display().to_string(), Lexer::new(&file)).parse() {
Err(e) => self.handle_parse_error(e), Err(e) => self.handle_parse_error(e),
Ok(file) => { Ok(file) => {
self.path.set_extension(""); self.path.set_extension("");

View File

@ -13,6 +13,8 @@ mod prec;
/// Parses a sequence of [Tokens](Token) into an [AST](cl_ast) /// Parses a sequence of [Tokens](Token) into an [AST](cl_ast)
#[derive(Debug)] #[derive(Debug)]
pub struct Parser<'t> { pub struct Parser<'t> {
/// Name of the file being parsed
file: Sym,
/// Lazy tokenizer /// Lazy tokenizer
lexer: Lexer<'t>, lexer: Lexer<'t>,
/// Look-ahead buffer /// Look-ahead buffer
@ -23,8 +25,8 @@ pub struct Parser<'t> {
/// Basic parser functionality /// Basic parser functionality
impl<'t> Parser<'t> { impl<'t> Parser<'t> {
pub fn new(lexer: Lexer<'t>) -> Self { pub fn new(filename: impl AsRef<str>, lexer: Lexer<'t>) -> Self {
Self { loc: Loc::from(&lexer), lexer, next: None } Self { file: filename.as_ref().into(), loc: Loc::from(&lexer), lexer, next: None }
} }
/// Gets the location of the last consumed [Token] /// Gets the location of the last consumed [Token]
@ -40,7 +42,7 @@ impl<'t> Parser<'t> {
/// Constructs an [Error] /// Constructs an [Error]
pub fn error(&self, reason: ErrorKind, while_parsing: Parsing) -> Error { pub fn error(&self, reason: ErrorKind, while_parsing: Parsing) -> Error {
Error { reason, while_parsing, loc: self.loc } Error { in_file: self.file, reason, while_parsing, loc: self.loc }
} }
/// Internal impl of peek and consume /// Internal impl of peek and consume
@ -262,7 +264,7 @@ impl Parse<'_> for File {
} { } {
items.push(Item::parse(p)?) items.push(Item::parse(p)?)
} }
Ok(File { items }) Ok(File { name: p.file.to_ref(), items })
} }
} }
@ -634,6 +636,7 @@ impl Parse<'_> for ImplKind {
Ok(ImplKind::Trait { impl_trait, for_type: Ty::parse(p)?.into() }) Ok(ImplKind::Trait { impl_trait, for_type: Ty::parse(p)?.into() })
} else { } else {
Err(Error { Err(Error {
in_file: p.file,
reason: ExpectedParsing { want: Parsing::Path }, reason: ExpectedParsing { want: Parsing::Path },
while_parsing: P, while_parsing: P,
loc: target.span.head, loc: target.span.head,

View File

@ -19,7 +19,7 @@ fn main() -> Result<(), Box<dyn Error>> {
Err(e) => Err(e)?, Err(e) => Err(e)?,
}; };
let mut parser = Parser::new(Lexer::new(&line)); let mut parser = Parser::new("", Lexer::new(&line));
let code = match parser.parse::<Stmt>() { let code = match parser.parse::<Stmt>() {
Ok(code) => { Ok(code) => {
rl.accept(); rl.accept();
@ -150,8 +150,8 @@ pub mod yamlify {
impl Yamlify for File { impl Yamlify for File {
fn yaml(&self, y: &mut Yamler) { fn yaml(&self, y: &mut Yamler) {
let File { items } = self; let File { name, items } = self;
y.key("File").yaml(items); y.key("File").pair("name", name).yaml(items);
} }
} }
impl Yamlify for Visibility { impl Yamlify for Visibility {

View File

@ -9,7 +9,7 @@ use cl_ast::File;
use cl_interpret::{builtin::builtins, convalue::ConValue, env::Environment, interpret::Interpret}; use cl_interpret::{builtin::builtins, convalue::ConValue, env::Environment, interpret::Interpret};
use cl_lexer::Lexer; use cl_lexer::Lexer;
use cl_parser::Parser; use cl_parser::Parser;
use std::{error::Error, path::Path}; use std::{borrow::Cow, error::Error, path::Path};
/// Run the command line interface /// Run the command line interface
pub fn run(args: Args) -> Result<(), Box<dyn Error>> { pub fn run(args: Args) -> Result<(), Box<dyn Error>> {
@ -49,7 +49,9 @@ pub fn run(args: Args) -> Result<(), Box<dyn Error>> {
if repl { if repl {
if let Some(file) = file { if let Some(file) = file {
load_file(&mut env, file)?; if let Err(e) = load_file(&mut env, file) {
eprintln!("{e}")
}
} }
let mut ctx = Context::with_env(env); let mut ctx = Context::with_env(env);
match mode { match mode {
@ -59,24 +61,36 @@ pub fn run(args: Args) -> Result<(), Box<dyn Error>> {
Mode::Run => menu::run(&mut ctx)?, Mode::Run => menu::run(&mut ctx)?,
} }
} else { } else {
let path = format_path_for_display(file.as_deref());
let code = match &file { let code = match &file {
Some(file) => std::fs::read_to_string(file)?, Some(file) => std::fs::read_to_string(file)?,
None => std::io::read_to_string(std::io::stdin())?, None => std::io::read_to_string(std::io::stdin())?,
}; };
match mode { match mode {
Mode::Lex => lex_code(&code, file), Mode::Lex => lex_code(&path, &code),
Mode::Fmt => fmt_code(&code), Mode::Fmt => fmt_code(&path, &code),
Mode::Run | Mode::Menu => run_code(&code, &mut env), Mode::Run | Mode::Menu => run_code(&path, &code, &mut env),
}?; }?;
} }
Ok(()) Ok(())
} }
fn format_path_for_display(path: Option<&Path>) -> Cow<str> {
match path {
Some(file) => file
.to_str()
.map(Cow::Borrowed)
.unwrap_or_else(|| Cow::Owned(file.display().to_string())),
None => Cow::Borrowed(""),
}
}
fn load_file(env: &mut Environment, path: impl AsRef<Path>) -> Result<ConValue, Box<dyn Error>> { fn load_file(env: &mut Environment, path: impl AsRef<Path>) -> Result<ConValue, Box<dyn Error>> {
let inliner = cl_parser::inliner::ModuleInliner::new(path.as_ref().with_extension("")); let path = path.as_ref();
let inliner = cl_parser::inliner::ModuleInliner::new(path.with_extension(""));
let file = std::fs::read_to_string(path)?; let file = std::fs::read_to_string(path)?;
let code = Parser::new(Lexer::new(&file)).parse()?; let code = Parser::new(path.display().to_string(), Lexer::new(&file)).parse()?;
let code = match inliner.inline(code) { let code = match inliner.inline(code) {
Ok(a) => a, Ok(a) => a,
Err((code, io_errs, parse_errs)) => { Err((code, io_errs, parse_errs)) => {
@ -89,6 +103,9 @@ fn load_file(env: &mut Environment, path: impl AsRef<Path>) -> Result<ConValue,
code code
} }
}; };
use cl_ast::WeightOf;
eprintln!("File {} weighs {} units", code.name, code.weight_of());
match env.eval(&code) { match env.eval(&code) {
Ok(v) => Ok(v), Ok(v) => Ok(v),
Err(e) => { Err(e) => {
@ -98,10 +115,10 @@ fn load_file(env: &mut Environment, path: impl AsRef<Path>) -> Result<ConValue,
} }
} }
fn lex_code(code: &str, path: Option<impl AsRef<Path>>) -> Result<(), Box<dyn Error>> { fn lex_code(path: &str, code: &str) -> Result<(), Box<dyn Error>> {
for token in Lexer::new(code) { for token in Lexer::new(code) {
if let Some(path) = &path { if !path.is_empty() {
print!("{}:", path.as_ref().display()); print!("{}:", path);
} }
match token { match token {
Ok(token) => print_token(&token), Ok(token) => print_token(&token),
@ -111,14 +128,14 @@ fn lex_code(code: &str, path: Option<impl AsRef<Path>>) -> Result<(), Box<dyn Er
Ok(()) Ok(())
} }
fn fmt_code(code: &str) -> Result<(), Box<dyn Error>> { fn fmt_code(path: &str, code: &str) -> Result<(), Box<dyn Error>> {
let code = Parser::new(Lexer::new(code)).parse::<File>()?; let code = Parser::new(path, Lexer::new(code)).parse::<File>()?;
println!("{code}"); println!("{code}");
Ok(()) Ok(())
} }
fn run_code(code: &str, env: &mut Environment) -> Result<(), Box<dyn Error>> { fn run_code(path: &str, code: &str, env: &mut Environment) -> Result<(), Box<dyn Error>> {
let code = Parser::new(Lexer::new(code)).parse::<File>()?; let code = Parser::new(path, Lexer::new(code)).parse::<File>()?;
match code.interpret(env)? { match code.interpret(env)? {
ConValue::Empty => {} ConValue::Empty => {}
ret => println!("{ret}"), ret => println!("{ret}"),

View File

@ -47,7 +47,7 @@ pub fn run(ctx: &mut ctx::Context) -> ReplResult<()> {
if line.trim().is_empty() { if line.trim().is_empty() {
return Ok(Response::Deny); return Ok(Response::Deny);
} }
let code = Parser::new(Lexer::new(line)).parse::<Stmt>()?; let code = Parser::new("", Lexer::new(line)).parse::<Stmt>()?;
let code = ModuleInliner::new(".").fold_stmt(code); let code = ModuleInliner::new(".").fold_stmt(code);
print!("{}", ansi::OUTPUT); print!("{}", ansi::OUTPUT);
@ -75,7 +75,7 @@ pub fn lex(_ctx: &mut ctx::Context) -> ReplResult<()> {
pub fn fmt(_ctx: &mut ctx::Context) -> ReplResult<()> { pub fn fmt(_ctx: &mut ctx::Context) -> ReplResult<()> {
read_and(ansi::BRIGHT_MAGENTA, "cl>", " ?>", |line| { read_and(ansi::BRIGHT_MAGENTA, "cl>", " ?>", |line| {
let mut p = Parser::new(Lexer::new(line)); let mut p = Parser::new("", Lexer::new(line));
match p.parse::<Stmt>() { match p.parse::<Stmt>() {
Ok(code) => println!("{}{code}{}", ansi::OUTPUT, ansi::RESET), Ok(code) => println!("{}{code}{}", ansi::OUTPUT, ansi::RESET),

View File

@ -34,7 +34,7 @@ const C_LISTING: &str = "\x1b[38;5;117m";
fn main() -> Result<(), Box<dyn Error>> { fn main() -> Result<(), Box<dyn Error>> {
let mut prj = Table::default(); let mut prj = Table::default();
let mut parser = Parser::new(Lexer::new(PREAMBLE)); let mut parser = Parser::new("PREAMBLE", Lexer::new(PREAMBLE));
let code = match parser.parse() { let code = match parser.parse() {
Ok(code) => code, Ok(code) => code,
Err(e) => { Err(e) => {
@ -91,7 +91,7 @@ fn enter_code(prj: &mut Table) -> Result<(), RlError> {
if line.trim().is_empty() { if line.trim().is_empty() {
return Ok(Response::Break); return Ok(Response::Break);
} }
let code = Parser::new(Lexer::new(line)).parse()?; let code = Parser::new("", Lexer::new(line)).parse()?;
let code = inline_modules(code, ""); let code = inline_modules(code, "");
let code = WhileElseDesugar.fold_file(code); let code = WhileElseDesugar.fold_file(code);
@ -102,7 +102,7 @@ fn enter_code(prj: &mut Table) -> Result<(), RlError> {
fn live_desugar() -> Result<(), RlError> { fn live_desugar() -> Result<(), RlError> {
read_and(C_RESV, "se>", "? >", |line| { read_and(C_RESV, "se>", "? >", |line| {
let code = Parser::new(Lexer::new(line)).parse::<Stmt>()?; let code = Parser::new("", Lexer::new(line)).parse::<Stmt>()?;
println!("Raw, as parsed:\n{C_LISTING}{code}\x1b[0m"); println!("Raw, as parsed:\n{C_LISTING}{code}\x1b[0m");
let code = SquashGroups.fold_stmt(code); let code = SquashGroups.fold_stmt(code);
@ -128,7 +128,7 @@ fn query_type_expression(prj: &mut Table) -> Result<(), RlError> {
return Ok(Response::Break); return Ok(Response::Break);
} }
// parse it as a path, and convert the path into a borrowed path // parse it as a path, and convert the path into a borrowed path
let ty: Ty = Parser::new(Lexer::new(line)).parse()?; let ty: Ty = Parser::new("", Lexer::new(line)).parse()?;
let id = ty.evaluate(prj, prj.root())?; let id = ty.evaluate(prj, prj.root())?;
pretty_handle(id.to_entry(prj))?; pretty_handle(id.to_entry(prj))?;
Ok(Response::Accept) Ok(Response::Accept)
@ -143,7 +143,7 @@ fn get_by_id(prj: &mut Table) -> Result<(), RlError> {
if line.trim().is_empty() { if line.trim().is_empty() {
return Ok(Response::Break); return Ok(Response::Break);
} }
let mut parser = Parser::new(Lexer::new(line)); let mut parser = Parser::new("", Lexer::new(line));
let def_id = match Parse::parse(&mut parser)? { let def_id = match Parse::parse(&mut parser)? {
cl_ast::Literal::Int(int) => int as _, cl_ast::Literal::Int(int) => int as _,
other => Err(format!("Expected integer, got {other}"))?, other => Err(format!("Expected integer, got {other}"))?,
@ -209,7 +209,7 @@ fn import_files(table: &mut Table) -> Result<(), RlError> {
return Ok(Response::Accept); return Ok(Response::Accept);
}; };
let mut parser = Parser::new(Lexer::new(&file)); let mut parser = Parser::new("", Lexer::new(&file));
let code = match parser.parse() { let code = match parser.parse() {
Ok(code) => inline_modules(code, PathBuf::from(line).parent().unwrap_or("".as_ref())), Ok(code) => inline_modules(code, PathBuf::from(line).parent().unwrap_or("".as_ref())),
Err(e) => { Err(e) => {