All pastes #372888 Raw Edit

Cody Brocious

public python v1 · immutable
#372888 ·published 2007-02-26 10:47 UTC
rendered paste body
def unflatten(stmt):	if stmt.__class__ == str:		stmt = stmt.split(' ')		out = []		while len(stmt):		elem = stmt.pop(0)				if elem == '(':			sub, stmt = unflatten(stmt)						out.append(sub)		elif elem == ')':			if len(stmt) and stmt[0] in ('?', '*', '+'):				out.append(stmt.pop(0))						break		elif elem:			out.append(elem)		return out, stmtdef findOps(stmt):	if not stmt:		return ()		out = stmt.pop(0)		if out.__class__ == list:		out = findOps(out)		if out.__class__ != tuple:		out = (out, None, None)		while len(stmt):		try:			op = stmt.pop(0)		except:			pass				if op == '/':			next = stmt.pop(0)		elif op == '?':			out = (out, 'Optional', None)			continue		elif op == '*':			out = (out, 'ZeroOrMore', None)			continue		elif op == '+':			out = (out, 'OneOrMore', None)			continue		else:			next = op			op = '.'				if next.__class__ == list:			next = findOps(next)						if not next:				next = None				out = (out, op, next)		return outdef unaryParse(item):	if item[0] == '!':		return (item[1:], 'NotAny', None)	elif item[-1] == '*':		return (item[:-1], 'ZeroOrMore', None)	elif item[-1] == '+':		return (item[:-1], 'OneOrMore', None)	elif item[-1] == '?':		return (item[:-1], 'Optional', None)		for char in item:		if char not in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ':			return item		return (item, 'Quote', None)def findUnary(stmt):	first, op, second = stmt		if first.__class__ == tuple:		first = findUnary(first)		if second.__class__ == tuple:		second = findUnary(second)		if first.__class__ == str:		first = unaryParse(first)		if second.__class__ == str:		second = unaryParse(second)		return (first, op, second)def generate(stmt, name):	first, op, second = stmt		if first.__class__ == tuple:		first = generate(first, name)		if second.__class__ == tuple:		second = generate(second, name)		if second == name:		return 'OneOrMore(%s)' % first		if op == '.':		return '(%s + %s)' % (first, second)	elif op == '/':		return '(%s | %s)' % (first, second)	elif op == None:		return first	elif op == 'Quote':		return repr(first)	else:		return '%s(%s)' % (op, first)lines = file('clean.peg', 'r').read().strip().split('\n')peg = [lines[i:i+3] for i in range(0, len(lines), 3)]exprs = {}for expr in peg:	type, name, stmt = expr		exprs[name] = (type, stmt)code = {}for name in exprs:	type, stmt = exprs[name]		stmt, rest = unflatten(stmt)		stmt = findOps(stmt)		stmt = findUnary(stmt)		stmt = generate(stmt, name)		code[name] = stmtorder = list(code.keys())def code_cmp(x, y):	if y in code[x]:		return 1	elif x in code[y]:		return -1	else:		return cmp(x, y)order.sort(code_cmp)for name in order:	print name, '=', code[name]"""; zoi-open <- any-word-pre inner-word &{ myAssign( new String(innerWordSEP2.toString()) ) } close-paren; zoi-word <- spaces? zoi-printable-pre inner-word &{ ! myCompare( startString, innerWordSEP3 ) } close-paren; zoi-close <- spaces? any-word-pre inner-word &{ myCompare( startString, innerWordSEP3 ) } close-paren spaces?"""