Beispiel #1
0
    def test_18_hookNoParam(self):
        @meta.hook(parsing.Parser)
        def my_hook(self):
            self.the_hook = True
            return True

        bnf = dsl.EBNF("""
            the_rule = [ #my_hook ]
        """)
        res = bnf.get_rules()
        self.assertTrue('the_rule' in res)
        self.assertIsInstance(res['the_rule'], parsing.Hook)
        self.assertTrue(res['the_rule'].name == "my_hook")
        dummyData = parsing.Parser()
        dummyData.set_rules(res)
        dummyData.the_hook = False
        res = dummyData.eval_rule('the_rule')
        self.assertTrue(res)
        self.assertTrue(dummyData.the_hook)
        bnf = dsl.EBNF("""
            the_rule = [ #my_hook() ]
        """)
        res = bnf.get_rules()
        self.assertTrue('the_rule' in res)
        self.assertIsInstance(res['the_rule'], parsing.Hook)
        self.assertTrue(res['the_rule'].name == "my_hook")
        dummyData = parsing.Parser()
        dummyData.set_rules(res)
        dummyData.the_hook = False
        res = dummyData.eval_rule('the_rule')
        self.assertTrue(res)
        self.assertTrue(dummyData.the_hook)
Beispiel #2
0
 def test_02_readInteger(self):
     """
         Basic test for integer parsing
     """
     parser = parsing.Parser()
     parser.parsed_stream("12 333 44444444444444444444444444", name="root")
     self.assertTrue(
         parser.begin_tag('n1') and
         parser.read_integer() and
         parser.end_tag('n1'),
         'failed in read_integer for n1')
     n1 = parser.get_tag('n1')
     parser.skip_ignore()
     self.assertEqual(str(n1), "12", "failed in capture n1")
     self.assertTrue(
         parser.begin_tag('n2') and
         parser.read_integer() and
         parser.end_tag('n2'),
         'failed in read_integer for n2')
     n2 = parser.get_tag('n2')
     parser.skip_ignore()
     self.assertEqual(str(n2), "333", "failed in capture n2")
     self.assertTrue(
         parser.begin_tag('n3') and
         parser.read_integer() and
         parser.end_tag('n3'),
         'failed in read_integer for n3')
     n3 = parser.get_tag('n3')
     self.assertEqual(str(n3), "44444444444444444444444444",
                      "failed in capture n3")
Beispiel #3
0
    def test_28_errors(self):
        with self.assertRaises(TypeError):

            @meta.hook(parsing.Parser, "num")
            def check5(self):
                pass

        with self.assertRaises(TypeError):

            @meta.rule(parsing.Parser, "Base.read_char")
            def check6(self):
                pass

        with self.assertRaises(TypeError):

            @meta.hook(parsing.Parser, "plop")
            def check7(self):
                pass

            bnf = dsl.EBNF("""
                main = [ #plop ]
            """)
            res = bnf.get_rules()
            p = parsing.Parser("")
            p.set_rules(res)
            p.eval_rule('main')
Beispiel #4
0
 def test_05_readCString(self):
     """
         Basic test for read_cstring
     """
     parser = parsing.Parser()
     parser.parsed_stream(
         r'"premiere chaine"'
         r'"deuxieme chaine\n"'
         r'"troisieme chainee \"."',
         name="root")
     self.assertTrue(
         parser.begin_tag('s1') and
         parser.read_cstring() and
         parser.end_tag('s1'),
         'failed in read_cstring for s1')
     s1 = parser.get_tag('s1')
     parser.skip_ignore()
     self.assertEqual(str(s1), '"premiere chaine"', "failed in capture s1")
     self.assertTrue(
         parser.begin_tag('s2') and
         parser.read_cstring() and
         parser.end_tag('s2'),
         'failed in read_cstring for s2')
     s2 = parser.get_tag('s2')
     parser.skip_ignore()
     self.assertEqual(str(s2), '"deuxieme chaine\\n"',
                      "failed in capture s2")
     self.assertTrue(
         parser.begin_tag('s3') and
         parser.read_cstring() and
         parser.end_tag('s3'),
         'failed in read_cstring for s3')
     s3 = parser.get_tag('s3')
     self.assertEqual(str(s3), r'"troisieme chainee \"."',
                      "failed in capture s3")
