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] |
|
2022-01-21 00:27:19 +08:00
|
|
|
['export'] 'type' NAME ['<' GenericTypeParameterList '>'] '=' 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]
|
2022-01-21 00:27:19 +08:00
|
|
|
funcbody = ['<' GenericTypeParameterList '>'] '(' [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
|
|
|
|
2021-12-28 04:48:58 +08:00
|
|
|
binding = NAME [':' TypeAnnotation]
|
|
|
|
bindinglist = binding [',' bindinglist] (* equivalent of Lua 5.1 'namelist', except with optional type annotations *)
|
2021-12-09 01:17:28 +08:00
|
|
|
|
2021-12-28 04:48:58 +08:00
|
|
|
var = NAME | prefixexp '[' exp ']' | prefixexp '.' Name
|
|
|
|
varlist = var {',' var}
|
|
|
|
prefixexp = var | functioncall | '(' exp ')'
|
|
|
|
functioncall = prefixexp funcargs | prefixexp ':' NAME funcargs
|
2021-12-09 01:17:28 +08:00
|
|
|
|
2021-12-28 04:48:58 +08:00
|
|
|
exp = (asexp | unop exp) { binop exp }
|
|
|
|
ifelseexp = 'if' exp 'then' exp {'elseif' exp 'then' exp} 'else' exp
|
|
|
|
asexp = simpleexp ['::' Type]
|
|
|
|
simpleexp = NUMBER | STRING | 'nil' | 'true' | 'false' | '...' | tableconstructor | 'function' body | prefixexp | ifelseexp
|
|
|
|
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 |
|
|
|
|
FunctionType
|
|
|
|
|
2022-01-21 00:27:19 +08:00
|
|
|
SingletonType = STRING | 'true' | 'false'
|
|
|
|
|
2021-12-28 04:48:58 +08:00
|
|
|
Type =
|
|
|
|
SimpleType ['?'] |
|
2022-02-23 03:24:15 +08:00
|
|
|
Type ['|' Type] |
|
|
|
|
Type ['&' Type]
|
2021-12-09 01:17:28 +08:00
|
|
|
|
2022-01-21 00:27:19 +08:00
|
|
|
GenericTypePackParameter = NAME '...' ['=' (TypePack | VariadicTypePack | GenericTypePack)]
|
|
|
|
GenericTypeParameterList = NAME ['=' Type] [',' GenericTypeParameterList] | GenericTypePackParameter {',' GenericTypePackParameter}
|
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]
|
|
|
|
TableType = '{' PropList '}'
|
|
|
|
FunctionType = ['<' GenericTypeList '>'] '(' [TypeList] ')' '->' ReturnType
|
2021-12-09 01:17:28 +08:00
|
|
|
```
|