core: Take care of logical operator precedence
authorZeeshan Ali (Khattak) <zeeshanak@gnome.org>
Mon, 9 Nov 2009 17:59:12 +0000 (19:59 +0200)
committerZeeshan Ali (Khattak) <zeeshanak@gnome.org>
Mon, 9 Nov 2009 18:09:18 +0000 (20:09 +0200)
'and' takes precedence over 'or'.

src/rygel/rygel-search-criteria-parser.vala

index 3c95d2c..825ecf3 100644 (file)
@@ -86,6 +86,11 @@ internal class Rygel.SearchCriteriaParser : Object, StateMachine {
             } else if (this.expression is LogicalExpression) {
                 // The previous expression must have lacked the 2nd operand
                 var l_expression = this.expression as LogicalExpression;
+                if (l_expression.operand2 != null &&
+                    l_expression.operand2 is LogicalExpression) {
+                    l_expression = l_expression.operand2 as LogicalExpression;
+                }
+
                 l_expression.operand2 = expression;
             }
         } else if (stack_top is OpenningBrace) {
@@ -105,20 +110,38 @@ internal class Rygel.SearchCriteriaParser : Object, StateMachine {
 
         var stack_top = this.exp_stack.peek_tail ();
         if (stack_top != null) {
-            this.exp_stack.poll_tail (); // Pop last expression
-            this.exp_stack.poll_tail (); // Pop opening brace
+            if (lop == LogicalOperator.AND && stack_top is LogicalExpression) {
+                // AND has precedence over OR
+                var previous = stack_top as LogicalExpression;
 
-            // Put new Logical expression on the top of the stack
-            this.exp_stack.offer_tail (expression);
+                expression.operand1 = previous.operand2;
+                previous.operand2 = expression;
+            } else {
+                this.exp_stack.poll_tail (); // Pop last expression
+                this.exp_stack.poll_tail (); // Pop opening brace
+
+                // Put new Logical expression on the top of the stack
+                this.exp_stack.offer_tail (expression);
 
-            // Make the previous expression on the stack it's first argument
-            expression.operand1 = stack_top;
+                // Make the previous expression on the stack it's first argument
+                expression.operand1 = stack_top;
+            }
         } else {
-            // Nothing on the stack? This must mean this is a logical expression
-            // combining the expression tree and the next expression that we
-            // haven't yet parsed.
-            expression.operand1 = this.expression;
-            this.expression = expression;
+            // Nothing on the stack?
+            if (lop == LogicalOperator.AND &&
+                this.expression is LogicalExpression) {
+                // AND has precedence over OR
+                var previous = this.expression as LogicalExpression;
+
+                expression.operand1 = previous.operand2;
+                previous.operand2 = expression;
+            } else {
+                // This must mean this is a logical expression combining the
+                // expression tree and the next expression that we haven't yet
+                // parsed.
+                expression.operand1 = this.expression;
+                this.expression = expression;
+            }
         }
     }