lawrancej / CompilerKit

Compiler construction library in C.
GNU Lesser General Public License v2.1
55 stars 33 forks source link

Nice to have a convenience function for character classes #27

Closed lawrancej closed 12 years ago

lawrancej commented 12 years ago

Using the existing regular expression classes, write a function that converts two characters, lo and hi into a character class. For example, if lo=a and hi=z, then the character class to match is [a-z]. Using the existing regular expression classes, this becomes the alternation a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z.

GObject *compilerkit_character_class_new (gunichar lo, gunichar hi)

mueschm commented 12 years ago

MINE!

mueschm commented 12 years ago

Question about code. I have completed the code in question, though I am unsure as to where to put it. If you could tell me where to put this I will commit it and request a pull.

GObject* compilerkit_character_class_new(gunichar hi, gunichar lo)
{
    GObject* newExpression = G_OBJECT(compilerkit_symbol_new(lo));
    int i;
    for(i = lo+1;i <= hi;i++)
    {
        if(i == 58)
            i = 65;
        else if(i == 91)
            i = 97;

        newExpression = G_OBJECT(compilerkit_alternation_new(newExpression,G_OBJECT(compilerkit_symbol_new((gunichar)i))));
    }
    return newExpression;
}
lawrancej commented 12 years ago

Hmm.. How about we put this in a new file, src/convenience.cand have the corresponding include in include/CompilerKit/convenience.h?

Also, what are the if statements doing?

mueschm commented 12 years ago

Alright, sounds good. Thank you.

They allow you to perform actions like 0-h and have all values in between work.

lawrancej commented 12 years ago

Hi Mike! I merged in your changes with some revisions. Please look it over and let me know what you think!