Skip to content

Commit f331f96

Browse files
authored
Merge pull request #164 from Fiusen/fix/luau-type-cast-parsing
Fix luau type cast parsing and improve goto tests
2 parents e324764 + a4b55c1 commit f331f96

File tree

9 files changed

+160
-73
lines changed

9 files changed

+160
-73
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88

99
### Added
1010

11+
- Diagnostic LUA1019: "Goto statements and labels are not supported in this lua version".
1112
- Support for floor division assignments (`//=`) by @toxamin in https://github.com/LorettaDevs/Loretta/pull/157.
1213
- Support for underlines before the number base prefix by @toxamin in https://github.com/LorettaDevs/Loretta/pull/156.
1314

@@ -21,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2122

2223
### Fixed
2324

25+
- Fixed parsing of Luau type casts (`::`) when `AcceptGoto` is false by @Fiusen in https://github.com/LorettaDevs/Loretta/pull/164.
2426
- Parsing of empty return statements at the end of the file by @toxamin in https://github.com/LorettaDevs/Loretta/pull/148.
2527

2628
<!-- ### Security -->

src/Compilers/Lua/Portable/Errors/ErrorCode.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ internal enum ErrorCode
6666
ERR_TypedLuaNotSupportedInLuaVersion = 1016,
6767
ERR_OnlyOneTableTypeIndexerIsAllowed = 1017,
6868
ERR_NormalTypeParametersComeBeforePacks = 1018,
69+
ERR_GotoNotSupportedInLuaVersion = 1019,
6970

7071
// MessageProvider stuff
7172
ERR_BadDocumentationMode = 2000,

src/Compilers/Lua/Portable/Generated/Loretta.Generators.SyntaxFactsGenerator/Loretta.Generators.SyntaxFactsGenerator.SyntaxFactsGenerator/SyntaxFacts.g.cs

Lines changed: 69 additions & 69 deletions
Large diffs are not rendered by default.

src/Compilers/Lua/Portable/LuaResources.Designer.cs

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Compilers/Lua/Portable/LuaResources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,4 +319,7 @@
319319
<data name="ERR_InterpolatedStringsNotSupportedInVersion" xml:space="preserve">
320320
<value>Interpolated strings are not supported in this lua version</value>
321321
</data>
322+
<data name="ERR_GotoNotSupportedInLuaVersion" xml:space="preserve">
323+
<value>Goto statements and labels are not supported in this lua version</value>
324+
</data>
322325
</root>

src/Compilers/Lua/Portable/Parser/LanguageParser.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -599,10 +599,16 @@ private GotoStatementSyntax ParseGotoStatement()
599599
var gotoKeyword = EatToken(SyntaxKind.GotoKeyword);
600600
var labelName = EatToken(SyntaxKind.IdentifierToken);
601601
var semicolonToken = TryMatchSemicolon();
602-
return SyntaxFactory.GotoStatement(
602+
603+
var node = SyntaxFactory.GotoStatement(
603604
gotoKeyword,
604605
labelName,
605606
semicolonToken);
607+
608+
if (!Options.SyntaxOptions.AcceptGoto)
609+
node = AddError(node, ErrorCode.ERR_GotoNotSupportedInLuaVersion);
610+
611+
return node;
606612
}
607613

608614
private BreakStatementSyntax ParseBreakStatement()
@@ -628,11 +634,17 @@ private GotoLabelStatementSyntax ParseGotoLabelStatement()
628634
var identifier = EatToken(SyntaxKind.IdentifierToken);
629635
var rightDelimiterToken = EatToken(SyntaxKind.ColonColonToken);
630636
var semicolonToken = TryMatchSemicolon();
631-
return SyntaxFactory.GotoLabelStatement(
637+
638+
var node = SyntaxFactory.GotoLabelStatement(
632639
leftDelimiterToken,
633640
identifier,
634641
rightDelimiterToken,
635642
semicolonToken);
643+
644+
if (!Options.SyntaxOptions.AcceptGoto)
645+
node = AddError(node, ErrorCode.ERR_GotoNotSupportedInLuaVersion);
646+
647+
return node;
636648
}
637649

638650
private FunctionDeclarationStatementSyntax ParseFunctionDeclarationStatement()

src/Compilers/Lua/Portable/Parser/Lexer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ private void LexSyntaxToken(ref TokenInfo info)
271271

