Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: use size_t for size comparing. #22

Merged
merged 2 commits into from
Feb 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,13 @@ P4_Ok

## Test

Peppa PEG test requires downloading the test framework Unity:

```bash
$ git submodule init
$ git submodule update
```

Assume you have `cmake` and `gcc` installed.

```bash
Expand Down
30 changes: 15 additions & 15 deletions peppapeg.c
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ P4_MatchSequence(P4_Source* s, P4_Expression* e) {

P4_MarkPosition(s, startpos);

for (int i = 0; i < e->count; i++) {
for (size_t i = 0; i < e->count; i++) {
member = e->members[i];

// Optional `WHITESPACE` and `COMMENT` are inserted between every member.
Expand Down Expand Up @@ -585,7 +585,7 @@ P4_MatchChoice(P4_Source* s, P4_Expression* e) {
// A member is attempted if previous yields no match.
// The oneof match matches successfully immediately if any match passes.
P4_MarkPosition(s, startpos);
for (int i = 0; i < e->count; i++) {
for (size_t i = 0; i < e->count; i++) {
member = e->members[i];
tok = P4_Match(s, member);
if (NO_ERROR(s)) break;
Expand Down Expand Up @@ -630,7 +630,7 @@ P4_MatchChoice(P4_Source* s, P4_Expression* e) {
*/
P4_PRIVATE(P4_Token*)
P4_MatchRepeat(P4_Source* s, P4_Expression* e) {
size_t min = -1, max = -1, repeated = 0;
size_t min = SIZE_MAX, max = SIZE_MAX, repeated = 0;

assert(e->repeat_min != min || e->repeat_max != max); // need at least one of min/max.
assert(e->repeat_expr != NULL); // need repeat expression.
Expand Down Expand Up @@ -678,7 +678,7 @@ P4_MatchRepeat(P4_Source* s, P4_Expression* e) {
P4_SetPosition(s, before_implicit); // ^ puke extra whitespace
// ^ now we are here

if (min != -1 && repeated < min) {
if (min != SIZE_MAX && repeated < min) {
P4_RaiseError(s, P4_MatchError, "insufficient repetitions");
goto finalize;
} else { // sufficient repetitions.
Expand All @@ -698,7 +698,7 @@ P4_MatchRepeat(P4_Source* s, P4_Expression* e) {
repeated++;
P4_AdoptToken(head, tail, tok);

if (max != -1 && repeated == max) { // enough attempts
if (max != SIZE_MAX && repeated == max) { // enough attempts
P4_RescueError(s);
break;
}
Expand All @@ -709,12 +709,12 @@ P4_MatchRepeat(P4_Source* s, P4_Expression* e) {
assert(NO_ERROR(s));

// fails when attempts are excessive, e.g. repeated > max.
if (max != -1 && repeated > max) {
if (max != SIZE_MAX && repeated > max) {
P4_RaiseError(s, P4_MatchError, "excessive repetitions");
goto finalize;
}

if (min != -1 && repeated < min) {
if (min != SIZE_MAX && repeated < min) {
P4_RaiseError(s, P4_MatchError, "insufficient repetitions");
goto finalize;
}
Expand Down Expand Up @@ -967,7 +967,7 @@ P4_PrintSourceAst(P4_Token* token, P4_String buf, size_t indent) {
while (current != NULL) {
for (size_t i = 0; i < indent; i++) strcat(buf, " ");
strcat(buf, "- ");
sprintf(idstr+1, "%lu", current->expr->id);
sprintf(idstr+1, "%u", current->expr->id);
strcat(buf, idstr);
if (current->head == NULL) {
strcat(buf, ": \"");
Expand Down Expand Up @@ -1160,12 +1160,12 @@ P4_CreateRepeatMinMax(P4_Expression* repeat, size_t min, size_t max) {

P4_PUBLIC(P4_Expression*)
P4_CreateRepeatMin(P4_Expression* repeat, size_t min) {
return P4_CreateRepeatMinMax(repeat, min, -1);
return P4_CreateRepeatMinMax(repeat, min, SIZE_MAX);
}

P4_PUBLIC(P4_Expression*)
P4_CreateRepeatMax(P4_Expression* repeat, size_t max) {
return P4_CreateRepeatMinMax(repeat, -1, max);
return P4_CreateRepeatMinMax(repeat, SIZE_MAX, max);
}

P4_PUBLIC(P4_Expression*)
Expand All @@ -1180,12 +1180,12 @@ P4_CreateZeroOrOnce(P4_Expression* repeat) {

P4_PUBLIC(P4_Expression*)
P4_CreateZeroOrMore(P4_Expression* repeat) {
return P4_CreateRepeatMinMax(repeat, 0, -1);
return P4_CreateRepeatMinMax(repeat, 0, SIZE_MAX);
}

P4_PUBLIC(P4_Expression*)
P4_CreateOnceOrMore(P4_Expression* repeat) {
return P4_CreateRepeatMinMax(repeat, 1, -1);
return P4_CreateRepeatMinMax(repeat, 1, SIZE_MAX);
}

P4_PUBLIC(P4_Expression*)
Expand Down Expand Up @@ -1223,7 +1223,7 @@ P4_PUBLIC(P4_Grammar*) P4_CreateGrammar(void) {
grammar->rules = NULL;
grammar->count = 0;
grammar->cap = 0;
grammar->spaced_count = -1;
grammar->spaced_count = SIZE_MAX;
grammar->spaced_rules = NULL;
grammar->depth = P4_DEFAULT_RECURSION_LIMIT;
return grammar;
Expand Down Expand Up @@ -1460,7 +1460,7 @@ P4_SetWhitespaces(P4_Grammar* grammar) {
grammar->spaced_rules = NULL;
}

grammar->spaced_count = -1;
grammar->spaced_count = SIZE_MAX;

return P4_MemoryError;
}
Expand All @@ -1470,7 +1470,7 @@ P4_GetWhitespaces(P4_Grammar* g) {
if (g == NULL)
return NULL;

if (g->spaced_count == -1)
if (g->spaced_count == SIZE_MAX)
P4_SetWhitespaces(g);

return g->spaced_rules;
Expand Down
2 changes: 1 addition & 1 deletion peppapeg.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ P4_PUBLIC(P4_Expression*) P4_CreateSequence(size_t);
P4_PUBLIC(P4_Expression*) P4_CreateChoice(size_t);
P4_PUBLIC(P4_Expression*) P4_CreateSequenceWithMembers(size_t, ...);
P4_PUBLIC(P4_Expression*) P4_CreateChoiceWithMembers(size_t, ...);
P4_PUBLIC(P4_Expression*) P4_CreateRepeat(P4_Expression*, size_t, size_t);
P4_PUBLIC(P4_Expression*) P4_CreateRepeatMinMax(P4_Expression*, size_t, size_t);
P4_PUBLIC(P4_Expression*) P4_CreateRepeatMin(P4_Expression*, size_t);
P4_PUBLIC(P4_Expression*) P4_CreateRepeatMax(P4_Expression*, size_t);
P4_PUBLIC(P4_Expression*) P4_CreateRepeatExact(P4_Expression*, size_t);
Expand Down