Beispiel #5
0
    def test_24_hookAndCapture(self):
        @meta.hook(parsing.Parser)
        def my_hook_multi(self, n1, n2, n3):
            self.test.assertTrue(self.value(n1) == "456")
            self.test.assertTrue(self.value(n2) == '"toto"')
            self.test.assertTrue(self.value(n3) == "blabla")
            return True

        bnf = dsl.EBNF("""

            N = [ Base.num ]

            S = [ Base.string ]

            I = [ Base.id ]

            the_rule = [ N:nth S:t I:i
                         #my_hook_multi(nth, t, i)
            ]
        """)
        res = bnf.get_rules()
        self.assertTrue('the_rule' in res)
        self.assertIsInstance(res['the_rule'], parsing.Seq)
        self.assertTrue(res['the_rule'][-1].name == "my_hook_multi")
        dummyData = parsing.Parser("""
            456    "toto"        blabla
            """)
        dummyData.set_rules(res)
        dummyData.test = self
        #with dummyData as s:
        eval_res = dummyData.eval_rule('the_rule')
        self.assertTrue(eval_res)
Beispiel #6
0
 def test_07_RepXN(self):
     """
         Basic test for repeater operator
     """
     parser = parsing.Parser()
     parser.parsed_stream("12343 91219****1323 23")
     parseTree = parsing.Seq(
         parsing.Call(parsing.Parser.begin_tag, 'i1'),
         parsing.Parser.read_integer,
         parsing.Call(parsing.Parser.end_tag, 'i1'),
         parsing.Rep0N(parsing.Call(parsing.Parser.read_char, '*')),
         parsing.Call(parsing.Parser.begin_tag, 'i2'),
         parsing.Rep1N(parsing.Call(parsing.Parser.read_range, '0', '9')),
         parsing.Call(parsing.Parser.end_tag, 'i2'),
         parsing.Rep0N(parsing.Call(parsing.Parser.read_char, '*')),
         parsing.Call(parsing.Parser.begin_tag, 'i3'),
         parsing.Parser.read_integer,
         parsing.Call(parsing.Parser.end_tag, 'i3'),
         parsing.Call(parsing.Parser.read_eof))
     parseTree(parser)
     # Warning! skip_ignore is called between each parsing.Seq
     self.assertEqual(str(parser.get_tag("i1")), "12343 ",
                      "failed in captured i1")
     self.assertEqual(str(parser.get_tag("i2")), "91219",
                      "failed in captured i2")
     self.assertEqual(str(parser.get_tag("i3")), "1323 ",
                      "failed in captured i3")
Beispiel #7
0
 def test_01_readIdentifier(self):
     """
         Basic test for identifier parsing
     """
     parser = parsing.Parser()
     parser.parsed_stream("ceci est un test", name="root")
     self.assertTrue(
         parser.begin_tag('sujet') and
         parser.read_identifier() and
         parser.end_tag('sujet'),
         'failed in read_identifier for sujet')
     sujet = parser.get_tag('sujet')
     parser.skip_ignore()
     self.assertEqual(str(sujet), "ceci", "failed in capture sujet")
     self.assertTrue(
         parser.begin_tag('verbe') and
         parser.read_identifier() and
         parser.end_tag('verbe'),
         'failed in read_identifier for verbe')
     verbe = parser.get_tag('verbe')
     parser.skip_ignore()
     self.assertEqual(str(verbe), "est", "failed in capture verbe")
     self.assertTrue(
         parser.begin_tag('other') and
         parser.read_until_eof() and
         parser.end_tag('other'),
         'failed in read_identifier for other')
     reste = parser.get_tag('other')
     self.assertEqual(str(reste), "un test", "failed in capture other")
