This repository has been archived by the owner on Jun 11, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.py
130 lines (117 loc) · 3.73 KB
/
scanner.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
class SubstitutionError(Exception):
pass
def scan(scanstr, i=0): # type: (Text) -> List[int]
START = 0
DOLLAR = 1
PAREN = 2
BRACE = 3
SINGLE_QUOTE = 4
DOUBLE_QUOTE = 5
BACKSLASH = 6
PLAIN = 7
RUN = 8
stack = [START]
start = i
while i < len(scanstr):
state = stack[-1]
c = scanstr[i]
if state == START:
if c == '$':
stack.append(DOLLAR)
elif c == '\\':
stack.append(BACKSLASH)
elif c == "'":
stack.append(SINGLE_QUOTE)
elif c == '"':
stack.append(DOUBLE_QUOTE)
elif c == "\n":
return [start, i+1]
elif c == ' ':
start += 1
else:
stack.append(RUN)
elif state == RUN:
if c in (' ', '\\', "$", "\n", "'", '"'):
stack.pop()
if stack[-1] == START:
return [start, i]
elif state == BACKSLASH:
stack.pop()
if stack[-1] == START:
return [i - 1, i + 1]
elif state == DOLLAR:
if c == '(':
start = i - 1
stack.append(PAREN)
elif c == '{':
start = i - 1
stack.append(BRACE)
else:
stack.pop()
elif state == PAREN:
if c == '(':
stack.append(PAREN)
elif c == ')':
stack.pop()
if stack[-1] == DOLLAR:
return [start, i + 1]
elif c == "'":
stack.append(SINGLE_QUOTE)
elif c == '"':
stack.append(DOUBLE_QUOTE)
elif state == BRACE:
if c == '{':
stack.append(BRACE)
elif c == '}':
stack.pop()
if stack[-1] == DOLLAR:
return [start, i + 1]
elif c == "'":
stack.append(SINGLE_QUOTE)
elif c == '"':
stack.append(DOUBLE_QUOTE)
elif state == SINGLE_QUOTE:
if c == "'":
stack.pop()
return [start, i + 1]
elif c == '\\':
stack.append(BACKSLASH)
elif state == DOUBLE_QUOTE:
if c == '"':
stack.pop()
return [start, i + 1]
elif c == '\\':
stack.append(BACKSLASH)
i += 1
if stack[-1] == RUN:
return [start, i]
if len(stack) > 1:
raise SubstitutionError(
"Substitution error, unfinished block starting at position {}: {}".format(start, scanstr[start:]))
else:
return None
def lex(cont, join=True):
prv = 0
pieces = []
n = scan(cont)
while n:
if (n[0] > prv or (not pieces) or (not join) or
(cont[n[0]:n[1]] == "\n") or (pieces[-1] == "\n")):
pieces.append(cont[n[0]:n[1]])
else:
pieces[-1] = pieces[-1] + cont[n[0]:n[1]]
prv = n[1]
n = scan(cont, n[1])
return pieces
def test():
print lex("--in=${foo bar}m", join=True) == ['--in=${foo bar}m']
print lex("--in=${foo bar}m", join=False) == ['--in=', '${foo bar}', 'm']
print lex("-a --in=${foo}m -b' x '", join=True) == ['-a', '--in=${foo}m', "-b' x '"]
print lex("-a --in=${foo}m -b' x '", join=False) == ['-a', '--in=', '${foo}', 'm', "-b", "' x '"]
print lex("a' 'b", join=True) == ["a' 'b"]
print lex("a' 'b", join=False) == ["a", "' '", "b"]
print lex("'a b'", join=True) == ["'a b'"]
print lex("'a b'", join=False) == ["'a b'"]
print lex("foo bar\nbaz blub", join=True) #== []
if __name__ == "__main__":
test()