From 5891de67241480abc83239cdfaea0492d57f0bee Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Thu, 4 May 2023 13:39:18 +0100 Subject: [PATCH] Fix grammar issues (#915) - The type list for function declarations does not accept defaults, but the grammar incorrectly allowed this. - Allow the empty table type `{}` - Allow composite types and types surrounded by parentheses (e.g. `string??`, `(string)`, `(string | number) & boolean`) --------- Co-authored-by: vegorov-rbx <75688451+vegorov-rbx@users.noreply.github.com> --- docs/_pages/grammar.md | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/docs/_pages/grammar.md b/docs/_pages/grammar.md index 03801602..96d40230 100644 --- a/docs/_pages/grammar.md +++ b/docs/_pages/grammar.md @@ -22,12 +22,12 @@ stat = varlist '=' explist | 'function' funcname funcbody | 'local' 'function' NAME funcbody | 'local' bindinglist ['=' explist] | - ['export'] 'type' NAME ['<' GenericTypeParameterList '>'] '=' Type + ['export'] 'type' NAME ['<' GenericTypeListWithDefaults '>'] '=' Type laststat = 'return' [explist] | 'break' | 'continue' funcname = NAME {'.' NAME} [':' NAME] -funcbody = ['<' GenericTypeParameterList '>'] '(' [parlist] ')' [':' ReturnType] block 'end' +funcbody = ['<' GenericTypeList '>'] '(' [parlist] ')' [':' ReturnType] block 'end' parlist = bindinglist [',' '...'] | '...' explist = {exp ','} exp @@ -63,17 +63,25 @@ SimpleType = NAME ['.' NAME] [ '<' [TypeParams] '>' ] | 'typeof' '(' exp ')' | TableType | - FunctionType + FunctionType | + '(' Type ')' SingletonType = STRING | 'true' | 'false' -Type = - SimpleType ['?'] | - Type ['|' Type] | - Type ['&' Type] +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} -GenericTypePackParameter = NAME '...' ['=' (TypePack | VariadicTypePack | GenericTypePack)] -GenericTypeParameterList = NAME ['=' Type] [',' GenericTypeParameterList] | GenericTypePackParameter {',' GenericTypePackParameter} TypeList = Type [',' TypeList] | '...' Type TypeParams = (Type | TypePack | VariadicTypePack | GenericTypePack) [',' TypeParams] TypePack = '(' [TypeList] ')' @@ -84,6 +92,6 @@ TableIndexer = '[' Type ']' ':' Type TableProp = NAME ':' Type TablePropOrIndexer = TableProp | TableIndexer PropList = TablePropOrIndexer {fieldsep TablePropOrIndexer} [fieldsep] -TableType = '{' PropList '}' -FunctionType = ['<' GenericTypeParameterList '>'] '(' [TypeList] ')' '->' ReturnType +TableType = '{' [PropList] '}' +FunctionType = ['<' GenericTypeList '>'] '(' [TypeList] ')' '->' ReturnType ```