-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSSParser.py
More file actions
114 lines (89 loc) · 3.14 KB
/
CSSParser.py
File metadata and controls
114 lines (89 loc) · 3.14 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
from HTMl_Tags import TagSelector, DescendantSelector
class CSSParser:
def __init__(self, s):
self.s = s
self.index = 0 # starting index of whitespace
# add to index for all whitespaces
def whitespace(self):
while self.index < len(self.s) and self.s[self.index].isspace():
self.index += 1
# parse property names
def word(self):
start = self.index
while self.index < len(self.s):
if self.s[self.index].isalnum() or self.s[self.index] in "#-.%":
self.index += 1
else:
break
if not (self.index > start):
raise Exception("Whitespace Parsing Error")
return self.s[start:self.index]
# check for literal punctuation
def literal(self, literal):
if not (self.index < len(self.s) and self.s[self.index] == literal):
raise Exception("Parsing error")
self.index += 1
def pair(self):
prop = self.word()
self.whitespace()
self.literal(":")
self.whitespace()
val = self.word()
return prop.casefold(), val
def body(self):
pairs = {}
while self.index < len(self.s) and self.s[self.index] != "}":
try:
prop, val = self.pair()
pairs[prop.casefold()] = val # set hash val
self.whitespace()
self.literal(";")
self.whitespace()
except Exception:
reason = self.ignore([";", "}"])
if reason == ";":
self.literal(";")
self.whitespace()
else:
break
return pairs
# deals with error from author or non supported items
def ignore(self, chars):
while self.index < len(self.s):
if self.s[self.index] in chars:
return self.s[self.index]
else:
self.index += 1
# no errors
return None
# selector
def selector(self) -> TagSelector:
out = TagSelector(self.word().casefold())
self.whitespace()
while self.index < len(self.s) and self.s[self.index] != "{":
tag = self.word()
descendant = TagSelector(tag.casefold())
out = DescendantSelector(out, descendant)
self.whitespace()
return out
# parses CSS files
def parse(self):
rules = []
while self.index < len(self.s):
try:
self.whitespace()
selector = self.selector() # get selector for CSS
self.literal("{")
self.whitespace()
body = self.body() # get body for CSS from selector
self.literal("}")
rules.append((selector, body)) # append to rules
except Exception:
# parser error
reason = self.ignore(["}"])
if reason == "}":
self.literal("}")
self.whitespace()
else:
break
return rules