/** * compilerkit_concatenation_vlist_new: * @fn compilerkit_concatenation_vlist_new * @memberof CompilerKitConcatenation * Construct a CompilerKitConcatenation instance. * @pre All GObject* until the last one are all non NULL, and the last arg must be NULL. * @param GObject* Left side of concatenation * @param GObject* Right side of concatenation * @param ... Comma separated list of GObject*, terminated by a NULL param * @return A new CompilerKitConcatenation struct if concatenation is necessary otherwise return the single element or EmptySet. */ GObject *compilerkit_concatenation_vlist_new (GObject *left, GObject *right, ...) { GObject* first; GObject* second; va_list args; va_start(args,right); first = compilerkit_concatenation_new(left, right); while(second = va_arg(args, GObject*)) { first = compilerkit_concatenation_new(first, second); } va_end(args); return first; }
/** * compilerkit_times_new: * @fn compilerkit_times_new * * Match regex `k` times. * * @pre None * @param GObject* A regex. * @param guint The number of times it should match. * @return GObject* regex that matches the given regex `k` times. */ GObject *compilerkit_times_new(GObject *regex, guint k) { GObject *result = regex; guint i; for (i = 1; i < k; i++) result = compilerkit_concatenation_new (result, regex); return result; }
/** @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_times_extended_new: * @fn compilerkit_times_extended_new * * Match regex at least `k`, and at most `l` times. * * @pre None * @param GObject* A regex. * @param guint The minimum number of times it should match. * @param guint The maximum number of times it should match. * @return GObject* regex that matches the given regex `k` times. */ GObject *compilerkit_times_extended_new(GObject *regex, guint k, guint l) { GObject *result; result = compilerkit_times_new(regex, k); for (k++; k <= l; k++) result = compilerkit_concatenation_new (result, compilerkit_optional_new(regex)); return result; }
/** * 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); }
/** * 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_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); }
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_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); }
/** * compilerkit_postive_closure_new: * @fn compilerkit_postive_closure_new * * Constructs a positive closure (internally the equivalent CompilerKitConcatenation and CompilerKitKleeneStar). * * For example, `compilerkit_positive_closure_new(compilerkit_concatenation_new(compilerkit_symbol_new('a'),compilerkit_symbol_new('b')))` produces the regex `(ab)+`. * * @pre `regex` is not NULL. * @param GObject* `regex` The regex to match one or more times. * @return GObject* regex that matches the given regex one or more times. */ GObject *compilerkit_positive_closure_new (GObject *regex) { g_assert (regex); return compilerkit_concatenation_new (regex, compilerkit_kleene_star_new (regex)); }