Skip to content

Commit

Permalink
Implement parser in pest
Browse files Browse the repository at this point in the history
  • Loading branch information
jzbor committed Dec 1, 2024
1 parent 1301bfc commit d2c70b0
Show file tree
Hide file tree
Showing 11 changed files with 320 additions and 396 deletions.
141 changes: 118 additions & 23 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ path = "src/main.rs"

[dependencies]
rustyline = { version = "10.0.0", optional = true }
nom = { version = "7", default-features = false, features = ["alloc"] }
nom_locate = { version = "4.0.0", default-features = false, features = ["alloc"] }
clap = { version = "4.3.11", features = ["derive"], optional = true }
colored = { version = "2.0.4", optional = true }
humantime = { version = "2.1.0", optional = true }
pest = { version = "2.7.14", features = [], default-features = false }
pest_derive = { version = "2.7.14", features = [], default-features = false }

[features]
default = ["std"]
std = [ "rustyline", "clap", "colored", "humantime" ]
std = [ "rustyline", "clap", "colored", "humantime", "pest/std", "pest_derive/std" ]
6 changes: 3 additions & 3 deletions examples/capav.lsh
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
term1 := (\x y . y x) (y y) z;
term2 := (\x y . y x) (a a) z;

@echo "This works as expected:"
@echo "This works as expected:";
!debug (!vnormalize term2);

@echo ""
@echo "But here we see the necessity of capture avoidance:"
@echo "";
@echo "But here we see the necessity of capture avoidance:";
!debug (!vnormalize term1);
10 changes: 5 additions & 5 deletions examples/fib.lsh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@usestd
@set strategy normal
@set numerals true
@usestd;
@set strategy normal;
@set numerals true;

iszero := λn.n (λx. FALSE) TRUE;

Expand All @@ -13,6 +13,6 @@ fib' := \f x. IFTHENELSE
(ADD (f f (PRED x)) (f f (PRED (PRED x)))));
fib := fib' fib';

@echo ""
@echo "6. Fibonacci number"
@echo "";
@echo "6. Fibonacci number";
listunsorted := !debug (!cnorm (fib $6));
12 changes: 6 additions & 6 deletions examples/sort.lsh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@set strategy normal
@usestd
@set strategy normal;
@usestd;


one := !n (SUCC NIL);
Expand Down Expand Up @@ -34,11 +34,11 @@ sort' := \f l. IFTHENELSE
sort := sort' sort';


@echo ""
@echo "Unsorted list:"
@echo "";
@echo "Unsorted list:";
listunsorted := !debug (!norm (CONS three (CONS one (CONS four ((CONS two NIL))))));

@echo ""
@echo "After sorting:"
@echo "";
@echo "After sorting:";
!debug (!time (!cnorm (sort listunsorted)));

36 changes: 19 additions & 17 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ extern crate alloc;
use alloc::borrow::ToOwned;
use alloc::format;
use alloc::string::{String, ToString};
use pest::RuleType;
use core::fmt::Display;

use crate::{parsing, r#macro::Macro};
use crate::r#macro::Macro;

pub type LashResult<T> = Result<T, LashError>;

Expand All @@ -25,6 +26,7 @@ pub enum LashErrorType {
SetKeyError,
SetValueError,
SyntaxError,
UnknownMacroError,
#[cfg(not(feature = "std"))]
NotFoundError,
#[cfg(not(feature = "std"))]
Expand Down Expand Up @@ -66,6 +68,13 @@ impl LashError {
}
}

pub fn new_syntax_error<T: RuleType>(err: pest::error::Error<T>) -> Self {
LashError {
error_type: LashErrorType::SyntaxError,
message: format!("\n{}", err),
}
}

pub fn new_set_key_error(key: &str) -> Self {
LashError {
error_type: LashErrorType::SetKeyError,
Expand All @@ -80,6 +89,13 @@ impl LashError {
}
}

pub fn new_unknown_macro_error(name: &str) -> Self {
LashError {
error_type: LashErrorType::UnknownMacroError,
message: format!("unknown macro '{}'", name),
}
}

#[cfg(not(feature = "std"))]
pub fn new_not_supported_error(message: String) -> Self {
LashError {
Expand Down Expand Up @@ -108,9 +124,10 @@ impl Display for LashError {
FileError => "File Error",
FormatError => "Format Error",
MacroArgError => "Macro Argument Error",
SyntaxError => "Syntax Error",
SetKeyError => "Set Key Error",
SetValueError => "Set Value Error",
SyntaxError => "Syntax Error",
UnknownMacroError => "Unknown Macro Error",
#[cfg(not(feature = "std"))]
NotFoundError => "Not Found",
#[cfg(not(feature = "std"))]
Expand All @@ -120,21 +137,6 @@ impl Display for LashError {
}
}

impl From<nom::Err<parsing::ParseError<'_>>> for LashError {
fn from(value: nom::Err<parsing::ParseError<'_>>) -> Self {
use nom::Err::*;
let message = match value {
Incomplete(_) => "incomplete data".to_owned(),
Error(e) => format!("{}", e),
Failure(e) => format!("{}", e),
};
LashError {
error_type: LashErrorType::SyntaxError,
message,
}
}
}

impl From<core::fmt::Error> for LashError {
fn from(value: core::fmt::Error) -> Self {
LashError {
Expand Down
Loading

0 comments on commit d2c70b0

Please sign in to comment.