-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdmParser.py
72 lines (54 loc) · 1.68 KB
/
dmParser.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
def _ParseLine(line_num, line):
insideQuote = False
result = []
word = ''
i = 0
while i < len(line):
c = line[i]
if not insideQuote:
if c == '#':
# Found start of comment
line = line[:i].strip()
break
elif c == '"':
if word == '':
# Found start of a quote
insideQuote = True
else:
# Found quote inside word. Keep it there
word += c
elif c.isspace():
if word != '':
result.append(word)
word = ''
else:
word += c
else: # insideQuote
if c == '\\' and line[i + 1] == '"':
word += '"'
i += 1
elif c == '"':
result.append(word)
word = ''
insideQuote = False
else:
word += c
i += 1
if word != '':
result.append(word)
if len(result) > 0:
result.insert(0, line_num)
result.insert(1, line)
return result
def Parse(lines):
rules = []
line_num = 0
for line in lines:
line = line.strip('\r\n').strip()
line_num += 1
if line == '':
continue
rule = _ParseLine(line_num, line)
if rule:
rules.append(rule)
return rules