MCPcopy Create free account
hub / github.com/googleprojectzero/domato / _generate

Method _generate

grammar.py:367–425  ·  view source on GitHub ↗

Generates a user-defined symbol. Selects a rule for the given symbol and resolves the right-hand side of the rule. Args: symbol: The name of the symbol that is being resolved. context: dictionary consisting of: 'lastvar': Index of las

(self, symbol, context,
                  recursion_depth=0, force_nonrecursive=False)

Source from the content-addressed store, hash-verified

365 return creators[idx]
366
367 def _generate(self, symbol, context,
368 recursion_depth=0, force_nonrecursive=False):
369 """Generates a user-defined symbol.
370
371 Selects a rule for the given symbol and resolves the right-hand side
372 of the rule.
373
374 Args:
375 symbol: The name of the symbol that is being resolved.
376 context: dictionary consisting of:
377 'lastvar': Index of last variable created.
378 'lines': Generated lines of code
379 (for programming language generation).
380 'variables': A dictionary containing the names of all
381 variables created so far.
382 recursion_depth: Current recursion depth
383 force_nonrecursive: Whether to force the use of only
384 non-recursive rules.
385
386 Returns:
387 A string containing the expansion of the symbol.
388
389 Raises:
390 GrammarError: If grammar description is incorrect causing
391 some rules being impossible to resolve
392 RecursionError: If maximum recursion level was reached.
393 """
394
395 # print symbol
396
397 # print 'Expanding ' + symbol + ' in depth ' + str(recursion_depth)
398
399 force_var_reuse = context['force_var_reuse']
400
401 # Check if we already have a variable of the given type.
402 if (symbol in context['variables'] and
403 symbol not in _NONINTERESTING_TYPES):
404 # print symbol + ':' + str(len(context['variables'][symbol])) + ':' + str(force_var_reuse)
405 if (force_var_reuse or
406 random.random() < self._var_reuse_prob or
407 len(context['variables'][symbol]) > self._max_vars_of_same_type):
408 # print 'reusing existing var of type ' + symbol
409 context['force_var_reuse'] = False
410 variables = context['variables'][symbol]
411 return variables[random.randint(0, len(variables) - 1)]
412 # print 'Not reusing existing var of type ' + symbol
413
414 creator = self._select_creator(
415 symbol,
416 recursion_depth,
417 force_nonrecursive
418 )
419 return self._expand_rule(
420 symbol,
421 creator,
422 context,
423 recursion_depth,
424 force_nonrecursive

Callers 3

_expand_ruleMethod · 0.95
generate_rootMethod · 0.95
generate_symbolMethod · 0.95

Calls 2

_select_creatorMethod · 0.95
_expand_ruleMethod · 0.95

Tested by

no test coverage detected