polarphp 支持语言的语法结构汇总

#基本文法结构

源代码字符集语法:
unicode_input_character:
    unicode_escape
    raw_input_character

unicode_escape:
    \u hex_digit hex_digit hex_digit hex_digit

raw_input_character:
    any unicode character

unicode_input_characters
    unicode_input_character
    unicode_input_character unicode_input_characters

input_character:
    any unicode character but not CR or LF

input_characters:
    input_character
    input_character input_characters
空白符语法:
white_space
    white_space_item
    white_space_item white_space

line_terminator:
    LF (U+000A)
    CR (U+000D)
    CRLF (U+000D followed by U+000A)

white_space_item:
    NUL (U+0000)
    Horizontal Tabulation  (U+0009)
    Vertical Tabulation  (U+000B)
    Form Feed (U+000C)
    Space (U+0020)
    line_terminator
注释语法:
comment:
    traditional_comment
    end_of_line_comment

traditional_comment:
    /* comment_tail

comment_tail:
    * comment_tail_star
    not_star comment_tail

comment_tail_star:
    /
    * comment_tail_star
    not_star_not_slash comment_tail 

not_star:
    input_character but not *
    line_terminator

not_star_not_slash:
    input_character but not * or /
    line_terminator

end_of_line_comment:
    // input_charactersopt
源代码基本元素语法:
input_element:
    white_space
    comment
    token
Token 语法:
token:
    identifier
    keyword
    literal
    punctuator
    operator
关键字语法:
keyword:
    (one of)
    abstract   and   array   as   break
    callable   case   catch   class   clone
    const   continue   declare   default   die
    do   echo   else   elseif   empty
    enddeclare   exit   extends   final   finally
    for   foreach   function   global   goto
    if   implements   instanceof   insteadof   interface
    list   namespace   new   or   thread_local
    private   protected   public   return   static
    switch   throw   trait   try   use
    var   while   xor   yield   yield from
    throws   import   module   fallthrough   synchronized
    volatile   void   transient   self   parent
    let   export   assert
标点符号(punctuator)语法:
punctuator:
    (one of)
    [   ]   (   )   {   }   ,   .   ...   @   ::
运算符(operator)语法:
operator:
    (one of)
    =   >   <   !   ~   ?   :   ->
    ==   >=   <=    !=    &&    ||   ++   --   ===   !==
    +   -   *   /   &   |   ^   %   <<   >>   ??
    +=   -=   *=   /=   &=   |=   ^=   %=   <<=   >>=
    **   **=   <=>
标识符语法:
identifier:
    typephp_letter typephp_letter_or_digits

typephp_letter_or_digits:
    typephp_letter_or_digit typephp_letter_or_digitsopt

typephp_letter:
    any unicode character that is a "typephp letter"

typephp_letter_or_digit:
    any unicode character that is a "typephp letter_or_digit"

type_identifier:
    identifier
整型字面量语法:
integer_literal:
    decimal_integer_literal
    hex_integer_literal
    octal_integer_literal
    binary_integer_literal

decimal_integer_literal:
    decimal_numeral integer_type_suffixopt

hex_integer_literal:
    hex_numeral integer_type_suffixopt

octal_integer_literal:
    octal_numeral integer_type_suffixopt

binary_integer_literal:
    binary_numeral  integer_type_suffixopt

integer_type_suffix:
    (one of)
    l L

decimal_numeral:
    0
    non_zero_digit digitsopt
    non_zero_digit underscores digits

non_zero_digit:
    (one of)
    1 2 3 4 5 6 7 8 9

digits:
    digit
    digit digits_and_underscoresopt digit

digit:
    0
    non_zero_digit

digits_and_underscores:
    digit_or_underscore
    digit_or_underscore digits_and_underscores

digit_or_underscore:
    digit
    _

underscores:
    _ underscoresopt

hex_numeral:
    0 x hex_digits
    0 X hex_digits

hex_digits:
    hex_digit
    hex_digit hex_digits_and_underscoresopt hex_digit

hex_digit:
    (one of)
    0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F

hex_digits_and_underscores:
    hex_digit_or_underscore
    hex_digit_or_underscore hex_digits_and_underscores

hex_digit_or_underscore:
    hex_digit
    _

octal_numeral:
    0 octal_digits
    0 underscores octal_digits

octal_digits:
    octal_digit
    octal_digit octal_digits_and_underscoresopt octal_digit

octal_digit:
    (one of)
    0 1 2 3 4 5 6 7

octal_digits_and_underscores:
    octal_digit_or_underscore
    octal_digit_or_underscore octal_digits_and_underscores

octal_digit_or_underscore:
    octal_digit
    _

binary_numeral:
    0 b binary_digits
    0 B binary_digits

binary_digits:
    binary_digit
    binary_digit binary_digits_and_underscoresopt binary_digit

binary_digit:
    (one of)
    0 1

binary_digits_and_underscores:
    binary_digit_or_underscore
    binary_digit_or_underscore binary_digits_and_underscores

binary_digit_or_underscore:
    binary_digit
    _
浮点型字面量语法:
floating_point_literal:
    decimal_floating_point_literal
    hexadecimal_floating_point_literal

decimal_floating_point_literal:
    digits . digitsopt exponent_partopt float_type_suffixopt
    . digits exponent_partopt float_type_suffixopt
    digits exponent_part float_type_suffixopt
    digits exponent_partopt float_type_suffix

exponent_part:
    exponent_indicator signed_integer

exponent_indicator:
    (one of)
    e E

signed_integer:
    sginopt digits

sgin:
    (one of)
    + -

float_type_suffix:
    (one of)
    f F d D

hexadecimal_floating_point_literal:
    hex_significand binary_exponent float_type_suffixopt

hex_significand:
    hex_numeral .opt
    0 x hex_digitsopt . hex_digits 
    0 X hex_digitsopt . hex_digits

binary_exponent:
    binary_exponent_indicator signed_integer

binary_exponent_indicator:
    (one of)
    p P
布尔型字面量语法:
boolean_literal:
    (one of)
    true false
字符型字面量语法:
character_literal:
    ' single_character ' 
    ' escape_sequence '

single_character:
    input_character but not ' or \

escape_sequence:
    \ b (Backspace U+0008)
    \ t (Horizontal Tabulation U+0009)
    \ n (New Line U+000A)
    \ f (Form Feed U+000C)
    \ r (Carriage Return U+000D)
    \ " (Quotation Mark U+0022)
    \ ' (Apostrophe U+0027)
    \ \ (Reverse Solidus U+005C)
    octal_escape (U+0000 ~ U+00FF)

octal_escape:
    \ octal_digit
    \ octal_digit octal_digit
    \ zero_to_three octal_digit octal_digit

zero_to_three:
    (one of)
    0 1 2 3
字符串字面量语法:(TODO 未实现 Heredoc 和 Nowdoc)
string_literal:
    single_quote_string
    double_quote_string

single_quote_string:
    b_prefixopt ' string_charactersopt '

double_quote_string:
    b_prefixopt " string_charactersopt "

b_prefix:
    (one of)
    b B

string_characters:
    string_character
    string_character string_characters

string_character:
    input_character but not " or \
    escape_sequence
空值字面量语法:
null_literal:
    null

#类型语法结构

基础类型语法:
fundamental_type:
    annotationsopt numeric_type
    annotationsopt Boolean

numeric_type:
    integral_type
    floating_point_type

integral_type:
    (one of)
    Byte Short Integer Long Char

floating_point_type:
    (one of)
    Float Double
扩展引用类型语法:
extended_reference_type:
    class_or_interface_type
    type_variable
    array_type
    annotation_type

class_or_interface_type:
    class_type
    interface_type

class_type:
    class_type_item
    namespace_name \ class_type_item

class_type_item:
    annotationsopt type_identifier type_argumentsopt
    annotationsopt type_identifier type_argumentsopt :: class_type_item

interface_type:
    class_type

annotation_type:
    class_type

type_variable:
    annotationsopt type_identifier

array_type:
    fundamental_type dimensions
    class_or_interface_or_annotation_type dimensions
    type_variable dimensions

dimensions:
    dimension
    dimension dimensions

dimension:
    annotationsopt [ ]
(泛型)类型形参语法:

type_parameters:
    < type_parameter_list >

type_parameter_list:
    type_parameter
    type_parameter , type_parameter_list

type_parameter:
    type_parameter_modifieropt type_identifier type_boundopt

type_parameter_modifier:
    type_parameter_modifier_item
    type_parameter_modifier_item type_parameter_modifier

type_parameter_modifier_item:
    annotation

type_bound:
    extends TypeVariable
    extends class_or_interface_type additional_bounds

additional_bounds:
    additional_bound
    additional_bound additional_bounds

additional_bound:
    & interface_type
(泛型)类型实参语法:
type_arguments:
    < type_argument_list >

type_argument_list:
    type_argument
    type_argument , type_argument_list

type_argument:
    extended_reference_type
    wildcard

wildcard:
    annotationsopt ? wildcard_boundsopt

wildcard_bounds:
    extends extended_reference_type
    super extended_reference_type

#程序实体命名语法结构

名称相关语法:
module_name:
    identifier
    identifier . module_name

namespace_name:
    identifier
    identifier \ namespace_name

namespace_name_as_prefix:
    \
    \opt namespace_name \
    namespace \
    namespace \ namespace_name \

type_name:
    type_identifier

qualified_type_name:
    namespace_name_as_prefixopt type_identifier

variable_name:
    $ identifier

#模块语法结构

模块相关语法:
module_declaration:
    exportopt annotationsopt module module_name ;

module_import_declaration:
    transientopt import module_name ;

#命名空间语法结构

命名空间定义语法:
namespace_declaration:
    namespace namespace_name ;
    namespace namespace_nameopt { declaration_seq }
命名空间引入语法:
namespace_use_declaration:
    use mixed_group_use_declaration ;
    use use_type group_use_declaration ;
    use use_typeopt use_declarations ;

use_type:
    (one of)
    const
    function

group_use_declaration:
    \opt namespace_name \ { unprefixed_use_declarations ,opt }

mixed_group_use_declaration:
    \opt namespace_name \ { inline_use_declarations ,opt }

inline_use_declarations:
    inline_use_declaration
    inline_use_declaration inline_use_declarations

unprefixed_use_declarations:
    unprefixed_use_declaration
    unprefixed_use_declaration unprefixed_use_declarations

use_declarations:
    use_declaration
    use_declaration use_declarations

inline_use_declaration:
    unprefixed_use_declaration
    use_type unprefixed_use_declaration

unprefixed_use_declaration:
    namespace_name
    namespace_name as identifier

use_declaration:
    \opt unprefixed_use_declaration

#编译单元语法结构

编译单元语法:
translation_unit:
    top_level_declaration_seqopt
    module-declaration top_level_declaration_seqopt

top_level_declaration_seq:
    top_level_declaration
    top_level_declaration top_level_declaration_seq

top_level_declaration:
    module_import_declaration
    namespace_declaration
    namespace_use_declaration
    declaration

#声明与定义相关语法

简单变量定义语法:
simple_declaration:
    annotationsopt decl_specifier_seq type init_declarator_listopt ;

decl_specifier_seq:
    decl_specifier
    decl_specifier decl_specifier_seq

decl_specifier:
    weak
    unowned
    const
    var
    thread_local

init_declarator_list:
    init_declarator
    init_declarator , init_declarator_list

init_declarator:
    variable_declarator_id variable_initializer_clauseopt

variable_declarator_id:
    identifier dimensionsopt

variable_initializer_clause:
    = variable_initializer

variable_initializer:
    expression
    array_initializer

array_initializer:
    { variable_initializer_listopt ,opt }

variable_initializer_list:
    variable_initializer
    variable_initializer , variable_initializer_listopt
类定义语法:
class_declaration:
    class_modifiersopt class type_identifier type_parametersopt super_classopt super_interfacesopt class_body

class_modifiers:
    class_modifier
    class_modifier class_modifiers

class_modifier:
    annotation 
    public
    protected 
    private 
    abstract 
    final

super_class:
    extends class_type

super_interfaces:
    implements interface_type_list

interface_type_list:
    interface_type
    interface_type interface_type_list

class_body:
    { class_body_declarationsopt }

class_body_declarations:
    class_body_declaration
    class_body_declaration class_body_declarations

class_body_declaration:
    class_member_declaration
    constructor_declaration
    destructor_declaration

class_member_declaration:
    field_declaration
    method_declaration
    class_declaration
    interface_declaration
    ;
类属性定义语法:
field_declaration:
    field_modifiersopt type variable_declarator_list ;

field_modifiers
    field_modifier
    field_modifier field_modifiers

field_modifier:
    (one of)
    annotation
    public protected volatile
    private static transient
    const

variable_declarator_list:
    variable_declarator
    variable_declarator , variable_declarator_list

variable_declarator:
    variable_declarator_id variable_initializer_clauseopt
类方法定义语法:
method_declaration:
    method_modifiersopt method_header method_body

method_modifiers:
    method_modifier
    method_modifier method_modifiers

method_modifier:
    (one of)
    annotation
    public protected private
    abstract static final 
    synchronized native

method_header:
    annotationsopt type_parametersopt function method_declarator result_type

method_declarator:
    identifier ( formal_parameter_listopt )

result_type:
    : result_type
    : void

formal_parameter_list:
    formal_parameter
    formal_parameter , formal_parameter_list

formal_parameter:
    variable_modifiersopt type variable_declarator_id 
    variable_arity_parameter

variable_modifiers:
    variable_modifier
    variable_modifier variable_modifiers

variable_modifier:
    annotation
    const

variable_arity_parameter:
    variable_modifiersopt type annotationsopt ... identifier

method_body:
    block
    ;
类构造方法定义语法:
constructor_declaration:
    constructor_modifiersopt constructor_declarator constructor_body

constructor_modifiers:
    constructor_modifier
    constructor_modifier constructor_modifiers

constructor_modifier:
    (one of)
    annotation
    public protected private

constructor_declarator:
    type_parametersopt function __construct ( formal_parameter_listopt )

constructor_body:
    { explicit_constructor_invocationopt block_statementsopt }

explicit_constructor_invocation
    type_argumentsopt self ( argument_listopt ) ;
    type_argumentsopt parent ( argument_listopt ) ;

argument_list:
    expression
    expression , argument_list
类析构方法定义语法:
destructor_declaration:
    destructor_modifiersopt destructor_declarator method_body

destructor_declarator:
    type_parametersopt function __destruct ( )

destructor_modifiers:
    destructor_modifier
    destructor_modifier destructor_modifiers

destructor_modifier:
    (one of)
    annotation
    public protected private
接口定义语法:
interface_declaration:
    interface_modifiersopt interface type_identifier type_parametersopt extends_interfacesopt interface_body

interface_modifiers:
    interface_modifier
    interface_modifier interface_modifiers

interface_modifier:
    (one of)
    annotation
    public protected private

extends_interfaces:
    extends interface_type_list

interface_body
    { interface_member_declarationsopt }

interface_member_declarations:
    interface_member_declaration
    interface_member_declaration interface_member_declarations

interface_member_declaration:
    constant_declaration
    interface_method_declaration
    ;

constant_declaration:
    constant_modifiersopt type variable_declarator_list ;

constant_modifiers
    constant_modifier
    constant_modifier constant_modifiers

constant_modifier:
    (one of)
    annotation
    public static const

interface_method_declaration
    interface_method_modifiersopt method_header ;

interface_method_modifiers:
    interface_method_modifier
    interface_method_modifier interface_method_modifiers

interface_method_modifier:
    (one of)
    annotation
    public private abstract 
    default static

#语句相关语法

选择语句块语法:
selection_statement:
    if_statement
    switch_statement

if_statement:
    if_statement_without_else
    if_statement_without_else else statement

if_statement_without_else:
    if ( expr ) statement elseif_statmentsopt

elseif_statments:
    elseif_statment
    elseif_statment elseif_statments

elseif_statment:
    elseif ( expr ) statement

switch_statement:
    switch ( expression ) switch_block

switch_block:
    { switch_block_statement_groupsopt switch_labelsopt }

switch_block_statement_groups:
    switch_block_statement_group
    switch_block_statement_group switch_block_statement_groups

switch_labels:
    switch_label
    switch_label switch_labels

switch_block_statement_group:
    switch_labels block_statements

switch_label:
    case expression :
    default :
迭代语句块语法:
iteration_statement:
    do_while_statement
    while_statement
    for_statement
    foreach_statement

do_statement
    do statement while ( expression ) ;

while_statement:
    while ( expression ) statement

for_statement:
    for ( for_initopt ; statement_expression_listopt ; for_updateopt ) statement

for_init:
    statement_expression_list
    local_variable_declaration

statement_expression_list:
    statement_expression
    statement_expression statement_expression_list

for_update:
    statement_expression_list

foreach_statement:
    foreach ( expression as foreach_keyopt foreach_value ) statement

foreach_key:
    local_variable_typeopt identifier =>

foreach_value:
    local_variable_typeopt identifier
跳转语句块语法:
jump_statement:
    goto_statement
    break_statement
    continue_statement
    return_statement
    throw_statement

goto_statement:
    goto identifier ;

continue_statement:
    continue breakout_levelopt ;

break_statement:
    break breakout_levelopt ;

breakout_level:
    identifier
    integer_literal

return_statement:
    return expressionopt ;

throw_statement:
    throw expression ;
异常捕获语法:
try_statement:
    try block catche_clauses
    try block catche_clausesopt finally_clause

catche_clauses:
    catche_clause
    catche_clause catche_clauses

catche_clause:
    catch ( catch_formal_parameter ) block

catch_formal_parameter:
    variable_modifier catch_types variable_declarator_id

catch_types:
    type
    type | catch_types

finally_clause:
    finally block

#表达式相关语法

表达式语法:
expression:
    yield_expression
    print ( yield_expression )

yield_expression:
    yield_from_expression
    yield
    yield yield_expression
    yield yield_from_expression => yield_expression

yield_from_expression:
    assignment_expression
    yield from assignment_expression

assignment_expression:
    conditional_expression
    simple_assignment_expression

simple_assignment_expression:
    variable assignment_operator assignment_expression

assignment_operator:
    (one of)
    =  *=  /=  %=  +=  -=  
    <<=>>=  >>>=  &=  ^=  |=
    **=
条件选择表达式语法:
conditional_expression:
    coalesce_expression
    coalesce_expression ? expressionopt : conditional_expression
数组表达式语法:
array_creation_expression:
    new qualified_type_name dim_exprs dimensionsopt
    new qualified_type_name dimensions array_initializer

dim_exprs:
    dim_expr dim_exprs

dim_expr:
    annotationsopt [ expression ]
逻辑表达式语法:
coalesce_expression:
    logical_inc_or_expression
    logical_inc_or_expression ?? coalesce_expression

logical_inc_or_expression:
    logical_and_expression
    logical_inc_or_expression || logical_and_expression

logical_and_expression:
    bitwise_inc_or_expression
    logical_and_expression && bitwise_inc_or_expression

bitwise_inc_or_expression:
    bitwise_exc_or_expression
    bitwise_inc_or_expression | bitwise_exc_or_expression

bitwise_exc_or_expression:
    bitwise_and_expression
    bitwise_exc_or_expression ^ bitwise_and_expression

bitwise_and_expression:
    equality_expression
    bitwise_and_expression & equality_expression

equality_expression:
    relational_expression
    equality_expression == relational_expression
    equality_expression != relational_expression
    equality_expression <> relational_expression
    equality_expression === relational_expression
    equality_expression !== relational_expression

relational_expression:
    shift_expression
    relational_expression < shift_expression
    relational_expression > shift_expression
    relational_expression <= shift_expression
    relational_expression >= shift_expression
    relational_expression <=> shift_expression

运算表达式语法:
shift_expression:
    additive_expression
    shift_expression << additive_expression
    shift_expression >> additive_expression

additive_expression:
    multiplicative_expression
    additive_expression + multiplicative_expression
    additive_expression - multiplicative_expression
    additive_expression . multiplicative_expression

multiplicative_expression:
    logical_not_expression
    multiplicative_expression * logical_not_expression
    multiplicative_expression / logical_not_expression
    multiplicative_expression % logical_not_expression

logical_not_expression:
    instanceof_expression
    ! instanceof_expression

instanceof_expression:
    unary_expression
    instanceof_subject instanceof qualified_type_name

instanceof_subject:
    instanceof_expression

unary_expression:
    unary_op_expression
    exponentiation_expression
    cast_expression

unary_op_expression:
    unary_operator unary_expression

unary_operator:
    (one of)
    + - ~

pre_increment_expression
    ++ unary_expression

pre_decrement_expression
    -- unary_expression

cast_expression:
    ( qualified_type_name  ) unary_expression

additional_bounds:
    additional_bound
    additional_bound & additional_bounds

additional_bound:
    interface_type

exponentiation_expression:
    clone_expression
    clone_expression ** exponentiation_expression

作用域表达式语法:
scoped_property_access_expression:
    scope_resolution_qualifier :: simple_variable

class_constant_access_expression:
    scope_resolution_qualifier :: identifier

scoped_call_expression:
   scope_resolution_qualifier ::type_argumentsopt identifier ( argmentsopt )

relative_scope:
    self
    parent
    static

scope_resolution_qualifier:
    relative_scope
    qualified_type_name
    dereferencable_expression

member_name:
    identifier
主表达式语法:
clone_expression:
    clone_target_expression
    clone clone_target_expression

clone_target_expression:
    pre_increment_expression
    pre_decrement_expression
    postfix_expression

postfix_expression:
    primary_expression
    post_increment_expression
    post_decrement_expression

primary_expression:
    primary_no_new_array
    array_creation_expression

primary_no_new_array:
    variable
    literal
    class_literal
    ( expression )
    anonymous_function_creation_expression
    class_instance_creation_expression
    class_constant_access_expression
    constant_access_expression

class_literal:
    qualified_type_name dimensionsopt::class
    void::class

constant_access_expression:
    qualified_type_name

anonymous_function_creation_expression:
    staticopt function ( formal_parameter_listopt ) anonymous_function_use_clauseopt result_type block

anonymous_function_use_clause:
    use ( use_variable_name_list )

use_variable_name_list:
    &opt variable_name
    &opt variable_name , use_variable_name_list

class_instance_creation_expression:
    new qualified_type_name ( argument_expression_listopt )
    new qualified_type_name
    new class ( argument_expression_listopt ) super_class super_interfaces class_body

函数调用表达式语法:
function_call_expression:
    qualified_type_name type_argumentsopt ( argument_expression_listopt )

argument_expression_list:
    argument_expression
    argument_expression , argument_expression_list

argument_expression:
    variadic_unpacking
    expression

variadic_unpacking:
    ... expression
类实例成员访问表达式语法:
member_access_expression:
    dereferencable_expression -> identifier

member_call_expression:
    dereferencable_expression -> identifier ( argument_expression_listopt )
数组访问表达式语法:
array_access_expression:
    dereferencable_expression [ expressionopt ]