Exemplo n.º 1
0
    def nextToken(self):
        while self.current:
            validelements = [(x,y) for x,y in self.alphabet.grammardict.items() if self.current[0] in y.first]
            if not validelements:
                if not self.generate_unknown:
                    raise Exception("Not found")
                string = self.current[0]
                self.consume()
                return unknownchar, string
            elif len(validelements) == 1:
                element = validelements[0][1]
                checker = load_checker(element)
                for size in range(element.maxsize or len(self.current), element.minsize, -1):
                    if checker.check(self.current[:size]):
                        break
                else:
                    raise Exception("Nothing consumed")
                string = self.current[:size]
                for _ in range(size):
                    self.consume()
                return validelements[0][0], string
            else:
                raise Exception("Multiple choices")

        return "EOF_TYPE", ""
Exemplo n.º 2
0
 def __init__(self, module):
     Checker.__init__(self)
     self._matchFun = module["matchFun"]
     auxdic = module.get('auxdic', {})
     self.auxgrammar = {}
     for key, value in auxdic.items():
         self.auxgrammar[key] = load_checker(value)
Exemplo n.º 3
0
 def __call__(self, inputstring): #-> set:
     result = set()
     for summary in self.searcher.search():
         typ = None
         name = None
         try:
             for mem in self.memorylist:
                 if summary["identifier"] in mem:
                     name = summary["identifier"]
                     try:
                         typ = mem.load(name)
                     except:
                         LOG.exception("Error while loading memory %s" % name)
                         continue
                     break
             else:
                 continue # not found 
             checker = load_checker(typ)
             if checker.check(inputstring):
                 result.add(str(name))
         except TypeError:
             continue
     return result
Exemplo n.º 4
0
 def __auxcheck(self, specdict, data):
     """Recursive checker implementation"""
     for key, spec in specdict.items():
         value = data.get(key)
         if key == "$or" and len(specdict) == 1:
             return any([self.__auxcheck(x, data) for x in spec])
         elif isinstance(spec, dict) and len(spec) == 1:
             operator = list(spec.keys())[0]
             operand = list(spec.values())[0]
             if operator == "$type":
                 if not load_checker(operand).check(str(value)):
                     return False
             elif operator == "$or":
                 if not any([self.__auxcheck({key:x}, data) for x in operand]):
                     return False
             else: #unknown operator
                 return spec == value
         elif isinstance(spec, dict):
             if not self.__auxcheck(spec, value):
                 return False
         else:
             if spec != value: 
                 return False
     return True
Exemplo n.º 5
0
 def __init__(self, gd):
     Checker.__init__(self)
     self.gd = gd
     from pydsl.Memory.Loader import load_checker
     self.checkerinstances = [load_checker(x) for x in self.gd.grammardict]
Exemplo n.º 6
0
 def checker(self):
     if self.__checker is None:
         from pydsl.Memory.Loader import load_checker
         self.__checker = load_checker(self.grammarname)
     return self.__checker
Exemplo n.º 7
0
def function(inputdic, inputgt, outputgt):
    grammarname =  str(inputdic['grammar'])
    from pydsl.Memory.Loader import load_checker
    grammar = load_checker(grammarname)
    result = grammar.check(inputdic['string'])
    return {"output":str(result)}
Exemplo n.º 8
0
 def testloadgrammar(self):
     #Load a grammar that uses a package (integerOPGrammar), call check
     from pydsl.Memory.Loader import load_checker # FIXME: is load_grammar_tool
     grammar = load_checker("integerop")
     self.assertTrue(grammar.check("123+3"))
Exemplo n.º 9
0
 def check(self, word):
     if not self._checker:
         from pydsl.Memory.Loader import load_checker
         self._checker = load_checker(self.gd)
     return self._checker.check(word)
Exemplo n.º 10
0
 def testChecker(self):
     checker = load_checker(self.alphabet)
     self.assertTrue(checker.check(["1234","11/11/1991"]))
     self.assertFalse(checker.check(["bcdf"]))