diff --git a/Parser.Tests/MParserShould.cs b/Parser.Tests/MParserShould.cs index 736ae11..64502b1 100644 --- a/Parser.Tests/MParserShould.cs +++ b/Parser.Tests/MParserShould.cs @@ -449,6 +449,21 @@ namespace Parser.Tests Assert.IsInstanceOf(actual); Assert.AreEqual(text, actual.FullText); } - + + [Test] + public void ParseElseif() + { + var text = @"if a == 1 + f() + elseif a == 2 + g() + elseif a == 3 + h() + end"; + var sut = CreateParser(text); + var actual = sut.ParseStatement(); + Assert.IsInstanceOf(actual); + Assert.AreEqual(text, actual.FullText); + } } } \ No newline at end of file diff --git a/Parser/MParser.cs b/Parser/MParser.cs index 2521a32..ed225ae 100644 --- a/Parser/MParser.cs +++ b/Parser/MParser.cs @@ -811,6 +811,21 @@ namespace Parser elseKeyword = Factory.Token(EatToken()); elseBody = ParseStatements(); } + if (CurrentToken.Kind == TokenKind.Identifier + && CurrentToken.PureToken.LiteralText == "elseif") + { + elseKeyword = null; + var ifStatement = ParseIfStatement(); + elseBody = Factory.StatementList(new List { ifStatement }); + return Factory.IfStatement( + ifKeyword, + condition, + body, + null, + elseBody, + null, + commas); + } var endKeyword = Factory.Token(EatIdentifier("end")); return Factory.IfStatement( @@ -903,6 +918,10 @@ namespace Parser { return null; } + else if (CurrentToken.PureToken.LiteralText == "elseif") + { + return null; + } else if (CurrentToken.PureToken.LiteralText == "end") { return null; diff --git a/Parser/SyntaxFactory.cs b/Parser/SyntaxFactory.cs index c494a05..c3cbf8f 100644 --- a/Parser/SyntaxFactory.cs +++ b/Parser/SyntaxFactory.cs @@ -483,10 +483,18 @@ namespace Parser if (elseKeyword != null) { children.Add(elseKeyword); + } + + if (elseBody != null) + { children.Add(elseBody); } - children.Add(endKeyword); + if (endKeyword != null) + { + children.Add(endKeyword); + } + var result = new IfStatementNode( children, ifKeyword,