FIDL 语法

修改后的 BNF 规则

这是 FIDL 源文件的语法。该语法采用 经过修改的 BNF 格式。

非终结符号与一系列其他符号相匹配,这些符号以 逗号。

nonterminal = list , of , symbols ;

有些符号为端子,要么全部大写,要么全部采用 双引号。

another-nonterminal = THESE , ARE , TERMINALS , AND , SO , IS , "this" ;

替换用竖线表示。

choice = this | that | the-other ;

选项(0 或 1)用括号表示。

optional = ( maybe , these ) , but , definitely , these ;

重复(零或更多)用括号和星号表示。

zero-or-more = ( list-part )* ;

重复(一次或多次)用括号和加号表示。

one-or-more = ( list-part )+ ;

令牌

词法拼写过程中会忽略空格和注释, 。

注释能够以两个斜杠(“//”)或三个斜杠(“///”)开头, 也可以包含在 [Doc] 属性中。 三斜杠变体和 [Doc] 属性的行为方式相同, 将注释传播到生成的目标。

fidldoc 工具会处理传播到基于 JSON 的注释中的注释 用于生成参考的中间表示法 (IR) FIDL 文件的文档页面。

请参阅 FIDL 样式指南,详细了解 评论。

语法

file 是起始符号。

file = library-header , ( using-list ) , declaration-list ;

library-header = ( attribute-list ) , "library" , compound-identifier , ";" ;

using-list = ( using , ";" )* ;

using = "using" , compound-identifier , ( "as" , IDENTIFIER ) ;

compound-identifier = IDENTIFIER ( "." , IDENTIFIER )* ;

declaration-list = ( declaration , ";" )* ;

declaration = const-declaration | layout-declaration | protocol-declaration
    | type-alias-declaration | resource-declaration | service-declaration ;

const-declaration = ( attribute-list ) , "const" , IDENTIFIER ,
    type-constructor , "=" , constant ;

layout-declaration = ( attribute-list ) , "type" , IDENTIFIER , "=" ,
    inline-layout ; [NOTE 1]

inline-layout = ( attribute-list ) , ( declaration-modifiers )* , layout-kind ,
    ( layout-subtype ) , layout-body ;

declaration-modifiers = "flexible" | "strict" | "resource",
    ( modifier-availability ) ; [NOTE 2]

layout-subtype = ":" , type-constructor ; [NOTE 3]

layout-kind = "struct" | "bits" | "enum" | "union" | "table" ;

layout-body = value-layout | struct-layout | ordinal-layout ;

value-layout = "{" , ( value-layout-member , ";" )+ , "}" ;

value-layout-member = ( attribute-list ) , IDENTIFIER , "=" ,
    constant ; [NOTE 4]

struct-layout =  "{" , ( struct-layout-member , ";" )* , "}" ;

struct-layout-member = ( attribute-list ) , member-field ;

ordinal-layout =  "{" , ( ordinal-layout-member , ";" )* , "}" ; [NOTE 5]

ordinal-layout-member = ( attribute-list ) , ordinal , ":" ,
    member-field ;

protocol-declaration = ( attribute-list ) , ( protocol-modifiers )* ,
    "protocol" , IDENTIFIER , "{" , ( protocol-member , ";" )*  , "}" ;

protocol-modifiers = "closed" | "ajar" | "closed" , ( modifier-availability ) ;

protocol-member = protocol-method | protocol-event | protocol-compose ;

protocol-method = ( attribute-list ) , ( method-modifiers )* , IDENTIFIER ,
    parameter-list , ( "->" , parameter-list ,
    ( "error" type-constructor ) ) ; [NOTE 6]

protocol-event = ( attribute-list ) , ( method-modifiers )* , "->" ,
    IDENTIFIER , parameter-list ;

method-modifiers = "strict" | "flexible" , ( modifier-availability ) ;

parameter-list = "(" , ( type-constructor ) , ")" ; [NOTE 7]

protocol-compose = ( attribute-list ) , "compose" , compound-identifier ;

type-alias-declaration = ( attribute-list ) , "alias" , IDENTIFIER ,  "=" ,
    type-constructor ;

resource-declaration = ( attribute-list ) , "resource_definition" , IDENTIFIER ,
    ":" , "uint32" , "{" , resource-properties ,  "}" ;

resource-properties = "properties" , "{" , ( member-field  , ";" )* , "}" , ";"

service-declaration = ( attribute-list ) , "service" , IDENTIFIER , "{" ,
                      ( service-member , ";" )* , "}" ;

service-member = ( attribute-list ) , member-field ; [NOTE 8]

member-field = IDENTIFIER , type-constructor ;

attribute-list = ( attribute )* ;

attribute = "@" , IDENTIFIER , ( "(" , constant | attribute-args , ")" ) ;

attribute-args = attribute-arg | attribute-arg , "," attribute-args ;

attribute-arg = IDENTIFIER , "=" , constant ;

modifier-availability = "(" , attribute-args , ")" ; [NOTE 9]

type-constructor = layout , ( "<" , layout-parameters , ">" ) ,
    ( ":" type-constraints ) ;

layout = compound-identifier | inline-layout ;

layout-parameters = layout-parameter | layout-parameter , "," ,
    layout-parameters ;

layout-parameter = type-constructor | constant ;

type-constraints = type-constraint | "<" type-constraint-list ">" ;

type-constraint-list = type-constraint | type-constraint , "," ,
    type-constraint-list ;

type-constraint = constant ;

constant = compound-identifier | literal ;

ordinal = NUMERIC-LITERAL ;

literal = STRING-LITERAL | NUMERIC-LITERAL | "true" | "false" ;

STRING-LITERAL

STRING-LITERAL 的语法如下:

STRING-LITERAL  = "\"" ( unicode-value )* "\"" ;
unicode-value   = literal-char | escaped-basic | escaped-unicode ;
literal-char    = any unicode character except CR, LF, "\" or "\"" ;
escaped-basic   = "\" ( "\" | "\"" | "n" | "r" | "t"  ) ;
escaped-unicode = "\u{" ( hex-digit ){1,6} "}" ;

