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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
# Copyright (C) 2012 Red Hat, Inc. All rights reserved.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
# Authors: Jan Safranek <jsafrane@redhat.com>
# -*- coding: utf-8 -*-
"""
Simple parser of MOF files. The only thing that is parsed out is list of
included files. This list is composed recursively, i.e. a MOF file can
include another MOF file, which can then include another one.
"""
# Based on pywbem.mof_compiler.py, (C) Copyright 2006-2007 Novell, Inc.
from pywbem import lex
from pywbem import yacc
from pywbem.lex import TOKEN
import os
class MOFLexer:
""" Lexer for MOF files. """
def __init__(self, **kwargs):
self.lexer = lex.lex(module=self, **kwargs)
reserved = {
'pragma':'PRAGMA',
'false': 'FALSE',
'true': 'TRUE',
'null': 'NULL'
}
tokens = reserved.values() + [
'IDENTIFIER',
'stringValue',
'floatValue',
'charValue',
'binaryValue',
'octalValue',
'decimalValue',
'hexValue',
]
literals = '#(){};[],$:='
# UTF-8 (from Unicode 4.0.0 standard):
# Table 3-6. Well-Formed UTF-8 Byte Sequences Code Points
# 1st Byte 2nd Byte 3rd Byte 4th Byte
# U+0000..U+007F 00..7F
# U+0080..U+07FF C2..DF 80..BF
# U+0800..U+0FFF E0 A0..BF 80..BF
# U+1000..U+CFFF E1..EC 80..BF 80..BF
# U+D000..U+D7FF ED 80..9F 80..BF
# U+E000..U+FFFF EE..EF 80..BF 80..BF
# U+10000..U+3FFFF F0 90..BF 80..BF 80..BF
# U+40000..U+FFFFF F1..F3 80..BF 80..BF 80..BF
# U+100000..U+10FFFF F4 80..8F 80..BF 80..BF
utf8_2 = r'[\xC2-\xDF][\x80-\xBF]'
utf8_3_1 = r'\xE0[\xA0-\xBF][\x80-\xBF]'
utf8_3_2 = r'[\xE1-\xEC][\x80-\xBF][\x80-\xBF]'
utf8_3_3 = r'\xED[\x80-\x9F][\x80-\xBF]'
utf8_3_4 = r'[\xEE-\xEF][\x80-\xBF][\x80-\xBF]'
utf8_4_1 = r'\xF0[\x90-\xBF][\x80-\xBF][\x80-\xBF]'
utf8_4_2 = r'[\xF1-\xF3][\x80-\xBF][\x80-\xBF][\x80-\xBF]'
utf8_4_3 = r'\xF4[\x80-\x8F][\x80-\xBF][\x80-\xBF]'
utf8Char = r'(%s)|(%s)|(%s)|(%s)|(%s)|(%s)|(%s)|(%s)' % (utf8_2, utf8_3_1,
utf8_3_2, utf8_3_3, utf8_3_4, utf8_4_1, utf8_4_2, utf8_4_3)
def t_COMMENT(self, t):
r'//.*'
pass
def t_MCOMMENT(self, t):
r'/\*(.|\n)*?\*/'
t.lineno += t.value.count('\n')
t_binaryValue = r'[+-]?[01]+[bB]'
t_octalValue = r'[+-]?0[0-7]+'
t_decimalValue = r'[+-]?([1-9][0-9]*|0)'
t_hexValue = r'[+-]?0[xX][0-9a-fA-F]+'
t_floatValue = r'[+-]?[0-9]*\.[0-9]+([eE][+-]?[0-9]+)?'
simpleEscape = r"""[bfnrt'"\\]"""
hexEscape = r'x[0-9a-fA-F]{1,4}'
escapeSequence = r'[\\]((%s)|(%s))' % (simpleEscape, hexEscape)
cChar = r"[^'\\\n\r]|(%s)" % escapeSequence
sChar = r'[^"\\\n\r]|(%s)' % escapeSequence
charValue = r"'%s'" % cChar
t_stringValue = r'"(%s)*"' % sChar
identifier_re = r'([a-zA-Z_]|(%s))([0-9a-zA-Z_]|(%s))*' % (utf8Char, utf8Char)
@TOKEN(identifier_re)
def t_IDENTIFIER(self, t):
t.type = self.reserved.get(t.value.lower(), 'IDENTIFIER') # check for reserved word
return t
# Define a rule so we can track line numbers
def t_newline(self, t):
r'\n+'
t.lexer.lineno += len(t.value)
t.lexer.linestart = t.lexpos
t_ignore = ' \r\t'
# Error handling rule
def t_error(self, t):
msg = "Illegal character '%s' " % t.value[0]
msg += "Line %d" % (t.lineno)
t.lexer.parser.log(msg)
t.lexer.skip(1)
class MOFParseError(ValueError):
pass
class MOFParser:
""" Parser of '#pragma include' directivers in MOF files."""
tokens = MOFLexer.tokens
def __init__(self, **kwargs):
self.lexer = MOFLexer()
self.parser = yacc.yacc(module=self, **kwargs)
self.files = []
def p_error(self, p):
ex = MOFParseError('Parse error at line %d' % (p.lineno))
if p is None:
ex.args = ('Unexpected end of file',)
raise ex
ex.file = self.filename
ex.lineno = p.lineno
raise ex
def p_mofSpecification(self, p):
"""mof : mofItemList"""
def p_mofItemList(self, p):
"""mofItemList : empty
| mofItemList mofItem
"""
def p_mofItem(self, p):
"""mofItem : pragma
| IDENTIFIER
| literal
| value
"""
def p_pragma(self, p):
"""pragma : '#' PRAGMA pragmaName '(' pragmaParameter ')'"""
directive = p[3].lower()
param = p[5]
if directive == 'include':
fname = param
fname = os.path.dirname(self.filename) + '/' + fname
self._parse_file(fname)
def p_pragmaName(self, p):
"""pragmaName : identifier"""
p[0] = p[1]
def p_pragmaParameter(self, p):
"""pragmaParameter : stringValue"""
p[0] = self._fixStringValue(p[1])
def _fixStringValue(self, s):
s = s[1:-1]
rv = ''
esc = False
i = -1
while i < len(s) - 1:
i += 1
ch = s[i]
if ch == '\\' and not esc:
esc = True
continue
if not esc:
rv += ch
continue
if ch == '"' : rv += '"'
elif ch == 'n' : rv += '\n'
elif ch == 't' : rv += '\t'
elif ch == 'b' : rv += '\b'
elif ch == 'f' : rv += '\f'
elif ch == 'r' : rv += '\r'
elif ch == '\\': rv += '\\'
elif ch in ['x', 'X']:
hexc = 0
j = 0
i += 1
while j < 4:
c = s[i + j];
c = c.upper()
if not c.isdigit() and not c in 'ABCDEF':
break;
hexc <<= 4
if c.isdigit():
hexc |= ord(c) - ord('0')
else:
hexc |= ord(c) - ord('A') + 0XA
j += 1
rv += chr(hexc)
i += j - 1
esc = False
return rv
def p_value(self, p):
"""value : integerValue
| floatValue
| charValue
| stringValue
| booleanValue
| nullValue
"""
def p_literal(self, p):
"""literal : '('
| ')'
| '{'
| '}'
| ';'
| '['
| ']'
| ','
| '$'
| ':'
| '='
"""
def p_integerValue(self, p):
"""integerValue : binaryValue
| octalValue
| decimalValue
| hexValue
"""
def p_booleanValue(self, p):
"""booleanValue : FALSE
| TRUE
"""
def p_nullValue(self, p):
"""nullValue : NULL"""
def p_identifier(self, p):
"""identifier : IDENTIFIER
"""
p[0] = p[1]
def p_empty(self, p):
'empty :'
def _parse_file(self, fname):
f = open(fname, 'r')
mof = f.read()
f.close()
self.files.append(fname)
old_filename = self.filename
self.filename = fname
# we must use fresh lexer so the old one can continue with parsing the
# old file
lex = self.lexer.lexer.clone()
lex.parser = self.parser
self.parser.parse(mof, lexer=lex)
self.filename = old_filename
def parse_includes(self, fnames):
"""
Parse given MOF files and return array with all parsed files,
including the included ones.
"""
self.filename = '__main__'
self.files = []
for fname in fnames:
if fname[0] != '/':
fname = os.path.curdir + '/' + fname
self._parse_file(fname)
return self.files
|