def parse_rule(text, start_pos, start_vals): while True: pos, vals = start_pos, start_vals pos, vals = parse_id(text, pos, vals) if pos is None: break m = re.match('=', text[pos:]) if not m: break pos, vals = pos + m.end(), vals + m.groups() pos, vals = parse__(text, pos, vals) if pos is None: break vals += ('def parse_',) vals = metalib.swap(vals) vals += ('(text, start_pos, start_vals):',) vals = metalib.nl(vals) pos, vals = parse_alts(text, pos, vals) if pos is None: break m = re.match('\\.', text[pos:]) if not m: break pos, vals = pos + m.end(), vals + m.groups() pos, vals = parse__(text, pos, vals) if pos is None: break vals += (' return None, None',) vals = metalib.nl(vals) return pos, vals return None, None
def parse_alts(text, start_pos, start_vals): while True: pos, vals = start_pos, start_vals vals += (' while True:',) vals = metalib.nl(vals) vals += (' pos, vals = start_pos, start_vals',) vals = metalib.nl(vals) pos, vals = parse_seq(text, pos, vals) if pos is None: break pos, vals = parse_alts2(text, pos, vals) if pos is None: break return pos, vals return None, None
def parse_seq(text, start_pos, start_vals): while True: pos, vals = start_pos, start_vals pos, vals = parse_token(text, pos, vals) if pos is None: break pos, vals = parse_seq(text, pos, vals) if pos is None: break return pos, vals while True: pos, vals = start_pos, start_vals vals += (' return pos, vals',) vals = metalib.nl(vals) return pos, vals return None, None
def parse_main(text, start_pos, start_vals): while True: pos, vals = start_pos, start_vals vals += ("# This file was generated by metatoo; you probably shouldn't edit it.",) vals = metalib.nl(vals) vals += ('import metalib, re',) vals = metalib.nl(vals) vals += ('def parse(text):',) vals = metalib.nl(vals) vals += (' pos, vals = parse_main(text, 0, ())',) vals = metalib.nl(vals) vals += (" if pos != len(text): raise Exception('Failed', pos)",) vals = metalib.nl(vals) vals += (" return ''.join(vals)",) vals = metalib.nl(vals) pos, vals = parse__(text, pos, vals) if pos is None: break pos, vals = parse_rules(text, pos, vals) if pos is None: break return pos, vals return None, None
def parse_token(text, start_pos, start_vals): while True: pos, vals = start_pos, start_vals m = re.match('{', text[pos:]) if not m: break pos, vals = pos + m.end(), vals + m.groups() pos, vals = parse_qchars(text, pos, vals) if pos is None: break m = re.match('}', text[pos:]) if not m: break pos, vals = pos + m.end(), vals + m.groups() pos, vals = parse__(text, pos, vals) if pos is None: break vals += (' vals += (',) vals = metalib.swap(vals) vals = metalib.quote(vals) vals += (',)',) vals = metalib.nl(vals) return pos, vals while True: pos, vals = start_pos, start_vals m = re.match('\\/', text[pos:]) if not m: break pos, vals = pos + m.end(), vals + m.groups() pos, vals = parse_xchars(text, pos, vals) if pos is None: break m = re.match('\\/', text[pos:]) if not m: break pos, vals = pos + m.end(), vals + m.groups() pos, vals = parse__(text, pos, vals) if pos is None: break vals += (' m = re.match(',) vals = metalib.swap(vals) vals = metalib.quote(vals) vals += (', text[pos:])',) vals = metalib.nl(vals) vals += (' if not m: break',) vals = metalib.nl(vals) vals += (' pos, vals = pos + m.end(), vals + m.groups()',) vals = metalib.nl(vals) return pos, vals while True: pos, vals = start_pos, start_vals pos, vals = parse_id(text, pos, vals) if pos is None: break vals += (' pos, vals = parse_',) vals = metalib.swap(vals) vals += ('(text, pos, vals)',) vals = metalib.nl(vals) vals += (' if pos is None: break',) vals = metalib.nl(vals) return pos, vals while True: pos, vals = start_pos, start_vals m = re.match(':', text[pos:]) if not m: break pos, vals = pos + m.end(), vals + m.groups() pos, vals = parse_id(text, pos, vals) if pos is None: break vals += (' vals = metalib.',) vals = metalib.swap(vals) vals += ('(vals)',) vals = metalib.nl(vals) return pos, vals return None, None