soasme / PeppaPEG

PEG Parser in ANSI C
https://soasme.com/PeppaPEG
MIT License
55 stars 7 forks source link

Add debug info/trace output #140

Open mingodad opened 2 years ago

mingodad commented 2 years ago

When trying to create a grammar with PeppaPEG I was not getting the expected result and the error message didn't helped me at first then I decided to add a debug info/trace to help me see where the parser was at the time of the error, I saw the already reserved P4_Source.verbose flag after I added the trace code (see attaced diff, ignore the changes for the Lua grammar) and I'm leaving it here just in case it can be useful to others or even be included directly tho this project:

dad.diff.zip

Partial output of peppa parse -d -G json.peg -e entry data.json:

===>   recursion_depth peppa.c:function -> rule_name : input_line_number
===>    0 P4_Parse ->  : 1
===>    1 match_expression ->  : 1
===>    2 match_sequence -> entry : 1
===>    3 match_expression -> entry : 1
===>    4 match_positive -> entry : 1
===>    5 match_expression -> entry : 1
===>    6 match_range -> entry : 1
===>    4 match_expression -> entry : 1
===>    5 match_repeat -> entry : 1
===>    6 match_expression -> entry : 1
===>    7 match_choice -> entry : 1
===>    8 match_expression -> entry : 1
===>    9 match_reference -> entry : 1
===>   10 match_expression -> entry : 1
===>   11 match_choice -> whitespace : 1
===>   12 match_expression -> whitespace : 1
===>   13 match_literal -> whitespace : 1
===>   12 match_expression -> whitespace : 1
===>   13 match_literal -> whitespace : 1
===>   12 match_expression -> whitespace : 1
===>   13 match_literal -> whitespace : 1
===>   12 match_expression -> whitespace : 1
===>   13 match_literal -> whitespace : 1
===>    3 match_expression -> entry : 1
===>    4 match_reference -> entry : 1
===>    5 match_expression -> entry : 1
===>    6 match_choice -> value : 1
===>    7 match_expression -> value : 1
===>    8 match_reference -> value : 1
===>    9 match_expression -> value : 1
===>   10 match_sequence -> object : 1
===>   11 match_expression -> object : 1
===>   12 match_literal -> object : 1
...
===>    4 match_expression -> entry : 1
===>    5 match_repeat -> entry : 1
===>    6 match_expression -> entry : 1
===>    7 match_choice -> entry : 1
===>    8 match_expression -> entry : 1
===>    9 match_reference -> entry : 1
===>   10 match_expression -> entry : 1
===>   11 match_choice -> whitespace : 1
===>   12 match_expression -> whitespace : 1
===>   13 match_literal -> whitespace : 1
===>   12 match_expression -> whitespace : 1
===>   13 match_literal -> whitespace : 1
===>   12 match_expression -> whitespace : 1
===>   13 match_literal -> whitespace : 1
===>    3 match_expression -> entry : 2
===>    4 match_negative -> entry : 2
===>    5 match_expression -> entry : 2
===>    6 match_range -> entry : 2
[{"slice":[0,50],"type":"array","children":[{"slice":[1,25],"type":"object","children":[{"slice":[2,24],"type":"item","children":[{"slice":[2,11],"type":"string"},{"slice":[13,24],"type":"array","children":[{"slice":[14,15],"type":"number"},{"slice":[16,19],"type":"number"},{"slice":[20,23],"type":"number"}]}]}]},{"slice":[26,43],"type":"array","children":[{"slice":[27,31],"type":"true"},{"slice":[32,37],"type":"false"},{"slice":[38,42],"type":"null"}]},{"slice":[44,49],"type":"string"}]}]
mingodad commented 2 years ago

Also I think that add const where it's possible is a good idea like I created a type:

typedef const char*           P4_CString;

Then changed some function signatures to allow embed PeppaPEG on https://github.com/mingodad/squilu :

Usages of Typedef P4_CString: [21 occurrences, 1 filtered]
PeppaPEG
peppa.c
  653:  P4_CString content;
  783:  P4_PRIVATE(void) P4_DiffPosition(P4_CString str, P4_Position* start, size_t offset, P4_Position* stop);
  917:  P4_PRIVATE(P4_String) P4_CopySliceString(P4_CString, P4_Slice*);
1456:  u8_next_char(P4_CString s, ucs4_t* c) {
1666:  P4_CreateNode (P4_CString str,
1854:  P4_CString str = remaining_text(s);
1893:  P4_CString str = remaining_text(s);
3100:  P4_CreateSource(P4_CString content, P4_CString entry_name) {
3100:  P4_CreateSource(P4_CString content, P4_CString entry_name) {
3396:  P4_DiffPosition(P4_CString str, P4_Position* start, size_t offset, P4_Position* stop) {
3770:  P4_CopySliceString(P4_CString s, P4_Slice* slice) {
4278:  P4_CString node_str = node->text + node->slice.start.pos;
4931:  P4_LoadGrammarResult(P4_CString rules, P4_Result* result) {
4975:  P4_LoadGrammar(P4_CString rules) {
peppa.h
  431:  P4_CString text;
1467:  P4_Source* P4_CreateSource(P4_CString content, P4_CString entry_name);
1467:  P4_Source* P4_CreateSource(P4_CString content, P4_CString entry_name);
1669:  P4_Node* P4_CreateNode(P4_CString text, P4_Position* start, P4_Position* stop, P4_String rule);
1788:  P4_Error P4_LoadGrammarResult(P4_CString rules, P4_Result* result);
1810:  P4_Grammar* P4_LoadGrammar(P4_CString rules);
soasme commented 2 years ago

@mingodad Thanks for the proposed solution. I'll take a look. Regarding the const char* typedef, I created another issue to track the efforts.