Skip to content

Commit

Permalink
Update core
Browse files Browse the repository at this point in the history
  • Loading branch information
rscarson committed Sep 15, 2023
1 parent 032fcb7 commit ae8763d
Show file tree
Hide file tree
Showing 54 changed files with 3,542 additions and 67 deletions.
318 changes: 266 additions & 52 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ chrono = "0.4.23"
rand = "0.8.5"

# Feature deps
#js-sandbox = { version = "0.2.0-rc.0", optional = true }
js-sandbox = {optional = true, git = "https://github.com/rscarson/js-sandbox"}
js-sandbox = { version = "0.2.0-rc.2", optional = true }
md-5 = { version = "0.10.5", optional = true }
sha2 = { version = "0.10.6", optional = true }
base64 = { version = "0.21.0", optional = true }
Expand Down
675 changes: 675 additions & 0 deletions example_extensions/zarbans_grotto.js

Large diffs are not rendered by default.

54 changes: 54 additions & 0 deletions examples/interactive_console.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use lavendeux_parser::{ ParserState, Token };
use std::collections::VecDeque;
use std::io::{ stdin, stdout, Write };
use std::env;

/// Get the next command from the user
fn next_command() -> String {
let mut input = String::new();
print!("> ");
let _=stdout().flush();
stdin().read_line(&mut input).expect("error: unable to read user input");

return input.trim().to_string();
}

fn main() {
let mut state : ParserState = ParserState::new();

// Load extensions
let results = state.extensions.load_all("./example_extensions");
for result in results {
if let Err(err) = result {
println!("{}", err);
}
}

// Preload command stack from arguments
let mut stack: VecDeque<String> = env::args().skip(1).collect();
if stack.is_empty() {
println!("Ready! Type expressions below!");
} else {
stack.push_back("exit".to_string());
}

loop {
// Make sure we have a command ready
if stack.is_empty() {
stack.push_back( next_command() );
}
let cmd = stack.pop_front().unwrap();

if cmd.len() == 0 {
continue;
} else if ["exit", "quit"].contains(&cmd.as_str()) {
break;
} else {
// Process the command
match Token::new(&cmd, &mut state) {
Ok(result) => println!("{}", result.text()),
Err(e) => eprintln!("{}: {}", cmd, e)
}
}
}
}
2 changes: 1 addition & 1 deletion examples/using_extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn main() -> Result<(), ParserError> {

for result in results {
if let Err(err) = result {
println!("Error: {}", err);
println!("{}", err);
}
}

Expand Down
41 changes: 41 additions & 0 deletions src/errors/arrays/array_empty.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use crate::Token;
use crate::errors::*;

use std::error::Error;
use std::fmt::{self, Display};

/// An error caused by attempting to use an empty array
#[derive(Debug, Clone)]
pub struct ArrayEmptyError {
src: ParserErrorSource
}
impl ArrayEmptyError {
/// Create a new instance of this error
///
/// # Arguments
/// * `src` - Token causing the error
pub fn new(src: &Token) -> Self {
Self {
src: ParserErrorSource::new(src)
}
}

/// Describes the location and text of the bad token
pub fn source(&self) -> &ParserErrorSource {
&self.src
}
}

impl Error for ArrayEmptyError {}
impl Display for ArrayEmptyError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "array is empty {}", self.src)?;
fmt::Result::Ok(())
}
}

impl Into<ParserError> for ArrayEmptyError {
fn into(self) -> ParserError {
ParserError::ArrayEmpty(self)
}
}
49 changes: 49 additions & 0 deletions src/errors/arrays/array_index.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use crate::Token;
use crate::errors::*;

use std::error::Error;
use std::fmt::{self, Display};

/// An error caused by attempting to use an out of bounds index on an array
#[derive(Debug, Clone)]
pub struct ArrayIndexError {
cause: usize,
src: ParserErrorSource
}
impl ArrayIndexError {
/// Create a new instance of this error
///
/// # Arguments
/// * `src` - Token causing the error
/// * `cause` - Reason for the error
pub fn new(src: &Token, cause: usize) -> Self {
Self {
cause: cause,
src: ParserErrorSource::new(src)
}
}

/// Return the cause of the error
pub fn cause(&self) -> &usize {
&self.cause
}

/// Describes the location and text of the bad token
pub fn source(&self) -> &ParserErrorSource {
&self.src
}
}

impl Error for ArrayIndexError {}
impl Display for ArrayIndexError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "array index {} out of bounds {}", self.cause, self.src)?;
fmt::Result::Ok(())
}
}

impl Into<ParserError> for ArrayIndexError {
fn into(self) -> ParserError {
ParserError::ArrayIndex(self)
}
}
41 changes: 41 additions & 0 deletions src/errors/arrays/array_length.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use crate::Token;
use crate::errors::*;

use std::error::Error;
use std::fmt::{self, Display};

