Skip to content

Commit

Permalink
Work on converting bocage interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeffrey Kegler authored and Jeffrey Kegler committed Dec 20, 2011
1 parent 7920ca5 commit 74372f7
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 60 deletions.
9 changes: 7 additions & 2 deletions r2/libmarpa/dev/api.texi
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ This manual is for libmarpa, version 0.0.0.
* Grammars:: Grammar objects
* Recognizers:: Recognizer objects
* Bocage:: Bocage objects
* Order:: Order objects
* Error handling:: Error handling
@end menu

Expand Down Expand Up @@ -413,16 +414,20 @@ Return value: %NULL if the GLib library is compatible with the
@deftypefun void marpa_b_unref (Marpa_Bocage @var{b})
@end deftypefun

@deftypefun Marpa_Grammar marpa_b_g(Marpa_Bocage @var{b})
@end deftypefun

@node Order
@chapter Order objects

@deftypefun gint marpa_o_and_order_set(Marpa_Bocage b, @
@deftypefun gint marpa_o_and_order_set ( @
Marpa_Bocage b, @
Marpa_Or_Node_ID or_node_id, @
Marpa_And_Node_ID* and_node_ids, @
gint length)
@end deftypefun

@deftypefun Marpa_And_Node_ID marpa_o_and_order_get( @
@deftypefun Marpa_And_Node_ID marpa_o_and_order_get ( @
Marpa_Bocage b, @
Marpa_Or_Node_ID or_node_id, gint ix)
@end deftypefun
Expand Down
74 changes: 54 additions & 20 deletions r2/libmarpa/dev/marpa.w
Original file line number Diff line number Diff line change
Expand Up @@ -10301,11 +10301,15 @@ Marpa_Bocage t_bocage;
@ @<Initialize recognizer elements@> =
B_of_R(r) = NULL;

@*0 The Recognizer of the Bocage.
@*0 The base objects of the bocage.
@ @d I_of_B(b) ((b)->t_input)
@ @d R_of_B(b) ((b)->t_recce)
@<Widely aligned bocage elements@> =
INPUT t_input;
/* Remove |R_of_B| and |t_recce| after interface conversion */
RECCE t_recce;
@

@<Unpack bocage objects@> =
const INPUT input = I_of_B(b);
const GRAMMAR g = G_of_I(input);
Expand Down Expand Up @@ -10347,31 +10351,39 @@ Marpa_Bocage marpa_b_new(Marpa_Recognizer r,
@<Declare bocage locals@>@;
@<Return if function guards fail@>@;
b = B_of_R(r) = g_slice_new(struct s_bocage);
if (b) {
R_DEV_ERROR ("bocage already set");
goto B_NEW_RETURN_ERROR;
}
r_ref(r);
I_of_B(b) = I_of_R(r);
/* Remove |R_of_B| and |t_recce| after interface conversion */
R_of_B(b) = r;
@<Initialize bocage elements@>@;
if (G_is_Trivial(g)) {
if (ordinal_arg > 0) goto B_NEW_ERROR;
if (ordinal_arg > 0) goto B_NEW_RETURN_ERROR;
return r_create_null_bocage(r, b);
}
r_update_earley_sets(r);
@<Set |end_of_parse_earley_set| and |end_of_parse_earleme|@>@;
if (end_of_parse_earleme == 0) {
if (! g->t_null_start_rule) goto B_NEW_ERROR;
if (! g->t_null_start_rule) goto B_NEW_RETURN_ERROR;
return r_create_null_bocage(r, b);
}
@<Set |completed_start_rule|@>@;
@<Find |start_eim|, |start_aim| and |start_aex|@>@;
if (!start_eim) goto B_NEW_ERROR;
if (!start_eim) goto B_NEW_RETURN_ERROR;
obstack_init(&bocage_setup_obs);
@<Allocate bocage setup working data@>@;
@<Populate the PSIA data@>@;
@<Create the or-nodes for all earley sets@>@;
@<Create the final and-nodes for all earley sets@>@;
@<Set top or node id in |b|@>;
obstack_free(&bocage_setup_obs, NULL);
r_ref(r);
return b;
B_NEW_ERROR: ;
B_NEW_RETURN_ERROR: ;
r_unref(r);
B_of_R(r) = NULL;
@<Destroy bocage elements, all phases@>;
return NULL;
}
Expand Down Expand Up @@ -10438,7 +10450,7 @@ struct s_bocage_setup_per_es* per_es_data = NULL;
}

if (!end_of_parse_earley_set)
goto B_NEW_ERROR;
goto B_NEW_RETURN_ERROR;
end_of_parse_earleme = Earleme_of_ES (end_of_parse_earley_set);
}

Expand All @@ -10447,7 +10459,7 @@ struct s_bocage_setup_per_es* per_es_data = NULL;
if (rule_id == -1)
{
completed_start_rule = g->t_proper_start_rule;
if (!completed_start_rule) goto B_NEW_ERROR;
if (!completed_start_rule) goto B_NEW_RETURN_ERROR;
}
else
{
Expand Down Expand Up @@ -10593,9 +10605,27 @@ to make sense.
Top_ORID_of_B (b) = ID_of_OR (top_or_node);
}

@*0 Top Or Node.
@ For an initialized bocage, return the ID of the top
or-node.
@*0 The grammar of the bocage.
@ This function returns the grammar of the bocage.
It never returns an error.
The grammar is always set when the bocage is initialized,
and is never changed while the bocage exists.
Fatal state is not reported,
because it is kept in the grammar,
so that
either we can return the grammar in spite of
its fatal state,
or the problem is so severe than no
errors can be properly reported.
@<Function definitions@> =
Marpa_Grammar marpa_b_g(Marpa_Bocage b)
{
@<Unpack bocage objects@>@;
return g;
}

