using F4SD.DocuEngine.DocuEngineParser.Exceptions; using System.ComponentModel; using System.Linq; namespace F4SD.DocuEngine.DocuEngineParser.ContentBlock { internal partial class ContentBlock { private void ParseIfCommand(ref int index) { string tempString = ""; bool foundTrueCondition = false; bool foundAllElseIf = false; int tempIndex = 0; tempString = ParseIfBlock(ref index, out var isIfConditionTrue); if (isIfConditionTrue) { foundTrueCondition = true; parsingContext.Append(tempString); } tempString = ""; while (!foundAllElseIf) { tempIndex = index; SkipWhiteSpace(ref index); if (DocuTemplate.Substring(index, 7).Equals("else-if")) { index += 7; tempString += ParseElseIfBlock(ref index, out var isElseIfConditionTrue); if (isElseIfConditionTrue && !foundTrueCondition) { foundTrueCondition = true; parsingContext.Append(tempString); } tempString = ""; } else { foundAllElseIf = true; index = tempIndex; } } tempIndex = index; SkipWhiteSpace(ref index); if (DocuTemplate.Substring(index, 4).Equals("else")) { index += 4; tempString += ParseElseBlock(ref index); if (!foundTrueCondition) { foundTrueCondition = true; parsingContext.Append(tempString); tempString = ""; } } else { index = tempIndex; } } private string ParseIfBlock(ref int index, out bool isConditionTrue) { string returnString = ""; string variableType = ""; string variableName = ""; char comparator; string value = ""; SkipWhiteSpace(ref index); variableType += ParseVariableType(ref index); SkipWhiteSpace(ref index); if (!(DocuTemplate.ElementAtOrDefault(index).Equals(';'))) { throw new SyntaxErrorException(";", DocuTemplate.ElementAtOrDefault(index).ToString()); } index++; SkipWhiteSpace(ref index); variableName += ParseVariableName(ref index); SkipWhiteSpace(ref index); if (!(DocuTemplate.ElementAtOrDefault(index).Equals(';'))) { throw new SyntaxErrorException(";", DocuTemplate.ElementAtOrDefault(index).ToString()); } index++; SkipWhiteSpace(ref index); comparator = ParseComparator(ref index); SkipWhiteSpace(ref index); if (!(DocuTemplate.ElementAtOrDefault(index).Equals(';'))) { throw new SyntaxErrorException(";", DocuTemplate.ElementAtOrDefault(index).ToString()); } index++; SkipWhiteSpace(ref index); value += ParseValue(ref index); SkipWhiteSpace(ref index); isConditionTrue = CheckCondition(variableType, variableName, comparator, value); SkipWhiteSpace(ref index); if (!DocuTemplate.Substring(index, 2).Equals("{{")) { throw new SyntaxErrorException("{{", DocuTemplate.Substring(index, 2)); } index += 2; ContentBlock contentBlock = new ContentBlock(DocuTemplate, Container); returnString += contentBlock.ParseContentBlock(ref index); return returnString; } private string ParseElseIfBlock(ref int index, out bool isConditionTrue) { string returnString = ""; string variableType = ""; string variableName = ""; char comparator; string value = ""; SkipWhiteSpace(ref index); if (!(DocuTemplate.ElementAt(index).Equals('('))) { throw new SyntaxErrorException("(", DocuTemplate.ElementAtOrDefault(index).ToString()); } index++; SkipWhiteSpace(ref index); variableType += ParseVariableType(ref index); SkipWhiteSpace(ref index); if (!(DocuTemplate.ElementAtOrDefault(index).Equals(';'))) { throw new SyntaxErrorException(";", DocuTemplate.ElementAtOrDefault(index).ToString()); } index++; SkipWhiteSpace(ref index); variableName += ParseVariableName(ref index); SkipWhiteSpace(ref index); if (!(DocuTemplate.ElementAtOrDefault(index).Equals(';'))) { throw new SyntaxErrorException(";", DocuTemplate.ElementAtOrDefault(index).ToString()); } index++; SkipWhiteSpace(ref index); comparator = ParseComparator(ref index); SkipWhiteSpace(ref index); if (!(DocuTemplate.ElementAtOrDefault(index).Equals(';'))) { throw new SyntaxErrorException(";", DocuTemplate.ElementAtOrDefault(index).ToString()); } index++; SkipWhiteSpace(ref index); value += ParseValue(ref index); SkipWhiteSpace(ref index); isConditionTrue = CheckCondition(variableType, variableName, comparator, value); SkipWhiteSpace(ref index); if (!DocuTemplate.Substring(index, 2).Equals("{{")) { throw new SyntaxErrorException("{{", DocuTemplate.Substring(index, 2)); } index += 2; ContentBlock contentBlock = new ContentBlock(DocuTemplate, Container); returnString += contentBlock.ParseContentBlock(ref index); return returnString; } private string ParseElseBlock(ref int index) { string returnString = ""; SkipWhiteSpace(ref index); if (!DocuTemplate.Substring(index, 2).Equals("{{")) { throw new SyntaxErrorException("{{", DocuTemplate.Substring(index, 2)); } index += 2; ContentBlock contentBlock = new ContentBlock(DocuTemplate, Container); returnString += contentBlock.ParseContentBlock(ref index); return returnString; } } }