备注 1

匿名布局简介的属性可放入下列两种情况之一: 位置:

  • type 关键字前面,或者
  • 作为 inline-layout 的一部分。

对于单个布局定义,将属性同时放置在两个位置是不行的 。

备注 2

该语法允许所有声明都使用 ( declaration-modifiers )*,但 编译器会进行如下限制:

  • 修饰符不能在同一个声明中出现两次。
  • flexiblestrict 修饰符不能一起使用。
  • flexiblestrict 修饰符只能在 layout-kind 时使用 为 bitsenumunion
  • 仅当 layout-kindstruct 时,resource 修饰符才能用于 tableunion

注意 3

该语法允许所有声明都使用 ( layout-subtype ),但编译器限制 仅当 layout-kindbitsenum 时,才允许此字段。

此外,layout-subtype允许type-constructor中 语法,但编译器将其限制为有符号或无符号整数类型 (请参阅基元)了解枚举和无符号整数类型。

Note 4

value-layout-member 允许在语法中使用更自由的 constant,但 编译器会限制 constant 可能采用的值:

  • enum 上下文中适合指定 subtype 的任何值。
  • 符合指定 subtype 且是 2 的幂的任何值,在 bits 的上下文。

注意 5

ordinal-layout 语法允许任意数量的成员,但使用严格的并集 至少要有一位成员

注意 6

protocol-method 错误节允许更自由的 type-constructor 但编译器会将其限制为 int32uint32 或 其枚举。

Note 7

parameter-list 允许在语法中使用更自由的 type-constructor, 但编译器仅支持结构体、表或联合布局。

Note 8

service-member 允许在语法中使用更自由的 type-constructor, 但编译器将其限制为 client_end 类型。

Note 9

modifier-availability允许在attribute-args中 语法,但编译器将其限制为 addedremoved 参数。