@@ -0,0 +1,2259 @@
+#!/usr/bin/env python%0Afrom parcon import *%0Afrom collections import namedtuple%0A%0Ahexchars = '0123456789abcdefABCDEF'%0A%0AReg = namedtuple(%22Reg%22, %5B%22num%22%5D)%0AImm = namedtuple(%22Imm%22, %5B%22value%22%5D)%0AMemRef = namedtuple(%22MemRef%22, %5B%22reg%22, %22offset%22%5D)%0A%0Adef keywords(vs):%0A    return First(*%5BKeyword(SignificantLiteral(v)) for v in vs%5D)%0A%0Ahexnum = SignificantLiteral('0x') + +CharIn(hexchars)%0Adecnum = +Digit()%0Aoffset = (CharIn(%22+-%22) + Exact(hexnum %7C decnum))%5Bflatten%5D%5B%22%22.join%5D%5Blambda x: int(x, 0)%5D%0A%0Areg = Literal('r') + integer%5Bint%5D%5BReg%5D%0Aimm = Literal('$') + Word(hexchars)%5Blambda x: int(x, 16)%5D%5BImm%5D%0Amemref = (Literal('%5B') + reg + Optional(offset, 0) + Literal('%5D'))%5Blambda x: MemRef(*x)%5D%0A%0Aunary_alu_ops = %5B'NEG', 'NEG64', 'END16', 'END64_16', 'END32', 'END64_32', 'END64', 'END64_64'%5D%0Abinary_alu_ops = %5B'ADD', 'SUB', 'MUL', 'DIV', 'OR', 'AND', 'LSH', 'RSH',%0A                  'MOD', 'XOR', 'MOV', 'ARSH'%5D%0Abinary_alu_ops.extend(%5Bx + '64' for x in binary_alu_ops%5D)%0A%0Aalu_instruction = %5C%0A    (keywords(unary_alu_ops) + reg) %7C %5C%0A    (keywords(binary_alu_ops) + reg + %22,%22 + (reg %7C imm))%0A%0Amem_sizes = %5B'W', 'H', 'B', 'DW'%5D%0Amem_store_reg_ops = %5B'STX' + s for s in mem_sizes%5D%0Amem_store_imm_ops = %5B'ST' + s for s in mem_sizes%5D%0Amem_load_ops = %5B'LDX' + s for s in mem_sizes%5D%0A%0Amem_instruction = %5C%0A    (keywords(mem_store_reg_ops) + memref + %22,%22 + reg) %7C %5C%0A    (keywords(mem_store_imm_ops) + memref + %22,%22 + imm) %7C %5C%0A    (keywords(mem_load_ops) + reg + %22,%22 + memref)%0A%0Ajmp_cmp_ops = %5B'JEQ', 'JGT', 'JGE', 'JSET', 'JNE', 'JSGT', 'JSGE'%5D%0Ajmp_instruction = %5C%0A    (keywords(jmp_cmp_ops) + reg + %22,%22 + (reg %7C imm) + %22,%22 + offset) %7C %5C%0A    (keywords(%5B'JA'%5D) + offset) %7C %5C%0A    (keywords(%5B'CALL'%5D) + imm) %7C %5C%0A    (keywords(%5B'EXIT'%5D)%5Blambda x: (x, )%5D)%0A%0Ainstruction = alu_instruction %7C mem_instruction %7C jmp_instruction%0A%0Astart = ZeroOrMore(instruction + Optional(Literal(';'))) + End()%0A%0Adef parse(source):%0A    return start.parse_string(source)%0A%0Aif __name__ == %22__main__%22:%0A    import argparse%0A    parser = argparse.ArgumentParser(description=%22Assembly parser%22, formatter_class=argparse.RawDescriptionHelpFormatter)%0A    parser.add_argument('file', type=argparse.FileType('r'), default='-')%0A    args = parser.parse_args()%0A    result = parse(args.file.read())%0A    for inst in result:%0A        print repr(inst)%0A

add parcon parser