Beispiel #8
0
    def test_11_namespaceRules(self):
        """
        Test the namespace handling
        """
        parser = parsing.Parser()

        @meta.add_method(parsing.Parser)
        def dummy(self):
            res = parsing.Node()
            res.text = "cool"
            self.rule_nodes["_"] = res
            return True
        meta.set_one(parsing.Parser._rules, "A.B.C.test",
                     parsing.Call(parsing.Parser.dummy))
        bRes = parser.eval_rule('test')
        self.assertEqual(bRes.text, "cool",
                         "failed rule node in global namespace")
        bRes = parser.eval_rule('C.test')
        self.assertEqual(bRes.text, "cool",
                         "failed rule node in global namespace")
        bRes = parser.eval_rule('B.C.test')
        self.assertEqual(bRes.text, "cool",
                         "failed rule node in global namespace")
        bRes = parser.eval_rule('A.B.C.test')
        self.assertEqual(bRes.text, "cool",
                         "failed rule node in global namespace")
Beispiel #9
0
    def test_27_nodescope(self):
        @meta.hook(parsing.Parser)
        def put(self, ast):
            # A.put visible in subrules
            ast.put = True
            return True

        @meta.hook(parsing.Parser)
        def check1(self):
            self.test.assertTrue('A' in self.rule_nodes)
            # _ is from rule1, not main
            self.test.assertFalse(hasattr(self.rule_nodes['_'], 'put'))
            # return of rule1 with .toto == True
            self.rule_nodes['_'].toto = True
            return True

        @meta.hook(parsing.Parser)
        def check2(self):
            self.test.assertTrue('A' in self.rule_nodes)
            self.test.assertTrue('B' in self.rule_nodes)
            return False

        @meta.hook(parsing.Parser)
        def check3(self):
            self.test.assertTrue('A' in self.rule_nodes)
            # B no more living (alternative)
            self.test.assertFalse('B' in self.rule_nodes)
            return True

        @meta.hook(parsing.Parser)
        def toto(self):
            self.test.assertTrue(hasattr(self.rule_nodes['r'], 'toto'))
            self.test.assertTrue(hasattr(self.rule_nodes['r'], 'bla'))
            return True

        @meta.hook(parsing.Parser)
        def check4(self):
            self.rule_nodes['_'].bla = True
            return True

        bnf = dsl.EBNF("""
            main =
            [ __scope__:A #put(_)
                rule1:r #toto eof
            ]

            rule1 =
            [
                #check1 __scope__:B #check2
                | #check3 #check4
            ]
        """)
        res = bnf.get_rules()
        self.assertTrue('main' in res)
        self.assertTrue('rule1' in res)
        dummyData = parsing.Parser("")
        dummyData.set_rules(res)
        dummyData.test = self
        #with dummyData as s:
        eval_res = dummyData.eval_rule('main')
Beispiel #10
0
    def test_25_list_id(self):
        @meta.hook(parsing.Parser)
        def in_list(self, ls, ident):
            if not hasattr(ls, 'list'):
                ls.list = []
            ls.list.append(self.value(ident))
            return True

        bnf = dsl.EBNF("""

            I = [ id ]

            list = [ [I : i #in_list(_, i) ]+ ]
        """)
        res = bnf.get_rules()
        self.assertTrue('list' in res)
        dummyData = parsing.Parser("""
            a     b c   d        e   f
        """)
        dummyData.set_rules(res)
        dummyData.test = self
        #with dummyData as s:
        eval_res = dummyData.eval_rule('list')
        self.assertTrue(eval_res)
        self.assertTrue(eval_res.list[0] == "a")
        self.assertTrue(eval_res.list[1] == "b")
        self.assertTrue(eval_res.list[2] == "c")
        self.assertTrue(eval_res.list[3] == "d")
        self.assertTrue(eval_res.list[4] == "e")
        self.assertTrue(eval_res.list[5] == "f")
