www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to write parser?

reply Suliman <evermind live.ru> writes:
I am trying to learn how to write text parser. I have example doc 
with follow format:

#Header
my header text


my sub header text


my sub 3 text

#Header21
my header2 text


my header2 text


my header3 text



look like:

<div 1>
#Header
my header text
<div 2>

my sub header text


my sub 3 text
</div 3>
</div 2>
</div 1>

<div 1>#Header21
my header2 text


my header2 text


my header3 text
</div 3>
</div 2>
</div 1>

It's seems that I wrong understand parser logic. I am trying to 
do it's in next way:

	bool isH1Open;
	bool isH2Open;
	bool isH3Open;

	string newcontent;

	foreach(line; content.lineSplitter)
	{
		if(line.length > 3) // to prevent access to line < 3 symblos
		{

			{
				isH1Open = true;
				line = `<div 1>` ~ "\n" ~ line ;
				newcontent ~= line;
				continue;
			}



			{
				isH2Open = false;
				line = "\n" ~ `</div 2>` ~ "\n";
				newcontent ~= line;
				continue;
			}


			{
				isH1Open = false;
				line = "\n" ~ `</div 1>` ~ "\n";
				newcontent ~= line;
				continue;
			}

	

			{
				isH2Open = true;
				line = "\n" ~ `<div 2>` ~ "\n" ~ line ;
				newcontent ~= line;
				continue;
			}


		}

But I am getting wrong output:

<div 1>
#Header
<div 2>

</div 1>

</div 2>
<div 1>
#Header31
<div 2>


it's there any better way to parse such format?
May 14 2017
parent crimaniak <crimaniak gmail.com> writes:
On Sunday, 14 May 2017 at 19:00:09 UTC, Suliman wrote:
 I am trying to learn how to write text parser. I have example 
 doc with follow format:

 #Header
 my header text


 my sub header text
 it's there any better way to parse such format?
Ready markdown Pegged based parser: https://github.com/PhilippeSigaud/Pegged/blob/master/pegged/examples/markdown.d
May 15 2017