Skip to content

Commit

Permalink
Add expression parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Mroik committed May 7, 2024
1 parent 00519df commit 4fa3111
Showing 1 changed file with 55 additions and 10 deletions.
65 changes: 55 additions & 10 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fn parse_and(queue: &str) -> Result {
}
let queue = queue[1..].trim_start();

match parse_term(queue) {
match parse_expression(queue) {
Result::Failure => Result::Failure,
Result::Success(left, rest) => {
let queue = rest.trim_start();
Expand All @@ -55,7 +55,7 @@ fn parse_and(queue: &str) -> Result {
}
let queue = queue[1..].trim_start();

match parse_term(queue) {
match parse_expression(queue) {
Result::Failure => Result::Failure,
Result::Success(right, rest) => {
let queue = rest.trim_start();
Expand All @@ -78,7 +78,7 @@ fn parse_or(queue: &str) -> Result {
}
let queue = queue[1..].trim_start();

match parse_term(queue) {
match parse_expression(queue) {
Result::Failure => Result::Failure,
Result::Success(left, rest) => {
let queue = rest.trim_start();
Expand All @@ -87,7 +87,7 @@ fn parse_or(queue: &str) -> Result {
}
let queue = queue[1..].trim_start();

match parse_term(queue) {
match parse_expression(queue) {
Result::Failure => Result::Failure,
Result::Success(right, rest) => {
let queue = rest.trim_start();
Expand All @@ -110,7 +110,7 @@ fn parse_not(queue: &str) -> Result {
}
let queue = queue[1..].trim_start();

match parse_term(queue) {
match parse_expression(queue) {
Result::Failure => Result::Failure,
Result::Success(t, rest) => Result::Success(Proposition::new_not(&t), rest),
}
Expand All @@ -123,7 +123,7 @@ fn parse_implies(queue: &str) -> Result {
}
let queue = queue[1..].trim_start();

match parse_term(queue) {
match parse_expression(queue) {
Result::Failure => Result::Failure,
Result::Success(left, rest) => {
let queue = rest.trim_start();
Expand All @@ -132,7 +132,7 @@ fn parse_implies(queue: &str) -> Result {
}
let queue = queue[2..].trim_start();

match parse_term(queue) {
match parse_expression(queue) {
Result::Failure => Result::Failure,
Result::Success(right, rest) => {
let queue = rest.trim_start();
Expand All @@ -154,7 +154,7 @@ fn parse_iff(queue: &str) -> Result {
}
let queue = queue[1..].trim_start();

match parse_term(queue) {
match parse_expression(queue) {
Result::Failure => Result::Failure,
Result::Success(left, rest) => {
let queue = rest.trim_start();
Expand All @@ -163,7 +163,7 @@ fn parse_iff(queue: &str) -> Result {
}
let queue = queue[3..].trim_start();

match parse_term(queue) {
match parse_expression(queue) {
Result::Failure => Result::Failure,
Result::Success(right, rest) => {
let queue = rest.trim_start();
Expand All @@ -178,9 +178,37 @@ fn parse_iff(queue: &str) -> Result {
}
}

fn parse_expression(queue: &str) -> Result {
let queue = queue.trim_start();
match parse_absurdum(queue) {
Result::Success(r, rest) => Result::Success(r, rest),
Result::Failure => match parse_term(queue) {
Result::Success(r, rest) => Result::Success(r, rest),
Result::Failure => match parse_and(queue) {
Result::Success(r, rest) => Result::Success(r, rest),
Result::Failure => match parse_or(queue) {
Result::Success(r, rest) => Result::Success(r, rest),
Result::Failure => match parse_not(queue) {
Result::Success(r, rest) => Result::Success(r, rest),
Result::Failure => match parse_implies(queue) {
Result::Success(r, rest) => Result::Success(r, rest),
Result::Failure => match parse_iff(queue) {
Result::Success(r, rest) => Result::Success(r, rest),
Result::Failure => Result::Failure,
},
},
},
},
},
},
}
}

#[cfg(test)]
mod tests {
use super::{parse_absurdum, parse_and, parse_iff, parse_implies, parse_not, parse_or, parse_term, Result};
use super::{
parse_absurdum, parse_and, parse_expression, parse_iff, parse_implies, parse_not, parse_or, parse_term, Result
};
use crate::fitch::Proposition;

#[test]
Expand Down Expand Up @@ -275,4 +303,21 @@ mod tests {
}
}
}

#[test]
fn parse_expression_test() {
let a = Proposition::new_term("A");
let b = Proposition::new_term("B");
let left = Proposition::new_or(&a, &b);
let right = Proposition::new_and(&a, &b);
let ris = Proposition::new_and(&left, &right);
let queue = " ((A v B) ^ (A ^ B)) ";
match parse_expression(queue) {
Result::Failure => assert!(false),
Result::Success(p, rest) => {
assert_eq!(p, ris);
assert_eq!(rest, " ");
}
}
}
}

0 comments on commit 4fa3111

Please sign in to comment.