Beispiel #11
0
    def test_09_RepRules(self):
        """
            Basic test for Rules
        """
        def check_word(parser, test, tutu):
            test.assertIn(parser.value(tutu), ('asbga', 'njnj'))
            return True

        def check_int(parser, test, toto):
            test.assertIn(parser.value(toto), ('12121', '89898'))
            return True

        check_word = mock.Mock(side_effect=check_word)
        check_int = mock.Mock(side_effect=check_int)

        parser = parsing.Parser()
        parser.parsed_stream("asbga    12121      njnj 89898")
        parser.rule_nodes['test'] = self
        parser.set_hooks({'checkWord': check_word, 'checkInt': check_int})
        parser.set_rules({
            'main':
            parsing.Seq(
                parsing.Rep0N(
                    parsing.Alt(
                        parsing.Seq(
                            parsing.Capture('tutu', parsing.Rule('word')),
                            parsing.Hook('checkWord',
                                         [("test", parsing.Node),
                                          ("tutu", parsing.Node)])),
                        parsing.Rule('int'))), parsing.Rule('Base.eof')),
            'word':
            parsing.Scope(
                parsing.Call(parsing.Parser.push_ignore,
                             parsing.Parser.ignore_null),
                parsing.Call(parsing.Parser.pop_ignore),
                parsing.Rep1N(
                    parsing.Alt(
                        parsing.Call(parsing.Parser.read_range, 'a', 'z'),
                        parsing.Call(parsing.Parser.read_range, 'A', 'Z')))),
            'int':
            parsing.Seq(
                parsing.Scope(
                    parsing.Call(parsing.Parser.push_ignore,
                                 parsing.Parser.ignore_null),
                    parsing.Call(parsing.Parser.pop_ignore),
                    parsing.Capture(
                        'toto',
                        parsing.Rep1N(
                            parsing.Call(parsing.Parser.read_range, '0',
                                         '9')))),
                parsing.Hook('checkInt', [("test", parsing.Node),
                                          ("toto", parsing.Node)]))
        })
        res = parser.eval_rule('main')
        self.assertTrue(res, "failed to parse")
        self.assertEqual(2, check_word.call_count)
        self.assertEqual(2, check_int.call_count)
Beispiel #12
0
 def test_13_defaultRules(self):
     """
     Test the presence of default rules
     """
     parser = parsing.Parser()
     self.assertTrue("num" in parser._rules, "failed no found Base.num")
     self.assertTrue("Base.num" in parser._rules,
                     "failed no found Base.num")
     self.assertTrue("string" in parser._rules,
                     "failed no found Base.string")
     self.assertTrue("Base.string" in parser._rules,
                     "failed no found Base.string")
Beispiel #13
0
 def test_19_Until(self):
     """
     Basic test for until ->R
     """
     parser = parsing.Parser()
     parser.parsed_stream("==|=|==tutu")
     parseTree = parsing.Seq(
         parsing.Until(parsing.Call(parsing.Parser.read_text, '|==')),
         parsing.Call(parsing.Parser.read_text, 'tutu'),
     )
     res = parseTree(parser)
     self.assertEqual(res, True, "failed to get the correct final value")
Beispiel #14
0
 def test_16_Negation(self):
     """
     Basic test for negation !R
     """
     parser = parsing.Parser()
     parser.parsed_stream("==")
     parseTree = \
         parsing.Seq(parsing.Call(parsing.Parser.read_char, '='),
                     parsing.Neg(parsing.Call(
                         parsing.Parser.read_char,
                         '=')))
     res = parseTree(parser)
     self.assertEqual(res, False, "failed to get the correct final value")
     self.assertEqual(parser._stream._cursor._index, 0,
                      "failed to get the correct index after a negation")
Beispiel #15
0
 def test_17_Lookahead(self):
     """
     Basic test for lookahead !!R
     """
     parser = parsing.Parser()
     parser.parsed_stream("==")
     parseTree = \
         parsing.Seq(parsing.Call(parsing.Parser.read_char, '='),
                     parsing.LookAhead(parsing.Call(
                         parsing.Parser.read_char,
                         '=')),
                     )
     res = parseTree(parser)
     self.assertEqual(res, True, "failed to get the correct final value")
     self.assertEqual(parser._stream._cursor._index, 1,
                      "failed to get the correct index after a lookahead")
