yhirose / cpp-peglib

A single file C++ header-only PEG (Parsing Expression Grammars) library
MIT License
880 stars 112 forks source link

Question: reverse error prioritization #235

Closed kfsone closed 2 years ago

kfsone commented 2 years ago

Given

config <- (identifier space "=" space number nl)*

I want to prioritize errors regarding "value" over errors regarding the missing space. so:

x =1

would complain about missing space, but

x = forty-two
x =forty-two

would both complain about the value.

In the second case, if it complains about both, I would like to try and produce the diagnostic:

x =forty-two
     ^^^^^^
1:4: error: syntax error, expecting <number>
1:4: error: syntax error, expecting <space> after "="

I'd prefer that order, because the real problem is the data not the style, but other-way-around is ok too.

Currently, I only appear to be able to create a non-recovering recovery that gives you the "bad value" type error:

config <- (assignment / NL)* !.

assignment <- IDENT assign_expr NL
assign_expr <- ~SPACE+ "=" assign_rhs
assign_rhs <- ~SPACE+ NO^er_invalid_rhs / (!NO)^er_missing_space

IDENT <- [a-z]+
SPACE <- ' '
NO <- [0-9]+
NL <- (SPACE* '\n')

er_invalid_rhs <- '' { message "expected a number after =" }
er_missing_space <- !' ' { message "missing space" }

with x =2 gives missing space, with x =q gives the same diagnostic as x = q (expcted number).

Questions are:

yhirose commented 2 years ago

@kfsone, thanks for the feedback.

your recommended approach for this / pointer to examples / doc, cpp-peglib reports errors in ascending order of positions. So there is no way to reverse the order.

is there something like %p for 'prior' in message? No

do error-recovery labels support precedence? No

Sorry for causing the inconvenience...