badicsalex / peginator

PEG parser generator for creating ASTs in Rust
MIT License
34 stars 3 forks source link

How to make indentation? #16

Closed hamza1312 closed 1 year ago

hamza1312 commented 1 year ago

So how to make indentation that will track when it's INDENT and DEDENT and can use it easily with the grammar?

badicsalex commented 1 year ago

There is no explicit support for indentation parsing in the generator, so the best thing you could do is preparse your text, and replace the indentation with special {INDENT} and {DEDENT} strings, and then do the parsing with peginator. The algorithm itself should look something like this: https://stackoverflow.com/a/40960461

I guess newlines will be important in your grammar, so you should probably replace those with a special {NEWLINE} string too, or use custom whitespace handling (by default newlines will be dropped between tokens).

Maybe some day peginator will have out-of-box support for these, but not currently.

hamza1312 commented 1 year ago

Can you show how ii can "pre-parse" Not the entire code, just how to handle the pre parsing in peginator.

hamza1312 commented 1 year ago

Will "@extern" do the job ig?

badicsalex commented 1 year ago

To pre-parse, before giving your String to peginator, you first transform it with your own code:

let content = load_file("in.txt");
let transformed_content = detect_indent_dedent(&content); // <= You have to write this function, it's not part of peginator
let ast = GeneratedParser::parse(&transformed_content);

@extern is for calling a specific parser while parsing. It's mostly for handling special-form literals or tokens that would be hard to describe in EBNF.

hamza1312 commented 1 year ago

Alright i get it now thank you.

hamza1312 commented 1 year ago

Closed.