Refactor parse error generation to a function