mirror of
https://github.com/luau-lang/luau.git
synced 2024-12-14 22:21:10 +00:00
da9d8e8c60
Implements the string interpolation RFC (#165). Adds the string interpolation as per the RFC. ```lua local name = "world" print(`Hello {name}!`) -- Hello world! ``` Co-authored-by: Arseny Kapoulkine <arseny.kapoulkine@gmail.com> Co-authored-by: Alexander McCord <11488393+alexmccord@users.noreply.github.com>
89 lines
3.1 KiB
Markdown
89 lines
3.1 KiB
Markdown
---
|
|
permalink: /grammar
|
|
title: Grammar
|
|
classes: wide
|
|
---
|
|
|
|
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
|
|
chunk = block
|
|
block = {stat [';']} [laststat [';']]
|
|
stat = varlist '=' explist |
|
|
var compoundop exp |
|
|
functioncall |
|
|
'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 ['<' GenericTypeParameterList '>'] '=' Type
|
|
|
|
laststat = 'return' [explist] | 'break' | 'continue'
|
|
|
|
funcname = NAME {'.' NAME} [':' NAME]
|
|
funcbody = ['<' GenericTypeParameterList '>'] '(' [parlist] ')' [':' ReturnType] block 'end'
|
|
parlist = bindinglist [',' '...'] | '...'
|
|
|
|
explist = {exp ','} exp
|
|
namelist = NAME {',' NAME}
|
|
|
|
binding = NAME [':' TypeAnnotation]
|
|
bindinglist = binding [',' bindinglist] (* equivalent of Lua 5.1 'namelist', except with optional type annotations *)
|
|
|
|
var = NAME | prefixexp '[' exp ']' | prefixexp '.' Name
|
|
varlist = var {',' var}
|
|
prefixexp = var | functioncall | '(' exp ')'
|
|
functioncall = prefixexp funcargs | prefixexp ':' NAME funcargs
|
|
|
|
exp = (asexp | unop exp) { binop exp }
|
|
ifelseexp = 'if' exp 'then' exp {'elseif' exp 'then' exp} 'else' exp
|
|
asexp = simpleexp ['::' Type]
|
|
stringinterp = INTERP_BEGIN exp { INTERP_MID exp } INTERP_END
|
|
simpleexp = NUMBER | STRING | 'nil' | 'true' | 'false' | '...' | tableconstructor | 'function' body | prefixexp | ifelseexp | stringinterp
|
|
funcargs = '(' [explist] ')' | tableconstructor | STRING
|
|
|
|
tableconstructor = '{' [fieldlist] '}'
|
|
fieldlist = field {fieldsep field} [fieldsep]
|
|
field = '[' exp ']' '=' exp | NAME '=' exp | exp
|
|
fieldsep = ',' | ';'
|
|
|
|
compoundop :: '+=' | '-=' | '*=' | '/=' | '%=' | '^=' | '..='
|
|
binop = '+' | '-' | '*' | '/' | '^' | '%' | '..' | '<' | '<=' | '>' | '>=' | '==' | '~=' | 'and' | 'or'
|
|
unop = '-' | 'not' | '#'
|
|
|
|
SimpleType =
|
|
'nil' |
|
|
SingletonType |
|
|
NAME ['.' NAME] [ '<' [TypeParams] '>' ] |
|
|
'typeof' '(' exp ')' |
|
|
TableType |
|
|
FunctionType
|
|
|
|
SingletonType = STRING | 'true' | 'false'
|
|
|
|
Type =
|
|
SimpleType ['?'] |
|
|
Type ['|' Type] |
|
|
Type ['&' Type]
|
|
|
|
GenericTypePackParameter = NAME '...' ['=' (TypePack | VariadicTypePack | GenericTypePack)]
|
|
GenericTypeParameterList = NAME ['=' Type] [',' GenericTypeParameterList] | GenericTypePackParameter {',' GenericTypePackParameter}
|
|
TypeList = Type [',' TypeList] | '...' Type
|
|
TypeParams = (Type | TypePack | VariadicTypePack | GenericTypePack) [',' TypeParams]
|
|
TypePack = '(' [TypeList] ')'
|
|
GenericTypePack = NAME '...'
|
|
VariadicTypePack = '...' Type
|
|
ReturnType = Type | TypePack
|
|
TableIndexer = '[' Type ']' ':' Type
|
|
TableProp = NAME ':' Type
|
|
TablePropOrIndexer = TableProp | TableIndexer
|
|
PropList = TablePropOrIndexer {fieldsep TablePropOrIndexer} [fieldsep]
|
|
TableType = '{' PropList '}'
|
|
FunctionType = ['<' GenericTypeList '>'] '(' [TypeList] ')' '->' ReturnType
|
|
```
|