Пример #1
0
    def test_holereachability11(self):
        lang = DefineLanguage('Lang', [
            DefineLanguage.NtDefinition(Nt('n', 'n'), [
                BuiltInPat(BuiltInPatKind.Number, 'number', 'number'),
            ]),
            DefineLanguage.NtDefinition(Nt('P', 'P'), [
                PatSequence([Nt('E', 'E_0')]),
            ]),
            DefineLanguage.NtDefinition(Nt('E', 'E'), [
                Nt('P', 'P_1'),
                PatSequence([
                    Repeat(PatSequence([
                        Nt('P', 'P'),
                    ])),
                    PatSequence([]),
                ]),
                BuiltInPat(BuiltInPatKind.Hole, 'hole', 'hole'),
            ]),
        ])

        graph = NtGraphBuilder(lang).run()
        DefineLanguage_HoleReachabilitySolver(lang, graph).run()
        self.assertEqual(result(lang, 'n'),
                         (NumberOfHoles.Zero, NumberOfHoles.Zero))
        self.assertEqual(result(lang, 'E'),
                         (NumberOfHoles.Zero, NumberOfHoles.Many))
        self.assertEqual(result(lang, 'P'),
                         (NumberOfHoles.Zero, NumberOfHoles.Many))
Пример #2
0
    def test_cyclecheck2(self):
        lang = DefineLanguage('Lang', [
            DefineLanguage.NtDefinition(Nt('m', 'm'), [
                Nt('e', 'e'),
                Nt('m', 'm'),
            ]),
            DefineLanguage.NtDefinition(Nt('e', 'e'), [
                PatSequence([
                    Lit('+', LitKind.Variable),
                    Nt('e', 'e_1'),
                    Nt('e', 'e_2')
                ]),
                Nt('n', 'n'),
            ]),
            DefineLanguage.NtDefinition(Nt('n', 'n'), [
                BuiltInPat(BuiltInPatKind.Number, 'number', 'number'),
            ]),
        ])

        successors, _ = DefineLanguage_NtClosureSolver(lang).run()
        try:
            DefineLanguage_NtCycleChecker(lang, successors).run()
            self.fail('should throw')
        except CompilationError as ex:
            self.assertIn(str(ex), [genmsg(['m', 'm'])])
Пример #3
0
    def test_patternnumholes0(self):
        lang = DefineLanguage('Lang', [
            DefineLanguage.NtDefinition(Nt('n', 'n'), [
                BuiltInPat(BuiltInPatKind.Number, 'number', 'number'),
            ]),
            DefineLanguage.NtDefinition(Nt('P', 'P'), [
                PatSequence([Nt('E', 'E')]),
            ]),
            DefineLanguage.NtDefinition(Nt('E', 'E'), [
                PatSequence([Nt('E', 'E'), Nt('n', 'n')]),
                BuiltInPat(BuiltInPatKind.Hole, 'hole', 'hole'),
            ]),
        ])

        graph = NtGraphBuilder(lang).run()
        DefineLanguage_HoleReachabilitySolver(lang, graph).run()
        pat = PatSequence([Repeat(Nt('E', 'E'))])
        nmin, nmax = PatternNumHolesChecker(lang, pat).run()
        self.assertEqual(nmin, NumberOfHoles.Zero)
        self.assertEqual(nmax, NumberOfHoles.Many)

        pat = PatSequence([Nt('P', 'P'), Repeat(Nt('E', 'E'))])
        nmin, nmax = PatternNumHolesChecker(lang, pat).run()
        self.assertEqual(nmin, NumberOfHoles.One)
        self.assertEqual(nmax, NumberOfHoles.Many)

        pat = PatSequence([Repeat(InHole(Nt('E', 'E'), Nt('n', 'n')))])
        nmin, nmax = PatternNumHolesChecker(lang, pat).run()
        self.assertEqual(nmin, NumberOfHoles.Zero)
        self.assertEqual(nmax, NumberOfHoles.Zero)

        pat = PatSequence([Nt('P', 'P'), Nt('n', 'n'), Nt('E', 'E')])
        nmin, nmax = PatternNumHolesChecker(lang, pat).run()
        self.assertEqual(nmin, NumberOfHoles.Many)
        self.assertEqual(nmax, NumberOfHoles.Many)

        pat = PatSequence([
            Nt('n', 'n'),
            BuiltInPat(BuiltInPatKind.Number, 'number', 'number'),
            BuiltInPat(BuiltInPatKind.Hole, 'hole', 'hole')
        ])
        nmin, nmax = PatternNumHolesChecker(lang, pat).run()
        self.assertEqual(nmin, NumberOfHoles.One)
        self.assertEqual(nmax, NumberOfHoles.One)
