Skip to content

Commit

Permalink
Allow setting of user pointer in grammar
Browse files Browse the repository at this point in the history
Required for vnmakarov#12.
  • Loading branch information
TheCount committed Nov 11, 2018
1 parent 03836a4 commit d6f0b58
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/yaep.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ struct grammar
struct term_sets *term_sets_ptr;
/* Allocator. */
YaepAllocator *alloc;
/* User pointer */
void *userptr;
};

/* The following variable value is the reference for the current
Expand Down Expand Up @@ -3049,9 +3051,40 @@ yaep_create_grammar (void)
grammar->symbs_ptr = symbs_ptr = symb_init ();
grammar->term_sets_ptr = term_sets_ptr = term_set_init ();
grammar->rules_ptr = rules_ptr = rule_init ();
grammar->userptr = NULL;
return grammar;
}

/* The following function sets the user pointer for a grammar. */
#ifdef __cplusplus
static
#endif
void
yaep_grammar_setuserptr (struct grammar *g, void *userptr)
{
if (g != NULL)
{
g->userptr = userptr;
}
}

/* The following function retrieves the user pointer of a grammar. */
#ifdef __cplusplus
static
#endif
void *
yaep_grammar_getuserptr (struct grammar *g)
{
if (g == NULL)
{
return NULL;
}
else
{
return g->userptr;
}
}

/* The following function makes grammar empty. */
static void
yaep_empty_grammar (void)
Expand Down
12 changes: 12 additions & 0 deletions src/yaep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,18 @@ yaep::set_recovery_match (int n_toks)
return yaep_set_recovery_match (this->grammar, n_toks);
}

void
yaep::setuserptr (void *userptr) noexcept
{
yaep_grammar_setuserptr (this->grammar, userptr);
}

void *
yaep::getuserptr () const noexcept
{
return yaep_grammar_getuserptr (this->grammar);
}

int
yaep::parse (int (*read_token) (void **attr),
void (*syntax_error) (int err_tok_num,
Expand Down
12 changes: 12 additions & 0 deletions src/yaep.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,16 @@ struct yaep_tree_node
the first. */
extern struct grammar *yaep_create_grammar (void);

/* The following function stores a user-defined pointer
in the given grammar. */
extern void yaep_grammar_setuserptr (struct grammar *g, void *userptr);

/* The following function retrieves a user-defined pointer
previously set with yaep_grammar_setuserptr() from
the given grammar. If no user pointer has been set,
a null pointer is returned. */
extern void *yaep_grammar_getuserptr (struct grammar *g);

/* The function returns the last occurred error code for given
grammar. */
extern int yaep_error_code (struct grammar *g);
Expand Down Expand Up @@ -353,6 +363,8 @@ class yaep
int set_cost_flag (int flag);
int set_error_recovery_flag (int flag);
int set_recovery_match (int n_toks);
void setuserptr (void *userptr) noexcept;
void *getuserptr () const noexcept;

/* See comments for function yaep_parse. */
int parse (int (*read_token) (void **attr),
Expand Down

0 comments on commit d6f0b58

Please sign in to comment.