factorize main parser code in ./lib/parser.js