/// An error caused by attempting to use arrays of different lengths
#[derive(Debug, Clone)]
pub struct ArrayLengthError {
src: ParserErrorSource
}
impl ArrayLengthError {
/// Create a new instance of this error
///
/// # Arguments
/// * `src` - Token causing the error
pub fn new(src: &Token) -> Self {
Self {
src: ParserErrorSource::new(src)
}
}

/// Describes the location and text of the bad token
pub fn source(&self) -> &ParserErrorSource {
&self.src
}
}

impl Error for ArrayLengthError {}
impl Display for ArrayLengthError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "array lengths incompatible {}", self.src)?;
fmt::Result::Ok(())
}
}

impl Into<ParserError> for ArrayLengthError {
fn into(self) -> ParserError {
ParserError::ArrayLength(self)
}
}
3 changes: 3 additions & 0 deletions src/errors/arrays/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod array_empty; pub use array_empty::*;
mod array_index; pub use array_index::*;
mod array_length; pub use array_length::*;
58 changes: 58 additions & 0 deletions src/errors/external/io.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use crate::Token;
use crate::errors::*;

use std::error::Error;
use std::fmt::{self, Display};

/// An error caused by filesystem issues
#[derive(Debug, Clone)]
pub struct IOError {
cause: String,
src: ParserErrorSource
}
impl IOError {
/// Create a new instance of this error
///
/// # Arguments
/// * `src` - Token causing the error
/// * `cause` - Reason for the error
pub fn new(src: &Token, cause: &str) -> Self {
Self {
cause: cause.to_string(),
src: ParserErrorSource::new(src)
}
}

/// Return the cause of the error
pub fn cause(&self) -> &str {
&self.cause
}

/// Describes the location and text of the bad token
pub fn source(&self) -> &ParserErrorSource {
&self.src
}

/// Create a new instance of this error from an existing error
///
/// # Arguments
/// * `src` - Token causing the error
/// * `error`- source error
pub fn from_ioerror(src: &Token, error: std::io::Error) -> Self {
Self::new(src, &error.to_string())
}
}

impl Error for IOError {}
impl Display for IOError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "IO error: {} {}", self.cause, self.src)?;
fmt::Result::Ok(())
}
}

impl Into<ParserError> for IOError {
fn into(self) -> ParserError {
ParserError::IO(self)
}
}
6 changes: 6 additions & 0 deletions src/errors/external/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#![warn(missing_docs)]

mod io; pub use io::*;
mod network; pub use network::*;
mod pest; pub use self::pest::*;
mod script; pub use script::*;
58 changes: 58 additions & 0 deletions src/errors/external/network.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use crate::Token;
use crate::errors::*;

use std::error::Error;
use std::fmt::{self, Display};

/// An error caused by network issues
#[derive(Debug, Clone)]
pub struct NetworkError {
cause: String,
src: ParserErrorSource
}
impl NetworkError {
/// Create a new instance of this error
///
/// # Arguments
/// * `src` - Token causing the error
/// * `cause` - Reason for the error
pub fn new(src: &Token, cause: &str) -> Self {
Self {
cause: cause.to_string(),
src: ParserErrorSource::new(src)
}
}

/// Return the cause of the error
pub fn cause(&self) -> &str {
&self.cause
}

/// Describes the location and text of the bad token
pub fn source(&self) -> &ParserErrorSource {
&self.src
}

/// Create a new instance of this error from an existing error
///
/// # Arguments
/// * `src` - Token causing the error
/// * `error`- source error
pub fn from_reqwesterror(src: &Token, error: reqwest::Error) -> Self {
Self::new(src, &error.to_string())
}
}

impl Error for NetworkError {}
impl Display for NetworkError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "network error: {} {}", self.cause, self.src)?;
fmt::Result::Ok(())
}
}

impl Into<ParserError> for NetworkError {
fn into(self) -> ParserError {
ParserError::Network(self)
}
}
41 changes: 41 additions & 0 deletions src/errors/external/pest.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use crate::Token;
use crate::errors::*;

use std::error::Error;
use std::fmt::{self, Display};

/// An error caused by a problem in parsing the syntax of an expression
#[derive(Debug, Clone)]
pub struct PestError {
src: ParserErrorSource
}
impl PestError {
/// Create a new instance of this error
///
/// # Arguments
/// * `src` - Token causing the error
pub fn new(src: &Token) -> Self {
Self {
src: ParserErrorSource::new(src)
}
}

/// Describes the location and text of the bad token
pub fn source(&self) -> &ParserErrorSource {
&self.src
}
}

impl Error for PestError {}
impl Display for PestError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "invalid syntax {}", self.src)?;
fmt::Result::Ok(())
}
}

impl Into<ParserError> for PestError {
fn into(self) -> ParserError {
ParserError::Pest(self)
}
}
Loading

0 comments on commit ae8763d

Please sign in to comment.