cl-ast: Add filename to File
- Better error reporting - Better pizza - Papa Cow's
This commit is contained in:
parent
cdb9ec49fe
commit
a4176c710e
@ -34,6 +34,7 @@ pub enum Visibility {
|
||||
/// A list of [Item]s
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
|
||||
pub struct File {
|
||||
pub name: &'static str,
|
||||
pub items: Vec<Item>,
|
||||
}
|
||||
|
||||
|
@ -13,8 +13,8 @@ pub trait WeightOf {
|
||||
|
||||
impl WeightOf for File {
|
||||
fn weight_of(&self) -> usize {
|
||||
let Self { items } = self;
|
||||
items.weight_of()
|
||||
let Self { name, items } = self;
|
||||
name.weight_of() + items.weight_of()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -44,8 +44,8 @@ pub trait Fold {
|
||||
s
|
||||
}
|
||||
fn fold_file(&mut self, f: File) -> File {
|
||||
let File { items } = f;
|
||||
File { items: items.into_iter().map(|i| self.fold_item(i)).collect() }
|
||||
let File { name, items } = f;
|
||||
File { name, items: items.into_iter().map(|i| self.fold_item(i)).collect() }
|
||||
}
|
||||
fn fold_attrs(&mut self, a: Attrs) -> Attrs {
|
||||
let Attrs { meta } = a;
|
||||
|
@ -26,7 +26,7 @@ pub trait Visit<'a>: Sized {
|
||||
fn visit_smuggled_float(&mut self, _f: &'a u64) {}
|
||||
fn visit_string(&mut self, _s: &'a str) {}
|
||||
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));
|
||||
}
|
||||
fn visit_attrs(&mut self, a: &'a Attrs) {
|
||||
|
@ -19,7 +19,7 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
let parent = path.parent().unwrap_or("".as_ref());
|
||||
|
||||
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) {
|
||||
Ok(code) => code,
|
||||
Err((code, ioerrs, perrs)) => {
|
||||
@ -40,7 +40,7 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
if env.get(main).is_ok() {
|
||||
let args = args
|
||||
.flat_map(|arg| {
|
||||
Parser::new(Lexer::new(&arg))
|
||||
Parser::new(&arg, Lexer::new(&arg))
|
||||
.parse::<Expr>()
|
||||
.map(|arg| env.eval(&arg))
|
||||
})
|
||||
|
@ -71,7 +71,7 @@ mod macros {
|
||||
///
|
||||
/// Returns a `Result<`[`Block`]`, ParseError>`
|
||||
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
|
||||
|
@ -1,6 +1,6 @@
|
||||
use super::*;
|
||||
|
||||
use cl_ast::Expr;
|
||||
use cl_ast::{Expr, Sym};
|
||||
use cl_lexer::error::{Error as LexError, Reason};
|
||||
use std::fmt::Display;
|
||||
pub type PResult<T> = Result<T, Error>;
|
||||
@ -8,6 +8,7 @@ pub type PResult<T> = Result<T, Error>;
|
||||
/// Contains information about [Parser] errors
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct Error {
|
||||
pub in_file: Sym,
|
||||
pub reason: ErrorKind,
|
||||
pub while_parsing: Parsing,
|
||||
pub loc: Loc,
|
||||
@ -129,13 +130,18 @@ pub enum Parsing {
|
||||
|
||||
impl Display for Error {
|
||||
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 {
|
||||
// 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
|
||||
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}")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -76,25 +76,29 @@ impl Fold for ModuleInliner {
|
||||
impl ModuleInliner {
|
||||
/// Attempts to read and parse a file for every module in the tree
|
||||
fn fold_module_kind(&mut self, m: Option<File>) -> Option<File> {
|
||||
use std::borrow::Cow;
|
||||
if let Some(f) = m {
|
||||
return Some(self.fold_file(f));
|
||||
}
|
||||
|
||||
// cd path/mod.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) {
|
||||
Err(error) => {
|
||||
let Some(basename) = self.path.file_name() else {
|
||||
return self.handle_io_error(error);
|
||||
};
|
||||
let path = self
|
||||
.path
|
||||
.parent()
|
||||
.and_then(Path::parent)
|
||||
.map(|path| path.join(basename))
|
||||
.unwrap_or_default();
|
||||
used_path = Cow::Owned(
|
||||
self.path
|
||||
.parent()
|
||||
.and_then(Path::parent)
|
||||
.map(|path| path.join(basename))
|
||||
.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),
|
||||
Ok(file) => file,
|
||||
}
|
||||
@ -102,7 +106,7 @@ impl ModuleInliner {
|
||||
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),
|
||||
Ok(file) => {
|
||||
self.path.set_extension("");
|
||||
|
@ -13,6 +13,8 @@ mod prec;
|
||||
/// Parses a sequence of [Tokens](Token) into an [AST](cl_ast)
|
||||
#[derive(Debug)]
|
||||
pub struct Parser<'t> {
|
||||
/// Name of the file being parsed
|
||||
file: Sym,
|
||||
/// Lazy tokenizer
|
||||
lexer: Lexer<'t>,
|
||||
/// Look-ahead buffer
|
||||
@ -23,8 +25,8 @@ pub struct Parser<'t> {
|
||||
|
||||
/// Basic parser functionality
|
||||
impl<'t> Parser<'t> {
|
||||
pub fn new(lexer: Lexer<'t>) -> Self {
|
||||
Self { loc: Loc::from(&lexer), lexer, next: None }
|
||||
pub fn new(filename: impl AsRef<str>, lexer: Lexer<'t>) -> Self {
|
||||
Self { file: filename.as_ref().into(), loc: Loc::from(&lexer), lexer, next: None }
|
||||
}
|
||||
|
||||
/// Gets the location of the last consumed [Token]
|
||||
@ -40,7 +42,7 @@ impl<'t> Parser<'t> {
|
||||
|
||||
/// Constructs an [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
|
||||
@ -262,7 +264,7 @@ impl Parse<'_> for File {
|
||||
} {
|
||||
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() })
|
||||
} else {
|
||||
Err(Error {
|
||||
in_file: p.file,
|
||||
reason: ExpectedParsing { want: Parsing::Path },
|
||||
while_parsing: P,
|
||||
loc: target.span.head,
|
||||
|
@ -19,7 +19,7 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
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>() {
|
||||
Ok(code) => {
|
||||
rl.accept();
|
||||
@ -150,8 +150,8 @@ pub mod yamlify {
|
||||
|
||||
impl Yamlify for File {
|
||||
fn yaml(&self, y: &mut Yamler) {
|
||||
let File { items } = self;
|
||||
y.key("File").yaml(items);
|
||||
let File { name, items } = self;
|
||||
y.key("File").pair("name", name).yaml(items);
|
||||
}
|
||||
}
|
||||
impl Yamlify for Visibility {
|
||||
|
@ -9,7 +9,7 @@ use cl_ast::File;
|
||||
use cl_interpret::{builtin::builtins, convalue::ConValue, env::Environment, interpret::Interpret};
|
||||
use cl_lexer::Lexer;
|
||||
use cl_parser::Parser;
|
||||
use std::{error::Error, path::Path};
|
||||
use std::{borrow::Cow, error::Error, path::Path};
|
||||
|
||||
/// Run the command line interface
|
||||
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 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);
|
||||
match mode {
|
||||
@ -59,24 +61,36 @@ pub fn run(args: Args) -> Result<(), Box<dyn Error>> {
|
||||
Mode::Run => menu::run(&mut ctx)?,
|
||||
}
|
||||
} else {
|
||||
let path = format_path_for_display(file.as_deref());
|
||||
let code = match &file {
|
||||
Some(file) => std::fs::read_to_string(file)?,
|
||||
None => std::io::read_to_string(std::io::stdin())?,
|
||||
};
|
||||
|
||||
match mode {
|
||||
Mode::Lex => lex_code(&code, file),
|
||||
Mode::Fmt => fmt_code(&code),
|
||||
Mode::Run | Mode::Menu => run_code(&code, &mut env),
|
||||
Mode::Lex => lex_code(&path, &code),
|
||||
Mode::Fmt => fmt_code(&path, &code),
|
||||
Mode::Run | Mode::Menu => run_code(&path, &code, &mut env),
|
||||
}?;
|
||||
}
|
||||
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>> {
|
||||
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 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) {
|
||||
Ok(a) => a,
|
||||
Err((code, io_errs, parse_errs)) => {
|
||||
@ -89,6 +103,9 @@ fn load_file(env: &mut Environment, path: impl AsRef<Path>) -> Result<ConValue,
|
||||
code
|
||||
}
|
||||
};
|
||||
use cl_ast::WeightOf;
|
||||
eprintln!("File {} weighs {} units", code.name, code.weight_of());
|
||||
|
||||
match env.eval(&code) {
|
||||
Ok(v) => Ok(v),
|
||||
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) {
|
||||
if let Some(path) = &path {
|
||||
print!("{}:", path.as_ref().display());
|
||||
if !path.is_empty() {
|
||||
print!("{}:", path);
|
||||
}
|
||||
match 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(())
|
||||
}
|
||||
|
||||
fn fmt_code(code: &str) -> Result<(), Box<dyn Error>> {
|
||||
let code = Parser::new(Lexer::new(code)).parse::<File>()?;
|
||||
fn fmt_code(path: &str, code: &str) -> Result<(), Box<dyn Error>> {
|
||||
let code = Parser::new(path, Lexer::new(code)).parse::<File>()?;
|
||||
println!("{code}");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn run_code(code: &str, env: &mut Environment) -> Result<(), Box<dyn Error>> {
|
||||
let code = Parser::new(Lexer::new(code)).parse::<File>()?;
|
||||
fn run_code(path: &str, code: &str, env: &mut Environment) -> Result<(), Box<dyn Error>> {
|
||||
let code = Parser::new(path, Lexer::new(code)).parse::<File>()?;
|
||||
match code.interpret(env)? {
|
||||
ConValue::Empty => {}
|
||||
ret => println!("{ret}"),
|
||||
|
@ -47,7 +47,7 @@ pub fn run(ctx: &mut ctx::Context) -> ReplResult<()> {
|
||||
if line.trim().is_empty() {
|
||||
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);
|
||||
|
||||
print!("{}", ansi::OUTPUT);
|
||||
@ -75,7 +75,7 @@ pub fn lex(_ctx: &mut ctx::Context) -> ReplResult<()> {
|
||||
|
||||
pub fn fmt(_ctx: &mut ctx::Context) -> ReplResult<()> {
|
||||
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>() {
|
||||
Ok(code) => println!("{}{code}{}", ansi::OUTPUT, ansi::RESET),
|
||||
|
@ -34,7 +34,7 @@ const C_LISTING: &str = "\x1b[38;5;117m";
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
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() {
|
||||
Ok(code) => code,
|
||||
Err(e) => {
|
||||
@ -91,7 +91,7 @@ fn enter_code(prj: &mut Table) -> Result<(), RlError> {
|
||||
if line.trim().is_empty() {
|
||||
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 = WhileElseDesugar.fold_file(code);
|
||||
|
||||
@ -102,7 +102,7 @@ fn enter_code(prj: &mut Table) -> Result<(), RlError> {
|
||||
|
||||
fn live_desugar() -> Result<(), RlError> {
|
||||
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");
|
||||
|
||||
let code = SquashGroups.fold_stmt(code);
|
||||
@ -128,7 +128,7 @@ fn query_type_expression(prj: &mut Table) -> Result<(), RlError> {
|
||||
return Ok(Response::Break);
|
||||
}
|
||||
// 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())?;
|
||||
pretty_handle(id.to_entry(prj))?;
|
||||
Ok(Response::Accept)
|
||||
@ -143,7 +143,7 @@ fn get_by_id(prj: &mut Table) -> Result<(), RlError> {
|
||||
if line.trim().is_empty() {
|
||||
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)? {
|
||||
cl_ast::Literal::Int(int) => int as _,
|
||||
other => Err(format!("Expected integer, got {other}"))?,
|
||||
@ -209,7 +209,7 @@ fn import_files(table: &mut Table) -> Result<(), RlError> {
|
||||
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() {
|
||||
Ok(code) => inline_modules(code, PathBuf::from(line).parent().unwrap_or("".as_ref())),
|
||||
Err(e) => {
|
||||
|
Loading…
x
Reference in New Issue
Block a user