-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.py
149 lines (122 loc) · 4.94 KB
/
parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env python3
import pyparsing as pp
import unittest
import tree
class ParserTree(tree.Tree):
def __init__(self):
tree.Tree.__init__(self)
def pp_addAtom(self, text, loc, toks):
print("Atom")
print("text: {}, loc: {}, toks: {}".format(text, loc, toks))
super().addAtom(toks[0])
def pp_addAtomNode(self, text, loc, toks):
print("AtomNode")
print("text: {}, loc: {}, toks: {}".format(text, loc, toks))
super().addAtom(toks[0])
super().addNode()
def pp_addOp(self, text, loc, toks):
print("Op")
print("text: {}, loc: {}, toks: {}".format(text, loc, toks))
super().addOp(toks[0])
def pp_addNode(self, text, loc, toks):
print("Node")
print("text: {}, loc: {}, toks: {}".format(text, loc, toks))
super().addNode()
def pp_addNot(self, text, loc, toks):
print("Not")
print("text: {}, loc: {}, toks: {}".format(text, loc, toks))
super().addNot()
class Parser:
"""
Defines the parsing rules. Attention: The parse results will be stored
in the list trees. Further parsing will append its results
to the list. So you get the entire list of older results if you look
into trees.
"""
def __init__(self):
[self.rules, self.tree] = self._defineRules()
def scan(self, text):
"""Scan a given text. Returns a list with the resulting trees."""
for data, start, end in self.rules.scanString(text):
pass
return self.tree
def parse(self, text):
"""Parses a given text. Returns a list with the resulting trees."""
print(self.rules.parseString(text))
return self.tree
def _defineRules(self):
"""
Creates the parsing rules and returns the defines ParseElement.
(see pyparsing.py)
"""
thetree = ParserTree()
# # Forwards
# paren_signed_node = pp.Forward()
# more_paren_signed_node = paren_signed_node ^ ( "(" + paren_signed_node + ")")
# atom_node = pp.Word(pp.alphanums).setParseAction(thetree.pp_addAtomNode)
# op = ( pp.Literal("|") ^ pp.Literal("&") ).setParseAction(thetree.pp_addOp)
# term_node = ( "(" + more_paren_signed_node + op + more_paren_signed_node + ")").setParseAction(thetree.pp_addNode)
# node = atom_node ^ term_node
# signed_node = node ^ ("!" + node).setParseAction(thetree.pp_addNot)
# paren_signed_node << signed_node ^ ( "(" + signed_node + ")")
# parens
lparen = pp.Literal("(").suppress()
rparen = pp.Literal(")").suppress()
# Forwards
paren_signed_node = pp.Forward()
atom_node = pp.Word(pp.alphanums).setParseAction(thetree.pp_addAtomNode)
op = ( pp.Literal("|") | pp.Literal("&") ).setParseAction(thetree.pp_addOp)
term_node = pp.Group(( lparen + paren_signed_node + op + paren_signed_node + rparen ).setParseAction(thetree.pp_addNode))
node = ( atom_node | term_node )
signed_node = ( node | pp.Group(("!" + node).setParseAction(thetree.pp_addNot)) )
paren_signed_node << ( signed_node | ( lparen + paren_signed_node + rparen ) )
term = paren_signed_node
return [term, thetree]
def clear(self):
self.tree.clear()
class ParserTest(unittest.TestCase):
# def test_parse_without_parens(self):
# parser = Parser()
# text = " hund | katz "
# parser.scan(text)
# self.assertEqual(str(parser.tree).replace(" ", ""), text.replace(" ", ""))
def test_parse1(self):
parser = Parser()
text = "Music"
parser.parse(text)
self.assertEqual(str(parser.tree).replace(" ", ""), "Music")
def test_parse2(self):
parser = Parser()
text = " Music "
parser.parse(text)
self.assertEqual(str(parser.tree).replace(" ", ""), "Music")
def test_parse3(self):
parser = Parser()
text = "!Music"
parser.parse(text)
self.assertEqual(str(parser.tree).replace(" ", ""), "!Music")
def test_parse4(self):
parser = Parser()
text = "(Music)"
parser.parse(text)
self.assertEqual(str(parser.tree).replace(" ", ""), "Music")
def test_parse5(self):
parser = Parser()
text = "(!Music)"
parser.parse(text)
self.assertEqual(str(parser.tree).replace(" ", ""), "!Music")
def test_parse6(self):
parser = Parser()
text = "((Music))"
parser.parse(text)
self.assertEqual(str(parser.tree).replace(" ", ""), "Music")
def test_parse7(self):
parser = Parser()
text = "(!(Music))"
parser.parse(text)
self.assertEqual(str(parser.tree).replace(" ", ""), "Music")
# def test_parse(self):
# parser = Parser()
# text = " ! ( ! ( (Music) | Photo )| Jara ) "
# parser.parse(text)
# self.assertEqual(str(parser.tree).replace(" ", ""), text.replace(" ", ""))