Compare commits

..

No commits in common. "732037c4a9ce5fa5a4ab32ed60647268ab2778f6" and "a088388e198940a8cf753d172d075dadf30ef545" have entirely different histories.

2 changed files with 20 additions and 54 deletions

View File

@ -72,45 +72,24 @@
//! 1. `function() print(` //! 1. `function() print(`
//! 1. `Hi!` //! 1. `Hi!`
//! 1. `) end` //! 1. `) end`
use std::{fmt::Display, str::FromStr}; use std::fmt::Display;
use parsing::{Rule, TrinitryParser}; use pest::{error::Error, Parser};
use pest::error::Error; use pest_derive::Parser;
mod parsing; #[derive(Parser)]
#[grammar = "trinitry.pest"]
pub struct TrinitryInvokation { pub struct Trinitry {
command: String, command: String,
arguments: Vec<String>, arguments: Vec<String>,
} }
impl TrinitryInvokation { impl Trinitry {
pub fn new(input: &str) -> Result<Self, <Self as FromStr>::Err> { pub fn new(input: &str) -> Result<Self, Error<Rule>> {
input.parse() let parsed = Self::parse(Rule::trinitry, input)?;
}
pub fn command(&self) -> &str {
&self.command
}
pub fn arguments(&self) -> &[String] {
&self.arguments
}
}
impl FromStr for TrinitryInvokation {
type Err = Error<Rule>;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let parsed = TrinitryParser::new(s)?;
Ok(Self::from(parsed))
}
}
impl From<TrinitryParser<'_>> for TrinitryInvokation {
fn from(parsed: TrinitryParser) -> Self {
let command = { let command = {
let command: Vec<_> = parsed.0.clone().find_tagged("command").collect(); let command: Vec<_> = parsed.clone().find_tagged("command").collect();
// Ensure that we have only one command // Ensure that we have only one command
// This should be ensured by the grammar, thus the 'debug_assert' // This should be ensured by the grammar, thus the 'debug_assert'
@ -123,9 +102,9 @@ impl From<TrinitryParser<'_>> for TrinitryInvokation {
.expect("This should contain exactly one element") .expect("This should contain exactly one element")
.to_owned() .to_owned()
}; };
let arguments: Vec<_> = parsed.0.clone().find_tagged("argument").collect(); let arguments: Vec<_> = parsed.clone().find_tagged("argument").collect();
Self { Ok(Trinitry {
command: command.as_str().to_owned(), command: command.as_str().to_owned(),
arguments: arguments arguments: arguments
.iter() .iter()
@ -155,11 +134,11 @@ impl From<TrinitryParser<'_>> for TrinitryInvokation {
arg.to_owned() arg.to_owned()
}) })
.collect(), .collect(),
} })
} }
} }
impl Display for TrinitryInvokation { impl Display for Trinitry {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if self.arguments.is_empty() { if self.arguments.is_empty() {
f.write_str(&self.command) f.write_str(&self.command)
@ -177,12 +156,12 @@ mod tests;
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use crate::TrinitryInvokation; use crate::Trinitry;
#[test] #[test]
fn parse_cmd() { fn parse_cmd() {
let string = "quit"; let string = "quit";
let p = TrinitryInvokation::new(string).unwrap_or_else(|e| { let p = Trinitry::new(string).unwrap_or_else(|e| {
panic!("{}", e); panic!("{}", e);
}); });
assert_eq!(&p.command, "quit"); assert_eq!(&p.command, "quit");
@ -192,7 +171,7 @@ mod test {
#[test] #[test]
fn parse_arg_clean() { fn parse_arg_clean() {
let string = r##"lua print("Hi")"##; let string = r##"lua print("Hi")"##;
let p = TrinitryInvokation::new(string).unwrap_or_else(|e| { let p = Trinitry::new(string).unwrap_or_else(|e| {
panic!("{}", e); panic!("{}", e);
}); });
assert_eq!(&p.command, "lua"); assert_eq!(&p.command, "lua");
@ -202,7 +181,7 @@ mod test {
#[test] #[test]
fn parse_arg_quote() { fn parse_arg_quote() {
let string = r##"write "some 'file' name""##; let string = r##"write "some 'file' name""##;
let p = TrinitryInvokation::new(string).unwrap_or_else(|e| { let p = Trinitry::new(string).unwrap_or_else(|e| {
panic!("{}", e); panic!("{}", e);
}); });
assert_eq!(&p.command, "write"); assert_eq!(&p.command, "write");
@ -212,7 +191,7 @@ mod test {
#[test] #[test]
fn parse_arg_single_quote() { fn parse_arg_single_quote() {
let string = r##"write 'some "file" name'"##; let string = r##"write 'some "file" name'"##;
let p = TrinitryInvokation::new(string).unwrap_or_else(|e| { let p = Trinitry::new(string).unwrap_or_else(|e| {
panic!("{}", e); panic!("{}", e);
}); });
assert_eq!(&p.command, "write"); assert_eq!(&p.command, "write");
@ -222,7 +201,7 @@ mod test {
#[test] #[test]
fn parse_arg_multi() { fn parse_arg_multi() {
let string = r##"write 'some "file" name' "other name" last"##; let string = r##"write 'some "file" name' "other name" last"##;
let p = TrinitryInvokation::new(string).unwrap_or_else(|e| { let p = Trinitry::new(string).unwrap_or_else(|e| {
panic!("{}", e); panic!("{}", e);
}); });

View File

@ -1,13 +0,0 @@
use pest::{error::Error, iterators::Pairs, Parser};
use pest_derive::Parser;
#[derive(Parser)]
#[grammar = "trinitry.pest"]
pub(crate) struct TrinitryParser<'a>(pub(crate) Pairs<'a, Rule>);
impl<'a> TrinitryParser<'a> {
pub fn new(input: &'a str) -> Result<Self, Error<Rule>> {
let parsed = Self::parse(Rule::trinitry, input)?;
Ok(Self(parsed))
}
}