def testFileLoader(self): repository = { 'integer': RegularExpression("^[0123456789]*$"), 'DayOfMonth': load_python_file('pydsl/contrib/grammar/DayOfMonth.py') } self.assertTrue( load_bnf_file("pydsl/contrib/grammar/Date.bnf", repository))
def testEcho(self): from pydsl.translator import translate, PythonTranslator from pydsl.grammar.definition import RegularExpression from pydsl.check import checker_factory cstring = checker_factory(RegularExpression('.*')) def function(my_input): return my_input pt = PythonTranslator(function) self.assertEqual(translate(pt,{'my_input':"1234"}),"1234")
def load_re_from_file(filepath): """Converts a re file to Regular Grammar instance""" regexp = None with open(filepath, 'r') as mlfile: flagstr = "" for line in mlfile: cleanline = re.sub("//.*$", "", line) if re.search("^\s*$", cleanline): continue if re.search("^#.*$", cleanline): flagstr = cleanline[1:] continue if regexp is not None: raise Exception("Regular expression file format error") else: regexp = cleanline.rstrip('\n') flags = 0 if "i" in flagstr: flags |= re.I from pydsl.grammar.definition import RegularExpression return RegularExpression(regexp, flags)
def testFirst(self): re1 = RegularExpression(re.compile('^a$')) self.assertEqual(len(re1.first()),1) from pydsl.grammar.definition import String self.assertEqual(re1.first()[0],String('a'))
def testAlphabet(self): from pydsl.encoding import ascii_encoding re1 = RegularExpression(re.compile('^a$')) self.assertEqual(re1.alphabet, ascii_encoding)
def testMax(self): re1 = RegularExpression(re.compile('^a$')) re1.maxsize
def testMin(self): re1 = RegularExpression(re.compile('^a$')) re1.minsize
def testFirst(self): re1 = RegularExpression(re.compile('^a$')) self.assertEqual(len(re1.first),1) from pydsl.grammar.definition import String self.assertIn(String('a'), re1.first)
def testEnumerate(self): re1 = RegularExpression(re.compile('^a$')) self.assertRaises(NotImplementedError, re1.enum)
def testInstantiation(self): re1 = RegularExpression('^a$') re2 = RegularExpression(re.compile('^a$')) self.assertEqual(str(re1), str(re2)) #FIXME python3 default flag value is 32
#productionset0 definition symbol1 = TerminalSymbol(String("S")) symbol2 = TerminalSymbol(String("R")) final1 = NonTerminalSymbol("exp") rule1 = Production([final1], (symbol1, symbol2)) productionset0 = BNFGrammar(final1, (rule1, symbol1, symbol2)) p0good = "SR" p0bad = "RS" #productionset1 definition symbol1 = TerminalSymbol(String("S")) symbol2 = TerminalSymbol(String("R")) symbol3 = TerminalSymbol(String(":")) symbol4 = TerminalSymbol(RegularExpression("^[0123456789]*$")) symbol5 = TerminalSymbol(load_python_file('pydsl/contrib/grammar/cstring.py')) final1 = NonTerminalSymbol("storeexp") final2 = NonTerminalSymbol("retrieveexp") final3 = NonTerminalSymbol("exp") rule1 = Production([final1], (symbol1, symbol3, symbol5)) rule2 = Production([final2], (symbol2, symbol3, symbol4)) rule3 = Production([final3], [final1]) rule4 = Production([final3], [final2]) rulelist = (rule1, rule2, rule3, rule4, symbol1, symbol2, symbol3, symbol4, symbol5) productionset1 = BNFGrammar(final3, rulelist) #productionset2 definition symbola = TerminalSymbol(String("A")) symbolb = TerminalSymbol(String("B"))