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. ;