# What is the best way to parse indent-respecting?

**URL:** https://platform.jetbrains.com/t/what-is-the-best-way-to-parse-indent-respecting/3284
**Category:** IntelliJ Platform
**Created:** [December 7, 2025, 5:26pm UTC](https://platform.jetbrains.com/t/what-is-the-best-way-to-parse-indent-respecting/3284 "2025-12-07T17:26:57Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![xepozz](https://sea1.discourse-cdn.com/flex001/user_avatar/platform.jetbrains.com/xepozz/32/589_2.png) [@xepozz](https://platform.jetbrains.com/u/xepozz)
#### Post date: [December 7, 2025, 5:26pm UTC](https://platform.jetbrains.com/t/what-is-the-best-way-to-parse-indent-respecting/3284/1 "2025-12-07T17:26:57Z")

</div>

So, the question is in the subject.

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

```auto
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?
