-
Notifications
You must be signed in to change notification settings - Fork 0
/
css_parser2.py~
209 lines (181 loc) · 4.38 KB
/
css_parser2.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/python
# ------------------------------------------------------------
# css_parser.py
#
# tokenizer for css
# ------------------------------------------------------------
import ply.lex as lex
import ply.yacc as yacc
import sys
import logging
DEBUG=False
#List of pstates
pstates=['NORMAL',
'RULE']
pstate='NORMAL'
inComment=False
actualRule=None
actualSelector=None
PASE=0
# List of token names that ARE NOT reserved words.
tokens=[
'START_COMMENT',
'END_COMMENT',
'DOLLAR',
'AT',
'EQUALS',
'RCURL',
'LCURL',
'COLON',
'SEMICOLON',
'STRING'
]
#List of reserved words.
reserved={
'private': 'PRIVATE'
}
#The final list of tokens made up from plain tokens and reserved words.
tokens = tokens + list(reserved.values())
#List of ignored characters
t_ignore = ' \t\r'
#Token value assignation
t_LCURL=r'\{'
t_RCURL=r'\}'
t_EQUALS=r'='
t_DOLLAR=r'[$]'
t_AT=r'@'
t_START_COMMENT=r'[/][*]'
t_END_COMMENT=r'[*][/]'
t_COLON=':'
t_SEMICOLON=r'[;]'
def t_STRING(t):
'([A-Za-z0-9 ]|[,_>.#"%()]|[-]|\'|[[]|[]])+'
#Si no es una palabra reservada devuelve String
t.type=reserved.get(t.value,'STRING')
return t
# Error handling rule
def t_error(t):
print "Illegal character '%s'" % t.value[0]
t.lexer.skip(1)
# NewLine handling rule
def t_newline(t):
r'\n+'
t.lexer.lineno+=t.value.count("\n")
#######################################################PARSER
lex.lex()
#dictionary of constants
constants={}
#directory of plain rules
plain_rules=[]
#directory of directionable rules
directionable_rules={}
def p_line(t):
'''line : constant_definition
| css_property
| selector
| rule_invocation
| comment
| void_line
| character'''
t[0]="hola"
def p_constant_definition(t):
'constant_definition : DOLLAR STRING EQUALS STRING'
console("constant_definition",t)
if(pstate=='NORMAL'):
constants[t[2]]=t[4]
print constants
else:
error("Constant definition in a "+pstate+ "pstate")
def p_constant_invocation(t):
'constant_invocation : DOLLAR STRING'
if(PASE==1):
console("constant_invocation",t)
if(pstate=='RULE'):
t[0]=constants[t[2]]
elif(pstate=='NORMAL'):
error("Constant invocation outside a rule.")
def p_rule_invocation(t):
'''rule_invocation : AT STRING
| AT STRING LCURL'''
if(PASE==0):
if(pstate=='NORMAL'):
actualRule=t[2]
directionable_rules[actualRule]=[]
print("new_rule")
elif(PASE==1):
if(pstate=='RULE'):
logMsg("rule_invocation")
def p_selector(t):
'''selector : STRING LCURL
| STRING COLON STRING LCURL'''
if(len(t)==3):
actualSelector=t[1]
else:
actualSelector=str(t[1])+":"+str(t[3])
if(PASE==0):
plain_rules.append([actualSelector,[]])
logMsg("selector: "+actualSelector)
def getAsInDic(tup,key):
for a in tup:
if(a[0]=="key"):
return a[1]
raise KeyError(str(a[0])+" no encontrado en"+str(tup))
def p_css_property(t):
'''css_property : STRING COLON STRING SEMICOLON
| STRING COLON constant_invocation SEMICOLON'''
if(PASE==0):
print("CSS PROP: "+str(actualRule)+" "+str(actualSelector))
try:
print "CSS prop:"+str(directionable_rules[actualRule])
directionable_rules[actualRule].append([t[1],t[3]])
print "CSS prop:"+str(directionable_rules[actualRule])
print
except KeyError:
print "Key Error directionable: "+str(actualRule)
try:
getAsInDic(plain_rules,actualSelector).append([t[1],t[3]])
except KeyError:
pass
console("property:",t)
def p_character(t):
'''character : START_COMMENT
| END_COMMENT
| RCURL'''
if(pstate=='RULE'):
if(t[1]=="}"):
actualRule=None
actualSelector=None
console("character:",t)
def p_comment(t):
'comment : START_COMMENT STRING END_COMMENT'
logMsg("comment: "+str(t[2]))
def p_void_line(t):
'void_line : '
#console("void", ["void_line"]);
pass
def p_error(t):
logMsg("error:"+str(t))
def logMsg(msg):
print msg
def console(name,t):
if(DEBUG):
print name+":"+str(list(t))
##################################################LOGGER
logging.basicConfig(
level=logging.DEBUG,
filename="log.txt",
filemode="w",
format="%(filename)10s:%(lineno)4d:%(message)s"
)
log=logging.getLogger()
##################################################END LOGGER
print "\t\tCONSTANTS"
print constants
print "\t\tPLAIN RULES"
print plain_rules
print "\t\tDIRECTIONABLE RULES"
print directionable_rules
print "\n\n\n"
#######################################################PARSE
def parse(line):
return yacc.yacc().parse(str(line),debug=log)