@*0 Top or-node.
@ Return the ID of the top or-node.
@<Public function prototypes@> =
Marpa_Or_Node_ID marpa_b_top_or_node(Marpa_Bocage b);
@ @<Function definitions@> =
Expand Down Expand Up @@ -10658,17 +10688,21 @@ marpa_b_ref (Marpa_Bocage b)
if the bocage already has been freed,
or was never initialized.
@<Private function prototypes@> =
gint b_free(BOCAGE b);
void
b_free(BOCAGE b);
@ @<Function definitions@> =
gint b_free(BOCAGE b) {
@<Return |-2| on failure@>@;
@<Unpack bocage objects@>@;
@<Fail if fatal error@>@;
if (b) {
@<Destroy bocage elements, all phases@>;
g_slice_free(struct s_bocage, b);
void
b_free (BOCAGE b)
{
const RECCE r = R_of_B (b);
@<Unpack bocage objects@>@;
B_of_R (r) = NULL;
r_unref (r);
if (b)
{
@<Destroy bocage elements, all phases@>;
g_slice_free (struct s_bocage, b);
}
return 1;
}

@*0 Trace Functions.
Expand Down
81 changes: 43 additions & 38 deletions r2/xs/R2.xs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ typedef struct marpa_b Bocage;
typedef struct {
Bocage *b;
char *message_buffer;
GArray* gint_array;
} B_Wrapper;

static const char grammar_c_class_name[] = "Marpa::R2::Internal::G_C";
Expand Down Expand Up @@ -150,6 +149,21 @@ error_r (R_Wrapper * r_wrapper)
return buffer;
}

/* Return value must be g_free()'d */
static const char *
error_b (B_Wrapper * b_wrapper)
{
const char *error_string;
Marpa_Bocage b = b_wrapper->b;
Marpa_Recce r = marpa_b_g(b);
const int error_code = marpa_g_error (g, &error_string);
char *buffer = b_wrapper->message_buffer;
g_free (buffer);
b_wrapper->message_buffer = buffer =
libmarpa_exception (error_code, error_string);
return buffer;
}

MODULE = Marpa::R2 PACKAGE = Marpa::R2::Internal

PROTOTYPES: DISABLE
Expand Down Expand Up @@ -1129,10 +1143,6 @@ PPCODE:
if (!r) { croak ("failure in marpa_r_new: %s", error_g (g_wrapper)); };
Newx( r_wrapper, 1, R_Wrapper );
r_wrapper->r = r;
r_wrapper->b = NULL;
r_wrapper->o = NULL;
r_wrapper->t = NULL;
r_wrapper->v = NULL;
r_wrapper->gint_array = g_array_new( FALSE, FALSE, sizeof(gint));
r_wrapper->message_buffer = NULL;
sv = sv_newmortal();
Expand Down Expand Up @@ -1965,51 +1975,46 @@ PPCODE:
MODULE = Marpa::R2 PACKAGE = Marpa::R2::Internal::B_C

void
bocage_setup( r_wrapper, rule_id, ordinal )
new( r_wrapper, rule_id, ordinal )
R_Wrapper *r_wrapper;
Marpa_Rule_ID rule_id;
Marpa_Earley_Set_ID ordinal;
PPCODE:
{
Marpa_Recognizer r = r_wrapper->r;
Marpa_Bocage b = r_wrapper->b;
if (b) {
croak ("Problem in r->bocage_setup(): recognizer already has bocage");
}
b = marpa_b_new(r, rule_id, ordinal);
r_wrapper->b = b;
if (!b) {
croak ("Problem in r->bocage_setup(): %s", error_r(r_wrapper));
}
XSRETURN_YES;
}
{
SV *sv;
Marpa_Recognizer r = r_wrapper->r;
b = marpa_b_new (r, rule_id, ordinal);
if (!b)
{
croak ("Problem in b->new(): %s", error_r (r_wrapper));
}
Newx (b_wrapper, 1, B_Wrapper);
b_wrapper->message_buffer = NULL;
sv = sv_newmortal ();
sv_setref_pv (sv, bocage_c_class_name, (void *) b_wrapper);
XPUSHs (sv);
}

void
bocage_clear( r_wrapper )
R_Wrapper *r_wrapper;
DESTROY( b_wrapper )
B_Wrapper *b_wrapper;
PPCODE:
{
gint result;
Marpa_Bocage b = r_wrapper->b;
if (!b) {
XSRETURN_YES;
}
result = marpa_b_free(b);
r_wrapper->b = 0;
if (result < 0) {
croak ("Problem in r->bocage_clear(): %s", error_r(r_wrapper));
}
XPUSHs( sv_2mortal( newSViv(result) ) );
}
{
const Marpa_Bocage b = b_wrapper->b;
marpa_b_unref(b);
g_free(b_wrapper->message_buffer);
Safefree( b_wrapper );
}

void
top_or_node( r_wrapper )
R_Wrapper *r_wrapper;
top_or_node( b_wrapper )
B_Wrapper *b_wrapper;
PPCODE:
{ Marpa_Bocage b = r_wrapper->b;
{
Marpa_Bocage b = b_wrapper->b;
gint result = marpa_b_top_or_node(b);
if (result < 0) {
croak ("Problem in r->top_or_node(): %s", error_r(r_wrapper));
croak ("Problem in r->top_or_node(): %s", error_b(b_wrapper));
}
XPUSHs( sv_2mortal( newSViv(result) ) );
}
Expand Down

0 comments on commit 74372f7

Please sign in to comment.