Пример #4
0
    def test_holereachability12(self):
        lang = DefineLanguage('Lang', [
            DefineLanguage.NtDefinition(Nt('n', 'n'), [
                BuiltInPat(BuiltInPatKind.Number, 'number', 'number'),
            ]),
            DefineLanguage.NtDefinition(Nt('P', 'P'), [
                PatSequence([Nt('E', 'E_0')]),
            ]),
            DefineLanguage.NtDefinition(Nt('E', 'E'), [
                Nt('P', 'P_1'),
                InHole(Nt('P', 'P'), Nt('n', 'n')),
                BuiltInPat(BuiltInPatKind.Hole, 'hole', 'hole'),
            ]),
        ])

        try:
            graph = NtGraphBuilder(lang).run()
            DefineLanguage_HoleReachabilitySolver(lang, graph).run()
            self.fail()
        except CompilationError as ex:
            self.assertEqual(str(ex), 'in-hole pattern in define-language')
Пример #5
0
    def test_computeclosure(self):
        lang = DefineLanguage('Lang', [
            DefineLanguage.NtDefinition(Nt('m', 'm'), [
                Nt('e', 'e'),
                PatSequence([
                    Lit('-', LitKind.Variable),
                    Nt('m', 'm_1'),
                    Nt('m', 'm_2')
                ]),
            ]),
            DefineLanguage.NtDefinition(Nt('e', 'e'), [
                PatSequence([
                    Lit('+', LitKind.Variable),
                    Nt('e', 'e_1'),
                    Nt('e', 'e_2')
                ]),
                Nt('n', 'n'),
            ]),
            DefineLanguage.NtDefinition(Nt('n', 'n'), [
                BuiltInPat(BuiltInPatKind.Number, 'number', 'number'),
            ]),
            DefineLanguage.NtDefinition(Nt('z', 'z'), [
                Nt('n', 'n'),
            ]),
            DefineLanguage.NtDefinition(Nt('y', 'y'), [
                BuiltInPat(BuiltInPatKind.Hole, 'hole', 'hole'),
            ]),
        ])

        _, closure = DefineLanguage_NtClosureSolver(lang).run()

        self.assertSetEqual(closure['m'], {'e', 'n', 'number'})
        self.assertSetEqual(closure['e'], {'n', 'number'})
        self.assertSetEqual(closure['z'], {'n', 'number'})
        self.assertSetEqual(closure['n'], {'number'})
        self.assertSetEqual(closure['y'], {'hole'})
Пример #6
0
    def test_holereachability19(self):
        lang = DefineLanguage('Lang', [
            DefineLanguage.NtDefinition(Nt('P', 'P'), [
                PatSequence([Nt('E', 'E_0')]),
            ]),
            DefineLanguage.NtDefinition(Nt('E', 'E'), [
                PatSequence([Lit(44, LitKind.Integer)]),
                BuiltInPat(BuiltInPatKind.Hole, 'hole', 'hole'),
            ]),
        ])

        graph = NtGraphBuilder(lang).run()
        DefineLanguage_HoleReachabilitySolver(lang, graph).run()
        self.assertEqual(result(lang, 'E'),
                         (NumberOfHoles.Zero, NumberOfHoles.One))
        self.assertEqual(result(lang, 'P'),
                         (NumberOfHoles.Zero, NumberOfHoles.One))