Beispiel #16
0
 def test_04_readCChar(self):
     """
         Basic test for read_cchar
     """
     parser = parsing.Parser()
     parser.parsed_stream(r"'c' '\t'", name="root")
     self.assertTrue(
         parser.begin_tag('c1') and parser.read_cchar()
         and parser.end_tag('c1'), 'failed in read_cchar for c1')
     c1 = parser.get_tag('c1')
     parser.skip_ignore()
     self.assertEqual(str(c1), "'c'", "failed in capture c1")
     self.assertTrue(
         parser.begin_tag('c2') and parser.read_cchar()
         and parser.end_tag('c2'), 'failed in read_cchar for c2')
     c2 = parser.get_tag('c2')
     self.assertEqual(str(c2), r"'\t'", "failed in capture c2")
Beispiel #17
0
 def test_15_error_index(self):
     """
     Test error index
     """
     parser = parsing.Parser()
     parser.parsed_stream("bla\n12abcd\nblu")
     self.assertTrue(parser.read_text("bla\n") and parser.read_integer(),
                     "failed to parse begin of stream")
     self.assertTrue(not parser.read_text("abcde"),
                     "failed to not parse abcde")
     self.assertEqual(parser._stream[parser._stream._cursor
                      .max_readed_position.index],
                      'a',
                      "failed when checking the correction position of last"
                      " readed character")
     self.assertEqual(parser._stream.last_readed_line, "12abcd",
                      "failed to get the correct last readed line")
Beispiel #18
0
 def test_18_Complement(self):
     """
     Basic test for complement ~R
     """
     parser = parsing.Parser()
     parser.parsed_stream("==")
     parseTree = parsing.Seq(
         parsing.Call(parsing.Parser.read_char, '='),
         parsing.Complement(parsing.Call(parsing.Parser.read_char, '=')))
     res = parseTree(parser)
     self.assertEqual(res, False, "failed to get the correct final value")
     self.assertEqual(parser._stream._cursor._index, 0,
                      "failed to get the correct index after a lookahead")
     parser.parsed_stream("=+")
     res = parseTree(parser)
     self.assertEqual(res, True, "failed to get the correct final value")
     self.assertEqual(parser._stream._cursor._index, 2,
                      "failed to get the correct index after a lookahead")
Beispiel #19
0
 def test_08_RepAlt(self):
     """
         Basic test for alternatives
     """
     parser = parsing.Parser()
     parser.parsed_stream("_ad121dwdw ()[]")
     parseTree = parsing.Seq(
         parsing.Call(parsing.Parser.begin_tag, 'w1'),
         parsing.Scope(
             begin=parsing.Call(parsing.Parser.push_ignore,
                                parsing.Parser.ignore_null),
             end=parsing.Call(parsing.Parser.pop_ignore),
             pt=parsing.Seq(
                 parsing.Alt(
                     parsing.Call(parsing.Parser.read_char, '_'),
                     parsing.Call(parsing.Parser.read_range, 'a', 'z'),
                     parsing.Call(parsing.Parser.read_range, 'A', 'Z')
                 ),
                 parsing.Rep0N(
                     parsing.Alt(
                         parsing.Call(parsing.Parser.read_char, '_'),
                         parsing.Call(parsing.Parser.read_range, 'a', 'z'),
                         parsing.Call(parsing.Parser.read_range, 'A', 'Z'),
                         parsing.Call(parsing.Parser.read_range,
                                      '0', '9'))))),
         parsing.Call(parsing.Parser.end_tag, 'w1'),
         parsing.Capture(
             'w2',
             parsing.Rep1N(
                 parsing.Alt(
                     parsing.Call(parsing.Parser.read_char, '('),
                     parsing.Call(parsing.Parser.read_char, ')'),
                     parsing.Call(parsing.Parser.read_char, '['),
                     parsing.Call(parsing.Parser.read_char, ']'),
                 )
             )),
         parsing.Call(parsing.Parser.read_eof)
     )
     parseTree(parser)
     # Warning! skip_ignore is called between each parsing.Seq
     self.assertEqual(str(parser.get_tag("w1")), "_ad121dwdw ",
                      "failed in captured w1")
     self.assertEqual(str(parser.get_tag("w2")), "()[]",
                      "failed in captured w2")
