@@ -189,15 +189,11 @@ export default class Parser {
189189 }
190190
191191 missingParenthesis ( ) {
192- return this . error ( 'Expected an opening parenthesis.' , {
193- index : this . currToken [ 5 ] ,
194- } ) ;
192+ return this . expected ( 'opening parenthesis' , this . currToken [ 5 ] ) ;
195193 }
196194
197195 missingSquareBracket ( ) {
198- return this . error ( 'Expected an opening square bracket.' , {
199- index : this . currToken [ 5 ] ,
200- } ) ;
196+ return this . expected ( 'opening square bracket' , this . currToken [ 5 ] ) ;
201197 }
202198
203199 namespace ( ) {
@@ -265,9 +261,7 @@ export default class Parser {
265261 }
266262 }
267263 if ( balanced ) {
268- this . error ( 'Expected a closing parenthesis.' , {
269- index : this . currToken [ 5 ] ,
270- } ) ;
264+ return this . expected ( 'closing parenthesis' , this . currToken [ 5 ] ) ;
271265 }
272266 }
273267
@@ -279,9 +273,7 @@ export default class Parser {
279273 this . position ++ ;
280274 }
281275 if ( ! this . currToken ) {
282- return this . error ( 'Expected a pseudo-class or pseudo-element.' , {
283- index : this . position - 1 ,
284- } ) ;
276+ return this . expected ( [ 'pseudo-class' , 'pseudo-element' ] , this . position - 1 ) ;
285277 }
286278 if ( this . currToken [ 0 ] === tokens . word ) {
287279 this . splitWord ( false , ( first , length ) => {
@@ -307,9 +299,7 @@ export default class Parser {
307299 }
308300 } ) ;
309301 } else {
310- this . error ( 'Expected a pseudo-class or pseudo-element.' , {
311- index : this . currToken [ 5 ] ,
312- } ) ;
302+ return this . expected ( [ 'pseudo-class' , 'pseudo-element' ] , this . currToken [ 5 ] ) ;
313303 }
314304 }
315305
@@ -503,6 +493,24 @@ export default class Parser {
503493 * Helpers
504494 */
505495
496+ expected ( description , index , found ) {
497+ if ( Array . isArray ( description ) ) {
498+ const last = description . pop ( ) ;
499+ description = `${ description . join ( ', ' ) } or ${ last } ` ;
500+ }
501+ const an = / ^ [ a e i o u ] / . test ( description [ 0 ] ) ? 'an' : 'a' ;
502+ if ( ! found ) {
503+ return this . error (
504+ `Expected ${ an } ${ description } .` ,
505+ { index}
506+ ) ;
507+ }
508+ return this . error (
509+ `Expected ${ an } ${ description } , found "${ found } " instead.` ,
510+ { index}
511+ ) ;
512+ }
513+
506514 parseNamespace ( namespace ) {
507515 if ( this . lossy && typeof namespace === 'string' ) {
508516 const trimmed = namespace . trim ( ) ;
0 commit comments