diff options
Diffstat (limited to 'babel/messages/jslexer.py')
-rw-r--r-- | babel/messages/jslexer.py | 23 |
1 files changed, 15 insertions, 8 deletions
diff --git a/babel/messages/jslexer.py b/babel/messages/jslexer.py index 886f69d..07fffde 100644 --- a/babel/messages/jslexer.py +++ b/babel/messages/jslexer.py @@ -9,17 +9,21 @@ :copyright: (c) 2013-2022 by the Babel Team. :license: BSD, see LICENSE for more details. """ +from __future__ import annotations + from collections import namedtuple +from collections.abc import Generator, Iterator, Sequence import re +from typing import NamedTuple -operators = sorted([ +operators: list[str] = sorted([ '+', '-', '*', '%', '!=', '==', '<', '>', '<=', '>=', '=', '+=', '-=', '*=', '%=', '<<', '>>', '>>>', '<<=', '>>=', '>>>=', '&', '&=', '|', '|=', '&&', '||', '^', '^=', '(', ')', '[', ']', '{', '}', '!', '--', '++', '~', ',', ';', '.', ':' ], key=len, reverse=True) -escapes = {'b': '\b', 'f': '\f', 'n': '\n', 'r': '\r', 't': '\t'} +escapes: dict[str, str] = {'b': '\b', 'f': '\f', 'n': '\n', 'r': '\r', 't': '\t'} name_re = re.compile(r'[\w$_][\w\d$_]*', re.UNICODE) dotted_name_re = re.compile(r'[\w$_][\w\d$_.]*[\w\d$_.]', re.UNICODE) @@ -30,9 +34,12 @@ line_join_re = re.compile(r'\\' + line_re.pattern) uni_escape_re = re.compile(r'[a-fA-F0-9]{1,4}') hex_escape_re = re.compile(r'[a-fA-F0-9]{1,2}') -Token = namedtuple('Token', 'type value lineno') +class Token(NamedTuple): + type: str + value: str + lineno: int -_rules = [ +_rules: list[tuple[str | None, re.Pattern[str]]] = [ (None, re.compile(r'\s+', re.UNICODE)), (None, re.compile(r'<!--.*')), ('linecomment', re.compile(r'//.*')), @@ -55,7 +62,7 @@ _rules = [ ] -def get_rules(jsx, dotted, template_string): +def get_rules(jsx: bool, dotted: bool, template_string: bool) -> list[tuple[str | None, re.Pattern[str]]]: """ Get a tokenization rule list given the passed syntax options. @@ -75,7 +82,7 @@ def get_rules(jsx, dotted, template_string): return rules -def indicates_division(token): +def indicates_division(token: Token) -> bool: """A helper function that helps the tokenizer to decide if the current token may be followed by a division operator. """ @@ -84,7 +91,7 @@ def indicates_division(token): return token.type in ('name', 'number', 'string', 'regexp') -def unquote_string(string): +def unquote_string(string: str) -> str: """Unquote a string with JavaScript rules. The string has to start with string delimiters (``'``, ``"`` or the back-tick/grave accent (for template strings).) """ @@ -151,7 +158,7 @@ def unquote_string(string): return u''.join(result) -def tokenize(source, jsx=True, dotted=True, template_string=True, lineno=1): +def tokenize(source: str, jsx: bool = True, dotted: bool = True, template_string: bool = True, lineno: int = 1) -> Generator[Token, None, None]: """ Tokenize JavaScript/JSX source. Returns a generator of tokens. |