Grammar

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.

  1. chunk = block
  2. block = {stat [';']} [laststat [';']]
  3. stat = varlist '=' explist |
  4. var compoundop exp |
  5. functioncall |
  6. 'do' block 'end' |
  7. 'while' exp 'do' block 'end' |
  8. 'repeat' block 'until' exp |
  9. 'if' exp 'then' block {'elseif' exp 'then' block} ['else' block] 'end' |
  10. 'for' binding '=' exp ',' exp [',' exp] 'do' block 'end' |
  11. 'for' bindinglist 'in' explist 'do' block 'end' |
  12. 'function' funcname funcbody |
  13. 'local' 'function' NAME funcbody |
  14. 'local' bindinglist ['=' explist] |
  15. ['export'] 'type' NAME ['<' GenericTypeParameterList '>'] '=' Type
  16. laststat = 'return' [explist] | 'break' | 'continue'
  17. funcname = NAME {'.' NAME} [':' NAME]
  18. funcbody = ['<' GenericTypeParameterList '>'] '(' [parlist] ')' [':' ReturnType] block 'end'
  19. parlist = bindinglist [',' '...'] | '...'
  20. explist = {exp ','} exp
  21. namelist = NAME {',' NAME}
  22. binding = NAME [':' TypeAnnotation]
  23. bindinglist = binding [',' bindinglist] (* equivalent of Lua 5.1 'namelist', except with optional type annotations *)
  24. var = NAME | prefixexp '[' exp ']' | prefixexp '.' Name
  25. varlist = var {',' var}
  26. prefixexp = var | functioncall | '(' exp ')'
  27. functioncall = prefixexp funcargs | prefixexp ':' NAME funcargs
  28. exp = (asexp | unop exp) { binop exp }
  29. ifelseexp = 'if' exp 'then' exp {'elseif' exp 'then' exp} 'else' exp
  30. asexp = simpleexp ['::' Type]
  31. simpleexp = NUMBER | STRING | 'nil' | 'true' | 'false' | '...' | tableconstructor | 'function' body | prefixexp | ifelseexp
  32. funcargs = '(' [explist] ')' | tableconstructor | STRING
  33. tableconstructor = '{' [fieldlist] '}'
  34. fieldlist = field {fieldsep field} [fieldsep]
  35. field = '[' exp ']' '=' exp | NAME '=' exp | exp
  36. fieldsep = ',' | ';'
  37. compoundop :: '+=' | '-=' | '*=' | '/=' | '%=' | '^=' | '..='
  38. binop = '+' | '-' | '*' | '/' | '^' | '%' | '..' | '<' | '<=' | '>' | '>=' | '==' | '~=' | 'and' | 'or'
  39. unop = '-' | 'not' | '#'
  40. SimpleType =
  41. 'nil' |
  42. SingletonType |
  43. NAME ['.' NAME] [ '<' [TypeParams] '>' ] |
  44. 'typeof' '(' exp ')' |
  45. TableType |
  46. FunctionType
  47. SingletonType = STRING | 'true' | 'false'
  48. Type =
  49. SimpleType ['?'] |
  50. Type ['|' Type] |
  51. Type ['&' Type]
  52. GenericTypePackParameter = NAME '...' ['=' (TypePack | VariadicTypePack | GenericTypePack)]
  53. GenericTypeParameterList = NAME ['=' Type] [',' GenericTypeParameterList] | GenericTypePackParameter {',' GenericTypePackParameter}
  54. TypeList = Type [',' TypeList] | '...' Type
  55. TypeParams = (Type | TypePack | VariadicTypePack | GenericTypePack) [',' TypeParams]
  56. TypePack = '(' [TypeList] ')'
  57. GenericTypePack = NAME '...'
  58. VariadicTypePack = '...' Type
  59. ReturnType = Type | TypePack
  60. TableIndexer = '[' Type ']' ':' Type
  61. TableProp = NAME ':' Type
  62. TablePropOrIndexer = TableProp | TableIndexer
  63. PropList = TablePropOrIndexer {fieldsep TablePropOrIndexer} [fieldsep]
  64. TableType = '{' PropList '}'
  65. FunctionType = ['<' GenericTypeList '>'] '(' [TypeList] ')' '->' ReturnType