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("some string", s.Token.PureToken.Value);
|
||||||
Assert.AreEqual(text, actual.FullText);
|
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);
|
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()
|
public StatementNode ParseStatementCore()
|
||||||
{
|
{
|
||||||
if (CurrentToken.Kind == TokenKind.Identifier)
|
if (CurrentToken.Kind == TokenKind.Identifier)
|
||||||
@ -864,7 +874,15 @@ namespace Parser
|
|||||||
else if (CurrentToken.PureToken.LiteralText == "for")
|
else if (CurrentToken.PureToken.LiteralText == "for")
|
||||||
{
|
{
|
||||||
return ParseForStatement();
|
return ParseForStatement();
|
||||||
}
|
}
|
||||||
|
else if (CurrentToken.PureToken.LiteralText == "try")
|
||||||
|
{
|
||||||
|
return ParseTryCatchStatement();
|
||||||
|
}
|
||||||
|
else if (CurrentToken.PureToken.LiteralText == "catch")
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
return ParseExpressionStatement();
|
return ParseExpressionStatement();
|
||||||
}
|
}
|
||||||
|
@ -595,5 +595,31 @@ namespace Parser
|
|||||||
SetParent(result);
|
SetParent(result);
|
||||||
return 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;
|
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