#!/usr/bin/env python
"""
:Author Patrik Valkovic
:Created 23.06.2017 16:39
:Licence GNUv3
Part of grammpy

"""

from .Rule import Rule
from .exceptions import RuleException, UselessEpsilonException, RuleSyntaxException, TerminalDoesNotExistsException, \
    NonterminalDoesNotExistsException


class IsMethodsRuleExtension(Rule):
    @classmethod
    def is_regular(cls):
        raise NotImplementedError()

    @classmethod
    def is_contextfree(cls):
        raise NotImplementedError()

    @classmethod
    def is_context(cls):
        raise NotImplementedError()

    @classmethod
    def is_unrestricted(cls):
        raise NotImplementedError()

    @staticmethod
    def _controlSide(cls, side, grammar):
        raise NotImplementedError()

    @classmethod
    def validate(cls, grammar):
        r = cls.rules
        if not isinstance(r, list):
            raise RuleSyntaxException(cls, 'Rules property is not enclosed in list')
        for rule in r:
            if not isinstance(rule, list):
                raise RuleSyntaxException(cls, 'One of the rules is not enclosed in tuple', rule)
            if len(rule) != 2:
                raise RuleSyntaxException(cls, 'One of the rules does not have defined left and right part', rule)
            l = rule[0]
            r = rule[1]
            IsMethodsRuleExtension._controlSide(cls, l, grammar)
            IsMethodsRuleExtension._controlSide(cls, r, grammar)

    @classmethod
    def is_valid(cls, grammar):
        try:
            cls.validate(grammar)
            return True
        except RuleException:
            return False

@@ -286,16 +286,43 @@
 ception%0A
+from .Constants import EPS%0A
 %0A%0Aclass 
@@ -748,35 +748,228 @@
     
-raise NotImplementedError()
+if not isinstance(side, list):%0A            raise RuleSyntaxException(cls, 'One side of rule is not enclose by list', side)%0A        if EPS in side and len(side) %3E 1:%0A            raise UselessEpsilonException(cls)%0A        
 %0A%0A  
@@ -1142,25 +1142,24 @@
  not enclose
-d
  in list')%0A 
@@ -1302,17 +1302,16 @@
  enclose
-d
  in tupl
@@ -1436,17 +1436,16 @@
 e define
-d
  left an

Add validation of epsilon