根据IDL文件生成RUST文件
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

111 rindas
3.8KB

  1. // IDL语法规则
  2. WHITESPACE = _{ " " | "\t" | "\r" | "\n" }
  3. COMMENT = _{ "//" ~ (!"\n" ~ ANY)* ~ "\n" | "/*" ~ (!"*/" ~ ANY)* ~ "*/" }
  4. // 基本字符定义
  5. ASCII_ALPHA = _{ 'a'..'z' | 'A'..'Z' }
  6. ASCII_DIGIT = _{ '0'..'9' }
  7. ASCII_ALPHANUM = _{ ASCII_ALPHA | ASCII_DIGIT }
  8. ASCII_HEX_DIGIT = _{ '0'..'9' | 'a'..'f' | 'A'..'F' }
  9. // 标识符
  10. identifier = @{ ASCII_ALPHA ~ (ASCII_ALPHANUM | "_")* }
  11. // 顶层规则
  12. idl_file = { SOI ~ (module_def | struct_def | enum_def | const_def | typedef_def | interface_def | union_def)* ~ EOI }
  13. // 模块定义
  14. module_def = { "module" ~ identifier ~ "{" ~ module_item* ~ "}" ~ ";" }
  15. module_item = { const_def | struct_def | enum_def | typedef_def | pragma_def | interface_def | union_def | module_def }
  16. // 常量定义
  17. const_def = { "const" ~ const_type ~ identifier ~ "=" ~ const_value ~ ";" }
  18. // 常量类型
  19. const_type = {
  20. "long" | "double" | "float" | "boolean" | "string" | "char" | "wchar" | "octet" |
  21. "long" ~ "long" |
  22. "unsigned" ~ "long" ~ "long" |
  23. "unsigned" ~ "long" |
  24. "unsigned" ~ "short" |
  25. "wstring"
  26. }
  27. // 常量值
  28. const_value = {
  29. hex_number | number | string | boolean | char_literal | identifier
  30. }
  31. // 数值类型
  32. number = @{ ("+" | "-")? ~ ASCII_DIGIT+ ~ ("." ~ ASCII_DIGIT+)? }
  33. hex_number = @{ ("0x" | "0X") ~ ASCII_HEX_DIGIT+ }
  34. integer = @{ ASCII_DIGIT+ }
  35. float = @{ ASCII_DIGIT+ ~ "." ~ ASCII_DIGIT+ }
  36. // 字符串和布尔类型
  37. string = { "\"" ~ (!"\"" ~ ANY)* ~ "\"" }
  38. boolean = { "true" | "false" | "TRUE" | "FALSE" }
  39. char_literal = { "'" ~ (!"'" ~ ANY) ~ "'" }
  40. // 结构体定义
  41. struct_def = { annotation* ~ "struct" ~ identifier ~ "{" ~ member_def* ~ "}" ~ ";" }
  42. member_def = { annotation* ~ type_spec ~ identifier ~ array_spec? ~ ";" }
  43. array_spec = { "[" ~ integer ~ "]" }
  44. // 枚举定义
  45. enum_def = { annotation* ~ "enum" ~ identifier ~ "{" ~ enum_value_def ~ ("," ~ enum_value_def)* ~ ","? ~ "}" ~ ";" }
  46. enum_value_def = { identifier ~ ("=" ~ integer)? }
  47. // 类型定义
  48. typedef_def = { "typedef" ~ type_spec ~ identifier ~ ";" }
  49. // 类型规范
  50. type_spec = {
  51. "void" | "double" | "float" | "boolean" | "char" | "wchar" | "octet" |
  52. "short" | "unsigned" ~ "short" |
  53. "long" ~ "long" | "unsigned" ~ "long" ~ "long" |
  54. "long" | "unsigned" ~ "long" |
  55. string_type | wstring_type |
  56. sequence_type |
  57. identifier
  58. }
  59. string_type = { "string" ~ ("<" ~ integer ~ ">")? }
  60. wstring_type = { "wstring" ~ ("<" ~ integer ~ ">")? }
  61. sequence_type = { "sequence" ~ "<" ~ type_spec ~ ("," ~ integer)? ~ ">" }
  62. // 接口定义
  63. interface_def = { annotation* ~ "interface" ~ identifier ~ (":" ~ identifier)? ~ "{" ~ interface_member* ~ "}" ~ ";" }
  64. interface_member = { method_def }
  65. method_def = { annotation* ~ type_spec ~ identifier ~ "(" ~ method_param_list? ~ ")" ~ ";" }
  66. method_param_list = { method_param ~ ("," ~ method_param)* }
  67. method_param = { param }
  68. param = { (in_param | out_param | inout_param | simple_param) ~ type_spec ~ identifier }
  69. in_param = @{ "in" ~ WHITESPACE }
  70. out_param = @{ "out" ~ WHITESPACE }
  71. inout_param = @{ "inout" ~ WHITESPACE }
  72. simple_param = { "" }
  73. // 联合类型定义
  74. union_def = {
  75. annotation* ~ "union" ~ identifier ~ "switch" ~ "(" ~ type_spec ~ ")" ~ "{" ~ union_case* ~ "}" ~ ";"
  76. }
  77. union_case = {
  78. ("case" ~ const_value ~ ":" ~ type_spec ~ identifier ~ ";") |
  79. ("default" ~ ":" ~ type_spec ~ identifier ~ ";")
  80. }
  81. // pragma定义
  82. pragma_def = { "#pragma" ~ identifier ~ (identifier | string)* ~ ";"? }
  83. // 注解
  84. annotation = { "@" ~ identifier ~ ("(" ~ annotation_param? ~ ")")? }
  85. annotation_param = { annotation_key_value | annotation_value }
  86. annotation_key_value = { identifier ~ "=" ~ annotation_value }
  87. annotation_value = { string | integer | float | boolean | identifier }
  88. wchar_literal = { "L" ~ "'" ~ (!"'" ~ ANY)* ~ "'" }
  89. wstring_literal = { "L" ~ "\"" ~ (!"\"" ~ ANY)* ~ "\"" }
  90. double_value = { float }
  91. float_value = { float ~ ("f" | "F")? }