forked from micropython/micropython-esp32-ulp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
69 lines (56 loc) · 1.7 KB
/
util.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
DEBUG = False
import gc
NORMAL, WHITESPACE = 0, 1
def garbage_collect(msg, verbose=DEBUG):
free_before = gc.mem_free()
gc.collect()
free_after = gc.mem_free()
if verbose:
print("%s: %d --gc--> %d bytes free" % (msg, free_before, free_after))
def split_tokens(line):
buf = ""
tokens = []
state = NORMAL
for c in line:
if ('a' <= c <= 'z') or ('A' <= c <= 'Z') or ('0' <= c <= '9') or c == '_':
if state != NORMAL:
if len(buf) > 0:
tokens.append(buf)
buf = ""
state = NORMAL
buf += c
elif c == ' ' or c == '\t':
if state != WHITESPACE:
if len(buf) > 0:
tokens.append(buf)
buf = ""
state = WHITESPACE
buf += c
else:
if len(buf) > 0:
tokens.append(buf)
buf = ""
tokens.append(c)
if len(buf) > 0:
tokens.append(buf)
return tokens
def validate_expression(param):
for token in split_tokens(param):
state = 0
for c in token:
if c not in ' \t+-*/%()<>&|~x0123456789abcdef':
return False
# the following allows hex digits a-f after 0x but not otherwise
if state == 0:
if c in 'abcdef':
return False
if c == '0':
state = 1
continue
if state == 1:
state = 2 if c == 'x' else 0
continue
if state == 2:
if c not in '0123456789abcdef':
state = 0
return True