Work in progress

The content of this page was not yet updated for Godot 4.2 and may be outdated. If you know how to improve this page or you can confirm that it’s up to date, feel free to open a pull request.

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 ( "=" | "+=" | "-=" | "*=" | "/="
  81. | "%=" | "&=" | "|=" | "^=" ) expression stmtEnd;
  82. varDeclStmt = "var" IDENTIFIER [ "=" expression ] stmtEnd;
  83. assertStmt = "assert" "(" expression [ "," STRING ] ")" stmtEnd ;
  84. yieldStmt = "yield" "(" [ expression "," expression ] ")" ;
  85. preloadStmt = "preload" "(" CONSTANT ")" ;
  86. (* This expression grammar encodes precedence. Items later in the list have
  87. higher precedence than the ones before. *)
  88. exprStmt = expression stmtEnd ;
  89. expression = cast [ "[" expression "]" ] ;
  90. cast = ternaryExpr [ "as" typeHint ];
  91. ternaryExpr = logicOr [ "if" logicOr "else" logicOr ] ;
  92. logicOr = logicAnd { ( "or" | "||" ) logicAnd } ;
  93. logicAnd = logicNot { ( "and" | "&&" ) logicNot };
  94. logicNot = ( "!" | "not" ) logicNot | in;
  95. in = comparison { "in" comparison };
  96. comparison = bitOr { ( "<" | ">" | "<=" | ">=" | "==" | "!=" ) bitOr } ;
  97. bitOr = bitXor { "|" bitXor } ;
  98. bitXor = bitAnd { "^" bitAnd } ;
  99. bitAnd = bitShift { "&" bitShift } ;
  100. bitShift = minus { ( "<<" | ">>" ) minus } ;
  101. minus = plus { "-" plus } ;
  102. plus = factor { "+" factor } ;
  103. factor = sign { ( "*" | "/" | "%" ) sign } ;
  104. sign = ( "-" | "+" ) sign | bitNot ;
  105. bitNot = "~" bitNot | is ;
  106. is = call [ "is" ( IDENTIFIER | BUILTINTYPE ) ] ;
  107. call
  108. = (attribute [ "(" [ argList ] ")" ])
  109. | "." IDENTIFIER "(" [ argList ] ")"
  110. | "$" ( STRING | IDENTIFIER { '/' IDENTIFIER } );
  111. attribute = subscription { "." IDENTIFIER } ;
  112. subscription = primary [ "[" expression "]" ] ;
  113. primary = "true" | "false" | "null" | "self" | literal | arrayDecl
  114. | dictDecl | "(" expression ")" ;
  115. literal = STRING | NUMBER | IDENTIFIER | BUILTINTYPE
  116. | "PI" | "TAU" | "NAN" | "INF" ;
  117. arrayDecl = "[" [ expression { "," expression } "," ] "]" ;
  118. dictDecl = "{" [ keyValue { "," keyValue } "," ] "}" ;
  119. keyValue
  120. = expression ":" expression
  121. | IDENTIFIER "=" expression
  122. ;

Previous Next


© 版权所有 2014-present Juan Linietsky, Ariel Manzur and the Godot community (CC BY 3.0). Revision b1c660f7.

Built with Sphinx using a theme provided by Read the Docs.