using C4IT.F4SD.DisplayFormatting; using F4SD.DocuEngine.DocuEngineParser.Exceptions; using System.ComponentModel; using System.Linq; using System.Text.RegularExpressions; namespace F4SD.DocuEngine.DocuEngineParser.ContentBlock { internal partial class ContentBlock { private string ParseVariableType(ref int index) { bool foundTypeEnd = false; string variableType = ""; while (!foundTypeEnd) { char c = DocuTemplate.ElementAt(index); if (char.IsLetter(c)) { variableType += c; index++; } else { foundTypeEnd = true; } } return variableType; } private string ParseVariableName(ref int index) { bool foundVariableEnd = false; string variableName = ""; while (!foundVariableEnd) { char c = DocuTemplate.ElementAt(index); if (char.IsLetterOrDigit(c) || c == '.') { variableName += c; index++; } else { foundVariableEnd = true; } } return variableName; } private void SkipWhiteSpace(ref int index) { bool foundNonWhiteSpace = false; while (!foundNonWhiteSpace) { char c = DocuTemplate.ElementAt(index); if (char.IsWhiteSpace(c)) { index++; } else { foundNonWhiteSpace = true; } } } private string ParseDisplayType(ref int index) { bool foundDisplayTypeEnd = false; string displayType = ""; while (!foundDisplayTypeEnd) { char c = DocuTemplate.ElementAt(index); if (char.IsLetterOrDigit(c)) { displayType += c; index++; } else { foundDisplayTypeEnd = true; } } return displayType; } private string SubstituteVariable(string variableName, RawValueType? displayType) { IRawValueFormatter rawValueFormatter = new RawValueFormatter(); var property = Container.GetPropertyByName(variableName); if (property == null) { //Syntax Error } if (displayType != null) { return rawValueFormatter.GetDisplayValue(property.Value, (RawValueType)displayType, null); } else if (property.ValueType != null) { return rawValueFormatter.GetDisplayValue(property.Value, (RawValueType)property.ValueType, null); } else { return property.Value.ToString(); } } private char ParseComparator(ref int index) { char comparator = DocuTemplate.ElementAtOrDefault(index); if (!(comparator == '<' || comparator == '>' || comparator == '=' || comparator == '!')) { throw new SyntaxErrorException("<\" / \">\" / \"=\" / \"!", comparator.ToString()); } index++; return comparator; } private string ParseValue(ref int index) { string value = ""; string pattern = @"("")(.*?)(""\s*\))"; Regex regex = new Regex(pattern); Match match = regex.Match(DocuTemplate, index); if (match.Success) { value = match.Groups[2].Value; string wholeMatch = match.Value.ToString(); index += wholeMatch.Length; } else { throw new SyntaxErrorException("\"VariableName\")", ""); } return value; } private bool CheckCondition(string variableType, string variableName, char comparator, string value) { string stringVariable = ""; var property = Container.GetPropertyByName(variableName); if (property == null) { //Syntax Error } stringVariable = property.Value.ToString(); switch (variableType) { case "string": if (!(comparator == '=' || comparator == '!')) { throw new SyntaxErrorException("=\" / \"!", comparator.ToString()); } return CompareStrings(stringVariable, comparator, value); case "int": if (!(comparator == '<' || comparator == '>' || comparator == '=' || comparator == '!')) { throw new SyntaxErrorException("<\" / \">\" / \"=\" / \"!", comparator.ToString()); } if (!(int.TryParse(value, out var intValue))) { throw new VariableErrorException(value, "int"); } stringVariable = stringVariable.Replace("\"", ""); if (!(int.TryParse(stringVariable, out var intVariable))) { throw new VariableErrorException(stringVariable, "int"); } return CompareIntegers(intVariable, comparator, intValue); case "bool": if (!(comparator == '=' || comparator == '!')) { throw new SyntaxErrorException("=\" / \"!", comparator.ToString()); } if (!(bool.TryParse(value, out var boolValue))) { throw new VariableErrorException(value, "bool"); } if (!(bool.TryParse(stringVariable, out var boolVariable))) { throw new VariableErrorException(stringVariable, "bool"); } return CompareBooleans(boolVariable, comparator, boolValue); default: throw new SyntaxErrorException("string\" / \"int\" / \"bool", variableType); } } private bool CompareStrings(string variable, char comparator, string value) { switch (comparator) { case '=': if (variable == value) { return true; } else { return false; } case '!': if (variable != value) { return true; } else { return false; } default: return false; } } private bool CompareIntegers(int variable, char comparator, int value) { switch (comparator) { case '=': if (variable == value) { return true; } else { return false; } case '!': if (variable != value) { return true; } else { return false; } case '>': if (variable > value) { return true; } else { return false; } case '<': if (variable < value) { return true; } else { return false; } default: return false; } } private bool CompareBooleans(bool variable, char comparator, bool value) { switch (comparator) { case '=': if (variable == value) { return true; } else { return false; } case '!': if (variable != value) { return true; } else { return false; } default: return false; } } } }