mna / pigeon

Command pigeon generates parsers in Go from a PEG grammar.
BSD 3-Clause "New" or "Revised" License
835 stars 66 forks source link

[Feature Request] Add Expression name to wanted when parsing error occured #69

Open F4ncyMooN opened 6 years ago

F4ncyMooN commented 6 years ago

When I use pigeon, parsing error information is very important to me.

Metric "METRIC" = left:ValidString+ "::" right:ValidString+ { // some code... return metric, nil }

ValidString = ':'? [a-zA-Z0-9_.-]+ { return string(c.text), nil }

For my use case, when I type something not correct, I want to get the expression name "METRIC". And then I will give a metric list to user. The metric list is about to change all the time so it can't be added to peg file. The generated program will give detailed literal options in the expected list. But I want the optional expression name when parsing error occured.

Thanks in advance.

breml commented 6 years ago

@F4ncyMooN Thanks for your feature request. At the moment I feel like all the necessary possibilities to achieve your goal are already present in pigeon grammar.

Have you read the section about error reporting on godoc? Additionally there is also the concept of labeled failures, which is described in the documentation as well (at the end of this section). With these mechanics it is possible to return custom error messages, which then may include the information you prefer. So I suggest, you have a look at these possibilities.

F4ncyMooN commented 6 years ago

I tried the labeled failure, but it seems only error message could be customized. What I want is to customize the expected field in parserError.

For example,

ValidString "METRIC" = ':'? [a-zA-Z0-9_.-]+ 

When parse ValidString failed, pigeon will recover and throw an error with expected:[":", [a-zA-Z0-9_.-]], digging into the code, only litMatcher' val field will be written in the expected area. Maybe putting METRIC into the expected is more understandable and convenient for users.

Thanks for your kindly reply.

breml commented 6 years ago

@F4ncyMooN I am currently on vacation. I will have a look when I am back.

breml commented 6 years ago

@F4ncyMooN Are you willing to provide a PR for this?