|
9 | 9 | :copyright: (c) 2013 by the Babel Team.
|
10 | 10 | :license: BSD, see LICENSE for more details.
|
11 | 11 | """
|
12 |
| - |
13 |
| -from operator import itemgetter |
| 12 | +from collections import namedtuple |
14 | 13 | import re
|
15 | 14 | from babel._compat import unichr
|
16 | 15 |
|
17 |
| -operators = [ |
| 16 | +operators = sorted([ |
18 | 17 | '+', '-', '*', '%', '!=', '==', '<', '>', '<=', '>=', '=',
|
19 | 18 | '+=', '-=', '*=', '%=', '<<', '>>', '>>>', '<<=', '>>=',
|
20 | 19 | '>>>=', '&', '&=', '|', '|=', '&&', '||', '^', '^=', '(', ')',
|
21 | 20 | '[', ']', '{', '}', '!', '--', '++', '~', ',', ';', '.', ':'
|
22 |
| -] |
23 |
| -operators.sort(key=lambda a: -len(a)) |
| 21 | +], key=len, reverse=True) |
24 | 22 |
|
25 | 23 | escapes = {'b': '\b', 'f': '\f', 'n': '\n', 'r': '\r', 't': '\t'}
|
26 | 24 |
|
| 25 | +division_re = re.compile(r'/=?') |
| 26 | +regex_re = re.compile(r'/(?:[^/\\]*(?:\\.[^/\\]*)*)/[a-zA-Z]*(?s)') |
| 27 | +line_re = re.compile(r'(\r\n|\n|\r)') |
| 28 | +line_join_re = re.compile(r'\\' + line_re.pattern) |
| 29 | +uni_escape_re = re.compile(r'[a-fA-F0-9]{1,4}') |
| 30 | +name_re = re.compile(r'(\$+\w*|[^\W\d]\w*)(?u)') |
| 31 | + |
| 32 | +Token = namedtuple('Token', 'type value lineno') |
| 33 | + |
27 | 34 | rules = [
|
28 | 35 | (None, re.compile(r'\s+(?u)')),
|
29 | 36 | (None, re.compile(r'<!--.*')),
|
30 | 37 | ('linecomment', re.compile(r'//.*')),
|
31 | 38 | ('multilinecomment', re.compile(r'/\*.*?\*/(?us)')),
|
32 |
| - ('name', re.compile(r'(\$+\w*|[^\W\d]\w*)(?u)')), |
| 39 | + ('name', name_re), |
33 | 40 | ('number', re.compile(r'''(?x)(
|
34 | 41 | (?:0|[1-9]\d*)
|
35 | 42 | (\.\d+)?
|
|
43 | 50 | )'''))
|
44 | 51 | ]
|
45 | 52 |
|
46 |
| -division_re = re.compile(r'/=?') |
47 |
| -regex_re = re.compile(r'/(?:[^/\\]*(?:\\.[^/\\]*)*)/[a-zA-Z]*(?s)') |
48 |
| -line_re = re.compile(r'(\r\n|\n|\r)') |
49 |
| -line_join_re = re.compile(r'\\' + line_re.pattern) |
50 |
| -uni_escape_re = re.compile(r'[a-fA-F0-9]{1,4}') |
51 |
| - |
52 |
| - |
53 |
| -class Token(tuple): |
54 |
| - """Represents a token as returned by `tokenize`.""" |
55 |
| - __slots__ = () |
56 |
| - |
57 |
| - def __new__(cls, type, value, lineno): |
58 |
| - return tuple.__new__(cls, (type, value, lineno)) |
59 |
| - |
60 |
| - type = property(itemgetter(0)) |
61 |
| - value = property(itemgetter(1)) |
62 |
| - lineno = property(itemgetter(2)) |
63 |
| - |
64 | 53 |
|
65 | 54 | def indicates_division(token):
|
66 | 55 | """A helper function that helps the tokenizer to decide if the current
|
|
0 commit comments