-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse4.py
More file actions
100 lines (91 loc) · 2.79 KB
/
parse4.py
File metadata and controls
100 lines (91 loc) · 2.79 KB
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
from simpleparse.common import numbers, strings, comments
from simpleparse.parser import Parser as SimpleParse
from element import Element
class Parser:
def __init__(self):
self.line = 0
self.stack = []
self.depth = 0
declaration = r'''
file ::= line+
line ::= depth, expr+, '\n'?
depth ::= [ \t]*
>expr< ::= symbol, ws*
<ws> ::= whitespace
>symbol< ::= method/element
>element< ::= attr/cat/iden/name/num/content
method ::= "def:", whitespace*, (element, whitespace*)+
name ::= [a-zA-Z]+, [0-9]*
cat ::= ".", name
iden ::= "#", name
attr ::= name, "=", !, value
value ::= name/[0-9]+/content
>num< ::= "*", whitespace*, !, multiples
multiples ::= [0-9]+
content ::= ('"',!,-'"'*,'"')/("'",!,-"'"*,"'")
'''
self.parser = SimpleParse(declaration, "line")
self.parse()
def parseLine(self,line):
b={}
success, children, nextcharacter = self.parser.parse(line)
for child in children:
if child[0] == "attr":
attr = line[child[1]:child[2]].split('=')
if not child[0] in b:
b[child[0]] = {}
b[child[0]][attr[0]] = attr[1]
elif child[0] == "content":
content = line[child[1]:child[2]][1:-1]
if not child[0] in b:
b[child[0]] = []
b[child[0]] += [content]
else:
if not child[0] in b:
b[child[0]] = ""
b[child[0]]+= line[child[1]:child[2]]
return self.compileLine(b)
def compileLine(self,line):
self.compiled = {
"depth": len(line['depth']) if 'depth' in line else 0,
"name": line['name'] if 'name' in line else '',
"cat": line['cat'] if 'cat' in line else '',
"iden": line['iden'] if 'iden' in line else '',
"attr": line['attr'] if 'attr' in line else [],
"children": [],
"content": line['content'] if 'content' in line else [''],
"closed": True,
"multiples":int(line['multiples']) if 'multiples' in line else 1
}
# print self.compiled['content']
return self.compiled
def lastElement(self):
return self.stack[-1]
def addElement(self,element,first=False):
if first is True:
self.toplevel = element
else:
self.lastElement().addChild(element)
self.stack += [element]
def parse(self):
page = open('asptest.txt','r').readlines()
for line in page:
self.line += 1
parsed = self.parseLine(line)
self.depth = parsed['depth']
element = Element(**parsed)
if self.stack == []:
self.addElement(element,True)
elif self.lastElement().depth < self.depth:
self.addElement(element)
elif self.lastElement().depth > self.depth:
while self.lastElement().depth >= self.depth:
self.stack.pop()
self.addElement(element)
elif self.lastElement().depth == self.depth:
# print self.lastElement().name, self.line
self.stack.pop()
# print self.lastElement().name, self.line
self.addElement(element)
self.toplevel.write()
a = Parser()