| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
"""Tokenize C++ source code.""" |
| 19 |
|
| 20 |
__author__ = '[email protected] (Neal Norwitz)' |
| 21 |
|
| 22 |
|
| 23 |
try: |
| 24 |
|
| 25 |
import builtins |
| 26 |
except ImportError: |
| 27 |
|
| 28 |
import __builtin__ as builtins |
| 29 |
|
| 30 |
|
| 31 |
import sys |
| 32 |
|
| 33 |
from cpp import utils |
| 34 |
|
| 35 |
|
| 36 |
if not hasattr(builtins, 'set'): |
| 37 |
|
| 38 |
from sets import Set as set |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
_letters = 'abcdefghijklmnopqrstuvwxyz' |
| 43 |
VALID_IDENTIFIER_CHARS = set(_letters + _letters.upper() + '_0123456789$') |
| 44 |
HEX_DIGITS = set('0123456789abcdefABCDEF') |
| 45 |
INT_OR_FLOAT_DIGITS = set('01234567890eE-+') |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
_STR_PREFIXES = set(('R', 'u8', 'u8R', 'u', 'uR', 'U', 'UR', 'L', 'LR')) |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
UNKNOWN = 'UNKNOWN' |
| 54 |
SYNTAX = 'SYNTAX' |
| 55 |
CONSTANT = 'CONSTANT' |
| 56 |
NAME = 'NAME' |
| 57 |
PREPROCESSOR = 'PREPROCESSOR' |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
WHENCE_STREAM, WHENCE_QUEUE = range(2) |
| 62 |
|
| 63 |
|
| 64 |
class Token(object): |
| 65 |
"""Data container to represent a C++ token. |
| 66 |
|
| 67 |
Tokens can be identifiers, syntax char(s), constants, or |
| 68 |
pre-processor directives. |
| 69 |
|
| 70 |
start contains the index of the first char of the token in the source |
| 71 |
end contains the index of the last char of the token in the source |
| 72 |
""" |
| 73 |
|
| 74 |
def __init__(self, token_type, name, start, end): |
| 75 |
self.token_type = token_type |
| 76 |
self.name = name |
| 77 |
self.start = start |
| 78 |
self.end = end |
| 79 |
self.whence = WHENCE_STREAM |
| 80 |
|
| 81 |
def __str__(self): |
| 82 |
if not utils.DEBUG: |
| 83 |
return 'Token(%r)' % self.name |
| 84 |
return 'Token(%r, %s, %s)' % (self.name, self.start, self.end) |
| 85 |
|
| 86 |
__repr__ = __str__ |
| 87 |
|
| 88 |
|
| 89 |
def _GetString(source, start, i): |
| 90 |
i = source.find('"', i+1) |
| 91 |
while source[i-1] == '\\': |
| 92 |
|
| 93 |
backslash_count = 1 |
| 94 |
j = i - 2 |
| 95 |
while source[j] == '\\': |
| 96 |
backslash_count += 1 |
| 97 |
j -= 1 |
| 98 |
|
| 99 |
if (backslash_count % 2) == 0: |
| 100 |
break |
| 101 |
i = source.find('"', i+1) |
| 102 |
return i + 1 |
| 103 |
|
| 104 |
|
| 105 |
def _GetChar(source, start, i): |
| 106 |
|
| 107 |
i = source.find("'", i+1) |
| 108 |
while source[i-1] == '\\': |
| 109 |
|
| 110 |
if (i - 2) > start and source[i-2] == '\\': |
| 111 |
break |
| 112 |
i = source.find("'", i+1) |
| 113 |
|
| 114 |
if i < 0: |
| 115 |
i = start |
| 116 |
return i + 1 |
| 117 |
|
| 118 |
|
| 119 |
def GetTokens(source): |
| 120 |
"""Returns a sequence of Tokens. |
| 121 |
|
| 122 |
Args: |
| 123 |
source: string of C++ source code. |
| 124 |
|
| 125 |
Yields: |
| 126 |
Token that represents the next token in the source. |
| 127 |
""" |
| 128 |
|
| 129 |
valid_identifier_chars = VALID_IDENTIFIER_CHARS |
| 130 |
hex_digits = HEX_DIGITS |
| 131 |
int_or_float_digits = INT_OR_FLOAT_DIGITS |
| 132 |
int_or_float_digits2 = int_or_float_digits | set('.') |
| 133 |
|
| 134 |
|
| 135 |
ignore_errors = False |
| 136 |
count_ifs = 0 |
| 137 |
|
| 138 |
i = 0 |
| 139 |
end = len(source) |
| 140 |
while i < end: |
| 141 |
|
| 142 |
while i < end and source[i].isspace(): |
| 143 |
i += 1 |
| 144 |
if i >= end: |
| 145 |
return |
| 146 |
|
| 147 |
token_type = UNKNOWN |
| 148 |
start = i |
| 149 |
c = source[i] |
| 150 |
if c.isalpha() or c == '_': |
| 151 |
token_type = NAME |
| 152 |
while source[i] in valid_identifier_chars: |
| 153 |
i += 1 |
| 154 |
|
| 155 |
|
| 156 |
if (source[i] == "'" and (i - start) == 1 and |
| 157 |
source[start:i] in 'uUL'): |
| 158 |
|
| 159 |
token_type = CONSTANT |
| 160 |
i = _GetChar(source, start, i) |
| 161 |
elif source[i] == "'" and source[start:i] in _STR_PREFIXES: |
| 162 |
token_type = CONSTANT |
| 163 |
i = _GetString(source, start, i) |
| 164 |
elif c == '/' and source[i+1] == '/': |
| 165 |
i = source.find('\n', i) |
| 166 |
if i == -1: |
| 167 |
i = end |
| 168 |
continue |
| 169 |
elif c == '/' and source[i+1] == '*': |
| 170 |
i = source.find('*/', i) + 2 |
| 171 |
continue |
| 172 |
elif c in ':+-<>&|*=': |
| 173 |
token_type = SYNTAX |
| 174 |
i += 1 |
| 175 |
new_ch = source[i] |
| 176 |
if new_ch == c and c != '>': |
| 177 |
i += 1 |
| 178 |
elif c == '-' and new_ch == '>': |
| 179 |
i += 1 |
| 180 |
elif new_ch == '=': |
| 181 |
i += 1 |
| 182 |
elif c in '()[]{}~!?^%;/.,': |
| 183 |
token_type = SYNTAX |
| 184 |
i += 1 |
| 185 |
if c == '.' and source[i].isdigit(): |
| 186 |
token_type = CONSTANT |
| 187 |
i += 1 |
| 188 |
while source[i] in int_or_float_digits: |
| 189 |
i += 1 |
| 190 |
|
| 191 |
for suffix in ('l', 'f'): |
| 192 |
if suffix == source[i:i+1].lower(): |
| 193 |
i += 1 |
| 194 |
break |
| 195 |
elif c.isdigit(): |
| 196 |
token_type = CONSTANT |
| 197 |
if c == '0' and source[i+1] in 'xX': |
| 198 |
|
| 199 |
i += 2 |
| 200 |
while source[i] in hex_digits: |
| 201 |
i += 1 |
| 202 |
else: |
| 203 |
while source[i] in int_or_float_digits2: |
| 204 |
i += 1 |
| 205 |
|
| 206 |
for suffix in ('ull', 'll', 'ul', 'l', 'f', 'u'): |
| 207 |
size = len(suffix) |
| 208 |
if suffix == source[i:i+size].lower(): |
| 209 |
i += size |
| 210 |
break |
| 211 |
elif c == '"': |
| 212 |
token_type = CONSTANT |
| 213 |
i = _GetString(source, start, i) |
| 214 |
elif c == "'": |
| 215 |
token_type = CONSTANT |
| 216 |
i = _GetChar(source, start, i) |
| 217 |
elif c == '#': |
| 218 |
token_type = PREPROCESSOR |
| 219 |
got_if = source[i:i+3] == '#if' and source[i+3:i+4].isspace() |
| 220 |
if got_if: |
| 221 |
count_ifs += 1 |
| 222 |
elif source[i:i+6] == '#endif': |
| 223 |
count_ifs -= 1 |
| 224 |
if count_ifs == 0: |
| 225 |
ignore_errors = False |
| 226 |
|
| 227 |
|
| 228 |
while 1: |
| 229 |
i1 = source.find('\n', i) |
| 230 |
i2 = source.find('//', i) |
| 231 |
i3 = source.find('/*', i) |
| 232 |
i4 = source.find('"', i) |
| 233 |
|
| 234 |
|
| 235 |
i = min([x for x in (i1, i2, i3, i4, end) if x != -1]) |
| 236 |
|
| 237 |
|
| 238 |
if source[i] == '"': |
| 239 |
i = source.find('"', i+1) + 1 |
| 240 |
assert i > 0 |
| 241 |
continue |
| 242 |
|
| 243 |
if not (i == i1 and source[i-1] == '\\'): |
| 244 |
if got_if: |
| 245 |
condition = source[start+4:i].lstrip() |
| 246 |
if (condition.startswith('0') or |
| 247 |
condition.startswith('(0)')): |
| 248 |
ignore_errors = True |
| 249 |
break |
| 250 |
i += 1 |
| 251 |
elif c == '\\': |
| 252 |
|
| 253 |
i += 1 |
| 254 |
continue |
| 255 |
elif ignore_errors: |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
i += 1 |
| 262 |
else: |
| 263 |
sys.stderr.write('Got invalid token in %s @ %d token:%s: %r\n' % |
| 264 |
('?', i, c, source[i-10:i+10])) |
| 265 |
raise RuntimeError('unexpected token') |
| 266 |
|
| 267 |
if i <= 0: |
| 268 |
print('Invalid index, exiting now.') |
| 269 |
return |
| 270 |
yield Token(token_type, source[start:i], start, i) |
| 271 |
|
| 272 |
|
| 273 |
if __name__ == '__main__': |
| 274 |
def main(argv): |
| 275 |
"""Driver mostly for testing purposes.""" |
| 276 |
for filename in argv[1:]: |
| 277 |
source = utils.ReadFile(filename) |
| 278 |
if source is None: |
| 279 |
continue |
| 280 |
|
| 281 |
for token in GetTokens(source): |
| 282 |
print('%-12s: %s' % (token.token_type, token.name)) |
| 283 |
|
| 284 |
sys.stdout.write('\n') |
| 285 |
|
| 286 |
|
| 287 |
main(sys.argv) |