Permalink
Browse files

formating

  • Loading branch information...
1 parent bc4b324 commit 482739d2098eee1161ff0f346fcd826d3569c6ee @uberj committed Oct 24, 2012
Showing with 33 additions and 104 deletions.
  1. +3 −1 invfilter.py
  2. +7 −8 invlex.py
  3. +15 −90 invparse.py
  4. +5 −5 parser_tests.py
  5. +3 −0 utils.py
View
4 invfilter.py
@@ -10,6 +10,7 @@ def __repr__(self):
def compile_Q(self, ntype):
pass
+
class TextFilter(_Filter):
def __init__(self, rvalue):
self.value = rvalue
@@ -18,10 +19,12 @@ def __init__(self, rvalue):
def compile_Q(self, value):
pass
+
class REFilter(TextFilter):
def compile_Q(self, value):
pass
+
class DirectiveFilter(_Filter):
def __init__(self, rvalue, directive, dvalue):
self.value = rvalue
@@ -31,4 +34,3 @@ def __init__(self, rvalue, directive, dvalue):
def compile_Q(self, directive, dvalue):
pass
-
View
15 invlex.py
@@ -3,18 +3,18 @@
import ply.lex as lex
from invfilter import *
+
class InvLexer(object):
tokens = (
- 'TEXT','DIRECTIVE', 'RE',
- 'AND','NOT','OR',
- 'LPAREN','RPAREN',
- )
+ 'TEXT', 'DIRECTIVE', 'RE',
+ 'AND', 'NOT', 'OR',
+ 'LPAREN', 'RPAREN',
+ )
# Tokens
-
- t_LPAREN = r'\('
- t_RPAREN = r'\)'
+ t_LPAREN = r'\('
+ t_RPAREN = r'\)'
def t_NOT(self, t):
r'-'
@@ -51,7 +51,6 @@ def t_TEXT(self, t):
t.value = TextFilter(t.value)
return t
-
# Ignored characters
t_ignore = " \t"
View
105 invparse.py
@@ -2,6 +2,7 @@
from invlex import InvLexer
import ply.yacc as yacc
+
class BOP(object):
def __init__(self, value, l_child, r_child):
self.value = value
@@ -14,6 +15,7 @@ def __str__(self):
def __repr__(self):
return "<BOP {0}>".format(self.value)
+
class UOP(object):
def __init__(self, value, child):
self.value = value
@@ -31,20 +33,24 @@ def __repr__(self):
('left', 'IMPAND'),
('left', 'PAREN'),
('right', 'NOT'),
- )
+)
+
def p_statement_expr(p):
'statement : expression'
p[0] = p[1]
+
def p_expression_expr_term(p):
'expression : expression term %prec IMPAND'
p[0] = BOP('AND', p[1], p[2])
+
def p_expression_term(p):
'expression : term'
p[0] = p[1]
+
def p_expression_uminus(p):
'expression : NOT expression'
p[0] = UOP('NOT', p[2])
@@ -67,128 +73,47 @@ def p_expression_binop(p):
p[0] = BOP('AND', p[1], p[3])
-
def p_expression_group(p):
'expression : LPAREN expression RPAREN %prec PAREN'
p[0] = p[2]
+
def p_term_DIRECTIVE(p):
'term : DIRECTIVE'
p[0] = p[1]
+
def p_term_TEXT(p):
'term : TEXT'
p[0] = p[1]
+
def p_term_RE(p):
'term : RE'
p[0] = p[1]
+
def p_error(p):
if not p:
print "Syntax error at end of line"
else:
print "Syntax error at '{0}'".format(p.value)
+
def build_parser():
lexer = InvLexer()
lexer.build_lexer()
tokens = lexer.tokens
p = yacc.yacc()
+
def parse(s):
return yacc.parse(s, lexer=lexer.lexer)
return parse
+
if __name__ == "__main__":
+ # Just build the parser to see any errors.
lexer = InvLexer()
lexer.build_lexer()
tokens = lexer.tokens
p = yacc.yacc()
-
- """
- s = "a b"
- print
- print '-' * 10
- s = "a OR -b AND c"
- print s
- yacc.parse(s, lexer=lexer.lexer)
- print
- print '-' * 10
- print s
- s = "(a OR b) AND c"
- p.parse(s)
- print
- print '-' * 10
-
- s = "-(a OR b) AND c"
- print s
- p.parse(s)
- print
- print '-' * 10
-
- s = "type=:a vlan=:b"
- print s
- p.parse(s)
- print
- print '-' * 10
-
- s = "/a"
- print s
- p.parse(s)
- print
- print '-'*10
-
- s = "(a) (b)"
- print s
- p.parse(s)
- print
- print '-'*10
-
- s = "a (b)"
- print s
- p.parse(s)
- print
- print '-'*10
-
- s = "(a) b"
- print s
- p.parse(s)
- print
- print '-'*10
-
- s = "(((a)) (b))"
- print s
- p.parse(s)
- print
- print '-'*10
-
- s = "(a b c)"
- print s
- p.parse(s)
- print
- print '-'*10
-
- s = "a b d OR c f g"
- print s
- p.parse(s)
- print
- print '-'*10
-
- s = "(a (b c))"
- print s
- p.parse(s)
- print
- print '-'*10
-
- s = "a -(c OR b)"
- print s
- p.parse(s)
- print
- print '-'*10
-
- s = "a AND"
- print s
- p.parse(s)
- print
- print '-'*10
- """
View
10 parser_tests.py
@@ -3,15 +3,17 @@
from invparse import build_parser
from utils import make_stack
+
class TestParser(unittest.TestCase):
def compare(self, ss, expected_stack_str):
parse = build_parser()
root_node = parse(ss)
stack = make_stack(root_node)
- actual = ' '.join(stack)
+ actual = ' '.join(stack)
self.assertEqual(actual, expected_stack_str, msg="Actual: {0} "
- "Excpected: {1} Parsing: {2}".format(actual, expected_stack_str,
- ss))
+ "Excpected: {1} Parsing: {2}".format(actual,
+ expected_stack_str, ss))
+
def test1(self):
ss = "(a AND (b OR (c d)))"
exp = 'a b c d AND OR AND'
@@ -77,7 +79,6 @@ def test24(self):
exp = 'a b c AND OR'
self.compare(ss, exp)
-
def test13(self):
ss = "(a (b OR c))"
exp = 'a b c OR AND'
@@ -134,6 +135,5 @@ def test23(self):
self.compare(ss, exp)
-
if __name__ == "__main__":
unittest.main()
View
3 utils.py
@@ -1,5 +1,6 @@
import pdb
+
def make_stack(node):
def _make_stack(stack, node):
if not node:
@@ -20,9 +21,11 @@ def _make_stack(stack, node):
_make_stack(stack, node)
return stack
+
def print_stack(stack):
print ' '.join([token.value for token in stack])
+
def istype(a, b):
if not a:
return False

0 comments on commit 482739d

Please sign in to comment.