def testLogicalExpression(self): repository = {'TrueFalse':load_bnf_file("pydsl/contrib/grammar/TrueFalse.bnf")} productionrulesetlogical = load_bnf_file("pydsl/contrib/grammar/LogicalExpression.bnf", repository) parser = BacktracingErrorRecursiveDescentParser(productionrulesetlogical) result = parser.get_trees("True&&False") self.assertTrue(result) result = parser.get_trees("True&|False") self.assertFalse(result)
def testLogicalExpression(self): repository = {'TrueFalse':load_bnf_file("pydsl/contrib/grammar/TrueFalse.bnf")} productionrulesetlogical = load_bnf_file("pydsl/contrib/grammar/LogicalExpression.bnf", repository) parser = BacktracingErrorRecursiveDescentParser(productionrulesetlogical) tokens = [x[0] for x in lex(productionrulesetlogical.alphabet, ascii_encoding, "True&&False")] result = parser.get_trees(tokens) self.assertTrue(result) result = parser.get_trees("True&|False") self.assertFalse(result)
def testLogicalExp(self): repository = {'TrueFalse':load_bnf_file("pydsl/contrib/grammar/TrueFalse.bnf")} productionrulesetlogical = load_bnf_file("pydsl/contrib/grammar/LogicalExpression.bnf", repository) parser = BacktracingErrorRecursiveDescentParser(productionrulesetlogical) tokens = [x[0] for x in lex(repository['TrueFalse'].alphabet, ascii_encoding, self.tokelist5)] self.assertEqual(len(tokens), 1) #tokens = [x[0] for x in lex(productionrulesetlogical.alphabet, Encoding('ascii'), tokens)] #FIXME tokens = [['True']] result = parser.get_trees(tokens) self.assertTrue(result)
def testLogLine(self): repository = { 'space': RegularExpression("^ $"), 'integer': RegularExpression("^[0123456789]*$"), 'ipv4': RegularExpression( "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$"), 'characters': RegularExpression("^[A-z]+$") } grammar = load_bnf_file("pydsl/contrib/grammar/logline.bnf", repository) checker = checker_factory(grammar) original_string = "1.2.3.4 - - [1/1/2003:11:11:11 +2] \"GET\" 1 1 \"referer\" \"useragent\"" tokenized = [ x.content for x in lex(grammar.alphabet, ascii_encoding, original_string) ] self.assertTrue(checker.check(tokenized)) self.assertFalse( checker.check( "1.2.3.4 - - [1/1/2003:11:11:11 +2] \"GOT\" 1 1 \"referer\" \"useragent\"" ))
def testLogicalExpression(self): repository = { 'TrueFalse': load_bnf_file("pydsl/contrib/grammar/TrueFalse.bnf") } productionrulesetlogical = load_bnf_file( "pydsl/contrib/grammar/LogicalExpression.bnf", repository) parser = BacktracingErrorRecursiveDescentParser( productionrulesetlogical) tokens = [ x[0] for x in lex(productionrulesetlogical.alphabet, ascii_encoding, "True&&False") ] result = parser.get_trees(tokens) self.assertTrue(result) result = parser.get_trees("True&|False") self.assertFalse(result)
def testOverlappingLexing(self): integer = RegularExpression("^[0123456789]*$") date = load_bnf_file("pydsl/contrib/grammar/Date.bnf", {'integer':integer, 'DayOfMonth':load_python_file('pydsl/contrib/grammar/DayOfMonth.py')}) mydef = GrammarCollection([integer,date]) lexer = lexer_factory(mydef) self.assertListEqual(lexer("123411/11/2001"), [("1234", integer),("11/11/2001", date)]) self.assertListEqual(lexer([x for x in "123411/11/2001"]), [("1234", integer),("11/11/2001", date)])
def testSimpleLexing(self): """Test checker instantiation and call""" integer = RegularExpression("^[0123456789]*$") date = load_bnf_file("pydsl/contrib/grammar/Date.bnf", {'integer':integer, 'DayOfMonth':load_python_file('pydsl/contrib/grammar/DayOfMonth.py')}) mydef = Choice([integer,date]) lexer = lexer_factory(mydef) self.assertListEqual(lexer("1234"), [("1234", integer)]) self.assertListEqual(lexer("123411/11/2001"), [("1234", integer),("11/11/2001", date)])
def testHTMLTable(self): repository = {'integer':RegularExpression("^[0123456789]*$")} productionrulesetlogical = load_bnf_file("pydsl/contrib/grammar/TrueHTMLTable.bnf", repository) parser = BacktracingErrorRecursiveDescentParser(productionrulesetlogical) result = parser.get_trees("<table><tr><td>1</td></tr></table>") self.assertTrue(result) result = parser.get_trees("<trble><tr><td>1</td></tr></table>") self.assertFalse(result)
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 testSimpleLexing(self): """Test checker instantiation and call""" integer = RegularExpression("^[0123456789]*$") date = load_bnf_file("pydsl/contrib/grammar/Date.bnf", {'integer':integer, 'DayOfMonth':load_python_file('pydsl/contrib/grammar/DayOfMonth.py')}) mydef = GrammarCollection([integer,date]) lexer = lexer_factory(mydef) self.assertListEqual(lexer("1234"), [(["1","2","3","4"], integer)]) self.assertListEqual(lexer([x for x in "1234"]), [(["1","2","3","4"], integer)])
def testLogLine(self): repository = {'space':RegularExpression("^ $"), 'integer':RegularExpression("^[0123456789]*$"), 'ipv4':RegularExpression("^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$"), 'characters':RegularExpression("^[A-z]+$")} grammar = load_bnf_file("pydsl/contrib/grammar/logline.bnf", repository) checker = checker_factory(grammar) self.assertTrue(checker.check("1.2.3.4 - - [1/1/2003:11:11:11 +2] \"GET\" 1 1 \"referer\" \"useragent\"")) self.assertFalse(checker.check("1.2.3.4 - - [1/1/2003:11:11:11 +2] \"GOT\" 1 1 \"referer\" \"useragent\""))
def testLogicalExp(self): repository = { 'TrueFalse': load_bnf_file("pydsl/contrib/grammar/TrueFalse.bnf") } productionrulesetlogical = load_bnf_file( "pydsl/contrib/grammar/LogicalExpression.bnf", repository) parser = BacktracingErrorRecursiveDescentParser( productionrulesetlogical) tokens = [ x[0] for x in lex(repository['TrueFalse'].alphabet, ascii_encoding, self.tokelist5) ] self.assertEqual(len(tokens), 1) from pydsl.Lex import common_ancestor self.assertEqual(common_ancestor(productionrulesetlogical.alphabet), None) #tokens = [x[0] for x in lex(productionrulesetlogical.alphabet, Encoding('ascii'), tokens)] #FIXME tokens = [['True']] result = parser.get_trees(tokens) self.assertTrue(result)
def testTrueFalse(self): productionrulesetlogical = load_bnf_file( "pydsl/contrib/grammar/TrueFalse.bnf") parser = BacktracingErrorRecursiveDescentParser( productionrulesetlogical) tokens = [ x[0] for x in lex(productionrulesetlogical.alphabet, ascii_encoding, self.tokelist5) ] result = parser.get_trees(tokens) self.assertTrue(result)
def testLogLine(self): repository = {'space':RegularExpression("^ $"), 'integer':RegularExpression("^[0123456789]*$"), 'ipv4':RegularExpression("^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$"), 'characters':RegularExpression("^[A-z]+$")} grammar = load_bnf_file("pydsl/contrib/grammar/logline.bnf", repository) checker = checker_factory(grammar) original_string = "1.2.3.4 - - [1/1/2003:11:11:11 +2] \"GET\" 1 1 \"referer\" \"useragent\"" tokenized = [x.content for x in lex(grammar.alphabet, ascii_encoding, original_string)] self.assertTrue(checker.check(tokenized)) self.assertFalse(checker.check("1.2.3.4 - - [1/1/2003:11:11:11 +2] \"GOT\" 1 1 \"referer\" \"useragent\""))
def testHTMLTable(self): repository = {'integer':RegularExpression("^[0123456789]*$")} productionrulesetlogical = load_bnf_file("pydsl/contrib/grammar/TrueHTMLTable.bnf", repository) parser = BacktracingErrorRecursiveDescentParser(productionrulesetlogical) lexed = lex(productionrulesetlogical.alphabet, ascii_encoding, "<table><tr><td>1</td></tr></table>") self.assertTrue(lexed) lexed = [x.content for x in lexed] result = parser.get_trees(lexed) self.assertTrue(result) lexed = [x[0] for x in lex(productionrulesetlogical.alphabet, ascii_encoding, "<table><td>1</td></tr></table>")] result = parser.get_trees(lexed) self.assertFalse(result)
def testEmptyInput(self): integer = RegularExpression("^[0123456789]*$") date = load_bnf_file( "pydsl/contrib/grammar/Date.bnf", { 'integer': integer, 'DayOfMonth': load_python_file('pydsl/contrib/grammar/DayOfMonth.py') }) mydef = GrammarCollection([integer, date]) lexer = lexer_factory(mydef) self.assertFalse(lexer(""))
def testOverlappingLexing(self): integer = RegularExpression("^[0123456789]*$") date = load_bnf_file( "pydsl/contrib/grammar/Date.bnf", { 'integer': integer, 'DayOfMonth': load_python_file('pydsl/contrib/grammar/DayOfMonth.py') }) mydef = GrammarCollection([integer, date]) lexer = lexer_factory(mydef) self.assertListEqual(lexer("123411/11/2001"), [("1234", integer), ("11/11/2001", date)]) self.assertListEqual(lexer([x for x in "123411/11/2001"]), [("1234", integer), ("11/11/2001", date)])
def testSimpleLexing(self): """Test checker instantiation and call""" integer = RegularExpression("^[0123456789]*$") date = load_bnf_file( "pydsl/contrib/grammar/Date.bnf", { 'integer': integer, 'DayOfMonth': load_python_file('pydsl/contrib/grammar/DayOfMonth.py') }) mydef = GrammarCollection([integer, date]) lexer = lexer_factory(mydef) self.assertListEqual(lexer("1234"), [(["1", "2", "3", "4"], integer)]) self.assertListEqual(lexer([x for x in "1234"]), [(["1", "2", "3", "4"], integer)])
def testHTMLTable(self): repository = {'integer': RegularExpression("^[0123456789]*$")} productionrulesetlogical = load_bnf_file( "pydsl/contrib/grammar/TrueHTMLTable.bnf", repository) parser = BacktracingErrorRecursiveDescentParser( productionrulesetlogical) lexed = lex(productionrulesetlogical.alphabet, ascii_encoding, "<table><tr><td>1</td></tr></table>") self.assertTrue(lexed) lexed = [x.content for x in lexed] result = parser.get_trees(lexed) self.assertTrue(result) lexed = [ x[0] for x in lex(productionrulesetlogical.alphabet, ascii_encoding, "<table><td>1</td></tr></table>") ] result = parser.get_trees(lexed) self.assertFalse(result)
def testTrueFalse(self): productionrulesetlogical = load_bnf_file("pydsl/contrib/grammar/TrueFalse.bnf") parser = BacktracingErrorRecursiveDescentParser(productionrulesetlogical) result = parser.get_trees(self.tokelist5) self.assertTrue(result)
def testEmptyInput(self): integer = RegularExpression("^[0123456789]*$") date = load_bnf_file("pydsl/contrib/grammar/Date.bnf", {'integer':integer, 'DayOfMonth':load_python_file('pydsl/contrib/grammar/DayOfMonth.py')}) mydef = GrammarCollection([integer,date]) lexer = lexer_factory(mydef) self.assertFalse(lexer(""))
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 setUp(self): self.integer = RegularExpression("^[0123456789]*$") self.date = load_bnf_file("pydsl/contrib/grammar/Date.bnf", {'integer':self.integer, 'DayOfMonth':load_python_file('pydsl/contrib/grammar/DayOfMonth.py')})
def testTrueFalse(self): productionrulesetlogical = load_bnf_file("pydsl/contrib/grammar/TrueFalse.bnf") parser = BacktracingErrorRecursiveDescentParser(productionrulesetlogical) tokens = [x[0] for x in lex(productionrulesetlogical.alphabet, ascii_encoding, self.tokelist5)] result = parser.get_trees(tokens) self.assertTrue(result)