Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

minor changes. too tired to write commit message.

  • Loading branch information...
commit c269de996c30393e539eb3d04b7e4ba55525160f 1 parent 2476a6b
@uberj authored
Showing with 55 additions and 5 deletions.
  1. +35 −0 django_compile.py
  2. +5 −5 invlex.py
  3. +15 −0 parser_tests.py
View
35 django_compile.py
@@ -0,0 +1,35 @@
+def compile_Q(stack):
+ q_stack = []
+ while True:
+ try:
+ top = stack.pop()
+ except IndexError:
+ break
+ if (istype(top, 'TERM') or istype(top, 'DIRECTIVE') or
+ istype(top, 'RE'):
+ q_stack.append(top.compile_Q())
+ elif istype(top, 'NOT'):
+ term = q_stack.pop()
+ q_stack.append(lambda Q: ~Q, term)
+ continue
+ elif istype(top, 'AND') or istype(top, 'OR'):
+ t1 = q_stack.pop()
+ t2 = q_stack.pop()
+ q_result = []
+ for qi, qj in izip(t1, t2):
+ if istype(top, 'AND'):
+ if qi and qj:
+ q_result.append(qi & qj)
+ else: # Something AND nothing is nothing
+ q_result.append(None)
+ elif istype(top, 'OR'):
+ if qi and qj:
+ q_result.append(qi | qj)
+ elif qi:
+ q_result.append(qi)
+ elif qj:
+ q_result.append(qj)
+ else:
+ q_result.append(None)
+ q_stack.append(q_result)
+ return q_stack
View
10 invlex.py
@@ -30,8 +30,8 @@ def t_OR(self, t):
return t
def t_DIRECTIVE(self, t):
- r'([a-zA-Z_]+)=:([a-zA-Z0-9_\.]+)'
- r = r'([a-zA-Z_]+)=:([a-zA-Z0-9_\.]+)'
+ r'([a-zA-Z_]+)=:([a-zA-Z0-9_\./]+)'
+ r = r'([a-zA-Z_]+)=:([a-zA-Z0-9_\./:]+)'
match = re.compile(r).match(t.value)
directive = match.groups(1)[0]
dvalue = match.groups(1)[1]
@@ -39,15 +39,15 @@ def t_DIRECTIVE(self, t):
return t
def t_RE(self, t):
- r'/[a-zA-Z0-9_\.]+'
- r = r'/([a-zA-Z0-9_\.]+)'
+ r'/\S+'
+ r = r'/(\S+)'
match = re.compile(r).match(t.value)
rvalue = match.groups(1)[0]
t.value = REFilter(rvalue)
return t
def t_TEXT(self, t):
- r'[a-zA-Z0-9_\.]+'
+ r'[a-zA-Z0-9_\.:-]+'
t.value = TextFilter(t.value)
return t
View
15 parser_tests.py
@@ -134,6 +134,21 @@ def test23(self):
exp = 'a b AND d AND c f AND g AND OR'
self.compare(ss, exp)
+ def test24(self):
+ ss = "-type=:A"
+ exp = "type=:A NOT"
+ self.compare(ss, exp)
+
+ def test25(self):
+ ss = "foopy -type=:A"
+ exp = "foopy type=:A NOT AND"
+ self.compare(ss, exp)
+
+ def test26(self):
+ ss = "ix-mn-xp32-y-xxxx.inband.foo.bar.com"
+ exp = "ix-mn-xp32-y-xxxx.inband.foo.bar.com"
+ self.compare(ss, exp)
+
if __name__ == "__main__":
unittest.main()
Please sign in to comment.
Something went wrong with that request. Please try again.