2021-12-09 01:17:28 +08:00
|
|
|
---
|
|
|
|
permalink: /grammar
|
|
|
|
title: Grammar
|
2021-12-28 04:51:23 +08:00
|
|
|
classes: wide
|
2021-12-09 01:17:28 +08:00
|
|
|
---
|
|
|
|
|
|
|
|
This is the complete syntax grammar for Luau in EBNF. More information about the terminal nodes String and Number
|
|
|
|
is available in the [syntax section](syntax).
|
|
|
|
|
|
|
|
```ebnf
|
2021-12-28 04:48:58 +08:00
|
|
|
chunk = block
|
|
|
|
block = {stat [';']} [laststat [';']]
|
|
|
|
stat = varlist '=' explist |
|
2021-12-09 01:17:28 +08:00
|
|
|
var compoundop exp |
|
|
|
|
functioncall |
|
2021-12-28 04:48:58 +08:00
|
|
|
'do' block 'end' |
|
|
|
|
'while' exp 'do' block 'end' |
|
|
|
|
'repeat' block 'until' exp |
|
|
|
|
'if' exp 'then' block {'elseif' exp 'then' block} ['else' block] 'end' |
|
|
|
|
'for' binding '=' exp ',' exp [',' exp] 'do' block 'end' |
|
|
|
|
'for' bindinglist 'in' explist 'do' block 'end' |
|
|
|
|
'function' funcname funcbody |
|
|
|
|
'local' 'function' NAME funcbody |
|
|
|
|
'local' bindinglist ['=' explist] |
|
2023-05-04 20:39:18 +08:00
|
|
|
['export'] 'type' NAME ['<' GenericTypeListWithDefaults '>'] '=' Type
|
2021-12-09 01:17:28 +08:00
|
|
|
|
2021-12-28 04:48:58 +08:00
|
|
|
laststat = 'return' [explist] | 'break' | 'continue'
|
2021-12-09 01:17:28 +08:00
|
|
|
|
2021-12-28 04:48:58 +08:00
|
|
|
funcname = NAME {'.' NAME} [':' NAME]
|
2023-05-04 20:39:18 +08:00
|
|
|
funcbody = ['<' GenericTypeList '>'] '(' [parlist] ')' [':' ReturnType] block 'end'
|
2021-12-28 04:48:58 +08:00
|
|
|
parlist = bindinglist [',' '...'] | '...'
|
2021-12-09 01:17:28 +08:00
|
|
|
|
2021-12-28 04:48:58 +08:00
|
|
|
explist = {exp ','} exp
|
|
|
|
namelist = NAME {',' NAME}
|
2021-12-09 01:17:28 +08:00
|
|
|
|
2023-04-25 03:21:26 +08:00
|
|
|
binding = NAME [':' Type]
|
2021-12-28 04:48:58 +08:00
|
|
|
bindinglist = binding [',' bindinglist] (* equivalent of Lua 5.1 'namelist', except with optional type annotations *)
|
2021-12-09 01:17:28 +08:00
|
|
|
|
2023-04-25 03:21:26 +08:00
|
|
|
var = NAME | prefixexp '[' exp ']' | prefixexp '.' NAME
|
2021-12-28 04:48:58 +08:00
|
|
|
varlist = var {',' var}
|
|
|
|
prefixexp = var | functioncall | '(' exp ')'
|
|
|
|
functioncall = prefixexp funcargs | prefixexp ':' NAME funcargs
|
2021-12-09 01:17:28 +08:00
|
|
|
|
2023-04-25 03:21:26 +08:00
|
|
|
exp = asexp { binop exp } | unop exp { binop exp }
|
2021-12-28 04:48:58 +08:00
|
|
|
ifelseexp = 'if' exp 'then' exp {'elseif' exp 'then' exp} 'else' exp
|
|
|
|
asexp = simpleexp ['::' Type]
|
2022-08-25 03:01:00 +08:00
|
|
|
stringinterp = INTERP_BEGIN exp { INTERP_MID exp } INTERP_END
|
2023-04-25 03:21:26 +08:00
|
|
|
simpleexp = NUMBER | STRING | 'nil' | 'true' | 'false' | '...' | tableconstructor | 'function' funcbody | prefixexp | ifelseexp | stringinterp
|
2021-12-28 04:48:58 +08:00
|
|
|
funcargs = '(' [explist] ')' | tableconstructor | STRING
|
2021-12-09 01:17:28 +08:00
|
|
|
|
2021-12-28 04:48:58 +08:00
|
|
|
tableconstructor = '{' [fieldlist] '}'
|
|
|
|
fieldlist = field {fieldsep field} [fieldsep]
|
|
|
|
field = '[' exp ']' '=' exp | NAME '=' exp | exp
|
|
|
|
fieldsep = ',' | ';'
|
2021-12-09 01:17:28 +08:00
|
|
|
|
2021-12-28 04:48:58 +08:00
|
|
|
compoundop :: '+=' | '-=' | '*=' | '/=' | '%=' | '^=' | '..='
|
|
|
|
binop = '+' | '-' | '*' | '/' | '^' | '%' | '..' | '<' | '<=' | '>' | '>=' | '==' | '~=' | 'and' | 'or'
|
|
|
|
unop = '-' | 'not' | '#'
|
2021-12-09 01:17:28 +08:00
|
|
|
|
2021-12-28 04:48:58 +08:00
|
|
|
SimpleType =
|
|
|
|
'nil' |
|
2022-01-21 00:27:19 +08:00
|
|
|
SingletonType |
|
|
|
|
NAME ['.' NAME] [ '<' [TypeParams] '>' ] |
|
2021-12-28 04:48:58 +08:00
|
|
|
'typeof' '(' exp ')' |
|
2021-12-09 01:17:28 +08:00
|
|
|
TableType |
|
2023-05-04 20:39:18 +08:00
|
|
|
FunctionType |
|
|
|
|
'(' Type ')'
|
2021-12-09 01:17:28 +08:00
|
|
|
|
2022-01-21 00:27:19 +08:00
|
|
|
SingletonType = STRING | 'true' | 'false'
|
|
|
|
|
2023-05-04 20:39:18 +08:00
|
|
|
UnionSuffix = {'?'} {'|' SimpleType {'?'}}
|
|
|
|
IntersectionSuffix = {'&' SimpleType}
|
|
|
|
Type = SimpleType (UnionSuffix | IntersectionSuffix)
|
|
|
|
|
|
|
|
GenericTypePackParameter = NAME '...'
|
|
|
|
GenericTypeList = NAME [',' GenericTypeList] | GenericTypePackParameter {',' GenericTypePackParameter}
|
|
|
|
|
|
|
|
GenericTypePackParameterWithDefault = NAME '...' '=' (TypePack | VariadicTypePack | GenericTypePack)
|
|
|
|
GenericTypeListWithDefaults =
|
|
|
|
GenericTypeList {',' GenericTypePackParameterWithDefault} |
|
|
|
|
NAME {',' NAME} {',' NAME '=' Type} {',' GenericTypePackParameterWithDefault} |
|
|
|
|
NAME '=' Type {',' GenericTypePackParameterWithDefault} |
|
|
|
|
GenericTypePackParameterWithDefault {',' GenericTypePackParameterWithDefault}
|
2021-12-09 01:17:28 +08:00
|
|
|
|
2021-12-28 04:48:58 +08:00
|
|
|
TypeList = Type [',' TypeList] | '...' Type
|
2022-01-21 00:27:19 +08:00
|
|
|
TypeParams = (Type | TypePack | VariadicTypePack | GenericTypePack) [',' TypeParams]
|
|
|
|
TypePack = '(' [TypeList] ')'
|
|
|
|
GenericTypePack = NAME '...'
|
|
|
|
VariadicTypePack = '...' Type
|
|
|
|
ReturnType = Type | TypePack
|
2021-12-28 04:48:58 +08:00
|
|
|
TableIndexer = '[' Type ']' ':' Type
|
|
|
|
TableProp = NAME ':' Type
|
|
|
|
TablePropOrIndexer = TableProp | TableIndexer
|
|
|
|
PropList = TablePropOrIndexer {fieldsep TablePropOrIndexer} [fieldsep]
|
2023-05-04 20:39:18 +08:00
|
|
|
TableType = '{' [PropList] '}'
|
|
|
|
FunctionType = ['<' GenericTypeList '>'] '(' [TypeList] ')' '->' ReturnType
|
2021-12-09 01:17:28 +08:00
|
|
|
```
|