Skip to content

Commit

Permalink
Merge pull request #14 from AnjaBruls/master
Browse files Browse the repository at this point in the history
Update examples
  • Loading branch information
Jeroen van der Heijden authored Dec 18, 2018
2 parents b8ed773 + 8250f03 commit 9a4a004
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 27 deletions.
23 changes: 11 additions & 12 deletions examples/tree_and_expect/expect/expect.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ const char * prt_expected(cleri_parse_t * pr)
int rc;
int count;

/* Creates a second buffer for the expect string. If something goes wrong,
redirection to only clean up the parse object. */
/* Creates a second buffer for the expect-string. If something goes wrong,
redirect to clean up the parse object. */
buffer_t * expect_buf = buffer_create();
if (expect_buf == NULL)
goto end_prt_expected;

/* Gets the expect string */
/* Gets the expect-string */
for (count = 0; pr->expect != NULL; count++)
{
const char * expect = get_cleri_obj(pr->expect->cl_obj, RETURN_REGEX_TYPE);
Expand All @@ -67,20 +67,20 @@ const char * prt_expected(cleri_parse_t * pr)
goto end_prt_expected;
}
}
/* Prints all the expected objects and clean up the second buffer */
/* Prints all the expected objects and cleans up the second buffer */
printf("Expected at position %lu: %s.\n", pr->pos, expect_buf->buf);
buffer_destroy(expect_buf);

