Example #1
0
/**
 * test_visitor_null_visit:
 * @fn test_visitor_null_visit
 * Tests whether compilerkit_visitor_visit produces appropriate results in the CompilerKitVisitor struct.
 * @pre None
 * @param None
 * @return void
 */
void test_visitor_null_visit(void)
{
    GObject *symbol;
    GObject *empty_set;
    CompilerKitVisitor* visitor;

    g_test_message ("Testing visitor null visits");
    g_test_timer_start ();

    symbol = compilerkit_symbol_new ('a');
    empty_set = compilerkit_empty_set_get_instance ();
    visitor = check_symbol();

    // Assert that visitor produces the correct result
    g_assert(compilerkit_visitor_visit(visitor, symbol) == symbol);

    // NULL objects will produce NULL
    g_assert(compilerkit_visitor_visit(visitor, NULL) == NULL);

    // Nothing registered for empty set, so return NULL
    g_assert(compilerkit_visitor_visit(visitor, empty_set) == NULL);

    // Decrease the reference count for objects to free them.
    g_object_unref (symbol);
    g_object_unref (empty_set);
    g_object_unref (visitor);

    // This test shouldn't take too long to run
    g_assert_cmpfloat(g_test_timer_elapsed (), <=, 1);
}
Example #2
0
/**
 * test_visitor_register_identity:
 * @fn test_visitor_register_identity
 * Tests whether compilerkit_visitor_register_identity function works as intended.
 * @pre None
 * @param None
 * @return void
 */
void test_visitor_register_identity (void)
{
    GObject *symbol;
    GObject *empty_set;
    CompilerKitVisitor *visitor;

    g_test_message ("Testing visitor register identity");
    g_test_timer_start ();

    symbol    = compilerkit_symbol_new ('a');
    empty_set = compilerkit_empty_set_get_instance ();
    visitor   = compilerkit_visitor_new();

    // Register the identity function (it returns whatever GObject* gets as a parameter during the visit)
    compilerkit_visitor_register_identity (visitor, COMPILERKIT_TYPE_EMPTY_SET);

    // Since we didn't register the symbol, the visitor should return NULL
    g_assert (compilerkit_visitor_visit (visitor, symbol) == NULL);

    // The visitor should produce symbol here, since we registered the identity function for the symbol class.
    g_assert (compilerkit_visitor_visit (visitor, empty_set) == empty_set);

    // Decrease the reference count for objects to free them.
    g_object_unref (symbol);
    g_object_unref (empty_set);
    g_object_unref (visitor);

    // This test shouldn't take too long to run
    g_assert_cmpfloat(g_test_timer_elapsed (), <=, 1);
}
/* ToNfa concatenation. */
static GObject *to_nfa_concatenation (CompilerKitVisitor *self, GObject *obj)
{
    CompilerKitConcatenation *cat;
    g_assert(COMPILERKIT_IS_CONCATENATION(obj));
    
    cat = COMPILERKIT_CONCATENATION (obj);

    compilerkit_visitor_visit(self, compilerkit_concatenation_get_left  (cat));
    compilerkit_visitor_visit(self, compilerkit_concatenation_get_right (cat));

    return NULL;
}
/* ToNfa alternation. */
static GObject *to_nfa_alternation (CompilerKitVisitor *self, GObject *obj)
{
    CompilerKitAlternation *alt;
    g_assert(COMPILERKIT_IS_ALTERNATION(obj));
    
    alt = COMPILERKIT_ALTERNATION (obj);

    compilerkit_visitor_visit(self, compilerkit_alternation_get_left  (alt));
    compilerkit_visitor_visit(self, compilerkit_alternation_get_right (alt));

    return NULL;
}
/* StringBuilder alternation. */
static GObject *string_builder_alternation (CompilerKitVisitor *self, GObject *obj)
{
    CompilerKitAlternation *alt;
    GString *str = (GString *) compilerkit_visitor_get_state(self);
    g_assert(COMPILERKIT_IS_ALTERNATION(obj));
    
    alt = COMPILERKIT_ALTERNATION (obj);

    compilerkit_visitor_visit(self, compilerkit_alternation_get_left  (alt));
    g_string_append_c(str, '|');
    compilerkit_visitor_visit(self, compilerkit_alternation_get_right (alt));

    return NULL;
}
/* ToNfa complement. */
static GObject *to_nfa_complement (CompilerKitVisitor *self, GObject *obj)
{
    CompilerKitComplement *comp;
    g_assert(COMPILERKIT_IS_COMPLEMENT(obj));
    
    comp = COMPILERKIT_COMPLEMENT (obj);
    
    compilerkit_visitor_visit(self, compilerkit_complement_get_node (comp));

    return NULL;
}
/* ToNfa Kleene star. */
static GObject *to_nfa_kleene_star (CompilerKitVisitor *self, GObject *obj)
{
    CompilerKitKleeneStar *star;
    g_assert(COMPILERKIT_IS_KLEENE_STAR(obj));
    
    star = COMPILERKIT_KLEENE_STAR (obj);
    
    compilerkit_visitor_visit(self, compilerkit_kleene_star_get_node (star));

    return NULL;
}
/**
 * compilerkit_to_string:
 * @fn compilerkit_to_string
 * Return a string representation of an object (i.e., regex, grammar).
 * @pre None
 * @param GObject* An object
 * @return A string representation of the object. The caller must free the returned string with `g_free()`
 * @memberof CompilerKitVisitor
 */
