badicsalex / peginator

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

Generalized position #6

Closed oovm closed 2 years ago

oovm commented 2 years ago

How about support @position for all rule?


The current string char and enum cannot mark positions.

For enum, just call position directly on all variants, the rust compiler will prompt which variants do not mark the position and report an error.

For string/char, position is also necessary, I want to report which escaped char fails and which identifier is undefined with position.

The hacking I am currently using is

@position
Identifier = identifier:IDENTIFIER;

@string
@no_skip_ws
IDENTIFIER = (XID_START | '_') {XID_CONTINUE};

But I think I can generate the Identifier directly

@position
@string
@no_skip_ws
Identifier  = (XID_START | '_') {XID_CONTINUE};

Leads to

pub struct Identifier {
    pub string: String,
    pub position: Range<usize>
}
badicsalex commented 2 years ago

Sounds like a good idea, I'll see what I can do.

(Except for @char, those rules are meant to be special and very lightweight)