MCPcopy Create free account
hub / github.com/nodejs/node / parse

Method parse

deps/v8/third_party/jinja2/ext.py:354–474  ·  view source on GitHub ↗

Parse a translatable tag.

(self, parser: "Parser")

Source from the content-addressed store, hash-verified

352 return extract_from_ast(source, gettext_functions)
353
354 def parse(self, parser: "Parser") -> t.Union[nodes.Node, t.List[nodes.Node]]:
355 """Parse a translatable tag."""
356 lineno = next(parser.stream).lineno
357
358 context = None
359 context_token = parser.stream.next_if("string")
360
361 if context_token is not None:
362 context = context_token.value
363
364 # find all the variables referenced. Additionally a variable can be
365 # defined in the body of the trans block too, but this is checked at
366 # a later state.
367 plural_expr: t.Optional[nodes.Expr] = None
368 plural_expr_assignment: t.Optional[nodes.Assign] = None
369 num_called_num = False
370 variables: t.Dict[str, nodes.Expr] = {}
371 trimmed = None
372 while parser.stream.current.type != "block_end":
373 if variables:
374 parser.stream.expect("comma")
375
376 # skip colon for python compatibility
377 if parser.stream.skip_if("colon"):
378 break
379
380 token = parser.stream.expect("name")
381 if token.value in variables:
382 parser.fail(
383 f"translatable variable {token.value!r} defined twice.",
384 token.lineno,
385 exc=TemplateAssertionError,
386 )
387
388 # expressions
389 if parser.stream.current.type == "assign":
390 next(parser.stream)
391 variables[token.value] = var = parser.parse_expression()
392 elif trimmed is None and token.value in ("trimmed", "notrimmed"):
393 trimmed = token.value == "trimmed"
394 continue
395 else:
396 variables[token.value] = var = nodes.Name(token.value, "load")
397
398 if plural_expr is None:
399 if isinstance(var, nodes.Call):
400 plural_expr = nodes.Name("_trans", "load")
401 variables[token.value] = plural_expr
402 plural_expr_assignment = nodes.Assign(
403 nodes.Name("_trans", "store"), var
404 )
405 else:
406 plural_expr = var
407 num_called_num = token.value == "num"
408
409 parser.stream.expect("block_end")
410
411 plural = None

Callers

nothing calls this directly

Calls 14

_parse_blockMethod · 0.95
_trim_whitespaceMethod · 0.95
_make_nodeMethod · 0.95
updateMethod · 0.65
nextFunction · 0.50
setFunction · 0.50
next_ifMethod · 0.45
expectMethod · 0.45
skip_ifMethod · 0.45
failMethod · 0.45
parse_expressionMethod · 0.45
NameMethod · 0.45

Tested by

no test coverage detected