gdscript_grammar.rst 5.9 KB

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