/// <summary> /// Decoder le résultat du traitement: la pile ne doit contenir qu'une seule expr. /// </summary> /// <param name="stack"></param> /// <param name="result"></param> /// <returns></returns> private bool AnalyzeStackContent(Stack <ExpressionBase> stack, ParseResult result) { // an error occurs if (result.HasError) { return(false); } // ok, la pile contient une et une seule expr if (stack.Count == 1) { result.RootExpr = stack.Pop(); return(true); } // cas spécial, normalement traité avant ici if (stack.Count == 0) { return(false); } // expression malformée, pb parenthèses ExprError error = new ExprError(); // todo: compter les bracket open et les end pour préciser l'erreur? error.Code = ErrorCode.BadExpressionBracketOpenCloseMismatch; error.Token = result.ListExprToken[result.ListExprToken.Count - 1]; //error.TokenPosition = error.Token.Position; result.ListError.Add(error); // sort du traitement return(false); }
/// <summary> /// Add an execution error, with an error parameter. /// </summary> /// <param name="errCode"></param> /// <param name="paramKey"></param> /// <param name="paramValue"></param> /// <returns></returns> public ExprError AddErrorExec(ErrorCode errCode, string paramKey, string paramValue) { ExprError error = new ExprError(); error.ErrorType = ErrorType.Exec; error.Code = errCode; if (string.IsNullOrEmpty(paramKey)) { paramKey = "Param1"; } if (string.IsNullOrEmpty(paramValue)) { paramKey = "(null)"; } ErrorParam errorParam = new ErrorParam(); errorParam.Key = paramKey; errorParam.Value = paramValue; error.ListErrorParam.Add(errorParam); ListError.Add(error); return(error); }
public void AddError(ExprError error) { if (error != null) { ListError.Add(error); } }
public ExprError AddError(ErrorCode errorCode, ExprToken token) { var error = new ExprError(); error.Code = errorCode; error.Token = token; ListError.Add(error); return(error); }
/// <summary> /// Add an execution error. /// </summary> /// <param name="errCode"></param> /// <returns></returns> public ExprError AddErrorExec(ErrorCode errCode) { ExprError error = new ExprError(); error.ErrorType = ErrorType.Exec; error.Code = errCode; ListError.Add(error); return(error); }
public static ExprError CreateError(ErrorCode errCode, string paramKey, string paramValue) { ExprError error = new ExprError(); error.Code = errCode; if (string.IsNullOrEmpty(paramKey)) paramKey = "Param1"; if (string.IsNullOrEmpty(paramValue)) paramKey = "(null)"; ErrorParam errorParam = new ErrorParam(); errorParam.Key = paramKey; errorParam.Value = paramValue; //error.Param = param; error.ListErrorParam.Add(errorParam); return error; }
private bool CheckObjectNameSyntax(ExprToken token, ParseResult result, ExprFinalOperand exprFinalOperand) { exprFinalOperand.ContentType = OperandType.ObjectName; // the first char mut be: a letter or an underscore if (!char.IsLetter(token.Value[0]) && token.Value[0] != '_') { // erreur! plus de token, token attendu: opérande gauche. ExprError error = new ExprError(); error.Code = ErrorCode.ObjectNameSyntaxWrong; result.ListError.Add(error); return(false); } // check others char of the objectName // ici(); exprFinalOperand.ContentType = OperandType.ObjectName; return(true); }
public ExprError AddErrorExec(ErrorCode errCode, string paramKey, string paramValue, string paramKey2, string paramValue2) { ExprError error = AddErrorExec(errCode, paramKey, paramValue); if (string.IsNullOrEmpty(paramKey2)) { paramKey = "Param2"; } if (string.IsNullOrEmpty(paramValue2)) { paramKey = "(null)"; } ErrorParam errorParam = new ErrorParam(); errorParam.Key = paramKey2; errorParam.Value = paramValue2; error.ListErrorParam.Add(errorParam); return(error); }
/// <summary> /// decode les tokens de l'expression. /// construit une structure d'objets arborescente binaire basée sur la classe BoolBinExprBase. /// /// Compact the tokens before decoding it. /// </summary> /// <param name="expr"></param> /// <param name="listTokens"></param> /// <returns></returns> public ParseResult Parse(string expr, List <ExprToken> listTokens) { ParseResult result = new ParseResult(); result.Expression = expr; result.ListExprToken.AddRange(listTokens); // err, any operator (bool and comp) is defined if (_exprEvalConfig == null) { ExprError error = new ExprError(); // its an internal error! error.Code = ErrorCode.AnyOperatorDefined; result.ListError.Add(error); return(result); } // err, the expression is null or empty if (listTokens.Count == 0) { // erreur! plus de token, token attendu: opérande gauche. ExprError error = new ExprError(); error.Code = ErrorCode.ExprIsNullOrEmpty; result.ListError.Add(error); return(result); } // adds open and close bracket to the tokens, exp A=B -> (A=B) AddBrackets(listTokens); // init stack pour gérer les parenthèse Stack <ExpressionBase> stack = new Stack <ExpressionBase>(); // parcours les tokens un à un foreach (ExprToken token in listTokens) { //----parenthèse ouvrante? empile le token if (token.Value == "(") { ExprBracketOpen bracketOpen = new ExprBracketOpen(); bracketOpen.Token = token; stack.Push(bracketOpen); continue; } //----closed bracket found? pop tokens from the stack until find the open bracket, build a sub-expression. if (token.Value == ")") { SubExprDecoder subExprDecoder = new SubExprDecoder(); if (!subExprDecoder.DecodeExpr(stack, token, result)) { break; } continue; } //----opérateur? empile le token // selon langue, recupere la liste des opérateurs et compare. if (DecodeTokenOperator(stack, token)) { continue; } //----is it function Call parameter separator? if (DecodeTokenFunctionCallParameterSeparator(stack, token)) { continue; } //----opérande? empile le token // n'est ni une parenthèse ouvrante, fermante, ni un opérateur. ExprFinalOperand exprFinalOperand = BuildOperand(token, result); stack.Push(exprFinalOperand); } // decoder le résultat du traitement: la pile ne doit contenir qu'une seule expr AnalyzeStackContent(stack, result); return(result); }
/// <summary> /// Build the final operand: not a separator. /// can be a number (positive or negative), an object name (var, fct,...) or a string. /// /// The string can be well-formed or bad-formed: the end tag is missing. /// </summary> /// <param name="token"></param> /// <returns></returns> private ExprFinalOperand BuildOperand(ExprToken token, ParseResult result) { ExprFinalOperand exprFinalOperand = new ExprFinalOperand(); exprFinalOperand.Operand = token.Value; exprFinalOperand.Token = token; //----is the value a string? starts with a string tag (quote or double-quote) if (_exprEvalConfig.StringTagValue == token.Value[0].ToString()) { // is the last char a string tag? //if (_exprEvalConfig.ListStringTag.Contains(token.Value[token.Value.Length-1]) && token.Value.Length>1) if (_exprEvalConfig.StringTagValue == token.Value[token.Value.Length - 1].ToString() && token.Value.Length > 1) { exprFinalOperand.ContentType = OperandType.ValueString; return(exprFinalOperand); } exprFinalOperand.ContentType = OperandType.ValueStringBadFormed; ExprError error = new ExprError(); error.Token = token; error.Code = ErrorCode.ValueStringBadFormed; result.ListError.Add(error); return(exprFinalOperand); } //----is the value an integer? int numberInt; if (int.TryParse(token.Value, out numberInt)) { exprFinalOperand.ContentType = OperandType.ValueInt; exprFinalOperand.ValueInt = numberInt; return(exprFinalOperand); } //----is the value a double? if (IsOperandDouble(token, result, exprFinalOperand)) { return(exprFinalOperand); } // is it a bad-formed number? (seems to be number: start with minus or a digit) if (token.Value[0] == '-' || char.IsDigit(token.Value[0])) { exprFinalOperand.ContentType = OperandType.ValueNumberBadFormed; ExprError error = new ExprError(); error.Token = token; error.Code = ErrorCode.ValueNumberBadFormed; result.ListError.Add(error); return(exprFinalOperand); } //-----is it a bool value? // todo: get values from the configurator: true/false, vrai/faux BoolConst boolConst = _exprEvalConfig.ListBoolConst.Find(bc => bc.BoolStringValue.Equals(token.Value, StringComparison.InvariantCultureIgnoreCase)); if (boolConst != null) { exprFinalOperand.ContentType = OperandType.ValueBool; exprFinalOperand.ValueBool = boolConst.BoolValue; return(exprFinalOperand); } // it's an object name, (sure?) check the syntax CheckObjectNameSyntax(token, result, exprFinalOperand); return(exprFinalOperand); }