Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I am making one custom markup language.For example:
<NAME1>, <NAME2> or <FAM>.
But only tags won't do me work, so I come to the conclusion I need if statement to make some checkings myself.

Example
<IF NAME1="George"><br />
<DoStuff><br />
<ENDIF>


Can you give me some ideas or guidance how to make the custom if statement?

EDIT

I don't think I get the question.

To get the idea.Lets say we have it done, and here is example how it will execute:
<minding your="" business="">
<br />
<IF NAME1="George"><br />
<DoStuff><br />
<DoOtherStuff><br />
<ENDIF><br />
<Minding Your Other Business><br />


if its true the executed will be:
<br />
<Minding Your Business><br />
<br />
<DoStuff><br />
<DoOtherStuff><br />
<br />
<Minding Your Other Business><br />


if - false:
<br />
<Minding Your Business><br />
<Minding Your Other Business><br />
Posted
Updated 3-Nov-14 0:06am
v3
Comments
Philippe Mori 1-Nov-14 22:41pm    
Do you really need a custom language. Won't XML or ASP.NET pages won't fit you situation?
Zhivko Kabaivanov 3-Nov-14 5:49am    
No, this is custom markup script that includes tags like above and tags representing Keys ({A},{TAB} end etc.).

Also its desktop application.
BillWoodruff 2-Nov-14 2:17am    
Mark-up of what: HTML ? Plain Text ?

If you must create a new "language:" consider doing some research on DSL (domain specific languages) in .NET.
Zhivko Kabaivanov 3-Nov-14 5:50am    
It is already made.

The algorithm is simple - if cursor find tag <name1> then replace it with George.
Richard MacCutchan 2-Nov-14 4:26am    
What sort of guidance? What you have written above looks OK as an IF statement.

I am not really sure about the objective, but I won't recommend to come up with an new markup altogether. Instead, look at how you can use the Xml to structure your logic. For e.g. you can make use of attributes to specify the condition:

HTML
<dostuff when="Name1 is George" /> 


What you can also do is have the attribute value defined in any programming language of your choice so that the parser does not have to be rewritten and it can already evaluate the condition.
 
Share this answer
 
I managed to make a method for doing what I want. So here I am sharing it if someone wants to edit, advice or etc. Please advise me.

C#
public static string ReplaceIfStatement(string script, CResultData resultData)
{
	string workingScript = script;
	string manipulatedScript = workingScript.Clone() as string; // storing the script in temp variable for further checks

	workingScript = CStringFunctions.ReplaceAll(workingScript, "\r", string.Empty); // ReplaceAll uses the string.Replace() method
	workingScript = CStringFunctions.ReplaceAll(workingScript, "\t", string.Empty);

	string ifStartFieldName = "<if hold=" />	string ifEndFieldName = "><endif>";

	workingScript = CStringFunctions.ReplaceAll(workingScript, "<if\n",>
	bool repeat = true;

	while (repeat)
	{
		int ifStartIndexOpenTag = workingScript.IndexOf(ifStartFieldName);
		int ifEndIndexOpenTag = workingScript.IndexOf(ifEndFieldName);

		if (ifStartIndexOpenTag != -1 
			|| ifEndIndexOpenTag != -1)
		{
			int ifStartIndexCloseTag = CStringFunctions.IndexOfString(
				workingScript,
				">",
				ifStartIndexOpenTag + ifStartFieldName.Length + 1); //IndexOfString() uses the IndexOf()

			int ifEndIndexCloseTag = CStringFunctions.IndexOfString(
				workingScript,
				">",
				ifEndIndexOpenTag + ifStartFieldName.Length + 1);

			if (ifStartIndexCloseTag != -1)
			{
				string ifStatement = CStringFunctions.SubstrString(
					workingScript,
					ifStartIndexOpenTag + ifStartFieldName.Length,
					ifStartIndexCloseTag - (ifStartIndexOpenTag + ifStartFieldName.Length));

				ifStatement = CStringFunctions.ReplaceAll(ifStatement, " ", string.Empty);

				string rightSideIfStatement = CStringFunctions.SubstrString(
					ifStatement,
					1,
					CStringFunctions.IndexOfString(ifStatement, "=") - 1);

				string leftSideIfStatement = CStringFunctions.SubstrString(
					ifStatement,
					CStringFunctions.IndexOfString(ifStatement, "=") + 1,
					CStringFunctions.IndexOfString(ifStatement, ")")
					- CStringFunctions.IndexOfString(ifStatement, "=") - 1);

				CResultData ifResultData = resultData.Clone(); // here is the class containing all props, ex: NAME1, NAME2 or FAM

				foreach (PropertyInfo property in ifResultData.GetType().GetProperties())
				{
					if (rightSideIfStatement == property.Name.ToUpper()
						&& leftSideIfStatement == (string)property.GetValue(ifResultData, null)) // In this statement compare the property name in the <if> and the <if> 
					{
						//if the compare is true i remove the <if> and the <endif> from the script
						manipulatedScript = CStringFunctions.ReplaceStringAtPosition(
							workingScript,
							ifStartIndexOpenTag,
							ifStartIndexCloseTag + 1,
							string.Empty);

						ifEndIndexOpenTag = CStringFunctions.IndexOfString(manipulatedScript, ifEndFieldName);
						ifEndIndexCloseTag = CStringFunctions.IndexOfString(
							manipulatedScript,
							">",
							ifEndIndexOpenTag + ifStartFieldName.Length + 1);

						manipulatedScript = CStringFunctions.ReplaceStringAtPosition(
							manipulatedScript,
							ifEndIndexOpenTag,
							ifEndIndexCloseTag + 1,
							string.Empty);

						workingScript = manipulatedScript;

						return workingScript;
					}
				}

				if (manipulatedScript != null && manipulatedScript.Length == workingScript.Length) // When the IF statement is false
				//here I compare the parameter script with the temp script if there are changes to remove from the script begging from <if> to <endif>
				{
					manipulatedScript = CStringFunctions.ReplaceStringAtPosition(
						workingScript,
						ifStartIndexOpenTag,
						ifEndIndexCloseTag + 1,
						string.Empty);

					workingScript = manipulatedScript;

					return workingScript;
				}
			}
			else
			{
				repeat = false;
			}
		}
		else
		{
			repeat = false;
		}
	}

	workingScript = manipulatedScript;

	return workingScript;
}</endif></if></endif></if></if></if></endif></if>
 
Share this answer
 
Comments
BillWoodruff 3-Nov-14 1:09am    
Answering the questions asked in the comments on your question can lead to specific feedback ... often.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900