/* If there are object in the olist, choose one and return this. */
/* If there are objects in the olist, choose one and return this. */
if (count > 0)
{
/* Chooses a random object out of the expect olist and returns it */
/* Chooses a random object out of the expect-olist and returns it */
int choice = (rand() * time(NULL)) % count; // seed
cleri_parse_expect_start(pr);

/* Count down to choice */
while (choice--)
{
/* Takes choice */
pr->expect = pr->expect->next;
if (pr->expect->cl_obj->tp == CLERI_TP_END_OF_STATEMENT)
cleri_parse_expect_start(pr);
Expand All @@ -91,7 +91,7 @@ const char * prt_expected(cleri_parse_t * pr)
/* Return the randomly choosen object.*/
return expect;
}
/* But if something went wrong, it returns NULL. */
/* But if something goes wrong, it returns NULL. */
end_prt_expected:
return NULL;
}
Expand All @@ -100,22 +100,21 @@ const char * prt_expected(cleri_parse_t * pr)
/* Guided autocorrection */
void test_autocor(cleri_grammar_t * grammar, const char * str, buffer_t * buf)
{
/* If parse function returns NULL, somthing went wrong and the program wil
/* If parse function returns NULL, something went wrong and the program will
be aborted */
cleri_parse_t * pr = cleri_parse(grammar, str);
if (pr == NULL)
abort();
printf("Your string: '%s'\n", str);

/* First time the buffer_printf() is used. Stores the string till 'pos'.
If error in buffer_printf() then it would give a -1. In that case clean
If error in buffer_printf() then it will return -1. In that case clean
up and leave the function. */
int rc = buffer_printf(buf, "%.*s ",(int)pr->pos, str);
if (rc)
goto test_autocor;

/* Loops as long as the string turns out to be valid or if 10 iterations
haved passed */
/* Loops until the string is valid or if 10 iterations have passed */
int trial;
for (trial = 0; !pr->is_valid && trial < 10; trial++)
{
Expand Down
12 changes: 6 additions & 6 deletions examples/tree_and_expect/expect/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ enum cleri_grammar_ids {
CLERI_GID_R_REGEX,
};

/* User-defined function used in the show_cleri_obj(). Here the regex elemnts
/* User-defined function used in the show_cleri_obj(). Here the regex elements
are given a user-defined name based on the gid.
With "choice" you can return either the explanation of the regex or an example
of the regex. */
Expand Down Expand Up @@ -47,7 +47,7 @@ const char * re_to_str(uint32_t gid, int choice)

cleri_grammar_t * create_grammar(void)
{
/* Define grammar and if error occurred redirect to goto*/
/* Define grammar and if error occurs, redirect (goto)*/
cleri_t * k_hi = cleri_keyword(0, "hi", 0);
if (k_hi == NULL)
goto end_create_grammar;
Expand All @@ -69,7 +69,7 @@ cleri_grammar_t * create_grammar(void)

return cleri_grammar(start, NULL);

/* On error: clean up the previous objects that were succesfully allocated */
/* If error occurs, clean up the previous objects that were succesfully allocated */
end_create_grammar:
if (k_hi)
cleri_free(k_hi);
Expand All @@ -89,7 +89,7 @@ cleri_grammar_t * create_grammar(void)

int main(void)
{
/* Creates grammar. If error occurred, NULL is returned and the program
/* Creates grammar. If error occurs, NULL is returned and the program
will be aborted. */
cleri_grammar_t * my_grammar = create_grammar();
if (my_grammar == NULL)
Expand All @@ -105,12 +105,12 @@ int main(void)
str[len-1]='\0';

/* Creates a dynamic buffer that will store the string to be parsed.
If error occurred NULL is returned and redirected to goto. */
If error occurrs, NULL is returned. */
buffer_t * buf = buffer_create();
if (buf == NULL)
goto end_main;

/* String will be parsed and if invalid autocorrected */
/* String will be parsed and if invalid it will be autocorrected */
test_autocor(my_grammar, (const char *)str, buf);

/* Cleanup*/
Expand Down
6 changes: 3 additions & 3 deletions examples/tree_and_expect/tree/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

cleri_grammar_t * create_grammar(void)
{
/* Define grammar and if error occurred redirect to goto. */
/* Define grammar and if error occurs, redirect (goto). */
cleri_t * k_hi = cleri_keyword(0, "hi", 0);
if (k_hi == NULL)
goto end_create_grammar;
Expand All @@ -28,7 +28,7 @@ cleri_grammar_t * create_grammar(void)

return cleri_grammar(start, NULL);

/* On error: clean up the previous objects that were succesfully allocated. */
/* If error occurs, clean up the previous objects that were succesfully allocated. */
end_create_grammar:
if (k_hi)
cleri_free(k_hi);
Expand All @@ -48,7 +48,7 @@ cleri_grammar_t * create_grammar(void)

int main(void)
{
/* Create grammar. If an error occurred, NULL is returned and
/* Create grammar. If an error occurs, NULL is returned and
the program will be aborted. */
cleri_grammar_t * my_grammar = create_grammar();
if (my_grammar == NULL)
Expand Down
11 changes: 5 additions & 6 deletions examples/tree_and_expect/tree/tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ int prt_JSON(char * string)
break;
}
}
/* Prints the last character as the "for" loop will stop before. */
/* Prints the last character, because the "for" loop will stop before. */
printf("\n%c\n", string[i]);
return 0;
}
Expand Down Expand Up @@ -77,7 +77,7 @@ const char * strtp(cleri_tp tp)
}
}

/* Recursive function. Gets children in the parse tree and concatenates them
/* Recursive function. Gets children of the parse tree and concatenates them
by using the buffer_printf(). The end result is a JSON string without spaces,
tabs and newlines.*/
int get_children(cleri_children_t * child, const char * orig, buffer_t * buf)
Expand Down Expand Up @@ -114,12 +114,11 @@ int get_children(cleri_children_t * child, const char * orig, buffer_t * buf)
else
rc += buffer_printf(buf, "]");

/* When error occurred in the buffer_printf(), it returns -1. Something
went wrong when rc is not 0 */
/* When error occurs in the buffer_printf(), it returns -1. */
return rc;
}

/* Prints the parse tree in JSON format of parse object. Note that the cl_obj
/* Prints the parse tree in JSON format. Note that the cl_obj
is NULL for the root node and the first can be found in its children. */
void prt_tree(cleri_grammar_t * grammar, const char * str)
{
Expand All @@ -131,7 +130,7 @@ void prt_tree(cleri_grammar_t * grammar, const char * str)
cleri_node_t * tree = pr->tree;

/* Creates a dynamic buffer that will store the string to be parsed.
If error occurred NULL is returned and redirected to goto. */
If error occurs, NULL is returned and redirected to goto. */
buffer_t * buf = buffer_create();
if (buf == NULL)
goto prt_tree;
Expand Down

0 comments on commit 9a4a004

Please sign in to comment.