Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f67be69

Browse files
author
Max
committedSep 16, 2022
more hints
1 parent bb8122e commit f67be69

File tree

1 file changed

+29
-25
lines changed

1 file changed

+29
-25
lines changed
 

‎plim/lexer.py

+29-25
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"""Plim lexer"""
33
import functools
44
import re
5-
from typing import Optional, Tuple, Any, Mapping, Callable, Iterator
5+
from typing import Optional, Tuple, Any, Mapping, Callable, Iterator, Sequence
66

77
import markdown2
88
from pyrsistent import v
@@ -175,6 +175,9 @@
175175

176176
# Searchers
177177
# ==================================================================================
178+
SourceIter = Iterator[tuple[int, str]]
179+
Parsed = Tuple[str, int, str, str]
180+
178181
def search_quotes(line: str, escape_char: str = '\\', quotes_re = QUOTES_RE) -> Optional[int]:
179182
"""
180183
:param line: may be empty
@@ -255,7 +258,7 @@ def extract_embedding_quotes(content) -> Optional[Tuple[Any, Any, Any]]:
255258

256259

257260
def _extract_braces_expression(
258-
line: str, source: Iterator[Tuple[Any, str]], starting_braces_re, open_braces_re, closing_braces_re
261+
line: str, source: SourceIter, starting_braces_re, open_braces_re, closing_braces_re
259262
) -> Optional[Tuple[Any, Any, Any]]:
260263
"""
261264
@@ -323,16 +326,16 @@ def _extract_braces_expression(
323326
)
324327

325328

