Skip to content

Commit 984d05f

Browse files
committed
Use semantic predicates to simplify lookahead and reduce backtracking
1 parent 0585548 commit 984d05f

File tree

2 files changed

+625
-2016
lines changed

2 files changed

+625
-2016
lines changed

ext/ffi/ffi.g

Lines changed: 75 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ declaration_specifiers(zend_ffi_dcl *dcl):
123123
{zend_ffi_set_abi(dcl, ZEND_FFI_ABI_THISCALL);}
124124
| "_Alignas"
125125
"("
126-
( &type_name
126+
( &type_name_start
127127
{zend_ffi_dcl align_dcl = ZEND_FFI_ATTR_INIT;}
128128
type_name(&align_dcl)
129129
{zend_ffi_align_as_type(dcl, &align_dcl);}
@@ -331,7 +331,6 @@ enumerator(zend_ffi_dcl *enum_dcl, int64_t *min, int64_t *max, int64_t *last):
331331
;
332332

333333
declarator(zend_ffi_dcl *dcl, const char **name, size_t *name_len):
334-
/* "char" is used as a terminator of nested declaration */
335334
{zend_ffi_dcl nested_dcl = {ZEND_FFI_DCL_CHAR, 0, 0, 0, NULL};}
336335
{zend_bool nested = 0;}
337336
pointer(dcl)?
@@ -346,44 +345,35 @@ declarator(zend_ffi_dcl *dcl, const char **name, size_t *name_len):
346345
{if (nested) zend_ffi_nested_declaration(dcl, &nested_dcl);}
347346
;
348347

349-
abstract_declarator(zend_ffi_dcl *dcl, const char **name, size_t *name_len):
350-
/* "char" is used as a terminator of nested declaration */
348+
abstract_declarator(zend_ffi_dcl *dcl):
351349
{zend_ffi_dcl nested_dcl = {ZEND_FFI_DCL_CHAR, 0, 0, 0, NULL};}
352350
{zend_bool nested = 0;}
353351
pointer(dcl)?
354-
( &nested_abstract_declarator
355-
nested_abstract_declarator(&nested_dcl, name, name_len)
352+
( &nested_declarator_start
353+
"("
354+
attributes(&nested_dcl)?
355+
abstract_declarator(&nested_dcl)
356+
")"
356357
{nested = 1;}
357-
| ID(name, name_len)
358-
| /* empty */
359-
)
358+
)?
360359
array_or_function_declarators(dcl)?
361360
{if (nested) zend_ffi_nested_declaration(dcl, &nested_dcl);}
362361
;
363362

364-
nested_abstract_declarator(zend_ffi_dcl *dcl, const char **name, size_t *name_len):
363+
parameter_declarator(zend_ffi_dcl *dcl, const char **name, size_t *name_len):
365364
{zend_ffi_dcl nested_dcl = {ZEND_FFI_DCL_CHAR, 0, 0, 0, NULL};}
366365
{zend_bool nested = 0;}
367-
"("
368-
attributes(&nested_dcl)?
369-
( pointer(dcl)
370-
( &nested_abstract_declarator
371-
nested_abstract_declarator(&nested_dcl, name, name_len)
372-
{nested = 1;}
373-
| ID(name, name_len)
374-
| /* empty */
375-
)
376-
array_or_function_declarators(dcl)?
377-
| ( &nested_abstract_declarator
378-
nested_abstract_declarator(&nested_dcl, name, name_len)
379-
array_or_function_declarators(dcl)?
380-
{nested = 1;}
381-
| ID(name, name_len)
382-
array_or_function_declarators(dcl)?
383-
| array_or_function_declarators(dcl)
384-
)
366+
pointer(dcl)?
367+
( &nested_declarator_start
368+
"("
369+
attributes(&nested_dcl)?
370+
parameter_declarator(&nested_dcl, name, name_len)
371+
")"
372+
{nested = 1;}
373+
| ID(name, name_len)
374+
| /* empty */
385375
)
386-
")"
376+
array_or_function_declarators(dcl)?
387377
{if (nested) zend_ffi_nested_declaration(dcl, &nested_dcl);}
388378
;
389379

@@ -451,17 +441,15 @@ parameter_declaration(HashTable **args):
451441
{FFI_G(allow_vla) = 1;}
452442
{zend_ffi_dcl param_dcl = ZEND_FFI_ATTR_INIT;}
453443
specifier_qualifier_list(&param_dcl)
454-
abstract_declarator(&param_dcl, &name, &name_len)
444+
parameter_declarator(&param_dcl, &name, &name_len)
455445
/*attributes(&param_dcl)? conflict ???*/
456446
{zend_ffi_add_arg(args, name, name_len, &param_dcl);}
457447
{FFI_G(allow_vla) = old_allow_vla;}
458448
;
459449

460450
type_name(zend_ffi_dcl *dcl):
461-
{const char *name = NULL;}
462-
{size_t name_len = 0;}
463451
specifier_qualifier_list(dcl)
464-
abstract_declarator(dcl, &name, &name_len)
452+
abstract_declarator(dcl)
465453
;
466454

467455
attributes(zend_ffi_dcl *dcl):
@@ -685,7 +673,7 @@ multiplicative_expression(zend_ffi_val *val):
685673
cast_expression(zend_ffi_val *val):
686674
{int do_cast = 0;}
687675
{zend_ffi_dcl dcl = ZEND_FFI_ATTR_INIT;}
688-
( &( "(" type_name ")" )
676+
( &( "(" type_name_start )
689677
"("
690678
type_name(&dcl)
691679
")"
@@ -751,7 +739,7 @@ unary_expression(zend_ffi_val *val):
751739
cast_expression(val)
752740
{zend_ffi_expr_bool_not(val);}
753741
| "sizeof"
754-
( &( "(" type_name ")" )
742+
( &( "(" type_name_start )
755743
"("
756744
type_name(&dcl)
757745
")"
@@ -765,7 +753,7 @@ unary_expression(zend_ffi_val *val):
765753
")"
766754
{zend_ffi_expr_alignof_type(val, &dcl);}
767755
| ("__alignof"|"__alignof__")
768-
( &( "(" type_name ")" )
756+
( &( "(" type_name_start )
769757
"("
770758
type_name(&dcl)
771759
")"
@@ -776,6 +764,57 @@ unary_expression(zend_ffi_val *val):
776764
)
777765
;
778766

767+
/* lookahead rules */
768+
nested_declarator_start:
769+
"("
770+
( ?{!zend_ffi_is_typedef_name((const char*)yy_text, yy_pos - yy_text)}
771+
ID
772+
| "__attribute"
773+
| "__attribute__"
774+
| "__declspec"
775+
| "*"
776+
| "("
777+
| "["
778+
)
779+
;
780+
781+
type_name_start:
782+
( ?{zend_ffi_is_typedef_name((const char*)yy_text, yy_pos - yy_text)}
783+
ID
784+
| "void"
785+
| "char"
786+
| "short"
787+
| "int"
788+
| "long"
789+
| "float"
790+
| "double"
791+
| "signed"
792+
| "unsigned"
793+
| "_Bool"
794+
| "_Complex"
795+
| "complex"
796+
| "__complex"
797+
| "__complex__"
798+
| "struct"
799+
| "union"
800+
| "enum"
801+
| "const"
802+
| "__const"
803+
| "__const__"
804+
| "restrict"
805+
| "__restict"
806+
| "__restrict__"
807+
| "volatile"
808+
| "__volatile"
809+
| "__volatile__"
810+
| "_Atomic"
811+
| "__attribute"
812+
| "__attribute__"
813+
| "__declspec"
814+
)
815+
;
816+
817+
/* scanner rules */
779818
ID(const char **name, size_t *name_len):
780819
/[A-Za-z_][A-Za-z_0-9]*/
781820
{*name = (const char*)yy_text; *name_len = yy_pos - yy_text;}

0 commit comments

Comments
 (0)