gchar *compilerkit_to_string (GObject *obj)
{
    GString *str;
    CompilerKitVisitor *visitor = compilerkit_string_builder_visitor();
    
    compilerkit_visitor_visit(visitor, obj);
    
    str = (GString *) compilerkit_visitor_get_state(visitor);
    g_object_unref (visitor);
    
    return g_string_free (str, FALSE);
}
/* StringBuilder production. */
static GObject *string_builder_production (CompilerKitVisitor *self, GObject *obj)
{
    GString *str = (GString *) compilerkit_visitor_get_state(self);
    CompilerKitProduction *production;
    GList *replacement;
    
    g_assert(COMPILERKIT_IS_PRODUCTION(obj));
    
    production = COMPILERKIT_PRODUCTION (obj);

    compilerkit_visitor_visit (self, compilerkit_production_get_variable(production));
    g_string_append (str, " -> ");
    replacement = compilerkit_production_get_replacement(production);
    while (replacement)
    {
        compilerkit_visitor_visit (self, (GObject *)replacement->data);
        replacement = g_list_next (replacement);
    }
    g_string_append (str, "\n");
    return NULL;
}
/* StringBuilder complement. */
static GObject *string_builder_complement (CompilerKitVisitor *self, GObject *obj)
{
    CompilerKitComplement *comp;
    GString *str = (GString *) compilerkit_visitor_get_state(self);

    g_assert(COMPILERKIT_IS_COMPLEMENT(obj));
    
    comp = COMPILERKIT_COMPLEMENT (obj);
    
    g_string_append_c (str, '!');
    compilerkit_visitor_visit(self, compilerkit_complement_get_node (comp));

    return NULL;
}
/* StringBuilder Kleene star. */
static GObject *string_builder_kleene_star (CompilerKitVisitor *self, GObject *obj)
{
    CompilerKitKleeneStar *star;
    GString *str = (GString *) compilerkit_visitor_get_state(self);

    g_assert(COMPILERKIT_IS_KLEENE_STAR(obj));
    
    star = COMPILERKIT_KLEENE_STAR (obj);
    
    g_string_append_c (str, '(');
    compilerkit_visitor_visit(self, compilerkit_kleene_star_get_node (star));
    g_string_append (str, ")*");

    return NULL;
}
/* StringBuilder grammar. */
static GObject *string_builder_grammar (CompilerKitVisitor *self, GObject *obj)
{
    GString *str = (GString *) compilerkit_visitor_get_state(self);
    GList *productions;
    CompilerKitGrammar *grammar;
    
    g_assert(COMPILERKIT_IS_GRAMMAR(obj));
    
    grammar = COMPILERKIT_GRAMMAR (obj);
    productions = compilerkit_grammar_productions (grammar);
    
    while (productions)
    {
        compilerkit_visitor_visit (self, productions->data);
        productions = g_list_next (productions);
    }
    return NULL;
}