]> git.phdru.name Git - phdru.name/cgi-bin/blog-ru/search-tags.git/commitdiff
Allow ' OR ' and ' or '
authorOleg Broytman <phd@phdru.name>
Sun, 22 Jun 2014 00:00:40 +0000 (04:00 +0400)
committerOleg Broytman <phd@phdru.name>
Sun, 22 Jun 2014 00:00:40 +0000 (04:00 +0400)
ChangeLog
TODO
parser/grammar
parser/parser.py
parser/test_parser.py

index b1310a4116b7044edb0a3536ac672276e5407a3f..5b2e9ebebd12b5ccfc1ceeb5e1af64e86f3ccf32 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,6 @@
 Version 0.3 (2014-06-??)
 
-   Allow '&&', '||', ' AND ' and ' and '.
+   Allow '&&', '||', ' AND ', ' and ', ' OR ' and ' or '.
 
 Version 0.2 (2014-06-06)
 
diff --git a/TODO b/TODO
index 2a96d7bf3c110d0530fd7e346c081d16f06ec84e..19857359a1d56ec6046a99389a0e605ce1d4eac6 100644 (file)
--- a/TODO
+++ b/TODO
@@ -1,5 +1,3 @@
-Allow ' OR ' and ' or '.
-
 Allow 'NOT ' and 'not '.
 
 Forbid stupid expressions: double NOT, extra parens and so on.
index 33326116c917ec5300f0df9b6ebbf95e44dfa65a..694437875fcc62548065bef2d439eeb3a9f41f46 100644 (file)
@@ -9,7 +9,7 @@
 #  TAG & (TAG | TAG)
 #  !(TAG | TAG)
 # Allowed operators: conjunction - & && AND and
-#                    disjunction - | ||
+#                    disjunction - | || OR or
 #                    negation    - !
 # This  is a simple version of the grammar and it allows
 # rather stupid expressions, like !!TAG or ((TAG)); in the future
@@ -32,6 +32,7 @@ expression : NAME
            | NOT_OP SP0 expression
            | expression SP0 OR_OP OR_OP SP0 expression
            | expression SP0 OR_OP SP0 expression
+           | l_expression or_word r_expression
            | expression_parens
 
 l_expression : expression_parens
@@ -49,6 +50,9 @@ expression_sp : expression SP1
 and_word : 'A' 'N' 'D'
          | 'a' 'n' 'd'
 
+or_word : 'O' 'R'
+        | 'o' 'r'
+
 SP0 : SP1 | empty
 
 empty :
index d278e4b210681817be55a9624083d3b7d62747a7..c28474bda544e2e213ef921f917e6f04e79c8b3a 100644 (file)
@@ -33,9 +33,12 @@ def p_expression_and(p):
     """expression : expression SP0 AND_OP SP0 expression"""
     p[0] = ('AND', p[1], p[5])
 
-def p_expression_and_word(p):
-    """expression : l_expression and_word r_expression"""
-    p[0] = ('AND', p[1], p[3])
+def p_expression_op_word(p):
+    """expression : l_expression op_word r_expression"""
+    if p[2] in ('AND', 'and'):
+        p[0] = ('AND', p[1], p[3])
+    elif p[2] in ('OR', 'or'):
+        p[0] = ('OR', p[1], p[3])
 
 def p_expression_not(p):
     """expression : NOT_OP SP0 expression"""
@@ -79,9 +82,9 @@ def p_expression_parens(p):
     """expression_parens : '(' SP0 expression SP0 ')'"""
     p[0] = ('PARENS', p[3])
 
-def p_and_word(p):
-    """and_word : NAME"""
-    if p[1] in ('AND', 'and'):
+def p_op_word(p):
+    """op_word : NAME"""
+    if p[1] in ('AND', 'and', 'OR', 'or'):
         p[0] = p[1]
     else:
         raise SyntaxError
index 50870e938e76dc5198df2d293a818fa1da4d6be4..d13e6e965e5d797f19d21af187b96ede78b6c698 100755 (executable)
@@ -33,6 +33,9 @@ class TestParser(unittest.TestCase):
         self.assertEqual(parser.parse('xxx and yyy'),
             ('AND', ('NAME', 'xxx'), ('NAME', 'yyy'))
         )
+        self.assertEqual(parser.parse('xxx or  yyy'),
+            ('OR', ('NAME', 'xxx'), ('NAME', 'yyy'))
+        )
 
     def test_05_bad_expression(self):
         self.assertIs(parser.parse('!(xxx&yyy'), None)