cl-parser: Transliterate to a trait-based parsing implementation

Bump version number.
This commit is contained in:
John 2024-07-31 01:39:00 -05:00
parent 388a69948e
commit 97808fd855
10 changed files with 613 additions and 545 deletions

View File

@ -15,7 +15,7 @@ resolver = "2"
[workspace.package]
repository = "https://git.soft.fish/j/Conlang"
version = "0.0.6"
version = "0.0.7"
authors = ["John Breaux <j@soft.fish>"]
edition = "2021"
license = "MIT"

View File

@ -2,6 +2,7 @@
use std::{error::Error, path::PathBuf};
use cl_ast::Expr;
use cl_interpret::{convalue::ConValue, env::Environment};
use cl_lexer::Lexer;
use cl_parser::{inliner::ModuleInliner, Parser};
@ -18,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)).file()?;
let code = Parser::new(Lexer::new(&code)).parse()?;
let code = match ModuleInliner::new(parent).inline(code) {
Ok(code) => code,
Err((code, ioerrs, perrs)) => {
@ -40,7 +41,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let args = args
.flat_map(|arg| {
Parser::new(Lexer::new(&arg))
.expr()
.parse::<Expr>()
.map(|arg| env.eval(&arg))
})
.collect::<Result<Vec<_>, _>>()?;

View File

@ -48,6 +48,7 @@ mod macros {
//! ```
#![allow(unused_macros)]
use crate::IResult;
use cl_parser::parser::Parse;
use super::*;
@ -63,14 +64,14 @@ mod macros {
///
/// Returns a `Result<`[`File`]`, ParseError>`
pub macro file($($t:tt)*) {
Parser::new(Lexer::new(stringify!( $($t)* ))).file()
File::parse(&mut Parser::new(Lexer::new(stringify!( $($t)* ))))
}
/// Stringifies, lexes, and parses everything you give to it
///
/// Returns a `Result<`[`Block`]`, ParseError>`
pub macro block($($t:tt)*) {
Parser::new(Lexer::new(stringify!({ $($t)* }))).block()
Block::parse(&mut Parser::new(Lexer::new(stringify!({ $($t)* }))))
}
/// Evaluates a block of code in the given environment

View File

@ -489,7 +489,7 @@ pub mod error {
pub enum Reason {
/// Found an opening delimiter of type [char], but not the expected closing delimiter
UnmatchedDelimiters(char),
/// Found a character that doesn't belong to any [Kind](cl_token::Kind)
/// Found a character that doesn't belong to any [TokenKind](cl_token::TokenKind)
UnexpectedChar(char),
/// Found a character that's not valid in identifiers while looking for an identifier
NotIdentifier(char),

View File

@ -85,7 +85,7 @@ impl Fold for ModuleInliner {
Ok(file) => file,
};
let kind = match Parser::new(Lexer::new(&file)).file() {
let kind = match Parser::new(Lexer::new(&file)).parse() {
Err(e) => return self.handle_parse_error(e),
Ok(file) => ModuleKind::Inline(file),
};

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,6 @@
//! Pretty prints a conlang AST in yaml
use cl_ast::Stmt;
use cl_lexer::Lexer;
use cl_parser::Parser;
use repline::{error::Error as RlError, Repline};
@ -19,7 +20,7 @@ fn main() -> Result<(), Box<dyn Error>> {
};
let mut parser = Parser::new(Lexer::new(&line));
let code = match parser.stmt() {
let code = match parser.parse::<Stmt>() {
Ok(code) => {
rl.accept();
code

View File

@ -5,6 +5,7 @@ use crate::{
menu,
tools::print_token,
};
use cl_ast::File;
use cl_interpret::{convalue::ConValue, env::Environment, interpret::Interpret};
use cl_lexer::Lexer;
use cl_parser::Parser;
@ -49,7 +50,7 @@ fn load_file(env: &mut Environment, path: impl AsRef<Path>) -> Result<ConValue,
let inliner =
cl_parser::inliner::ModuleInliner::new(path.as_ref().parent().unwrap_or(Path::new("")));
let file = std::fs::read_to_string(path)?;
let code = Parser::new(Lexer::new(&file)).file()?;
let code = Parser::new(Lexer::new(&file)).parse()?;
let code = match inliner.inline(code) {
Ok(a) => a,
Err((code, io_errs, parse_errs)) => {
@ -79,13 +80,13 @@ fn lex_code(code: &str, path: Option<impl AsRef<Path>>) -> Result<(), Box<dyn Er
}
fn fmt_code(code: &str) -> Result<(), Box<dyn Error>> {
let code = Parser::new(Lexer::new(code)).file()?;
let code = Parser::new(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)).file()?;
let code = Parser::new(Lexer::new(code)).parse::<File>()?;
match code.interpret(env)? {
ConValue::Empty => {}
ret => println!("{ret}"),

View File

@ -1,4 +1,5 @@
use crate::{ansi, ctx};
use cl_ast::Stmt;
use cl_lexer::Lexer;
use cl_parser::Parser;
use repline::{error::ReplResult, prebaked::*};
@ -42,7 +43,7 @@ pub fn run(ctx: &mut ctx::Context) -> ReplResult<()> {
use cl_parser::inliner::ModuleInliner;
read_and(ansi::CYAN, "cl>", " ?>", |line| {
let code = Parser::new(Lexer::new(line)).stmt()?;
let code = Parser::new(Lexer::new(line)).parse::<Stmt>()?;
let code = ModuleInliner::new(".").fold_stmt(code);
print!("{}", ansi::OUTPUT);
@ -71,7 +72,7 @@ pub fn fmt(_ctx: &mut ctx::Context) -> ReplResult<()> {
read_and(ansi::BRIGHT_MAGENTA, "cl>", " ?>", |line| {
let mut p = Parser::new(Lexer::new(line));
match p.stmt() {
match p.parse::<Stmt>() {
Ok(code) => println!("{}{code}{}", ansi::OUTPUT, ansi::RESET),
Err(e) => Err(e)?,
}

View File

@ -4,6 +4,7 @@ use cl_typeck::{entry::Entry, stage::*, table::Table, type_expression::TypeExpre
use cl_ast::{
ast_visitor::{Fold, Visit},
desugar::*,
Stmt, Ty,
};
use cl_lexer::Lexer;
use cl_parser::{inliner::ModuleInliner, Parser};
@ -38,7 +39,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let mut prj = Table::default();
let mut parser = Parser::new(Lexer::new(PREAMBLE));
let code = match parser.file() {
let code = match parser.parse() {
Ok(code) => code,
Err(e) => {
eprintln!("{STDLIB_DISPLAY_PATH}:{e}");
@ -94,7 +95,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)).file()?;
let code = Parser::new(Lexer::new(line)).parse()?;
let code = inline_modules(code, "");
let code = WhileElseDesugar.fold_file(code);
// Safety: this is totally unsafe
@ -105,7 +106,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)).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);
@ -131,7 +132,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 = Parser::new(Lexer::new(line)).ty()?.kind;
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)
@ -139,6 +140,7 @@ fn query_type_expression(prj: &mut Table) -> Result<(), RlError> {
}
fn get_by_id(prj: &mut Table) -> Result<(), RlError> {
use cl_parser::parser::Parse;
use cl_structures::index_map::MapIndex;
use cl_typeck::handle::Handle;
read_and(C_BYID, "id>", "? >", |line| {
@ -146,11 +148,11 @@ fn get_by_id(prj: &mut Table) -> Result<(), RlError> {
return Ok(Response::Break);
}
let mut parser = Parser::new(Lexer::new(line));
let def_id = match parser.literal()? {
let def_id = match Parse::parse(&mut parser)? {
cl_ast::Literal::Int(int) => int as _,
other => Err(format!("Expected integer, got {other}"))?,
};
let mut path = parser.path().unwrap_or_default();
let mut path = parser.parse::<cl_ast::Path>().unwrap_or_default();
path.absolute = false;
let handle = Handle::from_usize(def_id).to_entry(prj);
@ -212,7 +214,7 @@ fn import_files(table: &mut Table) -> Result<(), RlError> {
};
let mut parser = Parser::new(Lexer::new(&file));
let code = match parser.file() {
let code = match parser.parse() {
Ok(code) => inline_modules(code, PathBuf::from(line).parent().unwrap_or("".as_ref())),
Err(e) => {
eprintln!("{C_ERROR}{line}:{e}\x1b[0m");