326-
def extract_identifier(line, source, identifier_start='#', terminators=('.', ' ', CLOSE_BRACE, INLINE_TAG_SEPARATOR)):
329+
def extract_identifier(line: str, source, identifier_start: str = '#',
330+
terminators: Sequence[str] = v('.', ' ', CLOSE_BRACE, INLINE_TAG_SEPARATOR)
331+
) -> Optional[Tuple[str, str, Any]]:
327332
"""
328333
329334
:param line: Current line. It may be empty.
330335
:type line: str or unicode
331336
:param source:
332-
:type source: str
333337
:param identifier_start:
334338
:param terminators:
335-
:type terminators: tuple or set
336339
"""
337340
if not line or not line.startswith(identifier_start):
338341
return None
@@ -536,7 +539,7 @@ def extract_tag_attribute(line: str, source: str, syntax, inside_parentheses=Fal
536539
return None
537540

538541

539-
def extract_line_break(tail, source):
542+
def extract_line_break(tail, source: SourceIter):
540543
"""
541544
Checks the first character of the tail.
542545
@@ -558,7 +561,7 @@ def extract_line_break(tail, source):
558561
return found, tail, source
559562

560563

561-
def extract_statement_expression(tail: str, source: str) -> Tuple[str, str]:
564+
def extract_statement_expression(tail: str, source: SourceIter) -> Tuple[str, str]:
562565
"""
563566
564567
:param tail:
@@ -806,7 +809,7 @@ def parse_doctype(indent_level, current_line, ___, source, syntax):
806809
return DOCTYPES.get(doctype, DOCTYPES['5']), indent_level, '', source
807810

808811

809-
def parse_handlebars(indent_level, current_line, ___, source, syntax):
812+
def parse_handlebars(indent_level: int, current_line, ___, source: SourceIter, syntax) -> Parsed:
810813
"""
811814
812815
:param indent_level:
@@ -829,7 +832,7 @@ def parse_handlebars(indent_level, current_line, ___, source, syntax):
829832
return processed_tag, tail_indent, tail_line, source
830833

831834

832-
def parse_tag_tree(indent_level, current_line, ___, source, syntax):
835+
def parse_tag_tree(indent_level: int, current_line: str, ___: Any, source: SourceIter, syntax) -> Parsed:
833836
"""
834837
835838
:param indent_level:
@@ -961,7 +964,7 @@ def parse_python_new_style(indent_level, __, matched, source, syntax):
961964

962965

963966

964-
def parse_mako_text(indent, __, matched, source, syntax) -> Tuple[str, int, str, str]:
967+
def parse_mako_text(indent, __, matched, source, syntax) -> Parsed:
965968
"""
966969
967970
:param indent:
@@ -1060,7 +1063,7 @@ def parse_comment(indent_level, __, ___, source, syntax):
10601063
return '', 0, '', source
10611064

10621065

1063-
def parse_statements(indent_level, __, matched, source, syntax) -> Tuple[str, int, str, str]:
1066+
def parse_statements(indent_level: int, __: Any, matched, source: SourceIter, syntax) -> Parsed:
10641067
"""
10651068
10661069
:param indent_level:
@@ -1095,7 +1098,9 @@ def parse_statements(indent_level, __, matched, source, syntax) -> Tuple[str, in
10951098
else:
10961099
tail_indent, tail_line = scan_line(tail_line)
10971100

1098-
def complete_statement(buf, tail_indent, tail_line, source, statement, syntax):
1101+
def complete_statement(
1102+
buf: list[str], tail_indent: int, tail_line: str, source, statement: str, syntax
1103+
) -> Parsed:
10991104
buf.extend([
11001105
'\n',
11011106
syntax.STATEMENT_END_START_SEQUENCE,
@@ -1216,7 +1221,7 @@ def complete_statement(buf, tail_indent, tail_line, source, statement, syntax):
12161221

12171222

12181223

1219-
def parse_foreign_statements(indent_level, __, matched, source, syntax):
1224+
def parse_foreign_statements(indent_level: int, __: Any, matched, source: SourceIter, syntax):
12201225
"""
12211226
12221227
:param indent_level:
@@ -1237,7 +1242,7 @@ def parse_foreign_statements(indent_level, __, matched, source, syntax):
12371242
return parse_statements(indent_level, __, matched, source, syntax)
12381243

12391244

1240-
def parse_explicit_literal(indent_level, current_line, ___, source, syntax, parse_embedded) -> Tuple[str, int, str, str]:
1245+
def parse_explicit_literal(indent_level, current_line, ___, source, syntax, parse_embedded) -> Parsed:
12411246
"""
12421247
Parses lines and blocks started with the "|" (pipe) or "," (comma) character.
12431248
@@ -1254,7 +1259,7 @@ def parse_explicit_literal(indent_level, current_line, ___, source, syntax, pars
12541259
trailing_space_required = current_line[0] == LITERAL_CONTENT_SPACE_PREFIX
12551260

12561261
# ---------------------------------
1257-
def prepare_result(buf):
1262+
def prepare_result(buf: Sequence[str]) -> str:
12581263
result = joined(buf).rstrip()
12591264
if trailing_space_required:
12601265
result = u("{} ").format(result)
@@ -1296,14 +1301,13 @@ def prepare_result(buf):
12961301
parse_explicit_literal_no_embedded = functools.partial(parse_explicit_literal, parse_embedded=False)
12971302

12981303

1299-
def _parse_embedded_markup(content, syntax):
1304+
def _parse_embedded_markup(content: str, syntax) -> str:
13001305
"""
13011306
13021307
:param content:
13031308
:param syntax: an instance of one of :class:`plim.syntax.BaseSyntax` children.
13041309
:type syntax: :class:`plim.syntax.BaseSyntax`
13051310
:return:
1306-
:rtype: str
13071311
"""
13081312
buf = []
13091313
tail = content
@@ -1351,7 +1355,7 @@ def _inject_n_filter(line: str) -> str:
13511355
return line
13521356

13531357

1354-
def parse_variable(indent_level, __, matched, source, syntax) -> Tuple[str, int, str, str]:
1358+
def parse_variable(indent_level: int, __, matched, source: SourceIter, syntax) -> Parsed:
13551359
""" = variable or == variable
13561360
13571361
:param indent_level:
@@ -1389,7 +1393,7 @@ def parse_variable(indent_level, __, matched, source, syntax) -> Tuple[str, int,
13891393
return buf_str, 0, '', source
13901394

13911395

1392-
def parse_early_return(indent_level, __, matched, source, syntax) -> Tuple[str, int, str, str]:
1396+
def parse_early_return(indent_level, __, matched, source, syntax) -> Parsed:
13931397
"""
13941398
13951399
:param indent_level:
@@ -1403,7 +1407,7 @@ def parse_early_return(indent_level, __, matched, source, syntax) -> Tuple[str,
14031407
return u('\n<% {keyword} %>\n').format(keyword=matched.group('keyword')), indent_level, '', source
14041408

14051409

1406-
def parse_implicit_literal(indent_level, __, matched, source, syntax) -> Tuple[str, int, str, str]:
1410+
def parse_implicit_literal(indent_level, __, matched, source: SourceIter, syntax) -> Parsed:
14071411
"""
14081412
14091413
:param indent_level:
@@ -1423,7 +1427,7 @@ def parse_implicit_literal(indent_level, __, matched, source, syntax) -> Tuple[s
14231427
)
14241428

14251429

1426-
def parse_raw_html(indent_level, current_line, ___, source, syntax) -> Tuple[str, int, str, str]:
1430+
def parse_raw_html(indent_level, current_line, ___, source, syntax) -> Parsed:
14271431
"""
14281432
14291433
:param indent_level:
@@ -1457,7 +1461,7 @@ def parse_raw_html(indent_level, current_line, ___, source, syntax) -> Tuple[str
14571461
return joined(buf), 0, '', source
14581462

14591463

1460-
def parse_mako_one_liners(indent_level, __, matched, source, syntax) -> Tuple[str, int, str, str]:
1464+
def parse_mako_one_liners(indent_level, __, matched, source, syntax) -> Parsed:
14611465
"""
14621466
14631467
:param indent_level:
@@ -1478,7 +1482,7 @@ def parse_mako_one_liners(indent_level, __, matched, source, syntax) -> Tuple[st
14781482
return joined(buf), indent_level, '', source
14791483

14801484

1481-
def parse_def_block(indent_level: int, __, matched, source: str, syntax) -> Tuple[str, int, str, str]:
1485+
def parse_def_block(indent_level: int, __, matched, source: str, syntax) -> Parsed:
14821486
"""
14831487
14841488
:param indent_level:
@@ -1522,7 +1526,7 @@ def parse_def_block(indent_level: int, __, matched, source: str, syntax) -> Tupl
15221526
return joined(buf), 0, '', source
15231527

15241528

1525-
def parse_plim_tail(lineno, indent_level, tail_line, source, syntax):
1529+
def parse_plim_tail(lineno: int, indent_level, tail_line, source: SourceIter, syntax) -> Parsed:
15261530
"""
15271531
15281532
:param lineno:
@@ -1546,7 +1550,7 @@ def parse_plim_tail(lineno, indent_level, tail_line, source, syntax):
15461550

15471551
# Miscellaneous utilities
15481552
# ==================================================================================
1549-
def enumerate_source(source: str) -> Iterator[Tuple[int, str]]:
1553+
def enumerate_source(source: str) -> SourceIter:
15501554
"""
15511555
15521556
:param source:

0 commit comments

Comments
 (0)
Please sign in to comment.