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

[Feature]: Get/Set Grammar Rule Name. #53

Merged
merged 3 commits into from
Mar 28, 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
48 changes: 48 additions & 0 deletions peppapeg.c
Original file line number Diff line number Diff line change
Expand Up @@ -1294,6 +1294,7 @@ P4_CreateLiteral(const P4_String literal, bool sensitive) {
expr->id = 0;
expr->kind = P4_Literal;
expr->flag = 0;
expr->name = NULL;
expr->literal = strdup(literal);
expr->sensitive = sensitive;
return expr;
Expand All @@ -1308,6 +1309,7 @@ P4_CreateRange(P4_Rune lower, P4_Rune upper, size_t stride) {
expr->id = 0;
expr->kind = P4_Range;
expr->flag = 0;
expr->name = NULL;
expr->lower = lower;
expr->upper = upper;
expr->stride = stride;
Expand All @@ -1323,6 +1325,7 @@ P4_CreateReference(P4_RuleID id) {
expr->id = 0;
expr->kind = P4_Reference;
expr->flag = 0;
expr->name = NULL;
expr->ref_id = id;
expr->ref_expr = NULL;
return expr;
Expand All @@ -1337,6 +1340,7 @@ P4_CreatePositive(P4_Expression* refexpr) {
expr->id = 0;
expr->kind = P4_Positive;
expr->flag = 0;
expr->name = NULL;
expr->ref_expr = refexpr;
return expr;
}
Expand All @@ -1350,6 +1354,7 @@ P4_CreateNegative(P4_Expression* refexpr) {
expr->id = 0;
expr->kind = P4_Negative;
expr->flag = 0;
expr->name = NULL;
expr->ref_expr = refexpr;
return expr;
}
Expand All @@ -1362,6 +1367,7 @@ P4_CreateContainer(size_t count) {
P4_Expression* expr = malloc(sizeof(P4_Expression));
expr->id = 0;
expr->flag = 0;
expr->name = NULL;
expr->count = count;
expr->members = malloc(sizeof(P4_Expression*) * count);
return expr;
Expand Down Expand Up @@ -1467,6 +1473,7 @@ P4_CreateRepeatMinMax(P4_Expression* repeat, size_t min, size_t max) {
P4_Expression* expr = malloc(sizeof(P4_Expression));
expr->id = 0;
expr->flag = 0;
expr->name = NULL;
expr->kind = P4_Repeat;
expr->repeat_expr = repeat;
expr->repeat_min = min;
Expand Down Expand Up @@ -1510,6 +1517,7 @@ P4_CreateBackReference(size_t index, bool sensitive) {
expr->id = 0;
expr->kind = P4_BackReference;
expr->flag = 0;
expr->name = NULL;
expr->backref_index = index;
expr->sensitive = sensitive;
return expr;
Expand Down Expand Up @@ -1577,6 +1585,18 @@ P4_GetGrammarRule(P4_Grammar* grammar, P4_RuleID id) {
return NULL;
}

P4_PUBLIC P4_Expression*
P4_GetGrammarRuleByName(P4_Grammar* grammar, P4_String name) {
size_t i;
P4_Expression* rule = NULL;
for (i = 0; i < grammar->count; i++) {
rule = grammar->rules[i];
if (rule && rule->name && strcmp(rule->name, name) == 0)
return rule;
}
return NULL;
}

P4_PUBLIC P4_Error
P4_SetGrammarRuleFlag(P4_Grammar* grammar, P4_RuleID id, P4_ExpressionFlag flag) {
P4_Expression* expr = P4_GetGrammarRule(grammar, id);
Expand Down Expand Up @@ -1641,6 +1661,31 @@ P4_AddGrammarRule(P4_Grammar* grammar, P4_RuleID id, P4_Expression* expr) {
return P4_Ok;
}

P4_PUBLIC P4_Error
P4_SetGrammarRuleName(P4_Grammar* grammar, P4_RuleID id, P4_String name) {
P4_Expression* expr = P4_GetGrammarRule(grammar, id);

if (expr == NULL)
return P4_NullError;

if (expr->name != NULL)
free(expr->name);

expr->name = strdup(name);

return P4_Ok;
}

P4_PUBLIC P4_String
P4_GetGrammarRuleName(P4_Grammar* grammar, P4_RuleID id) {
P4_Expression* expr = P4_GetGrammarRule(grammar, id);

if (expr == NULL)
return NULL;

return expr->name;
}

P4_PUBLIC P4_Source*
P4_CreateSource(P4_String content, P4_RuleID rule_id) {
P4_Source* source = malloc(sizeof(P4_Source));
Expand Down Expand Up @@ -2129,6 +2174,9 @@ P4_DeleteExpression(P4_Expression* expr) {
break;
}

if (expr->name)
free(expr->name);

free(expr);
}

Expand Down
46 changes: 41 additions & 5 deletions peppapeg.h
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,8 @@ typedef struct P4_Source {
* The grammar rule.
*/
typedef struct P4_Expression {
/* The name of expression (only for debugging). */
/* P4_String name; */
/* The name of expression. */
P4_String name;
/** The id of expression. */
P4_RuleID id;
/** The kind of expression. */
Expand Down Expand Up @@ -1458,10 +1458,33 @@ void P4_DeleteGrammar(P4_Grammar*);
*/
P4_Error P4_AddGrammarRule(P4_Grammar*, P4_RuleID, P4_Expression*);

/**
* Set the name for a grammar rule.
*
* @param grammar The grammar.
* @param id The grammar rule id.
* @param name The grammar rule name.
* @return The error code.
*
* Example:
*
* P4_SetRuleName(grammar, ENTRY, "entry");
*/
P4_Error P4_SetGrammarRuleName(P4_Grammar* grammar, P4_RuleID id, P4_String name);




/**
* Get the name for a grammar rule.
*
* @param grammar The grammar.
* @param id The grammar rule id.
* @return The grammar rule name.
*
* Example:
*
* P4_String name = P4_GetRuleName(grammar, ENTRY);
* printf("%s\n", name);
*/
P4_String P4_GetGrammarRuleName(P4_Grammar* grammar, P4_RuleID id);

/**
* Delete a grammar rule.
Expand All @@ -1487,6 +1510,19 @@ void P4_DeleteGrammarRule(P4_Grammar*, P4_RuleID);
*/
P4_Expression* P4_GetGrammarRule(P4_Grammar*, P4_RuleID);

/**
* Get a grammar rule by its name.
*
* @param grammar The grammar.
* @param name The grammar rule name.
* @return The grammar rule expression. Returns NULL if not found.
*
* P4_AddLiteral(grammar, 1, "a", true);
* P4_SetGrammarRuleName(grammar, 1, "rule_a");
* P4_Expression* expr = P4_GetGrammarRule(grammar, "rule_a"); // The literal expression.
*/
P4_Expression* P4_GetGrammarRuleByName(P4_Grammar* grammar, P4_String name);

/**
* @brief Set the flag of a grammar rule.
* @param grammar The grammar.
Expand Down
16 changes: 16 additions & 0 deletions tests/test_misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,21 @@ void test_lineno_offset(void) {

}

void test_name(void) {
const int R1 = 1;

P4_Grammar* grammar = P4_CreateGrammar();
TEST_ASSERT_NOT_NULL(grammar);
TEST_ASSERT_EQUAL( P4_Ok, P4_AddLiteral(grammar, R1, "A", false));
TEST_ASSERT_EQUAL( P4_Ok, P4_SetGrammarRuleName(grammar, R1, "R1"));
TEST_ASSERT_EQUAL_STRING( "R1", P4_GetGrammarRuleName(grammar, R1));
P4_Expression* expr = P4_GetGrammarRule(grammar, R1);
TEST_ASSERT_EQUAL( expr, P4_GetGrammarRuleByName(grammar, "R1"));
TEST_ASSERT_EQUAL( NULL, P4_GetGrammarRuleByName(grammar, "R0"));

P4_DeleteGrammar(grammar);
}

int main(void) {
UNITY_BEGIN();

Expand All @@ -207,6 +222,7 @@ int main(void) {
RUN_TEST(test_error_string);
RUN_TEST(test_source_slice);
RUN_TEST(test_lineno_offset);
RUN_TEST(test_name);

return UNITY_END();
}