This is my code I need to parse into AST:
one
two
three
four
five
six
seven
It is indentation-driven, as you see. I can't really find a way to explain my parser (I'm using Antlr4) that a leading space is an indicator of a sub-level.
This is my code I need to parse into AST:
one
two
three
four
five
six
seven
It is indentation-driven, as you see. I can't really find a way to explain my parser (I'm using Antlr4) that a leading space is an indicator of a sub-level.
Basically you can't explain it to the parser without some help from the lexer.
Instead, what you do is hack the lexer to keep track of the number of spaces that start a line as it scans across the spaces. If the space count changes from the previous line, the lexer emits a token. If the count goes up, emit an INDENT token. If the count goes down, emit a DEDENT token.
Now you can add INDENT and DEDENT tokens into the parser rules. They act logically like { and } in C-like languages.