Given a raw string representing medical codes, write a parser to extract and validate specific formatting patterns.
Recognize uppercase ICD-10-CM codes in the form [A-Z][0-9]{2} with an optional dot and one to four uppercase letters or digits. Recognize CPT codes as exactly five digits. Return matches in their original order, excluding codes embedded in larger alphanumeric or dotted tokens.
Function: parse_medical_codes(raw) accepts a string and returns a list of {code, type} dictionaries, using ICD-10-CM or CPT as type values.
def parse_medical_codes(raw):