Gan
Gan

Reputation: 93

How to find the end tag line number?

I need kind favor to find the closing tag line number. For example:

<tr>
  <td>...</td>
  <td>...</td>
</tr>

<tr>
  <td>...</td>
  <td>...</td>

<tr>
  <td>...</td>
  <td>...</td>
</tr>

In that above line, the first <tr> tag is opened and closed correspondingly. In the second <tr> it is opened but not closed. Without closing the <tr> it open third <tr>. In this situation I need to show the line number as error. I can find the line number open tag. I need to find the closing tag line number. I am working in WPF C#.

Upvotes: 0

Views: 289

Answers (1)

user1693593
user1693593

Reputation:

You need to put them on "stack" while parsing. Push to your stack when having an open tag, remove in reverse order when a closing tag happen. If close-tag <> open tag type you have an error.

Count lines while parsing and when a mismatch occur print the line-number you're at. Count line numbers by counting line-feeds (char 10 for Windows/Linux, use 13 for Mac).

Compare closing tags with "/" + opening-tag

Stack (pesudo-representation to show the "mechanics"):

Parser res   Current stack
----------   -------------
Found TR  -> TR
Found TD  -> TR-TD
Found /TD -> TR (it matched last tag on stack) 
Found TD  -> TR-TD
Found /TD -> TR (it matched last tag on stack) 
Found /TR ->  (it matched last tag on stack - stack is empty, all ok) 

Found TR  -> TR
Found TD  -> TR-TD
Found /TD -> TR (it matched last tag on stack) 
Found TD  -> TR-TD
Found /TD -> TR (it matched last tag on stack) 
Found TR  -> TR (mismatch! /+TR <> TR as expected), print error + line number

Upvotes: 2

Related Questions