/** * 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; }
/** * test_symbol_flyweight: * @fn test_symbol_unicode * Tests whether compilerkit_symbol_new in CompilerKitSymbol struct allocates new space only for unique symbols. * @pre None * @param None * @return void */ void test_symbol_flyweight (void) { CompilerKitSymbol *symbol1, *symbol2, *symbol3; g_test_message ("Testing Symbol unicode"); g_test_timer_start (); /** @todo Test here */ symbol1 = COMPILERKIT_SYMBOL (compilerkit_symbol_new('a')); symbol2 = COMPILERKIT_SYMBOL (compilerkit_symbol_new('b')); symbol3 = COMPILERKIT_SYMBOL (compilerkit_symbol_new('a')); /* Symbol b is distinct from Symbol a */ g_assert (symbol2 != symbol3); g_assert (symbol2 != symbol1); /* The pointer to Symbol('a') should equal the pointer to Symbol('a'). This of course, would make Ayn Rand proud ;-) */ g_assert (symbol1 == symbol3); g_object_unref(symbol1); g_object_unref(symbol2); g_object_unref(symbol3); g_assert_cmpfloat(g_test_timer_elapsed (), <=, 1); }
int main (int argc, char ** argv) { GObject *alternationOne; GObject *alternationTwo; GObject *left; GObject *right; GObject *left_left; GObject *left_right; GObject *subAlternation; g_type_init(); alternationOne = compilerkit_alternation_new(compilerkit_symbol_new('a'),compilerkit_symbol_new('b')); // creates an alternation ('a' | 'b') alternationTwo = compilerkit_alternation_vlist_new(compilerkit_symbol_new('c'), compilerkit_symbol_new('d'), compilerkit_symbol_new('e'), NULL); //NULL terminated list of at least 2 GObjects //creates an alternation (('c' | 'd') | 'e') left = compilerkit_alternation_get_left(alternationOne); //returns the symbol 'a' right = compilerkit_alternation_get_right(alternationOne); //returns the symbol 'b' subAlternation = compilerkit_alternation_get_left(alternationTwo); //returns the alternation ('c' | 'd') left_left = compilerkit_alternation_get_left(compilerkit_alternation_get_left(alternationTwo)); //returns the symbol 'c' left_right = compilerkit_alternation_get_right(compilerkit_alternation_get_left(alternationTwo)); //returns the symbol 'd' right = compilerkit_alternation_get_right(alternationTwo); //returns the symbol 'e' g_object_unref (alternationOne); //Also de-references the parts (left and right) g_object_unref (alternationTwo); //Also de-references the parts (subAlternation, left_left, left_right, and right) }
/** * test_alternation_vlist_new: * @fn test_alternation_vlist_new * Tests method compilerkit_alternation_vlist_new in CompilerKitAlternation struct. * @pre None * @param None * @return void */ void test_alternation_vlist_new (void) { GObject* alt; GObject* one; GObject* two; GObject* three; g_test_message ("Testing Alternation vlist new"); g_test_timer_start (); one = compilerkit_symbol_new('a'); two = compilerkit_symbol_new('b'); three = compilerkit_symbol_new('c'); alt = compilerkit_alternation_vlist_new(one, two, three, NULL); g_assert (COMPILERKIT_IS_ALTERNATION(alt)); g_assert (one != two); g_assert (two != three); g_assert (one != three); g_assert (one != alt); g_assert (two != alt); g_assert (three != alt); g_assert (three == compilerkit_alternation_get_right(COMPILERKIT_ALTERNATION(alt))); g_assert (one == compilerkit_alternation_get_left(COMPILERKIT_ALTERNATION(compilerkit_alternation_get_left(COMPILERKIT_ALTERNATION(alt))))); g_assert (two == compilerkit_alternation_get_right(COMPILERKIT_ALTERNATION(compilerkit_alternation_get_left(COMPILERKIT_ALTERNATION(alt))))); g_object_unref (alt); // This will unref one, two and three as well g_assert_cmpfloat(g_test_timer_elapsed (), <=, 1); }
/** * compilerkit_parens: * @fn compilerkit_parens * * Match an object surrounded by parentheses. * * @pre None * @param GObject* a regex to match. * @return GObject* that matches itself surrounded by parentheses */ GObject *compilerkit_parens(GObject *obj) { return compilerkit_concatenation_vlist_new ( compilerkit_symbol_new ('('), obj, compilerkit_symbol_new (')'), NULL ); }
/** @todo Describe what task main will accomplish. */ int main (int argc, char ** argv) { GObject *concatenation; g_type_init(); concatenation = compilerkit_concatenation_new(compilerkit_symbol_new('a'),compilerkit_symbol_new('b')); /** @todo Briefly show how to use the methods in CompilerKitConcatenation to accomplish the task. */ g_object_unref (concatenation); }
/** * compilerkit_string_concatenation: * @fn compilerkit_string_concatenation * * Return a concatenation based off of a string. * * @pre None * @param gchar* A UTF-8 encoded string. * @return GObject* regex that matches the string literally. */ GObject *compilerkit_string_concatenation(gchar *string) { GObject *result; g_utf8_validate(string, -1, NULL); result = compilerkit_symbol_new (g_utf8_get_char (string)); string = g_utf8_next_char(string); while (*string) { result = compilerkit_concatenation_new (result, compilerkit_symbol_new (g_utf8_get_char(string))); string = g_utf8_next_char(string); } return result; }
/** * 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); }
/** * 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); }
void test_positive_closure(void) { GObject *regex, *positive_closure; CompilerKitConcatenation *cat; CompilerKitKleeneStar *star; regex = compilerkit_concatenation_new (compilerkit_symbol_new('a'), compilerkit_symbol_new('b')); positive_closure = compilerkit_positive_closure_new (regex); g_assert (COMPILERKIT_IS_CONCATENATION(positive_closure)); cat = COMPILERKIT_CONCATENATION(positive_closure); g_assert (compilerkit_concatenation_get_left (cat) == regex); g_assert (COMPILERKIT_IS_KLEENE_STAR (compilerkit_concatenation_get_right (cat))); star = COMPILERKIT_KLEENE_STAR (compilerkit_concatenation_get_right (cat)); g_assert (compilerkit_kleene_star_get_node (star) == regex); }
/** * test_concatenation_constructor_empty_set: * @fn test_concatenation_constructor_empty_set * Tests compilerkit_concatenation_new in CompilerKitConcatenation struct when either side is an empty set. * @pre None * @param None * @return void */ void test_concatenation_constructor_empty_set (void) { GObject* ckc; GObject* left; GObject* right; g_test_message ("Testing concatenation constructor when either side is an empty set"); g_test_timer_start (); // Right parameter is EmptySet { left = compilerkit_symbol_new('a'); right = compilerkit_empty_set_get_instance (); ckc = compilerkit_concatenation_new(left,right); // Assert that ckc is the empty set, and that emptyset is a singleton. g_assert (right == ckc); g_assert(COMPILERKIT_IS_EMPTY_SET(ckc)); g_object_unref (left); g_object_unref (right); } // Left parameter is EmptySet { left = compilerkit_empty_set_get_instance(); right = compilerkit_symbol_new('a'); ckc = compilerkit_concatenation_new(left,right); // Assert that concatenation is returning the empty set here. g_assert (ckc == left); g_assert (COMPILERKIT_IS_EMPTY_SET(ckc)); 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); }
/** * 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); }
/** * test_alternation_constructor: * @fn test_alternation_constructor * Tests method compilerkit_alternation_new in CompilerKitAlternation struct. * @pre None * @param None * @return void */ void test_alternation_constructor (void) { GObject* alt; GObject* left; GObject* right; g_test_message ("Testing Alternation constructor"); g_test_timer_start (); left = compilerkit_symbol_new('a'); right = compilerkit_symbol_new('b'); alt = compilerkit_alternation_new(left, right); g_assert (COMPILERKIT_IS_ALTERNATION(alt)); g_assert (left != right); g_assert (left != alt); g_assert (right != alt); g_object_unref (alt); // This will unref left and right as well g_assert_cmpfloat(g_test_timer_elapsed (), <=, 1); }
/** * test_concatenation_constructor_normal: * @fn test_concatenation_constructor_normal * Tests compilerkit_concatenation_new in CompilerKitConcatenation struct when both sides are symbols. * @pre None * @param None * @return void */ void test_concatenation_constructor_normal (void) { GObject* ckc; GObject* left; GObject* right; g_test_message ("Testing concatenation constructor when both sides are symbols"); g_test_timer_start (); left = compilerkit_symbol_new('a'); right = compilerkit_symbol_new('b'); ckc = compilerkit_concatenation_new (left, right); g_assert(COMPILERKIT_IS_CONCATENATION(ckc)); g_assert (left != ckc); g_assert (right != ckc); g_object_unref (ckc); // This will unref left and right as well // This test shouldn't take too long to run 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); }
/** * test_symbol_unicode: * @fn test_symbol_unicode * Tests compilerkit_symbol_new in CompilerKitSymbol struct for the ability to deal with Unicode (specifically, UTF-8). * @pre None * @param None * @return void */ void test_symbol_unicode (void) { CompilerKitSymbol *symbol; g_test_message ("Testing Symbol unicode"); g_test_timer_start (); gunichar ch = g_utf8_get_char("台"); symbol = COMPILERKIT_SYMBOL (compilerkit_symbol_new(ch)); g_assert(compilerkit_symbol_get_symbol (symbol) == ch /*'台'*/ ); g_object_unref(symbol); g_assert_cmpfloat(g_test_timer_elapsed (), <=, 1); }
/** * 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); }