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

Allow user setting Userdata for P4_Tokens. #47

Merged
merged 2 commits into from Mar 15, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions ROADMAP.md
Expand Up @@ -35,6 +35,7 @@
- [ ] Except (Any but).
- [ ] Any.
- [ ] Until.
- [x] print token ast in json format. Added in v1.8.0.
- [x] GetErrorString. Added in v1.7.0.
- [x] Join. Added in v1.7.0.
- [x] Start-of-Input & End-of-Input. Added in v1.7.0.
Expand Down
29 changes: 28 additions & 1 deletion peppapeg.c
Expand Up @@ -326,6 +326,20 @@ P4_DeleteToken(P4_Token* token) {
}
}

P4_PRIVATE(void)
P4_DeleteTokenUserData(P4_Grammar* grammar, P4_Token* token) {
if (grammar->free_func == NULL)
return;

P4_Token* tmp = token;
while (tmp != NULL) {
if (tmp->userdata != NULL)
grammar->free_func(tmp->userdata);
P4_DeleteTokenUserData(grammar, tmp->head);
tmp = tmp->next;
}
}

/*
* Push e into s->frame_stack.
*/
Expand Down Expand Up @@ -1296,6 +1310,7 @@ P4_PUBLIC P4_Grammar* P4_CreateGrammar(void) {
grammar->depth = P4_DEFAULT_RECURSION_LIMIT;
grammar->on_match = NULL;
grammar->on_error = NULL;
grammar->free_func = NULL;
return grammar;
}

Expand Down Expand Up @@ -1353,6 +1368,16 @@ P4_GetRecursionLimit(P4_Grammar* grammar) {
return grammar == NULL ? 0 : grammar->depth;
}

P4_Error
P4_SetUserDataFreeFunc(P4_Grammar* grammar, P4_UserDataFreeFunc free_func) {
if (grammar == NULL)
return P4_NullError;

grammar->free_func = free_func;

return P4_Ok;
}

P4_PUBLIC P4_Error
P4_AddGrammarRule(P4_Grammar* grammar, P4_RuleID id, P4_Expression* expr) {
size_t cap = grammar->cap;
Expand Down Expand Up @@ -1411,8 +1436,10 @@ P4_DeleteSource(P4_Source* source) {
if (source->errmsg)
free(source->errmsg);

if (source->root)
if (source->root) {
P4_DeleteTokenUserData(source->grammar, source->root);
P4_DeleteToken(source->root);
}

free(source);
}
Expand Down
24 changes: 24 additions & 0 deletions peppapeg.h
Expand Up @@ -236,6 +236,17 @@ typedef char* P4_String;
* */
typedef uint64_t P4_RuleID;

/**
* The reference of user data.
*/
typedef void* P4_UserData;

/**
* The function to free user data.
*/
typedef void (*P4_UserDataFreeFunc)(P4_UserData);


/*
*
* ░██████╗████████╗██████╗░██╗░░░██╗░█████╗░████████╗░██████╗
Expand Down Expand Up @@ -399,6 +410,9 @@ typedef struct P4_Token {
/** The id for matched grammar rule. */
P4_RuleID rule_id;

/** The user data. */
P4_UserData userdata;

/** the sibling token. NULL if not exists. */
struct P4_Token* next;
/** the first child of inner tokens. NULL if not exists. */
Expand Down Expand Up @@ -437,6 +451,8 @@ typedef struct P4_Grammar{
P4_MatchCallback on_match;
/** The callback after a match for an expression is failed. */
P4_ErrorCallback on_error;
/** The callback to free user data. */
P4_UserDataFreeFunc free_func;
} P4_Grammar;


Expand Down Expand Up @@ -1467,6 +1483,14 @@ P4_Error P4_SetGrammarRuleFlag(P4_Grammar*, P4_RuleID, P4_ExpressionFlag);
*/
P4_Error P4_SetRecursionLimit(P4_Grammar*, size_t limit);

/**
* @brief Set free function for the user data.
* @param grammar The grammar.
* @param free_func The free function.
* @return The error code.
*/
P4_Error P4_SetUserDataFreeFunc(P4_Grammar* grammar, P4_UserDataFreeFunc free_func);

/**
* @brief Get the maximum allowed recursion calls.
* @param grammar The grammar.
Expand Down