GDScript 语法

这是用 EBNF 描述的 GDScript 形式文法,供参考。

备注

此语法源自参考文档和当前实现,仅用于描述。GDScript 解析器不是根据语法定义生成的。如果有不一致的地方,应该是此语法的错误,不是 GDScript 的 Bug。

  1. (* GDScript EBNF grammar.
  2. Uppercase words are terminals generated by the tokenizer.
  3. INDENT/DEDENT are not generated by the tokenizer yet, but they are added
  4. here for reading convenience.
  5. Naturally, this only cover syntax. Semantics can't be inferred from this
  6. description.
  7. *)
  8. program = [ inheritance NEWLINE ] [ className ] { topLevelDecl } ;
  9. inheritance = "extends" ( IDENTIFIER | STRING ) { "." IDENTIFIER } ;
  10. className = "class_name" IDENTIFIER [ "," STRING ] NEWLINE ;
  11. topLevelDecl
  12. = classVarDecl
  13. | constDecl
  14. | signalDecl
  15. | enumDecl
  16. | methodDecl
  17. | constructorDecl
  18. | innerClass
  19. | "tool"
  20. ;
  21. classVarDecl = [ "onready" ] [ export ] "var" IDENTIFIER [ ":" typeHint ]
  22. [ "=" expression ] [ setget ] NEWLINE ;
  23. setget = "setget" [ IDENTIFIER ] [ "," IDENTIFIER] ;
  24. export = "export" [ "(" [ BUILTINTYPE | IDENTIFIER { "," literal } ] ")" ] ;
  25. typeHint = BUILTINTYPE | IDENTIFIER ;
  26. constDecl = "const" IDENTIFIER [ ":" typeHint ] "=" expression NEWLINE ;
  27. signalDecl = "signal" IDENTIFIER [ signalParList ] NEWLINE ;
  28. signalParList = "(" [ IDENTIFIER { "," IDENTIFIER } ] ")" ;
  29. enumDecl = "enum" [ IDENTIFIER ] "{" [ IDENTIFIER [ "=" INTEGER ]
  30. { "," IDENTIFIER [ "=" INTEGER ] } [ "," ] ] "}" NEWLINE ;
  31. methodDecl = [ rpc ] [ "static" ] "func" IDENTIFIER "(" [ parList ] ")"
  32. [ "->" typeHint] ":" stmtOrSuite ;
  33. parList = parameter { "," parameter } ;
  34. parameter = [ "var" ] IDENTIFIER [ ":" typeHint ] [ "=" expression ] ;
  35. rpc = "remote" | "master" | "puppet"
  36. | "remotesync" | "mastersync" | "puppetsync";
  37. constructorDecl = "func" IDENTIFIER "(" [ parList ] ")"
  38. [ "." "(" [ argList ] ")" ] ":" stmtOrSuite ;
  39. argList = expression { "," expression } ;
  40. innerClass = "class" IDENTIFIER [ inheritance ] ":" NEWLINE
  41. INDENT [ inheritance NEWLINE ] topLevelDecl { topLevelDecl } DEDENT ;
  42. stmtOrSuite = stmt | NEWLINE INDENT suite DEDENT ;
  43. suite = stmt { stmt };
  44. stmt
  45. = varDeclStmt
  46. | ifStmt
  47. | forStmt
  48. | whileStmt
  49. | matchStmt
  50. | flowStmt
  51. | assignmentStmt
  52. | exprStmt
  53. | assertStmt
  54. | yieldStmt
  55. | preloadStmt
  56. | "breakpoint" stmtEnd
  57. | "pass" stmtEnd
  58. ;
  59. stmtEnd = NEWLINE | ";" ;
  60. ifStmt = "if" expression ":" stmtOrSuite { "elif" expression ":" stmtOrSuite }
  61. [ "else" ":" stmtOrSuite ] ;
  62. whileStmt = "while" expression ":" stmtOrSuite;
  63. forStmt = "for" IDENTIFIER "in" expression ":" stmtOrSuite ;
  64. matchStmt = "match" expression ":" NEWLINE INDENT matchBlock DEDENT;
  65. matchBlock = patternList ":" stmtOrSuite { patternList ":" stmtOrSuite };
  66. patternList = pattern { "," pattern } ;
  67. (* Note: you can't have a binding in a pattern list, but to not complicate the
  68. grammar more it won't be restricted syntactically *)
  69. pattern = literal | BUILTINTYPE | CONSTANT | "_" | bindingPattern
  70. | arrayPattern | dictPattern ;
  71. bindingPattern = "var" IDENTIFIER ;
  72. arrayPattern = "[" [ pattern { "," pattern } [ ".." ] ] "]" ;
  73. dictPattern = "{" [ keyValuePattern ] { "," keyValuePattern } [ ".." ] "}" ;
  74. keyValuePattern = STRING [ ":" pattern ] ;
  75. flowStmt
  76. = "continue" stmtEnd
  77. | "break" stmtEnd
  78. | "return" [ expression ] stmtEnd
  79. ;
  80. assignmentStmt = subscription "=" expression stmtEnd;
  81. varDeclStmt = "var" IDENTIFIER [ "=" expression ] stmtEnd;
  82. assertStmt = "assert" "(" expression [ "," STRING ] ")" stmtEnd ;
  83. yieldStmt = "yield" "(" [ expression "," expression ] ")" ;
  84. preloadStmt = "preload" "(" CONSTANT ")" ;
  85. (* This expression grammar encodes precedence. Items later in the list have
  86. higher precedence than the ones before. *)
  87. exprStmt = expression stmtEnd ;
  88. expression = cast [ "[" expression "]" ] ;
  89. cast = ternaryExpr [ "as" typeHint ];
  90. ternaryExpr = logicOr [ "if" logicOr "else" logicOr ] ;
  91. logicOr = logicAnd { ( "or" | "||" ) logicAnd } ;
  92. logicAnd = logicNot { ( "and" | "&&" ) logicNot };
  93. logicNot = ( "!" | "not" ) logicNot | in;
  94. in = comparison { "in" comparison };
  95. comparison = bitOr { ( "<" | ">" | "<=" | ">=" | "==" | "!=" ) bitOr } ;
  96. bitOr = bitXor { "|" bitXor } ;
  97. bitXor = bitAnd { "^" bitAnd } ;
  98. bitAnd = bitShift { "&" bitShift } ;
  99. bitShift = minus { ( "<<" | ">>" ) minus } ;
  100. minus = plus { "-" plus } ;
  101. plus = factor { "+" factor } ;
  102. factor = sign { ( "*" | "/" | "%" ) sign } ;
  103. sign = ( "-" | "+" ) sign | bitNot ;
  104. bitNot = "~" bitNot | is ;
  105. is = call [ "is" ( IDENTIFIER | BUILTINTYPE ) ] ;
  106. call = attribute [ "(" [ argList ] ")" ];
  107. attribute = subscription { "." IDENTIFIER } ;
  108. subscription = primary [ "[" expression "]" ] ;
  109. primary = "true" | "false" | "null" | "self" | literal | arrayDecl
  110. | dictDecl | "(" expression ")" ;
  111. literal = STRING | NUMBER | IDENTIFIER | BUILTINTYPE
  112. | "PI" | "TAU" | "NAN" | "INF" ;
  113. arrayDecl = "[" [ expression { "," expression } "," ] "]" ;
  114. dictDecl = "{" [ keyValue { "," keyValue } "," ] "}" ;
  115. keyValue
  116. = expression ":" expression
  117. | IDENTIFIER "=" expression
  118. ;