GDScript grammarΒΆ
This is the formal grammar of GDScript written in EBNF, for reference purposes.
Note
This grammar is descriptive only, derived from the reference documentation and current implementation. The GDScript parser is not generated from a grammar definition. Inconsistencies here likely mean an error in this grammar, not a bug in GDScript.
(* GDScript EBNF grammar.
Uppercase words are terminals generated by the tokenizer.
INDENT/DEDENT are not generated by the tokenizer yet, but they are added
here for reading convenience.
Naturally, this only cover syntax. Semantics can't be inferred from this
description.
*)
program = [ inheritance NEWLINE ] [ className ] { topLevelDecl } ;
inheritance = "extends" ( IDENTIFIER | STRING ) { "." IDENTIFIER } ;
className = "class_name" IDENTIFIER [ "," STRING ] NEWLINE ;
topLevelDecl
= classVarDecl
| constDecl
| signalDecl
| enumDecl
| methodDecl
| constructorDecl
| innerClass
| "tool"
;
classVarDecl = [ "onready" ] [ export ] "var" IDENTIFIER [ ":" typeHint ]
[ "=" expression ] [ setget ] NEWLINE ;
setget = "setget" [ IDENTIFIER ] [ "," IDENTIFIER] ;
export = "export" [ "(" [ BUILTINTYPE | IDENTIFIER { "," literal } ] ")" ] ;
typeHint = BUILTINTYPE | IDENTIFIER ;
constDecl = "const" IDENTIFIER [ ":" typeHint ] "=" expression NEWLINE ;
signalDecl = "signal" IDENTIFIER [ signalParList ] NEWLINE ;
signalParList = "(" [ IDENTIFIER { "," IDENTIFIER } ] ")" ;
enumDecl = "enum" [ IDENTIFIER ] "{" [ IDENTIFIER [ "=" INTEGER ]
{ "," IDENTIFIER [ "=" INTEGER ] } [ "," ] ] "}" NEWLINE ;
methodDecl = [ rpc ] [ "static" ] "func" IDENTIFIER "(" [ parList ] ")"
[ "->" typeHint] ":" stmtOrSuite ;
parList = parameter { "," parameter } ;
parameter = [ "var" ] IDENTIFIER [ ":" typeHint ] [ "=" expression ] ;
rpc = "remote" | "master" | "puppet"
| "remotesync" | "mastersync" | "puppetsync";
constructorDecl = "func" IDENTIFIER "(" [ parList ] ")"
[ "." "(" [ argList ] ")" ] ":" stmtOrSuite ;
argList = expression { "," expression } ;
innerClass = "class" IDENTIFIER [ inheritance ] ":" NEWLINE
INDENT [ inheritance NEWLINE ] topLevelDecl { topLevelDecl } DEDENT ;
stmtOrSuite = stmt | NEWLINE INDENT suite DEDENT ;
suite = stmt { stmt };