phply: minor feature enhancements

o add spaceship operator support
o add null coalescing operator support
o allow type prefix in class property
This commit is contained in:
Ad Schellevis 2025-04-30 20:29:13 +02:00
parent 12cb1a4e32
commit f6c4932b62
3 changed files with 487 additions and 4 deletions

477
lib/phply/parsetab.py Normal file

File diff suppressed because one or more lines are too long

View file

@ -34,7 +34,7 @@ reserved = (
'REQUIRE_ONCE', 'RETURN', 'STATIC', 'SWITCH', 'UNSET', 'USE', 'VAR',
'WHILE', 'FINAL', 'INTERFACE', 'IMPLEMENTS', 'PUBLIC', 'PRIVATE',
'PROTECTED', 'ABSTRACT', 'CLONE', 'TRY', 'CATCH', 'THROW', 'NAMESPACE',
'FINALLY', 'TRAIT', 'YIELD',
'FINALLY', 'TRAIT', 'YIELD', 'INT'
)
# Not used by parser
@ -54,7 +54,7 @@ tokens = reserved + unparsed + (
'PLUS', 'MINUS', 'MUL', 'DIV', 'MOD', 'AND', 'OR', 'NOT', 'XOR', 'SL',
'SR', 'BOOLEAN_AND', 'BOOLEAN_OR', 'BOOLEAN_NOT', 'IS_SMALLER',
'IS_GREATER', 'IS_SMALLER_OR_EQUAL', 'IS_GREATER_OR_EQUAL', 'IS_EQUAL',
'IS_NOT_EQUAL', 'IS_IDENTICAL', 'IS_NOT_IDENTICAL',
'IS_NOT_EQUAL', 'IS_IDENTICAL', 'IS_NOT_IDENTICAL', 'NULL_COALESC', 'SPACESHIP',
# Assignment operators
'EQUALS', 'MUL_EQUAL', 'DIV_EQUAL', 'MOD_EQUAL', 'PLUS_EQUAL',
@ -126,6 +126,8 @@ t_php_IS_EQUAL = r'=='
t_php_IS_NOT_EQUAL = r'(!=(?!=))|(<>)'
t_php_IS_IDENTICAL = r'==='
t_php_IS_NOT_IDENTICAL = r'!=='
t_php_NULL_COALESC = r'\?\?'
t_php_SPACESHIP = r'\<\=\>'
# Assignment operators
t_php_EQUALS = r'='

View file

@ -35,7 +35,7 @@ precedence = (
('nonassoc', 'IS_EQUAL', 'IS_NOT_EQUAL', 'IS_IDENTICAL', 'IS_NOT_IDENTICAL'),
('nonassoc', 'IS_SMALLER', 'IS_SMALLER_OR_EQUAL', 'IS_GREATER', 'IS_GREATER_OR_EQUAL'),
('left', 'SL', 'SR'),
('left', 'PLUS', 'MINUS', 'CONCAT'),
('left', 'PLUS', 'MINUS', 'CONCAT', 'NULL_COALESC'),
('left', 'MUL', 'DIV', 'MOD'),
('right', 'BOOLEAN_NOT'),
('nonassoc', 'INSTANCEOF'),
@ -701,8 +701,10 @@ def p_method_body(p):
def p_non_empty_member_modifiers(p):
'''non_empty_member_modifiers : non_empty_member_modifiers member_modifier
| non_empty_member_modifiers ARRAY
| non_empty_member_modifiers INT
| member_modifier'''
if len(p) == 3:
if len(p) >= 3:
p[0] = p[1] + [p[2]]
else:
p[0] = [p[1]]
@ -1163,6 +1165,8 @@ def p_expr_binary_op(p):
| expr XOR expr
| expr CONCAT expr
| expr PLUS expr
| expr NULL_COALESC expr
| expr SPACESHIP expr
| expr MINUS expr
| expr MUL expr
| expr DIV expr