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).
|
|
|
|
|
|
|
|
> Note: this grammar is currently missing type pack syntax for generic arguments
|
|
|
|
|
|
|
|
```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] |
|
|
|
|
['export'] type NAME ['<' GenericTypeList '>'] '=' 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]
|
|
|
|
funcbody = '(' [parlist] ')' [':' ReturnType] block 'end'
|
|
|
|
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' |
|
|
|
|
NAME ['.' NAME] [ '<' TypeList '>' ] |
|
|
|
|
'typeof' '(' exp ')' |
|
2021-12-09 01:17:28 +08:00
|
|
|
TableType |
|
|
|
|
FunctionType
|
|
|
|
|
2021-12-28 04:48:58 +08:00
|
|
|
Type =
|
|
|
|
SimpleType ['?'] |
|
|
|
|
SimpleType ['|' Type] |
|
|
|
|
SimpleType ['&' Type]
|
2021-12-09 01:17:28 +08:00
|
|
|
|
2021-12-28 04:48:58 +08:00
|
|
|
GenericTypeList = NAME ['...'] {',' NAME ['...']}
|
|
|
|
TypeList = Type [',' TypeList] | '...' Type
|
|
|
|
ReturnType = Type | '(' TypeList ')'
|
|
|
|
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
|
|
|
```
|