// IDL语法规则 WHITESPACE = _{ " " | "\t" | "\r" | "\n" } COMMENT = _{ "//" ~ (!"\n" ~ ANY)* ~ "\n" | "/*" ~ (!"*/" ~ ANY)* ~ "*/" } // 基本字符定义 ASCII_ALPHA = _{ 'a'..'z' | 'A'..'Z' } ASCII_DIGIT = _{ '0'..'9' } ASCII_ALPHANUM = _{ ASCII_ALPHA | ASCII_DIGIT } ASCII_HEX_DIGIT = _{ '0'..'9' | 'a'..'f' | 'A'..'F' } // 标识符 identifier = @{ ASCII_ALPHA ~ (ASCII_ALPHANUM | "_")* } // 顶层规则 idl_file = { SOI ~ (module_def | struct_def | enum_def | const_def | typedef_def | interface_def | union_def)* ~ EOI } // 模块定义 module_def = { "module" ~ identifier ~ "{" ~ module_item* ~ "}" ~ ";" } module_item = { const_def | struct_def | enum_def | typedef_def | pragma_def | interface_def | union_def | module_def } // 常量定义 const_def = { "const" ~ const_type ~ identifier ~ "=" ~ const_value ~ ";" } // 常量类型 const_type = { "long" | "double" | "float" | "boolean" | "string" | "char" | "wchar" | "octet" | "long" ~ "long" | "unsigned" ~ "long" ~ "long" | "unsigned" ~ "long" | "unsigned" ~ "short" | "wstring" } // 常量值 const_value = { hex_number | number | string | boolean | char_literal | identifier } // 数值类型 number = @{ ("+" | "-")? ~ ASCII_DIGIT+ ~ ("." ~ ASCII_DIGIT+)? } hex_number = @{ ("0x" | "0X") ~ ASCII_HEX_DIGIT+ } integer = @{ ASCII_DIGIT+ } float = @{ ASCII_DIGIT+ ~ "." ~ ASCII_DIGIT+ } // 字符串和布尔类型 string = { "\"" ~ (!"\"" ~ ANY)* ~ "\"" } boolean = { "true" | "false" | "TRUE" | "FALSE" } char_literal = { "'" ~ (!"'" ~ ANY) ~ "'" } // 结构体定义 struct_def = { annotation* ~ "struct" ~ identifier ~ "{" ~ member_def* ~ "}" ~ ";" } member_def = { annotation* ~ type_spec ~ identifier ~ array_spec? ~ ";" } array_spec = { "[" ~ integer ~ "]" } // 枚举定义 enum_def = { annotation* ~ "enum" ~ identifier ~ "{" ~ enum_value_def ~ ("," ~ enum_value_def)* ~ ","? ~ "}" ~ ";" } enum_value_def = { identifier ~ ("=" ~ integer)? } // 类型定义 typedef_def = { "typedef" ~ type_spec ~ identifier ~ ";" } // 类型规范 type_spec = { "void" | "double" | "float" | "boolean" | "char" | "wchar" | "octet" | "short" | "unsigned" ~ "short" | "long" ~ "long" | "unsigned" ~ "long" ~ "long" | "long" | "unsigned" ~ "long" | string_type | wstring_type | sequence_type | identifier } string_type = { "string" ~ ("<" ~ integer ~ ">")? } wstring_type = { "wstring" ~ ("<" ~ integer ~ ">")? } sequence_type = { "sequence" ~ "<" ~ type_spec ~ ("," ~ integer)? ~ ">" } // 接口定义 interface_def = { annotation* ~ "interface" ~ identifier ~ (":" ~ identifier)? ~ "{" ~ interface_member* ~ "}" ~ ";" } interface_member = { method_def } method_def = { annotation* ~ type_spec ~ identifier ~ "(" ~ method_param_list? ~ ")" ~ ";" } method_param_list = { method_param ~ ("," ~ method_param)* } method_param = { param } param = { (in_param | out_param | inout_param | simple_param) ~ type_spec ~ identifier } in_param = @{ "in" ~ WHITESPACE } out_param = @{ "out" ~ WHITESPACE } inout_param = @{ "inout" ~ WHITESPACE } simple_param = { "" } // 联合类型定义 union_def = { annotation* ~ "union" ~ identifier ~ "switch" ~ "(" ~ type_spec ~ ")" ~ "{" ~ union_case* ~ "}" ~ ";" } union_case = { ("case" ~ const_value ~ ":" ~ type_spec ~ identifier ~ ";") | ("default" ~ ":" ~ type_spec ~ identifier ~ ";") } // pragma定义 pragma_def = { "#pragma" ~ identifier ~ (identifier | string)* ~ ";"? } // 注解 annotation = { "@" ~ identifier ~ ("(" ~ annotation_param? ~ ")")? } annotation_param = { annotation_key_value | annotation_value } annotation_key_value = { identifier ~ "=" ~ annotation_value } annotation_value = { string | integer | float | boolean | identifier } wchar_literal = { "L" ~ "'" ~ (!"'" ~ ANY)* ~ "'" } wstring_literal = { "L" ~ "\"" ~ (!"\"" ~ ANY)* ~ "\"" } double_value = { float } float_value = { float ~ ("f" | "F")? }