272272
case ':':
273273
TextWindow.AdvanceChar();
274-
if (_options.SyntaxOptions.AcceptGoto && TextWindow.PeekChar() == ':')
274+
if ((_options.SyntaxOptions.AcceptGoto || _options.SyntaxOptions.AcceptTypedLua) && TextWindow.PeekChar() == ':')
275275
{
276276
TextWindow.AdvanceChar();
277277
info.Kind = SyntaxKind.ColonColonToken;

src/Compilers/Lua/Portable/Syntax/SyntaxFacts.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ internal static bool IsTokenOrTriviaKindEnabled(SyntaxKind kind, LuaSyntaxOption
9191
{
9292
switch (kind)
9393
{
94-
case SyntaxKind.ColonColonToken when !options.AcceptGoto:
94+
case SyntaxKind.ColonColonToken when !options.AcceptGoto && !options.AcceptTypedLua:
9595
case SyntaxKind.SlashSlashToken or SyntaxKind.SlashSlashEqualsToken
9696
when !options.AcceptFloorDivision:
9797
case SyntaxKind.AmpersandAmpersandToken or SyntaxKind.PipePipeToken or SyntaxKind.BangToken

src/Compilers/Lua/Test/Portable/Parsing/RegressionTests.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,4 +289,67 @@ public async Task LanguageParser_ConcatIsRightAssociative()
289289
}
290290
EOF();
291291
}
292+
293+
[Test]
294+
public async Task LanguageParser_LuauTypeCast_ParsesCorrectly()
295+
{
296+
// ensure that "::" is correctly recognized as a TypeCastExpression in Luau
297+
await UsingTreeAsync("local a = {} :: table", new LuaParseOptions(LuaSyntaxOptions.Luau));
298+
await N(SyntaxKind.CompilationUnit);
299+
{
300+
await N(SyntaxKind.StatementList);
301+
{
302+
await N(SyntaxKind.LocalVariableDeclarationStatement);
303+
{
304+
await N(SyntaxKind.LocalKeyword);
305+
await N(SyntaxKind.LocalDeclarationName);
306+
{
307+
await N(SyntaxKind.IdentifierName);
308+
{
309+
await N(SyntaxKind.IdentifierToken, "a");
310+
}
311+
}
312+
await N(SyntaxKind.EqualsValuesClause);
313+
{
314+
await N(SyntaxKind.EqualsToken);
315+
await N(SyntaxKind.TypeCastExpression);
316+
{
317+
await N(SyntaxKind.TableConstructorExpression);
318+
{
319+
await N(SyntaxKind.OpenBraceToken);
320+
await N(SyntaxKind.CloseBraceToken);
321+
}
322+
await N(SyntaxKind.ColonColonToken, "::");
323+
await N(SyntaxKind.SimpleTypeName);
324+
{
325+
await N(SyntaxKind.IdentifierToken, "table");
326+
}
327+
}
328+
}
329+
}
330+
}
331+
await N(SyntaxKind.EndOfFileToken);
332+
}
333+
EOF();
334+
}
335+
[Test]
336+
public async Task LanguageParser_LuauGoto_GeneratesCorrectError()
337+
{
338+
// ensure that while "::" is enabled for type casts, using it for labels still generates the goto error in Luau
339+
await ParseAndValidateAsync(
340+
"::label::",
341+
LuaSyntaxOptions.Luau,
342+
// (1,1): error LUA1019: Goto statements and labels are not supported in this lua version
343+
Diagnostic(ErrorCode.ERR_GotoNotSupportedInLuaVersion, "::label::").WithLocation(1, 1));
344+
}
345+
[Test]
346+
public async Task LanguageParser_Lua52_TypeCast_GeneratesError()
347+
{
348+
// ensure that in Lua 5.2 (which supports goto but not types), using :: for a cast triggers the "Typed lua is not supported" error.
349+
await ParseAndValidateAsync(
350+
"local a = x :: table",
351+
LuaSyntaxOptions.Lua52,
352+
// (1,11): error LUA1016: Typed lua is not supported in this lua version
353+
Diagnostic(ErrorCode.ERR_TypedLuaNotSupportedInLuaVersion, "x :: table").WithLocation(1, 11));
354+
}
292355
}

0 commit comments

Comments
 (0)