Skip to content

Commit 01c7c51

Browse files
committed
try-catch in progress
1 parent 2c6410e commit 01c7c51

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ public class ExpressionEvaluator
5454
private static readonly Regex newLineCharsRegex = new Regex(@"\r\n|\r|\n");
5555

5656
// For script only
57-
private static readonly Regex blockKeywordsBeginningRegex = new Regex(@"^\s*(?<keyword>while|for|foreach|if|else\s+if)\s*[(]", RegexOptions.IgnoreCase);
57+
private static readonly Regex blockKeywordsBeginningRegex = new Regex(@"^\s*(?<keyword>while|for|foreach|if|else\s+if|catch)\s*[(]", RegexOptions.IgnoreCase);
5858
private static readonly Regex foreachParenthisEvaluationRegex = new Regex(@"^\s*(?<variableName>[a-zA-Z_][a-zA-Z0-9_]*)\s+(?<in>in)\s+(?<collection>.*)", RegexOptions.IgnoreCase);
59-
private static readonly Regex blockKeywordsWithoutParenthesesBeginningRegex = new Regex(@"^\s*(?<keyword>else|do)(?![a-zA-Z0-9_])", RegexOptions.IgnoreCase);
59+
private static readonly Regex blockKeywordsWithoutParenthesesBeginningRegex = new Regex(@"^\s*(?<keyword>else|do|try|finally)(?![a-zA-Z0-9_])", RegexOptions.IgnoreCase);
6060
private static readonly Regex blockBeginningRegex = new Regex(@"^\s*[{]");
6161
private static readonly Regex returnKeywordRegex = new Regex(@"^return(\s+|\()", RegexOptions.IgnoreCase | RegexOptions.Singleline);
6262
private static readonly Regex nextIsEndOfExpressionRegex = new Regex(@"^\s*[;]");
@@ -102,6 +102,13 @@ private enum IfBlockEvaluatedState
102102
ElseIf
103103
}
104104

105+
private enum TryBlockEvaluatedState
106+
{
107+
NoBlockEvaluated,
108+
Try,
109+
Catch
110+
}
111+
105112
#endregion
106113

107114
#region Dictionaries declarations (Primary types, number suffix, escaped chars, operators management, default vars and functions)
@@ -718,6 +725,7 @@ private object ScriptEvaluate(string script, ref bool valueReturned, ref bool br
718725
bool isContinue = continueCalled;
719726
int startOfExpression = 0;
720727
IfBlockEvaluatedState ifBlockEvaluatedState = IfBlockEvaluatedState.NoBlockEvaluated;
728+
TryBlockEvaluatedState tryBlockEvaluatedState = TryBlockEvaluatedState.NoBlockEvaluated;
721729
List<List<string>> ifElseStatementsList = new List<List<string>>();
722730

723731
object ManageJumpStatementsOrExpressionEval(string expression)
@@ -808,14 +816,14 @@ void ExecuteIfList()
808816
while (!isReturn && !isBreak && !isContinue && i < script.Length)
809817
{
810818
Match blockKeywordsBeginingMatch = null;
811-
Match elseBlockKeywordsBeginingMatch = null;
819+
Match blockKeywordsWithoutParenthesesBeginningMatch = null;
812820

813821
if (script.Substring(startOfExpression, i - startOfExpression).Trim().Equals(string.Empty)
814822
&& ((blockKeywordsBeginingMatch = blockKeywordsBeginningRegex.Match(script.Substring(i))).Success
815-
|| (elseBlockKeywordsBeginingMatch = blockKeywordsWithoutParenthesesBeginningRegex.Match(script.Substring(i))).Success))
823+
|| (blockKeywordsWithoutParenthesesBeginningMatch = blockKeywordsWithoutParenthesesBeginningRegex.Match(script.Substring(i))).Success))
816824
{
817-
i += blockKeywordsBeginingMatch.Success ? blockKeywordsBeginingMatch.Length : elseBlockKeywordsBeginingMatch.Length;
818-
string keyword = blockKeywordsBeginingMatch.Success ? blockKeywordsBeginingMatch.Groups["keyword"].Value.Replace(" ", "") : (elseBlockKeywordsBeginingMatch?.Groups["keyword"].Value ?? string.Empty);
825+
i += blockKeywordsBeginingMatch.Success ? blockKeywordsBeginingMatch.Length : blockKeywordsWithoutParenthesesBeginningMatch.Length;
826+
string keyword = blockKeywordsBeginingMatch.Success ? blockKeywordsBeginingMatch.Groups["keyword"].Value.Replace(" ", "") : (blockKeywordsWithoutParenthesesBeginningMatch?.Groups["keyword"].Value ?? string.Empty);
819827
List<string> keywordAttributes = blockKeywordsBeginingMatch.Success ? GetExpressionsBetweenParenthis(script, ref i, true, ";") : null;
820828

821829
if (blockKeywordsBeginingMatch.Success)

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -602,9 +602,10 @@ Currently the following script keywords are supported
602602
|Jump|[continue](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/continue)|Supported in do, for and while blocks|
603603
|Jump|[goto](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/goto)|Not supported (But if you looked after it -> Booo !!! Bad code)|
604604
|Jump|[return](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/return)|Supported|
605-
|Jump|[yield](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/yield)|Not supported|
606-
607-
Exception handling keywords are not yet supported
605+
|Jump/Exception|[throw](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/throw)|Not supported|
606+
|Exception Handling|[try-catch](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/try-catch)|Not supported|
607+
|Exception Handling|[try-finally](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/try-finally)|Not supported|
608+
|Exception Handling|[try-catch-finally](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/try-catch-finally)|Not supported|
608609
609610
*Remark : The way ScriptEvaluate works is to evaluate expressions one by one. There is no syntax check before the evaluation. So be aware that syntax or naming bugs only appears in execution and some code can already be evaluated at this time. Futhermore a syntaxic/naming bug in an if-else block for example can simply be ignored until the corresponding condition is met to evaluate the specific line of code.*
610611

0 commit comments

Comments
 (0)