diff --git a/pmd-lua/src/main/antlr4/net/sourceforge/pmd/lang/lua/antlr4/Lua.g4 b/pmd-lua/src/main/antlr4/net/sourceforge/pmd/lang/lua/antlr4/Lua.g4 index 1f84395d51..30e36071ff 100644 --- a/pmd-lua/src/main/antlr4/net/sourceforge/pmd/lang/lua/antlr4/Lua.g4 +++ b/pmd-lua/src/main/antlr4/net/sourceforge/pmd/lang/lua/antlr4/Lua.g4 @@ -62,6 +62,7 @@ Tested by Matt Hargett with: - Entire codebase and test suite for neovim v0.7.2: https://github.com/neovim/neovim/tree/v0.7.2 - Entire codebase for World of Warcraft Interface: https://github.com/tomrus88/BlizzardInterfaceCode - Benchmarks and conformance test suite for Luau 0.537: https://github.com/Roblox/luau/tree/0.537 + - Entire Lua codebase for nmap 7.92 : https://github.com/nmap/nmap */ grammar Lua; @@ -115,7 +116,7 @@ funcname ; funcbody - : ('<' genericTypeParameterList '>')? '(' parlist? ')' (':' '...'? returnType ) block 'end' // genericTypeParameterList and returnType + : ('<' genericTypeParameterList '>')? '(' parlist? ')' (':' '...'? returnType ) block 'end' ; parlist @@ -249,7 +250,7 @@ simpleType : NIL | singletonType | NAME ('.' NAME)? ('<' typeParams '>')? - | 'typeof' '(' NAME ('(' ')')? | '...' ')' // can't use `exp`, manually handle common cases + | 'typeof' '(' exp ')' | tableType | functionType ; @@ -260,25 +261,25 @@ singletonType type : simpleType ('?')? - | simpleType ('?')? ('|' type) // can't use type because it's mutually left-recursive - | simpleType ('?')? ('&' type) // can't use type because it's mutually left-recursive + | type ('|' type) + | type ('&' type) ; -genericTypePackParameter: NAME '...' ('=' (('(' (typeList)? ')') | variadicTypePack | genericTypePack))?; // typePack must be inlined here +genericTypePackParameter: NAME '...' ('=' (typePack | variadicTypePack | genericTypePack))?; genericTypeParameterList: NAME ('=' type)? (',' genericTypeParameterList)? | genericTypePackParameter (',' genericTypePackParameter)*; -typeList: type (',' type)? | variadicTypePack; +typeList: type (',' typeList)? | variadicTypePack; -typeParams: (type | variadicTypePack | genericTypePack) (',' typeParams)?; // had to remove typePack +typeParams: (type | typePack | variadicTypePack | genericTypePack) (',' typeParams)?; -// typePack: inlined everywhere to avoid overly greedy match when out-of-context +typePack: '(' (typeList)? ')'; genericTypePack: NAME '...'; variadicTypePack: '...' type; -returnType: variadicTypePack | '(' typeList ')' | '(' ')'; // can't use typePack, inline common cases +returnType: type | typePack; tableIndexer: '[' type ']' ':' type; @@ -288,7 +289,7 @@ tablePropOrIndexer : tableProp | tableIndexer; propList - : tablePropOrIndexer ((','|';') tablePropOrIndexer)* (','|';')?; + : tablePropOrIndexer (fieldsep tablePropOrIndexer)* fieldsep?; tableType : '{' propList '}'; @@ -296,7 +297,7 @@ tableType functionType: ('<' genericTypeParameterList '>')? '(' (typeList)? ')' '->' returnType; require - : 'require' '(' (NAME ('.' NAME)*) | NORMALSTRING ')' ('::' type)? + : 'local'? bindinglist '=' 'require' '(' exp ')' ('.' NAME)* ('::' type)? ';'? ; diff --git a/pmd-lua/src/test/resources/net/sourceforge/pmd/lang/lua/cpd/testdata/luauTypes.lua b/pmd-lua/src/test/resources/net/sourceforge/pmd/lang/lua/cpd/testdata/luauTypes.lua index 6afb9b7c56..9e418a61de 100644 --- a/pmd-lua/src/test/resources/net/sourceforge/pmd/lang/lua/cpd/testdata/luauTypes.lua +++ b/pmd-lua/src/test/resources/net/sourceforge/pmd/lang/lua/cpd/testdata/luauTypes.lua @@ -8,7 +8,7 @@ local _notVariable = not x local _length = #{x} export type Function = (...any) -> T... local _PlatformService = nil -local game = require(script.Parent.game) :: any +local game = require(script.Parent.game).default :: any pcall(function() _PlatformService = game:GetService('PlatformService') end) diff --git a/pmd-lua/src/test/resources/net/sourceforge/pmd/lang/lua/cpd/testdata/luauTypes.txt b/pmd-lua/src/test/resources/net/sourceforge/pmd/lang/lua/cpd/testdata/luauTypes.txt index 1517afea84..ac7939686c 100644 --- a/pmd-lua/src/test/resources/net/sourceforge/pmd/lang/lua/cpd/testdata/luauTypes.txt +++ b/pmd-lua/src/test/resources/net/sourceforge/pmd/lang/lua/cpd/testdata/luauTypes.txt @@ -84,8 +84,10 @@ L11 [.] 35 35 [game] 36 39 [)] 40 40 - [::] 42 43 - [any] 45 47 + [.] 41 41 + [default] 42 48 + [::] 50 51 + [any] 53 55 L12 [pcall] 1 5 [(] 6 6