Beispiel #1
0
    def test_with_some_words_2(self):

        parser = ParserPython(document, skipws=False)

        text = f"{self.words1}\n\n"

        parsed = parser.parse(text)
        # print('\n: parsed') ; pp(parsed)

        expect = NonTerminal(document(), [
            NonTerminal(body(), [
                NonTerminal(element(), [
                    Terminal(words(), 0, self.words1),
                ]),
                NonTerminal(element(), [
                    Terminal(newline(), 0, '\n'),
                ]),
                NonTerminal(element(), [
                    Terminal(blank_line(), 0, '\n'),
                ]),
            ]),
            Terminal(EOF(), 0, ''),
        ])
        # print('\n: expect') ; pp(expect)

        assert parsed == expect, (
            f"text = '{text}' :\n"
            f"[expect]\n{pp_str(expect)}\n[parsed]\n{pp_str(parsed)}")
Beispiel #2
0
    def test_with_some_paragraphs(self):

        parser = ParserPython(document, skipws=False)

        paragraph = f"{self.words1}\n{self.words2}\n{self.words3}\n"

        text = paragraph + '\n' + paragraph

        parsed = parser.parse(text)
        # print('\n: parsed') ; pp(parsed)

        x_paragraph = [
            NonTerminal(element(), [
                Terminal(words(), 0, self.words1),
            ]),
            NonTerminal(element(), [
                Terminal(newline(), 0, '\n'),
            ]),
            NonTerminal(element(), [
                Terminal(words(), 0, self.words2),
            ]),
            NonTerminal(element(), [
                Terminal(newline(), 0, '\n'),
            ]),
            NonTerminal(element(), [
                Terminal(words(), 0, self.words3),
            ]),
            NonTerminal(element(), [
                Terminal(newline(), 0, '\n'),
            ]),
        ]

        expect = NonTerminal(document(), [
            NonTerminal(body(), [
                *x_paragraph,
                NonTerminal(element(), [
                    Terminal(blank_line(), 0, '\n'),
                ]),
                *x_paragraph,
            ]),
            Terminal(EOF(), 0, ''),
        ])
        # print('\n: expect') ; pp(expect)

        assert parsed == expect, (
            f"text = '{text}' :\n"
            f"[expect]\n{pp_str(expect)}\n[parsed]\n{pp_str(parsed)}")
Beispiel #3
0
def option_line_expect(*terminals,
                       eof=False,
                       sep=None,
                       indent=None,
                       gap=None,
                       help_=None,
                       extra=0):

    if len(terminals) <= 0:
        raise ValueError(
            "No terminals provided.  Please provide at least one.")

    separator = expect_separator(sep)
    sep_space = expect_separator(' ')  # required for operands
    # print("[sep-space]")
    # pp(sep_space)

    members = [
        NonTerminal(
            option_list(),
            [
                NonTerminal(ol_first_option(), [
                    terminals[0],
                ]),
                *[
                    NonTerminal(
                        ol_term_with_separator(),
                        [
                            # separator,
                            (sep_space
                             if term.rule_name == 'operand' else separator),
                            NonTerminal(ol_term(), [term]),
                        ]) for term in terminals[1:]
                ],
            ]),
        Terminal(newline(), 0, '\n'),
    ]

    if indent and len(indent) > 0:
        members.insert(0, Terminal(StrMatch(' ', rule_name='wx'), 0, indent))

    if help_ and len(help_) > 0:
        if extra < 0:
            extra = 0
        # print(f": extra = {extra}")
        gap += ' ' * extra
        members.insert(
            -1, Terminal(StrMatch(gap, rule_name='option_line_gap'), 0, gap))
        members.insert(
            -1, Terminal(StrMatch('.', rule_name='option_line_help'), 0,
                         help_))

    expect = NonTerminal(option_line(), [*members])

    if eof:
        expect.append(Terminal(EOF(), 0, ''))

    return expect
Beispiel #4
0
    def check_parse(self, fcn, rule_f, rule, words, text, expect_list):
        # Unused: fcn, rule_f
        newline = common.newline()
        catchall = RegExMatch(r'.*', rule_name='catch_all')
        elements = [rule, words, catchall, newline]

        def grammar():
            return Sequence((OneOrMore(OrderedChoice(elements)), EOF),
                            rule_name="grammar")

        self.verify_grammar(grammar, text, expect_list, skipws=False)
Beispiel #5
0
    def test_newline_elements_only(self):

        parser = ParserPython(document, skipws=False)

        text = '\n\n\n'
        parsed = parser.parse(text)
        # print('\n: parsed') ; pp(parsed)

        p_newline = Terminal(newline(), 0, '\n')
        p_element = NonTerminal(element(), [p_newline])
        p_body = NonTerminal(body(), [p_element, p_element, p_element])
        p_eof = Terminal(EOF(), 0, '')
        expect = NonTerminal(document(), [p_body, p_eof])
        # print('\n: expect') ; pp(expect)

        assert parsed == expect, (
            f"text = '{text}' :\n"
            f"[expect]\n{pp_str(expect)}\n[parsed]\n{pp_str(parsed)}")
Beispiel #6
0
    def test_words_and_newline(self):
        def document():
            return Sequence(words, newline, EOF, rule_name='document')

        # print('\n: document') ; pp(document())
        parser = ParserPython(document, skipws=False)

        text = self.words1 + '\n'
        parsed = parser.parse(text)
        # print('\n: parsed') ; pp(parsed)

        p_w1_words = Terminal(words(), 0, self.words1)
        p_newline = Terminal(newline(), 0, '\n')
        p_eof = Terminal(EOF(), 0, '')
        expect = NonTerminal(document(), [p_w1_words, p_newline, p_eof])
        # print('\n: expect') ; pp(expect)

        assert parsed == expect, (
            f"text = '{text}' :\n"
            f"[expect]\n{pp_str(expect)}\n[parsed]\n{pp_str(parsed)}")
Beispiel #7
0
    def test_with_some_words_1(self):

        parser = ParserPython(document, skipws=False)

        text = f"{self.words1}\n\n"

        parsed = parser.parse(text)
        # print('\n: parsed') ; pp(parsed)

        t_words1 = Terminal(words(), 0, self.words1)
        e_words1 = NonTerminal(element(), [t_words1])
        t_newline = Terminal(newline(), 0, '\n')
        e_newline = NonTerminal(element(), [t_newline])
        t_blank_line = Terminal(blank_line(), 0, '\n')
        e_blank_line = NonTerminal(element(), [t_blank_line])
        p_body = NonTerminal(body(), [e_words1, e_newline, e_blank_line])
        p_eof = Terminal(EOF(), 0, '')
        expect = NonTerminal(document(), [p_body, p_eof])
        # print('\n: expect') ; pp(expect)

        assert parsed == expect, (
            f"text = '{text}' :\n"
            f"[expect]\n{pp_str(expect)}\n[parsed]\n{pp_str(parsed)}")
Beispiel #8
0
    def builder(self, texts):

        p_newline = Terminal(newline(), 0, '\n')

        text = ''.join(flatten(texts))

        body_ = []
        for atom in texts:
            if atom == '\n':
                # print(f": atom = <newline>")
                body_.append(NonTerminal(element(), [p_newline]))
            else:
                # print(f": atom = '{atom}'")
                body_.append(
                    NonTerminal(element(), [Terminal(words(), 0, atom)]))

        p_body = NonTerminal(body(), body_)
        p_eof = Terminal(EOF(), 0, '')
        expect = NonTerminal(document(), [p_body, p_eof])

        # print('\n: expect') ; pp(expect)

        return (text, expect)