Parse elseif

This commit is contained in:
Alexander Luzgarev 2018-04-05 17:58:33 +02:00
parent 94c2d75ad8
commit 5e8a1a0abe
3 changed files with 44 additions and 2 deletions

View File

@ -449,6 +449,21 @@ namespace Parser.Tests
Assert.IsInstanceOf<TryCatchStatementNode>(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<IfStatementNode>(actual);
Assert.AreEqual(text, actual.FullText);
}
}
}

View File

@ -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<SyntaxNode> { 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;

View File

@ -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,