public static void ParseFunctionDescriptorUsingFlexpressionBricksAndProvideRegEx(string expression, int performanceTestExecutionsCount = 1, ParsingOptions options = ParsingOptions.Cache) { var name = (FXB.Letter + (FXB.Digit | FXB.Letter).ZeroOrMore()).DefinesSealedRule("Name", "String"); var intNumber = FXB.Digit.OneOrMore().DefinesSealedRule("Integer", "Integer"); var floatNumber = (intNumber + "." + intNumber).DefinesSealedRule("Float", "Float"); var functionArg = (floatNumber | intNumber | name).DefinesSealedRule("FunctionArg"); var functionArgList = (functionArg & ("," & functionArg).ZeroOrMore()).DefinesRule("FunctionArgList"); var functionInvocation = (name & "(" & ~functionArgList & ")" & ';').DefinesSealedRule("FunctionInvocation"); var ctx = RegularExpressionBuildingContext.CreateStandard(); var regularExpressionString = functionInvocation.ToRegularExpressionString(ctx); var regex = new Regex(regularExpressionString, RegexOptions.Compiled); var res = regex.IsMatch(expression); var source = new StringSourceCode(new ParsingContextStreamFactory(new ProductFactory(), FXB.DefaultAlgebra), new SourceCodeFragmentFactory(), expression); for (var idx = 0; idx < performanceTestExecutionsCount; ++idx) { var rootNode = functionInvocation.TryParse(source, options); if (rootNode == null) { throw new InvalidOperationException($"Parsing error {source.GetError()}"); } } }
public void ON_ToRegularExpressionString_WHEN_FunctionInvocationRegularExpression_SHOULD_ReturnRegexThatParsesGivenExpression(string expression, bool expressionParsed) { var name = (SX.Letter + (SX.Digit | SX.Letter).ZeroOrMore()).DefinesRule("Name"); var intNumber = SX.Digit.OneOrMore().DefinesRule("Integer"); var floatNumber = (intNumber + "." + intNumber).DefinesRule("Float"); var functionArg = (floatNumber | intNumber | name).DefinesRule("FunctionArg"); var functionArgList = (functionArg & ("," & functionArg).ZeroOrMore()).DefinesRule("FunctionArgList"); var functionInvocation = (name & "(" & ~functionArgList & ")" & ';').DefinesRule("FunctionInvocation"); var ctx = RegularExpressionBuildingContext.CreateStandard(); var regularExpressionString = functionInvocation.ToRegularExpressionString(ctx); var regex = new Regex(regularExpressionString, RegexOptions.Compiled); var res = regex.IsMatch(expression); Assert.AreEqual(expressionParsed, res); var source = new StringSourceCode(new ParsingContextStreamFactory(), new SourceCodeFragmentFactory(), new ProductFactory(), expression); var rootNode = functionInvocation.TryParse(source.GetFurtherContext()); if (expressionParsed) { rootNode.Should().NotBeNull(); } else { rootNode.Should().BeNull(); } // * anything in between // + nothing in between // & optional white space in between // ^ white space in between // - positive lookahead // Parsing // Syntactic validation // Tree build // Semantic validation // Compilation }
public static void ParseMathExpressionWithFlexpressionBricks(string expression, int performanceTestExecutionsCount = 1, ParsingOptions options = ParsingOptions.Cache) { Console.WriteLine(); Console.WriteLine("Math expression demo using Flexpression Bricks"); Console.WriteLine($"Expression: {expression}"); var fx = new StandardFlexpressionAlgebra(-1); var intNumber = fx.Digit.OneOrMore().DefinesSealedRule("Integer", "Integer"); var floatNumber = (intNumber + "." + intNumber).DefinesSealedRule("Float", "Float"); var number = (intNumber | floatNumber).DefinesRule("Number"); var sumOper = FXB.ToCharacter('+'); var minOper = FXB.ToCharacter('-'); var mulOper = FXB.ToCharacter('*'); var divOper = FXB.ToCharacter('/'); var oper = (sumOper | minOper | mulOper | divOper).DefinesSealedRule("Operator"); var exprNoOperation = ("BracketExpression".Rule() | number).DefinesRule("ExpressionNoOperation"); var operation = exprNoOperation & oper & "Expression".Rule(); var expr = (operation | exprNoOperation).DefinesRule("Expression"); (FXB.ToCharacter('(') & expr & FXB.ToCharacter(')')).DefinesSealedRule("BracketExpression"); var ctx = RegularExpressionBuildingContext.CreateStandard(); var regularExpressionString = expr.ToRegularExpressionString(ctx); Console.WriteLine($"Regex: {regularExpressionString}"); var source = new StringSourceCode(new ParsingContextStreamFactory(new ProductFactory(), fx), new SourceCodeFragmentFactory(), expression); for (var idx = 0; idx < performanceTestExecutionsCount; ++idx) { var rootNode = expr.TryParse(source.GetFurtherContext(), options); } }