from mini_parse import Grammar, string_grammar, template, digit, letter, exact template_grammar = Grammar(string_grammar) template_grammar.main = template( '{a:digit} {b:dragons} {!:id}; {!:id} {c:people} {!:id}', { 'digit': digit, 'id': (letter | exact(' ')).plus().concat_all(), 'people': letter.plus().concat_all().join(exact(' and '), 2) })
import mini_parse as mp parser = mp.Grammar(mp.string_grammar) parser.main = mp.template( '{lhs:variable}{!ws}={!ws}{rhs:value}', {'variable': parser.variable, 'value': parser.value, 'ws': mp.whitespace}) parser.variable = mp.exact('foo') parser.value = mp.exact('5')
from mini_parse import exact, Grammar, string_grammar parser = Grammar(string_grammar) parser.main = exact('!').repeat(1)
from mini_parse import Grammar, string_grammar, and_series, or_series, \ and_then_series, exact, get_n_chars series_test = Grammar(string_grammar) series_test.space = exact(' ') series_test.main = and_series([ series_test.and_test, series_test.space, series_test.or_test, series_test.space, series_test.and_then_test ]) series_test.and_test = and_series( [exact('a'), exact('b') | exact('bc'), exact('d')]) series_test.or_test = or_series([ exact('e'), exact('f') | (exact('g') & exact('h')), exact('i') & exact('j') ]) series_test.and_then_test = and_then_series([ exact('k') + exact('lm'), lambda s: get_n_chars(len(s)), lambda t: exact(t + 'n') ]) print(series_test.parse('abcd f klm1212n'))
from mini_parse import Grammar, string_grammar, one_char, exact monkey = Grammar(string_grammar) monkey.stop = one_char('bd') monkey.plosive = monkey.stop & exact('a') monkey.syllable = (monkey.plosive + monkey.stop.optional()) | (exact('a') + (monkey.plosive | monkey.stop)) monkey.word = monkey.syllable + monkey.syllable.repeat(2).times() monkey.sentence = monkey.word.join(exact('#')) monkey.main = monkey.sentence print(monkey.parse('ba#ababadada#bad#dabbada')) print(monkey.parse('abdabaadab#ada')) print(monkey.parse('dad#ad#abaadad#badadbaad'))
from mini_parse import exact, none_char, one_of, Grammar, string_grammar csv_grammar = Grammar(string_grammar) csv_grammar.main = (csv_grammar.line & csv_grammar.eol).first().times() csv_grammar.eol = one_of(['\n', '\r', '\r\n', '\n\r']) csv_grammar.line = csv_grammar.cell.join(exact(',')) csv_grammar.cell = csv_grammar.quoted_cell | \ none_char(',\n\r').plus().concat_all() csv_grammar.quoted_cell = csv_grammar.quote.and_second( csv_grammar.quoted_char.plus().concat_all()).and_first(csv_grammar.quote) csv_grammar.quoted_char = none_char('"') | exact('""').goes_to('"') csv_grammar.quote = exact('"')
def read_hex(digits): d = int(digits, 16) return chr(d) def surround_by_ws(rule): return whitespace.and_second(rule).and_first(whitespace) sign_lambda = lambda t: -t[1] if t[0] == '-' else t[1] json = Grammar(string_grammar) json.main = json.object json.value = json.string | json.number | json.object | json.array | json.bool | json.null json.bool = exact('true').goes_to(True) | exact('false').goes_to(False) json.null = exact('null').goes_to(None) json.colon = surround_by_ws(exact(':')) json.comma = surround_by_ws(exact(',')) json.array = surround_by_ws(exact('[')).and_second( json.value.join(json.comma).optional([])).and_first( surround_by_ws(exact(']'))) json.object = surround_by_ws(exact('{')).and_second( (json.pair.join(json.comma) >> pairs_to_dict).optional({})).and_first( surround_by_ws(exact('}'))) json.pair = json.string.and_first(json.colon) & json.value json.string = exact('"').and_second(json.char.times().concat_all()).and_first(
from mini_parse import exact, Grammar, string_grammar, get_next_n, one_char, digit integer = digit.plus().concat_all() >> int binary_data = Grammar(string_grammar) binary_data.main = (exact('b').and_second(integer)).and_then( lambda size: exact(':').and_second(get_next_n(size))) >> \ (lambda x: int(x, 2))