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]: Don't Insert Whitespace In Repeat When On Spacing. #39

Merged
merged 4 commits into from
Mar 4, 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
2 changes: 2 additions & 0 deletions ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
- [ ] New Expression Kind: Function.
- [ ] New Expression Kind: Sub Grammar.
- [ ] Performance optimization: pre-alloc tokens.
- [ ] Token only have expr id, no expr.
- [ ] Register a optional name function to convert rule id to rule names.
- [ ] Stop on first error v/s Recover from Panic.
- [ ] Allow replacing malloc/free functions.
- [ ] New Source Flag: Support UTF-8 BOM sequence (0xEF 0xBB 0xBF) at the start of source.
Expand Down
20 changes: 8 additions & 12 deletions examples/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,18 +239,14 @@ P4_Grammar* P4_CreateJSONGrammar() {
))
goto finalize;

if (P4_Ok != P4_AddSequenceWithMembers(grammar, P4_JSONValue, 3,
P4_CreateZeroOrMore(P4_CreateReference(P4_JSONWhitespace)),
P4_CreateChoiceWithMembers(7,
P4_CreateReference(P4_JSONObject),
P4_CreateReference(P4_JSONArray),
P4_CreateReference(P4_JSONString),
P4_CreateReference(P4_JSONNumber),
P4_CreateReference(P4_JSONTrue),
P4_CreateReference(P4_JSONFalse),
P4_CreateReference(P4_JSONNull)
),
P4_CreateZeroOrMore(P4_CreateReference(P4_JSONWhitespace))
if (P4_Ok != P4_AddChoiceWithMembers(grammar, P4_JSONValue, 7,
P4_CreateReference(P4_JSONObject),
P4_CreateReference(P4_JSONArray),
P4_CreateReference(P4_JSONString),
P4_CreateReference(P4_JSONNumber),
P4_CreateReference(P4_JSONTrue),
P4_CreateReference(P4_JSONFalse),
P4_CreateReference(P4_JSONNull)
))
goto finalize;

Expand Down
2 changes: 1 addition & 1 deletion peppapeg.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
# define IS_LIFTED(e) (((e)->flag & P4_FLAG_LIFTED) != 0)
# define IS_RULE(e) ((e)->id != 0)
# define NEED_SILENT(s) ((s)->frame_stack ? (s)->frame_stack->silent : false)
# define NEED_SPACE(s) ((s)->frame_stack ? (s)->frame_stack->space : false)
# define NEED_SPACE(s) (!(s)->whitespacing && ((s)->frame_stack ? (s)->frame_stack->space : false))
# define NO_ERROR(s) ((s)->err == P4_Ok)
# define NO_MATCH(s) ((s)->err == P4_MatchError)

Expand Down
59 changes: 55 additions & 4 deletions tests/test_flags.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void test_spaced_rule_should_loosen_sequence(void) {
* Output:
* ENTRY: "HELLOWORLD"
*/
void test_spaced_rule_should_be_ignored_in_sequence(void) {
void test_spaced_rule_should_be_ignored_in_tight_sequence(void) {
P4_Grammar* grammar = P4_CreateGrammar();
TEST_ASSERT_NOT_NULL(grammar);
TEST_ASSERT_EQUAL(
Expand Down Expand Up @@ -95,6 +95,56 @@ void test_spaced_rule_should_be_ignored_in_sequence(void) {
P4_DeleteGrammar(grammar);
}

/*
* Rules:
* ENTRY = R1 ":" R1
* R1 = "1" ~ "2" # TIGHT | SQUASHED
* WHITESPACE = " " | "\t" | "\n" # SPACED, LIFTED
* Input:
* 12 : 12
* Output:
* ENTRY:
* R1: 12
* R1: 12
*/
void test_spaced_rule_should_loosen_sequence_despite_member_is_tight(void) {
P4_Grammar* grammar = P4_CreateGrammar();
TEST_ASSERT_NOT_NULL(grammar);
TEST_ASSERT_EQUAL(
P4_Ok,
P4_AddSequenceWithMembers(grammar, ENTRY, 3,
P4_CreateReference(R1),
P4_CreateLiteral(":", true),
P4_CreateReference(R1)
)
);
TEST_ADD_WHITESPACE(grammar, WHITESPACE);
TEST_ASSERT_EQUAL(
P4_Ok,
P4_AddSequenceWithMembers(grammar, R1, 2,
P4_CreateLiteral("1", true),
P4_CreateLiteral("2", true)
)
);
TEST_ASSERT_EQUAL(P4_Ok, P4_SetGrammarRuleFlag(grammar, R1, P4_FLAG_TIGHT | P4_FLAG_SQUASHED));

P4_Source* source = P4_CreateSource("12 : 12", ENTRY);
TEST_ASSERT_NOT_NULL(source);
TEST_ASSERT_EQUAL(
P4_Ok,
P4_Parse(grammar, source)
);
TEST_ASSERT_EQUAL(9, P4_GetSourcePosition(source));

P4_Token* token = P4_GetSourceAst(source);
TEST_ASSERT_NOT_NULL(token);
TEST_ASSERT_EQUAL_TOKEN_STRING("12 : 12", token);
TEST_ASSERT_EQUAL_TOKEN_RULE(ENTRY, token);

P4_DeleteSource(source);
P4_DeleteGrammar(grammar);
}

/*
* Rules:
* ENTRY = "HELLO" & "WORLD" # TIGHT
Expand All @@ -106,7 +156,7 @@ void test_spaced_rule_should_be_ignored_in_sequence(void) {
* Output:
* NULL
*/
void test_spaced_rule_should_be_ignored_in_sequence2(void) {
void test_spaced_rule_should_be_ignored_in_tight_sequence2(void) {
P4_Grammar* grammar = P4_CreateGrammar();
TEST_ASSERT_NOT_NULL(grammar);
TEST_ASSERT_EQUAL(
Expand Down Expand Up @@ -713,8 +763,9 @@ int main(void) {
UNITY_BEGIN();

RUN_TEST(test_spaced_rule_should_loosen_sequence);
RUN_TEST(test_spaced_rule_should_be_ignored_in_sequence);
RUN_TEST(test_spaced_rule_should_be_ignored_in_sequence2);
RUN_TEST(test_spaced_rule_should_be_ignored_in_tight_sequence);
RUN_TEST(test_spaced_rule_should_be_ignored_in_tight_sequence2);
RUN_TEST(test_spaced_rule_should_loosen_sequence_despite_member_is_tight);
RUN_TEST(test_spaced_rule_should_be_applied_in_repeat);
RUN_TEST(test_spaced_rule_should_be_ignored_in_tight_repeat);
RUN_TEST(test_spaced_rule_should_be_ignored_in_tight_repeat2);
Expand Down