Пример #7
0
    def test_det_1(self):
        lang = DefineLanguage('Lang', [
            DefineLanguage.NtDefinition(Nt('m', 'm'), [
                Nt('e', 'e'),
                PatSequence([
                    Lit('-', LitKind.Variable),
                    Nt('m', 'm_1'),
                    Nt('m', 'm_2')
                ]),
            ]),
            DefineLanguage.NtDefinition(Nt('e', 'e'), [
                PatSequence([
                    Lit('+', LitKind.Variable),
                    Nt('e', 'e_1'),
                    Nt('e', 'e_2')
                ]),
                Nt('n', 'n'),
            ]),
            DefineLanguage.NtDefinition(Nt('n', 'n'), [
                BuiltInPat(BuiltInPatKind.Number, 'number', 'number'),
            ]),
            DefineLanguage.NtDefinition(Nt('z', 'z'), [
                BuiltInPat(BuiltInPatKind.Number, 'number', 'number'),
            ]),
            DefineLanguage.NtDefinition(Nt('h', 'h'), [
                BuiltInPat(BuiltInPatKind.Hole, 'hole', 'hole'),
            ]),
        ])

        _, closure = DefineLanguage_NtClosureSolver(lang).run()

        #( number ... number ...)
        pat = PatSequence([
            Repeat(BuiltInPat(BuiltInPatKind.Number, 'number', 'number')),
            Repeat(BuiltInPat(BuiltInPatKind.Number, 'number', 'number')),
        ])

        expected = PatSequence([
            Repeat(BuiltInPat(BuiltInPatKind.Number, 'number', 'number')),
            Repeat(BuiltInPat(BuiltInPatKind.Number, 'number', 'number'),
                   RepeatMatchMode.Deterministic),
        ])

        actual = Pattern_EllipsisMatchModeRewriter(lang, pat, closure).run()
        self.assertEqual(actual, expected)

        # (e e ... m ... n)  no deterministm possible
        pat = PatSequence([
            Nt('e', 'e'),
            Repeat(Nt('e', 'e')),
            Repeat(Nt('m', 'm')),
            Nt('n', 'n')
        ])
        actual = Pattern_EllipsisMatchModeRewriter(lang, pat, closure).run()
        self.assertEqual(actual, pat)
        # (e ... number ... m ...) # s can be matched deterministically
        pat = PatSequence([
            Repeat(Nt('e', 'e')),
            Repeat(BuiltInPat(BuiltInPatKind.Number, 'number', 'number')),
            Repeat(Nt('m', 'm')),
        ])

        expected = PatSequence([
            Repeat(Nt('e', 'e')),
            Repeat(BuiltInPat(BuiltInPatKind.Number, 'number', 'number')),
            Repeat(Nt('m', 'm'), RepeatMatchMode.Deterministic),
        ])

        actual = Pattern_EllipsisMatchModeRewriter(lang, pat, closure).run()
        self.assertEqual(actual, expected)

        # (e e ... m ... h)  m should be deterministic
        pat = PatSequence([
            Nt('e', 'e'),
            Repeat(Nt('e', 'e')),
            Repeat(Nt('m', 'm')),
            Nt('h', 'h')
        ])

        expected = PatSequence([
            Nt('e', 'e'),
            Repeat(Nt('e', 'e')),
            Repeat(Nt('m', 'm'), RepeatMatchMode.Deterministic),
            Nt('h', 'h')
        ])

        actual = Pattern_EllipsisMatchModeRewriter(lang, pat, closure).run()
        self.assertEqual(actual, expected)

        # (e e ... m ... h ...)  m and h should be deterministic.
        pat = PatSequence([
            Nt('e', 'e'),
            Repeat(Nt('e', 'e')),
            Repeat(Nt('m', 'm')),
            Repeat(Nt('h', 'h'))
        ])

        expected = PatSequence([
            Nt('e', 'e'),
            Repeat(Nt('e', 'e')),
            Repeat(Nt('m', 'm'), RepeatMatchMode.Deterministic),
            Repeat(Nt('h', 'h'), RepeatMatchMode.Deterministic)
        ])

        actual = Pattern_EllipsisMatchModeRewriter(lang, pat, closure).run()
        self.assertEqual(actual, expected)

        # ((e) ... (m) ... (h) ...) m and h should be deterministic.
        pat = PatSequence([
            Repeat(PatSequence([Nt('e', 'e')])),
            Repeat(PatSequence([Nt('m', 'm')])),
            Repeat(PatSequence([Nt('h', 'h')])),
        ])

        expected = PatSequence([
            Repeat(PatSequence([Nt('e', 'e')])),
            Repeat(PatSequence([Nt('m', 'm')]), RepeatMatchMode.Deterministic),
            Repeat(PatSequence([Nt('h', 'h')]), RepeatMatchMode.Deterministic),
        ])

        actual = Pattern_EllipsisMatchModeRewriter(lang, pat, closure).run()
        self.assertEqual(actual, expected)

        #((e ...) ... (m ...) ... (m ... h ...) ...) -> (m ... h ...) term can be matched deterministically
        pat = PatSequence([
            Repeat(PatSequence([
                Repeat(Nt('e', 'e')),
            ])),
            Repeat(PatSequence([
                Repeat(Nt('m', 'm')),
            ])),
            Repeat(PatSequence([
                Repeat(Nt('m', 'm')),
                Repeat(Nt('h', 'h')),
            ])),
        ])

        expected = PatSequence([
            Repeat(
                PatSequence([
                    Repeat(Nt('e', 'e'), RepeatMatchMode.Deterministic),
                ])),
            Repeat(
                PatSequence([
                    Repeat(Nt('m', 'm'), RepeatMatchMode.Deterministic),
                ])),
            Repeat(
                PatSequence([
                    Repeat(Nt('m', 'm'), RepeatMatchMode.Deterministic),
                    Repeat(Nt('h', 'h'), RepeatMatchMode.Deterministic),
                ]), RepeatMatchMode.Deterministic),
        ])

        actual = Pattern_EllipsisMatchModeRewriter(lang, pat, closure).run()
        self.assertEqual(actual, expected)

        #((e ...) ... (m ... h ...) ... (m ... h ...)) ->  nondeterministc
        pat = PatSequence([
            Repeat(PatSequence([
                Repeat(Nt('e', 'e')),
            ])),
            Repeat(PatSequence([
                Repeat(Nt('m', 'm')),
                Repeat(Nt('h', 'h')),
            ])),
            PatSequence([
                Repeat(Nt('m', 'm')),
                Repeat(Nt('h', 'h')),
            ]),
        ])

        expected = PatSequence([
            Repeat(
                PatSequence([
                    Repeat(Nt('e', 'e'), RepeatMatchMode.Deterministic),
                ])),
            Repeat(
                PatSequence([
                    Repeat(Nt('m', 'm'), RepeatMatchMode.Deterministic),
                    Repeat(Nt('h', 'h'), RepeatMatchMode.Deterministic),
                ])),
            PatSequence([
                Repeat(Nt('m', 'm'), RepeatMatchMode.Deterministic),
                Repeat(Nt('h', 'h'), RepeatMatchMode.Deterministic),
            ]),
        ])

        actual = Pattern_EllipsisMatchModeRewriter(lang, pat, closure).run()
        self.assertEqual(actual, expected)

        # ((n h ... n) ... (m e) ...)
        pat = PatSequence([
            Repeat(
                PatSequence([
                    Nt('n', 'n'),
                    Repeat(Nt('h', 'h')),
                    Nt('n', 'n'),
                ])),
            Repeat(PatSequence([
                Nt('m', 'm'),
                Nt('e', 'e'),
            ])),
        ])

        expected = PatSequence([
            Repeat(
                PatSequence([
                    Nt('n', 'n'),
                    Repeat(Nt('h', 'h'), RepeatMatchMode.Deterministic),
                    Nt('n', 'n'),
                ])),
            Repeat(PatSequence([
                Nt('m', 'm'),
                Nt('e', 'e'),
            ]), RepeatMatchMode.Deterministic),
        ])

        actual = Pattern_EllipsisMatchModeRewriter(lang, pat, closure).run()
        self.assertEqual(actual, expected)

        # ((n "string" n) ... (m "string" e) ...)
        pat = PatSequence([
            Repeat(
                PatSequence([
                    Nt('n', 'n'),
                    Lit("string", LitKind.String),
                    Nt('n', 'n'),
                ])),
            Repeat(
                PatSequence([
                    Nt('m', 'm'),
                    Lit("string", LitKind.String),
                    Nt('e', 'e'),
                ]), RepeatMatchMode.Deterministic),
        ])

        actual = Pattern_EllipsisMatchModeRewriter(lang, pat, closure).run()
        self.assertEqual(actual, pat)

        # ((n "string" n) ... (m "another_string" e) ...)
        pat = PatSequence([
            Repeat(
                PatSequence([
                    Nt('n', 'n'),
                    Lit("string", LitKind.String),
                    Nt('n', 'n'),
                ])),
            Repeat(
                PatSequence([
                    Nt('m', 'm'),
                    Lit("another_string", LitKind.String),
                    Nt('e', 'e'),
                ])),
        ])

        expected = PatSequence([
            Repeat(
                PatSequence([
                    Nt('n', 'n'),
                    Lit("string", LitKind.String),
                    Nt('n', 'n'),
                ]), RepeatMatchMode.Deterministic),
            Repeat(
                PatSequence([
                    Nt('m', 'm'),
                    Lit("another_string", LitKind.String),
                    Nt('e', 'e'),
                ]), RepeatMatchMode.Deterministic),
        ])

        actual = Pattern_EllipsisMatchModeRewriter(lang, pat, closure).run()
        self.assertEqual(actual, expected)