def ordered_list(): return pg.AllOf( pg.Ignore(pg.Optional(pg.Many("\n"))), pg.Indented(pg.AllOf( numbered_bullet, pg.Optional(pg.Many(numbered_bullet, ordered_list))), optional=True))
def test_escaped(): html_text = pg.NamedPattern( 'html_text', pg.Many( pg.Words( pg.Words.letters+"</>"))) escaped_text = pg.NamedPattern( 'escaped_text', pg.Escaped(html_text)) data = """<p>Some Text</p>""" expected = [ 'escaped_text', ['html_text', "<p>Some Text</p>"]] result = pg.parse_string(data, escaped_text) assert expected == result html_text = pg.Many( pg.Words( pg.Words.letters+"</>")) escaped_text = pg.NamedPattern( 'escaped_text', pg.Escaped(html_text)) expected = [ 'escaped_text', "<p>Some Text</p>"] result = pg.parse_string(data, escaped_text) assert expected == result
def document(): return pg.AllOf( pg.Optional(pg.Ignore(newlines_or_eof)), pg.Optional(doctype), pg.OneOf( extends, pg.Many( pg.AllOf(pg.OneOf(element, code_block, comment), pg.Optional(pg.Ignore(pg.Many("\n")))))))
def nested_list(): return pg.AllOf( pg.Ignore( pg.Optional( pg.Many("\n"))), pg.Indented( pg.AllOf( list_item, pg.Optional( pg.Many( list_item, nested_list))), optional=True))
def test_parse_many(): emphasis = pg.NamedPattern( 'emphasis', pg.AllOf( pg.Ignore("*"), pg.Words(), pg.Ignore("*"))) words = pg.NamedPattern( 'words', pg.Words()) phrase = pg.NamedPattern( 'phrase', pg.OneOf( words, emphasis)) body = pg.NamedPattern( 'body', pg.Many(phrase)) expected = ['body', ['phrase', ['words', 'a phrase with ']], ['phrase', ['emphasis', "bold words"]], ['phrase', ['words', " in it"]]] result = pg.parse_string("a phrase with *bold words* in it", body) assert result == expected with py.test.raises(pg.NoPatternFound): result = pg.parse_string("123", body)
def test_indented_bullet(): paragraph = pg.NamedPattern( 'paragraph', pg.AllOf( pg.Ignore(pg.Optional("\n")), pg.Words())) indented_paragraphs = pg.Indented( pg.Many(paragraph), initial_indent="* ") data = """ * Paragraph One Paragraph Two """.strip() expected = [ 'indented_paragraphs', ['paragraph', "Paragraph One"], ['paragraph', "Paragraph Two"]] match, rest = indented_paragraphs(data, "indented_paragraphs") assert match == expected
def test_match_indented_nested_bullets(): bullet = pg.NamedPattern( 'bullet', pg.AllOf( pg.Ignore( pg.Optional( pg.Many("\n"))), pg.Ignore("* "), pg.Words())) @pg.lazy def indented_bullets(): return pg.Indented( pg.AllOf( bullet, pg.Optional( indented_bullets)), optional=True) data = """ * Line One * Line Two """ expected = [ 'indented_bullets', ['bullet', "Line One"], ['indented_bullets', ['bullet', "Line Two"]]] match, rest = indented_bullets(data, 'indented_bullets') assert match == expected assert rest == "\n"
def test_with_indent_chars(self): "Test that Indented can match with indents other than whitespace" lines = pg.Many( pg.OneOf( pg.Words(), pg.Text("\n"))) indented_text = pg.Indented( lines, indent_pattern="> ") data = """ > Some text > indented with > non whitespace """.strip() expected = ['indented_text', "Some text", "\n", "indented with", "\n", "non whitespace"] match, rest = indented_text(data, 'indented_text') assert match == expected assert rest == ""
def link_text(): return pg.AllOf( pg.Ignore("["), pg.Join( pg.Many( pg.Not("]"))), pg.Ignore("]"))
def test_match_many_specificty(): letter_a = pg.NamedPattern( 'letter_a', "a") letter_b = pg.NamedPattern( 'letter_b', "b") other_letters = pg.NamedPattern( 'other_letters', pg.Words()) match_letters = pg.Many( letter_a, letter_b, other_letters) data = "abac" expected = [ 'match_letters', ['letter_a', "a"], ['letter_b', "b"], ['letter_a', "a"], ['other_letters', "c"]] match, rest = match_letters(data, 'match_letters') assert match == expected assert rest == ""
def link_url(): return pg.AllOf( pg.Ignore("("), pg.Join( pg.Many( pg.Not(")"))), pg.Ignore(")"))
def code(): return pg.AllOf( pg.Ignore("`"), pg.Join( pg.Many( pg.Not("`"))), pg.Ignore("`"))
def test_match_many_simple(): a = pg.NamedPattern( 'a', "a") b = pg.NamedPattern( 'b', "b") letters = pg.Many(a, b) expected = ['letters', ['a', "a"], ['b', "b"], ['a', "a"], ['b', "b"]] match, rest = letters("abab", "letters") assert match == expected assert rest == "" match, rest = letters("ababcc", "letters") assert match == ['letters', ['a', "a"], ['b', "b"], ['a', "a"], ['b', "b"]] assert rest == "cc" match, rest = letters("ababcc", "") assert match == ['', ['a', "a"], ['b', "b"], ['a', "a"], ['b', "b"]] assert rest == "cc" with py.test.raises(pg.NoPatternFound): match, rest = letters("cab", "letters")
def test_match_one_of(): asterix = pg.Ignore("*") emphasis = pg.AllOf( asterix, pg.Many(pg.Not("*")), asterix) phrase = pg.OneOf( pg.Words(), emphasis) expected = ['phrase', "b", "o", "l", "d"] match, rest = phrase("*bold*", "phrase") assert match == expected assert rest == "" with py.test.raises(pg.NoPatternFound): match, rest = phrase("123", "phrase") match, rest = phrase("text", "phrase") assert match == ['phrase', "text"] assert rest == "" # Test match with no name match, rest = phrase("text", "") assert match == ['', "text"] assert rest == ""
def attribute_list(): return pg.AllOf( pg.Ignore("("), attribute, pg.Optional( pg.Many( pg.AllOf(pg.Ignore(","), pg.Ignore(pg.Optional(" ")), attribute))), pg.Ignore(")"))
def test_reprs(): # Pattern matchers assert repr(pg.Some("a")) == "<Some pattern='a'>" assert repr(pg.Ignore("#")) == "<Ignore pattern=<Text pattern='#'>>" assert repr(pg.Not("#")) == "<Not pattern=<Text pattern='#'>>" assert repr(pg.Optional("#")) == "<Optional pattern=<Text pattern='#'>>" # Option matchers assert repr(pg.OneOf("abc", pg.Not("#"))) == "<OneOf options=(<Text pattern='abc'>, <Not pattern=<Text pattern='#'>>)>" assert repr(pg.Many("abc", pg.Not("#"))) == "<Many options=(<Text pattern='abc'>, <Not pattern=<Text pattern='#'>>)>" assert repr(pg.Words()) == "<Words letters='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz .,'>"
def body(): return pg.Many( linebreaks, horizontal_rule, title_level_2, title_level_1, ordered_list, unordered_list, code_block, paragraph, blockquote, )
def test_match_many_empty(): "Test that empty matches don't raise NoPatternFound" linebreaks = pg.Many( pg.OneOf( pg.Ignore( "\n"))) data = "\n\n" expected = ['linebreaks', ""] match, rest = linebreaks(data, 'linebreaks') assert match == expected assert rest == ""
def horizontal_rule(): return pg.AllOf( pg.OneOf( "---", "___", "***", "- - -", "_ _ _", "* * *"), pg.Optional( pg.Ignore( pg.Many( pg.Not("\n")))))
def _unordered_list_template(bullet_type, spacing): return pg.Indented( pg.AllOf( bullet_type, pg.Many( pg.AllOf( pg.Ignore( spacing), pg.OneOf( bullet_type, unordered_list, ordered_list)))), optional=True)
def test_with_optional(self): """Test that optional allows you to match without an indent""" def list_item(): return pg.AllOf(pg.Ignore("* "), pg.Words()) indented_bullets = pg.Indented(pg.Many(list_item), optional=True) data = """* A bullet""" expected = ['indented_bullets', ['list_item', "A bullet"]] match, rest = pg.match_indented(data, indented_bullets, 'indented_bullets') assert match == expected assert rest == ""
def test_without_optional(self): """Test that without optional, no match is made""" def list_item(): return (pg.Ignore("\n* "), pg.Words()) indented_bullets = pg.Indented(pg.Many(list_item)) data = """ * A bullet""" expected = ['list_item', "A bullet"] with py.test.raises(pg.NoPatternFound): match, rest = pg.match_indented(data, indented_bullets, 'indented_bullets')
def test_match_join(): three_times_a = pg.Join(pg.CountOf(3, "a")) data = "aaa" expected = ['three_times_a', "aaa"] match, rest = pg.match_join(data, three_times_a, 'three_times_a') assert match == expected assert rest == "" not_d = pg.Join(pg.Many(pg.Not("d"))) data = "abcd" expected = ['not_d', "abc"] match, rest = pg.match_join(data, not_d, 'not_d') assert match == expected assert rest == "d"
def test_match_many_complex(): def emphasis(): return pg.AllOf(lambda: pg.Ignore("*"), lambda: pg.Words(), lambda: pg.Ignore("*")) def words(): return pg.Words() body = pg.Many(emphasis, words) expected = [ 'body', ['words', 'a phrase with '], ['emphasis', "bold words"], ['words', " in it"] ] match, rest = pg.match_many("a phrase with *bold words* in it", body, 'body') assert match == expected assert rest == ""
def test_match_escaped(): html_text = pg.NamedPattern( 'html_text', pg.Many( pg.Words( pg.Words.letters + "</>"))) escaped_text = pg.Escaped(html_text) data = """<p>Some text</p>""" expected = [ 'escaped_text', ['html_text', "<p>Some text</p>"]] match, rest = escaped_text(data, 'escaped_text') assert match == expected assert rest == ""
def numbered_bullet_with_paragraph(): return pg.Indented( pg.AllOf( paragraph, pg.Optional( pg.AllOf( linebreaks, paragraph))), initial_indent=pg.AllOf( pg.Optional( linebreaks), pg.OneOf( pg.AllOf( pg.Ignore(digits), pg.Ignore("."), pg.Many('\t')), pg.AllOf( digits, ".", " "))))
def test_with_optional(self): """Test that optional allows you to match without an indent""" list_item = pg.NamedPattern( 'list_item', pg.AllOf( pg.Ignore("* "), pg.Words())) indented_bullets = pg.Indented( pg.Many(list_item), optional=True) data = """* A bullet""" expected = [ 'indented_bullets', ['list_item', "A bullet"]] match, rest = indented_bullets(data, 'indented_bullets') assert match == expected assert rest == ""
def test_match_many_specificty(): def lettter_a(): return "a" def lettter_b(): return "b" def other_letters(): return pg.Words() match_letters = pg.Many(lettter_a, lettter_b, other_letters) data = "abac" expected = [ 'match_letters', ['lettter_a', "a"], ['lettter_b', "b"], ['lettter_a', "a"], ['other_letters', "c"] ] match, rest = pg.match_many(data, match_letters, 'match_letters') assert match == expected assert rest == ""
def test_match_many_complex(): emphasis = pg.NamedPattern( 'emphasis', pg.AllOf( pg.Ignore("*"), pg.Words(), pg.Ignore("*"))) words = pg.NamedPattern( 'words', pg.Words()) body = pg.Many(emphasis, words) expected = [ 'body', ['words', 'a phrase with '], ['emphasis', "bold words"], ['words', " in it"]] match, rest = body("a phrase with *bold words* in it", 'body') assert match == expected assert rest == ""
def test_match_many_simple(): def a(): return "a" def b(): return "b" letters = pg.Many(a, b) expected = ['letters', ['a', "a"], ['b', "b"], ['a', "a"], ['b', "b"]] match, rest = pg.match_many("abab", letters, "letters") assert match == expected assert rest == "" match, rest = pg.match_many("ababcc", letters, "letters") assert match == ['letters', ['a', "a"], ['b', "b"], ['a', "a"], ['b', "b"]] assert rest == "cc" match, rest = pg.match_many("ababcc", letters, "") assert match == ['', ['a', "a"], ['b', "b"], ['a', "a"], ['b', "b"]] assert rest == "cc" with py.test.raises(pg.NoPatternFound): match, rest = pg.match_many("cab", letters, "letters")