use pattern matching for macro expansion

@andreistefanescu please review