What is the best way to parse indent-respecting?

So, the question is in the subject.

By indent-respecting I mean YAML/Python like indentations beginning from the start of the line:

block1
  block2
      ## different indent size, 4 instead of 2, but there's no at 2+2+2 level, so its ok
      block2_line1
      block2_line2


# comments or empty lines


      block2_line3
  block3
    # same indent as from parents
    block3_line1
line1
line2
line3

I’ve done classical expressions/statements parsing, but feeling weakness in front of indent-respecting parsing.

As I understand, there’re several ways to count indents:

  • Lexer level
    • Each indent change produces a new token
      • 0 spaces → nothing
      • 2 spaces → INDENT
      • 2 spaces → nothing
      • 4 spaces → INDENT
      • 2 spaces → DEDENT
    • There’re disadvantages with parsing empty lines, indentations for comments and more
  • Parser level
    • It’s new level for me, but look like I should re-write my generated parser with it
    • OR wrap my generated parser with a new one which starts/stops blocks, counting the indents
  • Any other?

So parser-level looks solid for me, but I’m not sure it’s the best way to do it.

BTW, YamlParser is written manually, as I can understand. So should I do the same?

Anyone know a good explanation about that parser mechanism?