-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathcommon_tokenizer.py
More file actions
69 lines (63 loc) · 2.49 KB
/
Copy pathcommon_tokenizer.py
File metadata and controls
69 lines (63 loc) · 2.49 KB
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
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
from typing import Iterator
# Tokenizer for the config text format id Tech 3 games (Jedi Academy included) use for
# animation.cfg and similar files - matches COM_ParseExt (q_shared.c/bg_panimate.c): a `//`,
# `/* */`, or `"` is only special as the first character(s) of a new token. Once a bare token has
# started, it always runs to the next whitespace regardless of what characters occur inside it -
# comments/quotes cannot start mid-token (e.g. `foo//bar` is one token, not `foo` + a comment).
# Whitespace is any character <= ' ' (0x20), matching the engine; newlines are ordinary
# whitespace here, not a token/entry separator, so entries may span multiple lines.
def tokenize(text: str) -> Iterator[str]:
i = 0
n = len(text)
while True:
# skip whitespace and comments - a new token may start right after either
while i < n:
c = text[i]
if c <= ' ':
i += 1
continue
if text[i:i + 2] == "//":
i += 2
while i < n and text[i] != '\n':
i += 1
continue
if text[i:i + 2] == "/*":
i += 2
end = text.find("*/", i)
i = end + 2 if end != -1 else n
continue
break
if i >= n:
return
if text[i] == '"':
i += 1
start = i
end = text.find('"', i)
if end == -1:
yield text[start:n]
i = n
else:
yield text[start:end]
i = end + 1
continue
start = i
while i < n and text[i] > ' ':
i += 1
yield text[start:i]