예제 #1
0
    def test_simple_sequence(self):
        pars = self.token_parse_text("this is 1 first test 2 3 4")

        p_number = pars.Production(
            "number", Or([Terminal(str(x)) for x in range(0, 10)]))

        p_words = pars.Production("words",
                                  Repeat(Terminal(typ=self.lxtok.WORD)))

        root = pars.run()
        self.assertIsNotNone(root)

        self.assertEqual(
            self.get_flat(root),
            [
                "---root---",
                "words",
                ("this", "WORD"),
                ("is", "WORD"),
                "number",
                ("1", "UINT"),
                "words",
                ("first", "WORD"),
                ("test", "WORD"),
                "number",
                ("2", "UINT"),
                "number",
                ("3", "UINT"),
                "number",
                ("4", "UINT"),
            ],
        )
예제 #2
0
    def test_no_number(self):
        pars = self.token_parse_text("1a")

        p_number = pars.Production(
            "number", Or([Terminal(str(x)) for x in range(0, 10)]))

        self.assertRaises(NoSolution, pars.run)
예제 #3
0
    def test_opt_minus_number(self):
        pars = self.token_parse_text("1 - 2 3 4")

        p_minus = pars.Production("minus", Terminal(typ=self.lxtok.MINUS))

        p_number = pars.Production(
            "number", Or([Terminal(str(x)) for x in range(0, 10)]))

        p_opt_minus_number = pars.Production(
            "opt_minus_number", And([Optional(p_minus),
                                     pars.Call(p_number)]))

        root = pars.run()
        self.assertIsNotNone(root)

        self.assertEqual(
            self.get_flat(root),
            [
                "---root---",
                "number",
                ("1", "UINT"),
                "opt_minus_number",
                "minus",
                ("-", "MINUS"),
                "number",
                ("2", "UINT"),
                "number",
                ("3", "UINT"),
                "number",
                ("4", "UINT"),
            ],
        )
예제 #4
0
    def test_number(self):
        pars = self.token_parse_text("1")

        p_number = pars.Production(
            "number", Or([Terminal(str(x)) for x in range(0, 10)]))

        root = pars.run()
        self.assertIsNotNone(root)
예제 #5
0
    def test_opt_repeat_minus_or_plus_number(self):
        pars = self.token_parse_text("1 - 2 3 + - 4 5")

        p_minus = pars.Production("minus", Terminal(typ=self.lxtok.MINUS))
        p_plus = pars.Production("plus", Terminal(typ=self.lxtok.PLUS))

        p_number = pars.Production(
            "number", Or([Terminal(str(x)) for x in range(0, 10)]))

        p_opt_minus_number = pars.Production(
            "opt_repeat_minus_plus_number",
            And([
                Optional(Repeat(Or([p_minus, p_plus]), name="opt_sign")),
                pars.Call(p_number),
            ]),
        )

        root = pars.run()
        self.assertIsNotNone(root)

        self.assertEqual(
            self.get_flat(root),
            [
                "---root---",
                "number",
                ("1", "UINT"),
                "opt_repeat_minus_plus_number",
                "*opt_sign",
                "minus",
                ("-", "MINUS"),
                "number",
                ("2", "UINT"),
                "number",
                ("3", "UINT"),
                "opt_repeat_minus_plus_number",
                "*opt_sign",
                "plus",
                ("+", "PLUS"),
                "minus",
                ("-", "MINUS"),
                "number",
                ("4", "UINT"),
                "number",
                ("5", "UINT"),
            ],
        )
예제 #6
0
    def test_many_number(self):
        pars = self.token_parse_text("1 2 3 4")

        p_number = pars.Production(
            "number", Or([Terminal(str(x)) for x in range(0, 10)]))

        root = pars.run()
        self.assertIsNotNone(root)

        for e in root.get_tail():
            self.assertEqual(e.get_head(), "number")
예제 #7
0
파일: sample.py 프로젝트: kr-g/pyparsesynt
stream = lexx.tokenize(inp_text, )
lxtok = LexerTokens(lexx.tokens)

stream = list(Sanitizer().whitespace(stream, keep=[]))

pars = Parser()
pars.set_input(stream)

# p_assign = pars.Production("assignment", Terminal(":=="))

p_minus = pars.Production("minus", Terminal("-"))
p_plus = pars.Production("plus", Terminal(typ=lxtok.PLUS))

p_zahl_ohne_null_5 = pars.Production(
    "zahl_ohne_null",
    Or([Terminal(str(x)) for x in range(1, 5)]),
    alias="lower_5")

p_zahl_ohne_null_10 = pars.Production(
    "zahl_ohne_null",
    Or([Terminal(str(x)) for x in range(5, 10)]),
    alias="higher_or_equal_5",
)

p_zahl = pars.Production(
    "zahl",
    Or([
        Terminal(str(0)),
        pars.Call(p_zahl_ohne_null_5),
    ]),
)