Beispiel #20
0
    def test_20_hookOneParamChar(self):
        @meta.hook(parsing.Parser)
        def my_hook_char(self, txt):
            self.test.assertEqual(txt, "\t", 'failed to receive "\t" in hook')
            return True

        bnf = dsl.EBNF("""
            the_rule = [ #my_hook_char('\t') ]
        """)
        res = bnf.get_rules()
        self.assertTrue('the_rule' in res, "failed to fetch the rule name")
        self.assertIsInstance(res['the_rule'], parsing.Hook)
        self.assertTrue(res['the_rule'].name == "my_hook_char")
        dummyData = parsing.Parser()
        dummyData.set_rules(res)
        dummyData.test = self
        #with dummyData as s:
        res = dummyData.eval_rule('the_rule')
        self.assertTrue(res)
Beispiel #21
0
    def test_22_hookOneParamId(self):
        @meta.hook(parsing.Parser)
        def my_hook_id(self, n):
            self.test.assertIsInstance(n, parsing.Node)
            return True

        bnf = dsl.EBNF("""
            the_rule = [ #my_hook_id(_) ]
        """)
        res = bnf.get_rules()
        self.assertTrue('the_rule' in res)
        self.assertIsInstance(res['the_rule'], parsing.Hook)
        self.assertTrue(res['the_rule'].name == "my_hook_id")
        dummyData = parsing.Parser()
        dummyData.set_rules(res)
        dummyData.test = self
        #with dummyData as s:
        res = dummyData.eval_rule('the_rule')
        self.assertTrue(res)
Beispiel #22
0
    def test_26_set(self):
        class dummyList(parsing.Node):
            def __init__(self):
                self._ls = []

            def append(self, x):
                self._ls.append(x)

            def __getitem__(self, n):
                return self._ls[n]

        @meta.hook(parsing.Parser)
        def in_list(self, ls, ident):
            if type(ls) is parsing.Node:
                ls.set(dummyList())
            ls.append(self.value(ident))
            return True

        bnf = dsl.EBNF("""

            I = [ id ]

            list = [ [I : i #in_list(_, i) ]+ ]
        """)
        res = bnf.get_rules()
        self.assertTrue('list' in res)
        self.assertTrue('I' in res)
        dummyData = parsing.Parser("""
            a     b c   d        e   f
        """)
        dummyData.set_rules(res)
        dummyData.test = self
        #with dummyData as s:
        eval_res = dummyData.eval_rule('list')
        self.assertTrue(eval_res)
        self.assertTrue(eval_res[0] == "a")
        self.assertTrue(eval_res[1] == "b")
        self.assertTrue(eval_res[2] == "c")
        self.assertTrue(eval_res[3] == "d")
        self.assertTrue(eval_res[4] == "e")
        self.assertTrue(eval_res[5] == "f")
Beispiel #23
0
    def test_21_hookOneParamNum(self):
        @meta.hook(parsing.Parser)
        def my_hook_num(self, num):
            self.test.assertEqual(num, 123456,
                                  'failed to receive 123456 in hook')
            self.test.assertTrue(num == 123456)
            return True

        bnf = dsl.EBNF("""
            the_rule = [ #my_hook_num(123456) ]
        """)
        res = bnf.get_rules()
        self.assertTrue('the_rule' in res, "failed to fetch the rule name")
        self.assertIsInstance(res['the_rule'], parsing.Hook)
        self.assertTrue(res['the_rule'].name == "my_hook_num")
        dummyData = parsing.Parser()
        dummyData.set_rules(res)
        dummyData.test = self
        #with dummyData as s:
        res = dummyData.eval_rule('the_rule')
        self.assertTrue(res)
