Пример #1
0
/**
 * compilerkit_character_class_new:
 * @fn compilerkit_character_class_new
 *
 * Constructs a character class object (internally the equivalent CompilerKitAlternation).
 * 
 * For example, `compilerkit_character_class_new('a','z')` produces the regex `[a-z]`.
 * `compilerkit_character_class_new('!','~')` produces the a regex to match all characters between ASCII '!' and '~' (this happens to include all Latin printable characters).
 * 
 * @pre None.
 * @param gunichar `lo` The low end of the character class. If this is `'\0'`, `lo` is the next higher character.
 * @param gunichar `hi` The high end of the character class. If this is `'\0'`, return the empty string.
 * @return GObject* a pointer to the new character class regex.
 */
GObject* compilerkit_character_class_new(gunichar lo, gunichar hi)
{
	GObject* newExpression;
    gunichar i;

    sort_chars (&lo, &hi);

    /* If they're the same, return the symbol instead. */
    if (lo == hi)
    {
        return compilerkit_symbol_new (lo);
    }

    /* Don't match NULL in the regex */
    if (lo == '\0')
    {
        lo++;
    }

    /* This must mean lo and hi are both NULL. Return EmptyString instead. */
    else if (hi == '\0')
    {
        return compilerkit_empty_string_get_instance();
    }
    
    newExpression = compilerkit_symbol_new(lo);
    for(i = lo+1; i <= hi; i++)
    {
        newExpression = compilerkit_alternation_new (newExpression, compilerkit_symbol_new(i));
    }
    return newExpression;
}
Пример #2
0
int main (int argc, char ** argv)
{
    CompilerKitEmptyString* empty_string;
    g_type_init();
    
    empty_string = compilerkit_empty_string_get_instance();
    
    g_object_unref (empty_string);
}
Пример #3
0
/**
 * test_concatenation_method:
 * @fn test_concatenation_method
 * Tests method compilerkit_concatenation_method in CompilerKitConcatenation struct.
 * @pre None
 * @param None
 * @return void
 */
void test_concatenation_constructor_empty_string (void)
{
	GObject* ckc;
	GObject* left;
	GObject* right;

    g_test_message ("Testing Concatenation constructor when either side is an empty string");
    g_test_timer_start ();

    /** @todo Test here  */

	// Right parameter is EmptyString
    {
        left = compilerkit_symbol_new('a');
        right = compilerkit_empty_string_get_instance ();
        ckc = compilerkit_concatenation_new(left,right);

        g_assert(COMPILERKIT_IS_SYMBOL(ckc));
        g_assert (left == ckc);

        g_object_unref (left);
        g_object_unref (right);
    }

	// Left parameter is EmptyString
    {
        left = compilerkit_empty_string_get_instance ();
        right = compilerkit_symbol_new('a');
        ckc = compilerkit_concatenation_new(left,right);

        g_assert(COMPILERKIT_IS_SYMBOL(ckc));
        g_assert (ckc == right);

        g_object_unref (left);
        g_object_unref (right);
    }

    // This test shouldn't take too long to run
    g_assert_cmpfloat(g_test_timer_elapsed (), <=, 1);
}
Пример #4
0
/**
 * test_kleene_star_constructor:
 * @fn test_kleene_star_constructor
 * Tests compilerkit_kleene_star_new in CompilerKitKleeneStar struct.
 * @pre None
 * @param None
 * @return void
 */
void test_kleene_star_constructor (void)
{
	GObject* e_set;
	GObject* e_string;
	GObject* a;
	GObject* result;
	
	
    g_test_message ("Testing KleeneStar method");
    g_test_timer_start ();
	
	
	//empty set
	{
	
		e_set = compilerkit_empty_set_get_instance();
		result = compilerkit_kleene_star_new(e_set);
		g_assert(COMPILERKIT_IS_EMPTY_SET (result));
        g_object_unref (result);
	}
	
	//empty string
	{
		e_string = compilerkit_empty_string_get_instance();
		result = compilerkit_kleene_star_new(e_string);
		g_assert(COMPILERKIT_IS_EMPTY_STRING (result));
        g_object_unref (result);
	}
	
	//symbol
	{
		a = compilerkit_symbol_new('a');
		result = compilerkit_kleene_star_new(a);
		g_assert(COMPILERKIT_IS_KLEENE_STAR (result));
        g_object_unref (result);
	}
    
    
    g_assert_cmpfloat(g_test_timer_elapsed (), <=, 1);
}
/**
 * test_derivative_visitor:
 * @fn test_derivative_visitor
 * Tests compilerkit_derivative_visitor.
 * @pre None
 * @param None
 * @return void
 */
void test_derivative_visitor (void)
{
    CompilerKitVisitor *derivative;
    GObject *regex, *new_regex;
	//GObject *symbol, *cat, *star;	/** @todo these are currently unused */
    
    g_test_message ("Testing Derivative visitor");
    g_test_timer_start ();
    
    derivative = compilerkit_derivative_visitor();
    regex = compilerkit_symbol_new ('h');
    
    // When we match a single character, h, against the single character 'h', we should get the empty string, because they match.
    new_regex = compilerkit_derivative_apply_char (derivative, regex, 'h');
    g_assert (new_regex == compilerkit_empty_string_get_instance());
    
    // When we match a single character, h, against the single character 'e', we should get the empty set, because they don't match.
    new_regex = compilerkit_derivative_apply_char (derivative, regex, 'e');
    g_assert (new_regex == compilerkit_empty_set_get_instance());
    
    // When we match a single character, h, against a string, we should get the empty set, because they don't match.
    new_regex = compilerkit_derivative_apply_string (derivative, regex, "hello");
    g_assert (new_regex == compilerkit_empty_set_get_instance());
    
    // When we match "hi!" against the string "hi", we should get the symbol "!", because that's what remains.
    regex = compilerkit_concatenation_new (regex, compilerkit_concatenation_new (compilerkit_symbol_new ('i'), compilerkit_symbol_new ('!')));
    new_regex = compilerkit_derivative_apply_string (derivative, regex, "hi");
//    g_warn_if_fail (g_type_name(G_OBJECT_TYPE(new_regex)));
//    g_assert (COMPILERKIT_IS_SYMBOL (new_regex));
//    g_assert (compilerkit_symbol_get_symbol(COMPILERKIT_SYMBOL(new_regex)) == '!');
    
    g_object_unref (derivative);
    g_object_unref (regex);

    // This test shouldn't take too long to run
    g_assert_cmpfloat(g_test_timer_elapsed (), <=, 1);
}
Пример #6
0
/**
 * compilerkit_optional_new:
 * @fn compilerkit_optional_new
 *
 * Return a regex corresponding to the original regex|empty string
 * 
 * @pre regex is not NULL
 * @param GObject* regex to make optional
 * @return GObject* Alternation of the regex with the empty string
 */
GObject *compilerkit_optional_new (GObject *regex)
{
    return compilerkit_alternation_new (regex, compilerkit_empty_string_get_instance());
}