-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
54 changed files
with
3,542 additions
and
67 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Oops, something went wrong.