From 6702666738fb88127ddb69fad44cd6d873483409 Mon Sep 17 00:00:00 2001 From: wixoaGit Date: Wed, 20 Dec 2023 16:15:19 -0500 Subject: [PATCH] Address reviews, other minor fixes --- DMCompiler/Compiler/DM/DMParser.cs | 38 ++++++++++++------------ DMCompiler/Compiler/DM/DMParserHelper.cs | 4 +-- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/DMCompiler/Compiler/DM/DMParser.cs b/DMCompiler/Compiler/DM/DMParser.cs index 4d18ab8afd..d9c2554a89 100644 --- a/DMCompiler/Compiler/DM/DMParser.cs +++ b/DMCompiler/Compiler/DM/DMParser.cs @@ -8,14 +8,9 @@ namespace DMCompiler.Compiler.DM { public partial class DMParser : Parser { - private DreamPath _currentPath = DreamPath.Root; - private bool _allowVarDeclExpression = false; - public DMParser(DMLexer lexer) : base(lexer) { - } - private static readonly TokenType[] AssignTypes = { TokenType.DM_Equals, TokenType.DM_PlusEquals, @@ -149,6 +144,9 @@ public DMParser(DMLexer lexer) : base(lexer) { TokenType.DM_XorEquals, }; + public DMParser(DMLexer lexer) : base(lexer) { + } + public DMASTFile File() { var loc = Current().Location; List statements = new(); @@ -1352,7 +1350,7 @@ public DMASTProcStatementSwitch.SwitchCase[] SwitchInner() { List switchCases = new(); DMASTProcStatementSwitch.SwitchCase? switchCase = SwitchCase(); - while(switchCase is not null) { + while (switchCase is not null) { switchCases.Add(switchCase); Newline(); Whitespace(); @@ -1374,19 +1372,20 @@ public DMASTProcStatementSwitch.SwitchCase[] SwitchInner() { DMASTExpression? expression = Expression(); if (expression == null) { - if (expressions.Count == 0) { - Error(WarningCode.BadExpression, "Expected an expression"); - } else { - //Eat a trailing comma if there's at least 1 expression - break; - } + if (expressions.Count == 0) + DMCompiler.Emit(WarningCode.BadExpression, Current().Location, "Expected an expression"); + + break; } if (Check(TokenType.DM_To)) { - var loc = Current().Location; Whitespace(); + var loc = Current().Location; DMASTExpression? rangeEnd = Expression(); - if (rangeEnd == null) Error(WarningCode.BadExpression, "Expected an upper limit"); + if (rangeEnd == null) { + DMCompiler.Emit(WarningCode.BadExpression, loc, "Expected an upper limit"); + rangeEnd = new DMASTConstantNull(loc); // Fallback to null + } expressions.Add(new DMASTSwitchCaseRange(loc, expression, rangeEnd)); } else { @@ -1402,24 +1401,25 @@ public DMASTProcStatementSwitch.SwitchCase[] SwitchInner() { if (body == null) { DMASTProcStatement? statement = ProcStatement(); - var loc = Current().Location; if (statement != null) { - body = new DMASTProcBlockInner(loc,statement); + body = new DMASTProcBlockInner(statement.Location, statement); } else { - body = new DMASTProcBlockInner(loc); + body = new DMASTProcBlockInner(Current().Location); } } return new DMASTProcStatementSwitch.SwitchCaseValues(expressions.ToArray(), body); } else if (Check(TokenType.DM_Else)) { - var loc = Current().Location; Whitespace(); + var loc = Current().Location; if (Current().Type == TokenType.DM_If) { //From now on, all ifs/elseifs/elses are actually part of this if's chain, not the switch's. //Ambiguous, but that is parity behaviour. Ergo, the following emission. - Error(WarningCode.SuspiciousSwitchCase, "Expected \"if\" or \"else\" - \"else if\" is ambiguous as a switch case and may cause unintended flow"); + DMCompiler.Emit(WarningCode.SuspiciousSwitchCase, loc, + "Expected \"if\" or \"else\" - \"else if\" is ambiguous as a switch case and may cause unintended flow"); } + DMASTProcBlockInner? body = ProcBlock(); if (body == null) { diff --git a/DMCompiler/Compiler/DM/DMParserHelper.cs b/DMCompiler/Compiler/DM/DMParserHelper.cs index dad8c17798..9acef9c83e 100644 --- a/DMCompiler/Compiler/DM/DMParserHelper.cs +++ b/DMCompiler/Compiler/DM/DMParserHelper.cs @@ -1,5 +1,4 @@ using OpenDreamShared.Compiler; -using DMCompiler.Compiler.DMPreprocessor; using System; using System.Collections.Generic; using System.Linq; @@ -7,8 +6,7 @@ using DMCompiler.Bytecode; namespace DMCompiler.Compiler.DM { - public partial class DMParser : Parser { - + public partial class DMParser { /// /// A special override of Error() since, for DMParser, we know we are in a compilation context and can make use of error codes. ///