Parse try/catch statement
This commit is contained in:
parent
d78e5d671e
commit
4a2c6283c6
@ -439,5 +439,15 @@ namespace Parser.Tests
|
||||
Assert.AreEqual("some string", s.Token.PureToken.Value);
|
||||
Assert.AreEqual(text, actual.FullText);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ParseTryCatchStatement()
|
||||
{
|
||||
var text = "try a = b catch c = d end";
|
||||
var sut = CreateParser(text);
|
||||
var actual = sut.ParseStatement();
|
||||
Assert.IsInstanceOf<TryCatchStatementNode>(actual);
|
||||
Assert.AreEqual(text, actual.FullText);
|
||||
}
|
||||
}
|
||||
}
|
@ -825,6 +825,16 @@ namespace Parser
|
||||
return Factory.ForStatement(forKeyword, forAssignment, body, endKeyword, commas);
|
||||
}
|
||||
|
||||
public TryCatchStatementNode ParseTryCatchStatement()
|
||||
{
|
||||
var tryKeyword = Factory.Token(EatIdentifier("try"));
|
||||
var tryBody = ParseStatements();
|
||||
var catchKeyword = Factory.Token(EatIdentifier("catch"));
|
||||
var catchBody = ParseStatements();
|
||||
var endKeyword = Factory.Token(EatIdentifier("end"));
|
||||
return Factory.TryCatchStatement(tryKeyword, tryBody, catchKeyword, catchBody, endKeyword);
|
||||
}
|
||||
|
||||
public StatementNode ParseStatementCore()
|
||||
{
|
||||
if (CurrentToken.Kind == TokenKind.Identifier)
|
||||
@ -865,6 +875,14 @@ namespace Parser
|
||||
{
|
||||
return ParseForStatement();
|
||||
}
|
||||
else if (CurrentToken.PureToken.LiteralText == "try")
|
||||
{
|
||||
return ParseTryCatchStatement();
|
||||
}
|
||||
else if (CurrentToken.PureToken.LiteralText == "catch")
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return ParseExpressionStatement();
|
||||
}
|
||||
|
@ -595,5 +595,31 @@ namespace Parser
|
||||
SetParent(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
public TryCatchStatementNode TryCatchStatement(
|
||||
TokenNode tryKeyword,
|
||||
StatementListNode tryBody,
|
||||
TokenNode catchKeyword,
|
||||
StatementListNode catchBody,
|
||||
TokenNode endKeyword)
|
||||
{
|
||||
var children = new List<SyntaxNode>
|
||||
{
|
||||
tryKeyword,
|
||||
tryBody,
|
||||
catchKeyword,
|
||||
catchBody,
|
||||
endKeyword
|
||||
};
|
||||
var result = new TryCatchStatementNode(
|
||||
children,
|
||||
tryKeyword,
|
||||
tryBody,
|
||||
catchKeyword,
|
||||
catchBody,
|
||||
endKeyword);
|
||||
SetParent(result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
@ -658,4 +658,29 @@ namespace Parser
|
||||
Body = body;
|
||||
}
|
||||
}
|
||||
|
||||
public class TryCatchStatementNode : StatementNode
|
||||
{
|
||||
public TokenNode TryKeyword { get; }
|
||||
public StatementListNode TryBody { get; }
|
||||
public TokenNode CatchKeyword { get; }
|
||||
public StatementListNode CatchBody { get; }
|
||||
public TokenNode EndKeyword { get; }
|
||||
|
||||
public TryCatchStatementNode(
|
||||
List<SyntaxNode> children,
|
||||
TokenNode tryKeyword,
|
||||
StatementListNode tryBody,
|
||||
TokenNode catchKeyword,
|
||||
StatementListNode catchBody,
|
||||
TokenNode endKeyword
|
||||
) : base(children)
|
||||
{
|
||||
TryKeyword = tryKeyword;
|
||||
TryBody = tryBody;
|
||||
CatchKeyword = catchKeyword;
|
||||
CatchBody = catchBody;
|
||||
EndKeyword = endKeyword;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user