]> git.phdru.name Git - phdru.name/cgi-bin/blog-ru/search-tags.git/blobdiff - parser/parser.py
Allow 'NOT ' and 'not '
[phdru.name/cgi-bin/blog-ru/search-tags.git] / parser / parser.py
index cf463da5b63433082a0fd360b23586871232506e..e214698a60d156e3721ba851ca1371a17feba260 100644 (file)
@@ -4,9 +4,11 @@ from ply import lex, yacc
 
 literals = '()'
 
-tokens = ('NAME', 'AND_OP', 'OR_OP', 'NOT_OP', 'SP1')
+tokens = ('OP_WORD', 'NAME', 'AND_OP', 'OR_OP', 'NOT_OP', 'SP1')
 
-t_NAME = '([a-z][a-z0-9_]*)|AND|OR|NOT'
+t_OP_WORD = '(AND|and|OR|or|NOT|not)'
+
+t_NAME = '([a-z][a-z0-9_]*)'
 
 t_AND_OP = '&'
 
@@ -39,6 +41,8 @@ def p_expression_op_word(p):
         p[0] = ('AND', p[1], p[3])
     elif p[2] in ('OR', 'or'):
         p[0] = ('OR', p[1], p[3])
+    else:
+        raise ValueError(p)
 
 def p_expression_or_or(p):
     """expression : expression SP0 OR_OP OR_OP SP0 expression"""
@@ -52,6 +56,13 @@ def p_expression_not(p):
     """expression : NOT_OP SP0 expression"""
     p[0] = ('NOT', p[3])
 
+def p_expression_not_word(p):
+    """expression : op_word r_expression"""
+    if p[1] in ('NOT', 'not'):
+        p[0] = ('NOT', p[2])
+    else:
+        raise ValueError(p)
+
 def p_expression_in_parens(p):
     """expression : expression_parens"""
     p[0] = p[1]
@@ -83,8 +94,8 @@ def p_expression_parens(p):
     p[0] = ('PARENS', p[3])
 
 def p_op_word(p):
-    """op_word : NAME"""
-    if p[1] in ('AND', 'and', 'OR', 'or'):
+    """op_word : OP_WORD"""
+    if p[1] in ('AND', 'and', 'OR', 'or', 'NOT', 'not'):
         p[0] = p[1]
     else:
         raise SyntaxError