Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Metaobject regex
package metaobjectTest
@doc{*
usage:
String@regex(re)
regex checks if the variable holds only strings that match the
regular expression re.
*}
object Regex
func run {
var String@regex("a*b[A-Z]") ab;
var String s;
ab = "bB"; // ok
ab = "aaaaabC"; // ok
// ab = "a26"; // compile-time error
var Boolean ok = true;
s = "bB";
{
ab = s;
} catch: { (: ExceptionStr e :) ok = false };
assert ok;
ok = true;
s = """aaaaabC""";
{
ab = s;
} catch: { (: ExceptionStr e :) ok = false; };
assert ok;
ok = false;
s = "abAZ";
{
ab = s; // fails at runtime
} catch: { (: ExceptionStr e :) ok = true; };
assert ok;
ok = false;
s = "aaaa";
{
ab = s; // fails at runtime
} catch: { (: ExceptionStr e :) ok = true };
assert ok;
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment