/// <summary> /// Parses a Angular comment directive /// </summary> /// <param name="commentText">Comment text</param> /// <param name="directiveNameHandler">Directive name handler</param> /// <param name="expressionHandler">Binding expression handler</param> public static void ParseCommentDirective(string commentText, DirectiveNameDelegate directiveNameHandler, ExpressionDelegate expressionHandler) { Match match = _ngCommentDirectiveRegex.Match(commentText); if (match.Success) { var innerContext = new InnerMarkupParsingContext(commentText); var context = new MarkupParsingContext(innerContext); GroupCollection groups = match.Groups; Group directiveNameGroup = groups["directiveName"]; int directiveNamePosition = directiveNameGroup.Index; string originalDirectiveName = directiveNameGroup.Value; string normalizedDirectiveName = NormalizeDirectiveName(originalDirectiveName); innerContext.IncreasePosition(directiveNamePosition); directiveNameHandler?.Invoke(context, originalDirectiveName, normalizedDirectiveName); Group expressionGroup = groups["expression"]; if (expressionGroup.Success) { int expressionPosition = expressionGroup.Index; string expression = expressionGroup.Value.Trim(); innerContext.IncreasePosition(expressionPosition - directiveNamePosition); expressionHandler?.Invoke(context, expression); } } }
public float GetFitness() { if (time == 0 && distance == 0) { distance = float.Epsilon; } double[] inputs = CreateInputArray(); float fitness = (float)fitnessDelegate.Invoke(inputs); return(Mathf.Clamp(fitness, 0, Mathf.Infinity)); }
/// <summary> /// Parses a Angular class directive /// </summary> /// <param name="className">Class name</param> /// <param name="directiveNameHandler">Directive name handler</param> /// <param name="expressionHandler">Binding expression handler</param> /// <param name="semicolonHandler">Semicolon handler</param> public static void ParseClassDirective(string className, DirectiveNameDelegate directiveNameHandler, ExpressionDelegate expressionHandler, SemicolonDelegate semicolonHandler) { MatchCollection matches = _ngClassDirectiveRegex.Matches(className); int matchCount = matches.Count; if (matchCount > 0) { var innerContext = new InnerMarkupParsingContext(className); var context = new MarkupParsingContext(innerContext); int currentPosition = 0; for (int matchIndex = 0; matchIndex < matchCount; matchIndex++) { Match match = matches[matchIndex]; GroupCollection groups = match.Groups; Group directiveNameGroup = groups["directiveName"]; int directiveNamePosition = directiveNameGroup.Index; string originalDirectiveName = directiveNameGroup.Value; string normalizedDirectiveName = NormalizeDirectiveName(originalDirectiveName); innerContext.IncreasePosition(directiveNamePosition - currentPosition); currentPosition = directiveNamePosition; directiveNameHandler?.Invoke(context, originalDirectiveName, normalizedDirectiveName); Group expressionGroup = groups["expression"]; if (expressionGroup.Success) { int expressionPosition = expressionGroup.Index; string expression = expressionGroup.Value.Trim(); innerContext.IncreasePosition(expressionPosition - currentPosition); currentPosition = expressionPosition; expressionHandler?.Invoke(context, expression); } Group semicolonGroup = groups["semicolon"]; if (semicolonGroup.Success) { int semicolonPosition = semicolonGroup.Index; innerContext.IncreasePosition(semicolonPosition - currentPosition); currentPosition = semicolonPosition; semicolonHandler?.Invoke(context); } } } }
/// <summary> /// Parses a Knockout begin containerless comment /// </summary> /// <param name="commentText">Comment text</param> /// <param name="expressionHandler">Binding expression handler</param> public static void ParseBeginContainerlessComment(string commentText, ExpressionDelegate expressionHandler) { Match match = _koBeginContainerlessCommentRegex.Match(commentText); if (match.Success) { var innerContext = new InnerMarkupParsingContext(commentText); var context = new MarkupParsingContext(innerContext); Group expressionGroup = match.Groups["expression"]; int expressionPosition = expressionGroup.Index; string expression = expressionGroup.Value.TrimEnd(null); innerContext.IncreasePosition(expressionPosition); expressionHandler?.Invoke(context, expression); } }
//int debug_iter = 0; //Attempts to parse an expression. Sets invalidity flags and updates the status display //fieldNumber: X=1, Y=2, Z=3 public void ValidateExpression(int fieldNumber) { string expr; //Set the field we're looking at switch (fieldNumber) { case 1: expr = expressionXInput.text; //Debug.Log("Validation Field number" + fieldNumber + ". X equation input='" + expr + "'"); break; case 2: expr = expressionYInput.text; //Debug.Log("Validation Field number" + fieldNumber + ". Y equation input='" + expr + "'"); break; case 3: expr = expressionZInput.text; //Debug.Log("Validation Field number" + fieldNumber + ". Z equation input='" + expr + "'"); break; default: //shouldn't happen //Debug.Log("fieldNumber using default."); expr = expressionXInput.text; break; } //debug_iter += 1; //Debug.Log("Validating: Current expr at (" + debug_iter + "): <" + expr + ">"); //Validate try { //First check for empty expression if (expr == "") { //Debug.Log("Empty string detected. Current expr at (" + debug_iter + "): <" + expr + ">"); throw new EmptyExpressionException("Please enter all equations."); } ExpressionParser parser = new ExpressionParser(); //First, read in the user parameters as constants and give it test values int i = 1; foreach (KeyValuePair <string, double> p in parameters) { //each test value should be unique and nonzero to avoid divide by zero parser.AddConst(p.Key, () => 0.00013 * (i * 17)); i++; } //Next, try to evaluate the expression Expression tempExpr = parser.EvaluateExpression(expr); //If no ParseException was thrown, expression parsed correctly //This is where I'd check for custom functions, if that was handled. //Now let's check the parameters of the Expression foreach (KeyValuePair <string, Parameter> p in tempExpr.Parameters) { if (!(variablesAvailable.Contains(p.Key))) { throw new UnknownIdentiferException("Unknown identifier '" + p.Key + "'."); } } //Now for an invocation test: if (order == 1) { ExpressionDelegate deleg = tempExpr.ToDelegate("x", "y", "z", "t"); double[] values = { 0.125778, 0.13456928, 0.2944782, 0.41698793 }; deleg.Invoke(values); } else //order == 2 { ExpressionDelegate deleg = tempExpr.ToDelegate("x", "x'", "y", "y'", "z", "z'", "t"); double[] values = { 0.155926, 0.13562489, 0.2990285, 0.412947352, 1.8308463, 1.0329475, 2.7386352 }; deleg.Invoke(values); } //If no exception, then expression is validated. //Debug.Log("Expression validated: fieldNumber=" + fieldNumber); valid[(fieldNumber - 1)] = true; //All good in this expression. Check the other invalid flags CheckInvalidFlags(); UpdateStatusDisplay(validationStatusDisplay.text); } catch (ExpressionParser.ParseException ex) { //validation unsuccessful due to parsing error valid[(fieldNumber - 1)] = false; UpdateStatusDisplay(ex.Message); } catch (UnknownIdentiferException ex) { //validation unsuccessful due to unknown identifier valid[(fieldNumber - 1)] = false; UpdateStatusDisplay(ex.Message); } catch (EmptyExpressionException ex) { //validation unsuccessful due to empty expression valid[(fieldNumber - 1)] = false; UpdateStatusDisplay(ex.Message); } catch (Exception ex) { //validation unsuccessful on invocation test valid[(fieldNumber - 1)] = false; UpdateStatusDisplay("Unknown error. Check inputs."); Debug.Log(ex.Message); } }