Beispiel #24
0
    def test_23_hookParams(self):
        @meta.hook(parsing.Parser)
        def my_hook_params(self, n, num, txt):
            self.test.assertIsInstance(n, parsing.Node)
            self.test.assertTrue(num == 123456)
            self.test.assertTrue(txt == "cool")
            return True

        bnf = dsl.EBNF("""
            the_rule = [ #my_hook_params(_, 123456, "cool") ]
        """)
        res = bnf.get_rules()
        self.assertTrue('the_rule' in res)
        self.assertIsInstance(res['the_rule'], parsing.Hook)
        self.assertTrue(res['the_rule'].name == "my_hook_params")
        dummyData = parsing.Parser()
        dummyData.set_rules(res)
        dummyData.test = self
        #with dummyData as s:
        res = dummyData.eval_rule('the_rule')
        self.assertTrue(res)
Beispiel #25
0
 def test_10_contextVariables(self):
     """
     Basic test for context variables
     """
     parser = parsing.Parser()
     parser.rule_nodes.update({'coucou': 42,
                              'grigri': 666,
                              'toto': [12, 33]})
     self.assertEqual(parser.rule_nodes['toto'], [12, 33],
                      "failed comparing list")
     parser.push_rule_nodes()
     parser.rule_nodes.update({'local1': 666, 'local2': 777})
     parser.rule_nodes['toto'] = [1, 2, 3, 4]
     self.assertEqual(parser.rule_nodes['coucou'], 42,
                      "failed outer scope not visible in local")
     parser.push_rule_nodes()
     self.assertEqual(parser.rule_nodes['grigri'], 666,
                      "failed outer scope not visible in local")
     self.assertTrue('grigri' in parser.rule_nodes,
                     "failed outer scope not visible in local")
     parser.pop_rule_nodes()
Beispiel #26
0
    def test_25_directive(self):
        class dummyDir(parsing.DirectiveWrapper):
            def begin(self, parser, a: int, b: int, c: int):
                parser.test.assertTrue(a == 1)
                parser.test.assertTrue(b == 2)
                parser.test.assertTrue(c == 3)
                # for workflow checking
                parser.workflow = 1
                return True

            def end(self, parser, a: int, b: int, c: int):
                parser.test.assertTrue(a == 1)
                parser.test.assertTrue(b == 2)
                parser.test.assertTrue(c == 3)
                # for workflow checking
                parser.test.assertTrue(parser.workflow == 2)
                return True

        @meta.hook(parsing.Parser)
        def my_hook(self):
            # for workflow checking
            self.test.assertTrue(self.workflow == 1)
            self.workflow = 2
            return True

        dsl.EBNF.set_directives({'toto.dummyDir': dummyDir})
        bnf = dsl.EBNF("""
            the_rule = [ @toto.dummyDir(1, 2, 3) test ]

            test = [ #my_hook Base.eof ]
        """)
        res = bnf.get_rules()
        self.assertTrue('the_rule' in res, "failed to fetch the rule name")
        dummyData = parsing.Parser()
        dummyData.set_rules(res)
        dummyData.test = self
        #with dummyData as s:
        res = dummyData.eval_rule('the_rule')
        self.assertTrue(res)
Beispiel #27
0
 def test_06_CallAndSeq(self):
     """
         Basic test for call/clauses
     """
     parser = parsing.Parser()
     parser.parsed_stream("abc def ghg")
     parseTree = parsing.Seq(parsing.Call(parsing.Parser.begin_tag, 'i1'),
                             parsing.Parser.read_identifier,
                             parsing.Call(parsing.Parser.end_tag, 'i1'),
                             parsing.Call(parsing.Parser.begin_tag, 'i2'),
                             parsing.Parser.read_identifier,
                             parsing.Call(parsing.Parser.end_tag, 'i2'),
                             parsing.Call(parsing.Parser.begin_tag, 'i3'),
                             parsing.Parser.read_identifier,
                             parsing.Call(parsing.Parser.end_tag, 'i3'))
     parseTree(parser)
     # Warning! skip_ignore is called between each parsing.Seq
     self.assertEqual(str(parser.get_tag("i1")), "abc ",
                      "failed in captured i1")
     self.assertEqual(str(parser.get_tag("i2")), "def ",
                      "failed in captured i2")
     self.assertEqual(str(parser.get_tag("i3")), "ghg",
                      "failed in captured i3")