text
stringlengths
0
828
if len(cls.rules) > 1:
raise CantCreateSingleRuleException(cls)
return cls.rules[0][0]"
4004,"def _get_rule(cls):
# type: (_MetaRule) -> (List[object], List[object])
""""""
Get rule on the Rule class.
:param cls: Rule for which return the rule.
:return: Rule inside the Rule class.
:raise RuleNotDefinedException: If the rule is not defined.
:raise CantCreateSingleRuleException: If the rule consists of more rules.
:raise NotASingleSymbolException: If number of symbols on the left is more.
""""""
if cls._traverse:
return (cls.left, cls.right)
if len(cls.rules) > 1:
raise CantCreateSingleRuleException(cls)
return cls.rules[0]"
4005,"def _get_rules(cls):
# type: (_MetaRule) -> List[(List[object], List[object])]
""""""
Get rules on the Rule class.
:param cls: Rule for which return the rules.
:return: Rules inside the Rule class.
:raise RuleNotDefinedException: If the rule is not defined.
:raise CantCreateSingleRuleException: If the rule consists of more rules.
:raise NotASingleSymbolException: If number of symbols on the left is more.
""""""
cls._traverse = True
r = cls.rule
cls._traverse = False
return [r]"
4006,"def _controlSide(cls, side, grammar):
# type: (_MetaRule, List[object], Grammar) -> None
""""""
Validate one side of the rule.
:param side: Iterable side of the rule.
:param grammar: Grammar on which to validate.
:raise RuleSyntaxException: If invalid syntax is use.
:raise UselessEpsilonException: If useless epsilon is used.
:raise TerminalDoesNotExistsException: If terminal does not exists in the grammar.
:raise NonterminalDoesNotExistsException: If nonterminal does not exists in the grammar.
""""""
if not isinstance(side, list):
raise RuleSyntaxException(cls, 'One side of rule is not enclose by list', side)
if len(side) == 0:
raise RuleSyntaxException(cls, 'One side of rule is not define', side)
if EPS in side and len(side) > 1:
raise UselessEpsilonException(cls)
for symb in side:
if isclass(symb) and issubclass(symb, Nonterminal):
if symb not in grammar.nonterminals:
raise NonterminalDoesNotExistsException(cls, symb, grammar)
elif symb is EPS:
continue
elif symb not in grammar.terminals:
raise TerminalDoesNotExistsException(cls, symb, grammar)"
4007,"def validate(cls, grammar):
# type: (_MetaRule, Grammar) -> None
""""""
Perform rules validation of the class.
:param grammar: Grammar on which to validate.
:raise RuleSyntaxException: If invalid syntax is used.
:raise UselessEpsilonException: If epsilon used in rules in useless.
:raise TerminalDoesNotExistsException: If terminal does not exists in the grammar.
:raise NonterminalDoesNotExistsException: If nonterminal does not exists in the grammar.
""""""
# check if the rule is not defined multiple times
defined = set(dir(cls))
if 'rules' in defined and len(defined & {'rule', 'left', 'right', 'toSymbol', 'fromSymbol'}) > 0 or \
'rule' in defined and len(defined & {'left', 'right', 'toSymbol', 'fromSymbol'}) > 0 or \
'left' in defined and 'fromSymbol' in defined or \
'right' in defined and 'toSymbol' in defined:
raise MultipleDefinitionException(cls, 'Rule is defined multiple times')
# check if the rule is defined properly
all = cls.rules
if not isinstance(all, list):
raise RuleSyntaxException(cls, 'Rules property is not enclose in list')
for rule in all:
if not isinstance(rule, tuple):
raise RuleSyntaxException(cls, 'One of the rules is not enclose in tuple', rule)
if len(rule) != 2:
raise RuleSyntaxException(cls, 'One of the rules does not have define left and right part', rule)
left = rule[0]
right = rule[1]
cls._controlSide(left, grammar)
cls._controlSide(right, grammar)
if left == [EPS] and right == [EPS]:
raise UselessEpsilonException(cls)"
4008,"def no_empty_value(func):
""""""Raises an exception if function argument is empty.""""""
@wraps(func)
def wrapper(value):
if not value:
raise Exception(""Empty value not allowed"")
return func(value)
return wrapper"
4009,"def to_bool(value):
""""""Converts human boolean-like values to Python boolean.