Skip to content

Commit

Permalink
Merge pull request #19 from Jkutkut/17-bug-operator-preference
Browse files Browse the repository at this point in the history
bugfix GH-17 operator preference
  • Loading branch information
Jkutkut authored Jul 23, 2024
2 parents 994deef + 8a8e248 commit ee7757f
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "osmia"
version = "1.13.1"
version = "1.13.2"
edition = "2021"
authors = ["Jkutkut"]

Expand Down
55 changes: 34 additions & 21 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,22 @@ use crate::model::{
/// Continue → "{{" "continue" "}}" ;
///
/// json → object | array | expression ;
/// jsonObject → "{" ( Literal ":" json "," )* ( Literal ":" json )? "}" ;
/// jsonArray → "[" ( json "," )* ( json )? "]" ;
/// jsonObject → "{" ( Literal ":" json "," )* ( Literal ":" json )? "}" ;
/// jsonArray → "[" ( json "," )* ( json )? "]" ;
/// ListOrVariable → Variable | array ;
///
/// expression → equality ;
/// expression → logic_or ;
/// logic_or → logic_and ( "||" logic_and )* ;
/// logic_and → equality ( "&&" equality )* ;
/// equality → bitwise ( ( "!=" | "==" ) bitwise )* ;
/// bitwise → comparison ( ( "&" | "|" | "^" ) comparison )* ;
/// comparison → bitshift ( ( ">" | ">=" | "<" | "<=" ) bitshift )* ;
/// bitshift → bool_op ( ( ">>" | "<<" ) bool_op )* ;
/// bool_op → term ( ( "&&" | "||" ) term )* ;
/// bitshift → term ( ( ">>" | "<<" ) term )* ;
/// term → factor ( ( "-" | "+" ) factor )* ;
/// factor → unary ( ( "/" | "*" ) unary )* ;
/// unary → ( "!" | "-" | "+" ) unary | primary ;
/// primary → Literal | Variable | grouping;
/// grouping → "(" expression ")" ;
/// primary → Literal | Variable | grouping;
/// grouping → "(" expression ")" ;
/// ```
pub struct Parser {
tokens: Vec<Token>,
Expand Down Expand Up @@ -398,7 +399,31 @@ impl Parser {

impl Parser {
fn expression(&mut self) -> Result<Expression, String> {
self.equality()
self.logic_or()
}

fn logic_or(&mut self) -> Result<Expression, String> {
let mut expr = self.logic_and()?;
while self.match_and_advance(&[Token::Or]) {
let operator = self.get_previous().clone();
let right = self.logic_and()?;
expr = Self::new_binary(
expr, operator, right
)?;
}
Ok(expr)
}

fn logic_and(&mut self) -> Result<Expression, String> {
let mut expr = self.equality()?;
while self.match_and_advance(&[Token::And]) {
let operator = self.get_previous().clone();
let right = self.equality()?;
expr = Self::new_binary(
expr, operator, right
)?;
}
Ok(expr)
}

fn equality(&mut self) -> Result<Expression, String> {
Expand Down Expand Up @@ -443,22 +468,10 @@ impl Parser {
}

fn bitshift(&mut self) -> Result<Expression, String> {
let mut expr = self.bool_op()?;
let mut expr = self.term()?;
while self.match_and_advance(&[
Token::BitShiftLeft, Token::BitShiftRight
]) {
let operator = self.get_previous().clone();
let right = self.bool_op()?;
expr = Self::new_binary(
expr, operator, right
)?;
}
Ok(expr)
}

fn bool_op(&mut self) -> Result<Expression, String> {
let mut expr = self.term()?;
while self.match_and_advance(&[Token::And, Token::Or]) {
let operator = self.get_previous().clone();
let right = self.term()?;
expr = Self::new_binary(
Expand Down
28 changes: 28 additions & 0 deletions src/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -632,3 +632,31 @@ macro_tests!(
"Nothing is null"
)
);

macro_tests!(
test_io,
(
gh_17_01,
r#"
{{if (foo != null) && foo}}
1. foo does exists
{{fi}}
{{if foo != null && foo}}
2. foo does exists
{{fi}}"#.trim(),
None,
""
),
(
gh_17_02,
r#"
{{if (foo != null) && foo}}
1. foo does exists
{{fi}}
{{if foo != null && foo}}
2. foo does exists
{{fi}}"#.trim(),
Some(r#"{"foo": "Something not null"}"#),
"1. foo does exists\n2. foo does exists\n"
)
);

0 comments on commit ee7757f

Please sign in to comment.