File tree Expand file tree Collapse file tree 3 files changed +191
-0
lines changed Expand file tree Collapse file tree 3 files changed +191
-0
lines changed Original file line number Diff line number Diff line change @@ -32,6 +32,29 @@ module.exports = {
3232 }
3333 }
3434
35+ // https://github.com/php/php-src/blob/master/Zend/zend_language_scanner.l#L1546
36+ if ( id === this . tok . T_ENUM ) {
37+ if ( this . version < 801 ) {
38+ return this . tok . T_STRING ;
39+ }
40+ const initial = this . offset ;
41+ let ch = this . input ( ) ;
42+ while ( ch == " " ) {
43+ ch = this . input ( ) ;
44+ }
45+ let isEnum = false ;
46+ if ( this . is_LABEL_START ( ) ) {
47+ while ( this . is_LABEL ( ) ) {
48+ ch += this . input ( ) ;
49+ }
50+ const label = ch . slice ( 0 , - 1 ) . toLowerCase ( ) ;
51+ isEnum = label !== "extends" && label !== "implements" ;
52+ }
53+
54+ this . unput ( this . offset - initial ) ;
55+ return isEnum ? this . tok . T_ENUM : this . tok . T_STRING ;
56+ }
57+
3558 if ( this . offset < this . size && id !== this . tok . T_YIELD_FROM ) {
3659 // If immediately followed by a backslash, this is a T_NAME_RELATIVE or T_NAME_QUALIFIED.
3760 let ch = this . input ( ) ;
Original file line number Diff line number Diff line change @@ -275,8 +275,155 @@ Program {
275275}
276276` ;
277277
278+ exports [` Test enums can't be parsed with PHP < 8 1` ] = ` "Parse Error : syntax error, unexpected 'Foo' (T_STRING), expecting ';' on line 1"` ;
279+
278280exports [` Test enums cannot have properties 1` ] = ` "Parse Error : syntax error, unexpected 'int' (T_STRING) on line 3"` ;
279281
282+ exports [` Test enums doesn't confuse enums with identifiers 1` ] = `
283+ Program {
284+ " children" : Array [
285+ Class {
286+ " attrGroups" : Array [],
287+ " body" : Array [
288+ Method {
289+ " arguments" : Array [],
290+ " attrGroups" : Array [],
291+ " body" : Block {
292+ " children" : Array [],
293+ " kind" : " block" ,
294+ },
295+ " byref" : false ,
296+ " isAbstract" : false ,
297+ " isFinal" : false ,
298+ " isStatic" : false ,
299+ " kind" : " method" ,
300+ " name" : Identifier {
301+ " kind" : " identifier" ,
302+ " name" : " enum" ,
303+ },
304+ " nullable" : false ,
305+ " type" : null ,
306+ " visibility" : " " ,
307+ },
308+ ],
309+ " extends" : null ,
310+ " implements" : null ,
311+ " isAbstract" : false ,
312+ " isAnonymous" : false ,
313+ " isFinal" : false ,
314+ " kind" : " class" ,
315+ " name" : Identifier {
316+ " kind" : " identifier" ,
317+ " name" : " Enum" ,
318+ },
319+ },
320+ Interface {
321+ " attrGroups" : Array [],
322+ " body" : Array [],
323+ " extends" : null ,
324+ " kind" : " interface" ,
325+ " name" : Identifier {
326+ " kind" : " identifier" ,
327+ " name" : " Enum" ,
328+ },
329+ },
330+ Trait {
331+ " body" : Array [],
332+ " kind" : " trait" ,
333+ " name" : Identifier {
334+ " kind" : " identifier" ,
335+ " name" : " Enum" ,
336+ },
337+ },
338+ _Function {
339+ " arguments" : Array [],
340+ " attrGroups" : Array [],
341+ " body" : Block {
342+ " children" : Array [],
343+ " kind" : " block" ,
344+ },
345+ " byref" : false ,
346+ " kind" : " function" ,
347+ " name" : Identifier {
348+ " kind" : " identifier" ,
349+ " name" : " enum" ,
350+ },
351+ " nullable" : false ,
352+ " type" : null ,
353+ },
354+ Class {
355+ " attrGroups" : Array [],
356+ " body" : Array [],
357+ " extends" : Name {
358+ " kind" : " name" ,
359+ " name" : " Foo" ,
360+ " resolution" : " uqn" ,
361+ },
362+ " implements" : null ,
363+ " isAbstract" : false ,
364+ " isAnonymous" : false ,
365+ " isFinal" : false ,
366+ " kind" : " class" ,
367+ " name" : Identifier {
368+ " kind" : " identifier" ,
369+ " name" : " Enum" ,
370+ },
371+ },
372+ Class {
373+ " attrGroups" : Array [],
374+ " body" : Array [],
375+ " extends" : null ,
376+ " implements" : Array [
377+ Name {
378+ " kind" : " name" ,
379+ " name" : " Foo" ,
380+ " resolution" : " uqn" ,
381+ },
382+ ],
383+ " isAbstract" : false ,
384+ " isAnonymous" : false ,
385+ " isFinal" : false ,
386+ " kind" : " class" ,
387+ " name" : Identifier {
388+ " kind" : " identifier" ,
389+ " name" : " Enum" ,
390+ },
391+ },
392+ Class {
393+ " attrGroups" : Array [],
394+ " body" : Array [],
395+ " extends" : Name {
396+ " kind" : " name" ,
397+ " name" : " Foo" ,
398+ " resolution" : " uqn" ,
399+ },
400+ " implements" : null ,
401+ " isAbstract" : false ,
402+ " isAnonymous" : false ,
403+ " isFinal" : false ,
404+ " kind" : " class" ,
405+ " name" : Identifier {
406+ " kind" : " identifier" ,
407+ " name" : " Enum" ,
408+ },
409+ },
410+ Enum {
411+ " attrGroups" : Array [],
412+ " body" : Array [],
413+ " implements" : null ,
414+ " kind" : " enum" ,
415+ " name" : Identifier {
416+ " kind" : " identifier" ,
417+ " name" : " extendsFoo" ,
418+ },
419+ " valueType" : null ,
420+ },
421+ ],
422+ " errors" : Array [],
423+ " kind" : " program" ,
424+ }
425+ ` ;
426+
280427exports [` Test enums empty 1` ] = `
281428Program {
282429 " children" : Array [
Original file line number Diff line number Diff line change @@ -90,4 +90,25 @@ describe("Test enums", function () {
9090 ` ) ;
9191 } ) . toThrowErrorMatchingSnapshot ( ) ;
9292 } ) ;
93+
94+ it ( "doesn't confuse enums with identifiers" , function ( ) {
95+ expect (
96+ parser . parseEval ( `
97+ class Enum { function enum () {} }
98+ interface Enum {}
99+ trait Enum {}
100+ function enum() {}
101+ class Enum extends Foo {}
102+ class Enum implements Foo {}
103+ class Enum exTends Foo {}
104+ enum extendsFoo {}
105+ ` )
106+ ) . toMatchSnapshot ( ) ;
107+ } ) ;
108+
109+ it ( "can't be parsed with PHP < 8" , function ( ) {
110+ expect ( ( ) => {
111+ parser . parseEval ( "enum Foo {}" , { parser : { version : "8.0" } } ) ;
112+ } ) . toThrowErrorMatchingSnapshot ( ) ;
113+ } ) ;
93114} ) ;
You can’t perform that action at this time.
0 commit comments